From 5191e54494ac26de7f84287d66630e0722874417 Mon Sep 17 00:00:00 2001 From: hong <43953930+phlrain@users.noreply.github.com> Date: Thu, 5 Mar 2020 11:36:05 +0800 Subject: [PATCH] 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 --- paddle/fluid/framework/attribute.h | 21 ++++++++++++++++++--- paddle/fluid/framework/op_proto_maker.cc | 1 + paddle/fluid/imperative/layer.cc | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/framework/attribute.h b/paddle/fluid/framework/attribute.h index f648f8b60b0..56a4030be3b 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 37f66a9692a..3408ab262c1 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 398b2e56ad6..6aa494d8e34 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); } -- GitLab