From 9a36fd4b5dbdb84c4e9f1f478e98941912004b9d Mon Sep 17 00:00:00 2001 From: Charles-hit <56987902+Charles-hit@users.noreply.github.com> Date: Thu, 15 Jun 2023 15:57:34 +0800 Subject: [PATCH] support composite higher grad maker auto gen (#54666) * support composite higher grad maker * fix composite maker higher order gen --- .../operators/generator/templates/op.c.j2 | 2 +- .../generator/templates/operator_utils.c.j2 | 44 +++++++++++-------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/paddle/fluid/operators/generator/templates/op.c.j2 b/paddle/fluid/operators/generator/templates/op.c.j2 index 68673256a00..7c5eaa98223 100644 --- a/paddle/fluid/operators/generator/templates/op.c.j2 +++ b/paddle/fluid/operators/generator/templates/op.c.j2 @@ -44,7 +44,7 @@ using paddle::framework::GradVarName; {{backward_op_reused_maker(op, op_dict[op["forward"]["name"]], op["invoke"])}} {% endif %} {% if op is composite_op %} -{{composite_grad_op_maker(op_dict[op["name"]])}} +{{composite_grad_op_maker(op_dict[op["name"]], op_dict[op["forward"]["name"]])}} {% endif %} {% endfor %} } // namespace operators diff --git a/paddle/fluid/operators/generator/templates/operator_utils.c.j2 b/paddle/fluid/operators/generator/templates/operator_utils.c.j2 index 1c72e5dd303..0253697ba68 100644 --- a/paddle/fluid/operators/generator/templates/operator_utils.c.j2 +++ b/paddle/fluid/operators/generator/templates/operator_utils.c.j2 @@ -681,13 +681,15 @@ class {{name | to_pascal_case}}OpMaker : public framework::SingleGradOpMaker }; {% endmacro %} -{% macro composite_grad_op_maker(backward_op) %} +{% macro composite_grad_op_maker(backward_op, forward_op) %} {% set op_name = backward_op["op_name"] %} {% set inputs = backward_op["inputs"] | to_variable_names("name")%} {% set input_dict = backward_op["input_dict"] %} {% set fluid_inputs = backward_op["inputs"] | to_variable_names("fluid_name")%} {% set forward_fluid_inputs = backward_op["forward"]["inputs"] | to_variable_names("fluid_name")%} {% set forward_fluid_outputs = backward_op["forward"]["outputs"] | to_variable_names("fluid_name")%} + {% set forward_fluid_orig_inputs = forward_op["inputs"] | map(attribute="fluid_name") | list %} + {% set forward_fluid_orig_outputs = forward_op["outputs"] | map(attribute="fluid_name") | list %} {% set attrs = backward_op["attrs"] | to_variable_names("name") %} {% set fluid_attrs = backward_op["attrs"] | to_variable_names("fluid_name") %} {% set attr_dict = backward_op["attr_dict"] %} @@ -700,12 +702,12 @@ class {{op_name | to_composite_grad_opmaker_name}} : public prim::CompositeGradO using prim::CompositeGradOpMakerBase::CompositeGradOpMakerBase; void Apply() override { //get inputs -{{construct_composite_input(inputs, fluid_inputs, forward_fluid_inputs, forward_fluid_outputs, input_dict)}} +{{construct_composite_input(inputs, fluid_inputs, forward_fluid_inputs, forward_fluid_outputs, forward_fluid_orig_inputs, forward_fluid_orig_outputs, input_dict)}} {{construct_composite_tensor_attr(attrs, fluid_attrs, attr_dict, op_name)}} //get attr {{construct_composite_attr(attrs, fluid_attrs, attr_dict)}} //get output -{{construct_composite_output(outputs, fluid_outputs, output_dict)}} +{{construct_composite_output(outputs, fluid_outputs, forward_fluid_inputs, forward_fluid_orig_inputs, output_dict)}} //get output ptr {{construct_composite_output_ptr(outputs, output_dict)}} //get output orginal name @@ -718,51 +720,54 @@ class {{op_name | to_composite_grad_opmaker_name}} : public prim::CompositeGradO }; {%- endmacro %} -{% macro construct_composite_input(inputs, fluid_inputs, forward_fluid_inputs, forward_fluid_outputs, input_dict) %} +{% macro construct_composite_input(inputs, fluid_inputs, forward_fluid_inputs, forward_fluid_outputs, forward_fluid_orig_inputs, forward_fluid_orig_outputs, input_dict) %} {% set inputs_length = inputs | length %} {% for i in range(inputs_length) %} {% set input_typename = input_dict[inputs[i]]["typename"] %} {% set input_optional_flag = input_dict[inputs[i]]["optional"] %} {% if fluid_inputs[i] in forward_fluid_inputs %} + {% set name_in_forward_orig = forward_fluid_orig_inputs[forward_fluid_inputs.index(fluid_inputs[i])]%} {% if input_typename == "Tensor" %} {% if input_optional_flag == True %} - auto {{inputs[i]}} = this->GetOptionalSingleForwardInput("{{fluid_inputs[i]}}"); + auto {{inputs[i]}} = this->GetOptionalSingleForwardInput({{name_in_forward_orig | to_opmaker_name}}); {% else %} - auto {{inputs[i]}} = this->GetSingleForwardInput("{{fluid_inputs[i]}}"); + auto {{inputs[i]}} = this->GetSingleForwardInput({{name_in_forward_orig | to_opmaker_name}}); {% endif %} {% elif input_typename == "Tensor[]" %} {% if input_optional_flag == True %} - auto {{inputs[i]}} = this->GetOptionalMultiForwardInput("{{fluid_inputs[i]}}"); + auto {{inputs[i]}} = this->GetOptionalMultiForwardInput({{name_in_forward_orig | to_opmaker_name}}); {% else %} - auto {{inputs[i]}} = this->GetMultiForwardInput("{{fluid_inputs[i]}}"); + auto {{inputs[i]}} = this->GetMultiForwardInput({{name_in_forward_orig | to_opmaker_name}}); {% endif %} {% endif %} {% elif fluid_inputs[i] in forward_fluid_outputs %} + {% set name_in_forward_orig = forward_fluid_orig_outputs[forward_fluid_outputs.index(fluid_inputs[i])]%} {% if input_typename == "Tensor" %} {% if input_optional_flag == True %} - auto {{inputs[i]}} = this->GetOptionalSingleForwardOutput("{{fluid_inputs[i]}}"); + auto {{inputs[i]}} = this->GetOptionalSingleForwardOutput({{name_in_forward_orig | to_opmaker_name}}); {% else %} - auto {{inputs[i]}} = this->GetSingleForwardOutput("{{fluid_inputs[i]}}"); + auto {{inputs[i]}} = this->GetSingleForwardOutput({{name_in_forward_orig | to_opmaker_name}}); {% endif %} {% elif input_typename == "Tensor[]" %} {% if input_optional_flag == True %} - auto {{inputs[i]}} = this->GetOptionalMultiForwardOutput("{{fluid_inputs[i]}}"); + auto {{inputs[i]}} = this->GetOptionalMultiForwardOutput({{name_in_forward_orig | to_opmaker_name}}); {% else %} - auto {{inputs[i]}} = this->GetMultiForwardOutput("{{fluid_inputs[i]}}"); + auto {{inputs[i]}} = this->GetMultiForwardOutput({{name_in_forward_orig | to_opmaker_name}}); {% endif %} {% endif %} {% elif fluid_inputs[i][:-5] in forward_fluid_outputs %} + {% set name_in_forward_orig = forward_fluid_orig_outputs[forward_fluid_outputs.index(fluid_inputs[i][:-5])]%} {% if input_typename == "Tensor" %} {% if input_optional_flag == True %} - auto {{inputs[i]}} = this->GetOptionalSingleOutputGrad("{{fluid_inputs[i][:-5]}}"); + auto {{inputs[i]}} = this->GetOptionalSingleOutputGrad({{name_in_forward_orig | to_opmaker_name}}); {% else %} - auto {{inputs[i]}} = this->GetSingleOutputGrad("{{fluid_inputs[i][:-5]}}"); + auto {{inputs[i]}} = this->GetSingleOutputGrad({{name_in_forward_orig | to_opmaker_name}}); {% endif %} {% elif input_typename == "Tensor[]" %} {% if input_optional_flag == True %} - auto {{inputs[i]}} = this->GetOptionalMultiOutputGrad("{{fluid_inputs[i][:-5]}}"); + auto {{inputs[i]}} = this->GetOptionalMultiOutputGrad({{name_in_forward_orig | to_opmaker_name}}); {% else %} - auto {{inputs[i]}} = this->GetMultiOutputGrad("{{fluid_inputs[i][:-5]}}"); + auto {{inputs[i]}} = this->GetMultiOutputGrad({{name_in_forward_orig | to_opmaker_name}}); {%- endif %} {%- endif %} {%- endif %} @@ -803,14 +808,15 @@ class {{op_name | to_composite_grad_opmaker_name}} : public prim::CompositeGradO {% endfor %} {%- endmacro %} -{% macro construct_composite_output(outputs, fluid_outputs, output_dict) %} +{% macro construct_composite_output(outputs, fluid_outputs, forward_fluid_inputs, forward_fluid_orig_inputs, output_dict) %} {% set outputs_length = outputs | length %} {% for i in range(outputs_length) %} + {% set name_in_forward_orig = forward_fluid_orig_inputs[forward_fluid_inputs.index(fluid_outputs[i][:-5])]%} {% set output_typename = output_dict[outputs[i]]["typename"] %} {% if output_typename == "Tensor" %} - auto {{outputs[i] + "_t"}} = this->GetSingleInputGrad("{{fluid_outputs[i][:-5]}}"); + auto {{outputs[i] + "_t"}} = this->GetSingleInputGrad({{name_in_forward_orig | to_opmaker_name}}); {% elif output_typename == "Tensor[]" %} - auto {{outputs[i] + "_t"}} = this->GetMultiInputGrad("{{fluid_outputs[i][:-5]}}"); + auto {{outputs[i] + "_t"}} = this->GetMultiInputGrad({{name_in_forward_orig | to_opmaker_name}}); {%- endif %} {%- endfor %} {%- endmacro %} -- GitLab