unfold_op.cc 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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
/* 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 "paddle/fluid/operators/unfold_op.h"

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 已提交
54
all element will be rearranged as a column. While the convolution filter sliding over the input
55 56 57 58 59 60 61 62 63
feature map, a series of such columns will be formed. 
    )DOC");
  }
};

class UnfoldOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
  void InferShape(framework::InferShapeContext* ctx) const override {
64 65 66 67 68 69
    PADDLE_ENFORCE_EQ(
        ctx->HasInput("X"), true,
        platform::errors::NotFound("Input(X) of UnfoldOp should not be null"));
    PADDLE_ENFORCE_EQ(
        ctx->HasOutput("Y"), true,
        platform::errors::NotFound("Output(Y) of UnfoldOp should not be null"));
70 71 72 73 74 75 76 77 78
    auto in_dims = ctx->GetInputDim("X");
    std::vector<int> kernel_sizes =
        ctx->Attrs().Get<std::vector<int>>("kernel_sizes");
    std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
    std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
    std::vector<int> dilations =
        ctx->Attrs().Get<std::vector<int>>("dilations");

    // Only [N, C, H, W] input supported now
79 80 81 82 83 84 85 86 87 88 89 90
    PADDLE_ENFORCE_EQ(
        in_dims.size(), 4,
        platform::errors::InvalidArgument(
            "Input should be 4-D tensor of format [N, C, H, W], but get %u",
            in_dims.size()));
    PADDLE_ENFORCE_EQ(
        in_dims.size() - kernel_sizes.size(), 2U,
        platform::errors::InvalidArgument(
            "The dims of X should be larger than that of kernel_sizes "
            "by a number of 2, due to the batch size and input channel dim. "
            "But recieved dims(X:%u) - dims(kernel_sizes:%u) != 2",
            in_dims.size(), kernel_sizes.size()));
91 92
    PADDLE_ENFORCE_EQ(
        strides.size(), kernel_sizes.size(),
93 94 95 96
        platform::errors::InvalidArgument(
            "The dims of strides should be the same with that of kernel_sizes. "
            "But recieved dims(strides: %u) != dims(kernel_sizes: %u).",
            strides.size(), kernel_sizes.size()));
97 98
    PADDLE_ENFORCE_EQ(
        paddings.size(), 2 * strides.size(),
99 100 101 102
        platform::errors::InvalidArgument(
            "The dims of paddings should be 2 times of that of strides. "
            "But recieved dims(paddings: %u) != 2*dims(strides: %u).",
            paddings.size(), strides.size()));
103 104
    PADDLE_ENFORCE_EQ(
        strides.size(), dilations.size(),
105 106 107 108
        platform::errors::InvalidArgument(
            "The dims of strides should be the same with that of dilations. "
            "But recieved dims(strides: %u) != dims(dilations: %u).",
            strides.size(), dilations.size()));
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    // check kernel_sizes
    PADDLE_ENFORCE_GT(kernel_sizes[0], 0,
                      platform::errors::InvalidArgument(
                          "The `kernel_sizes` should be greater than zero, "
                          "but recieved kernel_height: %d kernel_width: %d.",
                          kernel_sizes[0], kernel_sizes[1]));
    PADDLE_ENFORCE_GT(kernel_sizes[1], 0,
                      platform::errors::InvalidArgument(
                          "The `kernel_sizes` should be greater than zero, "
                          "but recieved kernel_height: %d kernel_width: %d.",
                          kernel_sizes[0], kernel_sizes[1]));
    // check strides
    PADDLE_ENFORCE_GT(strides[0], 0,
                      platform::errors::InvalidArgument(
                          "The `strides` should be greater than zero, "
                          "but recieved strides_height: %d strides_width: %d.",
                          strides[0], strides[1]));
    PADDLE_ENFORCE_GT(strides[1], 0,
                      platform::errors::InvalidArgument(
                          "The `strides` should be greater than zero, "
                          "but recieved strides_height: %d strides_width: %d.",
                          strides[0], strides[1]));
    // check dilations
    PADDLE_ENFORCE_GT(
        dilations[0], 0,
        platform::errors::InvalidArgument(
            "The `dilations` should be greater than zero, "
            "but recieved dilations_height: %d dilations_width: %d.",
            dilations[0], dilations[1]));
    PADDLE_ENFORCE_GT(
        dilations[1], 0,
        platform::errors::InvalidArgument(
            "The `dilations` should be greater than zero, "
            "but recieved dilations_height: %d dilations_width: %d.",
            dilations[0], dilations[1]));

146 147 148 149 150
    bool contain_unknown_dim = framework::contain_unknown_dim(in_dims);
    bool check = ctx->IsRuntime() || !contain_unknown_dim;
    if (check) {
      std::vector<int> out_dims;
      out_dims.push_back(in_dims[0]);
151

152 153
      int output_channels = in_dims[1] * kernel_sizes[0] * kernel_sizes[1];
      out_dims.push_back(output_channels);
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      int output_height =
          CalcOutputSize(in_dims[2], kernel_sizes[0], dilations[0], paddings[0],
                         paddings[2], strides[0]);
      int output_width =
          CalcOutputSize(in_dims[3], kernel_sizes[1], dilations[1], paddings[1],
                         paddings[3], strides[1]);
      // check output height and width
      PADDLE_ENFORCE_GT(
          output_height, 0,
          platform::errors::InvalidArgument(
              "The sliding blocks calculated from input spatial size "
              "(%d, %d), kernel_sizes (%d, %d), strides (%d, %d), "
              "dilations (%d, %d), is (%d, %d), which should be a "
              "positive integer.",
              in_dims[2], in_dims[3], kernel_sizes[0], kernel_sizes[1],
              strides[0], strides[1], dilations[0], dilations[1], output_height,
              output_width));
      PADDLE_ENFORCE_GT(
          output_width, 0,
          platform::errors::InvalidArgument(
              "The sliding blocks calculated from input spatial size "
              "(%d, %d), kernel_sizes (%d, %d), strides (%d, %d), "
              "dilations (%d, %d), is (%d, %d), which should be a "
              "positive integer.",
              in_dims[2], in_dims[3], kernel_sizes[0], kernel_sizes[1],
              strides[0], strides[1], dilations[0], dilations[1], output_height,
              output_width));
      int output_col_length = output_height * output_width;
      out_dims.push_back(output_col_length);
184

185 186
      ctx->SetOutputDim("Y", framework::make_ddim(out_dims));
    }
187 188 189 190 191
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
192 193 194
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
195 196 197 198 199 200 201 202
  }
};

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

  void InferShape(framework::InferShapeContext* ctx) const override {
203 204 205 206 207 208 209 210 211
    PADDLE_ENFORCE_EQ(
        ctx->HasInput(framework::GradVarName("Y")), true,
        platform::errors::NotFound("The gradient of Y should not be null"));
    PADDLE_ENFORCE_EQ(
        ctx->HasInput("X"), true,
        platform::errors::NotFound("The input X should not be null"));
    PADDLE_ENFORCE_EQ(
        ctx->HasOutput(framework::GradVarName("X")), true,
        platform::errors::NotFound("The gradient of X should not be null"));
212 213 214 215 216 217
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
218 219 220
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Y")),
                                   ctx.device_context());
221 222 223
  }
};

H
hong 已提交
224 225
template <typename T>
class UnfoldGradMaker : public framework::SingleGradOpMaker<T> {
226
 public:
H
hong 已提交
227
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
228 229

 protected:
230
  void Apply(GradOpPtr<T> op) const override {
231
    op->SetType("unfold_grad");
H
hong 已提交
232 233 234 235
    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());
236 237 238
  }
};

239
DECLARE_NO_NEED_BUFFER_VARS_INFERER(UnfoldGradOpNoNeedBufferVarsInferer, "X");
240 241 242 243 244 245

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(unfold, ops::UnfoldOp, ops::UnfoldOpMaker,
H
hong 已提交
246 247
                  ops::UnfoldGradMaker<paddle::framework::OpDesc>,
                  ops::UnfoldGradMaker<paddle::imperative::OpBase>);
248
REGISTER_OPERATOR(unfold_grad, ops::UnfoldGradOp,
249
                  ops::UnfoldGradOpNoNeedBufferVarsInferer);
250 251 252 253 254 255 256 257

REGISTER_OP_CPU_KERNEL(
    unfold, ops::UnfoldOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::UnfoldOpKernel<paddle::platform::CPUDeviceContext, double>);
REGISTER_OP_CPU_KERNEL(
    unfold_grad,
    ops::UnfoldGradOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::UnfoldGradOpKernel<paddle::platform::CPUDeviceContext, double>);