diff --git a/paddle/fluid/eager/custom_operator/custom_operator_node.cc b/paddle/fluid/eager/custom_operator/custom_operator_node.cc index c99f524b246a7c74bb2fef904c295f1f91942471..81b5437afc3a6e515ff7fc874a7709042e03eb9f 100644 --- a/paddle/fluid/eager/custom_operator/custom_operator_node.cc +++ b/paddle/fluid/eager/custom_operator/custom_operator_node.cc @@ -15,7 +15,6 @@ #include "paddle/fluid/eager/custom_operator/custom_operator_node.h" #include "paddle/fluid/framework/custom_operator.h" -#include "paddle/fluid/framework/op_meta_info_helper.h" #include "paddle/fluid/platform/profiler/event_tracing.h" #include "paddle/phi/api/ext/op_meta_info.h" #include "paddle/phi/core/dense_tensor.h" @@ -34,17 +33,12 @@ static void ConstructFwdAndBwdMap( } VLOG(7) << "Construct DoubleGrad's CustomEdgesSlotMap "; - auto inputs_names = - paddle::framework::OpMetaInfoHelper::GetInputs(vec_map[1]); - auto outputs_names = - paddle::framework::OpMetaInfoHelper::GetOutputs(vec_map[1]); - auto attrs_names = paddle::framework::OpMetaInfoHelper::GetAttrs(vec_map[1]); - auto grad_outputs_names = - paddle::framework::OpMetaInfoHelper::GetOutputs(vec_map[2]); - auto grad_inputs_names = - paddle::framework::OpMetaInfoHelper::GetInputs(vec_map[2]); - auto grad_attrs_names = - paddle::framework::OpMetaInfoHelper::GetAttrs(vec_map[2]); + auto inputs_names = paddle::OpMetaInfoHelper::GetInputs(vec_map[1]); + auto outputs_names = paddle::OpMetaInfoHelper::GetOutputs(vec_map[1]); + auto attrs_names = paddle::OpMetaInfoHelper::GetAttrs(vec_map[1]); + auto grad_outputs_names = paddle::OpMetaInfoHelper::GetOutputs(vec_map[2]); + auto grad_inputs_names = paddle::OpMetaInfoHelper::GetInputs(vec_map[2]); + auto grad_attrs_names = paddle::OpMetaInfoHelper::GetAttrs(vec_map[2]); std::vector> res(5); in_out_map[op_type].push_back(res); // Prepare pos map for grad_outputs @@ -170,13 +164,12 @@ RunCustomOpNode::operator()(paddle::small_vector, bool create_graph, bool is_new_grad) { // NOLINT paddle::CustomOpKernelContext ctx; - auto grad_inputs_name = paddle::framework::OpMetaInfoHelper::GetInputs( + auto grad_inputs_name = paddle::OpMetaInfoHelper::GetInputs( egr::Controller::Instance().GetOpMetaInfoMap().at(op_type_)[1]); - auto grad_outputs_names = paddle::framework::OpMetaInfoHelper::GetOutputs( + auto grad_outputs_names = paddle::OpMetaInfoHelper::GetOutputs( + egr::Controller::Instance().GetOpMetaInfoMap().at(op_type_)[1]); + const auto& grad_inplace_map = paddle::OpMetaInfoHelper::GetInplaceMap( egr::Controller::Instance().GetOpMetaInfoMap().at(op_type_)[1]); - const auto& grad_inplace_map = - paddle::framework::OpMetaInfoHelper::GetInplaceMap( - egr::Controller::Instance().GetOpMetaInfoMap().at(op_type_)[1]); auto map = egr::Controller::Instance().GetCustomEdgesSlotMap().at(op_type_); auto kernel_map = egr::Controller::Instance().GetOpMetaInfoMap(); @@ -240,9 +233,9 @@ RunCustomOpNode::operator()(paddle::small_vector, } VLOG(7) << "Run Kernel of Grad Custom Op: " << op_type_ << "_grad"; + // handle inplace map ctx.MapPlainOutputs(grad_inputs_name, grad_outputs_names, grad_inplace_map); - (*paddle::framework::OpMetaInfoHelper::GetKernelFn( - kernel_map.at(op_type_)[1]))(&ctx); + (*paddle::OpMetaInfoHelper::GetKernelFn(kernel_map.at(op_type_)[1]))(&ctx); ctx.AssignInplaceOutputs(); VLOG(7) << "Get AutogradMeta for inputs and outputs for Custom Op"; @@ -333,8 +326,8 @@ RunCustomOpNode::operator()(paddle::small_vector, ctx.InputRangeAt(it->first).second)); } - auto attrs_names = paddle::framework::OpMetaInfoHelper::GetAttrs( - meta_info_map.at(op_type_)[2]); + auto attrs_names = + paddle::OpMetaInfoHelper::GetAttrs(meta_info_map.at(op_type_)[2]); std::vector attrs(attrs_names.size()); // Prepare attrs for Grad node for (auto it = slot_map[1][4].begin(); it != slot_map[1][4].end(); it++) { @@ -357,12 +350,10 @@ RunCustomOpDoubleGradNode::operator()( paddle::CustomOpKernelContext ctx; auto meta_info_map = egr::Controller::Instance().GetOpMetaInfoMap(); const auto& vec_map = meta_info_map.at(op_type_); - auto grad_inputs_name = - paddle::framework::OpMetaInfoHelper::GetInputs(vec_map[2]); - auto grad_outputs_names = - paddle::framework::OpMetaInfoHelper::GetOutputs(vec_map[2]); + auto grad_inputs_name = paddle::OpMetaInfoHelper::GetInputs(vec_map[2]); + auto grad_outputs_names = paddle::OpMetaInfoHelper::GetOutputs(vec_map[2]); const auto& grad_inplace_map = - paddle::framework::OpMetaInfoHelper::GetInplaceMap(vec_map[2]); + paddle::OpMetaInfoHelper::GetInplaceMap(vec_map[2]); auto map = egr::Controller::Instance().GetCustomEdgesSlotMap().at(op_type_); auto kernel_map = egr::Controller::Instance().GetOpMetaInfoMap(); @@ -429,9 +420,9 @@ RunCustomOpDoubleGradNode::operator()( } VLOG(7) << "Run Kernel of Grad Custom Op: " << name(); + // handle inplace map ctx.MapPlainOutputs(grad_inputs_name, grad_outputs_names, grad_inplace_map); - (*paddle::framework::OpMetaInfoHelper::GetKernelFn( - kernel_map.at(op_type_)[2]))(&ctx); + (*paddle::OpMetaInfoHelper::GetKernelFn(kernel_map.at(op_type_)[2]))(&ctx); ctx.AssignInplaceOutputs(); return outs; diff --git a/paddle/fluid/framework/custom_operator.cc b/paddle/fluid/framework/custom_operator.cc index 2ddbf738787906cd449e116cb3a0371bf801683f..d1ff32e1c3a38636d7167890b9b7f9fe5e8d10c2 100644 --- a/paddle/fluid/framework/custom_operator.cc +++ b/paddle/fluid/framework/custom_operator.cc @@ -28,7 +28,6 @@ limitations under the License. */ #include "paddle/fluid/eager/api/utils/global_utils.h" #include "paddle/fluid/framework/attribute.h" #include "paddle/fluid/framework/convert_utils.h" -#include "paddle/fluid/framework/op_meta_info_helper.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/framework/phi_utils.h" @@ -285,7 +284,7 @@ static void RunKernelFunc( VLOG(4) << "Initialize phi tensor operants successfully"; } - // handle inplace case + // handle inplace map kernel_ctx.MapPlainOutputs(inputs, outputs, inplace_map); func(&kernel_ctx); kernel_ctx.AssignInplaceOutputs(); diff --git a/paddle/fluid/framework/op_meta_info_helper.h b/paddle/fluid/framework/op_meta_info_helper.h deleted file mode 100644 index 20154e1ee385b494fe3b49429791a1170900d213..0000000000000000000000000000000000000000 --- a/paddle/fluid/framework/op_meta_info_helper.h +++ /dev/null @@ -1,58 +0,0 @@ -/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#pragma once - -#include -#include - -#include "paddle/phi/api/ext/op_meta_info.h" - -namespace paddle { -namespace framework { - -class OpMetaInfoHelper { - public: - static const std::string& GetOpName(const paddle::OpMetaInfo& info) { - return info.name_; - } - static const std::vector& GetInputs( - const paddle::OpMetaInfo& info) { - return info.inputs_; - } - static const std::vector& GetOutputs( - const paddle::OpMetaInfo& info) { - return info.outputs_; - } - static const std::vector& GetAttrs( - const paddle::OpMetaInfo& info) { - return info.attrs_; - } - static const std::unordered_map& GetInplaceMap( - const paddle::OpMetaInfo& info) { - return info.inplace_map_; - } - static const KernelFunc& GetKernelFn(const paddle::OpMetaInfo& info) { - return info.kernel_fn_; - } - static const InferShapeFunc& GetInferShapeFn(const paddle::OpMetaInfo& info) { - return info.infer_shape_fn_; - } - static const InferDtypeFunc& GetInferDtypeFn(const paddle::OpMetaInfo& info) { - return info.infer_dtype_fn_; - } -}; - -} // namespace framework -} // namespace paddle diff --git a/paddle/fluid/inference/tensorrt/convert/generic_and_custom_plugin_creater.cc b/paddle/fluid/inference/tensorrt/convert/generic_and_custom_plugin_creater.cc index 76e6d3ffcbb9f0fd2e9bf2cfab15ee9a77a4365f..ad01ddff4ce046d134ab90491fda42581d7c0726 100644 --- a/paddle/fluid/inference/tensorrt/convert/generic_and_custom_plugin_creater.cc +++ b/paddle/fluid/inference/tensorrt/convert/generic_and_custom_plugin_creater.cc @@ -12,11 +12,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -#include "paddle/fluid/framework/op_meta_info_helper.h" #include "paddle/fluid/inference/tensorrt/convert/op_converter.h" #include "paddle/fluid/inference/tensorrt/helper.h" #include "paddle/fluid/inference/tensorrt/plugin/generic_plugin.h" #include "paddle/fluid/inference/tensorrt/plugin_arg_mapping_context.h" +#include "paddle/phi/api/ext/op_meta_info.h" namespace paddle { namespace inference { @@ -48,7 +48,7 @@ class CustomPluginCreater : public OpConverter { auto &op_info = meta_info_map.at(op_desc.Type()).front(); // set inputs - auto &op_input_names = framework::OpMetaInfoHelper::GetInputs(op_info); + auto &op_input_names = OpMetaInfoHelper::GetInputs(op_info); for (auto ¶m_name : op_input_names) { for (auto &arg_name : op_desc.Input(param_name)) { inputs.push_back(engine_->GetITensor(arg_name)); @@ -60,7 +60,7 @@ class CustomPluginCreater : public OpConverter { // set attrs std::vector plugindatas; - auto &op_attrs_names = framework::OpMetaInfoHelper::GetAttrs(op_info); + auto &op_attrs_names = OpMetaInfoHelper::GetAttrs(op_info); auto &attrs = op_desc.GetAttrMap(); std::list int_attrs; @@ -147,7 +147,7 @@ class CustomPluginCreater : public OpConverter { CHECK(layer); // set outputs - auto &op_output_names = framework::OpMetaInfoHelper::GetOutputs(op_info); + auto &op_output_names = OpMetaInfoHelper::GetOutputs(op_info); std::vector output_names; for (auto ¶m_name : op_output_names) { for (auto &arg_name : op_desc.Output(param_name)) diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index bd32d8569f1dd608c28caf71e6ed65b769a8ee20..f3a1179826607561062cb4bc9cb6a6d9f67338f1 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -18,7 +18,6 @@ #include "paddle/fluid/framework/block_desc.h" #include "paddle/fluid/framework/data_layout.h" -#include "paddle/fluid/framework/op_meta_info_helper.h" #include "paddle/fluid/framework/phi_utils.h" #include "paddle/fluid/inference/tensorrt/dynamic_shape_infermeta_factory.h" #include "paddle/phi/core/compat/op_utils.h" diff --git a/paddle/fluid/pybind/eager_functions.cc b/paddle/fluid/pybind/eager_functions.cc index 28221f2b9ebf34d8f74e8858b551065385a026ce..7475ab7321d9e304921860149431970408b5d823 100644 --- a/paddle/fluid/pybind/eager_functions.cc +++ b/paddle/fluid/pybind/eager_functions.cc @@ -32,7 +32,6 @@ typedef SSIZE_T ssize_t; #include "paddle/fluid/eager/utils.h" #include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/framework/custom_operator.h" -#include "paddle/fluid/framework/op_meta_info_helper.h" #include "paddle/fluid/framework/phi_utils.h" #include "paddle/fluid/framework/python_headers.h" #include "paddle/fluid/memory/allocation/allocator.h" @@ -327,18 +326,12 @@ static void ConstructFwdAndBwdMap( return; } else { VLOG(7) << "Construct CustomEdgesSlotMap "; - auto inputs_names = - paddle::framework::OpMetaInfoHelper::GetInputs(vec_map[0]); - auto outputs_names = - paddle::framework::OpMetaInfoHelper::GetOutputs(vec_map[0]); - auto attrs_names = - paddle::framework::OpMetaInfoHelper::GetAttrs(vec_map[0]); - auto grad_outputs_names = - paddle::framework::OpMetaInfoHelper::GetOutputs(vec_map[1]); - auto grad_inputs_names = - paddle::framework::OpMetaInfoHelper::GetInputs(vec_map[1]); - auto grad_attrs_names = - paddle::framework::OpMetaInfoHelper::GetAttrs(vec_map[1]); + auto inputs_names = paddle::OpMetaInfoHelper::GetInputs(vec_map[0]); + auto outputs_names = paddle::OpMetaInfoHelper::GetOutputs(vec_map[0]); + auto attrs_names = paddle::OpMetaInfoHelper::GetAttrs(vec_map[0]); + auto grad_outputs_names = paddle::OpMetaInfoHelper::GetOutputs(vec_map[1]); + auto grad_inputs_names = paddle::OpMetaInfoHelper::GetInputs(vec_map[1]); + auto grad_attrs_names = paddle::OpMetaInfoHelper::GetAttrs(vec_map[1]); std::vector> res(5); in_out_map.insert({op_type, {res}}); @@ -525,23 +518,21 @@ static PyObject* eager_api_run_custom_op(PyObject* self, "sure you registered your op first and try again. ", op_type)); VLOG(7) << "Run Kernel of Custom Op: " << op_type; - std::vector res_attrs = - CastAttrsToTargetType(ctx.Attrs(), - paddle::framework::OpMetaInfoHelper::GetAttrs( - meta_info_map.at(op_type)[0])); + std::vector res_attrs = CastAttrsToTargetType( + ctx.Attrs(), + paddle::OpMetaInfoHelper::GetAttrs(meta_info_map.at(op_type)[0])); ctx.EmplaceBackAttrs(res_attrs); const auto& vec_map = meta_info_map.at(op_type); - // handle inplace case - const auto& inputs = paddle::framework::OpMetaInfoHelper::GetInputs( - meta_info_map.at(op_type)[0]); - const auto& outputs = paddle::framework::OpMetaInfoHelper::GetOutputs( - meta_info_map.at(op_type)[0]); + const auto& inputs = + paddle::OpMetaInfoHelper::GetInputs(meta_info_map.at(op_type)[0]); + const auto& outputs = + paddle::OpMetaInfoHelper::GetOutputs(meta_info_map.at(op_type)[0]); const auto& inplace_map = - paddle::framework::OpMetaInfoHelper::GetInplaceMap( - meta_info_map.at(op_type)[0]); + paddle::OpMetaInfoHelper::GetInplaceMap(meta_info_map.at(op_type)[0]); + // handle inplace map ctx.MapPlainOutputs(inputs, outputs, inplace_map); - (*paddle::framework::OpMetaInfoHelper::GetKernelFn(vec_map[0]))(&ctx); + (*paddle::OpMetaInfoHelper::GetKernelFn(vec_map[0]))(&ctx); ctx.AssignInplaceOutputs(); VLOG(7) << "Get AutogradMeta for inputs and outputs for Custom Op"; @@ -569,7 +560,7 @@ static PyObject* eager_api_run_custom_op(PyObject* self, trace_backward, &(ins_auto_grad_metas[i])); } - // handle inplace case + // handle inplace map for (size_t i = 0; i < ctx.InputRange().size(); i++) { if (inplace_map.find(inputs[i]) != inplace_map.end()) { size_t input_size = @@ -653,8 +644,8 @@ static PyObject* eager_api_run_custom_op(PyObject* self, ctx.InputRangeAt(it->first).second)); } - auto attrs_names = paddle::framework::OpMetaInfoHelper::GetAttrs( - meta_info_map.at(op_type)[1]); + auto attrs_names = + paddle::OpMetaInfoHelper::GetAttrs(meta_info_map.at(op_type)[1]); std::vector attrs(attrs_names.size()); // Prepare attrs for Grad node for (auto it = slot_map[0][4].begin(); it != slot_map[0][4].end(); it++) { diff --git a/paddle/phi/api/ext/op_meta_info.h b/paddle/phi/api/ext/op_meta_info.h index a5296cdfe1477e31317a117a3ee044064018fcfe..8aa6bc1e462d754dc5c5d909f9ee9c011b160a2f 100644 --- a/paddle/phi/api/ext/op_meta_info.h +++ b/paddle/phi/api/ext/op_meta_info.h @@ -33,10 +33,8 @@ limitations under the License. */ */ namespace paddle { -namespace framework { -class PADDLE_API OpMetaInfoHelper; -} // namespace framework +class PADDLE_API OpMetaInfoHelper; using Tensor = paddle::Tensor; ///////////////// Util Marco Define //////////////// @@ -130,7 +128,7 @@ class PADDLE_API CustomOpKernelContext { } } - // handle inplace case + // handle inplace map void MapPlainOutputs( const std::vector& inputs, const std::vector& outputs, @@ -144,7 +142,7 @@ class PADDLE_API CustomOpKernelContext { std::vector inputs_; std::vector outputs_; std::vector attrs_; - // handle inplace case + // handle inplace map std::vector plain_outputs_; std::unordered_map inplace_tensor_map_; @@ -589,7 +587,7 @@ class PADDLE_API OpMetaInfo { OpMetaInfo& SetInferDtypeFn(InferDtypeFunc&& func); private: - friend class framework::OpMetaInfoHelper; + friend class OpMetaInfoHelper; // 1. desc info std::string name_; @@ -603,6 +601,39 @@ class PADDLE_API OpMetaInfo { InferDtypeFunc infer_dtype_fn_{nullptr}; }; +//////////////// Op Meta Info Helper ///////////////// +class OpMetaInfoHelper { + public: + static const std::string& GetOpName(const paddle::OpMetaInfo& info) { + return info.name_; + } + static const std::vector& GetInputs( + const paddle::OpMetaInfo& info) { + return info.inputs_; + } + static const std::vector& GetOutputs( + const paddle::OpMetaInfo& info) { + return info.outputs_; + } + static const std::vector& GetAttrs( + const paddle::OpMetaInfo& info) { + return info.attrs_; + } + static const std::unordered_map& GetInplaceMap( + const paddle::OpMetaInfo& info) { + return info.inplace_map_; + } + static const KernelFunc& GetKernelFn(const paddle::OpMetaInfo& info) { + return info.kernel_fn_; + } + static const InferShapeFunc& GetInferShapeFn(const paddle::OpMetaInfo& info) { + return info.infer_shape_fn_; + } + static const InferDtypeFunc& GetInferDtypeFn(const paddle::OpMetaInfo& info) { + return info.infer_dtype_fn_; + } +}; + //////////////// Op Meta Info Map ///////////////// class PADDLE_API OpMetaInfoMap { diff --git a/paddle/phi/api/lib/op_meta_info.cc b/paddle/phi/api/lib/op_meta_info.cc index d051981d76b51195ece942ec5ed9c1f1a990512e..35b8267873e7f6eaec37925119a7aa06ab78ca3b 100644 --- a/paddle/phi/api/lib/op_meta_info.cc +++ b/paddle/phi/api/lib/op_meta_info.cc @@ -299,6 +299,27 @@ OpMetaInfoBuilder& OpMetaInfoBuilder::Attrs(std::vector&& attrs) { OpMetaInfoBuilder& OpMetaInfoBuilder::SetInplaceMap( std::unordered_map&& inplace_map) { + const std::vector& inputs = + OpMetaInfoHelper::GetInputs(*info_ptr_); + const std::vector& outputs = + OpMetaInfoHelper::GetOutputs(*info_ptr_); + for (const auto& pair : inplace_map) { + PADDLE_ENFORCE( + std::find(inputs.begin(), inputs.end(), pair.first) != inputs.cend(), + phi::errors::PreconditionNotMet( + "The register of operator %s's `SetInplaceMap` failed. " + "Please make sure: 1. Call `Inputs` and `Outputs` before " + "`SetInplaceMap`; 2. The keys of inplace_map are inside `Inputs`", + name_)); + PADDLE_ENFORCE(std::find(outputs.begin(), outputs.end(), pair.second) != + outputs.cend(), + phi::errors::PreconditionNotMet( + "The register of operator %s's `SetInplaceMap` failed. " + "Please make sure: 1. Call `Inputs` and `Outputs` " + "before `SetInplaceMap`; 2. The values of inplace_map " + "are inside `Outputs`", + name_)); + } info_ptr_->SetInplaceMap( std::forward>(inplace_map)); return *this;