未验证 提交 5191e544 编写于 作者: H hong 提交者: GitHub

reduce default attrs for dynamic graph (#22850)

* reduce default attrs for dynamic graph, test=develop

* add some explanations for explicit attr, test=develop

* tweak explicit attr comments, test=develop
上级 aca3f531
...@@ -339,9 +339,11 @@ class OpAttrChecker { ...@@ -339,9 +339,11 @@ class OpAttrChecker {
return *(checker.target<TypedAttrChecker<T>>()); return *(checker.target<TypedAttrChecker<T>>());
} }
void Check(AttributeMap* attr_map) const { void Check(AttributeMap* attr_map, bool explicit_only = false) const {
for (const auto& checker : attr_checkers_) { auto checker_num = attr_checkers_.size();
checker(attr_map, false); if (explicit_only) checker_num = explicit_checker_num_;
for (size_t i = 0; i < checker_num; ++i) {
attr_checkers_[i](attr_map, false);
} }
} }
...@@ -353,8 +355,21 @@ class OpAttrChecker { ...@@ -353,8 +355,21 @@ class OpAttrChecker {
return default_values_map; return default_values_map;
} }
void RecordExplicitCheckerNum() {
explicit_checker_num_ = attr_checkers_.size();
}
private: private:
std::vector<AttrChecker> attr_checkers_; std::vector<AttrChecker> attr_checkers_;
// in order to improve the efficiency of dynamic graph mode,
// we divede the attribute into explicit type and implicit type.
// for explicit attribute, we mean the attribute added in the customized
// op makers, usually it's defined in the overloaded Make method.
// for implicit attribute, we mean the attribute added outside of the Make
// method like "op_role", "op_role_var", and they are useless in dynamic graph
// mode
size_t explicit_checker_num_;
}; };
} // namespace framework } // namespace framework
......
...@@ -62,6 +62,7 @@ void OpProtoAndCheckerMaker::operator()(proto::OpProto* proto, ...@@ -62,6 +62,7 @@ void OpProtoAndCheckerMaker::operator()(proto::OpProto* proto,
proto_ = proto; proto_ = proto;
op_checker_ = attr_checker; op_checker_ = attr_checker;
Make(); Make();
op_checker_->RecordExplicitCheckerNum();
AddAttr<int>(OpRoleAttrName(), "The role of this operator") AddAttr<int>(OpRoleAttrName(), "The role of this operator")
.InEnum( .InEnum(
......
...@@ -290,7 +290,7 @@ OpBase::OpBase(size_t id, const std::string& type, const NameVarBaseMap& ins, ...@@ -290,7 +290,7 @@ OpBase::OpBase(size_t id, const std::string& type, const NameVarBaseMap& ins,
// Step 1: Run forward // Step 1: Run forward
if (info.Checker() != nullptr) { if (info.Checker() != nullptr) {
info.Checker()->Check(&attrs_); info.Checker()->Check(&attrs_, true);
} }
op_ = framework::OpRegistry::CreateOp(type, {}, {}, {}, false); op_ = framework::OpRegistry::CreateOp(type, {}, {}, {}, false);
...@@ -301,7 +301,7 @@ OpBase::OpBase(size_t id, const std::string& type, const NameVarBaseMap& ins, ...@@ -301,7 +301,7 @@ OpBase::OpBase(size_t id, const std::string& type, const NameVarBaseMap& ins,
void OpBase::CreateOperatorBase() { void OpBase::CreateOperatorBase() {
const auto& info = framework::OpInfoMap::Instance().Get(type_); const auto& info = framework::OpInfoMap::Instance().Get(type_);
if (info.Checker() != nullptr) { if (info.Checker() != nullptr) {
info.Checker()->Check(&attrs_); info.Checker()->Check(&attrs_, true);
} }
op_ = framework::OpRegistry::CreateOp(type_, {}, {}, {}, false); op_ = framework::OpRegistry::CreateOp(type_, {}, {}, {}, false);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册