未验证 提交 9a36fd4b 编写于 作者: C Charles-hit 提交者: GitHub

support composite higher grad maker auto gen (#54666)

* support composite higher grad maker

* fix composite maker higher order gen
上级 3a275162
...@@ -44,7 +44,7 @@ using paddle::framework::GradVarName; ...@@ -44,7 +44,7 @@ using paddle::framework::GradVarName;
{{backward_op_reused_maker(op, op_dict[op["forward"]["name"]], op["invoke"])}} {{backward_op_reused_maker(op, op_dict[op["forward"]["name"]], op["invoke"])}}
{% endif %} {% endif %}
{% if op is composite_op %} {% 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 %} {% endif %}
{% endfor %} {% endfor %}
} // namespace operators } // namespace operators
......
...@@ -681,13 +681,15 @@ class {{name | to_pascal_case}}OpMaker : public framework::SingleGradOpMaker<T> ...@@ -681,13 +681,15 @@ class {{name | to_pascal_case}}OpMaker : public framework::SingleGradOpMaker<T>
}; };
{% endmacro %} {% 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 op_name = backward_op["op_name"] %}
{% set inputs = backward_op["inputs"] | to_variable_names("name")%} {% set inputs = backward_op["inputs"] | to_variable_names("name")%}
{% set input_dict = backward_op["input_dict"] %} {% set input_dict = backward_op["input_dict"] %}
{% set fluid_inputs = backward_op["inputs"] | to_variable_names("fluid_name")%} {% 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_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_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 attrs = backward_op["attrs"] | to_variable_names("name") %}
{% set fluid_attrs = backward_op["attrs"] | to_variable_names("fluid_name") %} {% set fluid_attrs = backward_op["attrs"] | to_variable_names("fluid_name") %}
{% set attr_dict = backward_op["attr_dict"] %} {% set attr_dict = backward_op["attr_dict"] %}
...@@ -700,12 +702,12 @@ class {{op_name | to_composite_grad_opmaker_name}} : public prim::CompositeGradO ...@@ -700,12 +702,12 @@ class {{op_name | to_composite_grad_opmaker_name}} : public prim::CompositeGradO
using prim::CompositeGradOpMakerBase::CompositeGradOpMakerBase; using prim::CompositeGradOpMakerBase::CompositeGradOpMakerBase;
void Apply() override { void Apply() override {
//get inputs //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)}} {{construct_composite_tensor_attr(attrs, fluid_attrs, attr_dict, op_name)}}
//get attr //get attr
{{construct_composite_attr(attrs, fluid_attrs, attr_dict)}} {{construct_composite_attr(attrs, fluid_attrs, attr_dict)}}
//get output //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 //get output ptr
{{construct_composite_output_ptr(outputs, output_dict)}} {{construct_composite_output_ptr(outputs, output_dict)}}
//get output orginal name //get output orginal name
...@@ -718,51 +720,54 @@ class {{op_name | to_composite_grad_opmaker_name}} : public prim::CompositeGradO ...@@ -718,51 +720,54 @@ class {{op_name | to_composite_grad_opmaker_name}} : public prim::CompositeGradO
}; };
{%- endmacro %} {%- 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 %} {% set inputs_length = inputs | length %}
{% for i in range(inputs_length) %} {% for i in range(inputs_length) %}
{% set input_typename = input_dict[inputs[i]]["typename"] %} {% set input_typename = input_dict[inputs[i]]["typename"] %}
{% set input_optional_flag = input_dict[inputs[i]]["optional"] %} {% set input_optional_flag = input_dict[inputs[i]]["optional"] %}
{% if fluid_inputs[i] in forward_fluid_inputs %} {% 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_typename == "Tensor" %}
{% if input_optional_flag == True %} {% 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 %} {% else %}
auto {{inputs[i]}} = this->GetSingleForwardInput("{{fluid_inputs[i]}}"); auto {{inputs[i]}} = this->GetSingleForwardInput({{name_in_forward_orig | to_opmaker_name}});
{% endif %} {% endif %}
{% elif input_typename == "Tensor[]" %} {% elif input_typename == "Tensor[]" %}
{% if input_optional_flag == True %} {% 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 %} {% else %}
auto {{inputs[i]}} = this->GetMultiForwardInput("{{fluid_inputs[i]}}"); auto {{inputs[i]}} = this->GetMultiForwardInput({{name_in_forward_orig | to_opmaker_name}});
{% endif %} {% endif %}
{% endif %} {% endif %}
{% elif fluid_inputs[i] in forward_fluid_outputs %} {% 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_typename == "Tensor" %}
{% if input_optional_flag == True %} {% 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 %} {% else %}
auto {{inputs[i]}} = this->GetSingleForwardOutput("{{fluid_inputs[i]}}"); auto {{inputs[i]}} = this->GetSingleForwardOutput({{name_in_forward_orig | to_opmaker_name}});
{% endif %} {% endif %}
{% elif input_typename == "Tensor[]" %} {% elif input_typename == "Tensor[]" %}
{% if input_optional_flag == True %} {% 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 %} {% else %}
auto {{inputs[i]}} = this->GetMultiForwardOutput("{{fluid_inputs[i]}}"); auto {{inputs[i]}} = this->GetMultiForwardOutput({{name_in_forward_orig | to_opmaker_name}});
{% endif %} {% endif %}
{% endif %} {% endif %}
{% elif fluid_inputs[i][:-5] in forward_fluid_outputs %} {% 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_typename == "Tensor" %}
{% if input_optional_flag == True %} {% 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 %} {% else %}
auto {{inputs[i]}} = this->GetSingleOutputGrad("{{fluid_inputs[i][:-5]}}"); auto {{inputs[i]}} = this->GetSingleOutputGrad({{name_in_forward_orig | to_opmaker_name}});
{% endif %} {% endif %}
{% elif input_typename == "Tensor[]" %} {% elif input_typename == "Tensor[]" %}
{% if input_optional_flag == True %} {% 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 %} {% 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 %} {%- endif %}
{%- endif %} {%- endif %}
...@@ -803,14 +808,15 @@ class {{op_name | to_composite_grad_opmaker_name}} : public prim::CompositeGradO ...@@ -803,14 +808,15 @@ class {{op_name | to_composite_grad_opmaker_name}} : public prim::CompositeGradO
{% endfor %} {% endfor %}
{%- endmacro %} {%- 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 %} {% set outputs_length = outputs | length %}
{% for i in range(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"] %} {% set output_typename = output_dict[outputs[i]]["typename"] %}
{% if output_typename == "Tensor" %} {% 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[]" %} {% 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 %} {%- endif %}
{%- endfor %} {%- endfor %}
{%- endmacro %} {%- endmacro %}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册