pool_op.cc 21.4 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

F
From00 已提交
19 20
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
21
#include "paddle/fluid/platform/device/gpu/gpu_dnn.h"
F
From00 已提交
22 23 24
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/unary.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
  std::vector<int> ksize = ctx.Attr<std::vector<int>>("ksize");
  // Fast but not exhustive check
38 39
  return ((src_tz[src_tz.size() - 1] % ksize[1] == 0) &&
          (src_tz[src_tz.size() - 2] % ksize[0] == 0));
40 41
}

42
framework::OpKernelType PoolOp::GetExpectedKernelType(
C
chengduo 已提交
43
    const framework::ExecutionContext& ctx) const {
44
  framework::LibraryType library_{framework::LibraryType::kPlain};
45
  std::string data_format = "AnyLayout";
M
mozga-intel 已提交
46
  framework::DataLayout layout_ = framework::StringToDataLayout(data_format);
47
  auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
M
mozga-intel 已提交
48

49
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
50 51
  if (platform::CanCUDNNBeUsed(ctx)) {
    library_ = framework::LibraryType::kCUDNN;
C
chengduoZH 已提交
52 53
  }
#endif
54 55
#ifdef PADDLE_WITH_MKLDNN
  if (library_ == framework::LibraryType::kPlain &&
56
      this->CanMKLDNNBeUsed(ctx, data_type) && CanMKLDNNSupportPool(ctx)) {
57
    library_ = framework::LibraryType::kMKLDNN;
M
mozga-intel 已提交
58
    layout_ = framework::DataLayout::kMKLDNN;
59
  }
60
#endif
61

62
  return framework::OpKernelType(data_type, ctx.GetPlace(), layout_, library_);
63 64
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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());
}

