pool_op.cc 27.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/pool_op.h"
16

17
#include <unordered_map>
18 19 20
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/platform/cudnn_helper.h"
#endif
21 22 23
#ifdef PADDLE_WITH_HIP
#include "paddle/fluid/platform/miopen_helper.h"
#endif
24 25 26
#ifdef PADDLE_WITH_MKLDNN
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif
27 28 29 30

namespace paddle {
namespace operators {

31 32
int PoolOutputSize(int input_size, int filter_size, int padding_1,
                   int padding_2, int stride, bool ceil_mode) {
33 34
  int output_size;
  if (!ceil_mode) {
35 36
    output_size =
        (input_size - filter_size + padding_1 + padding_2) / stride + 1;
37 38
  } else {
    output_size =
39 40 41
        (input_size - filter_size + padding_1 + padding_2 + stride - 1) /
            stride +
        1;
42
  }
43 44
  PADDLE_ENFORCE_GT(
      output_size, 0,
45 46 47 48 49 50
      platform::errors::InvalidArgument(
          "the output size must be greater than 0. But received: "
          "output_size = %d due to the settings of input_size(%d), "
          "padding(%d,%d), "
          "k_size(%d) and stride(%d). Please check again!",
          output_size, input_size, padding_1, padding_2, filter_size, stride));
51 52 53
  return output_size;
}

C
chengduo 已提交
54
void PoolOp::InferShape(framework::InferShapeContext* ctx) const {
55 56 57 58 59 60
  PADDLE_ENFORCE_EQ(
      ctx->HasInput("X"), true,
      platform::errors::NotFound("Input(X) of Pool operator is not found."));
  PADDLE_ENFORCE_EQ(
      ctx->HasOutput("Out"), true,
      platform::errors::NotFound("Output(Out) of Pool operator is not found."));
61

C
chengduoZH 已提交
62
  std::string pooling_type = ctx->Attrs().Get<std::string>("pooling_type");
63 64 65
  std::vector<int> ksize = ctx->Attrs().Get<std::vector<int>>("ksize");
  std::vector<int> strides = ctx->Attrs().Get<std::vector<int>>("strides");
  std::vector<int> paddings = ctx->Attrs().Get<std::vector<int>>("paddings");
66
  bool ceil_mode = ctx->Attrs().Get<bool>("ceil_mode");
67
  bool adaptive = ctx->Attrs().Get<bool>("adaptive");
68 69 70 71
  bool global_pooling = ctx->Attrs().Get<bool>("global_pooling");
  std::string data_format = ctx->Attrs().Get<std::string>("data_format");
  std::string padding_algorithm =
      ctx->Attrs().Get<std::string>("padding_algorithm");
72

73
  auto in_x_dims = ctx->GetInputDim("X");
74 75
  PADDLE_ENFORCE_EQ(
      in_x_dims.size() == 4 || in_x_dims.size() == 5, true,
76 77 78 79
      platform::errors::InvalidArgument(
          "the input of Op(pool) should be 4-D or 5-D Tensor. But "
          "received: %u-D Tensor and it's shape is [%s].",
          in_x_dims.size(), in_x_dims));
80 81 82

  PADDLE_ENFORCE_EQ(
      in_x_dims.size() - ksize.size(), 2U,
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      platform::errors::InvalidArgument(
          "the dimension of input minus the size of "
          "Attr(ksize) must be euqal to 2 in Op(pool). "
          "But received: the dimension of input minus the size "
          "of Attr(ksize) is %d, the "
          "input's dimension is %d, the shape of input "
          "is [%s], the Attr(ksize)'s size is %d, the Attr(ksize) is [%s].",
          in_x_dims.size() - ksize.size(), in_x_dims.size(), in_x_dims,
          ksize.size(), framework::make_ddim(ksize)));

  PADDLE_ENFORCE_EQ(
      ksize.size(), strides.size(),
      platform::errors::InvalidArgument(
          "the size of Attr(ksize) and Attr(strides) in "
          "Op(pool) must be equal. "
          "But received: Attr(ksize)'s size is %d, Attr(strides)'s "
          "size is %d, Attr(ksize) is [%s], Attr(strides)is [%s].",
          ksize.size(), strides.size(), framework::make_ddim(ksize),
          framework::make_ddim(strides)));
102

103 104 105 106
  // MKL-DNN Kernels are using NCHW order of dims description
  // so we ignore data_format consideration for MKL-DNN kernel
  const bool channel_last = (this->IsMKLDNNType() == false) &&
                            (data_format == "NHWC" || data_format == "NDHWC");
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

  // update paddings if "SAME" or global_pooling
  framework::DDim data_dims;
  if (channel_last) {
    data_dims = framework::slice_ddim(in_x_dims, 1, in_x_dims.size() - 1);
  } else {
    data_dims = framework::slice_ddim(in_x_dims, 2, in_x_dims.size());
  }
  UpdatePadding(&paddings, global_pooling, adaptive, padding_algorithm,
                data_dims, strides, ksize);

  if (global_pooling) {
    UpdateKsize(&ksize, data_dims);
  }

  std::vector<int64_t> output_shape;
123 124 125
  if (adaptive) {
    output_shape.insert(output_shape.end(), ksize.begin(), ksize.end());
  } else {
126
    for (int i = 0; i < data_dims.size(); ++i) {
127
      if ((!ctx->IsRuntime()) && (data_dims[i] < 0)) {
128
        output_shape.push_back(data_dims[i]);
K
Kaipeng Deng 已提交
129
      } else {
130 131 132
        output_shape.push_back(
            PoolOutputSize(data_dims[i], ksize[i], paddings[2 * i],
                           paddings[2 * i + 1], strides[i], ceil_mode));
K
Kaipeng Deng 已提交
133
      }
134
    }
135
  }
136 137 138 139 140 141 142 143 144 145

  // output_N = input_N
  output_shape.insert(output_shape.begin(), in_x_dims[0]);
  // output_C = input_C
  if (channel_last) {
    output_shape.push_back(in_x_dims[in_x_dims.size() - 1]);
  } else {
    output_shape.insert(output_shape.begin() + 1, in_x_dims[1]);
  }

146
  ctx->SetOutputDim("Out", framework::make_ddim(output_shape));
Y
Yang Yu 已提交
147
  ctx->ShareLoD("X", "Out");
148 149
}

150 151 152 153 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
bool CanMKLDNNSupportPool(const framework::ExecutionContext& ctx) {
  if (ctx.Attr<bool>("adaptive") == false) return true;
  // (jczaja): oneDNN is supporting only unchangable in size pool window
  auto src_tz = paddle::framework::vectorize(ctx.Input<Tensor>("X")->dims());
  std::vector<int> ksize = ctx.Attr<std::vector<int>>("ksize");
  // Fast but not exhustive check
  if ((src_tz[src_tz.size() - 1] % ksize[1] == 0) &&
      (src_tz[src_tz.size() - 2] % ksize[0] == 0))
    return true;

  // Exhustive check
  auto IH = static_cast<double>(src_tz[src_tz.size() - 2]);
  auto IW = static_cast<double>(src_tz[src_tz.size() - 1]);
  auto OH = static_cast<double>(ksize[0]);
  auto OW = static_cast<double>(ksize[1]);

  auto SH = static_cast<int>(floor((IH * 2.0) / OH) - floor(IH / OH));
  auto SW = static_cast<int>(floor((IW * 2.0) / OW) - floor(IW / OW));
  auto KH = static_cast<int>(ceil((IH * 2.0) / OH) - floor(IH / OH));
  auto KW = static_cast<int>(ceil((IW * 2.0) / OW) - floor(IW / OW));

  auto PH = (SH * (static_cast<int>(OH) - 1) + KH - static_cast<int>(IH));
  auto PW = (SW * (static_cast<int>(OW) - 1) + KW - static_cast<int>(IW));
  // If there is additional padding needed then
  // this is situation that oneDNN cannot comply with
  // paddlepaddle reference implementation
  return (PH == 0) && (PW == 0);
}

179
framework::OpKernelType PoolOp::GetExpectedKernelType(
C
chengduo 已提交
180
    const framework::ExecutionContext& ctx) const {
181
  framework::LibraryType library_{framework::LibraryType::kPlain};
182
  std::string data_format = "AnyLayout";
M
mozga-intel 已提交
183
  framework::DataLayout layout_ = framework::StringToDataLayout(data_format);
184
  auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
M
mozga-intel 已提交
185

186
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
187 188
  if (platform::CanCUDNNBeUsed(ctx)) {
    library_ = framework::LibraryType::kCUDNN;
C
chengduoZH 已提交
189 190
  }
#endif
191 192
#ifdef PADDLE_WITH_MKLDNN
  if (library_ == framework::LibraryType::kPlain &&
193
      this->CanMKLDNNBeUsed(ctx, data_type) && CanMKLDNNSupportPool(ctx)) {
194
    library_ = framework::LibraryType::kMKLDNN;
M
mozga-intel 已提交
195
    layout_ = framework::DataLayout::kMKLDNN;
196
  }
197
#endif
198

199
  return framework::OpKernelType(data_type, ctx.GetPlace(), layout_, library_);
200 201
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
framework::OpKernelType PoolOp::GetKernelTypeForVar(
    const std::string& var_name, const Tensor& tensor,
    const framework::OpKernelType& expected_kernel_type) const {
#ifdef PADDLE_WITH_MKLDNN
  if ((expected_kernel_type.data_layout_ == framework::DataLayout::kMKLDNN) &&
      (tensor.layout() != framework::DataLayout::kMKLDNN)) {
    auto attrs = Attrs();
    auto ar = paddle::framework::AttrReader(attrs);
    const std::string data_format = ar.Get<std::string>("data_format");
    auto dl = framework::StringToDataLayout(data_format);
    // Some models may have intentionally set "AnyLayout" for pool
    // op. Treat this as NCHW (default data_format value)
    if (dl != framework::DataLayout::kAnyLayout) {
      return framework::OpKernelType(expected_kernel_type.data_type_,
                                     tensor.place(), dl);
    }
  }
#endif
  return framework::OpKernelType(expected_kernel_type.data_type_,
                                 tensor.place(), tensor.layout());
}

C
chengduo 已提交
224
void PoolOpGrad::InferShape(framework::InferShapeContext* ctx) const {
225 226 227
  PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true,
                    platform::errors::NotFound(
                        "Input(X) of Pool Gradoperator is not found."));
228
  PADDLE_ENFORCE_EQ(ctx->HasOutput(framework::GradVarName("X")), true,
229 230
                    platform::errors::NotFound(
                        "Input(X@GRAD) of Pool Gradoperator is not found."));
231 232 233
  ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
}

234
framework::OpKernelType PoolOpGrad::GetExpectedKernelType(
C
chengduo 已提交
235
    const framework::ExecutionContext& ctx) const {
236
  framework::LibraryType library_{framework::LibraryType::kPlain};
237
  std::string data_format = "AnyLayout";
M
mozga-intel 已提交
238
  framework::DataLayout layout_ = framework::StringToDataLayout(data_format);
239
  auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
M
mozga-intel 已提交
240

241
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
242 243
  if (platform::CanCUDNNBeUsed(ctx)) {
    library_ = framework::LibraryType::kCUDNN;
C
chengduoZH 已提交
244 245
  }
#endif
246 247
#ifdef PADDLE_WITH_MKLDNN
  if (library_ == framework::LibraryType::kPlain &&
248 249
      this->CanMKLDNNBeUsed(ctx, input_data_type) &&
      CanMKLDNNSupportPool(ctx)) {
250
    library_ = framework::LibraryType::kMKLDNN;
M
mozga-intel 已提交
251
    layout_ = framework::DataLayout::kMKLDNN;
252
  }
253
#endif
254

K
Kexin Zhao 已提交
255 256
  return framework::OpKernelType(input_data_type, ctx.GetPlace(), layout_,
                                 library_);
257 258
}

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
framework::OpKernelType PoolOpGrad::GetKernelTypeForVar(
    const std::string& var_name, const Tensor& tensor,
    const framework::OpKernelType& expected_kernel_type) const {
#ifdef PADDLE_WITH_MKLDNN
  if ((expected_kernel_type.data_layout_ == framework::DataLayout::kMKLDNN) &&
      (tensor.layout() != framework::DataLayout::kMKLDNN)) {
    auto attrs = Attrs();
    auto ar = paddle::framework::AttrReader(attrs);
    const std::string data_format = ar.Get<std::string>("data_format");
    return framework::OpKernelType(expected_kernel_type.data_type_,
                                   tensor.place(),
                                   framework::StringToDataLayout(data_format));
  }
#endif
  return framework::OpKernelType(expected_kernel_type.data_type_,
                                 tensor.place(), tensor.layout());
}

Y
Yu Yang 已提交
277
void Pool2dOpMaker::Make() {
278 279
  AddInput(
      "X",
C
chengduoZH 已提交
280
      "(Tensor) The input tensor of pooling operator. "
K
kexinzhao 已提交
281 282 283
      "The format of input tensor is NCHW, where N is batch size, C is the "
      "number of channels, H is the height of the feature, "
      "and W is the width of the feature.");
284
  AddOutput("Out",
K
kexinzhao 已提交
285 286 287 288
            "(Tensor) The output tensor of pooling operator. "
            "The format of output tensor is also NCHW, "
            "where N is batch size, C is the number of channels, "
            "H is the height of the feature, "
289
            "and W is the width of the feature.");
290

C
chengduoZH 已提交
291
  AddAttr<std::string>("pooling_type",
C
chengduoZH 已提交
292 293
                       "(string), pooling type, can be \"max\" for max-pooling "
                       "and \"avg\" for average-pooling.")
294
      .InEnum({"max", "avg"});
C
fix bug  
chengduoZH 已提交
295
  AddAttr<std::vector<int>>("ksize",
K
kexinzhao 已提交
296 297
                            "(vector<int>) The pooling window "
                            "size(height, width) of the pooling operator. "
C
chengduoZH 已提交
298
                            "If global_pooling = true, ksize and paddings will "
C
fix bug  
chengduoZH 已提交
299 300
                            "be ignored.");  // TODO(Chengduo): Add checker.
                                             // (Currently,
C
fix doc  
chengduoZH 已提交
301
  // TypedAttrChecker don't support vector type.)
302 303
  AddAttr<bool>(
      "global_pooling",
K
Kaipeng Deng 已提交
304 305 306
      "(bool) Whether to use the global pooling. "
      "If global_pooling = true, kernel size and paddings will be ignored. "
      "Default False.")
307
      .SetDefault(false);
K
kexinzhao 已提交
308 309 310
  AddAttr<std::vector<int>>("strides",
                            "(vector<int>, default {1, 1}), strides(height, "
                            "width) of pooling operator.")
311 312
      .SetDefault({1, 1});
  // TODO(Chengduo): Add checker. (Currently,
C
fix doc  
chengduoZH 已提交
313 314 315
  // TypedAttrChecker don't support vector type.)
  AddAttr<std::vector<int>>(
      "paddings",
316 317
      "(vector<int>, default {0,0}), paddings(height_top, height_bottom, "
      "width_left, wifth_right) of pooling operator."
318
      "If global_pooling = true, paddings and kernel size will be ignored.")
319
      .SetDefault({0, 0});
320 321
  AddAttr<bool>(
      "exclusive",
K
Kaipeng Deng 已提交
322
      "(bool) When true, will exclude the zero-padding in the "
323
      "averaging calculating, otherwise, include the zero-padding. Note, it "
K
Kaipeng Deng 已提交
324 325
      "is only used when pooling_type is avg. The default is True. "
      "Default True.")
326
      .SetDefault(true);
327 328
  AddAttr<bool>(
      "adaptive",
K
Kaipeng Deng 已提交
329
      "(bool) When true, will perform adaptive pooling instead, "
330 331
      "output shape in H and W dimensions will be same as ksize, input data "
      "will be divided into grids specify by ksize averagely and perform "
K
Kaipeng Deng 已提交
332 333
      "pooling in each grid area to get output pooling value. "
      "Default False.")
334 335
      .SetDefault(false);

336 337
  AddAttr<bool>(
      "use_cudnn",
K
Kaipeng Deng 已提交
338
      "(bool) Only used in cudnn kernel, need install cudnn. Default False")
D
Double_V 已提交
339 340
      .SetDefault(false)
      .AsExtra();
341 342
  AddAttr<bool>(
      "ceil_mode",
K
Kaipeng Deng 已提交
343
      "(bool) Whether to use the ceil function to calculate "
W
wanghaoshuang 已提交
344
      "output height and width. False is the default. If it is set to False, "
K
Kaipeng Deng 已提交
345
      "the floor function will be used. Default False")
346
      .SetDefault(false);
347
  AddAttr<bool>("use_mkldnn",
K
Kaipeng Deng 已提交
348
                "(bool) Only used in mkldnn kernel. Default False")
D
Double_V 已提交
349 350
      .SetDefault(false)
      .AsExtra();
351 352 353 354
  AddAttr<bool>(
      "use_quantizer",
      "(bool, default false) "
      "This parameter is no longer used. Use 'mkldnn_data_type' instead.")
D
Double_V 已提交
355 356
      .SetDefault(false)
      .AsExtra();
357 358 359 360
  AddAttr<std::string>(
      "mkldnn_data_type",
      "(string, default \"float32\"). Data type of mkldnn kernel")
      .SetDefault("float32")
D
Double_V 已提交
361 362
      .InEnum({"float32", "int8", "bfloat16"})
      .AsExtra();
363 364 365 366 367 368
  AddAttr<std::string>(
      "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. ")
369
      .SetDefault("NCHW");
370 371 372
  AddAttr<bool>("is_test",
                "(bool, default false) Set to true for inference only, false "
                "for training. Some layers may run faster when this is true.")
D
Double_V 已提交
373 374
      .SetDefault(false)
      .AsExtra();
375

376 377 378 379 380 381
  AddAttr<std::string>(
      "padding_algorithm",
      "(string, default \"EXPLICIT\") An optional string from: \"EXPLICIT\","
      "\"SAME\",\"VALID\". Set to \"EXPLICIT\" for explicit padding. "
      "Set to \"SAME\" or \"VALID\" for algorithm of padding. ")
      .SetDefault("EXPLICIT");
382
  // TODO(dzhwinter): need to registered layout transform function
383 384

  AddComment(R"DOC(
K
Kaipeng Deng 已提交
385 386 387
This operation calculates the pooling output based on
the input, pooling_type and pool_size, pool_stride, pool_padding parameters.
Input(X) and Output(Out) are in NCHW or NHWC format, where N is batch size, C is the
K
kexinzhao 已提交
388
number of channels, H is the height of the feature, and W is the width of the feature.
K
Kaipeng Deng 已提交
389
Parameters(pool_size, pool_stride, pool_padding) hold two integer elements.
C
fix doc  
chengduoZH 已提交
390
These two elements represent height and width, respectively.
C
chengduoZH 已提交
391 392
The input(X) size and output(Out) size may be different.

393
Example:
F
fengjiayi 已提交
394

C
chengduoZH 已提交
395
  Input:
F
fengjiayi 已提交
396

K
kexinzhao 已提交
397
       X shape: $(N, C, H_{in}, W_{in})$
F
fengjiayi 已提交
398

C
chengduoZH 已提交
399
  Output:
F
fengjiayi 已提交
400

K
kexinzhao 已提交
401
       Out shape: $(N, C, H_{out}, W_{out})$
F
fengjiayi 已提交
402

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  For pool_padding = "SAME":
       $$
       H_{out} = \\frac{(H_{in} + strides[0] - 1)}{strides[0]}
       $$
       $$
       W_{out} = \\frac{(W_{in} + strides[1] - 1)}{strides[1]}
       $$

  For pool_padding = "VALID":
       $$
       H_{out} = \\frac{(H_{in} - ksize[0] + strides[0])}{strides[0]}
       $$
       $$
       W_{out} = \\frac{(W_{in} - ksize[1] + strides[1])}{strides[1]}
       $$

419 420
  For ceil_mode = false:
       $$
421
       H_{out} = \\frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom}{strides[0]} + 1
F
fengjiayi 已提交
422 423
       $$
       $$
424
       W_{out} = \\frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right}{strides[1]} + 1
K
kexinzhao 已提交
425
       $$
426

427 428
  For ceil_mode = true:
       $$
429
       H_{out} = \\frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom + strides[0] - 1)}{strides[0]} + 1
