pool_op.cc 22.3 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>
F
From00 已提交
18 19 20 21 22 23
#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/backward.h"
#include "paddle/phi/infermeta/unary.h"

24
#include "paddle/fluid/platform/device/gpu/gpu_dnn.h"
25 26 27
#ifdef PADDLE_WITH_MKLDNN
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif
28 29 30 31

namespace paddle {
namespace operators {

32 33 34
bool CanMKLDNNSupportPool(const framework::ExecutionContext& ctx) {
  if (ctx.Attr<bool>("adaptive") == false) return true;
  // (jczaja): oneDNN is supporting only unchangable in size pool window
35
  auto src_tz = phi::vectorize(ctx.Input<Tensor>("X")->dims());
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  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);
}

61
framework::OpKernelType PoolOp::GetExpectedKernelType(
C
chengduo 已提交
62
    const framework::ExecutionContext& ctx) const {
63
  framework::LibraryType library_{framework::LibraryType::kPlain};
64
  std::string data_format = "AnyLayout";
M
mozga-intel 已提交
65
  framework::DataLayout layout_ = framework::StringToDataLayout(data_format);
66
  auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
M
mozga-intel 已提交
67

68
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
69 70
  if (platform::CanCUDNNBeUsed(ctx)) {
    library_ = framework::LibraryType::kCUDNN;
C
chengduoZH 已提交
71 72
  }
#endif
73 74
#ifdef PADDLE_WITH_MKLDNN
  if (library_ == framework::LibraryType::kPlain &&
75
      this->CanMKLDNNBeUsed(ctx, data_type) && CanMKLDNNSupportPool(ctx)) {
76
    library_ = framework::LibraryType::kMKLDNN;
M
mozga-intel 已提交
77
    layout_ = framework::DataLayout::kMKLDNN;
78
  }
79
#endif
80

81
  return framework::OpKernelType(data_type, ctx.GetPlace(), layout_, library_);
82 83
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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());
}

