unfold_op.cc 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15 16 17
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/infermeta/unary.h"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

namespace paddle {
namespace operators {

class UnfoldOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X",
             "Tensor, "
             "the input of unfold op. "
             "The format of X is [N, C_in, H, W], "
             "where N is the batch size, C_in is the input channels, "
             "H is the height and W is the width");
    AddOutput(
        "Y",
        "Tensor, "
        "the output of unfold op. "
        "The format of Y is [N, C_in*filter_height*filter_width, "
        "output_height*output_width], where N is the batch size, "
        "C_in is the input channels of X, filter_height and filter_width is "
        "height and width of the filtering kernel, output_height and "
        "output_width "
        "is the calculated height and width of output feature map.");
    AddAttr<std::vector<int>>(
        "kernel_sizes",
        "vector<int>, the kernel sizes of the convolution operator.");
    AddAttr<std::vector<int>>(
        "strides", "vector<int>, the strides of the convolution operator.");
    AddAttr<std::vector<int>>(
        "paddings",
        "vector<int>, the paddings applied to pad the feature map.");
    AddAttr<std::vector<int>>(
        "dilations", "vector<int>, the dilations of the convolution operator.");
    AddComment(R"DOC(
**Unfold Operator**

This Operator is used to extract sliding local blocks from a batched input tensor, also known
as im2col when operated on batched 2D image tensor. For each block under the convolution filter,
T
tianshuo78520a 已提交
56
all element will be rearranged as a column. While the convolution filter sliding over the input
57
feature map, a series of such columns will be formed.
58 59 60 61 62 63 64 65 66 67 68
    )DOC");
  }
};

class UnfoldOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
69 70 71
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
72 73 74 75 76 77 78 79
  }
};

class UnfoldGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
80
    PADDLE_ENFORCE_EQ(
81 82
        ctx->HasInput(framework::GradVarName("Y")),
        true,
83 84
        platform::errors::NotFound("The gradient of Y should not be null"));
    PADDLE_ENFORCE_EQ(
85 86
        ctx->HasInput("X"),
        true,
87 88
        platform::errors::NotFound("The input X should not be null"));
    PADDLE_ENFORCE_EQ(
89 90
        ctx->HasOutput(framework::GradVarName("X")),
        true,
91
        platform::errors::NotFound("The gradient of X should not be null"));
92 93 94 95 96 97
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
98 99 100
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Y")),
                                   ctx.device_context());
101 102 103
  }
};

H
hong 已提交
104 105
template <typename T>
class UnfoldGradMaker : public framework::SingleGradOpMaker<T> {
106
 public:
H
hong 已提交
107
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
108 109

 protected:
110
  void Apply(GradOpPtr<T> op) const override {
111
    op->SetType("unfold_grad");
H
hong 已提交
112 113 114 115
    op->SetInput(framework::GradVarName("Y"), this->OutputGrad("Y"));
    op->SetInput("X", this->Input("X"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetAttrMap(this->Attrs());
116 117 118
  }
};

119
DECLARE_NO_NEED_BUFFER_VARS_INFERER(UnfoldGradOpNoNeedBufferVarsInferer, "X");
120 121 122 123 124

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
125 126
DECLARE_INFER_SHAPE_FUNCTOR(unfold,
                            UnfoldInferShapeFunctor,
127
                            PD_INFER_META(phi::UnfoldInferMeta));
128 129 130
REGISTER_OPERATOR(unfold,
                  ops::UnfoldOp,
                  ops::UnfoldOpMaker,
H
hong 已提交
131
                  ops::UnfoldGradMaker<paddle::framework::OpDesc>,
132 133
                  ops::UnfoldGradMaker<paddle::imperative::OpBase>,
                  UnfoldInferShapeFunctor);
134 135
REGISTER_OPERATOR(unfold_grad,
                  ops::UnfoldGradOp,
136
                  ops::UnfoldGradOpNoNeedBufferVarsInferer);