pool_op.cc 21.5 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
framework::OpKernelType PoolOp::GetKernelTypeForVar(
66 67
    const std::string& var_name,
    const Tensor& tensor,
68 69 70 71 72 73 74 75 76 77 78
    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) {
79 80
      return framework::OpKernelType(
          expected_kernel_type.data_type_, tensor.place(), dl);
81 82 83
    }
  }
#endif
84 85
  return framework::OpKernelType(
      expected_kernel_type.data_type_, tensor.place(), tensor.layout());
86 87
}

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

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

109 110
  return framework::OpKernelType(
      input_data_type, ctx.GetPlace(), layout_, library_);
111 112
}

113
framework::OpKernelType PoolOpGrad::GetKernelTypeForVar(
114 115
    const std::string& var_name,
    const Tensor& tensor,
116 117 118 119 120 121 122 123 124 125 126 127
    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
128 129
  return framework::OpKernelType(
      expected_kernel_type.data_type_, tensor.place(), tensor.layout());
130 131
}

Y
Yu Yang 已提交
132
void Pool2dOpMaker::Make() {
133 134
  AddInput(
      "X",
C
chengduoZH 已提交
135
      "(Tensor) The input tensor of pooling operator. "
K
kexinzhao 已提交
136 137 138
      "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.");
139
  AddOutput("Out",
K
kexinzhao 已提交
140 141 142 143
            "(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, "
144
            "and W is the width of the feature.");
145

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

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

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

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

248
Example:
F
fengjiayi 已提交
249

C
chengduoZH 已提交
250
  Input:
F
fengjiayi 已提交
251

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

C
chengduoZH 已提交
254
  Output:
F
fengjiayi 已提交
255

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

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

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

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

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

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

324
)DOC");
325 326
}

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

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

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

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

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

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

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

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

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

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

548
)DOC");
549
}
550 551 552 553 554
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

555 556
DECLARE_INFER_SHAPE_FUNCTOR(pool2d,
                            Pool2dInferShapeFunctor,
F
From00 已提交
557
                            PD_INFER_META(phi::PoolInferMeta));
558 559
DECLARE_INFER_SHAPE_FUNCTOR(pool2d_grad,
                            Pool2dGradInferShapeFunctor,
F
From00 已提交
560 561 562 563 564
                            PD_INFER_META(phi::PoolGradInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(pool2d_double_grad,
                            Pool2dDoubleGradInferShapeFunctor,
                            PD_INFER_META(phi::PoolInferMeta));

H
hong 已提交
565
REGISTER_OPERATOR(
566 567 568 569
    pool2d,
    ops::PoolOp,
    ops::Pool2dOpMaker,
    ops::PoolOpInferVarType,
H
hong 已提交
570
    paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
F
From00 已提交
571 572
    paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>,
    Pool2dInferShapeFunctor);
573 574
REGISTER_OPERATOR(pool2d_grad,
                  ops::PoolOpGrad,
575
                  ops::Pool2dOpGradGradMaker<paddle::framework::OpDesc>,
F
From00 已提交
576 577
                  ops::Pool2dOpGradGradMaker<paddle::imperative::OpBase>,
                  Pool2dGradInferShapeFunctor);
578 579
REGISTER_OPERATOR(pool2d_double_grad,
                  ops::PoolOp,
F
From00 已提交
580 581
                  Pool2dDoubleGradInferShapeFunctor);

582 583
DECLARE_INFER_SHAPE_FUNCTOR(pool3d,
                            Pool3dInferShapeFunctor,
F
From00 已提交
584
                            PD_INFER_META(phi::PoolInferMeta));
585 586
DECLARE_INFER_SHAPE_FUNCTOR(pool3d_grad,
                            Pool3dGradInferShapeFunctor,
F
From00 已提交
587
                            PD_INFER_META(phi::PoolGradInferMeta));
588

H
hong 已提交
589
REGISTER_OPERATOR(
590 591 592 593
    pool3d,
    ops::PoolOp,
    ops::Pool3dOpMaker,
    ops::PoolOpInferVarType,
H
hong 已提交
594
    paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
F
From00 已提交
595 596 597
    paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>,
    Pool3dInferShapeFunctor);
REGISTER_OPERATOR(pool3d_grad, ops::PoolOpGrad, Pool3dGradInferShapeFunctor);