106
framework::OpKernelType PoolOpGrad::GetExpectedKernelType(
C
chengduo 已提交
107
    const framework::ExecutionContext& ctx) const {
108
  framework::LibraryType library_{framework::LibraryType::kPlain};
109
  std::string data_format = "AnyLayout";
M
mozga-intel 已提交
110
  framework::DataLayout layout_ = framework::StringToDataLayout(data_format);
111
  auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
M
mozga-intel 已提交
112

113
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
114 115
  if (platform::CanCUDNNBeUsed(ctx)) {
    library_ = framework::LibraryType::kCUDNN;
C
chengduoZH 已提交
116 117
  }
#endif
118 119
#ifdef PADDLE_WITH_MKLDNN
  if (library_ == framework::LibraryType::kPlain &&
120 121
      this->CanMKLDNNBeUsed(ctx, input_data_type) &&
      CanMKLDNNSupportPool(ctx)) {
122
    library_ = framework::LibraryType::kMKLDNN;
M
mozga-intel 已提交
123
    layout_ = framework::DataLayout::kMKLDNN;
124
  }
125
#endif
126

K
Kexin Zhao 已提交
127 128
  return framework::OpKernelType(input_data_type, ctx.GetPlace(), layout_,
                                 library_);
129 130
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
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 已提交
149
void Pool2dOpMaker::Make() {
150 151
  AddInput(
      "X",
C
chengduoZH 已提交
152
      "(Tensor) The input tensor of pooling operator. "
K
kexinzhao 已提交
153 154 155
      "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.");
156
  AddOutput("Out",
K
kexinzhao 已提交
157 158 159 160
            "(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, "
161
            "and W is the width of the feature.");
162

C
chengduoZH 已提交
163
  AddAttr<std::string>("pooling_type",
C
chengduoZH 已提交
164 165
                       "(string), pooling type, can be \"max\" for max-pooling "
                       "and \"avg\" for average-pooling.")
166
      .InEnum({"max", "avg"});
C
fix bug  
chengduoZH 已提交
167
  AddAttr<std::vector<int>>("ksize",
K
kexinzhao 已提交
168 169
                            "(vector<int>) The pooling window "
                            "size(height, width) of the pooling operator. "
C
chengduoZH 已提交
170
                            "If global_pooling = true, ksize and paddings will "
C
fix bug  
chengduoZH 已提交
171 172
                            "be ignored.");  // TODO(Chengduo): Add checker.
                                             // (Currently,
C
fix doc  
chengduoZH 已提交
173
  // TypedAttrChecker don't support vector type.)
174 175
  AddAttr<bool>(
      "global_pooling",
K
Kaipeng Deng 已提交
176 177 178
      "(bool) Whether to use the global pooling. "
      "If global_pooling = true, kernel size and paddings will be ignored. "
      "Default False.")
179
      .SetDefault(false);
K
kexinzhao 已提交
180 181 182
  AddAttr<std::vector<int>>("strides",
                            "(vector<int>, default {1, 1}), strides(height, "
                            "width) of pooling operator.")
183 184
      .SetDefault({1, 1});
  // TODO(Chengduo): Add checker. (Currently,
C
fix doc  
chengduoZH 已提交
185 186 187
  // TypedAttrChecker don't support vector type.)
  AddAttr<std::vector<int>>(
      "paddings",
188 189
      "(vector<int>, default {0,0}), paddings(height_top, height_bottom, "
      "width_left, wifth_right) of pooling operator."
190
      "If global_pooling = true, paddings and kernel size will be ignored.")
191
      .SetDefault({0, 0});
192 193
  AddAttr<bool>(
      "exclusive",
K
Kaipeng Deng 已提交
194
      "(bool) When true, will exclude the zero-padding in the "
195
      "averaging calculating, otherwise, include the zero-padding. Note, it "
K
Kaipeng Deng 已提交
196 197
      "is only used when pooling_type is avg. The default is True. "
      "Default True.")
198
      .SetDefault(true);
199 200
  AddAttr<bool>(
      "adaptive",
K
Kaipeng Deng 已提交
201
      "(bool) When true, will perform adaptive pooling instead, "
202 203
      "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 已提交
204 205
      "pooling in each grid area to get output pooling value. "
      "Default False.")
206 207
      .SetDefault(false);

208 209
  AddAttr<bool>(
      "use_cudnn",
K
Kaipeng Deng 已提交
210
      "(bool) Only used in cudnn kernel, need install cudnn. Default False")
D
Double_V 已提交
211 212
      .SetDefault(false)
      .AsExtra();
213 214
  AddAttr<bool>(
      "ceil_mode",
K
Kaipeng Deng 已提交
215
      "(bool) Whether to use the ceil function to calculate "
W
wanghaoshuang 已提交
216
      "output height and width. False is the default. If it is set to False, "
K
Kaipeng Deng 已提交
217
      "the floor function will be used. Default False")
218
      .SetDefault(false);
219
  AddAttr<bool>("use_mkldnn",
K
Kaipeng Deng 已提交
220
                "(bool) Only used in mkldnn kernel. Default False")
D
Double_V 已提交
221 222
      .SetDefault(false)
      .AsExtra();
223 224 225 226
  AddAttr<bool>(
      "use_quantizer",
      "(bool, default false) "
      "This parameter is no longer used. Use 'mkldnn_data_type' instead.")
D
Double_V 已提交
227 228
      .SetDefault(false)
      .AsExtra();
229 230 231 232
  AddAttr<std::string>(
      "mkldnn_data_type",
      "(string, default \"float32\"). Data type of mkldnn kernel")
      .SetDefault("float32")
D
Double_V 已提交
233 234
      .InEnum({"float32", "int8", "bfloat16"})
      .AsExtra();
235 236 237 238 239 240
  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. ")
241
      .SetDefault("NCHW");
242 243 244
  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 已提交
245 246
      .SetDefault(false)
      .AsExtra();
247

248 249 250 251 252 253
  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");
254
  // TODO(dzhwinter): need to registered layout transform function
255 256

  AddComment(R"DOC(
K
Kaipeng Deng 已提交
257 258 259
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 已提交
260
number of channels, H is the height of the feature, and W is the width of the feature.
K
Kaipeng Deng 已提交
261
Parameters(pool_size, pool_stride, pool_padding) hold two integer elements.
C
fix doc  
chengduoZH 已提交
262
These two elements represent height and width, respectively.
C
chengduoZH 已提交
263 264
The input(X) size and output(Out) size may be different.

265
Example:
F
fengjiayi 已提交
266

C
chengduoZH 已提交
267
  Input:
F
fengjiayi 已提交
268

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

C
chengduoZH 已提交
271
  Output:
F
fengjiayi 已提交
272

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

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  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]}
       $$

291 292
  For ceil_mode = false:
       $$
293
       H_{out} = \\frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom}{strides[0]} + 1
