diff --git a/paddle/fluid/ir/dialect/pd_op.yaml b/paddle/fluid/ir/dialect/pd_op.yaml index a343b11f185d05224a6c51331691c7780acb4a39..a7d1cb14e35e5c213fe5eff5edc2c1ecdcc0f59f 100644 --- a/paddle/fluid/ir/dialect/pd_op.yaml +++ b/paddle/fluid/ir/dialect/pd_op.yaml @@ -116,7 +116,42 @@ - {typename: Tensor, name: out, optional: false, intermediate: false} no_need_buffer: null data_transform: null - invoke: {func: add_n_impl, args: inputs} + infer_meta: + func: AddNInferMeta + param: [inputs] + kernel: + func: [add_n] + param: [inputs] + backend: null + layout: null + data_type: null + dispatch: {fetch: null} + force_backend: null + backward: add_n_grad + +- name: add_n_with_kernel + inputs: + - typename: Tensor[] + name: inputs + optional: false + no_need_buffer: false + data_transform: {} + attrs: [] + outputs: + - {typename: Tensor, name: out, optional: false, intermediate: false} + no_need_buffer: null + data_transform: null + infer_meta: + func: AddNInferMeta + param: [inputs] + kernel: + func: [add_n] + param: [inputs] + backend: null + layout: null + data_type: null + dispatch: {fetch: null} + force_backend: null backward: add_n_grad - name: write_to_array diff --git a/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.cc b/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.cc index a0ce5775735387e21f473942ddac393b0ea03f26..f1ad5042cd69f6126ebf68270bcf3f259c452e74 100644 --- a/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.cc +++ b/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.cc @@ -350,7 +350,6 @@ void BuildScope(const ir::Block& block, << paddle::framework::GenScopeTreeDebugInfo( const_cast(inner_scope->root())); - // int count = value_2_var_name->size(); for (auto it = block.begin(); it != block.end(); ++it) { ir::Operation* op = *it; diff --git a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc index 855c094ebf309c37cd650ebdf530f4f81df0b441..a7f209e7e4c3196e511febcd7416ccbf1fbab4d5 100644 --- a/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc +++ b/paddle/fluid/ir/transforms/pd_op_to_kernel_pass.cc @@ -65,7 +65,6 @@ phi::KernelKey GetKernelKey( phi::Backend kernel_backend = phi::Backend::UNDEFINED; phi::DataLayout kernel_layout = phi::DataLayout::UNDEFINED; phi::DataType kernel_data_type = phi::DataType::UNDEFINED; - if (op_info_parser != nullptr) { // only suppurt non vector input for now int tensor_input_number = op_info_parser->InputTensorNumber(); @@ -84,12 +83,36 @@ phi::KernelKey GetKernelKey( } else if (input_map.count(slot_name)) { // parse from input int in_index = input_map.at(slot_name); + auto type = map_value_pair.at(op->operand(in_index)).type(); + + if (type.isa()) { + kernel_data_type = TransToPhiDataType( + type.dyn_cast() + .dtype()); + } else if (type.isa()) { + auto vec_data = type.dyn_cast().data(); + if (vec_data.size() == 0) { + kernel_data_type = phi::DataType::UNDEFINED; + } else { + if (vec_data[0].isa()) { + kernel_data_type = TransToPhiDataType( + vec_data[0] + .dyn_cast() + .dtype()); + } else { + PADDLE_THROW(phi::errors::Unimplemented( + "Only support DenseTensorType in vector")); + } + } + } else if (type.isa()) { + kernel_data_type = TransToPhiDataType( + type.dyn_cast() + .dtype()); + } else { + PADDLE_THROW(phi::errors::Unimplemented( + "Only support DenseTensorType, SelectedRows, VectorType")); + } - dialect::DenseTensorType type = - op->operand(in_index) - .type() - .dyn_cast(); - kernel_data_type = TransToPhiDataType(type.dtype()); } else { PADDLE_ENFORCE_EQ(attr_map.count(slot_name), true, @@ -146,7 +169,6 @@ phi::KernelKey GetKernelKey( if (op_info_parser != nullptr && op_info_parser->IsTensorAttribute(i)) { continue; } - auto input_tmp = op->operand(i); // NOTE: if not input_tmp, it's an optional input if (!input_tmp) { diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.cc b/paddle/fluid/ir_adaptor/translator/op_translator.cc index 2c59cc5adf775ec091330df4909f2c45065807ab..803d31e4aa26d1814288ded2808315dc1358c287 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/op_translator.cc @@ -977,7 +977,8 @@ struct SplitOpTranscriber : public OpTranscriber { // process sections int num = paddle::get(op_desc.GetAttr("num")); if (num <= 0) { - if (op_desc.HasInput("SectionsTensorList")) { + if (op_desc.HasInput("SectionsTensorList") && + op_desc.Input("SectionsTensorList").size() > 0) { // get SectionsTensorList from input auto sec_tensor_list = op_desc.Input("SectionsTensorList"); @@ -989,7 +990,7 @@ struct SplitOpTranscriber : public OpTranscriber { ir::Attribute new_attr = attribute_translator( "paddle::dialect::IntArrayAttribute", op_desc.GetAttr("sections")); auto sec_defin_op = - InsertFullOperationForAttributeInput(ctx, program, new_attr); + InsertFullArrayOperationForAttributeInput(ctx, program, new_attr); op_inputs.push_back(sec_defin_op->result(0)); } } @@ -1087,6 +1088,26 @@ struct FetchOpTranscriber : public OpTranscriber { } }; +// NOTE, add_n op in legacy ops don't have a kernel, so we use a new op for now +struct AddNOpTranscriber : public OpTranscriber { + ir::OpInfo LoopkUpOpInfo(ir::IrContext* ctx, const OpDesc& op_desc) override { + std::string target_op_name = + kTargetDialectPrefix + OpNameCompatibleMapping(op_desc.Type()); + if (IsInplace(op_desc)) { + target_op_name += "_"; + } else { + target_op_name += "_with_kernel"; + } + const auto& op_info = ctx->GetRegisteredOpInfo(target_op_name); + if (!op_info) { + IR_THROW( + "Op assign_value should have corresponding OpInfo pd.assign_value_"); + } + + return op_info; + } +}; + OpTranslator::OpTranslator() { general_handler = OpTranscriber(); special_handlers["feed"] = FeedOpTranscriber(); @@ -1098,6 +1119,7 @@ OpTranslator::OpTranslator() { special_handlers["assign_value"] = AssignValueOpTranscriber(); special_handlers["increment"] = IncrementOpTranscriber(); special_handlers["rnn"] = RnnOpTranscriber(); + special_handlers["add_n"] = AddNOpTranscriber(); } } // namespace translator diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 3b9b094aaf59593a17125b62ebe0b7c529d813e0..590808a6bd8871bd604760977c26468ad0a32ad2 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -2534,6 +2534,15 @@ out : Out extra : attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32"] + int_array : + starts : + data_type : int + tensor_name : StartsTensor + tensors_name : StartsTensorList + ends : + data_type : int + tensor_name : EndsTensor + tensors_name : EndsTensorList - op : slogdet(slogdeterminant) backward : slogdet_grad(slogdeterminant_grad) diff --git a/paddle/phi/kernels/cpu/fetch_kernel.cc b/paddle/phi/kernels/cpu/fetch_kernel.cc index 6d8a5aec2855d818433312090e146225ecaa0c47..672ceba1b84b356fc25d72bde35fb5bba2457443 100644 --- a/paddle/phi/kernels/cpu/fetch_kernel.cc +++ b/paddle/phi/kernels/cpu/fetch_kernel.cc @@ -24,6 +24,7 @@ void FetchKernel(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out) { phi::Copy(dev_ctx, x, phi::CPUPlace(), true, out); + out->set_lod(x.lod()); } } // namespace phi PD_REGISTER_KERNEL(fetch, diff --git a/python/paddle/fluid/executor.py b/python/paddle/fluid/executor.py index c183a2f9bd5232da554f7d07eac806a7076c1540..b3deb787960e6580b7a9289add98b7958d01af5e 100755 --- a/python/paddle/fluid/executor.py +++ b/python/paddle/fluid/executor.py @@ -855,11 +855,18 @@ class _ExecutorCache: if build_strategy is None or build_strategy.enable_inplace else False ) + enable_addto = ( True if build_strategy is not None and build_strategy.enable_addto else False ) + + if os.getenv("FLAGS_enable_new_ir_in_executor"): + # todo(phlrain), skip inplace add addto pass in new IR + enable_inplace = False + enable_addto = False + if enable_inplace or enable_addto: # inplace should skip feed and fetch var skip_var_names = eval(_get_program_cache_key(feed, fetch_list)) diff --git a/test/legacy_test/eager_op_test.py b/test/legacy_test/eager_op_test.py index e4d1ff08d99afe0edcaa8aef1e25978af7ec38bf..838e8de35235cd4b19e359e8d1fbc91d18606e7f 100644 --- a/test/legacy_test/eager_op_test.py +++ b/test/legacy_test/eager_op_test.py @@ -2309,6 +2309,8 @@ class OpTest(unittest.TestCase): self.op_type not in compile_vs_runtime_white_list.COMPILE_RUN_OP_WHITE_LIST ): + if os.getenv("FLAGS_enable_new_ir_in_executor"): + return self.check_compile_vs_runtime(fetch_list, outs) def check_output_customized(self, checker, custom_place=None):