From ab754417ae0ff6ae18592cb711245caca0be757c Mon Sep 17 00:00:00 2001 From: Wang Xin Date: Tue, 11 Apr 2023 10:47:38 +0800 Subject: [PATCH] add autogen code support for reverse op (#52701) * add autogen code support for reverse op * bug fixed --- paddle/fluid/operators/reverse_op.cc | 117 ----------------------- paddle/phi/api/yaml/backward.yaml | 6 ++ paddle/phi/api/yaml/legacy_backward.yaml | 6 -- paddle/phi/api/yaml/legacy_ops.yaml | 9 -- paddle/phi/api/yaml/op_compat.yaml | 11 +++ paddle/phi/api/yaml/ops.yaml | 10 ++ 6 files changed, 27 insertions(+), 132 deletions(-) delete mode 100644 paddle/fluid/operators/reverse_op.cc diff --git a/paddle/fluid/operators/reverse_op.cc b/paddle/fluid/operators/reverse_op.cc deleted file mode 100644 index 07c3aac52078a..0000000000000 --- a/paddle/fluid/operators/reverse_op.cc +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright (c) 2018 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 -#include - -#include "paddle/fluid/framework/infershape_utils.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/phi/core/infermeta_utils.h" -#include "paddle/phi/infermeta/unary.h" - -namespace paddle { -namespace operators { - -class ReverseOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - auto input_data_type = - framework::OperatorWithKernel::IndicateVarDataType(ctx, "X"); - - return phi::KernelKey(input_data_type, ctx.GetPlace()); - } -}; - -class ReverseOpVarTypeInference : public framework::VarTypeInference { - public: - void operator()(framework::InferVarTypeContext* ctx) const override { - ctx->SetOutputType("Out", ctx->GetInputType("X")); - ctx->SetOutputDataType("Out", ctx->GetInputDataType("X")); - } -}; - -class ReverseOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("X", "The phi::DenseTensor to be flipped."); - AddOutput("Out", "The phi::DenseTensor after flipping."); - AddAttr>( - "axis", "The axises that along which order of elements is reversed.") - .SupportTensor(); - AddComment(R"DOC( - Reverse Operator. - - Reverse the order of elements in the input phi::DenseTensor along given axises. - - Case 1: - Given - X = [[1, 2, 3, 4, 5] - [6, 7, 8, 9, 10] - [11, 12, 13, 14, 15]], - and - axis = [0], - we get: - Out = [[11, 12, 13, 14, 15] - [6, 7, 8, 9, 10] - [1, 2, 3, 4, 5]]. - - Case 2: - Given - X = [[[1, 2, 3, 4] - [5, 6, 7, 8]] - [[9, 10, 11, 12] - [13, 14, 15, 16]]], - and - axis = [0, 2], - we get: - Out = [[[12, 11, 10, 9] - [16, 15, 14, 13]] - [[4, 3, 2, 1] - [8, 7, 6, 5]]], - )DOC"); - } -}; - -template -class ReverseGradMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - void Apply(GradOpPtr grad_op) const override { - grad_op->SetType("reverse"); - grad_op->SetInput("X", this->OutputGrad("Out")); - grad_op->SetOutput("Out", this->InputGrad("X")); - grad_op->SetAttr("axis", this->GetAttr("axis")); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -DECLARE_INFER_SHAPE_FUNCTOR(reverse, - ReverseInferShapeFunctor, - PD_INFER_META(phi::ReverseInferMeta)); -REGISTER_OPERATOR(reverse, - ops::ReverseOp, - ops::ReverseOpMaker, - ops::ReverseGradMaker, - ops::ReverseGradMaker, - ops::ReverseOpVarTypeInference, - ReverseInferShapeFunctor); -REGISTER_OPERATOR(reverse_grad, ops::ReverseOp, ops::ReverseOpVarTypeInference); diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index 1c3599d07a595..f4608f008535c 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -1328,6 +1328,12 @@ kernel : func : renorm_grad +- backward_op : reverse_grad + forward : reverse (Tensor x, IntArray axis) -> Tensor(out) + args : (Tensor out_grad, IntArray axis) + output : Tensor(x_grad) + invoke : reverse(out_grad, axis) + - backward_op : roll_grad forward : roll(Tensor x, IntArray shifts, int64_t[] axis) -> Tensor(out) args : (Tensor x, Tensor out_grad, IntArray shifts, int64_t[] axis) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 3cf1904b1ed72..181b819cde96e 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -884,12 +884,6 @@ backward : reshape_double_grad inplace : (out_grad -> x_grad) -- backward_op : reverse_grad - forward : reverse (Tensor x, IntArray axis) -> Tensor(out) - args : (Tensor out_grad, IntArray axis) - output : Tensor(x_grad) - invoke : reverse(out_grad, axis) - - backward_op : rnn_grad forward : rnn (Tensor x, Tensor[] pre_state, Tensor[] weight_list, Tensor sequence_length, Tensor dropout_state_in, float dropout_prob, bool is_bidirec, int input_size, int hidden_size, int num_layers, str mode, int seed, bool is_test) -> Tensor(out), Tensor(dropout_state_out), Tensor[](state), Tensor(reserve) args : (Tensor x, Tensor[] pre_state, Tensor[] weight_list, Tensor sequence_length, Tensor out, Tensor dropout_state_out, Tensor reserve, Tensor out_grad, Tensor[] state_grad, float dropout_prob, bool is_bidirec, int input_size, int hidden_size, int num_layers, str mode, int seed, bool is_test) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index c70d365927f1c..89aef2203ccaf 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -1133,15 +1133,6 @@ intermediate : xshape backward: reshape_grad -- op : reverse - args : (Tensor x, IntArray axis) - output : Tensor - infer_meta : - func : ReverseInferMeta - kernel : - func : reverse - backward : reverse_grad - - op : rmsprop_ args : (Tensor param, Tensor mean_square, Tensor grad, Tensor moment, Tensor learning_rate, Tensor mean_grad, Tensor master_param, float epsilon, float decay, float momentum, bool centered, bool multi_precision) output : Tensor(param_out), Tensor(moment_out), Tensor(mean_square_out), Tensor(mean_grad_out), Tensor(master_param_out) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index baf9c516a6ca8..8e36affbb7596 100644 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1751,6 +1751,17 @@ extra : attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32", bool use_quantizer = false] +- op : reverse + inputs: + x : X + outputs: + out : Out + int_array: + axis : + data_type : int + support_tensor : true + manual_signature : [reverse] + - op : roll backward : roll_grad inputs : diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 91e478c008a90..8de00fc785ca0 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -1436,6 +1436,16 @@ func : renorm backward : renorm_grad +- op : reverse + args : (Tensor x, IntArray axis) + output : Tensor + infer_meta : + func : ReverseInferMeta + kernel : + func : reverse + data_type : x + backward : reverse_grad + - op : roll args : (Tensor x, IntArray shifts={}, int64_t[] axis={}) output : Tensor(out)