diff --git a/paddle/fluid/operators/deformable_conv_v1_op.cc b/paddle/fluid/operators/deformable_conv_v1_op.cc deleted file mode 100644 index a597c1e00396d..0000000000000 --- a/paddle/fluid/operators/deformable_conv_v1_op.cc +++ /dev/null @@ -1,194 +0,0 @@ -// Copyright (c) 2019 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 "paddle/fluid/framework/infershape_utils.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/phi/core/infermeta_utils.h" -#include "paddle/phi/infermeta/multiary.h" - -namespace paddle { -namespace operators { -class DeformableConvV1OpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("Input", - "(Tensor) The input of deformable conv op. " - "The shape of input is " - "[N, channel_in, H, W]"); - AddInput("Offset", - "(Tensor) The input offset. " - "The shape of the offset is " - "[N, deformable_groups * kernel_w * kernel_h * 2, H, W"); - AddInput("Filter", - "(Tensor) The Input Filter " - "The shape of the wight is " - "[num_filters, channel_in, kernel_h, kernel_w."); - AddOutput("Output", - "(Tensor) The output. " - "The shape of the output tensor is " - "[N, num_filters, out_height, out_width]]."); - AddAttr>("strides", - "(vector default:{1, 1}), the " - "strides(h_stride, w_stride) of " - "convolution operator.") - .SetDefault({1, 1}); - AddAttr>("paddings", - "(vector default:{0,0}), the " - "paddings(h_pad, w_pad) of " - "convolution operator. ") - .SetDefault({0, 0}); - AddAttr>("dilations", - "(vector default:{1, 1}), the " - "dilations(h_dilation, w_dilation) of " - "convolution operator.") - .SetDefault({1, 1}); - AddAttr( - "groups", - "(int default:1), the groups number of the convolution operator. " - "According to grouped convolution in Alex Krizhevsky's Deep CNN paper: " - "when group=2, the first half of the filters is only connected to the " - "first half of the input channels, while the second half of the " - "filters " - "is only connected to the second half of the input channels.") - .SetDefault(1); - AddAttr("deformable_groups", - "(int default:1), the number of the deformable groups.") - .SetDefault(1); - AddAttr("im2col_step", - "im2col maximum number of image per computation") - .SetDefault(64); - AddComment(R"DOC( -**Deformable Convolution v1 Operator** - -Deformable Convolution is a new method based Convolution which feature has offset -in spatial location. - -1. Get offset of each pixel in feature map with convolution layers which number - of channels should be double of weight size. - -2. Add offset to pixel to get new location and the new value which are computed - directly through bilinear interpolation with four nearest pixel. - -3. Get the product of pixel and weight as result - -Compute 2-D deformable convolution on 4-D input. - -Given input image x, output feature map y, the deformable convolution operation can be expressed as follow: - -$$ -y(p) = \\sum_{k=1}^{K}{w_k * x(p + p_k + \\Delta p_k)} -$$ - -Where $$\\Delta p_k$$ is the learnable offset for the k-th location, respectively. - -Refer to 'https://arxiv.org/abs/1703.06211 ' - -Example: - Input: - Input shape: $(N, C_{in}, H_{in}, W_{in})$ - Filter shape: $(C_{out}, C_{in}, H_f, W_f)$ - Offset shape: $(N, 2 * deformable_groups, * H_f * W_f, H_{out}, W_{out})$ - Output: - Output shape: $(N, C_{out}, H_{out}, W_{out})$ - where $H_{out}, W_{out}$ must be equal to $H_{in}, W_{in}$ respectively. - Where -$$ - H_{out}= \frac{(H_{in} + 2 * paddings[0] - (dilations[0] * (H_f - 1) + 1))}{strides[0]}+ 1 \\ - W_{out}= \frac{(W_{in} + 2 * paddings[1] - (dilations[1] * (W_f - 1) + 1))}{strides[1]}+ 1 -$$ -)DOC"); - } -}; - -class DeformableConvV1Op : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "Input"), - ctx.device_context().GetPlace()); - } -}; - -template -class DeformableConvV1GradOpMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - protected: - void Apply(GradOpPtr op) const override { - op->SetType("deformable_conv_v1_grad"); - op->SetInput("Input", this->Input("Input")); - op->SetInput("Filter", this->Input("Filter")); - op->SetInput("Offset", this->Input("Offset")); - op->SetInput(framework::GradVarName("Output"), this->OutputGrad("Output")); - - op->SetOutput(framework::GradVarName("Input"), this->InputGrad("Input")); - op->SetOutput(framework::GradVarName("Filter"), this->InputGrad("Filter")); - op->SetOutput(framework::GradVarName("Offset"), this->InputGrad("Offset")); - - op->SetAttrMap(this->Attrs()); - } -}; - -class DeformableConvV1GradOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext *ctx) const override { - auto in_dims = ctx->GetInputDim("Input"); - auto filter_dims = ctx->GetInputDim("Filter"); - auto offset_dims = ctx->GetInputDim("Offset"); - - OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Output")), - "Input", - "Output@Grad", - "deformable_conv_v1_grad"); - if (ctx->HasOutput(framework::GradVarName("Input"))) { - ctx->SetOutputDim(framework::GradVarName("Input"), in_dims); - } - if (ctx->HasOutput(framework::GradVarName("Filter"))) { - ctx->SetOutputDim(framework::GradVarName("Filter"), filter_dims); - } - if (ctx->HasOutput(framework::GradVarName("Offset"))) { - ctx->SetOutputDim(framework::GradVarName("Offset"), offset_dims); - } - } - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "Input"), - ctx.device_context().GetPlace()); - } -}; -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -DECLARE_INFER_SHAPE_FUNCTOR(deformable_conv, - DeformableConvV1InferShapeFunctor, - PD_INFER_META(phi::DeformableConvInferMeta)); - -REGISTER_OPERATOR(deformable_conv_v1, - ops::DeformableConvV1Op, - ops::DeformableConvV1OpMaker, - ops::DeformableConvV1GradOpMaker, - ops::DeformableConvV1GradOpMaker, - DeformableConvV1InferShapeFunctor); -REGISTER_OPERATOR(deformable_conv_v1_grad, ops::DeformableConvV1GradOp); diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index 0e348847554c2..6b332aefe8db4 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -389,6 +389,16 @@ kernel : func : cumprod_grad +- backward_op : deformable_conv_grad + forward : deformable_conv (Tensor x, Tensor offset, Tensor filter, Tensor mask, int[] strides={1, 1}, int[] paddings={0, 0}, int[] dilations={1, 1}, int deformable_groups=1, int groups=1, int im2col_step=64) -> Tensor(out) + args : (Tensor x, Tensor offset, Tensor filter,Tensor mask,Tensor out_grad, int[] strides, int[] paddings, int[] dilations, int deformable_groups, int groups, int im2col_step) + output : Tensor(x_grad), Tensor(offset_grad), Tensor(filter_grad) + infer_meta : + func : DeformableConvGradInferMeta + kernel : + func : deformable_conv_grad + data_type : x + - backward_op : det_grad forward : det (Tensor x) -> Tensor(out) args : (Tensor x, Tensor out, Tensor out_grad) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index 8a3f0c15b677f..6c8dea33f8f9a 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -231,17 +231,6 @@ data_type: x composite: cumsum_grad(x, out_grad, axis, flatten, exclusive, reverse, x_grad) -- backward_op : deformable_conv_grad - forward : deformable_conv(Tensor x, Tensor offset, Tensor filter, Tensor mask, int[] strides, int[] paddings, int[] dilations, int deformable_groups, int groups, int im2col_step) -> Tensor(out) - args : (Tensor x, Tensor offset, Tensor filter, Tensor mask, Tensor out_grad, int[] strides, int[] paddings, int[] dilations, int deformable_groups, int groups, int im2col_step) - output : Tensor(x_grad), Tensor(offset_grad), Tensor(filter_grad), Tensor(mask_grad) - infer_meta : - func : DeformableConvGradInferMeta - kernel : - func : deformable_conv_grad - data_type : x - optional : mask - - backward_op : depthwise_conv2d_double_grad forward : depthwise_conv2d_grad (Tensor input, Tensor filter, Tensor grad_out, int[] strides, int[] paddings, str padding_algorithm, int groups, int[] dilations, str data_format) -> Tensor(grad_input), Tensor(grad_filter) args : (Tensor input, Tensor filter, Tensor grad_out, Tensor grad_input_grad, Tensor grad_filter_grad, int[] strides, int[] paddings, str padding_algorithm, int groups, int[] dilations, str data_format) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index e941b54fe5030..839c9b0bb2faa 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -251,17 +251,6 @@ param : [x, mode] backend : place -- op : deformable_conv - args : (Tensor x, Tensor offset, Tensor filter, Tensor mask, int[] strides, int[] paddings, int[] dilations, int deformable_groups, int groups, int im2col_step) - output : Tensor(out) - infer_meta : - func : DeformableConvInferMeta - kernel : - func : deformable_conv - data_type : x - optional : mask - backward : deformable_conv_grad - - op : depthwise_conv2d args : (Tensor x, Tensor filter, int[] strides, int[] paddings, str padding_algorithm, int groups, int[] dilations, 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 23e509e5dd1a4..373f7bb845515 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -569,8 +569,8 @@ outputs : out : Out -- op : deformable_conv - backward : deformable_conv_grad +- op : deformable_conv (deformable_conv_v1) + backward : deformable_conv_grad (deformable_conv_v1_grad) inputs : {x : Input, offset : Offset, filter : Filter, mask : Mask} outputs : diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index b2c262861ba16..87af3490e9c7a 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -494,6 +494,16 @@ func : cumprod backward : cumprod_grad +- op : deformable_conv + args : (Tensor x, Tensor offset, Tensor filter, Tensor mask, int[] strides={1, 1}, int[] paddings={0, 0}, int[] dilations={1, 1}, int deformable_groups=1, int groups=1, int im2col_step=64) + output : Tensor(out) + infer_meta : + func : DeformableConvInferMeta + kernel : + func : deformable_conv + data_type : x + backward : deformable_conv_grad + - op : det args : (Tensor x) output : Tensor diff --git a/paddle/phi/ops/compat/deformable_conv_sig.cc b/paddle/phi/ops/compat/deformable_conv_sig.cc deleted file mode 100644 index f7c5f4d6a34fe..0000000000000 --- a/paddle/phi/ops/compat/deformable_conv_sig.cc +++ /dev/null @@ -1,54 +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 DeformableConvOpV1ArgumentMapping( - const ArgumentMappingContext& ctx) { - return KernelSignature("deformable_conv", - {"Input", "Offset", "Filter", "Mask"}, - {"strides", - "paddings", - "dilations", - "deformable_groups", - "groups", - "im2col_step"}, - {"Output"}); -} - -KernelSignature DeformableConvGradOpV1ArgumentMapping( - const ArgumentMappingContext& ctx) { - return KernelSignature( - "deformable_conv_grad", - {"Input", "Offset", "Filter", "Mask", "Output@GRAD"}, - {"strides", - "paddings", - "dilations", - "deformable_groups", - "groups", - "im2col_step"}, - {"Input@GRAD", "Offset@GRAD", "Filter@GRAD", "Mask@GRAD"}); -} - -} // namespace phi - -PD_REGISTER_BASE_KERNEL_NAME(deformable_conv_v1, deformable_conv); -PD_REGISTER_BASE_KERNEL_NAME(deformable_conv_v1_grad, deformable_conv_grad); - -PD_REGISTER_ARG_MAPPING_FN(deformable_conv_v1, - phi::DeformableConvOpV1ArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(deformable_conv_v1_grad, - phi::DeformableConvGradOpV1ArgumentMapping);