diff --git a/paddle/framework/details/op_registry.h b/paddle/framework/details/op_registry.h index 357ad21f39f3b1f6dbdb98063f8fb24ec6800ec6..b731840ef2a4b2d5d82b019d28ad6517fa4b7607 100644 --- a/paddle/framework/details/op_registry.h +++ b/paddle/framework/details/op_registry.h @@ -28,7 +28,8 @@ enum OpInfoFillType { kOperator = 0, kOpProtoAndCheckerMaker = 1, kGradOpDescMaker = 2, - kVarTypeInference = 3 + kVarTypeInference = 3, + kShapeInference = 4 }; template @@ -42,7 +43,10 @@ struct OpInfoFillTypeID { ? kGradOpDescMaker : (std::is_base_of::value ? kVarTypeInference - : static_cast(-1)))); + : (std::is_base_of::value + ? kShapeInference + : static_cast( + -1))))); } }; @@ -121,6 +125,16 @@ struct OpInfoFiller { } }; +template +struct OpInfoFiller { + void operator()(const char* op_type, OpInfo* info) const { + info->infer_shape_ = [](InferShapeContext* ctx) { + T inference; + inference(ctx); + }; + } +}; + } // namespace details } // namespace framework diff --git a/paddle/framework/op_desc.cc b/paddle/framework/op_desc.cc index 3bea675033c163e1a936a01538d6046a90ab5dd6..133869e7b58dd2082bd6e099351609f7ed37e96a 100644 --- a/paddle/framework/op_desc.cc +++ b/paddle/framework/op_desc.cc @@ -14,6 +14,7 @@ limitations under the License. */ #include "paddle/framework/op_desc.h" #include +#include #include #include "paddle/framework/block_desc.h" #include "paddle/framework/operator.h" @@ -229,26 +230,26 @@ void OpDescBind::Flush() { } } -using InferShapeFuncMap = - std::unordered_map>; - -static InferShapeFuncMap &InferShapeFuncs() { - static InferShapeFuncMap *g_map = nullptr; - if (g_map == nullptr) { - g_map = new InferShapeFuncMap(); - auto &info_map = OpInfoMap::Instance(); - // all registered kernels - for (auto &pair : OperatorWithKernel::AllOpKernels()) { - auto &info = info_map.Get(pair.first); - // use empty type here to avoid runtime checks. +static std::once_flag init_infer_shape_funcs; + +static void InitInferShapeFuncs() { + std::call_once(init_infer_shape_funcs, [] { + auto &map = OpInfoMap::Instance(); + auto &info_map = *map.mutable_map(); + + for (auto &kern_pair : OperatorWithKernel::AllOpKernels()) { + auto op_type = kern_pair.first; + auto &op_info = info_map.at(op_type); auto op = - static_cast(info.Creator()("", {}, {}, {})); - g_map->insert( - {pair.first, [op](InferShapeContext *ctx) { op->InferShape(ctx); }}); + static_cast(op_info.Creator()("", {}, {}, {})); + if (op_info.infer_shape_) { // infer_shape has been registered. + continue; + } + op_info.infer_shape_ = [op](InferShapeContext *ctx) { + op->InferShape(ctx); + }; } - } - return *g_map; + }); } void OpDescBind::CheckAttrs() { @@ -265,13 +266,12 @@ void OpDescBind::CheckAttrs() { void OpDescBind::InferShape(const BlockDescBind &block) const { VLOG(3) << "CompileTime infer shape on " << Type(); - auto &funcs = InferShapeFuncs(); - auto it = funcs.find(this->Type()); - if (it == funcs.end()) { - PADDLE_THROW("Operator %s has not been registered", this->Type()); - } + InitInferShapeFuncs(); + auto &infer_shape = OpInfoMap::Instance().Get(this->Type()).infer_shape_; + PADDLE_ENFORCE(static_cast(infer_shape), + "%s's infer_shape has not been registered", this->Type()); CompileTimeInferShapeContext ctx(*this, block); - it->second(&ctx); + infer_shape(&ctx); } void OpDescBind::InferVarType(BlockDescBind *block) const { diff --git a/paddle/framework/op_info.h b/paddle/framework/op_info.h index 59a64d71371b546f76eabdeed7e7514e8fb0f84a..d3b1a3b5fa2cf8f6a9571e92a319f3757666657e 100644 --- a/paddle/framework/op_info.h +++ b/paddle/framework/op_info.h @@ -25,12 +25,19 @@ namespace paddle { namespace framework { +class InferShapeBase { + public: + virtual ~InferShapeBase() = default; + virtual void operator()(InferShapeContext*) const = 0; +}; + struct OpInfo { OpCreator creator_; GradOpMakerFN grad_op_maker_; OpProto* proto_{nullptr}; OpAttrChecker* checker_{nullptr}; InferVarTypeFN infer_var_type_; + InferShapeFN infer_shape_; bool HasOpProtoAndChecker() const { return proto_ != nullptr && checker_ != nullptr; @@ -87,13 +94,13 @@ class OpInfoMap { } } - const std::unordered_map& map() const { - return map_; - } + const std::unordered_map& map() const { return map_; } + + std::unordered_map* mutable_map() { return &map_; } private: OpInfoMap() = default; - std::unordered_map map_; + std::unordered_map map_; DISABLE_COPY_AND_ASSIGN(OpInfoMap); }; diff --git a/paddle/framework/operator.h b/paddle/framework/operator.h index 5177c2f21982945dc7ca82307781cdeaf7abe960..aa79f16df82ab9d81e093af60b730d9aacd09568 100644 --- a/paddle/framework/operator.h +++ b/paddle/framework/operator.h @@ -636,7 +636,9 @@ class OperatorWithKernel : public OperatorBase { }); } - virtual void InferShape(InferShapeContext* ctx) const = 0; + virtual void InferShape(InferShapeContext* ctx) const { + OpInfoMap::Instance().Get(Type()).infer_shape_(ctx); + } protected: // indicate kernel DataType by input data. Defaultly all input data must be diff --git a/paddle/framework/type_defs.h b/paddle/framework/type_defs.h index 00da7289394cf18e013220a4bedde2c182f6a4a4..c38c4a8ae9a46c8bda913e7643e812592de68e6e 100644 --- a/paddle/framework/type_defs.h +++ b/paddle/framework/type_defs.h @@ -28,6 +28,8 @@ class OperatorBase; class OpDescBind; class BlockDescBind; class BlockDesc; +class InferShapeContext; + using VariableNameMap = std::map>; // The order should be as same as framework.proto @@ -49,5 +51,7 @@ using GradOpMakerFN = std::function>( using InferVarTypeFN = std::function; +using InferShapeFN = std::function; + } // namespace framework } // namespace paddle diff --git a/paddle/operators/mul_op.cc b/paddle/operators/mul_op.cc index b9b9cd7ca05b4373c27f672cc1ee20daab6827a8..245d3b47d3a6331a3cf20dbdbd972639d68cd496 100644 --- a/paddle/operators/mul_op.cc +++ b/paddle/operators/mul_op.cc @@ -19,11 +19,9 @@ namespace operators { using framework::Tensor; -class MulOp : public framework::OperatorWithKernel { +class MulOpShapeInference : public framework::InferShapeBase { public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext* ctx) const override { + void operator()(framework::InferShapeContext* ctx) const override { PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of MulOp should not be null."); PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) of MulOp should not be null."); PADDLE_ENFORCE(ctx->HasOutput("Out"), @@ -137,7 +135,10 @@ class MulOpGrad : public framework::OperatorWithKernel { } // namespace paddle namespace ops = paddle::operators; -REGISTER_OP(mul, ops::MulOp, ops::MulOpMaker, mul_grad, ops::MulOpGrad); +REGISTER_OPERATOR(mul, paddle::framework::OperatorWithKernel, ops::MulOpMaker, + ops::MulOpShapeInference, + paddle::framework::DefaultGradOpDescMaker); +REGISTER_OPERATOR(mul_grad, ops::MulOpGrad); REGISTER_OP_CPU_KERNEL(mul, ops::MulKernel); REGISTER_OP_CPU_KERNEL(mul_grad, ops::MulGradKernel);