diff --git a/paddle/fluid/framework/attribute.h b/paddle/fluid/framework/attribute.h index f648f8b60b04880f70c3bf001fb07956052e16ed..56a4030be3be1facf6c35ea108545bb0515212dc 100644 --- a/paddle/fluid/framework/attribute.h +++ b/paddle/fluid/framework/attribute.h @@ -339,9 +339,11 @@ class OpAttrChecker { return *(checker.target>()); } - void Check(AttributeMap* attr_map) const { - for (const auto& checker : attr_checkers_) { - checker(attr_map, false); + void Check(AttributeMap* attr_map, bool explicit_only = false) const { + 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); } } @@ -353,8 +355,21 @@ class OpAttrChecker { return default_values_map; } + void RecordExplicitCheckerNum() { + explicit_checker_num_ = attr_checkers_.size(); + } + private: std::vector 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 diff --git a/paddle/fluid/framework/op_proto_maker.cc b/paddle/fluid/framework/op_proto_maker.cc index 37f66a9692a8e6636dbe40008e0fa935000ff7e9..3408ab262c16197b92e407d0af6043c8a062b5d4 100644 --- a/paddle/fluid/framework/op_proto_maker.cc +++ b/paddle/fluid/framework/op_proto_maker.cc @@ -62,6 +62,7 @@ void OpProtoAndCheckerMaker::operator()(proto::OpProto* proto, proto_ = proto; op_checker_ = attr_checker; Make(); + op_checker_->RecordExplicitCheckerNum(); AddAttr(OpRoleAttrName(), "The role of this operator") .InEnum( diff --git a/paddle/fluid/imperative/layer.cc b/paddle/fluid/imperative/layer.cc index 398b2e56ad69e9c119210463745df25db719f7bd..6aa494d8e34759bdfe2d779489b0a6132d19519c 100644 --- a/paddle/fluid/imperative/layer.cc +++ b/paddle/fluid/imperative/layer.cc @@ -290,7 +290,7 @@ OpBase::OpBase(size_t id, const std::string& type, const NameVarBaseMap& ins, // Step 1: Run forward if (info.Checker() != nullptr) { - info.Checker()->Check(&attrs_); + info.Checker()->Check(&attrs_, true); } op_ = framework::OpRegistry::CreateOp(type, {}, {}, {}, false); @@ -301,7 +301,7 @@ OpBase::OpBase(size_t id, const std::string& type, const NameVarBaseMap& ins, void OpBase::CreateOperatorBase() { const auto& info = framework::OpInfoMap::Instance().Get(type_); if (info.Checker() != nullptr) { - info.Checker()->Check(&attrs_); + info.Checker()->Check(&attrs_, true); } op_ = framework::OpRegistry::CreateOp(type_, {}, {}, {}, false); }