F
fengjiayi 已提交
294 295
       $$
       $$
296
       W_{out} = \\frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right}{strides[1]} + 1
K
kexinzhao 已提交
297
       $$
298

299 300
  For ceil_mode = true:
       $$
301
       H_{out} = \\frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom + strides[0] - 1)}{strides[0]} + 1
F
fengjiayi 已提交
302 303
       $$
       $$
304
       W_{out} = \\frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right + strides[1] - 1)}{strides[1]} + 1
305
       $$
K
kexinzhao 已提交
306

307
  For exclusive = false:
308
       $$
309
       hstart = i * strides[0] - pad_height_top
310 311 312 313 314
       $$
       $$
       hend = hstart + ksize[0]
       $$
       $$
315
       wstart = j * strides[1] - pad_width_left
316 317 318 319 320 321 322
       $$
       $$
       wend = wstart + ksize[1]
       $$
       $$
       Output(i ,j) = \\frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]}
       $$
323

324
  For exclusive = true:
325
       $$
326
       hstart = max(0, i * strides[0] - pad_height_top)
327 328 329 330 331
       $$
       $$
       hend = min(H, hstart + ksize[0])
       $$
       $$
332
       wstart = max(0, j * strides[1] - pad_width_left)
333 334 335 336 337 338 339
       $$
       $$
       wend = min(W, wstart + ksize[1])
       $$
       $$
       Output(i ,j) = \\frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)}
       $$
340

341
)DOC");
342 343
}