87
framework::OpKernelType PoolOpGrad::GetExpectedKernelType(
C
chengduo 已提交
88
    const framework::ExecutionContext& ctx) const {
89
  framework::LibraryType library_{framework::LibraryType::kPlain};
90
  std::string data_format = "AnyLayout";
M
mozga-intel 已提交
91
  framework::DataLayout layout_ = framework::StringToDataLayout(data_format);
92
  auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
M
mozga-intel 已提交
93

94
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
95 96
  if (platform::CanCUDNNBeUsed(ctx)) {
    library_ = framework::LibraryType::kCUDNN;
C
chengduoZH 已提交
97 98
  }
#endif
99 100
#ifdef PADDLE_WITH_MKLDNN
  if (library_ == framework::LibraryType::kPlain &&
101 102
      this->CanMKLDNNBeUsed(ctx, input_data_type) &&
      CanMKLDNNSupportPool(ctx)) {
103
    library_ = framework::LibraryType::kMKLDNN;
M
mozga-intel 已提交
104
    layout_ = framework::DataLayout::kMKLDNN;
105
  }
106
#endif
107

K
Kexin Zhao 已提交
108 109
  return framework::OpKernelType(input_data_type, ctx.GetPlace(), layout_,
                                 library_);
110 111
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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 已提交
130
void Pool2dOpMaker::Make() {
131 132
  AddInput(
      "X",
C
chengduoZH 已提交
133
      "(Tensor) The input tensor of pooling operator. "
K
kexinzhao 已提交
134 135 136
      "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.");
137
  AddOutput("Out",
K
kexinzhao 已提交
138 139 140 141
            "(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, "
142
            "and W is the width of the feature.");
143

C
chengduoZH 已提交
144
  AddAttr<std::string>("pooling_type",
C
chengduoZH 已提交
145 146
                       "(string), pooling type, can be \"max\" for max-pooling "
                       "and \"avg\" for average-pooling.")
147
      .InEnum({"max", "avg"});
C
fix bug  
chengduoZH 已提交
148
  AddAttr<std::vector<int>>("ksize",
K
kexinzhao 已提交
149 150
                            "(vector<int>) The pooling window "
                            "size(height, width) of the pooling operator. "
C
chengduoZH 已提交
151
                            "If global_pooling = true, ksize and paddings will "
C
fix bug  
chengduoZH 已提交
152 153
                            "be ignored.");  // TODO(Chengduo): Add checker.
                                             // (Currently,
C
fix doc  
chengduoZH 已提交
154
  // TypedAttrChecker don't support vector type.)
155 156
  AddAttr<bool>(
      "global_pooling",
K
Kaipeng Deng 已提交
157 158 159
      "(bool) Whether to use the global pooling. "
      "If global_pooling = true, kernel size and paddings will be ignored. "
      "Default False.")
160
      .SetDefault(false);
K
kexinzhao 已提交
161 162 163
  AddAttr<std::vector<int>>("strides",
                            "(vector<int>, default {1, 1}), strides(height, "
                            "width) of pooling operator.")
164 165
      .SetDefault({1, 1});
  // TODO(Chengduo): Add checker. (Currently,
C
fix doc  
chengduoZH 已提交
166 167 168
  // TypedAttrChecker don't support vector type.)
  AddAttr<std::vector<int>>(
      "paddings",
169 170
      "(vector<int>, default {0,0}), paddings(height_top, height_bottom, "
      "width_left, wifth_right) of pooling operator."
171
      "If global_pooling = true, paddings and kernel size will be ignored.")
172
      .SetDefault({0, 0});
173 174
  AddAttr<bool>(
      "exclusive",
K
Kaipeng Deng 已提交
175
      "(bool) When true, will exclude the zero-padding in the "
176
      "averaging calculating, otherwise, include the zero-padding. Note, it "
K
Kaipeng Deng 已提交
177 178
      "is only used when pooling_type is avg. The default is True. "
      "Default True.")
179
      .SetDefault(true);
180 181
  AddAttr<bool>(
      "adaptive",
K
Kaipeng Deng 已提交
182
      "(bool) When true, will perform adaptive pooling instead, "
183 184
      "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 已提交
185 186
      "pooling in each grid area to get output pooling value. "
      "Default False.")
187 188
      .SetDefault(false);

189 190
  AddAttr<bool>(
      "use_cudnn",
K
Kaipeng Deng 已提交
191
      "(bool) Only used in cudnn kernel, need install cudnn. Default False")
D
Double_V 已提交
192 193
      .SetDefault(false)
      .AsExtra();
194 195
  AddAttr<bool>(
      "ceil_mode",
K
Kaipeng Deng 已提交
196
      "(bool) Whether to use the ceil function to calculate "
W
wanghaoshuang 已提交
197
      "output height and width. False is the default. If it is set to False, "
K
Kaipeng Deng 已提交
198
      "the floor function will be used. Default False")
199
      .SetDefault(false);
200
  AddAttr<bool>("use_mkldnn",
K
Kaipeng Deng 已提交
201
                "(bool) Only used in mkldnn kernel. Default False")
D
Double_V 已提交
202 203
      .SetDefault(false)
      .AsExtra();
204 205 206 207
  AddAttr<bool>(
      "use_quantizer",
      "(bool, default false) "
      "This parameter is no longer used. Use 'mkldnn_data_type' instead.")
D
Double_V 已提交
208 209
      .SetDefault(false)
      .AsExtra();
210 211 212 213
  AddAttr<std::string>(
      "mkldnn_data_type",
      "(string, default \"float32\"). Data type of mkldnn kernel")
      .SetDefault("float32")
D
Double_V 已提交
214 215
      .InEnum({"float32", "int8", "bfloat16"})
      .AsExtra();
216 217 218 219 220 221
  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. ")
222
      .SetDefault("NCHW");
223 224 225
  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 已提交
226 227
      .SetDefault(false)
      .AsExtra();
228

229 230 231 232 233 234
  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");
235
  // TODO(dzhwinter): need to registered layout transform function
236 237

  AddComment(R"DOC(
K
Kaipeng Deng 已提交
238 239 240
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 已提交
241
number of channels, H is the height of the feature, and W is the width of the feature.
K
Kaipeng Deng 已提交
242
Parameters(pool_size, pool_stride, pool_padding) hold two integer elements.
C
fix doc  
chengduoZH 已提交
243
These two elements represent height and width, respectively.
C
chengduoZH 已提交
244 245
The input(X) size and output(Out) size may be different.

246
Example:
F
fengjiayi 已提交
247

C
chengduoZH 已提交
248
  Input:
F
fengjiayi 已提交
249

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

C
chengduoZH 已提交
252
  Output:
F
fengjiayi 已提交
253

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

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  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]}
       $$

272 273
  For ceil_mode = false:
       $$
274
       H_{out} = \\frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom}{strides[0]} + 1
