From 005fee12c7009d5aa2de75efc224b843ffe18f53 Mon Sep 17 00:00:00 2001 From: Sanbu <96160062+sanbuphy@users.noreply.github.com> Date: Fri, 28 Apr 2023 15:40:29 +0800 Subject: [PATCH] Support static graph code-gen for unpool (#52947) --- paddle/fluid/operators/unpool_op.cc | 85 ------------------------ paddle/phi/api/yaml/backward.yaml | 11 +++ paddle/phi/api/yaml/legacy_backward.yaml | 11 --- paddle/phi/api/yaml/legacy_ops.yaml | 10 --- paddle/phi/api/yaml/op_compat.yaml | 10 +++ paddle/phi/api/yaml/ops.yaml | 10 +++ paddle/phi/ops/compat/unpool_sig.cc | 36 ---------- 7 files changed, 31 insertions(+), 142 deletions(-) delete mode 100644 paddle/phi/ops/compat/unpool_sig.cc diff --git a/paddle/fluid/operators/unpool_op.cc b/paddle/fluid/operators/unpool_op.cc index 6eb6b81eb4d..62441dd6127 100644 --- a/paddle/fluid/operators/unpool_op.cc +++ b/paddle/fluid/operators/unpool_op.cc @@ -24,64 +24,6 @@ limitations under the License. */ namespace paddle { namespace operators { -class Unpool2dOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput( - "X", - "(Tensor) The input tensor of unpool operator. " - "The format of input tensor is NCHW. Where N is batch size, C is the " - "number of channels, H and W is the height and width of feature."); - AddInput( - "Indices", - "(Tensor) The input tensor of the indices given out by MaxPool2d. " - "The format of input tensor is NCHW. Where N is batch size, C is the " - "number of channels, H and W is the height and width of feature."); - AddOutput("Out", - "(Tensor) The output tensor of unpool operator." - "The format of output tensor is also NCHW." - "Where N is batch size, C is " - "the number of channels, H and W is the height and " - "width of feature."); - AddAttr>( - "ksize", - "(vector), the unpooling window size(height, width) " - "of unpooling operator."); - AddAttr>("strides", - "(vector, default:{1, 1}), " - "strides (height, width) of unpooling operator.") - .SetDefault({1, 1}); - AddAttr>("paddings", - "(vector default:{0,0}), " - "paddings (height, width) of unpooling operator.") - .SetDefault({0, 0}); - AddAttr( - "unpooling_type", - "(string), unpooling type, can be \"max\" for max-unpooling ") - .InEnum({"max"}); - AddAttr>("output_size", - "(vector, optional). The shape of output.") - .SetDefault({0, 0}) - .SupportTensor(); - 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("NCHW"); - AddComment(R"DOC( -Input shape is: $(N, C_{in}, H_{in}, W_{in})$, Output shape is: -$(N, C_{out}, H_{out}, W_{out})$, where -$$ -H_{out} = (H_{in}-1) * strides[0] - 2 * paddings[0] + ksize[0] \\ -W_{out} = (W_{in}-1) * strides[1] - 2 * paddings[1] + ksize[1] -$$ -Paper: http://www.matthewzeiler.com/wp-content/uploads/2017/07/iccv2011.pdf -)DOC"); - } -}; - class Unpool3dOpMaker : public framework::OpProtoAndCheckerMaker { public: void Make() override { @@ -200,18 +142,6 @@ class Unpool3dOpGradMaker : public framework::SingleGradOpMaker { } }; -class UnpoolOpGrad : public framework::OperatorWithKernel { - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "X"), - ctx.GetPlace()); - } - - public: - using framework::OperatorWithKernel::OperatorWithKernel; -}; - class Unpool3dOpGrad : public framework::OperatorWithKernel { protected: phi::KernelKey GetExpectedKernelType( @@ -228,21 +158,6 @@ class Unpool3dOpGrad : public framework::OperatorWithKernel { } // namespace paddle namespace ops = paddle::operators; -DECLARE_INFER_SHAPE_FUNCTOR(unpool, - UnpoolInferShapeFunctor, - PD_INFER_META(phi::UnpoolInferMeta)); -REGISTER_OPERATOR(unpool, - ops::UnpoolOp, - ops::Unpool2dOpMaker, - ops::UnpoolOpGradMaker, - ops::UnpoolOpGradMaker, - UnpoolInferShapeFunctor); - -DECLARE_INFER_SHAPE_FUNCTOR(unpool_grad, - UnpoolGradInferShapeFunctor, - PD_INFER_META(phi::UnchangedInferMeta)); - -REGISTER_OPERATOR(unpool_grad, ops::UnpoolOpGrad, UnpoolGradInferShapeFunctor); DECLARE_INFER_SHAPE_FUNCTOR(unpool, Unpool3dInferShapeFunctor, diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index 97a0968f171..0e348847554 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -2064,3 +2064,14 @@ kernel : func : where_grad no_need_buffer : x, y + +- backward_op: unpool_grad + forward: unpool (Tensor x, Tensor indices, int[] ksize, int[] strides = {1,1}, int[] paddings ={0,0} ,IntArray output_size = {0,0}, str data_format="NCHW") -> Tensor(out) + args: (Tensor x, Tensor indices, Tensor out, Tensor out_grad, int[] ksize, int[] strides, int[] paddings, IntArray output_size, str data_format) + output: Tensor(x_grad) + infer_meta: + func: UnchangedInferMeta + param : [x] + kernel: + func: unpool_grad + data_type: x diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 5f971d98853..8a3f0c15b67 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -1053,14 +1053,3 @@ kernel: func: unpool3d_grad data_type: x - -- backward_op: unpool_grad - forward: unpool (Tensor x, Tensor indices, int[] ksize, int[] strides, int[] padding, IntArray output_size, str data_format) -> Tensor(out) - args: (Tensor x, Tensor indices, Tensor out, Tensor out_grad, int[] ksize, int[] strides, int[] padding, IntArray output_size, str data_format) - output: Tensor(x_grad) - infer_meta: - func: UnchangedInferMeta - param : [x] - kernel: - func: unpool_grad - data_type: x diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index c3610b4ae5a..128b078a786 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -1217,16 +1217,6 @@ func : unique data_type : x -- op : unpool - args: (Tensor x, Tensor indices, int[] ksize, int[] strides, int[] padding, IntArray output_size, str data_format) - output: Tensor(out) - infer_meta: - func: UnpoolInferMeta - kernel: - func: unpool - data_type: x - backward: unpool_grad - - op : unpool3d args: (Tensor x, Tensor indices, int[] ksize, int[] strides, int[] padding, int[] output_size, str data_format) output: Tensor(out) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index c3d326eafbf..564a7de4e94 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -2411,6 +2411,16 @@ outputs : {out : Out, index : Index, counts : Counts} +- op : unpool + inputs : + {x : X, indices: Indices} + outputs : + out : Out + int_array : + output_size: + data_type : int + support_tensor : true + - op : unsqueeze (unsqueeze2) backward : unsqueeze_grad (unsqueeze2_grad), unsqueeze_double_grad(unsqueeze2_double_grad) inputs : diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index bea41007e5a..72bf1d67a28 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2133,6 +2133,16 @@ data_type : x optional : index, counts +- op : unpool + args: (Tensor x, Tensor indices, int[] ksize, int[] strides = {1,1}, int[] paddings ={0,0} ,IntArray output_size = {0,0}, str data_format="NCHW") + output: Tensor(out) + infer_meta: + func: UnpoolInferMeta + kernel: + func: unpool + data_type: x + backward: unpool_grad + - op : unsqueeze args : (Tensor x, IntArray axis = {}) output : Tensor(out), Tensor(xshape) diff --git a/paddle/phi/ops/compat/unpool_sig.cc b/paddle/phi/ops/compat/unpool_sig.cc deleted file mode 100644 index fb751b4b4b6..00000000000 --- a/paddle/phi/ops/compat/unpool_sig.cc +++ /dev/null @@ -1,36 +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 UnpoolOpArgumentMapping(const ArgumentMappingContext& ctx) { - return KernelSignature( - "unpool", - {"X", "Indices"}, - {"ksize", "strides", "paddings", "output_size", "data_format"}, - {"Out"}); -} - -KernelSignature UnpoolGradOpArgumentMapping(const ArgumentMappingContext& ctx) { - return KernelSignature( - "unpool_grad", - {"X", "Indices", "Out", "Out@GRAD"}, - {"ksize", "strides", "paddings", "output_size", "data_format"}, - {"X@GRAD"}); -} - -} // namespace phi -PD_REGISTER_ARG_MAPPING_FN(unpool, phi::UnpoolOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(unpool_grad, phi::UnpoolGradOpArgumentMapping); -- GitLab