From 7c6073eb17c1f42109509f3515957e528a04ec09 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Sun, 28 May 2023 22:43:42 +0800 Subject: [PATCH 01/13] [static op generation] transpose --- .../generator/get_expected_kernel_func.cc | 10 ++ .../generator/get_expected_kernel_func.h | 4 + paddle/fluid/operators/transpose_op.cc | 98 ------------------- paddle/phi/api/yaml/backward.yaml | 18 ++++ paddle/phi/api/yaml/legacy_backward.yaml | 18 ---- paddle/phi/api/yaml/legacy_ops.yaml | 9 -- paddle/phi/api/yaml/op_compat.yaml | 2 + paddle/phi/api/yaml/ops.yaml | 9 ++ paddle/phi/ops/compat/transpose_sig.cc | 2 - 9 files changed, 43 insertions(+), 127 deletions(-) diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.cc b/paddle/fluid/operators/generator/get_expected_kernel_func.cc index 0520af7c505b7..ba6736e4a67f0 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.cc +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.cc @@ -285,5 +285,15 @@ phi::KernelKey GetLayerNormExpectedKernelType( return phi::KernelKey(input_data_type, ctx.GetPlace()); } +phi::KernelKey GetTransposeExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr) { + auto data_type = op_ptr->OperatorWithKernel::IndicateVarDataType(ctx, "X"); + auto& data_format = ctx.Attr("data_format"); + phi::DataLayout layout_ = phi::StringToDataLayout(data_format); + return phi::KernelKey( + ctx.GetPlace(), layout_, phi::TransToPhiDataType(data_type)); +} + } // namespace operators } // namespace paddle diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.h b/paddle/fluid/operators/generator/get_expected_kernel_func.h index bf7c691fffa46..a566f4834e96f 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.h +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.h @@ -72,5 +72,9 @@ phi::KernelKey GetLayerNormExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr); +phi::KernelKey GetTransposeExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr); + } // namespace operators } // namespace paddle diff --git a/paddle/fluid/operators/transpose_op.cc b/paddle/fluid/operators/transpose_op.cc index 652f88fec8f45..f24d7f904dec3 100644 --- a/paddle/fluid/operators/transpose_op.cc +++ b/paddle/fluid/operators/transpose_op.cc @@ -21,92 +21,6 @@ limitations under the License. */ namespace paddle { namespace operators { -phi::KernelKey TransposeOp::GetExpectedKernelType( - const framework::ExecutionContext &ctx) const { - auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X"); - auto &data_format = ctx.Attr("data_format"); - phi::DataLayout layout_ = phi::StringToDataLayout(data_format); - return phi::KernelKey( - ctx.GetPlace(), layout_, phi::TransToPhiDataType(data_type)); -} - -class TransposeOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput( - "X", - "(Tensor) The input tensor, tensors with rank up to 6 are supported."); - AddOutput("Out", "(Tensor)The output tensor."); - AddAttr>( - "axis", - "(vector) A list of values, and the size of the list should be " - "the same with the input tensor rank. This operator permutes the input " - "tensor's axes according to the values given."); - AddAttr("use_mkldnn", - "(bool, default false) Only used in mkldnn kernel") - .SetDefault(false) - .AsExtra(); - AddAttr( - "data_format", - "(string, default NCHW) Only used in " - "An optional string from: \"NHWC\", \"NCHW\". " - "Defaults to \"NHWC\". Specify the data format of the output data, " - "the input will be transformed automatically. ") - .SetDefault("AnyLayout") - .AsExtra(); - AddAttr( - "mkldnn_data_type", - "(string, default \"float32\"). Data type of mkldnn kernel") - .SetDefault("float32") - .InEnum({"float32", "int8", "bfloat16"}) - .AsExtra(); - AddComment(R"DOC( -Transpose Operator. - -The input tensor will be permuted according to the axes given. -The behavior of this operator is similar to how `numpy.transpose` works. - -- suppose the input `X` is a 2-D tensor: - $$ - X = \begin{pmatrix} - 0 &1 &2 \\ - 3 &4 &5 - \end{pmatrix}$$ - - the given `axes` is: $[1, 0]$, and $Y$ = transpose($X$, axis) - - then the output $Y$ is: - - $$ - Y = \begin{pmatrix} - 0 &3 \\ - 1 &4 \\ - 2 &5 - \end{pmatrix}$$ - -- Given a input tensor with shape $(N, C, H, W)$ and the `axes` is -$[0, 2, 3, 1]$, then shape of the output tensor will be: $(N, H, W, C)$. - -)DOC"); - } -}; - -class TransposeOpGrad : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - auto data_type = OperatorWithKernel::IndicateVarDataType( - ctx, framework::GradVarName("Out")); - std::string data_format = ctx.Attr("data_format"); - phi::DataLayout layout_ = phi::StringToDataLayout(data_format); - return phi::KernelKey( - ctx.GetPlace(), layout_, phi::TransToPhiDataType(data_type)); - } -}; - void Transpose2Op::InferShape(framework::InferShapeContext *ctx) const { using CompatMetaTensor = framework::CompatMetaTensor; CompatMetaTensor x(ctx->GetInputVarPtrs("X")[0], ctx->IsRuntime()); @@ -262,18 +176,6 @@ DECLARE_INFER_SHAPE_FUNCTOR(transpose2_grad, PD_INFER_META(phi::TransposeGradInferMeta)); namespace ops = paddle::operators; -REGISTER_OPERATOR( - transpose, - ops::TransposeOp, - ops::TransposeOpMaker, - paddle::framework::DefaultGradOpMaker, - paddle::framework::DefaultGradOpMaker, - TransposeInferShapeFunctor); - -REGISTER_OPERATOR(transpose_grad, - ops::TransposeOpGrad, - ops::TransposeGradInferVarType, - TransposeGradInferShapeFunctor); REGISTER_OPERATOR(transpose2, ops::Transpose2Op, diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index f9868df491388..b2c8dda0b8bb0 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -2101,6 +2101,24 @@ data_type : out_grad no_need_buffer : x +- backward_op : transpose_double_grad + forward : transpose_grad (Tensor grad_out, int[] perm) -> Tensor(grad_x) + args : (Tensor grad_x_grad, int[] perm) + output : Tensor(grad_out_grad) + invoke : transpose(grad_x_grad, perm) + +- backward_op : transpose_grad + forward : transpose (Tensor x, int[] perm) -> Tensor(out) + args : (Tensor out_grad, int[] perm) + output : Tensor(x_grad) + infer_meta : + func : TransposeGradInferMeta + param : [out_grad, perm] + kernel : + func : transpose_grad + backward : transpose_double_grad + composite: transpose_grad(out_grad, perm, x_grad) + - backward_op : triangular_solve_grad forward : triangular_solve (Tensor x, Tensor y, bool upper=true, bool transpose=false, bool unitriangular=false) -> Tensor(out) args : (Tensor x, Tensor y, Tensor out, Tensor out_grad, bool upper, bool transpose, bool unitriangular) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 3e625667cf445..22fcd4870357c 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -934,24 +934,6 @@ kernel : func : trans_layout_grad -- backward_op : transpose_double_grad - forward : transpose_grad (Tensor grad_out, int[] perm) -> Tensor(grad_x) - args : (Tensor grad_x_grad, int[] perm) - output : Tensor(grad_out_grad) - invoke : transpose(grad_x_grad, perm) - -- backward_op : transpose_grad - forward : transpose (Tensor x, int[] perm) -> Tensor(out) - args : (Tensor out_grad, int[] perm) - output : Tensor(x_grad) - infer_meta : - func : TransposeGradInferMeta - param : [out_grad, perm] - kernel : - func : transpose_grad - backward : transpose_double_grad - composite: transpose_grad(out_grad, perm, x_grad) - - backward_op : tril_grad forward : tril(Tensor x, int diagonal) -> Tensor(out) args : (Tensor out_grad, int diagonal) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 4dba3fdd74ec8..354d9cbef1fb1 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -1060,15 +1060,6 @@ func : transpose backward : trans_layout_grad -- op : transpose - args : (Tensor x, int[] perm) - output : Tensor - infer_meta : - func : TransposeInferMeta - kernel : - func : transpose - backward : transpose_grad - - op : tril args : (Tensor x, int diagonal) output : Tensor(out) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index bbe3017e27eba..4cd406e763923 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -2382,6 +2382,8 @@ extra : outputs : [XShape] attrs : [bool use_mkldnn = false, str data_format = "AnyLayout", str mkldnn_data_type = "float32"] + get_expected_kernel_type: + transpose: GetTransposeExpectedKernelType - op : triangular_solve backward : triangular_solve_grad diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index a9c10037f80d7..38b7e112e8f85 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2192,6 +2192,15 @@ func : trace backward : trace_grad +- op : transpose + args : (Tensor x, int[] perm) + output : Tensor + infer_meta : + func : TransposeInferMeta + kernel : + func : transpose + backward : transpose_grad + - op : triangular_solve args : (Tensor x, Tensor y, bool upper=true, bool transpose=false, bool unitriangular=false) output : Tensor diff --git a/paddle/phi/ops/compat/transpose_sig.cc b/paddle/phi/ops/compat/transpose_sig.cc index ca5b76e1298a0..f7a465e0e2ffe 100644 --- a/paddle/phi/ops/compat/transpose_sig.cc +++ b/paddle/phi/ops/compat/transpose_sig.cc @@ -34,5 +34,3 @@ PD_REGISTER_BASE_KERNEL_NAME(transpose2_grad, transpose_grad); PD_REGISTER_ARG_MAPPING_FN(transpose2, phi::TransposeOpArgumentMapping); PD_REGISTER_ARG_MAPPING_FN(transpose2_grad, phi::TransposeGradOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(transpose, phi::TransposeOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(transpose_grad, phi::TransposeGradOpArgumentMapping); From 9f329480cc7e88b910bbe10990c56eb45a060019 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Wed, 31 May 2023 06:48:17 +0800 Subject: [PATCH 02/13] [static op generation] concat --- paddle/fluid/operators/concat_op.cc | 224 ----------------------- paddle/fluid/operators/concat_op.h | 47 ----- paddle/phi/api/yaml/backward.yaml | 19 ++ paddle/phi/api/yaml/legacy_backward.yaml | 19 -- paddle/phi/api/yaml/legacy_ops.yaml | 10 - paddle/phi/api/yaml/ops.yaml | 10 + paddle/phi/ops/compat/concat_sig.cc | 38 ---- 7 files changed, 29 insertions(+), 338 deletions(-) delete mode 100644 paddle/fluid/operators/concat_op.cc delete mode 100644 paddle/fluid/operators/concat_op.h delete mode 100644 paddle/phi/ops/compat/concat_sig.cc diff --git a/paddle/fluid/operators/concat_op.cc b/paddle/fluid/operators/concat_op.cc deleted file mode 100644 index ec3f0c76df2c8..0000000000000 --- a/paddle/fluid/operators/concat_op.cc +++ /dev/null @@ -1,224 +0,0 @@ -/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#include "paddle/fluid/operators/concat_op.h" - -#include - -#include -#include -#include - -#include "paddle/fluid/framework/infershape_utils.h" -#include "paddle/fluid/prim/api/composite_backward/composite_backward_api.h" -#include "paddle/fluid/prim/utils/static/composite_grad_desc_maker.h" -#include "paddle/fluid/prim/utils/static/desc_tensor.h" -#include "paddle/phi/infermeta/multiary.h" -#include "paddle/phi/kernels/funcs/concat_funcs.h" - -namespace paddle { -namespace operators { - -class ConcatOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - auto inputs = ctx.MultiInput("X"); - auto input_data_type = framework::proto::VarType::Type(0); - bool flag = 0; - for (auto *input : inputs) { - if (input->IsInitialized()) { - input_data_type = framework::TransToProtoVarType(input->dtype()); - flag = 1; - break; - } - } - if (flag == 0) { - PADDLE_THROW(platform::errors::InvalidArgument( - "All Inputs of Concat OP are Empty!")); - } - return phi::KernelKey(input_data_type, ctx.GetPlace()); - } - - phi::KernelKey GetKernelTypeForVar( - const std::string &var_name, - const phi::DenseTensor &tensor, - const phi::KernelKey &expected_kernel_type) const override { - if (var_name == "AxisTensor") { - return phi::KernelKey(phi::Backend::ALL_BACKEND, - expected_kernel_type.layout(), - expected_kernel_type.dtype()); - } - return phi::KernelKey( - tensor.place(), tensor.layout(), expected_kernel_type.dtype()); - } -}; - -class ConcatOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("X", "Input tensors of concat operator.").AsDuplicable(); - AddOutput("Out", "Output tensor of concat operator."); - AddAttr("axis", - "The axis along which the input tensors will be concatenated." - "The axis could also be negative numbers. Negative axis is " - "interpreted as counting from the end of the rank." - "i.e., axis + rank(X) th dimension.") - .SetDefault(0) - .SupportTensor(); - AddInput("AxisTensor", - "(Tensor) The axis along which the input tensors will be " - "concatenated. " - "It has higher priority than Attr(axis). " - "The shape of AxisTensor must be [1].") - .AsDispensable(); - AddComment(R"DOC( -Concat Operator. - -Concatenate the input tensors along dimension axis. -Examples: - Input[0] = [[1,2],[3,4]] - Input[1] = [[5,6]] - axis = 0 - Output = [[1,2], - [3,4], - [5,6]] - -)DOC"); - } -}; - -class ConcatOpGrad : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext *ctx) const override { - auto in_x = "X"; - auto out_x_g_n = framework::GradVarName(in_x); - ctx->SetOutputsDim(out_x_g_n, ctx->GetInputsDim(in_x)); - - ctx->ShareAllLoD(in_x, out_x_g_n); - } - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - auto input_data_type = OperatorWithKernel::IndicateVarDataType( - ctx, framework::GradVarName("Out")); - return phi::KernelKey(input_data_type, ctx.GetPlace()); - } - - phi::KernelKey GetKernelTypeForVar( - const std::string &var_name, - const phi::DenseTensor &tensor, - const phi::KernelKey &expected_kernel_type) const override { - if (var_name == "AxisTensor") { - return phi::KernelKey(phi::Backend::ALL_BACKEND, - expected_kernel_type.layout(), - expected_kernel_type.dtype()); - } - return phi::KernelKey( - tensor.place(), tensor.layout(), expected_kernel_type.dtype()); - } -}; - -DECLARE_NO_NEED_BUFFER_VARS_INFERER(ConcatOpGradNoNeedBufferVarInferer, "X"); - -template -class ConcatGradOpMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - protected: - void Apply(GradOpPtr op) const override { - op->SetType("concat_grad"); - op->SetInput("X", this->Input("X")); - if (this->HasInput("AxisTensor")) { - op->SetInput("AxisTensor", this->Input("AxisTensor")); - } - op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); - op->SetOutput(framework::GradVarName("X"), this->InputGrad("X", false)); - op->SetAttrMap(this->Attrs()); - } -}; - -class ConcatCompositeGradOpMaker : public prim::CompositeGradOpMakerBase { - using prim::CompositeGradOpMakerBase::CompositeGradOpMakerBase; - - public: - void Apply() override { - std::vector input = this->GetMultiForwardInput("X"); - paddle::optional tensor_axis = - this->GetOptionalSingleForwardInput("AxisTensor"); - paddle::Tensor out_grad = this->GetSingleOutputGrad("Out"); - std::vector input_grad = this->GetMultiInputGrad("X"); - - std::vector input_grad_ptr; - for (auto i = 0; i < static_cast(input_grad.size()); ++i) { - input_grad_ptr.push_back(&input_grad[i]); - } - int axis = static_cast(this->Attr("axis")); - std::vector dx_ptr = this->GetOutputPtr(input_grad_ptr); - std::vector dx_name = this->GetOutputName(input_grad); - - VLOG(6) << "Runing concat_grad composite func"; - if (tensor_axis.is_initialized()) { - PADDLE_THROW(platform::errors::Unimplemented( - "We don't support dynamic index from tensor for concat composite " - "grad for now. ")); - } else { - prim::concat_grad(input, out_grad, axis, dx_ptr); - } - this->RecoverOutputName(input_grad, dx_name); - } -}; - -template -class ConcatDoubleGradOpMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - protected: - void Apply(GradOpPtr grad_op) const override { - grad_op->SetType("concat"); - grad_op->SetInput("X", this->OutputGrad(framework::GradVarName("X"))); - grad_op->SetOutput("Out", this->InputGrad(framework::GradVarName("Out"))); - grad_op->SetAttrMap(this->Attrs()); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -DECLARE_INFER_SHAPE_FUNCTOR(concat, - ConcatInferShapeFunctor, - PD_INFER_META(phi::ConcatInferMeta)); - -REGISTER_OPERATOR(concat, - ops::ConcatOp, - ops::ConcatOpMaker, - ops::ConcatGradOpMaker, - ops::ConcatGradOpMaker, - ops::ConcatCompositeGradOpMaker, - ConcatInferShapeFunctor); -REGISTER_OPERATOR(concat_grad, - ops::ConcatOpGrad, - ops::ConcatDoubleGradOpMaker, - ops::ConcatDoubleGradOpMaker, - ops::ConcatOpGradNoNeedBufferVarInferer); diff --git a/paddle/fluid/operators/concat_op.h b/paddle/fluid/operators/concat_op.h deleted file mode 100644 index 58d978ea9c73a..0000000000000 --- a/paddle/fluid/operators/concat_op.h +++ /dev/null @@ -1,47 +0,0 @@ -/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - -http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#pragma once - -#include -#include -#include - -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/operators/math/concat_and_split.h" -#include "paddle/fluid/operators/utils.h" -#include "paddle/phi/kernels/concat_kernel.h" -#include "paddle/phi/kernels/funcs/concat_funcs.h" -#include "paddle/phi/kernels/funcs/strided_memcpy.h" - -namespace paddle { -namespace operators { - -static inline int64_t ComputeAxis(int64_t axis, int64_t rank) { - PADDLE_ENFORCE_EQ( - axis >= -rank && axis < rank, - true, - platform::errors::InvalidArgument( - "The axis is expected to be in range of [%d, %d), but got %d", - -rank, - rank, - axis)); - if (axis < 0) { - axis = axis + rank; - } - return axis > 0 ? axis : 0; -} - -} // namespace operators -} // namespace paddle diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index f9868df491388..76a7b89984b33 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -318,6 +318,25 @@ func : complex_grad data_type : real +- backward_op : concat_double_grad + forward : concat_grad (Tensor[] x, Tensor grad_out, Scalar axis) -> Tensor[](grad_x) + args : (Tensor[] grad_x_grad, Scalar axis = 0) + output : Tensor(grad_out_grad) + invoke : concat(grad_x_grad, axis) + +- backward_op : concat_grad + forward : concat (Tensor[] x, Scalar axis) -> Tensor(out) + args : (Tensor[] x, Tensor out_grad, Scalar axis = 0) + output : Tensor[](x_grad){x.size()} + infer_meta : + func : UnchangedMultiInferMeta + param : [x] + kernel : + func : concat_grad + composite : concat_grad(x, out_grad, axis, x_grad) + no_need_buffer : x + backward : concat_double_grad + - backward_op : conj_grad forward : conj (Tensor x) -> Tensor(out) args : (Tensor out_grad) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 3e625667cf445..70874d55e08bb 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -118,25 +118,6 @@ kernel : func : channel_shuffle_grad -- backward_op : concat_double_grad - forward : concat_grad (Tensor[] x, Tensor grad_out, Scalar axis) -> Tensor[](grad_x) - args : (Tensor[] grad_x_grad, Scalar axis = 0) - output : Tensor(grad_out_grad) - invoke : concat(grad_x_grad, axis) - -- backward_op : concat_grad - forward : concat (Tensor[] x, Scalar axis) -> Tensor(out) - args : (Tensor[] x, Tensor out_grad, Scalar axis = 0) - output : Tensor[](x_grad){x.size()} - infer_meta : - func : UnchangedMultiInferMeta - param : [x] - kernel : - func : concat_grad - composite : concat_grad(x, out_grad, axis, x_grad) - no_need_buffer : x - backward : concat_double_grad - - backward_op : conv2d_grad forward : conv2d (Tensor input, Tensor filter, int[] strides, int[] paddings, str padding_algorithm, int[] dilations, int groups, str data_format) -> Tensor(out) args : (Tensor input, Tensor filter, Tensor out_grad, int[] strides, int[] paddings, str padding_algorithm, int[] dilations, int groups, str data_format) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 4dba3fdd74ec8..ffafff09cdfa9 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -162,16 +162,6 @@ data_type : x inplace : (x -> out), (input_found_infinite -> output_found_infinite) -- op : concat - args : (Tensor[] x, Scalar(int64_t) axis) - output : Tensor - infer_meta : - func : ConcatInferMeta - param : [x, axis] - kernel : - func : concat - backward : concat_grad - - op : conv2d args : (Tensor input, Tensor filter, int[] strides, int[] paddings, str padding_algorithm, int[] dilations, int groups, str data_format) output : Tensor diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index a9c10037f80d7..d5bdc6ab843c2 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -445,6 +445,16 @@ data_type : real backward : complex_grad +- op : concat + args : (Tensor[] x, Scalar(int64_t) axis) + output : Tensor + infer_meta : + func : ConcatInferMeta + param : [x, axis] + kernel : + func : concat + backward : concat_grad + - op : conj args : (Tensor x) output : Tensor (out) diff --git a/paddle/phi/ops/compat/concat_sig.cc b/paddle/phi/ops/compat/concat_sig.cc deleted file mode 100644 index c5e6a4b020089..0000000000000 --- a/paddle/phi/ops/compat/concat_sig.cc +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#include "paddle/phi/core/compat/op_utils.h" - -namespace phi { - -KernelSignature ConcatOpArgumentMapping(const ArgumentMappingContext& ctx) { - if (ctx.HasInput("AxisTensor")) { - return KernelSignature("concat", {"X"}, {"AxisTensor"}, {"Out"}); - } - return KernelSignature("concat", {"X"}, {"axis"}, {"Out"}); -} - -KernelSignature ConcatGradOpArgumentMapping(const ArgumentMappingContext& ctx) { - if (ctx.HasInput("AxisTensor")) { - return KernelSignature( - "concat_grad", {"X", "Out@GRAD"}, {"AxisTensor"}, {"X@GRAD"}); - } - return KernelSignature( - "concat_grad", {"X", "Out@GRAD"}, {"axis"}, {"X@GRAD"}); -} - -} // namespace phi - -PD_REGISTER_ARG_MAPPING_FN(concat, phi::ConcatOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(concat_grad, phi::ConcatGradOpArgumentMapping); From e2b04544224e5e1165ebc4b7c57608bea45f03ff Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Sat, 3 Jun 2023 06:58:30 +0800 Subject: [PATCH 03/13] Revert "[static op generation] concat" This reverts commit 9f329480cc7e88b910bbe10990c56eb45a060019. --- paddle/fluid/operators/concat_op.cc | 224 +++++++++++++++++++++++ paddle/fluid/operators/concat_op.h | 47 +++++ paddle/phi/api/yaml/backward.yaml | 19 -- paddle/phi/api/yaml/legacy_backward.yaml | 19 ++ paddle/phi/api/yaml/legacy_ops.yaml | 10 + paddle/phi/api/yaml/ops.yaml | 10 - paddle/phi/ops/compat/concat_sig.cc | 38 ++++ 7 files changed, 338 insertions(+), 29 deletions(-) create mode 100644 paddle/fluid/operators/concat_op.cc create mode 100644 paddle/fluid/operators/concat_op.h create mode 100644 paddle/phi/ops/compat/concat_sig.cc diff --git a/paddle/fluid/operators/concat_op.cc b/paddle/fluid/operators/concat_op.cc new file mode 100644 index 0000000000000..ec3f0c76df2c8 --- /dev/null +++ b/paddle/fluid/operators/concat_op.cc @@ -0,0 +1,224 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include "paddle/fluid/operators/concat_op.h" + +#include + +#include +#include +#include + +#include "paddle/fluid/framework/infershape_utils.h" +#include "paddle/fluid/prim/api/composite_backward/composite_backward_api.h" +#include "paddle/fluid/prim/utils/static/composite_grad_desc_maker.h" +#include "paddle/fluid/prim/utils/static/desc_tensor.h" +#include "paddle/phi/infermeta/multiary.h" +#include "paddle/phi/kernels/funcs/concat_funcs.h" + +namespace paddle { +namespace operators { + +class ConcatOp : public framework::OperatorWithKernel { + public: + using framework::OperatorWithKernel::OperatorWithKernel; + + protected: + phi::KernelKey GetExpectedKernelType( + const framework::ExecutionContext &ctx) const override { + auto inputs = ctx.MultiInput("X"); + auto input_data_type = framework::proto::VarType::Type(0); + bool flag = 0; + for (auto *input : inputs) { + if (input->IsInitialized()) { + input_data_type = framework::TransToProtoVarType(input->dtype()); + flag = 1; + break; + } + } + if (flag == 0) { + PADDLE_THROW(platform::errors::InvalidArgument( + "All Inputs of Concat OP are Empty!")); + } + return phi::KernelKey(input_data_type, ctx.GetPlace()); + } + + phi::KernelKey GetKernelTypeForVar( + const std::string &var_name, + const phi::DenseTensor &tensor, + const phi::KernelKey &expected_kernel_type) const override { + if (var_name == "AxisTensor") { + return phi::KernelKey(phi::Backend::ALL_BACKEND, + expected_kernel_type.layout(), + expected_kernel_type.dtype()); + } + return phi::KernelKey( + tensor.place(), tensor.layout(), expected_kernel_type.dtype()); + } +}; + +class ConcatOpMaker : public framework::OpProtoAndCheckerMaker { + public: + void Make() override { + AddInput("X", "Input tensors of concat operator.").AsDuplicable(); + AddOutput("Out", "Output tensor of concat operator."); + AddAttr("axis", + "The axis along which the input tensors will be concatenated." + "The axis could also be negative numbers. Negative axis is " + "interpreted as counting from the end of the rank." + "i.e., axis + rank(X) th dimension.") + .SetDefault(0) + .SupportTensor(); + AddInput("AxisTensor", + "(Tensor) The axis along which the input tensors will be " + "concatenated. " + "It has higher priority than Attr(axis). " + "The shape of AxisTensor must be [1].") + .AsDispensable(); + AddComment(R"DOC( +Concat Operator. + +Concatenate the input tensors along dimension axis. +Examples: + Input[0] = [[1,2],[3,4]] + Input[1] = [[5,6]] + axis = 0 + Output = [[1,2], + [3,4], + [5,6]] + +)DOC"); + } +}; + +class ConcatOpGrad : public framework::OperatorWithKernel { + public: + using framework::OperatorWithKernel::OperatorWithKernel; + + void InferShape(framework::InferShapeContext *ctx) const override { + auto in_x = "X"; + auto out_x_g_n = framework::GradVarName(in_x); + ctx->SetOutputsDim(out_x_g_n, ctx->GetInputsDim(in_x)); + + ctx->ShareAllLoD(in_x, out_x_g_n); + } + + protected: + phi::KernelKey GetExpectedKernelType( + const framework::ExecutionContext &ctx) const override { + auto input_data_type = OperatorWithKernel::IndicateVarDataType( + ctx, framework::GradVarName("Out")); + return phi::KernelKey(input_data_type, ctx.GetPlace()); + } + + phi::KernelKey GetKernelTypeForVar( + const std::string &var_name, + const phi::DenseTensor &tensor, + const phi::KernelKey &expected_kernel_type) const override { + if (var_name == "AxisTensor") { + return phi::KernelKey(phi::Backend::ALL_BACKEND, + expected_kernel_type.layout(), + expected_kernel_type.dtype()); + } + return phi::KernelKey( + tensor.place(), tensor.layout(), expected_kernel_type.dtype()); + } +}; + +DECLARE_NO_NEED_BUFFER_VARS_INFERER(ConcatOpGradNoNeedBufferVarInferer, "X"); + +template +class ConcatGradOpMaker : public framework::SingleGradOpMaker { + public: + using framework::SingleGradOpMaker::SingleGradOpMaker; + + protected: + void Apply(GradOpPtr op) const override { + op->SetType("concat_grad"); + op->SetInput("X", this->Input("X")); + if (this->HasInput("AxisTensor")) { + op->SetInput("AxisTensor", this->Input("AxisTensor")); + } + op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); + op->SetOutput(framework::GradVarName("X"), this->InputGrad("X", false)); + op->SetAttrMap(this->Attrs()); + } +}; + +class ConcatCompositeGradOpMaker : public prim::CompositeGradOpMakerBase { + using prim::CompositeGradOpMakerBase::CompositeGradOpMakerBase; + + public: + void Apply() override { + std::vector input = this->GetMultiForwardInput("X"); + paddle::optional tensor_axis = + this->GetOptionalSingleForwardInput("AxisTensor"); + paddle::Tensor out_grad = this->GetSingleOutputGrad("Out"); + std::vector input_grad = this->GetMultiInputGrad("X"); + + std::vector input_grad_ptr; + for (auto i = 0; i < static_cast(input_grad.size()); ++i) { + input_grad_ptr.push_back(&input_grad[i]); + } + int axis = static_cast(this->Attr("axis")); + std::vector dx_ptr = this->GetOutputPtr(input_grad_ptr); + std::vector dx_name = this->GetOutputName(input_grad); + + VLOG(6) << "Runing concat_grad composite func"; + if (tensor_axis.is_initialized()) { + PADDLE_THROW(platform::errors::Unimplemented( + "We don't support dynamic index from tensor for concat composite " + "grad for now. ")); + } else { + prim::concat_grad(input, out_grad, axis, dx_ptr); + } + this->RecoverOutputName(input_grad, dx_name); + } +}; + +template +class ConcatDoubleGradOpMaker : public framework::SingleGradOpMaker { + public: + using framework::SingleGradOpMaker::SingleGradOpMaker; + + protected: + void Apply(GradOpPtr grad_op) const override { + grad_op->SetType("concat"); + grad_op->SetInput("X", this->OutputGrad(framework::GradVarName("X"))); + grad_op->SetOutput("Out", this->InputGrad(framework::GradVarName("Out"))); + grad_op->SetAttrMap(this->Attrs()); + } +}; + +} // namespace operators +} // namespace paddle + +namespace ops = paddle::operators; + +DECLARE_INFER_SHAPE_FUNCTOR(concat, + ConcatInferShapeFunctor, + PD_INFER_META(phi::ConcatInferMeta)); + +REGISTER_OPERATOR(concat, + ops::ConcatOp, + ops::ConcatOpMaker, + ops::ConcatGradOpMaker, + ops::ConcatGradOpMaker, + ops::ConcatCompositeGradOpMaker, + ConcatInferShapeFunctor); +REGISTER_OPERATOR(concat_grad, + ops::ConcatOpGrad, + ops::ConcatDoubleGradOpMaker, + ops::ConcatDoubleGradOpMaker, + ops::ConcatOpGradNoNeedBufferVarInferer); diff --git a/paddle/fluid/operators/concat_op.h b/paddle/fluid/operators/concat_op.h new file mode 100644 index 0000000000000..58d978ea9c73a --- /dev/null +++ b/paddle/fluid/operators/concat_op.h @@ -0,0 +1,47 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#pragma once + +#include +#include +#include + +#include "paddle/fluid/framework/op_registry.h" +#include "paddle/fluid/operators/math/concat_and_split.h" +#include "paddle/fluid/operators/utils.h" +#include "paddle/phi/kernels/concat_kernel.h" +#include "paddle/phi/kernels/funcs/concat_funcs.h" +#include "paddle/phi/kernels/funcs/strided_memcpy.h" + +namespace paddle { +namespace operators { + +static inline int64_t ComputeAxis(int64_t axis, int64_t rank) { + PADDLE_ENFORCE_EQ( + axis >= -rank && axis < rank, + true, + platform::errors::InvalidArgument( + "The axis is expected to be in range of [%d, %d), but got %d", + -rank, + rank, + axis)); + if (axis < 0) { + axis = axis + rank; + } + return axis > 0 ? axis : 0; +} + +} // namespace operators +} // namespace paddle diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index 76a7b89984b33..f9868df491388 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -318,25 +318,6 @@ func : complex_grad data_type : real -- backward_op : concat_double_grad - forward : concat_grad (Tensor[] x, Tensor grad_out, Scalar axis) -> Tensor[](grad_x) - args : (Tensor[] grad_x_grad, Scalar axis = 0) - output : Tensor(grad_out_grad) - invoke : concat(grad_x_grad, axis) - -- backward_op : concat_grad - forward : concat (Tensor[] x, Scalar axis) -> Tensor(out) - args : (Tensor[] x, Tensor out_grad, Scalar axis = 0) - output : Tensor[](x_grad){x.size()} - infer_meta : - func : UnchangedMultiInferMeta - param : [x] - kernel : - func : concat_grad - composite : concat_grad(x, out_grad, axis, x_grad) - no_need_buffer : x - backward : concat_double_grad - - backward_op : conj_grad forward : conj (Tensor x) -> Tensor(out) args : (Tensor out_grad) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 70874d55e08bb..3e625667cf445 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -118,6 +118,25 @@ kernel : func : channel_shuffle_grad +- backward_op : concat_double_grad + forward : concat_grad (Tensor[] x, Tensor grad_out, Scalar axis) -> Tensor[](grad_x) + args : (Tensor[] grad_x_grad, Scalar axis = 0) + output : Tensor(grad_out_grad) + invoke : concat(grad_x_grad, axis) + +- backward_op : concat_grad + forward : concat (Tensor[] x, Scalar axis) -> Tensor(out) + args : (Tensor[] x, Tensor out_grad, Scalar axis = 0) + output : Tensor[](x_grad){x.size()} + infer_meta : + func : UnchangedMultiInferMeta + param : [x] + kernel : + func : concat_grad + composite : concat_grad(x, out_grad, axis, x_grad) + no_need_buffer : x + backward : concat_double_grad + - backward_op : conv2d_grad forward : conv2d (Tensor input, Tensor filter, int[] strides, int[] paddings, str padding_algorithm, int[] dilations, int groups, str data_format) -> Tensor(out) args : (Tensor input, Tensor filter, Tensor out_grad, int[] strides, int[] paddings, str padding_algorithm, int[] dilations, int groups, str data_format) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index ffafff09cdfa9..4dba3fdd74ec8 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -162,6 +162,16 @@ data_type : x inplace : (x -> out), (input_found_infinite -> output_found_infinite) +- op : concat + args : (Tensor[] x, Scalar(int64_t) axis) + output : Tensor + infer_meta : + func : ConcatInferMeta + param : [x, axis] + kernel : + func : concat + backward : concat_grad + - op : conv2d args : (Tensor input, Tensor filter, int[] strides, int[] paddings, str padding_algorithm, int[] dilations, int groups, str data_format) output : Tensor diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index d5bdc6ab843c2..a9c10037f80d7 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -445,16 +445,6 @@ data_type : real backward : complex_grad -- op : concat - args : (Tensor[] x, Scalar(int64_t) axis) - output : Tensor - infer_meta : - func : ConcatInferMeta - param : [x, axis] - kernel : - func : concat - backward : concat_grad - - op : conj args : (Tensor x) output : Tensor (out) diff --git a/paddle/phi/ops/compat/concat_sig.cc b/paddle/phi/ops/compat/concat_sig.cc new file mode 100644 index 0000000000000..c5e6a4b020089 --- /dev/null +++ b/paddle/phi/ops/compat/concat_sig.cc @@ -0,0 +1,38 @@ +/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include "paddle/phi/core/compat/op_utils.h" + +namespace phi { + +KernelSignature ConcatOpArgumentMapping(const ArgumentMappingContext& ctx) { + if (ctx.HasInput("AxisTensor")) { + return KernelSignature("concat", {"X"}, {"AxisTensor"}, {"Out"}); + } + return KernelSignature("concat", {"X"}, {"axis"}, {"Out"}); +} + +KernelSignature ConcatGradOpArgumentMapping(const ArgumentMappingContext& ctx) { + if (ctx.HasInput("AxisTensor")) { + return KernelSignature( + "concat_grad", {"X", "Out@GRAD"}, {"AxisTensor"}, {"X@GRAD"}); + } + return KernelSignature( + "concat_grad", {"X", "Out@GRAD"}, {"axis"}, {"X@GRAD"}); +} + +} // namespace phi + +PD_REGISTER_ARG_MAPPING_FN(concat, phi::ConcatOpArgumentMapping); +PD_REGISTER_ARG_MAPPING_FN(concat_grad, phi::ConcatGradOpArgumentMapping); From a8229c21ec26c2d8fa523a7e4756d75af3ec3277 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Sat, 3 Jun 2023 07:16:08 +0800 Subject: [PATCH 04/13] Revert "[static op generation] transpose" This reverts commit 7c6073eb17c1f42109509f3515957e528a04ec09. --- .../generator/get_expected_kernel_func.cc | 10 -- .../generator/get_expected_kernel_func.h | 4 - paddle/fluid/operators/transpose_op.cc | 98 +++++++++++++++++++ paddle/phi/api/yaml/backward.yaml | 18 ---- paddle/phi/api/yaml/legacy_backward.yaml | 18 ++++ paddle/phi/api/yaml/legacy_ops.yaml | 9 ++ paddle/phi/api/yaml/op_compat.yaml | 2 - paddle/phi/api/yaml/ops.yaml | 9 -- paddle/phi/ops/compat/transpose_sig.cc | 2 + 9 files changed, 127 insertions(+), 43 deletions(-) diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.cc b/paddle/fluid/operators/generator/get_expected_kernel_func.cc index ba6736e4a67f0..0520af7c505b7 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.cc +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.cc @@ -285,15 +285,5 @@ phi::KernelKey GetLayerNormExpectedKernelType( return phi::KernelKey(input_data_type, ctx.GetPlace()); } -phi::KernelKey GetTransposeExpectedKernelType( - const framework::ExecutionContext& ctx, - const framework::OperatorWithKernel* op_ptr) { - auto data_type = op_ptr->OperatorWithKernel::IndicateVarDataType(ctx, "X"); - auto& data_format = ctx.Attr("data_format"); - phi::DataLayout layout_ = phi::StringToDataLayout(data_format); - return phi::KernelKey( - ctx.GetPlace(), layout_, phi::TransToPhiDataType(data_type)); -} - } // namespace operators } // namespace paddle diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.h b/paddle/fluid/operators/generator/get_expected_kernel_func.h index a566f4834e96f..bf7c691fffa46 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.h +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.h @@ -72,9 +72,5 @@ phi::KernelKey GetLayerNormExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr); -phi::KernelKey GetTransposeExpectedKernelType( - const framework::ExecutionContext& ctx, - const framework::OperatorWithKernel* op_ptr); - } // namespace operators } // namespace paddle diff --git a/paddle/fluid/operators/transpose_op.cc b/paddle/fluid/operators/transpose_op.cc index f24d7f904dec3..652f88fec8f45 100644 --- a/paddle/fluid/operators/transpose_op.cc +++ b/paddle/fluid/operators/transpose_op.cc @@ -21,6 +21,92 @@ limitations under the License. */ namespace paddle { namespace operators { +phi::KernelKey TransposeOp::GetExpectedKernelType( + const framework::ExecutionContext &ctx) const { + auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X"); + auto &data_format = ctx.Attr("data_format"); + phi::DataLayout layout_ = phi::StringToDataLayout(data_format); + return phi::KernelKey( + ctx.GetPlace(), layout_, phi::TransToPhiDataType(data_type)); +} + +class TransposeOpMaker : public framework::OpProtoAndCheckerMaker { + public: + void Make() override { + AddInput( + "X", + "(Tensor) The input tensor, tensors with rank up to 6 are supported."); + AddOutput("Out", "(Tensor)The output tensor."); + AddAttr>( + "axis", + "(vector) A list of values, and the size of the list should be " + "the same with the input tensor rank. This operator permutes the input " + "tensor's axes according to the values given."); + AddAttr("use_mkldnn", + "(bool, default false) Only used in mkldnn kernel") + .SetDefault(false) + .AsExtra(); + AddAttr( + "data_format", + "(string, default NCHW) Only used in " + "An optional string from: \"NHWC\", \"NCHW\". " + "Defaults to \"NHWC\". Specify the data format of the output data, " + "the input will be transformed automatically. ") + .SetDefault("AnyLayout") + .AsExtra(); + AddAttr( + "mkldnn_data_type", + "(string, default \"float32\"). Data type of mkldnn kernel") + .SetDefault("float32") + .InEnum({"float32", "int8", "bfloat16"}) + .AsExtra(); + AddComment(R"DOC( +Transpose Operator. + +The input tensor will be permuted according to the axes given. +The behavior of this operator is similar to how `numpy.transpose` works. + +- suppose the input `X` is a 2-D tensor: + $$ + X = \begin{pmatrix} + 0 &1 &2 \\ + 3 &4 &5 + \end{pmatrix}$$ + + the given `axes` is: $[1, 0]$, and $Y$ = transpose($X$, axis) + + then the output $Y$ is: + + $$ + Y = \begin{pmatrix} + 0 &3 \\ + 1 &4 \\ + 2 &5 + \end{pmatrix}$$ + +- Given a input tensor with shape $(N, C, H, W)$ and the `axes` is +$[0, 2, 3, 1]$, then shape of the output tensor will be: $(N, H, W, C)$. + +)DOC"); + } +}; + +class TransposeOpGrad : public framework::OperatorWithKernel { + public: + using framework::OperatorWithKernel::OperatorWithKernel; + + protected: + phi::KernelKey GetExpectedKernelType( + const framework::ExecutionContext &ctx) const override { + auto data_type = OperatorWithKernel::IndicateVarDataType( + ctx, framework::GradVarName("Out")); + std::string data_format = ctx.Attr("data_format"); + phi::DataLayout layout_ = phi::StringToDataLayout(data_format); + return phi::KernelKey( + ctx.GetPlace(), layout_, phi::TransToPhiDataType(data_type)); + } +}; + void Transpose2Op::InferShape(framework::InferShapeContext *ctx) const { using CompatMetaTensor = framework::CompatMetaTensor; CompatMetaTensor x(ctx->GetInputVarPtrs("X")[0], ctx->IsRuntime()); @@ -176,6 +262,18 @@ DECLARE_INFER_SHAPE_FUNCTOR(transpose2_grad, PD_INFER_META(phi::TransposeGradInferMeta)); namespace ops = paddle::operators; +REGISTER_OPERATOR( + transpose, + ops::TransposeOp, + ops::TransposeOpMaker, + paddle::framework::DefaultGradOpMaker, + paddle::framework::DefaultGradOpMaker, + TransposeInferShapeFunctor); + +REGISTER_OPERATOR(transpose_grad, + ops::TransposeOpGrad, + ops::TransposeGradInferVarType, + TransposeGradInferShapeFunctor); REGISTER_OPERATOR(transpose2, ops::Transpose2Op, diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index b2c8dda0b8bb0..f9868df491388 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -2101,24 +2101,6 @@ data_type : out_grad no_need_buffer : x -- backward_op : transpose_double_grad - forward : transpose_grad (Tensor grad_out, int[] perm) -> Tensor(grad_x) - args : (Tensor grad_x_grad, int[] perm) - output : Tensor(grad_out_grad) - invoke : transpose(grad_x_grad, perm) - -- backward_op : transpose_grad - forward : transpose (Tensor x, int[] perm) -> Tensor(out) - args : (Tensor out_grad, int[] perm) - output : Tensor(x_grad) - infer_meta : - func : TransposeGradInferMeta - param : [out_grad, perm] - kernel : - func : transpose_grad - backward : transpose_double_grad - composite: transpose_grad(out_grad, perm, x_grad) - - backward_op : triangular_solve_grad forward : triangular_solve (Tensor x, Tensor y, bool upper=true, bool transpose=false, bool unitriangular=false) -> Tensor(out) args : (Tensor x, Tensor y, Tensor out, Tensor out_grad, bool upper, bool transpose, bool unitriangular) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 22fcd4870357c..3e625667cf445 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -934,6 +934,24 @@ kernel : func : trans_layout_grad +- backward_op : transpose_double_grad + forward : transpose_grad (Tensor grad_out, int[] perm) -> Tensor(grad_x) + args : (Tensor grad_x_grad, int[] perm) + output : Tensor(grad_out_grad) + invoke : transpose(grad_x_grad, perm) + +- backward_op : transpose_grad + forward : transpose (Tensor x, int[] perm) -> Tensor(out) + args : (Tensor out_grad, int[] perm) + output : Tensor(x_grad) + infer_meta : + func : TransposeGradInferMeta + param : [out_grad, perm] + kernel : + func : transpose_grad + backward : transpose_double_grad + composite: transpose_grad(out_grad, perm, x_grad) + - backward_op : tril_grad forward : tril(Tensor x, int diagonal) -> Tensor(out) args : (Tensor out_grad, int diagonal) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 354d9cbef1fb1..4dba3fdd74ec8 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -1060,6 +1060,15 @@ func : transpose backward : trans_layout_grad +- op : transpose + args : (Tensor x, int[] perm) + output : Tensor + infer_meta : + func : TransposeInferMeta + kernel : + func : transpose + backward : transpose_grad + - op : tril args : (Tensor x, int diagonal) output : Tensor(out) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 4cd406e763923..bbe3017e27eba 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -2382,8 +2382,6 @@ extra : outputs : [XShape] attrs : [bool use_mkldnn = false, str data_format = "AnyLayout", str mkldnn_data_type = "float32"] - get_expected_kernel_type: - transpose: GetTransposeExpectedKernelType - op : triangular_solve backward : triangular_solve_grad diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 38b7e112e8f85..a9c10037f80d7 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2192,15 +2192,6 @@ func : trace backward : trace_grad -- op : transpose - args : (Tensor x, int[] perm) - output : Tensor - infer_meta : - func : TransposeInferMeta - kernel : - func : transpose - backward : transpose_grad - - op : triangular_solve args : (Tensor x, Tensor y, bool upper=true, bool transpose=false, bool unitriangular=false) output : Tensor diff --git a/paddle/phi/ops/compat/transpose_sig.cc b/paddle/phi/ops/compat/transpose_sig.cc index f7a465e0e2ffe..ca5b76e1298a0 100644 --- a/paddle/phi/ops/compat/transpose_sig.cc +++ b/paddle/phi/ops/compat/transpose_sig.cc @@ -34,3 +34,5 @@ PD_REGISTER_BASE_KERNEL_NAME(transpose2_grad, transpose_grad); PD_REGISTER_ARG_MAPPING_FN(transpose2, phi::TransposeOpArgumentMapping); PD_REGISTER_ARG_MAPPING_FN(transpose2_grad, phi::TransposeGradOpArgumentMapping); +PD_REGISTER_ARG_MAPPING_FN(transpose, phi::TransposeOpArgumentMapping); +PD_REGISTER_ARG_MAPPING_FN(transpose_grad, phi::TransposeGradOpArgumentMapping); From 58963931b7c1b3183cae96ab21902d26b0415af1 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Sat, 3 Jun 2023 07:20:58 +0800 Subject: [PATCH 05/13] mv transpose2 from transpose_sig.cc --- paddle/phi/ops/compat/transpose_sig.cc | 6 ------ 1 file changed, 6 deletions(-) diff --git a/paddle/phi/ops/compat/transpose_sig.cc b/paddle/phi/ops/compat/transpose_sig.cc index ca5b76e1298a0..0a503fbdabee4 100644 --- a/paddle/phi/ops/compat/transpose_sig.cc +++ b/paddle/phi/ops/compat/transpose_sig.cc @@ -28,11 +28,5 @@ KernelSignature TransposeGradOpArgumentMapping( } // namespace phi -PD_REGISTER_BASE_KERNEL_NAME(transpose2, transpose); -PD_REGISTER_BASE_KERNEL_NAME(transpose2_grad, transpose_grad); - -PD_REGISTER_ARG_MAPPING_FN(transpose2, phi::TransposeOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(transpose2_grad, - phi::TransposeGradOpArgumentMapping); PD_REGISTER_ARG_MAPPING_FN(transpose, phi::TransposeOpArgumentMapping); PD_REGISTER_ARG_MAPPING_FN(transpose_grad, phi::TransposeGradOpArgumentMapping); From c92ffae8ac5db213e52a525ea22d3f8cc563fedf Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Sat, 3 Jun 2023 07:28:37 +0800 Subject: [PATCH 06/13] mv transpose2 --- paddle/fluid/operators/transpose_op.cc | 149 ----------------------- paddle/phi/api/yaml/legacy_backward.yaml | 18 --- paddle/phi/api/yaml/legacy_ops.yaml | 9 -- paddle/phi/api/yaml/static_backward.yaml | 18 +++ paddle/phi/api/yaml/static_ops.yaml | 9 ++ 5 files changed, 27 insertions(+), 176 deletions(-) diff --git a/paddle/fluid/operators/transpose_op.cc b/paddle/fluid/operators/transpose_op.cc index 652f88fec8f45..d58bd803b756c 100644 --- a/paddle/fluid/operators/transpose_op.cc +++ b/paddle/fluid/operators/transpose_op.cc @@ -107,138 +107,6 @@ class TransposeOpGrad : public framework::OperatorWithKernel { } }; -void Transpose2Op::InferShape(framework::InferShapeContext *ctx) const { - using CompatMetaTensor = framework::CompatMetaTensor; - CompatMetaTensor x(ctx->GetInputVarPtrs("X")[0], ctx->IsRuntime()); - CompatMetaTensor out(ctx->GetOutputVarPtrs("Out")[0], ctx->IsRuntime()); - std::vector axis = ctx->Attrs().Get>("axis"); - phi::TransposeInferMeta(x, axis, &out); - - if (!ctx->HasOutput("XShape")) return; - const auto &in_dims = ctx->GetInputDim("X"); - std::vector x_shape_dim(in_dims.size() + 1); - x_shape_dim[0] = 0; - for (int i = 0; i < in_dims.size(); ++i) { - x_shape_dim[i + 1] = in_dims[i]; - } - ctx->SetOutputDim("XShape", phi::make_ddim(x_shape_dim)); - ctx->ShareLoD("X", /*->*/ "XShape"); -} - -phi::KernelKey Transpose2Op::GetExpectedKernelType( - const framework::ExecutionContext &ctx) const { - auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X"); - auto &data_format = ctx.Attr("data_format"); - phi::DataLayout layout_ = phi::StringToDataLayout(data_format); - return phi::KernelKey( - ctx.GetPlace(), layout_, phi::TransToPhiDataType(data_type)); -} - -void Transpose2OpMaker::Make() { - AddInput( - "X", - "(Tensor) The input tensor, tensors with rank up to 6 are supported."); - AddOutput("Out", "(Tensor)The output tensor."); - AddAttr>( - "axis", - "(vector) A list of values, and the size of the list should be " - "the same with the input tensor rank. This operator permutes the input " - "tensor's axes according to the values given."); - AddOutput("XShape", "(Tensor)The output tensor.").AsIntermediate().AsExtra(); - AddComment(R"DOC( -Transpose Operator. - -The input tensor will be permuted according to the axes given. -The behavior of this operator is similar to how `numpy.transpose` works. - -- suppose the input `X` is a 2-D tensor: - $$ - X = \begin{pmatrix} - 0 &1 &2 \\ - 3 &4 &5 - \end{pmatrix}$$ - - the given `axes` is: $[1, 0]$, and $Y$ = transpose($X$, axis) - - then the output $Y$ is: - - $$ - Y = \begin{pmatrix} - 0 &3 \\ - 1 &4 \\ - 2 &5 - \end{pmatrix}$$ - -- Given a input tensor with shape $(N, C, H, W)$ and the `axes` is -$[0, 2, 3, 1]$, then shape of the output tensor will be: $(N, H, W, C)$. - -)DOC"); - Apply(); -} - -template -class Transpose2GradMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - void Apply(GradOpPtr grad_op) const override { - grad_op->SetType("transpose2_grad"); - grad_op->SetInput("XShape", this->Output("XShape")); - grad_op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); - grad_op->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); - grad_op->SetAttrMap(this->Attrs()); - } -}; - -class Transpose2CompositeGradOpMaker : public prim::CompositeGradOpMakerBase { - using prim::CompositeGradOpMakerBase::CompositeGradOpMakerBase; - - public: - void Apply() override { - paddle::Tensor xshape = this->GetSingleForwardOutput("XShape"); - paddle::Tensor out_grad = this->GetSingleOutputGrad("Out"); - paddle::Tensor dx = this->GetSingleInputGrad("X"); - auto *dx_ptr = this->GetOutputPtr(&dx); - std::string dx_name = this->GetOutputName(dx); - std::vector axis = - static_cast>(this->Attr>("axis")); - VLOG(6) << "Runing transpose2_grad composite func"; - prim::transpose_grad(out_grad, axis, dx_ptr); - this->RecoverOutputName(dx, dx_name); - } -}; - -template -class Transpose2DoubleGradMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - void Apply(GradOpPtr grad_op) const override { - grad_op->SetType("transpose2"); - grad_op->SetInput("X", this->OutputGrad(framework::GradVarName("X"))); - grad_op->SetOutput("Out", this->InputGrad(framework::GradVarName("Out"))); - grad_op->SetOutput("XShape", this->Input("XShape")); - grad_op->SetAttrMap(this->Attrs()); - } -}; - -class Transpose2OpGrad : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - framework::proto::VarType::Type data_type = - OperatorWithKernel::IndicateVarDataType(ctx, - framework::GradVarName("Out")); - std::string data_format = ctx.Attr("data_format"); - phi::DataLayout layout_ = phi::StringToDataLayout(data_format); - return phi::KernelKey( - ctx.GetPlace(), layout_, phi::TransToPhiDataType(data_type)); - } -}; - class TransposeGradInferVarType : public framework::VarTypeInference { public: void operator()(framework::InferVarTypeContext *ctx) const override { @@ -257,10 +125,6 @@ DECLARE_INFER_SHAPE_FUNCTOR(transpose_grad, TransposeGradInferShapeFunctor, PD_INFER_META(phi::TransposeGradInferMeta)); -DECLARE_INFER_SHAPE_FUNCTOR(transpose2_grad, - Transpose2GradInferShapeFunctor, - PD_INFER_META(phi::TransposeGradInferMeta)); - namespace ops = paddle::operators; REGISTER_OPERATOR( transpose, @@ -274,16 +138,3 @@ REGISTER_OPERATOR(transpose_grad, ops::TransposeOpGrad, ops::TransposeGradInferVarType, TransposeGradInferShapeFunctor); - -REGISTER_OPERATOR(transpose2, - ops::Transpose2Op, - ops::Transpose2OpMaker, - ops::Transpose2GradMaker, - ops::Transpose2GradMaker, - ops::Transpose2CompositeGradOpMaker); -REGISTER_OPERATOR(transpose2_grad, - ops::Transpose2OpGrad, - ops::TransposeGradInferVarType, - ops::Transpose2DoubleGradMaker, - ops::Transpose2DoubleGradMaker, - Transpose2GradInferShapeFunctor); diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 07547bbb89737..5d01d87371ecc 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -868,24 +868,6 @@ kernel : func : trans_layout_grad -- backward_op : transpose_double_grad - forward : transpose_grad (Tensor grad_out, int[] perm) -> Tensor(grad_x) - args : (Tensor grad_x_grad, int[] perm) - output : Tensor(grad_out_grad) - invoke : transpose(grad_x_grad, perm) - -- backward_op : transpose_grad - forward : transpose (Tensor x, int[] perm) -> Tensor(out) - args : (Tensor out_grad, int[] perm) - output : Tensor(x_grad) - infer_meta : - func : TransposeGradInferMeta - param : [out_grad, perm] - kernel : - func : transpose_grad - backward : transpose_double_grad - composite: transpose_grad(out_grad, perm, x_grad) - - backward_op : tril_grad forward : tril(Tensor x, int diagonal) -> Tensor(out) args : (Tensor out_grad, int diagonal) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 532e445a1a782..5b0ace78d7081 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -1008,15 +1008,6 @@ func : transpose backward : trans_layout_grad -- op : transpose - args : (Tensor x, int[] perm) - output : Tensor - infer_meta : - func : TransposeInferMeta - kernel : - func : transpose - backward : transpose_grad - - op : tril args : (Tensor x, int diagonal) output : Tensor(out) diff --git a/paddle/phi/api/yaml/static_backward.yaml b/paddle/phi/api/yaml/static_backward.yaml index ed0a9fc43b4d7..8ba4a78d0f9b1 100755 --- a/paddle/phi/api/yaml/static_backward.yaml +++ b/paddle/phi/api/yaml/static_backward.yaml @@ -143,6 +143,24 @@ func : swish_grad inplace : (out_grad -> x_grad) +- backward_op : transpose_double_grad + forward : transpose_grad (Tensor grad_out, int[] perm) -> Tensor(grad_x) + args : (Tensor grad_x_grad, int[] perm) + output : Tensor(grad_out_grad) + invoke : transpose(grad_x_grad, perm) + +- backward_op : transpose_grad + forward : transpose (Tensor x, int[] perm) -> Tensor(out) + args : (Tensor out_grad, int[] perm) + output : Tensor(x_grad) + infer_meta : + func : TransposeGradInferMeta + param : [out_grad, perm] + kernel : + func : transpose_grad + backward : transpose_double_grad + composite: transpose_grad(out_grad, perm, x_grad) + - backward_op : tril_triu_grad forward : tril_triu (Tensor x, int diagonal = 0, bool lower = false) -> Tensor(out) args : (Tensor out_grad, int diagonal, bool lower) diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index 1d309fb4f1f42..1647cadfa6a58 100755 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -429,6 +429,15 @@ func : swish_raw backward : swish_grad +- op : transpose + args : (Tensor x, int[] perm) + output : Tensor + infer_meta : + func : TransposeInferMeta + kernel : + func : transpose + backward : transpose_grad + - op : tril_indices args : (int rows = 0, int cols = 0, int offset = 0, DataType dtype = DataType::INT64) output : Tensor(out) From 065c2c65abd858be295159863836e7adb95d8f45 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Sat, 3 Jun 2023 07:38:07 +0800 Subject: [PATCH 07/13] add GetTranspose2ExpectedKernelType --- .../operators/generator/get_expected_kernel_func.cc | 10 ++++++++++ .../operators/generator/get_expected_kernel_func.h | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.cc b/paddle/fluid/operators/generator/get_expected_kernel_func.cc index 96060b6de5bdf..9fe3594b4d41c 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.cc +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.cc @@ -371,5 +371,15 @@ phi::KernelKey GetConvExpectedKernelType( return phi::KernelKey(input_data_type, ctx.GetPlace()); } +phi::KernelKey GetTranspose2ExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr) { + auto data_type = op_ptr->IndicateVarDataType(ctx, "X"); + auto& data_format = ctx.Attr("data_format"); + phi::DataLayout layout_ = phi::StringToDataLayout(data_format); + return phi::KernelKey( + ctx.GetPlace(), layout_, phi::TransToPhiDataType(data_type)); +} + } // namespace operators } // namespace paddle diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.h b/paddle/fluid/operators/generator/get_expected_kernel_func.h index 37dfc7607da5e..779b740dd6e80 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.h +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.h @@ -92,5 +92,9 @@ phi::KernelKey GetConvExpectedKernelType( const framework::ExecutionContext& ctx, const framework::OperatorWithKernel* op_ptr); +phi::KernelKey GetTranspose2ExpectedKernelType( + const framework::ExecutionContext& ctx, + const framework::OperatorWithKernel* op_ptr); + } // namespace operators } // namespace paddle From 7e7a44d2ea919af5097dc6c5e39e7707e80b5a95 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Sat, 3 Jun 2023 07:40:13 +0800 Subject: [PATCH 08/13] add GetTranspose2ExpectedKernelType --- paddle/phi/api/yaml/op_compat.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 59472fd3bfb28..b06e3ee71f35e 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -2494,6 +2494,8 @@ extra : outputs : [XShape] attrs : [bool use_mkldnn = false, str data_format = "AnyLayout", str mkldnn_data_type = "float32"] + get_expected_kernel_type: + transpose: GetTranspose2ExpectedKernelType - op : triangular_solve backward : triangular_solve_grad From 1d4a378afd07fd4e73296d6fbad9ee687fe971f7 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Sat, 3 Jun 2023 07:44:11 +0800 Subject: [PATCH 09/13] add transpose2 --- paddle/phi/api/yaml/op_compat.yaml | 2 +- paddle/phi/api/yaml/static_ops.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index b06e3ee71f35e..6f6bdfbef5832 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -2495,7 +2495,7 @@ outputs : [XShape] attrs : [bool use_mkldnn = false, str data_format = "AnyLayout", str mkldnn_data_type = "float32"] get_expected_kernel_type: - transpose: GetTranspose2ExpectedKernelType + transpose2: GetTranspose2ExpectedKernelType - op : triangular_solve backward : triangular_solve_grad diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index 1647cadfa6a58..21c76ad31a0ac 100755 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -429,7 +429,7 @@ func : swish_raw backward : swish_grad -- op : transpose +- op : transpose(transpose2) args : (Tensor x, int[] perm) output : Tensor infer_meta : From 0684b9400db5bceef2f23e64bc02bc1278730e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Sat, 3 Jun 2023 12:58:24 +0800 Subject: [PATCH 10/13] Apply suggestions from code review --- paddle/phi/api/yaml/static_ops.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index 21c76ad31a0ac..1647cadfa6a58 100755 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -429,7 +429,7 @@ func : swish_raw backward : swish_grad -- op : transpose(transpose2) +- op : transpose args : (Tensor x, int[] perm) output : Tensor infer_meta : From 3e89ac49ed9c96db1f28bc71723353426934d786 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Mon, 5 Jun 2023 18:43:03 +0800 Subject: [PATCH 11/13] add transpose2 --- paddle/phi/api/yaml/backward.yaml | 18 ++++++++++++++++++ paddle/phi/api/yaml/ops.yaml | 9 +++++++++ paddle/phi/api/yaml/static_backward.yaml | 18 ------------------ paddle/phi/api/yaml/static_ops.yaml | 9 --------- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index bf9bc57c69117..82134feb273d4 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -2173,6 +2173,24 @@ data_type : out_grad no_need_buffer : x +- backward_op : transpose_double_grad + forward : transpose_grad (Tensor grad_out, int[] perm) -> Tensor(grad_x) + args : (Tensor grad_x_grad, int[] perm) + output : Tensor(grad_out_grad) + invoke : transpose(grad_x_grad, perm) + +- backward_op : transpose_grad + forward : transpose (Tensor x, int[] perm) -> Tensor(out) + args : (Tensor out_grad, int[] perm) + output : Tensor(x_grad) + infer_meta : + func : TransposeGradInferMeta + param : [out_grad, perm] + kernel : + func : transpose_grad + backward : transpose_double_grad + composite: transpose_grad(out_grad, perm, x_grad) + - backward_op : triangular_solve_grad forward : triangular_solve (Tensor x, Tensor y, bool upper=true, bool transpose=false, bool unitriangular=false) -> Tensor(out) args : (Tensor x, Tensor y, Tensor out, Tensor out_grad, bool upper, bool transpose, bool unitriangular) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 043527bbdef22..3f1ee9e87d193 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2242,6 +2242,15 @@ func : trace backward : trace_grad +- op : transpose(transpose2) + args : (Tensor x, int[] perm) + output : Tensor + infer_meta : + func : TransposeInferMeta + kernel : + func : transpose + backward : transpose_grad + - op : triangular_solve args : (Tensor x, Tensor y, bool upper=true, bool transpose=false, bool unitriangular=false) output : Tensor diff --git a/paddle/phi/api/yaml/static_backward.yaml b/paddle/phi/api/yaml/static_backward.yaml index 8ba4a78d0f9b1..ed0a9fc43b4d7 100755 --- a/paddle/phi/api/yaml/static_backward.yaml +++ b/paddle/phi/api/yaml/static_backward.yaml @@ -143,24 +143,6 @@ func : swish_grad inplace : (out_grad -> x_grad) -- backward_op : transpose_double_grad - forward : transpose_grad (Tensor grad_out, int[] perm) -> Tensor(grad_x) - args : (Tensor grad_x_grad, int[] perm) - output : Tensor(grad_out_grad) - invoke : transpose(grad_x_grad, perm) - -- backward_op : transpose_grad - forward : transpose (Tensor x, int[] perm) -> Tensor(out) - args : (Tensor out_grad, int[] perm) - output : Tensor(x_grad) - infer_meta : - func : TransposeGradInferMeta - param : [out_grad, perm] - kernel : - func : transpose_grad - backward : transpose_double_grad - composite: transpose_grad(out_grad, perm, x_grad) - - backward_op : tril_triu_grad forward : tril_triu (Tensor x, int diagonal = 0, bool lower = false) -> Tensor(out) args : (Tensor out_grad, int diagonal, bool lower) diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index 21c76ad31a0ac..1d309fb4f1f42 100755 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -429,15 +429,6 @@ func : swish_raw backward : swish_grad -- op : transpose(transpose2) - args : (Tensor x, int[] perm) - output : Tensor - infer_meta : - func : TransposeInferMeta - kernel : - func : transpose - backward : transpose_grad - - op : tril_indices args : (int rows = 0, int cols = 0, int offset = 0, DataType dtype = DataType::INT64) output : Tensor(out) From a40bd7ac816b494fb845df0341b7d3af193f8863 Mon Sep 17 00:00:00 2001 From: Liyulingyue <852433440@qq.com> Date: Mon, 5 Jun 2023 19:58:58 +0800 Subject: [PATCH 12/13] add transpose2 --- paddle/phi/api/yaml/ops.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 3f1ee9e87d193..07d92e211b6e9 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2242,7 +2242,7 @@ func : trace backward : trace_grad -- op : transpose(transpose2) +- op : transpose (transpose2) args : (Tensor x, int[] perm) output : Tensor infer_meta : From 11d9e9a02b98048b06fc16c1626baf109032c1ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Tue, 6 Jun 2023 17:14:57 +0800 Subject: [PATCH 13/13] Apply suggestions from code review --- paddle/phi/api/yaml/ops.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 07d92e211b6e9..6c69949c18ab5 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2242,7 +2242,7 @@ func : trace backward : trace_grad -- op : transpose (transpose2) +- op : transpose args : (Tensor x, int[] perm) output : Tensor infer_meta :