344 345 346 347 348 349 350
template <typename T>
class Pool2dOpGradGradMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> grad_op) const override {
F
From00 已提交
351
    grad_op->SetType("pool2d_double_grad");
352 353 354 355 356 357
    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 已提交
358 359
class PoolOpInferVarType : public framework::PassInDtypeAndVarTypeToOutput {
 protected:
360
  std::unordered_map<std::string, std::string>& GetInputOutputWithSameType()
C
chengduo 已提交
361
      const override {
362 363
    static std::unordered_map<std::string, std::string> m{{"X", /*->*/ "Out"}};
    return m;
C
chengduo 已提交
364 365 366
  }
};

Y
Yu Yang 已提交
367
void Pool3dOpMaker::Make() {
K
kexinzhao 已提交
368 369
  AddInput("X",
           "(Tensor) The input tensor of pooling operator. "
370 371
           "The format of input tensor is NCDHW or NDHWC, where N is batch "
           "size, C is "
K
kexinzhao 已提交
372 373 374
           "the number of channels, and D, H and W is the depth, height and "
           "width of "
           "the feature, respectively.");
375
  AddOutput("Out",
C
chengduoZH 已提交
376
            "(Tensor) The output tensor of pooling operator."
377
            "The format of output tensor is also NCDHW or NDHWC, "
K
kexinzhao 已提交
378 379
            "where N is batch size, C is "
            "the number of channels, and D, H and W is the depth, height and "
380
            "width of the feature, respectively.");
381

C
chengduoZH 已提交
382
  AddAttr<std::string>("pooling_type",
K
kexinzhao 已提交
383
                       "(string) Pooling type, can be \"max\" for max-pooling "
C
chengduoZH 已提交
384
                       "and \"avg\" for average-pooling.")
385
      .InEnum({"max", "avg"});
K
kexinzhao 已提交
386 387 388 389
  AddAttr<std::vector<int>>(
      "ksize",
      "(vector<int>) The pooling window size(depth, height, "
      "width) of pooling operator. "
C
chengduoZH 已提交
390
      "If global_pooling = true, ksize and paddings will "
K
kexinzhao 已提交
391 392
      "be ignored.");  // TODO(Chengduo): Add checker.
                       // (Currently,
C
fix bug  
chengduoZH 已提交
393
  // TypedAttrChecker don't support vector type.)
C
chengduoZH 已提交
394 395
  AddAttr<bool>(
      "global_pooling",
K
Kaipeng Deng 已提交
396 397 398
      "(bool) Whether to use the global pooling. "
      "If global_pooling = true, kernel size and paddings will be ignored. "
      "Default False")
399
      .SetDefault(false);
K
kexinzhao 已提交
400 401 402 403
  AddAttr<std::vector<int>>(
      "strides",
      "(vector<int>, default {1,1,1}) Strides(depth, height, "
      "width) of the pooling operator.")
404 405
      .SetDefault({1, 1, 1});  // TODO(Chengduo): Add checker. (Currently,
                               // TypedAttrChecker don't support vector type.)
C
fix bug  
chengduoZH 已提交
406 407
  AddAttr<std::vector<int>>(
      "paddings",
408 409 410 411
      "(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 已提交
412
      "If global_pooling = true, ksize and paddings will be ignored.")
413 414
      .SetDefault({0, 0, 0});  // TODO(Chengduo): Add checker. (Currently,
                               // TypedAttrChecker don't support vector type.)
415 416
  AddAttr<bool>(
      "exclusive",
K
Kaipeng Deng 已提交
417
      "(bool) When true, will exclude the zero-padding in the "
418
      "averaging calculating, otherwise, include the zero-padding. Note, it "
K
Kaipeng Deng 已提交
419 420
      "is only used when pooling_type is avg. The default is True. "
      "Default True")
421
      .SetDefault(true);
422 423
  AddAttr<bool>(
      "adaptive",
K
Kaipeng Deng 已提交
424
      "(bool) When true, will perform adaptive pooling instead, "
425 426
      "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 已提交
427 428
      "pooling in each grid area to get output pooling value. "
      "Default False")
429
      .SetDefault(false);
430

431 432
  AddAttr<bool>(
      "use_cudnn",
K
Kaipeng Deng 已提交
433
      "(bool) Only used in cudnn kernel, need install cudnn. Default False")
D
Double_V 已提交
434 435
      .SetDefault(false)
      .AsExtra();
436 437
  AddAttr<bool>(
      "ceil_mode",
K
Kaipeng Deng 已提交
438
      "(bool) Whether to use the ceil function to calculate "
W
wanghaoshuang 已提交
439
      "output height and width. False is the default. If it is set to False, "
K
Kaipeng Deng 已提交
440
      "the floor function will be used. Default False")
441
      .SetDefault(false);
442
  AddAttr<bool>("use_mkldnn",
K
Kaipeng Deng 已提交
443
                "(bool) Only used in mkldnn kernel. Default False")
D
Double_V 已提交
444 445
      .SetDefault(false)
      .AsExtra();
446 447
  AddAttr<std::string>(
      "data_format",
448 449 450
      "(string, default NCDHW) Only used in "
      "An optional string from: \"NDHWC\", \"NCDHW\". "
      "Defaults to \"NDHWC\". Specify the data format of the output data, "
451
      "the input will be transformed automatically. ")
452 453 454 455 456 457 458
      .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");
459 460
  // TODO(dzhwinter): need to registered layout transform function

461
  AddComment(R"DOC(
K
Kaipeng Deng 已提交
462 463
This operation calculates the output based on
the input, pooling_type, pool_size, pool_stride, and pool_padding parameters.
464
Input(X) and output(Out) are in NCDHW or NDHWC format, where N is batch
K
kexinzhao 已提交
465
size, C is the number of channels, and D, H and W are the depth, height and
K
Kaipeng Deng 已提交
466 467
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 已提交
468
width, respectively. The input(X) size and output(Out) size may be different.
C
chengduoZH 已提交
469 470 471

Example:
  Input:
K
kexinzhao 已提交
472
       X shape: $(N, C, D_{in}, H_{in}, W_{in})$
C
chengduoZH 已提交
473
  Output:
K
kexinzhao 已提交
474
       Out shape: $(N, C, D_{out}, H_{out}, W_{out})$
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

  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]}
       $$

498
  For ceil_mode = false:
499
       $$
500
       D_{out} = \\frac{(D_{in} - ksize[0] + pad_depth_front + pad_depth_back)}{strides[0]} + 1
501 502
       $$
       $$
503
       H_{out} = \\frac{(H_{in} - ksize[1] + pad_height_top + pad_height_bottom)}{strides[1]} + 1
504 505
       $$
       $$
506
       W_{out} = \\frac{(W_{in} - ksize[2] + pad_width_left + pad_width_right)}{strides[2]} + 1
507
       $$
508
  For ceil_mode = true:
509
       $$
510
       D_{out} = \\frac{(D_{in} - ksize[0] + pad_depth_front + pad_depth_back + strides[0] -1)}{strides[0]} + 1
511 512
       $$
       $$
513
       H_{out} = \\frac{(H_{in} - ksize[1] + pad_height_top + pad_height_bottom + strides[1] -1)}{strides[1]} + 1
514 515
       $$
       $$
516
       W_{out} = \\frac{(W_{in} - ksize[2] + pad_width_left + pad_width_right + strides[2] -1)}{strides[2]} + 1
517
       $$
D
dengkaipeng 已提交
518

519
  For exclusive = false:
520
       $$
521
       dstart = i * strides[0] - pad_depth_front
522 523 524 525 526
       $$
       $$
       dend = dstart + ksize[0]
       $$
       $$
527
       hstart = j * strides[1] - pad_height_top
528 529 530 531 532
       $$
       $$
       hend = hstart + ksize[1]
       $$
       $$
533
       wstart = k * strides[2] -  pad_width_left
534 535 536 537 538 539 540
       $$
       $$
       wend = wstart + ksize[2]
       $$
       $$
       Output(i ,j, k) = \\frac{sum(Input[dstart:dend, hstart:hend, wstart:wend])}{ksize[0] * ksize[1] * ksize[2]}
       $$
541

542
  For exclusive = true:
543
       $$
544
       dstart = max(0, i * strides[0] - pad_depth_front)
545 546 547 548 549
       $$
       $$
       dend = min(D, dstart + ksize[0])
       $$
       $$
550 551 552
       hstart = max(0, j * strides[1] - pad_height_top)
       $$
       $$
553 554 555
       hend = min(H, hstart + ksize[1])
       $$
       $$
556
       wstart = max(0, k * strides[2] - pad_width_left)
557 558 559 560 561 562 563
       $$
       $$
       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 已提交
564

565
)DOC");
566
}
567 568 569 570 571
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