F
fengjiayi 已提交
430 431
       $$
       $$
432
       W_{out} = \\frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right + strides[1] - 1)}{strides[1]} + 1
433
       $$
K
kexinzhao 已提交
434

435
  For exclusive = false:
436
       $$
437
       hstart = i * strides[0] - pad_height_top
438 439 440 441 442
       $$
       $$
       hend = hstart + ksize[0]
       $$
       $$
443
       wstart = j * strides[1] - pad_width_left
444 445 446 447 448 449 450
       $$
       $$
       wend = wstart + ksize[1]
       $$
       $$
       Output(i ,j) = \\frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]}
       $$
451

452
  For exclusive = true:
453
       $$
454
       hstart = max(0, i * strides[0] - pad_height_top)
455 456 457 458 459
       $$
       $$
       hend = min(H, hstart + ksize[0])
       $$
       $$
460
       wstart = max(0, j * strides[1] - pad_width_left)
461 462 463 464 465 466 467
       $$
       $$
       wend = min(W, wstart + ksize[1])
       $$
       $$
       Output(i ,j) = \\frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)}
       $$
468

469
)DOC");
470 471
}

472 473 474 475 476 477 478 479 480 481 482 483 484 485
template <typename T>
class Pool2dOpGradGradMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> grad_op) const override {
    grad_op->SetType("pool2d_grad_grad");
    grad_op->SetInput("X", this->OutputGrad(framework::GradVarName("X")));
    grad_op->SetOutput("Out", this->InputGrad(framework::GradVarName("Out")));
    grad_op->SetAttrMap(this->Attrs());
  }
};