F
fengjiayi 已提交
275 276
       $$
       $$
277
       W_{out} = \\frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right}{strides[1]} + 1
K
kexinzhao 已提交
278
       $$
279

280 281
  For ceil_mode = true:
       $$
282
       H_{out} = \\frac{(H_{in} - ksize[0] + pad_height_top + pad_height_bottom + strides[0] - 1)}{strides[0]} + 1
F
fengjiayi 已提交
283 284
       $$
       $$
285
       W_{out} = \\frac{(W_{in} - ksize[1] + pad_width_left + pad_width_right + strides[1] - 1)}{strides[1]} + 1
286
       $$
K
kexinzhao 已提交
287

288
  For exclusive = false:
289
       $$
290
       hstart = i * strides[0] - pad_height_top
291 292 293 294 295
       $$
       $$
       hend = hstart + ksize[0]
       $$
       $$
296
       wstart = j * strides[1] - pad_width_left
297 298 299 300 301 302 303
       $$
       $$
       wend = wstart + ksize[1]
       $$
       $$
       Output(i ,j) = \\frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]}
       $$
304

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

322
)DOC");
323 324
}

325 326 327 328 329 330 331
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 已提交
332
    grad_op->SetType("pool2d_double_grad");
333 334 335 336 337 338
    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 已提交
339 340
class PoolOpInferVarType : public framework::PassInDtypeAndVarTypeToOutput {
 protected:
341
  std::unordered_map<std::string, std::string>& GetInputOutputWithSameType()
C
chengduo 已提交
342
      const override {
343 344
    static std::unordered_map<std::string, std::string> m{{"X", /*->*/ "Out"}};
    return m;
C
chengduo 已提交
345 346 347
  }
};