F
From00 已提交
572 573 574 575 576 577 578 579
DECLARE_INFER_SHAPE_FUNCTOR(pool2d, Pool2dInferShapeFunctor,
                            PD_INFER_META(phi::PoolInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(pool2d_grad, Pool2dGradInferShapeFunctor,
                            PD_INFER_META(phi::PoolGradInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(pool2d_double_grad,
                            Pool2dDoubleGradInferShapeFunctor,
                            PD_INFER_META(phi::PoolInferMeta));

H
hong 已提交
580 581 582
REGISTER_OPERATOR(
    pool2d, ops::PoolOp, ops::Pool2dOpMaker, ops::PoolOpInferVarType,
    paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
F
From00 已提交
583 584
    paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>,
    Pool2dInferShapeFunctor);
585 586
REGISTER_OPERATOR(pool2d_grad, ops::PoolOpGrad,
                  ops::Pool2dOpGradGradMaker<paddle::framework::OpDesc>,
F
From00 已提交
587 588 589 590 591 592 593 594 595
                  ops::Pool2dOpGradGradMaker<paddle::imperative::OpBase>,
                  Pool2dGradInferShapeFunctor);
REGISTER_OPERATOR(pool2d_double_grad, ops::PoolOp,
                  Pool2dDoubleGradInferShapeFunctor);

DECLARE_INFER_SHAPE_FUNCTOR(pool3d, Pool3dInferShapeFunctor,
                            PD_INFER_META(phi::PoolInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(pool3d_grad, Pool3dGradInferShapeFunctor,
                            PD_INFER_META(phi::PoolGradInferMeta));
596

H
hong 已提交
597 598 599
REGISTER_OPERATOR(
    pool3d, ops::PoolOp, ops::Pool3dOpMaker, ops::PoolOpInferVarType,
    paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
F
From00 已提交
600 601 602
    paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>,
    Pool3dInferShapeFunctor);
REGISTER_OPERATOR(pool3d_grad, ops::PoolOpGrad, Pool3dGradInferShapeFunctor);