C
chengduo 已提交
486 487
class PoolOpInferVarType : public framework::PassInDtypeAndVarTypeToOutput {
 protected:
488
  std::unordered_map<std::string, std::string>& GetInputOutputWithSameType()
C
chengduo 已提交
489
      const override {
490 491
    static std::unordered_map<std::string, std::string> m{{"X", /*->*/ "Out"}};
    return m;
C
chengduo 已提交
492 493 494
  }
};

Y
Yu Yang 已提交
495
void Pool3dOpMaker::Make() {
K
kexinzhao 已提交
496 497
  AddInput("X",
           "(Tensor) The input tensor of pooling operator. "
498 499
           "The format of input tensor is NCDHW or NDHWC, where N is batch "
           "size, C is "
K
kexinzhao 已提交
500 501 502
           "the number of channels, and D, H and W is the depth, height and "
           "width of "
           "the feature, respectively.");
503
  AddOutput("Out",
C
chengduoZH 已提交
504
            "(Tensor) The output tensor of pooling operator."
505
            "The format of output tensor is also NCDHW or NDHWC, "
K
kexinzhao 已提交
506 507
            "where N is batch size, C is "
            "the number of channels, and D, H and W is the depth, height and "
508
            "width of the feature, respectively.");
509

C
chengduoZH 已提交
510
  AddAttr<std::string>("pooling_type",
K
kexinzhao 已提交
511
                       "(string) Pooling type, can be \"max\" for max-pooling "
C
chengduoZH 已提交
512
                       "and \"avg\" for average-pooling.")
513
      .InEnum({"max", "avg"});
K
kexinzhao 已提交
514 515 516 517
  AddAttr<std::vector<int>>(
      "ksize",
      "(vector<int>) The pooling window size(depth, height, "
      "width) of pooling operator. "
C
chengduoZH 已提交
518
      "If global_pooling = true, ksize and paddings will "
K
kexinzhao 已提交
519 520
      "be ignored.");  // TODO(Chengduo): Add checker.
                       // (Currently,
C
fix bug  
chengduoZH 已提交
521
  // TypedAttrChecker don't support vector type.)
C
chengduoZH 已提交
522 523
  AddAttr<bool>(
      "global_pooling",
K
Kaipeng Deng 已提交
524 525 526
      "(bool) Whether to use the global pooling. "
      "If global_pooling = true, kernel size and paddings will be ignored. "
      "Default False")
527
      .SetDefault(false);
K
kexinzhao 已提交
528 529 530 531
  AddAttr<std::vector<int>>(
      "strides",
      "(vector<int>, default {1,1,1}) Strides(depth, height, "
      "width) of the pooling operator.")
532 533
      .SetDefault({1, 1, 1});  // TODO(Chengduo): Add checker. (Currently,
                               // TypedAttrChecker don't support vector type.)
C
fix bug  
chengduoZH 已提交
534 535
  AddAttr<std::vector<int>>(
      "paddings",
536 537 538 539
      "(vector<int>, default {0,0,0}), paddings(pad_depth_front, "
      "pad_depth_back, "
      "pad_height_top, pad_height_bottom, pad_width_left, pad_width_right"
      ") of pooling operator. "
C
chengduoZH 已提交
540
      "If global_pooling = true, ksize and paddings will be ignored.")
541 542
      .SetDefault({0, 0, 0});  // TODO(Chengduo): Add checker. (Currently,
                               // TypedAttrChecker don't support vector type.)
543 544
  AddAttr<bool>(
      "exclusive",
K
Kaipeng Deng 已提交
545
      "(bool) When true, will exclude the zero-padding in the "
546
      "averaging calculating, otherwise, include the zero-padding. Note, it "
K
Kaipeng Deng 已提交
547 548
      "is only used when pooling_type is avg. The default is True. "
      "Default True")
549
      .SetDefault(true);
550 551
  AddAttr<bool>(
      "adaptive",
K
Kaipeng Deng 已提交
552
      "(bool) When true, will perform adaptive pooling instead, "
553 554
      "output shape in H and W dimensions will be same as ksize, input data "
      "will be divided into grids specify by ksize averagely and perform "
K
Kaipeng Deng 已提交
555 556
      "pooling in each grid area to get output pooling value. "
      "Default False")
557
      .SetDefault(false);
558

559 560
  AddAttr<bool>(
      "use_cudnn",
K
Kaipeng Deng 已提交
561
      "(bool) Only used in cudnn kernel, need install cudnn. Default False")
D
Double_V 已提交
562 563
      .SetDefault(false)
      .AsExtra();
564 565
  AddAttr<bool>(
      "ceil_mode",
K
Kaipeng Deng 已提交
566
      "(bool) Whether to use the ceil function to calculate "
W
wanghaoshuang 已提交
567
      "output height and width. False is the default. If it is set to False, "
K
Kaipeng Deng 已提交
568
      "the floor function will be used. Default False")
569
      .SetDefault(false);
570
  AddAttr<bool>("use_mkldnn",
K
Kaipeng Deng 已提交
571
                "(bool) Only used in mkldnn kernel. Default False")
D
Double_V 已提交
572 573
      .SetDefault(false)
      .AsExtra();
574 575
  AddAttr<std::string>(
      "data_format",
576 577 578
      "(string, default NCDHW) Only used in "
      "An optional string from: \"NDHWC\", \"NCDHW\". "
      "Defaults to \"NDHWC\". Specify the data format of the output data, "
579
      "the input will be transformed automatically. ")
580 581 582 583 584 585 586
      .SetDefault("NCDHW");
  AddAttr<std::string>(
      "padding_algorithm",
      "(string, default \"EXPLICIT\") An optional string from: \"EXPLICIT\","
      "\"SAME\",\"VALID\". Set to \"EXPLICIT\" for explicit padding. "
      "Set to \"SAME\" or \"VALID\" for algorithm of padding. ")
      .SetDefault("EXPLICIT");
587 588
  // TODO(dzhwinter): need to registered layout transform function

589
  AddComment(R"DOC(
K
Kaipeng Deng 已提交
590 591
This operation calculates the output based on
the input, pooling_type, pool_size, pool_stride, and pool_padding parameters.
592
Input(X) and output(Out) are in NCDHW or NDHWC format, where N is batch
K
kexinzhao 已提交
593
size, C is the number of channels, and D, H and W are the depth, height and
K
Kaipeng Deng 已提交
594 595
width of the feature, respectively. Parameters(pool_size, pool_stride, pool_padding)
hold three integer elements. These three elements represent depth, height and
K
kexinzhao 已提交
596
width, respectively. The input(X) size and output(Out) size may be different.
C
chengduoZH 已提交
597 598 599

Example:
  Input:
K
kexinzhao 已提交
600
       X shape: $(N, C, D_{in}, H_{in}, W_{in})$
C
chengduoZH 已提交
601
  Output:
K
kexinzhao 已提交
602
       Out shape: $(N, C, D_{out}, H_{out}, W_{out})$
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

  For pool_padding = "SAME":
       $$
       D_{out} = \\frac{(D_{in} + strides[0] - 1)}{strides[0]}
       $$
       $$
       H_{out} = \\frac{(H_{in} + strides[1] - 1)}{strides[1]}
       $$
       $$
       W_{out} = \\frac{(W_{in} + strides[2] - 1)}{strides[2]}
       $$

  For pool_padding = "VALID":
       $$
       D_{out} = \\frac{(D_{in} - ksize[0] + strides[0])}{strides[0]}
       $$
       $$
       H_{out} = \\frac{(H_{in} - ksize[1] + strides[1])}{strides[1]}
       $$
       $$
       W_{out} = \\frac{(W_{in} - ksize[2] + strides[2])}{strides[2]}
       $$

626
  For ceil_mode = false:
627
       $$
628
       D_{out} = \\frac{(D_{in} - ksize[0] + pad_depth_front + pad_depth_back)}{strides[0]} + 1
629 630
       $$
       $$
631
       H_{out} = \\frac{(H_{in} - ksize[1] + pad_height_top + pad_height_bottom)}{strides[1]} + 1
632 633
       $$
       $$
634
       W_{out} = \\frac{(W_{in} - ksize[2] + pad_width_left + pad_width_right)}{strides[2]} + 1
635
       $$
636
  For ceil_mode = true:
637
       $$
638
       D_{out} = \\frac{(D_{in} - ksize[0] + pad_depth_front + pad_depth_back + strides[0] -1)}{strides[0]} + 1
639 640
       $$
       $$
641
       H_{out} = \\frac{(H_{in} - ksize[1] + pad_height_top + pad_height_bottom + strides[1] -1)}{strides[1]} + 1
642 643
       $$
       $$
644
       W_{out} = \\frac{(W_{in} - ksize[2] + pad_width_left + pad_width_right + strides[2] -1)}{strides[2]} + 1
645
       $$
D
dengkaipeng 已提交
646

647
  For exclusive = false:
648
       $$
649
       dstart = i * strides[0] - pad_depth_front
650 651 652 653 654
       $$
       $$
       dend = dstart + ksize[0]
       $$
       $$
655
       hstart = j * strides[1] - pad_height_top
656 657 658 659 660
       $$
       $$
       hend = hstart + ksize[1]
       $$
       $$
661
       wstart = k * strides[2] -  pad_width_left
662 663 664 665 666 667 668
       $$
       $$
       wend = wstart + ksize[2]
       $$
       $$
       Output(i ,j, k) = \\frac{sum(Input[dstart:dend, hstart:hend, wstart:wend])}{ksize[0] * ksize[1] * ksize[2]}
       $$
669

670
  For exclusive = true:
671
       $$
672
       dstart = max(0, i * strides[0] - pad_depth_front)
673 674 675 676 677
       $$
       $$
       dend = min(D, dstart + ksize[0])
       $$
       $$
678 679 680
       hstart = max(0, j * strides[1] - pad_height_top)
       $$
       $$
681 682 683
       hend = min(H, hstart + ksize[1])
       $$
       $$
684
       wstart = max(0, k * strides[2] - pad_width_left)
685 686 687 688 689 690 691
       $$
       $$
       wend = min(W, wstart + ksize[2])
       $$
       $$
       Output(i ,j, k) = \\frac{sum(Input[dstart:dend, hstart:hend, wstart:wend])}{(dend - dstart) * (hend - hstart) * (wend - wstart)}
       $$
K
kexinzhao 已提交
692

693
)DOC");
694
}
695 696 697 698 699
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