Y
Yu Yang 已提交
348
void Pool3dOpMaker::Make() {
K
kexinzhao 已提交
349 350
  AddInput("X",
           "(Tensor) The input tensor of pooling operator. "
351 352
           "The format of input tensor is NCDHW or NDHWC, where N is batch "
           "size, C is "
K
kexinzhao 已提交
353 354 355
           "the number of channels, and D, H and W is the depth, height and "
           "width of "
           "the feature, respectively.");
356
  AddOutput("Out",
C
chengduoZH 已提交
357
            "(Tensor) The output tensor of pooling operator."
358
            "The format of output tensor is also NCDHW or NDHWC, "
K
kexinzhao 已提交
359 360
            "where N is batch size, C is "
            "the number of channels, and D, H and W is the depth, height and "
361
            "width of the feature, respectively.");
362

C
chengduoZH 已提交
363
  AddAttr<std::string>("pooling_type",
K
kexinzhao 已提交
364
                       "(string) Pooling type, can be \"max\" for max-pooling "
C
chengduoZH 已提交
365
                       "and \"avg\" for average-pooling.")
366
      .InEnum({"max", "avg"});
K
kexinzhao 已提交
367 368 369 370
  AddAttr<std::vector<int>>(
      "ksize",
      "(vector<int>) The pooling window size(depth, height, "
      "width) of pooling operator. "
C
chengduoZH 已提交
371
      "If global_pooling = true, ksize and paddings will "
K
kexinzhao 已提交
372 373
      "be ignored.");  // TODO(Chengduo): Add checker.
                       // (Currently,
C
fix bug  
chengduoZH 已提交
374
  // TypedAttrChecker don't support vector type.)
C
chengduoZH 已提交
375 376
  AddAttr<bool>(
      "global_pooling",
K
Kaipeng Deng 已提交
377 378 379
      "(bool) Whether to use the global pooling. "
      "If global_pooling = true, kernel size and paddings will be ignored. "
      "Default False")
380
      .SetDefault(false);
K
kexinzhao 已提交
381 382 383 384
  AddAttr<std::vector<int>>(
      "strides",
      "(vector<int>, default {1,1,1}) Strides(depth, height, "
      "width) of the pooling operator.")
385 386
      .SetDefault({1, 1, 1});  // TODO(Chengduo): Add checker. (Currently,
                               // TypedAttrChecker don't support vector type.)
C
fix bug  
chengduoZH 已提交
387 388
  AddAttr<std::vector<int>>(
      "paddings",
389 390 391 392
      "(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 已提交
393
      "If global_pooling = true, ksize and paddings will be ignored.")
394 395
      .SetDefault({0, 0, 0});  // TODO(Chengduo): Add checker. (Currently,
                               // TypedAttrChecker don't support vector type.)
396 397
  AddAttr<bool>(
      "exclusive",
K
Kaipeng Deng 已提交
398
      "(bool) When true, will exclude the zero-padding in the "
399
      "averaging calculating, otherwise, include the zero-padding. Note, it "
K
Kaipeng Deng 已提交
400 401
      "is only used when pooling_type is avg. The default is True. "
      "Default True")
402
      .SetDefault(true);
403 404
  AddAttr<bool>(
      "adaptive",
K
Kaipeng Deng 已提交
405
      "(bool) When true, will perform adaptive pooling instead, "
406 407
      "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 已提交
408 409
      "pooling in each grid area to get output pooling value. "
      "Default False")
410
      .SetDefault(false);
411

412 413
  AddAttr<bool>(
      "use_cudnn",
K
Kaipeng Deng 已提交
414
      "(bool) Only used in cudnn kernel, need install cudnn. Default False")
D
Double_V 已提交
415 416
      .SetDefault(false)
      .AsExtra();
417 418
  AddAttr<bool>(
      "ceil_mode",
K
Kaipeng Deng 已提交
419
      "(bool) Whether to use the ceil function to calculate "
W
wanghaoshuang 已提交
420
      "output height and width. False is the default. If it is set to False, "
K
Kaipeng Deng 已提交
421
      "the floor function will be used. Default False")
422
      .SetDefault(false);
423
  AddAttr<bool>("use_mkldnn",
K
Kaipeng Deng 已提交
424
                "(bool) Only used in mkldnn kernel. Default False")
D
Double_V 已提交
425 426
      .SetDefault(false)
      .AsExtra();
427 428
  AddAttr<std::string>(
      "data_format",
429 430 431
      "(string, default NCDHW) Only used in "
      "An optional string from: \"NDHWC\", \"NCDHW\". "
      "Defaults to \"NDHWC\". Specify the data format of the output data, "
432
      "the input will be transformed automatically. ")
433 434 435 436 437 438 439
      .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");
440 441
  // TODO(dzhwinter): need to registered layout transform function

442
  AddComment(R"DOC(
K
Kaipeng Deng 已提交
443 444
This operation calculates the output based on
the input, pooling_type, pool_size, pool_stride, and pool_padding parameters.
445
Input(X) and output(Out) are in NCDHW or NDHWC format, where N is batch
K
kexinzhao 已提交
446
size, C is the number of channels, and D, H and W are the depth, height and
K
Kaipeng Deng 已提交
447 448
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 已提交
449
width, respectively. The input(X) size and output(Out) size may be different.
C
chengduoZH 已提交
450 451 452

Example:
  Input:
K
kexinzhao 已提交
453
       X shape: $(N, C, D_{in}, H_{in}, W_{in})$
C
chengduoZH 已提交
454
  Output:
K
kexinzhao 已提交
455
       Out shape: $(N, C, D_{out}, H_{out}, W_{out})$
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

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

479
  For ceil_mode = false:
480
       $$
481
       D_{out} = \\frac{(D_{in} - ksize[0] + pad_depth_front + pad_depth_back)}{strides[0]} + 1
482 483
       $$
       $$
484
       H_{out} = \\frac{(H_{in} - ksize[1] + pad_height_top + pad_height_bottom)}{strides[1]} + 1
485 486
       $$
       $$
487
       W_{out} = \\frac{(W_{in} - ksize[2] + pad_width_left + pad_width_right)}{strides[2]} + 1
488
       $$
489
  For ceil_mode = true:
490
       $$
491
       D_{out} = \\frac{(D_{in} - ksize[0] + pad_depth_front + pad_depth_back + strides[0] -1)}{strides[0]} + 1
492 493
       $$
       $$
494
       H_{out} = \\frac{(H_{in} - ksize[1] + pad_height_top + pad_height_bottom + strides[1] -1)}{strides[1]} + 1
495 496
       $$
       $$
497
       W_{out} = \\frac{(W_{in} - ksize[2] + pad_width_left + pad_width_right + strides[2] -1)}{strides[2]} + 1
498
       $$
D
dengkaipeng 已提交
499

500
  For exclusive = false:
501
       $$
502
       dstart = i * strides[0] - pad_depth_front
503 504 505 506 507
       $$
       $$
       dend = dstart + ksize[0]
       $$
       $$
508
       hstart = j * strides[1] - pad_height_top
509 510 511 512 513
       $$
       $$
       hend = hstart + ksize[1]
       $$
       $$
514
       wstart = k * strides[2] -  pad_width_left
515 516 517 518 519 520 521
       $$
       $$
       wend = wstart + ksize[2]
       $$
       $$
       Output(i ,j, k) = \\frac{sum(Input[dstart:dend, hstart:hend, wstart:wend])}{ksize[0] * ksize[1] * ksize[2]}
       $$
522

523
  For exclusive = true:
524
       $$
525
       dstart = max(0, i * strides[0] - pad_depth_front)
526 527 528 529 530
       $$
       $$
       dend = min(D, dstart + ksize[0])
       $$
       $$
531 532 533
       hstart = max(0, j * strides[1] - pad_height_top)
       $$
       $$
534 535 536
       hend = min(H, hstart + ksize[1])
       $$
       $$
537
       wstart = max(0, k * strides[2] - pad_width_left)
538 539 540 541 542 543 544
       $$
       $$
       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 已提交
545

546
)DOC");
547
}
548 549 550 551 552
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

F
From00 已提交
553 554 555 556 557 558 559 560
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 已提交
561 562 563
REGISTER_OPERATOR(
    pool2d, ops::PoolOp, ops::Pool2dOpMaker, ops::PoolOpInferVarType,
    paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
F
From00 已提交
564 565
    paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>,
    Pool2dInferShapeFunctor);
566 567
REGISTER_OPERATOR(pool2d_grad, ops::PoolOpGrad,
                  ops::Pool2dOpGradGradMaker<paddle::framework::OpDesc>,
F
From00 已提交
568 569 570 571 572 573 574 575 576
                  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));
577

H
hong 已提交
578 579 580
REGISTER_OPERATOR(
    pool3d, ops::PoolOp, ops::Pool3dOpMaker, ops::PoolOpInferVarType,
    paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
F
From00 已提交
581 582 583
    paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>,
    Pool3dInferShapeFunctor);
REGISTER_OPERATOR(pool3d_grad, ops::PoolOpGrad, Pool3dGradInferShapeFunctor);