From dd90f10252fa47aabebf4ddcf6975b8f37bd5687 Mon Sep 17 00:00:00 2001 From: Sanbu <96160062+sanbuphy@users.noreply.github.com> Date: Tue, 9 May 2023 16:23:45 +0800 Subject: [PATCH] Support static graph code-gen for unpool3d (#53479) --- paddle/fluid/operators/unpool_op.cc | 179 ----------------------- 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 | 6 + paddle/phi/api/yaml/ops.yaml | 10 ++ paddle/phi/ops/compat/unpool3d_sig.cc | 37 ----- 7 files changed, 27 insertions(+), 237 deletions(-) delete mode 100644 paddle/fluid/operators/unpool_op.cc delete mode 100644 paddle/phi/ops/compat/unpool3d_sig.cc diff --git a/paddle/fluid/operators/unpool_op.cc b/paddle/fluid/operators/unpool_op.cc deleted file mode 100644 index 62441dd6127e3..0000000000000 --- a/paddle/fluid/operators/unpool_op.cc +++ /dev/null @@ -1,179 +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. -Indicesou 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 - -#include "paddle/fluid/framework/infershape_utils.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/phi/infermeta/backward.h" -#include "paddle/phi/infermeta/binary.h" - -namespace paddle { -namespace operators { - -class Unpool3dOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput( - "X", - "(Tensor) The input tensor of unpool operator. " - "The format of input tensor is NCDHW. Where N is batch size, C is the " - "number of channels, D, H and W is the depth, height and width of " - "feature."); - AddInput( - "Indices", - "(Tensor) The input tensor of the indices given out by MaxPool3d. " - "The format of input tensor is NCDHW. Where N is batch size, C is the " - "number of channels, D, H and W is the depth, height and width of " - "feature."); - AddOutput("Out", - "(Tensor) The output tensor of unpool operator." - "The format of output tensor is also NCDHW." - "Where N is batch size, C is " - "the number of channels, D, H and W is the depth, height and " - "width of feature."); - AddAttr>( - "ksize", - "(vector), the unpooling window size(depth, height, width) " - "of unpooling operator."); - AddAttr>( - "strides", - "(vector, default:{1, 1, 1}), " - "strides (depth, height, width) of unpooling operator.") - .SetDefault({1, 1, 1}); - AddAttr>( - "paddings", - "(vector default:{0, 0,0}), " - "paddings (depth, height, width) of unpooling operator.") - .SetDefault({0, 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, 0}); - AddAttr( - "data_format", - "(string, default NCDHW)" - "Defaults to \"NCDHW\". Specify the data format of the output data, ") - .SetDefault("NCDHW"); - AddComment(R"DOC( -Input shape is: $(N, C_{in}, D_{in}, H_{in}, W_{in})$, Output shape is: -$(N, C_{out}, D_{out}, H_{out}, W_{out})$, where -$$ -D_{out} = (D_{in}-1) * strides[0] - 2 * paddings[0] + ksize[0] \\ -H_{out} = (H_{in}-1) * strides[1] - 2 * paddings[1] + ksize[1] \\ -W_{out} = (W_{in}-1) * strides[2] - 2 * paddings[2] + ksize[2] -$$ -)DOC"); - } -}; - -int UnpoolOutputSize(int input_size, int ksize, int padding, int stride) { - int output_size = (input_size - 1) * stride - 2 * padding + ksize; - return output_size; -} - -class UnpoolOp : 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 Unpool3dOp : 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; -}; - -template -class UnpoolOpGradMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - void Apply(GradOpPtr op) const override { - op->SetType(this->ForwardOpType() + "_grad"); - op->SetInput("X", this->Input("X")); - op->SetInput("Indices", this->Input("Indices")); - op->SetInput("Out", this->Output("Out")); - op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); - op->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); - op->SetAttrMap(this->Attrs()); - } -}; - -template -class Unpool3dOpGradMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - void Apply(GradOpPtr op) const override { - op->SetType(this->ForwardOpType() + "_grad"); - op->SetInput("X", this->Input("X")); - op->SetInput("Indices", this->Input("Indices")); - op->SetInput("Out", this->Output("Out")); - op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); - op->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); - op->SetAttrMap(this->Attrs()); - } -}; - -class Unpool3dOpGrad : 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; -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -DECLARE_INFER_SHAPE_FUNCTOR(unpool, - Unpool3dInferShapeFunctor, - PD_INFER_META(phi::Unpool3dInferMeta)); - -REGISTER_OPERATOR(unpool3d, - ops::Unpool3dOp, - ops::Unpool3dOpMaker, - ops::Unpool3dOpGradMaker, - ops::Unpool3dOpGradMaker, - Unpool3dInferShapeFunctor); - -DECLARE_INFER_SHAPE_FUNCTOR(unpool3d_grad, - Unpool3dGradInferShapeFunctor, - PD_INFER_META(phi::UnchangedInferMeta)); - -REGISTER_OPERATOR(unpool3d_grad, - ops::Unpool3dOpGrad, - Unpool3dGradInferShapeFunctor); diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index 0e348847554c2..7b3b0d225af10 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -2065,6 +2065,17 @@ func : where_grad no_need_buffer : x, y +- backward_op: unpool3d_grad + forward: unpool3d (Tensor x, Tensor indices, int[] ksize, int[] strides={1,1,1}, int[] paddings={0,0,0}, int[] output_size={0,0,0}, str data_format="NCDHW") -> Tensor(out) + args: (Tensor x, Tensor indices, Tensor out, Tensor out_grad, int[] ksize, int[] strides, int[] paddings, int[] output_size, str data_format) + output: Tensor(x_grad) + infer_meta: + func: UnchangedInferMeta + param : [x] + kernel: + func: unpool3d_grad + data_type: x + - 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) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 8a3f0c15b677f..b844f76290c0e 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -1042,14 +1042,3 @@ kernel : func : yolo_loss_grad optional : gt_score - -- backward_op: unpool3d_grad - forward: unpool3d (Tensor x, Tensor indices, int[] ksize, int[] strides, int[] padding, int[] output_size, str data_format) -> Tensor(out) - args: (Tensor x, Tensor indices, Tensor out, Tensor out_grad, int[] ksize, int[] strides, int[] padding, int[] output_size, str data_format) - output: Tensor(x_grad) - infer_meta: - func: UnchangedInferMeta - param : [x] - kernel: - func: unpool3d_grad - data_type: x diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index e941b54fe5030..266e981254f36 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -1207,16 +1207,6 @@ func : unique data_type : x -- op : unpool3d - args: (Tensor x, Tensor indices, int[] ksize, int[] strides, int[] padding, int[] output_size, str data_format) - output: Tensor(out) - infer_meta: - func: Unpool3dInferMeta - kernel: - func: unpool3d - data_type: x - backward: unpool3d_grad - - op : yolo_loss args : (Tensor x, Tensor gt_box, Tensor gt_label, Tensor gt_score, int[] anchors, int[] anchor_mask, int class_num, float ignore_thresh, int downsample_ratio, bool use_label_smooth=true, float scale_x_y=1.0) output : Tensor(loss), Tensor(objectness_mask), Tensor(gt_match_mask) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 23e509e5dd1a4..97cca3ced8078 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -2431,6 +2431,12 @@ data_type : int support_tensor : true +- op : unpool3d + inputs : + {x : X, indices: Indices} + outputs : + out : Out + - 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 b2c262861ba16..2f0960e9d30c5 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -2153,6 +2153,16 @@ data_type: x backward: unpool_grad +- op : unpool3d + args: (Tensor x, Tensor indices, int[] ksize, int[] strides={1,1,1}, int[] paddings={0,0,0}, int[] output_size={0,0,0}, str data_format="NCDHW") + output: Tensor(out) + infer_meta: + func: Unpool3dInferMeta + kernel: + func: unpool3d + data_type: x + backward: unpool3d_grad + - op : unsqueeze args : (Tensor x, IntArray axis = {}) output : Tensor(out), Tensor(xshape) diff --git a/paddle/phi/ops/compat/unpool3d_sig.cc b/paddle/phi/ops/compat/unpool3d_sig.cc deleted file mode 100644 index c73aca837d57b..0000000000000 --- a/paddle/phi/ops/compat/unpool3d_sig.cc +++ /dev/null @@ -1,37 +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 Unpool3dOpArgumentMapping(const ArgumentMappingContext& ctx) { - return KernelSignature( - "unpool3d", - {"X", "Indices"}, - {"ksize", "strides", "paddings", "output_size", "data_format"}, - {"Out"}); -} - -KernelSignature Unpool3dGradOpArgumentMapping( - const ArgumentMappingContext& ctx) { - return KernelSignature( - "unpool3d_grad", - {"X", "Indices", "Out", "Out@GRAD"}, - {"ksize", "strides", "paddings", "output_size", "data_format"}, - {"X@GRAD"}); -} - -} // namespace phi -PD_REGISTER_ARG_MAPPING_FN(unpool3d, phi::Unpool3dOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(unpool3d_grad, phi::Unpool3dGradOpArgumentMapping);