H
hong 已提交
700 701 702 703
REGISTER_OPERATOR(
    pool2d, ops::PoolOp, ops::Pool2dOpMaker, ops::PoolOpInferVarType,
    paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
    paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>);
704 705 706 707
REGISTER_OPERATOR(pool2d_grad, ops::PoolOpGrad,
                  ops::Pool2dOpGradGradMaker<paddle::framework::OpDesc>,
                  ops::Pool2dOpGradGradMaker<paddle::imperative::OpBase>);
REGISTER_OPERATOR(pool2d_grad_grad, ops::PoolOp);
708

Q
QI JUN 已提交
709 710 711 712 713
REGISTER_OP_CPU_KERNEL(
    pool2d, ops::PoolKernel<paddle::platform::CPUDeviceContext, float>,
    ops::PoolKernel<paddle::platform::CPUDeviceContext, double>);
REGISTER_OP_CPU_KERNEL(
    pool2d_grad, ops::PoolGradKernel<paddle::platform::CPUDeviceContext, float>,
714
    ops::PoolGradKernel<paddle::platform::CPUDeviceContext, double>);
715 716 717 718
REGISTER_OP_CPU_KERNEL(
    pool2d_grad_grad,
    ops::PoolGradGradKernel<paddle::platform::CPUDeviceContext, float>,
    ops::PoolGradGradKernel<paddle::platform::CPUDeviceContext, double>);
719

H
hong 已提交
720 721 722 723
REGISTER_OPERATOR(
    pool3d, ops::PoolOp, ops::Pool3dOpMaker, ops::PoolOpInferVarType,
    paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
    paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>);
724
REGISTER_OPERATOR(pool3d_grad, ops::PoolOpGrad);
725

Q
QI JUN 已提交
726 727 728 729 730 731
REGISTER_OP_CPU_KERNEL(
    pool3d, ops::PoolKernel<paddle::platform::CPUDeviceContext, float>,
    ops::PoolKernel<paddle::platform::CPUDeviceContext, double>);
REGISTER_OP_CPU_KERNEL(
    pool3d_grad, ops::PoolGradKernel<paddle::platform::CPUDeviceContext, float>,
    ops::PoolGradKernel<paddle::platform::CPUDeviceContext, double>);