From c012a0f111371cda88c3ead77b657ca0db255801 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Fri, 18 Aug 2023 08:35:43 +0000 Subject: [PATCH 01/13] register set_value in new ir --- paddle/fluid/framework/executor_cache.cc | 11 ++ .../new_executor/standalone_executor.cc | 6 + paddle/fluid/ir/dialect/pd_attribute.h | 5 + paddle/fluid/ir/dialect/pd_op.yaml | 45 ++++- paddle/fluid/ir/dialect/utils.cc | 1 + paddle/fluid/ir/dialect/utils.h | 1 - .../ir/phi_kernel_adaptor/phi_kernel_util.h | 5 +- .../translator/attribute_translator.cc | 9 +- .../ir_adaptor/translator/op_translator.cc | 180 ++++++++++++++---- paddle/phi/api/yaml/op_compat.yaml | 17 ++ paddle/phi/infermeta/unary.cc | 2 + .../phi/kernels/impl/set_value_kernel_impl.h | 13 ++ python/paddle/fluid/executor.py | 3 + .../paddle/jit/dy2static/partial_program.py | 19 ++ test/ir/new_ir/test_special_op_translator.py | 25 +++ 15 files changed, 298 insertions(+), 44 deletions(-) diff --git a/paddle/fluid/framework/executor_cache.cc b/paddle/fluid/framework/executor_cache.cc index 67646dec6db670..a791190e35dad3 100644 --- a/paddle/fluid/framework/executor_cache.cc +++ b/paddle/fluid/framework/executor_cache.cc @@ -20,6 +20,8 @@ #include "paddle/ir/core/program.h" #include "paddle/ir/core/value.h" +#include // NOLINT + namespace paddle { namespace framework { class ProgramDesc; @@ -437,8 +439,12 @@ std::unique_ptr<::ir::Program> ConstructFowardIrProgram( program.get()); program_translator.Translate(); + program->Print(std::cout); + std::cout << std::endl; auto ir_res = paddle::dialect::PdOpLowerToKernelPass(program.get()); + ir_res->Print(std::cout); + std::cout << std::endl; return ir_res; } @@ -511,7 +517,12 @@ std::unique_ptr<::ir::Program> ConstructBackwardIrProgram( program.get()); program_translator.Translate(); + program->Print(std::cout); + std::cout << std::endl; + auto res = paddle::dialect::PdOpLowerToKernelPass(program.get()); + res->Print(std::cout); + std::cout << std::endl; return res; } diff --git a/paddle/fluid/framework/new_executor/standalone_executor.cc b/paddle/fluid/framework/new_executor/standalone_executor.cc index 0e1d2de6bed29c..c65d4b812701a2 100644 --- a/paddle/fluid/framework/new_executor/standalone_executor.cc +++ b/paddle/fluid/framework/new_executor/standalone_executor.cc @@ -21,6 +21,8 @@ #include "paddle/fluid/ir_adaptor/translator/translate.h" +#include // NOLINT + PHI_DECLARE_bool(enable_new_ir_in_executor); namespace paddle { @@ -65,6 +67,8 @@ StandaloneExecutor::StandaloneExecutor(const platform::Place& place, if (FLAGS_enable_new_ir_in_executor) { VLOG(6) << "begin to translate" << std::endl; auto base_program = paddle::TranslateLegacyProgramToProgram(*program); + base_program->Print(std::cout); + std::cout << std::endl; auto block = base_program->block(); for (auto it = block->begin(); it != block->end(); ++it) { @@ -90,6 +94,8 @@ StandaloneExecutor::StandaloneExecutor(const platform::Place& place, auto kernel_program = paddle::dialect::PdOpLowerToKernelPass(base_program.get(), place); + kernel_program->Print(std::cout); + std::cout << std::endl; interpretercores_.emplace_back( std::make_shared(place_, fetch_var_names_, diff --git a/paddle/fluid/ir/dialect/pd_attribute.h b/paddle/fluid/ir/dialect/pd_attribute.h index 05514705efe14c..e71e254f2c1047 100644 --- a/paddle/fluid/ir/dialect/pd_attribute.h +++ b/paddle/fluid/ir/dialect/pd_attribute.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/fluid/ir/dialect/pd_attribute_storage.h" +#include "paddle/fluid/ir/dialect/utils.h" #include "paddle/ir/core/attribute.h" #include "paddle/ir/core/builtin_attribute.h" #include "paddle/phi/common/scalar.h" @@ -49,6 +50,10 @@ class ScalarAttribute : public ir::Attribute { (val.type_id() == ir::StrAttribute::type_id()); } + static ir::Attribute get(ir::IrContext *ctx, phi::Scalar scalar) { + return TransToIrAttribute(scalar, ctx); + } + phi::Scalar data(); }; diff --git a/paddle/fluid/ir/dialect/pd_op.yaml b/paddle/fluid/ir/dialect/pd_op.yaml index 1c20c409df1eb9..8e3b5ee07c6635 100644 --- a/paddle/fluid/ir/dialect/pd_op.yaml +++ b/paddle/fluid/ir/dialect/pd_op.yaml @@ -318,7 +318,6 @@ view: null backward: null - - name: shadow_feed inputs: - typename: Tensor @@ -344,3 +343,47 @@ force_backend: null inplace: null backward: null + +- name : set_value + inputs: + - {typename: Tensor, name: x, optional: false, no_need_buffer: false, data_transform: {} } + - {typename: Tensor, name: starts, optional: false, no_need_buffer: false, data_transform: {} } + - {typename: Tensor, name: ends, optional: false, no_need_buffer: false, data_transform: {} } + - {typename: Tensor, name: steps, optional: false, no_need_buffer: false, data_transform: {} } + attrs: + - {typename: 'int64_t[]', name: axes} + - {typename: 'int64_t[]', name: decrease_axes} + - {typename: 'int64_t[]', name: none_axes} + - {typename: 'Scalar[]', name: values} + outputs: + - {typename: Tensor, name: out, optional: false, intermediate: false} + infer_meta: + func: SetValueInferMeta + param: [x] + kernel: + func: [set_value] + param: [x, starts, ends, steps, axes, decrease_axes, none_axes, values] + inplace: null + backward: null + +- name : set_value_with_tensor + inputs: + - {typename: Tensor, name: x, optional: false, no_need_buffer: false, data_transform: {} } + - {typename: Tensor, name: values, optional: false, no_need_buffer: false, data_transform: {} } + - {typename: Tensor, name: starts, optional: false, no_need_buffer: false, data_transform: {} } + - {typename: Tensor, name: ends, optional: false, no_need_buffer: false, data_transform: {} } + - {typename: Tensor, name: steps, optional: false, no_need_buffer: false, data_transform: {} } + attrs: + - {typename: 'int64_t[]', name: axes} + - {typename: 'int64_t[]', name: decrease_axes} + - {typename: 'int64_t[]', name: none_axes} + outputs: + - {typename: Tensor, name: out, optional: false, intermediate: false} + infer_meta: + func: SetValueInferMeta + param: [x] + kernel: + func: [set_value_with_tensor] + param: [x, values, starts, ends, steps, axes, decrease_axes, none_axes] + inplace: null + backward: null diff --git a/paddle/fluid/ir/dialect/utils.cc b/paddle/fluid/ir/dialect/utils.cc index cd6ff35ef7f4e1..ec0a055e9ba776 100644 --- a/paddle/fluid/ir/dialect/utils.cc +++ b/paddle/fluid/ir/dialect/utils.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/fluid/ir/dialect/utils.h" +#include "paddle/fluid/ir/dialect/pd_attribute.h" namespace paddle { namespace dialect { diff --git a/paddle/fluid/ir/dialect/utils.h b/paddle/fluid/ir/dialect/utils.h index 13a9f3d7ac8b82..ab4583378ee74b 100644 --- a/paddle/fluid/ir/dialect/utils.h +++ b/paddle/fluid/ir/dialect/utils.h @@ -16,7 +16,6 @@ #include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/framework/data_type.h" -#include "paddle/fluid/ir/dialect/pd_attribute.h" #include "paddle/fluid/ir/dialect/pd_type_storage.h" #include "paddle/ir/core/builtin_attribute.h" #include "paddle/ir/core/builtin_type.h" diff --git a/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h b/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h index fc2658d482c5b4..bec6809bec27ef 100644 --- a/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h +++ b/paddle/fluid/ir/phi_kernel_adaptor/phi_kernel_util.h @@ -37,6 +37,7 @@ #include "paddle/fluid/ir/dialect/kernel_type.h" #include "paddle/fluid/ir/dialect/pd_attribute.h" #include "paddle/fluid/ir/interface/op_yaml_info_parser.h" +#include "paddle/ir/core/type_name.h" #include "paddle/phi/core/infermeta_utils.h" #include "glog/logging.h" @@ -78,8 +79,8 @@ void BuildPhiContext(ir::Operation* op, Context* ctx) { paddle::framework::Scope* inner_scope = local_scope != nullptr ? local_scope : scope; - VLOG(6) << "BuildPhiContext in scope[" << scope << "] inner_scope[" - << inner_scope << "]"; + VLOG(6) << "Build " << get_type_name() << " in scope[" << scope + << "] inner_scope[" << inner_scope << "]"; auto attr_map = op->attributes(); diff --git a/paddle/fluid/ir_adaptor/translator/attribute_translator.cc b/paddle/fluid/ir_adaptor/translator/attribute_translator.cc index ada0d80688ac2c..00890af7878e5f 100644 --- a/paddle/fluid/ir_adaptor/translator/attribute_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/attribute_translator.cc @@ -135,8 +135,13 @@ class AttributeVisitor { virtual ir::Attribute operator()( const std::vector& ss) { VLOG(10) << "translating vector"; - IR_THROW( - "not support translating std::vector"); + std::vector attrs; + attrs.reserve(ss.size()); + for (const auto& v : ss) { + attrs.push_back(dialect::ScalarAttribute::get(ctx, v)); + } + VLOG(10) << "translating vector Done"; + return ir::ArrayAttribute::get(ctx, attrs); } virtual ir::Attribute operator()(const paddle::blank& blank) { diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.cc b/paddle/fluid/ir_adaptor/translator/op_translator.cc index ef53f1d496a3dd..942e455bd29294 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/op_translator.cc @@ -183,9 +183,7 @@ inline ir::Operation* InsertFullArrayOperationForAttributeInput( IR_ENFORCE(attr.isa(), "Encounter non IntArray type when trying to insert IntArray " "mutable attribute"); - phi::IntArray int_array = attr.dyn_cast().data(); - ir::Builder builder(ctx, program->block()); dialect::FullIntArrayOp full_int_array_op = builder.Build( @@ -193,40 +191,6 @@ inline ir::Operation* InsertFullArrayOperationForAttributeInput( return full_int_array_op.operation(); } -inline ir::OpResult GetAttributeAsInput(ir::IrContext* ctx, - ir::Program* program, - const OpDesc& op_desc, - const OpInputInfo& input_info) { - auto& attribute_translator = AttributeTranslator::instance(); - auto& op_normalizer = OpNameNormalizer::instance(); - - auto legacy_attr_name = - op_normalizer.GetLegacyAttrName(op_desc.Type(), input_info.name); - - if (!op_desc.HasAttr(legacy_attr_name)) { - IR_THROW("Op %s arg %s should not be zero size", - op_desc.Type(), - legacy_attr_name); - } - paddle::framework::Attribute legacy_attr = op_desc.GetAttr(legacy_attr_name); - VLOG(10) << "[" << op_desc.Type() << "][attribute]" - << " name: " << legacy_attr_name << " " << legacy_attr.index(); - ir::Attribute new_attr = - attribute_translator(input_info.type_name, legacy_attr); - - ir::Operation* defining_op = nullptr; - bool is_int_array = (input_info.type_name.find("IntArrayAttribute") != - input_info.type_name.npos); - if (is_int_array) { - defining_op = - InsertFullArrayOperationForAttributeInput(ctx, program, new_attr); - } else { - defining_op = InsertFullOperationForAttributeInput(ctx, program, new_attr); - } - - return defining_op->result(0); -} - } // namespace /// @brief This class is used to translate a OpDesc, it's a functor class and @@ -287,6 +251,10 @@ struct OpTranscriber { const OpDesc& op_desc, const OpInputInfoList& input_infos, ir::Program* program); + virtual ir::OpResult GetAttributeAsInput(ir::IrContext* ctx, + ir::Program* program, + const OpDesc& op_desc, + const OpInputInfo& input_info); }; ir::OpInfo OpTranscriber::LoopkUpOpInfo(ir::IrContext* ctx, @@ -347,6 +315,40 @@ void OpTranscriber::InsertSliceOperationForInput( } } +ir::OpResult OpTranscriber::GetAttributeAsInput(ir::IrContext* ctx, + ir::Program* program, + const OpDesc& op_desc, + const OpInputInfo& input_info) { + auto& attribute_translator = AttributeTranslator::instance(); + auto& op_normalizer = OpNameNormalizer::instance(); + + auto legacy_attr_name = + op_normalizer.GetLegacyAttrName(op_desc.Type(), input_info.name); + + if (!op_desc.HasAttr(legacy_attr_name)) { + IR_THROW("Op %s arg %s should not be zero size", + op_desc.Type(), + legacy_attr_name); + } + paddle::framework::Attribute legacy_attr = op_desc.GetAttr(legacy_attr_name); + VLOG(10) << "[" << op_desc.Type() << "][attribute]" + << " name: " << legacy_attr_name << " " << legacy_attr.index(); + ir::Attribute new_attr = + attribute_translator(input_info.type_name, legacy_attr); + + ir::Operation* defining_op = nullptr; + bool is_int_array = (input_info.type_name.find("IntArrayAttribute") != + input_info.type_name.npos); + if (is_int_array) { + defining_op = + InsertFullArrayOperationForAttributeInput(ctx, program, new_attr); + } else { + defining_op = InsertFullOperationForAttributeInput(ctx, program, new_attr); + } + + return defining_op->result(0); +} + std::vector OpTranscriber::GenerateOperationInput( ir::IrContext* ctx, TranslationContext* param_map, @@ -1135,10 +1137,18 @@ struct ShadowOutputOpTranscriber : public OpTranscriber { TranslationContext* param_map, const OpDesc& op_desc, ir::Program* program) override { + auto op_info = ctx->GetRegisteredOpInfo(ir::SetParameterOp::name()); + std::vector op_inputs; auto legacy_input_vars = op_desc.Input("x", true); auto defining_info = (*param_map)[legacy_input_vars[0]]; + if (defining_info.generated_by_vector) { + InsertSliceOperationForTarget( + ctx, param_map, program, defining_info, legacy_input_vars[0]); + defining_info = param_map->at(legacy_input_vars[0]).value; + } + op_inputs.push_back(defining_info.value); ir::AttributeMap attribute_map = { @@ -1147,9 +1157,8 @@ struct ShadowOutputOpTranscriber : public OpTranscriber { op_desc.GetAttrIfExists("name"))}, }; - auto create_op_info = ctx->GetRegisteredOpInfo(ir::SetParameterOp::name()); ir::Operation* operation = - ir::Operation::Create(op_inputs, attribute_map, {}, create_op_info); + ir::Operation::Create(op_inputs, attribute_map, {}, op_info); program->block()->push_back(operation); return operation; @@ -1455,6 +1464,100 @@ struct ElementwiseGradTranscriber : public OpTranscriber { } }; +struct SetValueOpTranscriber : public OpTranscriber { + ir::OpResult GetAttributeAsInput(ir::IrContext* ctx, + ir::Program* program, + const OpDesc& op_desc, + const OpInputInfo& input_info) override { + auto& attribute_translator = AttributeTranslator::instance(); + auto& op_normalizer = OpNameNormalizer::instance(); + + auto legacy_attr_name = + op_normalizer.GetLegacyAttrName(op_desc.Type(), input_info.name); + + if (!op_desc.HasAttr(legacy_attr_name)) { + IR_THROW("Op %s arg %s should not be zero size", + op_desc.Type(), + legacy_attr_name); + } + framework::Attribute legacy_attr = op_desc.GetAttr(legacy_attr_name); + VLOG(10) << "[" << op_desc.Type() << "][attribute]" + << " name: " << legacy_attr_name << " " << legacy_attr.index(); + ir::Attribute new_attr = + attribute_translator("paddle::dialect::IntArrayAttribute", legacy_attr); + + ir::Operation* defining_op = + InsertFullArrayOperationForAttributeInput(ctx, program, new_attr); + return defining_op->result(0); + } +}; + +struct SetValueWithTensorOpTranscriber : public SetValueOpTranscriber { + ir::OpInfo LoopkUpOpInfo(ir::IrContext* ctx, const OpDesc& op_desc) override { + std::string target_op_name = dialect::SetValueWithTensorOp::name(); + const auto& op_info = ctx->GetRegisteredOpInfo(target_op_name); + if (!op_info) { + IR_THROW( + "Op set_value should have corresponding OpInfo " + "pd.set_value_with_tensor"); + } + + return op_info; + } + + InputHandlerFn GetSpecialInputHandlers( + const std::string& input_name) override { + if (input_name != "values") { + return nullptr; + } + return [](ir::IrContext* ctx, + TranslationContext* param_map, + const OpDesc& op_desc, + const std::string&, + const OpInputInfo& info, + ir::Program* program) -> ir::OpResult { + std::vector legacy_input_vars; + IR_ENFORCE(op_desc.HasInput("ValueTensor"), + "[set_value] should have ValueTensor"); + legacy_input_vars = op_desc.Input("ValueTensor", true); + IR_ENFORCE( + legacy_input_vars.size() == 1u, + "[set_value][ValueTensor] should only have 1 variable, but got %d", + legacy_input_vars.size()); + auto var_name = legacy_input_vars[0]; + auto defining_info = (*param_map)[var_name]; + if (defining_info.generated_by_vector) { + InsertSliceOperationForTarget( + ctx, param_map, program, defining_info, var_name); + defining_info = param_map->at(var_name).value; + } + return defining_info.value; + }; + } +}; + +struct LegacySetValueDispatcher : public OpTranscriber { + ir::Operation* operator()(ir::IrContext* ctx, + TranslationContext* param_map, + const OpDesc& op_desc, + ir::Program* program) override { + std::vector legacy_input_vars; + + // if op has input with name "ValueTensor", then use that input as value + if (op_desc.HasInput("ValueTensor")) { + legacy_input_vars = op_desc.Input("ValueTensor", true); + if (legacy_input_vars.size() > 0) { + VLOG(10) << "legacy op:" << op_desc.Type() + << " has ValueTensor and convert to set_value_with_tensor"; + return SetValueWithTensorOpTranscriber()( + ctx, param_map, op_desc, program); + } + } + + return SetValueOpTranscriber()(ctx, param_map, op_desc, program); + } +}; + OpTranslator::OpTranslator() { general_handler = OpTranscriber(); special_handlers["add_n"] = AddNOpTranscriber(); @@ -1472,6 +1575,7 @@ OpTranslator::OpTranslator() { special_handlers["reduce_any"] = ReduceOpTranscriber(); special_handlers["rnn"] = RnnOpTranscriber(); special_handlers["shadow_output"] = ShadowOutputOpTranscriber(); + special_handlers["set_value"] = LegacySetValueDispatcher(); special_handlers["split"] = SplitOpTranscriber(); special_handlers["sum"] = AddNOpTranscriber(); diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 0f7be38a74386c..3aabbcc9259d4d 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -3025,6 +3025,23 @@ outputs: {out: Out, noise: Noise} +- op: set_value + backward: set_value_grad + inputs: + x : Input + outputs: + out: Out + int_array: + starts: + data_type : int64_t + tensors_name : StartsTensorList + ends: + data_type : int64_t + tensors_name : EndsTensorList + steps: + data_type : int64_t + tensors_name : StepsTensorList + - op: sigmoid_cross_entropy_with_logits backward: sigmoid_cross_entropy_with_logits_grad inputs : diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 1116b9cedf1e62..08430e9352abcd 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -3505,6 +3505,8 @@ void SetValueInferMeta(const MetaTensor& x, MetaTensor* out) { phi::errors::InvalidArgument( "The rank of input should be less than 7, but received %d.", in_dims.size())); + VLOG(0) << "[debug][set_value] input dims:" << in_dims; + VLOG(0) << "[debug][set_value] out:" << out; out->set_dims(in_dims); } diff --git a/paddle/phi/kernels/impl/set_value_kernel_impl.h b/paddle/phi/kernels/impl/set_value_kernel_impl.h index 2c545ac06ada11..3e682333e5521d 100644 --- a/paddle/phi/kernels/impl/set_value_kernel_impl.h +++ b/paddle/phi/kernels/impl/set_value_kernel_impl.h @@ -25,6 +25,8 @@ #include "paddle/phi/kernels/funcs/elementwise_functor.h" #include "paddle/phi/kernels/funcs/slice_utils.h" +#include "glog/logging.h" + namespace phi { // check whether the tensor with dimension of second can assign to the @@ -80,9 +82,11 @@ void SetValueImpl(const Context& dev_ctx, const std::vector& none_axes, DenseTensor* out) { auto in_dims = in.dims(); + LOG(INFO) << "in: " << in_dims; std::vector starts_local = starts.GetData(); std::vector ends_local = ends.GetData(); std::vector steps_local = steps.GetData(); + LOG(INFO) << "starts: " << starts_local.size(); phi::funcs::CheckAndUpdateSliceAttrs( in_dims, axes, &starts_local, &ends_local, &steps_local); auto slice_dims = phi::funcs::GetSliceDims( @@ -116,6 +120,8 @@ void SetValueImpl(const Context& dev_ctx, slice_dims_for_assign = phi::make_ddim(slice_dims_with_none); } + LOG(INFO) << "slice_dims_for_assign: " << slice_dims_for_assign; + auto place = dev_ctx.GetPlace(); auto& eigen_place = *dev_ctx.eigen_device(); @@ -165,6 +171,7 @@ void SetValueImpl(const Context& dev_ctx, out_e.stridedSlice(starts_indices, ends_indices, strides_indices) .device(eigen_place) = slice_e; + LOG(INFO) << "out e stridedSlicew"; // Step 2: Set a tensor with the same shape as out tensor. And its data at // '_index' is the same as value, and data out of '_index' to zero @@ -200,6 +207,7 @@ void SetValueImpl(const Context& dev_ctx, // // TODO(zoooo0820): Reimplement logic of set_value to avoid using // elementwise-sub. + LOG(INFO) << "Set value on GPU: "; funcs::ElementwiseCompute, T>( dev_ctx, slice_tensor, @@ -207,6 +215,7 @@ void SetValueImpl(const Context& dev_ctx, funcs::SubtractFunctor(), &slice_tensor); } else { + LOG(INFO) << "Set value not gpu: "; funcs::ElementwiseCompute, T>( dev_ctx, slice_tensor, @@ -220,6 +229,7 @@ void SetValueImpl(const Context& dev_ctx, pad_e.device(eigen_place) = pad_e.constant(T(0)); pad_e.stridedSlice(starts_indices, ends_indices, strides_indices) .device(eigen_place) = slice_e; + LOG(INFO) << "slice_tensor"; // Step 3: Set out tensor with value out_e.device(eigen_place) = out_e - pad_e; @@ -236,7 +246,10 @@ void SetTensorValueKernel(const Context& dev_ctx, const std::vector& decrease_axes, const std::vector& none_axes, DenseTensor* out) { + LOG(INFO) << x.dims(); + const int rank = x.dims().size(); + LOG(INFO) << "rank " << rank; switch (rank) { case 1: diff --git a/python/paddle/fluid/executor.py b/python/paddle/fluid/executor.py index 69376d22f98c9d..175a9e4090e2db 100755 --- a/python/paddle/fluid/executor.py +++ b/python/paddle/fluid/executor.py @@ -874,6 +874,9 @@ def _get_program_and_executor(self, cached_data): _apply_inplace_addto_pass( program, enable_inplace, enable_addto, skip_var_names ) + print('-------------[debug][program start]------------------') + print(program) + print('-------------[debug][program end]------------------', flush=True) new_program = program.clone() if ( diff --git a/python/paddle/jit/dy2static/partial_program.py b/python/paddle/jit/dy2static/partial_program.py index 3596a97c907439..14380a78f7c80f 100644 --- a/python/paddle/jit/dy2static/partial_program.py +++ b/python/paddle/jit/dy2static/partial_program.py @@ -802,6 +802,25 @@ def _get_forward_backward_program_form( self._apply_inplace_pass( forward_builded_program, backward_builded_program ) + + print( + '-------------[debug][forward_builded_program start]------------------' + ) + print(forward_builded_program) + print( + '-------------[debug][forward_builded_program end]------------------', + flush=True, + ) + + print( + '-------------[debug][backward_builded_program start]------------------' + ) + print(backward_builded_program) + print( + '-------------[debug][backward_builded_program end]------------------', + flush=True, + ) + return [forward_builded_program, backward_builded_program] def _apply_inplace_pass(self, forward_program, backward_program): diff --git a/test/ir/new_ir/test_special_op_translator.py b/test/ir/new_ir/test_special_op_translator.py index 4cc11fd9c28189..a55a99dfc8fcbf 100644 --- a/test/ir/new_ir/test_special_op_translator.py +++ b/test/ir/new_ir/test_special_op_translator.py @@ -300,5 +300,30 @@ def test_op(self): _ = ir.translate_to_new_ir(main_program.desc) +class TestShadowOutputSlice(unittest.TestCase): + def test_op(self): + place = core.Place() + place.set_place(paddle.CPUPlace()) + new_scope = paddle.static.Scope() + main_program = paddle.static.Program() + with paddle.static.scope_guard(new_scope): + with paddle.static.program_guard(main_program): + x = paddle.rand([3, 9, 5]) + y = paddle.static.data( + name="y", shape=[3, 9, 5], dtype="float32" + ) + + _, out, _ = paddle.split(x, num_or_sections=3, axis=1) + helper = LayerHelper('shadow_output') + helper.append_op( + type="shadow_output", + inputs={"x": [out.name]}, + outputs={"out": [y.name]}, + attrs={"name": out.name}, + ) + + l = ir.translate_to_new_ir(main_program.desc) + + if __name__ == "__main__": unittest.main() From 6f1230fdf9d7cc0ed6d4ffa5a11de0979a2f7317 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Tue, 22 Aug 2023 09:20:28 +0000 Subject: [PATCH 02/13] fix --- .../instruction/phi_kernel_instruction.cc | 13 +++++++ .../fluid/ir/dialect/op_generator/op_gen.py | 11 ++++++ paddle/fluid/ir/dialect/pd_op.yaml | 38 +++++++++++++++---- .../translator/attribute_translator.cc | 7 +++- paddle/phi/api/yaml/op_compat.yaml | 17 +++++++++ paddle/phi/core/kernel_context.cc | 12 +++++- paddle/phi/core/kernel_utils.h | 3 ++ 7 files changed, 92 insertions(+), 9 deletions(-) diff --git a/paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.cc b/paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.cc index d5b7b5affc5d4b..7b811004c036e3 100644 --- a/paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.cc +++ b/paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.cc @@ -32,6 +32,8 @@ #include "paddle/ir/core/operation.h" #include "paddle/ir/core/value.h" +#include "paddle/phi/core/kernel_context.h" + #include "paddle/fluid/framework/new_executor/instruction/instruction_util.h" namespace paddle { namespace framework { @@ -167,11 +169,22 @@ PhiKernelInstruction::PhiKernelInstruction( VLOG(6) << "finish process no need buffer"; } +void PrintKernelContext(const phi::KernelContext& kernel_context) { + VLOG(6) << "------------kernel_contxt start-----------"; + VLOG(6) << "inputs: " << kernel_context.InputsSize(); + VLOG(6) << "output: " << kernel_context.OutputsSize(); + VLOG(6) << "attrs: " << kernel_context.AttrsSize(); + VLOG(6) << "------------kernel_contxt end-------------"; +} + void PhiKernelInstruction::Run() { if (infer_meta_interface_) { infer_meta_interface_->infer_meta_(&(infer_meta_context_)); } VLOG(6) << "Run op " << phi_op_name_ << " infer meta."; + + PrintKernelContext(kernel_context_); + (*(phi_kernel_))(&(kernel_context_)); VLOG(6) << "Run op " << phi_op_name_ << " kernel."; } diff --git a/paddle/fluid/ir/dialect/op_generator/op_gen.py b/paddle/fluid/ir/dialect/op_generator/op_gen.py index d990141add5a01..367114d24eaac2 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_gen.py @@ -294,6 +294,9 @@ def __init__(self, op_yaml_item, op_compat_item): self.mutable_attribute_name_list, self.mutable_attribute_type_list, ) = self.parse_mutable_attribute() + if "set_value" in self.op_phi_name: + print(self.mutable_attribute_name_list) + print(self.mutable_attribute_type_list) ( self.non_mutable_attribute_name_list, @@ -363,6 +366,8 @@ def parse_mutable_attribute(self): """ {'axis': 'paddle::dialect::ScalarAttribute', 'rotl': 'paddle::dialect::IntArrayAttribute'} """ + if "set_value" in self.op_phi_name: + print(self.op_compat_item) mutable_attribute_name_list = [] mutable_attribute_type_list = [] # scalar @@ -417,6 +422,9 @@ def parse_mutable_attribute(self): ], ] ) + if "set_value" in self.op_phi_name: + print(mutable_attribute_name_list) + print(mutable_attribute_type_list) sorted_mutable_attribute_name_list = [] sorted_mutable_attribute_type_list = [] for attr_name in self.attribute_name_list: @@ -684,6 +692,9 @@ def OpGenerator( op_info_items.append( OpInfoParser(op, op_compat_parser.get_compat(op['name'])) ) + if op["name"] == "set_value": + print(op_compat_parser.get_compat(op['name'])) + print(op_info_items[-1].mutable_attribute_name_list) # (3) CodeGen: Traverse op_info_items and generate ops_name_list = [] # all op class name store in this list diff --git a/paddle/fluid/ir/dialect/pd_op.yaml b/paddle/fluid/ir/dialect/pd_op.yaml index 8e3b5ee07c6635..3744ae26d367fb 100644 --- a/paddle/fluid/ir/dialect/pd_op.yaml +++ b/paddle/fluid/ir/dialect/pd_op.yaml @@ -347,13 +347,14 @@ - name : set_value inputs: - {typename: Tensor, name: x, optional: false, no_need_buffer: false, data_transform: {} } - - {typename: Tensor, name: starts, optional: false, no_need_buffer: false, data_transform: {} } - - {typename: Tensor, name: ends, optional: false, no_need_buffer: false, data_transform: {} } - - {typename: Tensor, name: steps, optional: false, no_need_buffer: false, data_transform: {} } attrs: + - {typename: 'int64_t[]', name: starts} + - {typename: 'int64_t[]', name: ends} + - {typename: 'int64_t[]', name: steps} - {typename: 'int64_t[]', name: axes} - {typename: 'int64_t[]', name: decrease_axes} - {typename: 'int64_t[]', name: none_axes} + - {typename: 'int64_t[]', name: shapes} - {typename: 'Scalar[]', name: values} outputs: - {typename: Tensor, name: out, optional: false, intermediate: false} @@ -362,7 +363,7 @@ param: [x] kernel: func: [set_value] - param: [x, starts, ends, steps, axes, decrease_axes, none_axes, values] + param: [x, starts, ends, steps, axes, decrease_axes, none_axes, shapes, values] inplace: null backward: null @@ -370,10 +371,10 @@ inputs: - {typename: Tensor, name: x, optional: false, no_need_buffer: false, data_transform: {} } - {typename: Tensor, name: values, optional: false, no_need_buffer: false, data_transform: {} } - - {typename: Tensor, name: starts, optional: false, no_need_buffer: false, data_transform: {} } - - {typename: Tensor, name: ends, optional: false, no_need_buffer: false, data_transform: {} } - - {typename: Tensor, name: steps, optional: false, no_need_buffer: false, data_transform: {} } attrs: + - {typename: 'int64_t[]', name: starts} + - {typename: 'int64_t[]', name: ends} + - {typename: 'int64_t[]', name: steps} - {typename: 'int64_t[]', name: axes} - {typename: 'int64_t[]', name: decrease_axes} - {typename: 'int64_t[]', name: none_axes} @@ -387,3 +388,26 @@ param: [x, values, starts, ends, steps, axes, decrease_axes, none_axes] inplace: null backward: null + + +- name : set_value_grad + inputs: + - {typename: Tensor, name: out_grad, optional: false, no_need_buffer: false, data_transform: {} } + attrs: + - {typename: 'int64_t[]', name: starts} + - {typename: 'int64_t[]', name: ends} + - {typename: 'int64_t[]', name: steps} + - {typename: 'int64_t[]', name: axes} + - {typename: 'int64_t[]', name: decrease_axes} + - {typename: 'int64_t[]', name: none_axes} + outputs: + - {typename: Tensor, name: x_grad, optional: false, intermediate: false} + - {typename: Tensor, name: value_grad, optional: false, intermediate: false} + infer_meta: + func: SetValueInferMeta + param: [x] + kernel: + func: [set_value] + param: [x, starts, ends, steps, axes, decrease_axes, none_axes, shapes, values] + inplace: null + backward: null diff --git a/paddle/fluid/ir_adaptor/translator/attribute_translator.cc b/paddle/fluid/ir_adaptor/translator/attribute_translator.cc index 00890af7878e5f..a1aa26fb961167 100644 --- a/paddle/fluid/ir_adaptor/translator/attribute_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/attribute_translator.cc @@ -113,7 +113,7 @@ class AttributeVisitor { } virtual ir::Attribute operator()(const std::vector& i64s) { - VLOG(10) << "translating vector"; + VLOG(10) << "translating vector size: " << i64s.size(); std::vector attrs; attrs.reserve(i64s.size()); for (const auto& v : i64s) { @@ -169,6 +169,11 @@ class Int64ArrayAttributeVisitor : public AttributeVisitor { } return ir::ArrayAttribute::get(ctx, attrs); } + + ir::Attribute operator()(const paddle::blank& blank) override { + VLOG(10) << "translating paddle::blank to int64[]"; + return ir::ArrayAttribute::get(ctx, {}); + } }; class IntArrayAttributeVisitor : public AttributeVisitor { diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 3aabbcc9259d4d..bbf6ad58267d4c 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -3042,6 +3042,23 @@ data_type : int64_t tensors_name : StepsTensorList +- op: set_value_with_tensor + backward: set_value_grad + inputs: + x : Input + outputs: + out: Out + int_array: + starts: + data_type : int64_t + tensors_name : StartsTensorList + ends: + data_type : int64_t + tensors_name : EndsTensorList + steps: + data_type : int64_t + tensors_name : StepsTensorList + - op: sigmoid_cross_entropy_with_logits backward: sigmoid_cross_entropy_with_logits_grad inputs : diff --git a/paddle/phi/core/kernel_context.cc b/paddle/phi/core/kernel_context.cc index 2d4863957b4252..0db2795cef60d1 100644 --- a/paddle/phi/core/kernel_context.cc +++ b/paddle/phi/core/kernel_context.cc @@ -13,6 +13,10 @@ // limitations under the License. #include "paddle/phi/core/kernel_context.h" +#include "paddle/ir/core/type_name.h" +#include "paddle/phi/core/enforce.h" + +#include "glog/logging.h" namespace phi { @@ -116,10 +120,16 @@ const std::pair& KernelContext::OutputRangeAt(size_t idx) const { template const AttrType& KernelContext::AttrAt(size_t idx) const { try { + VLOG(6) << "Attribute at index `" << idx << "` trying"; + VLOG(6) << "Attribute at index `" << attrs_.at(idx).index() << "` trying"; + PADDLE_GET_CONST(AttrType, attrs_.at(idx)); return paddle::get(attrs_.at(idx)); } catch (paddle::bad_variant_access const& ex) { + VLOG(6) << "Attribute at index `" << idx << "` not found in kernel"; PADDLE_THROW(phi::errors::InvalidArgument( - "Attribute cast error in Op Kernel Context.")); + "Attribute %s %d cast error in Op Kernel Context.", + ir::get_type_name(), + attrs_.at(idx).index())); } } diff --git a/paddle/phi/core/kernel_utils.h b/paddle/phi/core/kernel_utils.h index 33af6abc83aa4c..1f95e013510011 100644 --- a/paddle/phi/core/kernel_utils.h +++ b/paddle/phi/core/kernel_utils.h @@ -29,6 +29,8 @@ #include "paddle/phi/core/tensor_array.h" #include "paddle/phi/core/type_defs.h" +#include "glog/logging.h" + namespace phi { // PD_KERNEL has been used by custom op api @@ -257,6 +259,7 @@ namespace phi { static void Compute(KernelContext* ctx, PreviousArgs&... pargs) { \ static_assert(out_idx == 0, \ "Kernel's Attributes should appear before Outputs."); \ + VLOG(0) << "[kernel] intarray" << attr_idx; \ const Attribute& t = ctx->AttrAt(attr_idx); \ static Attribute cmp_t = phi::TensorRef(nullptr); \ static Attribute vec_ref = \ From 2c7b9dcb67a30a6ed672775637d8e6e6b18159f2 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Wed, 23 Aug 2023 06:54:23 +0000 Subject: [PATCH 03/13] register set_value_grad --- paddle/fluid/ir/dialect/pd_op.yaml | 11 +-- .../ir_adaptor/translator/op_compat_info.h | 70 ++++++++++++------- .../ir_adaptor/translator/op_translator.cc | 15 ++++ paddle/phi/api/yaml/op_compat.yaml | 4 ++ paddle/phi/infermeta/backward.cc | 14 ++++ paddle/phi/infermeta/backward.h | 5 ++ 6 files changed, 89 insertions(+), 30 deletions(-) diff --git a/paddle/fluid/ir/dialect/pd_op.yaml b/paddle/fluid/ir/dialect/pd_op.yaml index 3744ae26d367fb..5cd23223fc1d40 100644 --- a/paddle/fluid/ir/dialect/pd_op.yaml +++ b/paddle/fluid/ir/dialect/pd_op.yaml @@ -393,6 +393,7 @@ - name : set_value_grad inputs: - {typename: Tensor, name: out_grad, optional: false, no_need_buffer: false, data_transform: {} } + - {typename: Tensor, name: values, optional: false, no_need_buffer: false, data_transform: {} } attrs: - {typename: 'int64_t[]', name: starts} - {typename: 'int64_t[]', name: ends} @@ -402,12 +403,12 @@ - {typename: 'int64_t[]', name: none_axes} outputs: - {typename: Tensor, name: x_grad, optional: false, intermediate: false} - - {typename: Tensor, name: value_grad, optional: false, intermediate: false} + - {typename: Tensor, name: values_grad, optional: false, intermediate: false} infer_meta: - func: SetValueInferMeta - param: [x] + func: SetValueGradInferMeta + param: [out_grad, values] kernel: - func: [set_value] - param: [x, starts, ends, steps, axes, decrease_axes, none_axes, shapes, values] + func: [set_value_grad] + param: [out_grad, starts, ends, steps, axes, decrease_axes, none_axes] inplace: null backward: null diff --git a/paddle/fluid/ir_adaptor/translator/op_compat_info.h b/paddle/fluid/ir_adaptor/translator/op_compat_info.h index 02276d0ee26bc4..6fd1426b6efd90 100644 --- a/paddle/fluid/ir_adaptor/translator/op_compat_info.h +++ b/paddle/fluid/ir_adaptor/translator/op_compat_info.h @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include #include @@ -75,42 +76,61 @@ class OpNameNormalizer { return op_mutable_attribute_infos.at(op_type).at(arg_name); } + std::optional GetDirectMapping(const std::string& op_type, + const std::string& arg_name) { + if (op_arg_name_mappings.find(op_type) == op_arg_name_mappings.end()) { + return {}; + } + auto& arg_mappings = op_arg_name_mappings[op_type]; + if (arg_mappings.find(arg_name) == arg_mappings.end()) { + return {}; + } + return arg_mappings.at(arg_name); + } + + std::optional GetGradNameMapping(const std::string& op_type, + const std::string& arg_name) { + std::string target = kPhiGradSuffix; + std::string data = kFluidVarGradSuffix; + + size_t first_grad_pos = arg_name.find(target); + size_t type_pos = op_type.find(target); + std::optional ret = this->GetDirectMapping( + op_type.substr(0, type_pos), arg_name.substr(0, first_grad_pos)); + if (!ret) { + return {}; + } + auto legacy_name = ret.value() + arg_name.substr(first_grad_pos); + for (size_t pos = 0; + legacy_name.npos != (pos = legacy_name.find(target, pos)); + pos += data.length()) { + legacy_name.replace(pos, target.length(), data); + } + return legacy_name; + } + std::string GetLegacyArgName(const std::string& op_type, const std::string& arg_name) { + if (auto ret = GetDirectMapping(op_type, arg_name)) { + return ret.value(); + } + bool is_grad_op = (op_type.find(kPhiGradSuffix) != std::string::npos); bool is_grad_arg = (arg_name.find(kPhiGradSuffix) != std::string::npos); if (is_grad_op && is_grad_arg) { - std::string target = kPhiGradSuffix; - std::string data = kFluidVarGradSuffix; - - size_t first_grad_pos = arg_name.find(target); - size_t type_pos = op_type.find(target); - std::string legacy_name = this->GetLegacyArgName( - op_type.substr(0, type_pos), arg_name.substr(0, first_grad_pos)); - legacy_name += arg_name.substr(first_grad_pos); - for (size_t pos = 0; - legacy_name.npos != (pos = legacy_name.find(target, pos)); - pos += data.length()) { - legacy_name.replace(pos, target.length(), data); + if (auto ret = GetGradNameMapping(op_type, arg_name)) { + return ret.value(); } - return legacy_name; } else if (is_grad_op && !is_grad_arg) { // backwward op using forward args: like trace_grad using forward input size_t type_pos = op_type.find(kPhiGradSuffix); - std::string legacy_name = - this->GetLegacyArgName(op_type.substr(0, type_pos), arg_name); - - return legacy_name; - } - if (op_arg_name_mappings.find(op_type) == op_arg_name_mappings.end()) { - return arg_name; - } - auto& arg_mappings = op_arg_name_mappings[op_type]; - if (arg_mappings.find(arg_name) == arg_mappings.end()) { - return arg_name; + if (auto ret = GetDirectMapping(op_type.substr(0, type_pos), arg_name)) { + return ret.value(); + } } - return arg_mappings.at(arg_name); + + return arg_name; } std::string GetLegacyAttrName(const std::string& op_type, diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.cc b/paddle/fluid/ir_adaptor/translator/op_translator.cc index 942e455bd29294..cdc5138bbf071f 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/op_translator.cc @@ -1536,6 +1536,20 @@ struct SetValueWithTensorOpTranscriber : public SetValueOpTranscriber { } }; +struct SetValueGradOpTranscriber : public SetValueWithTensorOpTranscriber { + ir::OpInfo LoopkUpOpInfo(ir::IrContext* ctx, const OpDesc& op_desc) override { + std::string target_op_name = dialect::SetValueGradOp::name(); + const auto& op_info = ctx->GetRegisteredOpInfo(target_op_name); + if (!op_info) { + IR_THROW( + "Op set_value_grad should have corresponding OpInfo " + "pd.set_value_grad"); + } + + return op_info; + } +}; + struct LegacySetValueDispatcher : public OpTranscriber { ir::Operation* operator()(ir::IrContext* ctx, TranslationContext* param_map, @@ -1576,6 +1590,7 @@ OpTranslator::OpTranslator() { special_handlers["rnn"] = RnnOpTranscriber(); special_handlers["shadow_output"] = ShadowOutputOpTranscriber(); special_handlers["set_value"] = LegacySetValueDispatcher(); + special_handlers["set_value_grad"] = SetValueGradOpTranscriber(); special_handlers["split"] = SplitOpTranscriber(); special_handlers["sum"] = AddNOpTranscriber(); diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index bbf6ad58267d4c..68e6a2f545cf4b 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -3042,6 +3042,10 @@ data_type : int64_t tensors_name : StepsTensorList +- op: set_value_grad + outputs: + values_grad: ValueTensor@GRAD + - op: set_value_with_tensor backward: set_value_grad inputs: diff --git a/paddle/phi/infermeta/backward.cc b/paddle/phi/infermeta/backward.cc index a1b1be2861b3d1..e17bdd8fc298f9 100644 --- a/paddle/phi/infermeta/backward.cc +++ b/paddle/phi/infermeta/backward.cc @@ -1247,4 +1247,18 @@ void FusedRopeGradInferMeta(const MetaTensor& sin, } } +void SetValueGradInferMeta(const MetaTensor& out_grad, + const MetaTensor& values, + MetaTensor* x_grad, + MetaTensor* value_grad) { + if (x_grad) { + x_grad->set_dims(out_grad.dims()); + x_grad->share_lod(out_grad); + } + if (value_grad) { + value_grad->set_dims(values.dims()); + value_grad->share_lod(values); + } +} + } // namespace phi diff --git a/paddle/phi/infermeta/backward.h b/paddle/phi/infermeta/backward.h index 184c8d10fb0748..b8a3aee03bde45 100644 --- a/paddle/phi/infermeta/backward.h +++ b/paddle/phi/infermeta/backward.h @@ -476,4 +476,9 @@ void IndexPutGradInferMeta(const MetaTensor& x, bool accumulate, MetaTensor* x_grad, MetaTensor* value_grad); + +void SetValueGradInferMeta(const MetaTensor& out_grad, + const MetaTensor& values, + MetaTensor* x_grad, + MetaTensor* value_grad); } // namespace phi From bf6081a985d50dbf5fdb5ba49f6ab05f042023ea Mon Sep 17 00:00:00 2001 From: kangguangli Date: Wed, 23 Aug 2023 07:41:14 +0000 Subject: [PATCH 04/13] fix --- paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h | 1 - 1 file changed, 1 deletion(-) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h b/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h index 37063cc8217c46..870a3e25c5773e 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h +++ b/paddle/fluid/ir/dialect/paddle_dialect/utils/utils.h @@ -16,7 +16,6 @@ // #include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/framework/data_type.h" -#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_attribute.h" #include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_type_storage.h" #include "paddle/ir/core/builtin_attribute.h" #include "paddle/ir/core/builtin_type.h" From 1d7ae83e058a256505c04ffec214fba527e59196 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Wed, 23 Aug 2023 12:20:13 +0000 Subject: [PATCH 05/13] fix --- paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml index 2234286ce488a3..2a5427f440999e 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml @@ -365,7 +365,7 @@ - {typename: 'int64_t[]', name: axes} - {typename: 'int64_t[]', name: decrease_axes} - {typename: 'int64_t[]', name: none_axes} - - {typename: 'int64_t[]', name: shapes} + - {typename: 'int64_t[]', name: shape} - {typename: 'Scalar[]', name: values} outputs: - {typename: Tensor, name: out, optional: false, intermediate: false} @@ -374,7 +374,7 @@ param: [x] kernel: func: [set_value] - param: [x, starts, ends, steps, axes, decrease_axes, none_axes, shapes, values] + param: [x, starts, ends, steps, axes, decrease_axes, none_axes, shape, values] inplace: null backward: null From 6faf2abd403c256cd92f7efcc3e2aa79795b6669 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Thu, 24 Aug 2023 06:28:59 +0000 Subject: [PATCH 06/13] remove debug info --- paddle/fluid/framework/executor_cache.cc | 11 ----------- .../instruction/phi_kernel_instruction.cc | 13 ------------- .../fluid/ir/dialect/op_generator/op_gen.py | 1 - paddle/phi/core/kernel_context.cc | 12 +----------- paddle/phi/core/kernel_utils.h | 3 --- paddle/phi/infermeta/unary.cc | 2 -- .../phi/kernels/impl/set_value_kernel_impl.h | 13 ------------- python/paddle/fluid/executor.py | 3 --- .../paddle/jit/dy2static/partial_program.py | 19 ------------------- 9 files changed, 1 insertion(+), 76 deletions(-) diff --git a/paddle/fluid/framework/executor_cache.cc b/paddle/fluid/framework/executor_cache.cc index 0f8447cc5eebe7..959d4a4157ba36 100644 --- a/paddle/fluid/framework/executor_cache.cc +++ b/paddle/fluid/framework/executor_cache.cc @@ -20,8 +20,6 @@ #include "paddle/ir/core/program.h" #include "paddle/ir/core/value.h" -#include // NOLINT - namespace paddle { namespace framework { class ProgramDesc; @@ -439,12 +437,8 @@ std::unique_ptr<::ir::Program> ConstructFowardIrProgram( program.get()); program_translator.Translate(); - program->Print(std::cout); - std::cout << std::endl; auto ir_res = paddle::dialect::PdOpLowerToKernelPass(program.get()); - ir_res->Print(std::cout); - std::cout << std::endl; return ir_res; } @@ -517,12 +511,7 @@ std::unique_ptr<::ir::Program> ConstructBackwardIrProgram( program.get()); program_translator.Translate(); - program->Print(std::cout); - std::cout << std::endl; - auto res = paddle::dialect::PdOpLowerToKernelPass(program.get()); - res->Print(std::cout); - std::cout << std::endl; return res; } diff --git a/paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.cc b/paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.cc index f58f16a5f8f2b7..093435f8b98a2f 100644 --- a/paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.cc +++ b/paddle/fluid/framework/new_executor/instruction/phi_kernel_instruction.cc @@ -32,8 +32,6 @@ #include "paddle/ir/core/operation.h" #include "paddle/ir/core/value.h" -#include "paddle/phi/core/kernel_context.h" - #include "paddle/fluid/framework/new_executor/instruction/instruction_util.h" namespace paddle { namespace framework { @@ -169,22 +167,11 @@ PhiKernelInstruction::PhiKernelInstruction( VLOG(6) << "finish process no need buffer"; } -void PrintKernelContext(const phi::KernelContext& kernel_context) { - VLOG(6) << "------------kernel_contxt start-----------"; - VLOG(6) << "inputs: " << kernel_context.InputsSize(); - VLOG(6) << "output: " << kernel_context.OutputsSize(); - VLOG(6) << "attrs: " << kernel_context.AttrsSize(); - VLOG(6) << "------------kernel_contxt end-------------"; -} - void PhiKernelInstruction::Run() { if (infer_meta_interface_) { infer_meta_interface_->infer_meta_(&(infer_meta_context_)); } VLOG(6) << "Run op " << phi_op_name_ << " infer meta."; - - PrintKernelContext(kernel_context_); - (*(phi_kernel_))(&(kernel_context_)); VLOG(6) << "Run op " << phi_op_name_ << " kernel."; } diff --git a/paddle/fluid/ir/dialect/op_generator/op_gen.py b/paddle/fluid/ir/dialect/op_generator/op_gen.py index 786640db907747..2042c626f067be 100644 --- a/paddle/fluid/ir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/ir/dialect/op_generator/op_gen.py @@ -720,7 +720,6 @@ def OpGenerator( op_info_items[op['name']] = OpInfoParser( op, op_compat_parser.get_compat(op['name']) ) - # (3) CodeGen: Traverse op_info_items and generate ops_name_list = [] # all op class name store in this list ops_declare_list = [] # all op class declare store in this list diff --git a/paddle/phi/core/kernel_context.cc b/paddle/phi/core/kernel_context.cc index 0db2795cef60d1..2d4863957b4252 100644 --- a/paddle/phi/core/kernel_context.cc +++ b/paddle/phi/core/kernel_context.cc @@ -13,10 +13,6 @@ // limitations under the License. #include "paddle/phi/core/kernel_context.h" -#include "paddle/ir/core/type_name.h" -#include "paddle/phi/core/enforce.h" - -#include "glog/logging.h" namespace phi { @@ -120,16 +116,10 @@ const std::pair& KernelContext::OutputRangeAt(size_t idx) const { template const AttrType& KernelContext::AttrAt(size_t idx) const { try { - VLOG(6) << "Attribute at index `" << idx << "` trying"; - VLOG(6) << "Attribute at index `" << attrs_.at(idx).index() << "` trying"; - PADDLE_GET_CONST(AttrType, attrs_.at(idx)); return paddle::get(attrs_.at(idx)); } catch (paddle::bad_variant_access const& ex) { - VLOG(6) << "Attribute at index `" << idx << "` not found in kernel"; PADDLE_THROW(phi::errors::InvalidArgument( - "Attribute %s %d cast error in Op Kernel Context.", - ir::get_type_name(), - attrs_.at(idx).index())); + "Attribute cast error in Op Kernel Context.")); } } diff --git a/paddle/phi/core/kernel_utils.h b/paddle/phi/core/kernel_utils.h index 1f95e013510011..33af6abc83aa4c 100644 --- a/paddle/phi/core/kernel_utils.h +++ b/paddle/phi/core/kernel_utils.h @@ -29,8 +29,6 @@ #include "paddle/phi/core/tensor_array.h" #include "paddle/phi/core/type_defs.h" -#include "glog/logging.h" - namespace phi { // PD_KERNEL has been used by custom op api @@ -259,7 +257,6 @@ namespace phi { static void Compute(KernelContext* ctx, PreviousArgs&... pargs) { \ static_assert(out_idx == 0, \ "Kernel's Attributes should appear before Outputs."); \ - VLOG(0) << "[kernel] intarray" << attr_idx; \ const Attribute& t = ctx->AttrAt(attr_idx); \ static Attribute cmp_t = phi::TensorRef(nullptr); \ static Attribute vec_ref = \ diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 47bf37dde7c237..db4964dc3dd408 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -3528,8 +3528,6 @@ void SetValueInferMeta(const MetaTensor& x, MetaTensor* out) { phi::errors::InvalidArgument( "The rank of input should be less than 7, but received %d.", in_dims.size())); - VLOG(0) << "[debug][set_value] input dims:" << in_dims; - VLOG(0) << "[debug][set_value] out:" << out; out->set_dims(in_dims); } diff --git a/paddle/phi/kernels/impl/set_value_kernel_impl.h b/paddle/phi/kernels/impl/set_value_kernel_impl.h index 3e682333e5521d..2c545ac06ada11 100644 --- a/paddle/phi/kernels/impl/set_value_kernel_impl.h +++ b/paddle/phi/kernels/impl/set_value_kernel_impl.h @@ -25,8 +25,6 @@ #include "paddle/phi/kernels/funcs/elementwise_functor.h" #include "paddle/phi/kernels/funcs/slice_utils.h" -#include "glog/logging.h" - namespace phi { // check whether the tensor with dimension of second can assign to the @@ -82,11 +80,9 @@ void SetValueImpl(const Context& dev_ctx, const std::vector& none_axes, DenseTensor* out) { auto in_dims = in.dims(); - LOG(INFO) << "in: " << in_dims; std::vector starts_local = starts.GetData(); std::vector ends_local = ends.GetData(); std::vector steps_local = steps.GetData(); - LOG(INFO) << "starts: " << starts_local.size(); phi::funcs::CheckAndUpdateSliceAttrs( in_dims, axes, &starts_local, &ends_local, &steps_local); auto slice_dims = phi::funcs::GetSliceDims( @@ -120,8 +116,6 @@ void SetValueImpl(const Context& dev_ctx, slice_dims_for_assign = phi::make_ddim(slice_dims_with_none); } - LOG(INFO) << "slice_dims_for_assign: " << slice_dims_for_assign; - auto place = dev_ctx.GetPlace(); auto& eigen_place = *dev_ctx.eigen_device(); @@ -171,7 +165,6 @@ void SetValueImpl(const Context& dev_ctx, out_e.stridedSlice(starts_indices, ends_indices, strides_indices) .device(eigen_place) = slice_e; - LOG(INFO) << "out e stridedSlicew"; // Step 2: Set a tensor with the same shape as out tensor. And its data at // '_index' is the same as value, and data out of '_index' to zero @@ -207,7 +200,6 @@ void SetValueImpl(const Context& dev_ctx, // // TODO(zoooo0820): Reimplement logic of set_value to avoid using // elementwise-sub. - LOG(INFO) << "Set value on GPU: "; funcs::ElementwiseCompute, T>( dev_ctx, slice_tensor, @@ -215,7 +207,6 @@ void SetValueImpl(const Context& dev_ctx, funcs::SubtractFunctor(), &slice_tensor); } else { - LOG(INFO) << "Set value not gpu: "; funcs::ElementwiseCompute, T>( dev_ctx, slice_tensor, @@ -229,7 +220,6 @@ void SetValueImpl(const Context& dev_ctx, pad_e.device(eigen_place) = pad_e.constant(T(0)); pad_e.stridedSlice(starts_indices, ends_indices, strides_indices) .device(eigen_place) = slice_e; - LOG(INFO) << "slice_tensor"; // Step 3: Set out tensor with value out_e.device(eigen_place) = out_e - pad_e; @@ -246,10 +236,7 @@ void SetTensorValueKernel(const Context& dev_ctx, const std::vector& decrease_axes, const std::vector& none_axes, DenseTensor* out) { - LOG(INFO) << x.dims(); - const int rank = x.dims().size(); - LOG(INFO) << "rank " << rank; switch (rank) { case 1: diff --git a/python/paddle/fluid/executor.py b/python/paddle/fluid/executor.py index 5f54582263f26a..913a29ff6171af 100755 --- a/python/paddle/fluid/executor.py +++ b/python/paddle/fluid/executor.py @@ -1007,9 +1007,6 @@ def _get_program_and_executor(self, cached_data): _apply_inplace_addto_pass( program, enable_inplace, enable_addto, skip_var_names ) - print('-------------[debug][program start]------------------') - print(program) - print('-------------[debug][program end]------------------', flush=True) new_program = program.clone() if ( diff --git a/python/paddle/jit/dy2static/partial_program.py b/python/paddle/jit/dy2static/partial_program.py index 46f224fbe1f780..0264d645a0676b 100644 --- a/python/paddle/jit/dy2static/partial_program.py +++ b/python/paddle/jit/dy2static/partial_program.py @@ -815,25 +815,6 @@ def _get_forward_backward_program_form( self._apply_inplace_pass( forward_builded_program, backward_builded_program ) - - print( - '-------------[debug][forward_builded_program start]------------------' - ) - print(forward_builded_program) - print( - '-------------[debug][forward_builded_program end]------------------', - flush=True, - ) - - print( - '-------------[debug][backward_builded_program start]------------------' - ) - print(backward_builded_program) - print( - '-------------[debug][backward_builded_program end]------------------', - flush=True, - ) - return [forward_builded_program, backward_builded_program] def _apply_inplace_pass(self, forward_program, backward_program): From 387feb69a81dc697f073462944976eb2b4d95c66 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Thu, 24 Aug 2023 06:42:02 +0000 Subject: [PATCH 07/13] add unittest --- test/ir/new_ir/test_special_op_translator.py | 57 ++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test/ir/new_ir/test_special_op_translator.py b/test/ir/new_ir/test_special_op_translator.py index 84ef14714d8fba..30ad9ca1b2eaa2 100644 --- a/test/ir/new_ir/test_special_op_translator.py +++ b/test/ir/new_ir/test_special_op_translator.py @@ -325,6 +325,63 @@ def test_op(self): l = ir.translate_to_new_ir(main_program.desc) +class TestSetValueOp(unittest.TestCase): + def test_no_mutable_attribute(self): + place = core.Place() + place.set_place(paddle.CPUPlace()) + main_program = paddle.static.Program() + with paddle.static.program_guard(main_program): + x = paddle.ones(shape=self.shape, dtype=self.dtype) + x = paddle.static.setitem(x, (0, 0), 6) + _ = ir.translate_to_new_ir(main_program.desc) + + def test_with_mutable_attribute(self): + place = core.Place() + place.set_place(paddle.CPUPlace()) + main_program = paddle.static.Program() + with paddle.static.program_guard(main_program): + x = paddle.ones(shape=self.shape, dtype=self.dtype) + zero = paddle.full([], 0, dtype="int32") + x = paddle.static.setitem(x, zero, 6) + _ = ir.translate_to_new_ir(main_program.desc) + + def test_grad(self): + place = core.Place() + place.set_place(paddle.CPUPlace()) + main_program = paddle.static.Program() + with paddle.static.program_guard(main_program): + x = paddle.static.data( + name="x", shape=[7, 6, 5, 4, 3, 2], dtype='float32' + ) + value = paddle.tensor.fill_constant([1, 3, 2], "float32", 1) + # test stop_gradient + value.stop_gradient = False + x.stop_gradient = False + attrs = { + 'axes': [0], + 'starts': [6], + 'ends': [0], + 'steps': [-4], + 'decrease_axes': [], + 'none_axes': [], + 'dtype': paddle.float32, + } + inputs = {'Input': x, 'ValueTensor': value} + + helper = LayerHelper("set_value") + y = helper.create_variable_for_type_inference(dtype=x.dtype) + + helper.append_op( + type="set_value", inputs=inputs, outputs={'Out': y}, attrs=attrs + ) + y2 = y + 1 + loss = paddle.sum(y2) + opt = paddle.optimizer.Adam() + opt.minimize(loss) + + _ = ir.translate_to_new_ir(main_program.desc) + + class TestCheckUnregisteredOp(unittest.TestCase): def test_program(self): main_program = paddle.static.Program() From e739e64700a54492e35208138e688f31a02e9aff Mon Sep 17 00:00:00 2001 From: kangguangli Date: Thu, 24 Aug 2023 07:13:11 +0000 Subject: [PATCH 08/13] fix --- .../ir_adaptor/translator/op_translator.cc | 60 ------------------- 1 file changed, 60 deletions(-) diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.cc b/paddle/fluid/ir_adaptor/translator/op_translator.cc index 0f138f23d6fa39..cad900ee7f6da0 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/op_translator.cc @@ -210,66 +210,6 @@ inline ir::Operation* InsertStackOperationForTarget( } // namespace -/// @brief This class is used to translate a OpDesc, it's a functor class and -/// should have no non-static data member, since we expected it's stateless. -struct OpTranscriber { - public: - virtual ~OpTranscriber() = default; - - public: - virtual ir::Operation* operator()(ir::IrContext* ctx, - TranslationContext* param_map, - const OpDesc& op_desc, - ir::Program* program); - - public: - virtual ir::OpInfo LoopkUpOpInfo(ir::IrContext* ctx, const OpDesc& op_desc); - virtual std::vector GenerateOperationInput( - ir::IrContext* ctx, - TranslationContext* param_map, - const OpDesc& op_desc, - const std::string& normalized_op_name, - const OpInputInfoList& input_infos, - ir::Program* program); - virtual std::tuple GenerateOperationOutput( - ir::IrContext* ctx, - const OpDesc& op_desc, - const OpOutputInfoList& output_infos); - virtual void HandleNonexistentAttribute(ir::IrContext*, - ir::AttributeMap* attribute_map, - const OpAttributeInfo& info) { - auto& attribute_translator = AttributeTranslator::instance(); - (*attribute_map)[info.name] = - attribute_translator(info.type_name, paddle::framework::Attribute()); - } - virtual ir::AttributeMap TranslateOpAttribute( - ir::IrContext* ctx, - const std::string& normalized_op_name, - const OpAttributeInfoList& op_attr_infos, - const OpDesc& op_desc); - - virtual void RecordOpResultMapping(ir::IrContext* ctx, - TranslationContext* param_map, - const OpDesc& op_desc, - ir::Operation* operation, - const OpOutputMapping& arg_to_idx); - - public: - virtual InputHandlerFn GetSpecialInputHandlers( - const std::string& input_name) { - return nullptr; - } - virtual AttributeHandlerFn GetSpecialAttributeHandlers( - const std::string& input_name) { - return nullptr; - } - virtual void InsertSliceOperationForInput(ir::IrContext* ctx, - TranslationContext* param_map, - const OpDesc& op_desc, - const OpInputInfoList& input_infos, - ir::Program* program); -}; - ir::OpInfo OpTranscriber::LoopkUpOpInfo(ir::IrContext* ctx, const OpDesc& op_desc) { std::string target_op_name = From 117750e9ac872bc9588a68b1c2c34f89f4070cb1 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Thu, 24 Aug 2023 07:19:07 +0000 Subject: [PATCH 09/13] fix --- paddle/fluid/ir_adaptor/translator/op_translator.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.h b/paddle/fluid/ir_adaptor/translator/op_translator.h index 9acff2d9d70688..afc7566be12b3e 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.h +++ b/paddle/fluid/ir_adaptor/translator/op_translator.h @@ -85,6 +85,10 @@ struct OpTranscriber { const std::string& normalized_op_name, const OpAttributeInfoList& op_attr_infos, const OpDesc& op_desc); + virtual ir::OpResult GetAttributeAsInput(ir::IrContext* ctx, + ir::Program* program, + const OpDesc& op_desc, + const OpInputInfo& input_info); virtual void RecordOpResultMapping(ir::IrContext* ctx, TranslationContext* param_map, From 971f4fbdcdf3c9aed03afd78e37d82ed5399bfed Mon Sep 17 00:00:00 2001 From: kangguangli Date: Thu, 24 Aug 2023 07:48:27 +0000 Subject: [PATCH 10/13] fix --- .../ir_adaptor/translator/op_compat_info.h | 15 ++-- paddle/phi/infermeta/backward.cc | 2 + test/ir/new_ir/test_special_op_translator.py | 4 +- test/legacy_test/test_i0_op.py | 73 ------------------- 4 files changed, 14 insertions(+), 80 deletions(-) diff --git a/paddle/fluid/ir_adaptor/translator/op_compat_info.h b/paddle/fluid/ir_adaptor/translator/op_compat_info.h index 6fd1426b6efd90..fa13415ffdfd66 100644 --- a/paddle/fluid/ir_adaptor/translator/op_compat_info.h +++ b/paddle/fluid/ir_adaptor/translator/op_compat_info.h @@ -95,12 +95,13 @@ class OpNameNormalizer { size_t first_grad_pos = arg_name.find(target); size_t type_pos = op_type.find(target); - std::optional ret = this->GetDirectMapping( - op_type.substr(0, type_pos), arg_name.substr(0, first_grad_pos)); - if (!ret) { - return {}; + std::string legacy_name = arg_name.substr(0, first_grad_pos); + std::optional ret = + this->GetDirectMapping(op_type.substr(0, type_pos), legacy_name); + if (ret) { + legacy_name = ret.value(); } - auto legacy_name = ret.value() + arg_name.substr(first_grad_pos); + legacy_name = legacy_name + arg_name.substr(first_grad_pos); for (size_t pos = 0; legacy_name.npos != (pos = legacy_name.find(target, pos)); pos += data.length()) { @@ -112,6 +113,7 @@ class OpNameNormalizer { std::string GetLegacyArgName(const std::string& op_type, const std::string& arg_name) { if (auto ret = GetDirectMapping(op_type, arg_name)) { + VLOG(10) << "[" << op_type << "] found " << ret.value(); return ret.value(); } @@ -120,16 +122,19 @@ class OpNameNormalizer { if (is_grad_op && is_grad_arg) { if (auto ret = GetGradNameMapping(op_type, arg_name)) { + VLOG(10) << "[" << op_type << "] found " << ret.value(); return ret.value(); } } else if (is_grad_op && !is_grad_arg) { // backwward op using forward args: like trace_grad using forward input size_t type_pos = op_type.find(kPhiGradSuffix); if (auto ret = GetDirectMapping(op_type.substr(0, type_pos), arg_name)) { + VLOG(10) << "[" << op_type << "] found " << ret.value(); return ret.value(); } } + VLOG(10) << "[" << op_type << "] not found mapping for " << arg_name; return arg_name; } diff --git a/paddle/phi/infermeta/backward.cc b/paddle/phi/infermeta/backward.cc index e17bdd8fc298f9..31da8f079bb432 100644 --- a/paddle/phi/infermeta/backward.cc +++ b/paddle/phi/infermeta/backward.cc @@ -1253,10 +1253,12 @@ void SetValueGradInferMeta(const MetaTensor& out_grad, MetaTensor* value_grad) { if (x_grad) { x_grad->set_dims(out_grad.dims()); + x_grad->set_dtype(out_grad.dtype()); x_grad->share_lod(out_grad); } if (value_grad) { value_grad->set_dims(values.dims()); + value_grad->set_dtype(values.dtype()); value_grad->share_lod(values); } } diff --git a/test/ir/new_ir/test_special_op_translator.py b/test/ir/new_ir/test_special_op_translator.py index 30ad9ca1b2eaa2..e6ab9e3d92b463 100644 --- a/test/ir/new_ir/test_special_op_translator.py +++ b/test/ir/new_ir/test_special_op_translator.py @@ -331,7 +331,7 @@ def test_no_mutable_attribute(self): place.set_place(paddle.CPUPlace()) main_program = paddle.static.Program() with paddle.static.program_guard(main_program): - x = paddle.ones(shape=self.shape, dtype=self.dtype) + x = paddle.ones(shape=[2, 3, 4], dtype="float32") x = paddle.static.setitem(x, (0, 0), 6) _ = ir.translate_to_new_ir(main_program.desc) @@ -340,7 +340,7 @@ def test_with_mutable_attribute(self): place.set_place(paddle.CPUPlace()) main_program = paddle.static.Program() with paddle.static.program_guard(main_program): - x = paddle.ones(shape=self.shape, dtype=self.dtype) + x = paddle.ones(shape=[2, 3, 4], dtype="float32") zero = paddle.full([], 0, dtype="int32") x = paddle.static.setitem(x, zero, 6) _ = ir.translate_to_new_ir(main_program.desc) diff --git a/test/legacy_test/test_i0_op.py b/test/legacy_test/test_i0_op.py index a30f67312c8d02..a5512c5829f64b 100644 --- a/test/legacy_test/test_i0_op.py +++ b/test/legacy_test/test_i0_op.py @@ -19,7 +19,6 @@ from scipy import special import paddle -from paddle.fluid import core np.random.seed(100) paddle.seed(100) @@ -34,78 +33,6 @@ def ref_i0_grad(x, dout): return dout * gradx -class TestI0API(unittest.TestCase): - DTYPE = "float64" - DATA = [0, 1, 2, 3, 4, 5] - - def setUp(self): - self.x = np.array(self.DATA).astype(self.DTYPE) - self.place = [paddle.CPUPlace()] - if core.is_compiled_with_cuda(): - self.place.append(paddle.CUDAPlace(0)) - - def test_api_static(self): - def run(place): - paddle.enable_static() - with paddle.static.program_guard(paddle.static.Program()): - x = paddle.static.data( - name="x", shape=self.x.shape, dtype=self.DTYPE - ) - out = paddle.i0(x) - exe = paddle.static.Executor(place) - res = exe.run( - paddle.static.default_main_program(), - feed={"x": self.x}, - fetch_list=[out], - ) - out_ref = output_i0(self.x) - np.testing.assert_allclose(res[0], out_ref, rtol=1e-5) - paddle.disable_static() - - for place in self.place: - run(place) - - def test_api_dygraph(self): - def run(place): - paddle.disable_static(place) - x = paddle.to_tensor(self.x) - out = paddle.i0(x) - - out_ref = output_i0(self.x) - np.testing.assert_allclose(out.numpy(), out_ref, rtol=1e-5) - paddle.enable_static() - - for place in self.place: - run(place) - - def test_empty_input_error(self): - for place in self.place: - paddle.disable_static(place) - x = None - self.assertRaises(ValueError, paddle.i0, x) - paddle.enable_static() - - -class TestI0Float32Zero2EightCase(TestI0API): - DTYPE = "float32" - DATA = [0, 1, 2, 3, 4, 5, 6, 7, 8] - - -class TestI0Float32OverEightCase(TestI0API): - DTYPE = "float32" - DATA = [9, 10, 11, 12] - - -class TestI0Float64Zero2EightCase(TestI0API): - DTYPE = "float64" - DATA = [0, 1, 2, 3, 4, 5, 6, 7, 8] - - -class TestI0Float64OverEightCase(TestI0API): - DTYPE = "float64" - DATA = [9, 10, 11, 12] - - class TestI0Op(OpTest): def setUp(self) -> None: self.op_type = "i0" From 33df9c90f1c0b80fc35c4cbcfcc8641ffc384c6e Mon Sep 17 00:00:00 2001 From: kangguangli Date: Thu, 24 Aug 2023 07:49:43 +0000 Subject: [PATCH 11/13] fix --- test/legacy_test/test_i0_op.py | 73 ++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/test/legacy_test/test_i0_op.py b/test/legacy_test/test_i0_op.py index a5512c5829f64b..a30f67312c8d02 100644 --- a/test/legacy_test/test_i0_op.py +++ b/test/legacy_test/test_i0_op.py @@ -19,6 +19,7 @@ from scipy import special import paddle +from paddle.fluid import core np.random.seed(100) paddle.seed(100) @@ -33,6 +34,78 @@ def ref_i0_grad(x, dout): return dout * gradx +class TestI0API(unittest.TestCase): + DTYPE = "float64" + DATA = [0, 1, 2, 3, 4, 5] + + def setUp(self): + self.x = np.array(self.DATA).astype(self.DTYPE) + self.place = [paddle.CPUPlace()] + if core.is_compiled_with_cuda(): + self.place.append(paddle.CUDAPlace(0)) + + def test_api_static(self): + def run(place): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data( + name="x", shape=self.x.shape, dtype=self.DTYPE + ) + out = paddle.i0(x) + exe = paddle.static.Executor(place) + res = exe.run( + paddle.static.default_main_program(), + feed={"x": self.x}, + fetch_list=[out], + ) + out_ref = output_i0(self.x) + np.testing.assert_allclose(res[0], out_ref, rtol=1e-5) + paddle.disable_static() + + for place in self.place: + run(place) + + def test_api_dygraph(self): + def run(place): + paddle.disable_static(place) + x = paddle.to_tensor(self.x) + out = paddle.i0(x) + + out_ref = output_i0(self.x) + np.testing.assert_allclose(out.numpy(), out_ref, rtol=1e-5) + paddle.enable_static() + + for place in self.place: + run(place) + + def test_empty_input_error(self): + for place in self.place: + paddle.disable_static(place) + x = None + self.assertRaises(ValueError, paddle.i0, x) + paddle.enable_static() + + +class TestI0Float32Zero2EightCase(TestI0API): + DTYPE = "float32" + DATA = [0, 1, 2, 3, 4, 5, 6, 7, 8] + + +class TestI0Float32OverEightCase(TestI0API): + DTYPE = "float32" + DATA = [9, 10, 11, 12] + + +class TestI0Float64Zero2EightCase(TestI0API): + DTYPE = "float64" + DATA = [0, 1, 2, 3, 4, 5, 6, 7, 8] + + +class TestI0Float64OverEightCase(TestI0API): + DTYPE = "float64" + DATA = [9, 10, 11, 12] + + class TestI0Op(OpTest): def setUp(self) -> None: self.op_type = "i0" From 52a4185b7576634067ab90ab28c40b70716abfde Mon Sep 17 00:00:00 2001 From: kangguangli Date: Thu, 24 Aug 2023 08:51:26 +0000 Subject: [PATCH 12/13] fix --- paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml index 2a5427f440999e..21e2c05d59ecb5 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml @@ -376,7 +376,7 @@ func: [set_value] param: [x, starts, ends, steps, axes, decrease_axes, none_axes, shape, values] inplace: null - backward: null + backward: set_value_grad - name : set_value_with_tensor inputs: @@ -398,7 +398,7 @@ func: [set_value_with_tensor] param: [x, values, starts, ends, steps, axes, decrease_axes, none_axes] inplace: null - backward: null + backward: set_value_grad - name : set_value_grad From 775f8962884959a2324ec1dea669209e8c1607e0 Mon Sep 17 00:00:00 2001 From: kangguangli Date: Fri, 25 Aug 2023 07:15:06 +0000 Subject: [PATCH 13/13] resolve comments --- .../ir/dialect/paddle_dialect/ir/pd_op.yaml | 4 +- .../ir_adaptor/translator/op_compat_gen.py | 3 + paddle/phi/api/yaml/op_compat.yaml | 4 - test/ir/new_ir/test_special_op_translator.py | 104 +++++++++++------- 4 files changed, 70 insertions(+), 45 deletions(-) diff --git a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml index 21e2c05d59ecb5..ac76e130aac863 100644 --- a/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml +++ b/paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.yaml @@ -375,7 +375,7 @@ kernel: func: [set_value] param: [x, starts, ends, steps, axes, decrease_axes, none_axes, shape, values] - inplace: null + inplace: {out: x} backward: set_value_grad - name : set_value_with_tensor @@ -397,7 +397,7 @@ kernel: func: [set_value_with_tensor] param: [x, values, starts, ends, steps, axes, decrease_axes, none_axes] - inplace: null + inplace: {out: x} backward: set_value_grad diff --git a/paddle/fluid/ir_adaptor/translator/op_compat_gen.py b/paddle/fluid/ir_adaptor/translator/op_compat_gen.py index b69c49771033c6..7c1a78305e71cc 100644 --- a/paddle/fluid/ir_adaptor/translator/op_compat_gen.py +++ b/paddle/fluid/ir_adaptor/translator/op_compat_gen.py @@ -126,6 +126,9 @@ def insert_new_mutable_attributes( backward_op, op_compat_item["scalar"] ) + # special mapping list + op_arg_name_mappings["set_value_grad"]["values_grad"] = "ValueTensor@GRAD" + op_name_normailzer_template = env.get_template("op_compat_info.cc.j2") with open(output_source_file, 'wt') as f: op_compat_definition = op_name_normailzer_template.render( diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 266d445e155ba2..bda47baba89448 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -3067,10 +3067,6 @@ data_type : int64_t tensors_name : StepsTensorList -- op: set_value_grad - outputs: - values_grad: ValueTensor@GRAD - - op: set_value_with_tensor backward: set_value_grad inputs: diff --git a/test/ir/new_ir/test_special_op_translator.py b/test/ir/new_ir/test_special_op_translator.py index e6ab9e3d92b463..5763ad03e59486 100644 --- a/test/ir/new_ir/test_special_op_translator.py +++ b/test/ir/new_ir/test_special_op_translator.py @@ -329,57 +329,83 @@ class TestSetValueOp(unittest.TestCase): def test_no_mutable_attribute(self): place = core.Place() place.set_place(paddle.CPUPlace()) + exe = paddle.static.Executor(place) + + new_scope = paddle.static.Scope() main_program = paddle.static.Program() - with paddle.static.program_guard(main_program): - x = paddle.ones(shape=[2, 3, 4], dtype="float32") - x = paddle.static.setitem(x, (0, 0), 6) - _ = ir.translate_to_new_ir(main_program.desc) + with paddle.static.scope_guard(new_scope): + with paddle.static.program_guard(main_program): + x = paddle.ones(shape=[2, 3, 4], dtype="float32") + x = paddle.static.setitem(x, (0, 0), 6) + ret = exe.run(main_program, fetch_list=x.name) + + x_data = np.ones([2, 3, 4]).astype("float32") + x_data[0, 0] = 6 + np.testing.assert_array_equal(ret[0], x_data) def test_with_mutable_attribute(self): place = core.Place() place.set_place(paddle.CPUPlace()) + exe = paddle.static.Executor(place) + + new_scope = paddle.static.Scope() main_program = paddle.static.Program() - with paddle.static.program_guard(main_program): - x = paddle.ones(shape=[2, 3, 4], dtype="float32") - zero = paddle.full([], 0, dtype="int32") - x = paddle.static.setitem(x, zero, 6) - _ = ir.translate_to_new_ir(main_program.desc) + with paddle.static.scope_guard(new_scope): + with paddle.static.program_guard(main_program): + x = paddle.ones(shape=[2, 3, 4], dtype="float32") + zero = paddle.full([], 0, dtype="int32") + x = paddle.static.setitem(x, zero, 6) + ret = exe.run(main_program, fetch_list=x.name) + + x_data = np.ones([2, 3, 4]).astype("float32") + x_data[0] = 6 + np.testing.assert_array_equal(ret[0], x_data) def test_grad(self): place = core.Place() place.set_place(paddle.CPUPlace()) + exe = paddle.static.Executor(place) + new_scope = paddle.static.Scope() main_program = paddle.static.Program() - with paddle.static.program_guard(main_program): - x = paddle.static.data( - name="x", shape=[7, 6, 5, 4, 3, 2], dtype='float32' - ) - value = paddle.tensor.fill_constant([1, 3, 2], "float32", 1) - # test stop_gradient - value.stop_gradient = False - x.stop_gradient = False - attrs = { - 'axes': [0], - 'starts': [6], - 'ends': [0], - 'steps': [-4], - 'decrease_axes': [], - 'none_axes': [], - 'dtype': paddle.float32, - } - inputs = {'Input': x, 'ValueTensor': value} - - helper = LayerHelper("set_value") - y = helper.create_variable_for_type_inference(dtype=x.dtype) - - helper.append_op( - type="set_value", inputs=inputs, outputs={'Out': y}, attrs=attrs - ) - y2 = y + 1 - loss = paddle.sum(y2) - opt = paddle.optimizer.Adam() - opt.minimize(loss) + input_shape = [7, 6, 5, 4, 3, 2] + with paddle.static.scope_guard(new_scope): + with paddle.static.program_guard(main_program): + x = paddle.ones(shape=input_shape, dtype="float32") + value = paddle.tensor.fill_constant([1, 3, 2], "float32", 1) + # test stop_gradient + value.stop_gradient = False + x.stop_gradient = False + attrs = { + 'axes': [0], + 'starts': [6], + 'ends': [0], + 'steps': [-4], + 'decrease_axes': [], + 'none_axes': [], + 'dtype': paddle.float32, + } + inputs = {'Input': x, 'ValueTensor': value} + + helper = LayerHelper("set_value") + y = helper.create_variable_for_type_inference(dtype=x.dtype) - _ = ir.translate_to_new_ir(main_program.desc) + helper.append_op( + type="set_value", + inputs=inputs, + outputs={'Out': y}, + attrs=attrs, + ) + y2 = y + 1 + loss = paddle.sum(y2) + opt = paddle.optimizer.Adam() + opt.minimize(loss) + + x_data = np.arange( + 0, np.prod(input_shape), dtype="float32" + ).reshape(input_shape) + fetch_list = [x.grad_name, value.grad_name] + ret = exe.run(main_program, fetch_list=fetch_list) + self.assertTrue((ret[0][6:0:-4] == 0).all()) class TestCheckUnregisteredOp(unittest.TestCase):