-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimize attr default value #33357
optimize attr default value #33357
Conversation
Thanks for your contribution! |
paddle/fluid/imperative/tracer.cc
Outdated
@@ -181,7 +181,8 @@ void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins, | |||
#endif | |||
} | |||
|
|||
OpBase::Run(*op, new_ins, outs, attrs, place); | |||
OpBase::Run(*op, new_ins, outs, attrs, attr_checker->GetAttrDefaultMap(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attr_checker 需要判断是不是存在null的情况
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改,感谢!
Sorry to inform you that d44aeb6's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually. |
paddle/fluid/framework/attribute.h
Outdated
if (it == attrs_.end()) { | ||
if (attrs_default_ != nullptr) { | ||
it = attrs_default_->find(name); | ||
if (it == attrs_default_->end()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bool found = false;
if ( it == attrs_ends() )
{
if (attrs_default_ != nullptr) {
it = attrs_default_->find(name);
if (it == attrs_default_->end())
{
found = false;
}
}
PADDLE_ENFORE( found == true, "")
paddle/fluid/framework/attribute.h
Outdated
platform::errors::InvalidArgument( | ||
"Attribute (%s) is not set correctly.", attr_name_)); | ||
// default_value_setter_ has no more than one element | ||
attr_map->emplace(attr_name_, default_value_setter_[0]()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto res = attr_map->emplace(attr_name_, default_value_setter_0);
it = res.first;
emplace 之后会返回一个pair, 这个pair的第一个元素是新的iterator
paddle/fluid/framework/attribute.h
Outdated
// default_value_setter_ has no more than one element | ||
attr_map->emplace(attr_name_, default_value_setter_[0]()); | ||
} | ||
it = attr_map->find(attr_name_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有了上面的操作后,这个find可以省略
@@ -93,6 +93,8 @@ void OpProtoAndCheckerMaker::operator()(proto::OpProto* proto, | |||
AddAttr<std::string>(OpDeviceAttrName(), "Device type of this operator.") | |||
.SetDefault(""); | |||
Validate(); | |||
|
|||
op_checker_->InitDefaultMap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个Init可以放在68行后面,后面这些信息动态图都是不需要的
paddle/fluid/imperative/layer.h
Outdated
@@ -108,7 +108,7 @@ class VarBase { | |||
|
|||
void ClearGradVarBase() { grad_var_ = nullptr; } | |||
|
|||
void SetGradVarBase(VarBase& grad_var) { | |||
void SetGradVarBase(VarBase& grad_var) { // NOLINT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用const &或者*,避免NOLINT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
个人觉得,DefaultAttr相比AttrDefault更符合英语的修饰习惯
paddle/fluid/framework/attribute.h
Outdated
explicit AttrReader(const AttributeMap& attrs) | ||
: attrs_(attrs), attrs_default_(nullptr) {} | ||
|
||
AttrReader(const AttributeMap& attrs, const AttributeMap& attrs_default) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
命名有些奇怪,建议attrs_default
-> default_attrs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同感
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
paddle/fluid/framework/attribute.h
Outdated
@@ -410,15 +435,26 @@ class OpAttrChecker { | |||
explicit_checker_num_ = attr_checkers_.size(); | |||
} | |||
|
|||
void InitDefaultMap() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InitDefaultMap意义不明,InitDefaultMap -> InitDefaultAttributeMap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
paddle/fluid/framework/attribute.h
Outdated
@@ -410,15 +435,26 @@ class OpAttrChecker { | |||
explicit_checker_num_ = attr_checkers_.size(); | |||
} | |||
|
|||
void InitDefaultMap() { | |||
for (const auto& checker : attr_checkers_) { | |||
checker(&attrs_default_, true, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attrs_default_ -> default_attrs_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
paddle/fluid/framework/attribute.h
Outdated
} | ||
} | ||
|
||
const AttributeMap& GetAttrDefaultMap() const { return attrs_default_; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetAttrDefaultMap -> GetDefaultAttrMap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
@@ -113,9 +113,18 @@ class GradOpBaseMakerBase { | |||
return vec_temp; | |||
} | |||
|
|||
// Only for dygraph | |||
void SetDygraphAttrsDefaultMap(const framework::AttributeMap& attrs_default) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SetDygraphAttrsDefaultMap -> SetDygraphDefaultAttrsMap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
it = attrs_default_.find(name); | ||
if (it == attrs_default_.end()) { | ||
PADDLE_THROW( | ||
platform::errors::NotFound("Can not find [%s] in attrs", name)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
paddle/fluid/imperative/layer.h
Outdated
@@ -108,7 +108,7 @@ class VarBase { | |||
|
|||
void ClearGradVarBase() { grad_var_ = nullptr; } | |||
|
|||
void SetGradVarBase(VarBase& grad_var) { | |||
void SetGradVarBase(VarBase& grad_var) { // NOLINT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用const &或者*,避免NOLINT
paddle/fluid/framework/attribute.h
Outdated
ExtractAttribute<T> extract_attr(name); | ||
T* attr_value = extract_attr(attr); | ||
return *attr_value; | ||
} | ||
|
||
private: | ||
const AttributeMap& attrs_; | ||
const AttributeMap* attrs_default_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么不同样使用const &类型,这里有什么考虑吗
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同感
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
引用必须在构造函数的时候初始化。考虑到动态图静态图都使用AttrReader他的构造函数有2种:
AttrReader(const AttributeMap& attrs)
: attrs_(attrs), default_attrs_(nullptr) {}
AttrReader(const AttributeMap& attrs, const AttributeMap& default_attrs)
: attrs_(attrs), default_attrs_(&default_attrs) {}
paddle/fluid/imperative/tracer.cc
Outdated
@@ -154,9 +154,14 @@ void Tracer::TraceOp(const std::string& type, const NameVarBaseMap& ins, | |||
const auto& op_info = op->Info(); | |||
auto* attr_checker = op_info.Checker(); | |||
if (attr_checker) { | |||
attr_checker->Check(&attrs, true); | |||
attr_checker->Check(&attrs, true, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议这里加上参数名注释,方便找到是这里调用的,attr_checker->Check(&attrs, true, /*without_default_value=*/true);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
paddle/fluid/framework/attribute.h
Outdated
void operator()(AttributeMap* attr_map, | ||
bool get_default_value_only = false) const { | ||
void operator()(AttributeMap* attr_map, bool get_default_value_only = false, | ||
bool without_default_value = false) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without_default_value这个参数的命名理解起来比较困难,是不是改成only_check_exist_value之类的比较容易理解
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without_default_value
让人有点儿容易和get_default_value_only
的作用混淆....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some comments
paddle/fluid/framework/attribute.h
Outdated
explicit AttrReader(const AttributeMap& attrs) | ||
: attrs_(attrs), attrs_default_(nullptr) {} | ||
|
||
AttrReader(const AttributeMap& attrs, const AttributeMap& attrs_default) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同感
paddle/fluid/framework/attribute.h
Outdated
PADDLE_ENFORCE_NE(attrs_.count(name), 0, | ||
auto it = attrs_.find(name); | ||
bool found = it != attrs_.end(); | ||
if (it == attrs_.end()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use found
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done,thx!
paddle/fluid/framework/attribute.h
Outdated
ExtractAttribute<T> extract_attr(name); | ||
T* attr_value = extract_attr(attr); | ||
return *attr_value; | ||
} | ||
|
||
private: | ||
const AttributeMap& attrs_; | ||
const AttributeMap* attrs_default_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同感
paddle/fluid/framework/attribute.h
Outdated
void operator()(AttributeMap* attr_map, | ||
bool get_default_value_only = false) const { | ||
void operator()(AttributeMap* attr_map, bool get_default_value_only = false, | ||
bool without_default_value = false) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without_default_value
让人有点儿容易和get_default_value_only
的作用混淆....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
paddle/fluid/framework/attribute.h
Outdated
auto checker_num = attr_checkers_.size(); | ||
if (explicit_only) checker_num = explicit_checker_num_; | ||
for (size_t i = 0; i < checker_num; ++i) { | ||
attr_checkers_[i](attr_map, false); | ||
attr_checkers_[i](attr_map, false, only_check_exist_value); | ||
} | ||
} | ||
|
||
AttributeMap GetAttrsDefaultValuesMap() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetDefaultAttrsValuesMap()?
@@ -228,6 +241,7 @@ class SingleGradOpMaker<imperative::OpBase> | |||
{ | |||
imperative::TracedGradOp traced_grad_op(node); | |||
try { | |||
traced_grad_op.SetAttrDefaultMap(this->DefaultAttrsMap()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SetDefaultAttrMap?
@@ -285,6 +295,10 @@ class TracedGradOp { | |||
return op_->SetAttrMap(attrs); | |||
} | |||
|
|||
void SetAttrDefaultMap(const framework::AttributeMap& attrs) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SetDefaultAttrMap?
@@ -35,10 +35,12 @@ class DygraphInferShapeContext : public framework::InferShapeContext { | |||
DygraphInferShapeContext(const NameVarMap<VarType>* in, | |||
const NameVarMap<VarType>* out, | |||
const framework::AttributeMap* attr, | |||
const framework::AttributeMap* attr_default, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
806564c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR types
Performance optimization
PR changes
Others
Describe
1. 背景
每个OP有很多的Attribute,这些Attribute在OP Maker中设置默认值和Checker。在上层使用时,通常只传递少部分Attribute,而更多的,需要靠Checker补填默认值。这部分逻辑在
TypedAttrChecker
中。而Attribute的存储方式是
using AttributeMap = std::unordered_map<std::string, Attribute>;
,经测试发现,这种数据结构的补填、拷贝都非常慢。在动态图中,直接增加了框架的调度成本。2. 内容
本PR优化Attribute补填默认值带来的性能开销。
主要方法是: 不再向上层传入的AttrsMap中补填默认值。而是在注册前向OP时生成DefaultAttrMap,DefaultAttrMap用于存储OP的默认值。在传递AttrsMap时,伴随传递DefaultAttrMap,在查询Attr时,先从AttrsMap中查找,再从DefaultAttrMap中查找。对于GradOP和DoubleGradOP,其Attr为前向OP的Attr,因此在执行dygraph_grad_op_maker_时,不仅传入AttrsMap,也传入DefaultAttrMap,最终存储在OPBase中。
3. 性能测试
使用core.ops.elementwise_add测试,本PR对core.ops.elementwise_add有18.93%的性能提升。