Skip to content

Commit 8f1e398

Browse files
committed
move param exclusive to the last in pool2d/pool3d for forward compatibility:. test=develop
1 parent c93e044 commit 8f1e398

File tree

7 files changed

+62
-51
lines changed

7 files changed

+62
-51
lines changed

paddle/fluid/API.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ paddle.fluid.layers.conv3d ArgSpec(args=['input', 'num_filters', 'filter_size',
6767
paddle.fluid.layers.sequence_pool ArgSpec(args=['input', 'pool_type'], varargs=None, keywords=None, defaults=None)
6868
paddle.fluid.layers.sequence_softmax ArgSpec(args=['input', 'use_cudnn', 'name'], varargs=None, keywords=None, defaults=(False, None))
6969
paddle.fluid.layers.softmax ArgSpec(args=['input', 'use_cudnn', 'name'], varargs=None, keywords=None, defaults=(True, None))
70-
paddle.fluid.layers.pool2d ArgSpec(args=['input', 'pool_size', 'pool_type', 'pool_stride', 'pool_padding', 'global_pooling', 'use_cudnn', 'ceil_mode', 'name'], varargs=None, keywords=None, defaults=(-1, 'max', 1, 0, False, True, False, None))
71-
paddle.fluid.layers.pool3d ArgSpec(args=['input', 'pool_size', 'pool_type', 'pool_stride', 'pool_padding', 'global_pooling', 'use_cudnn', 'ceil_mode', 'name'], varargs=None, keywords=None, defaults=(-1, 'max', 1, 0, False, True, False, None))
70+
paddle.fluid.layers.pool2d ArgSpec(args=['input', 'pool_size', 'pool_type', 'pool_stride', 'pool_padding', 'global_pooling', 'use_cudnn', 'ceil_mode', 'name', 'exclusive'], varargs=None, keywords=None, defaults=(-1, 'max', 1, 0, False, True, False, None))
71+
paddle.fluid.layers.pool3d ArgSpec(args=['input', 'pool_size', 'pool_type', 'pool_stride', 'pool_padding', 'global_pooling', 'use_cudnn', 'ceil_mode', 'name', 'exclusive'], varargs=None, keywords=None, defaults=(-1, 'max', 1, 0, False, True, False, None))
7272
paddle.fluid.layers.batch_norm ArgSpec(args=['input', 'act', 'is_test', 'momentum', 'epsilon', 'param_attr', 'bias_attr', 'data_layout', 'in_place', 'name', 'moving_mean_name', 'moving_variance_name', 'do_model_average_for_mean_and_var', 'fuse_with_relu'], varargs=None, keywords=None, defaults=(None, False, 0.9, 1e-05, None, None, 'NCHW', False, None, None, None, False, False))
7373
paddle.fluid.layers.beam_search_decode ArgSpec(args=['ids', 'scores', 'beam_size', 'end_id', 'name'], varargs=None, keywords=None, defaults=(None,))
7474
paddle.fluid.layers.conv2d_transpose ArgSpec(args=['input', 'num_filters', 'output_size', 'filter_size', 'padding', 'stride', 'dilation', 'groups', 'param_attr', 'bias_attr', 'use_cudnn', 'act', 'name'], varargs=None, keywords=None, defaults=(None, None, 0, 1, 1, None, None, None, True, None, None))

paddle/fluid/operators/math/pooling.cc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class Pool2dFunctor<platform::CPUDeviceContext, PoolProcess, T> {
2929
public:
3030
void operator()(const platform::CPUDeviceContext& context,
3131
const framework::Tensor& input, const std::vector<int>& ksize,
32-
const std::vector<int>& strides, const std::vector<int>& paddings,
33-
PoolProcess pool_process, bool exclusive,
34-
framework::Tensor* output) {
32+
const std::vector<int>& strides,
33+
const std::vector<int>& paddings, PoolProcess pool_process,
34+
bool exclusive, framework::Tensor* output) {
3535
const int batch_size = input.dims()[0];
3636
const int input_height = input.dims()[2];
3737
const int input_width = input.dims()[3];
@@ -69,7 +69,7 @@ class Pool2dFunctor<platform::CPUDeviceContext, PoolProcess, T> {
6969
}
7070
}
7171
int pool_size = exclusive ? (hend - hstart) * (wend - wstart)
72-
: ksize_height * ksize_width;
72+
: ksize_height * ksize_width;
7373
pool_process.finalize(static_cast<T>(pool_size), &ele);
7474
output_data[ph * output_width + pw] = ele;
7575
}
@@ -126,7 +126,7 @@ class Pool2dGradFunctor<platform::CPUDeviceContext, PoolProcess, T> {
126126
int wend = std::min(wstart + ksize_width, input_width);
127127
wstart = std::max(wstart, 0);
128128
int pool_size = exclusive ? (hend - hstart) * (wend - wstart)
129-
: ksize_height * ksize_width;
129+
: ksize_height * ksize_width;
130130
float scale = 1.0 / pool_size;
131131
for (int h = hstart; h < hend; ++h) {
132132
for (int w = wstart; w < wend; ++w) {
@@ -249,8 +249,8 @@ class Pool3dFunctor<platform::CPUDeviceContext, PoolProcess, T> {
249249
public:
250250
void operator()(const platform::CPUDeviceContext& context,
251251
const framework::Tensor& input, const std::vector<int>& ksize,
252-
const std::vector<int>& strides, const std::vector<int>& paddings,
253-
PoolProcess pool_process,
252+
const std::vector<int>& strides,
253+
const std::vector<int>& paddings, PoolProcess pool_process,
254254
bool exclusive, framework::Tensor* output) {
255255
const int batch_size = input.dims()[0];
256256
const int input_depth = input.dims()[2];
@@ -301,9 +301,10 @@ class Pool3dFunctor<platform::CPUDeviceContext, PoolProcess, T> {
301301
}
302302
}
303303
}
304-
int pool_size = exclusive ?
305-
(dend - dstart) * (hend - hstart) * (wend - wstart)
306-
: ksize_depth * ksize_height * ksize_width;
304+
int pool_size =
305+
exclusive
306+
? (dend - dstart) * (hend - hstart) * (wend - wstart)
307+
: ksize_depth * ksize_height * ksize_width;
307308
pool_process.finalize(static_cast<T>(pool_size), &ele);
308309
output_data[output_idx] = ele;
309310
}
@@ -371,9 +372,10 @@ class Pool3dGradFunctor<platform::CPUDeviceContext, PoolProcess, T> {
371372
int wend = std::min(wstart + ksize_width, input_width);
372373
wstart = std::max(wstart, 0);
373374

374-
int pool_size = exclusive ?
375-
(dend - dstart) * (hend - hstart) * (wend - wstart)
376-
: ksize_depth * ksize_height * ksize_width;
375+
int pool_size =
376+
exclusive
377+
? (dend - dstart) * (hend - hstart) * (wend - wstart)
378+
: ksize_depth * ksize_height * ksize_width;
377379
float scale = 1.0 / pool_size;
378380
for (int d = dstart; d < dend; ++d) {
379381
for (int h = hstart; h < hend; ++h) {

paddle/fluid/operators/math/pooling.cu

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ __global__ void KernelPool2D(const int nthreads, const T* input_data,
5353
}
5454
}
5555
int pool_size = exclusive ? (hend - hstart) * (wend - wstart)
56-
: ksize_height * ksize_width;
56+
: ksize_height * ksize_width;
5757
pool_process.finalize(static_cast<T>(pool_size), &ele);
5858
output_data[index] = ele;
5959
}
@@ -97,7 +97,7 @@ __global__ void KernelPool2DGrad(
9797
hstart = max(hstart, 0);
9898
wstart = max(wstart, 0);
9999
int pool_size = exclusive ? (hend - hstart) * (wend - wstart)
100-
: ksize_height * ksize_width;
100+
: ksize_height * ksize_width;
101101
int output_sub_idx = ph * output_width + pw;
102102
pool_process.compute(input, output_data[output_sub_idx],
103103
output_grad[output_sub_idx],
@@ -191,7 +191,7 @@ class Pool2dFunctor<platform::CUDADeviceContext, PoolProcess, T> {
191191
KernelPool2D<PoolProcess, T><<<grid, threads, 0, context.stream()>>>(
192192
nthreads, input_data, input_channels, input_height, input_width,
193193
output_height, output_width, ksize_height, ksize_width, stride_height,
194-
stride_width, padding_height, padding_width, pool_process, exclusive,
194+
stride_width, padding_height, padding_width, pool_process, exclusive,
195195
output_data);
196196
}
197197
};
@@ -317,11 +317,11 @@ template class Pool2dGradFunctor<platform::CUDADeviceContext,
317317

318318
template <typename PoolProcess, typename T>
319319
__global__ void KernelPool3D(
320-
const int nthreads, const T* input_data, const int channels,
321-
const int input_depth, const int input_height, const int input_width,
322-
const int output_depth, const int output_height, const int output_width,
320+
const int nthreads, const T* input_data, const int channels,
321+
const int input_depth, const int input_height, const int input_width,
322+
const int output_depth, const int output_height, const int output_width,
323323
const int ksize_depth, const int ksize_height, const int ksize_width,
324-
const int stride_depth, const int stride_height, const int stride_width,
324+
const int stride_depth, const int stride_height, const int stride_width,
325325
const int padding_depth, const int padding_height, const int padding_width,
326326
PoolProcess pool_process, bool exclusive, T* output_data) {
327327
for (int index = blockIdx.x * blockDim.x + threadIdx.x; index < nthreads;
@@ -352,9 +352,9 @@ __global__ void KernelPool3D(
352352
}
353353
}
354354
}
355-
int pool_size = exclusive ?
356-
(dend - dstart) * (hend - hstart) * (wend - wstart)
357-
: ksize_depth * ksize_height * ksize_width;
355+
int pool_size = exclusive
356+
? (dend - dstart) * (hend - hstart) * (wend - wstart)
357+
: ksize_depth * ksize_height * ksize_width;
358358
pool_process.finalize(static_cast<T>(pool_size), &ele);
359359
output_data[index] = ele;
360360
}
@@ -412,9 +412,9 @@ __global__ void KernelPool3DGrad(
412412
dstart = max(dstart, 0);
413413
hstart = max(hstart, 0);
414414
wstart = max(wstart, 0);
415-
int pool_size = exclusive ?
416-
(dend - dstart) * (hend - hstart) * (wend - wstart)
417-
: ksize_depth * ksize_height * ksize_width;
415+
int pool_size =
416+
exclusive ? (dend - dstart) * (hend - hstart) * (wend - wstart)
417+
: ksize_depth * ksize_height * ksize_width;
418418
int output_sub_idx = (pd * output_height + ph) * output_width + pw;
419419
pool_process.compute(input, output_data[output_sub_idx],
420420
output_grad[output_sub_idx],
@@ -522,8 +522,8 @@ class Pool3dFunctor<platform::CUDADeviceContext, PoolProcess, T> {
522522
nthreads, input_data, input_channels, input_depth, input_height,
523523
input_width, output_depth, output_height, output_width, ksize_depth,
524524
ksize_height, ksize_width, stride_depth, stride_height, stride_width,
525-
padding_depth, padding_height, padding_width, pool_process,
526-
exclusive, output_data);
525+
padding_depth, padding_height, padding_width, pool_process, exclusive,
526+
output_data);
527527
}
528528
};
529529

paddle/fluid/operators/pool_cudnn_op.cu.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class PoolCUDNNOpKernel : public framework::OpKernel<T> {
7373
if (pooling_type == "max") {
7474
pooling_mode = PoolingMode::kMaximum;
7575
} else {
76-
pooling_mode = exclusive ? PoolingMode::kAverageExclusive : PoolingMode::kAverageInclusive;
76+
pooling_mode = exclusive ? PoolingMode::kAverageExclusive
77+
: PoolingMode::kAverageInclusive;
7778
}
7879

7980
cudnnPoolingDescriptor_t cudnn_pool_desc =
@@ -143,7 +144,8 @@ class PoolCUDNNGradOpKernel : public framework::OpKernel<T> {
143144
pooling_mode = PoolingMode::kMaximum;
144145
}
145146
} else {
146-
pooling_mode = exclusive ? PoolingMode::kAverageExclusive : PoolingMode::kAverageInclusive;
147+
pooling_mode = exclusive ? PoolingMode::kAverageExclusive
148+
: PoolingMode::kAverageInclusive;
147149
}
148150

149151
cudnnPoolingDescriptor_t cudnn_pool_desc =

python/paddle/fluid/layers/nn.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,8 +2067,8 @@ def pool2d(input,
20672067
global_pooling=False,
20682068
use_cudnn=True,
20692069
ceil_mode=False,
2070-
exclusive=True,
2071-
name=None):
2070+
name=None,
2071+
exclusive=True):
20722072
"""
20732073
${comment}
20742074
@@ -2085,10 +2085,10 @@ def pool2d(input,
20852085
global_pooling (bool): ${global_pooling_comment}
20862086
use_cudnn (bool): ${use_cudnn_comment}
20872087
ceil_mode (bool): ${ceil_mode_comment}
2088-
exclusive (bool): Whether to exclude padding points in average pooling
2089-
mode, default is true
20902088
name (str|None): A name for this layer(optional). If set None, the
20912089
layer will be named automatically.
2090+
exclusive (bool): Whether to exclude padding points in average pooling
2091+
mode, default is true
20922092
20932093
Returns:
20942094
Variable: The pooling result.
@@ -2161,8 +2161,8 @@ def pool3d(input,
21612161
global_pooling=False,
21622162
use_cudnn=True,
21632163
ceil_mode=False,
2164-
exclusive=True,
2165-
name=None):
2164+
name=None,
2165+
exclusive=True):
21662166
"""
21672167
This function adds the operator for pooling in 3-dimensions, using the
21682168
pooling configurations mentioned in input parameters.
@@ -2176,10 +2176,10 @@ def pool3d(input,
21762176
global_pooling (bool): ${global_pooling_comment}
21772177
use_cudnn (bool): ${use_cudnn_comment}
21782178
ceil_mode (bool): ${ceil_mode_comment}
2179-
exclusive (bool): Whether to exclude padding points in average pooling
2180-
mode, default is true
21812179
name (str): A name for this layer(optional). If set None, the layer
21822180
will be named automatically.
2181+
exclusive (bool): Whether to exclude padding points in average pooling
2182+
mode, default is true
21832183
21842184
Returns:
21852185
Variable: output of pool3d layer.

python/paddle/fluid/tests/unittests/test_pool2d_op.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ def setUp(self):
9696
if self.global_pool:
9797
self.paddings = [0 for _ in range(len(self.paddings))]
9898
input = np.random.random(self.shape).astype(self.dtype)
99-
output = self.pool2D_forward_naive(input, self.ksize, self.strides,
100-
self.paddings, self.global_pool,
101-
self.ceil_mode, self.exclusive).astype(self.dtype)
99+
output = self.pool2D_forward_naive(
100+
input, self.ksize, self.strides, self.paddings, self.global_pool,
101+
self.ceil_mode, self.exclusive).astype(self.dtype)
102102
self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)}
103103

104104
self.attrs = {
@@ -110,7 +110,8 @@ def setUp(self):
110110
'use_cudnn': self.use_cudnn,
111111
'use_mkldnn': self.use_mkldnn,
112112
'ceil_mode': self.ceil_mode,
113-
'data_format': 'AnyLayout', # TODO(dzhwinter) : should be fix latter
113+
'data_format':
114+
'AnyLayout', # TODO(dzhwinter) : should be fix latter
114115
'exclusive': self.exclusive
115116
}
116117

@@ -329,10 +330,12 @@ class TestCeilModeCase4(TestCase2):
329330
def init_ceil_mode(self):
330331
self.ceil_mode = True
331332

333+
332334
class TestAvgInclude(TestCase2):
333335
def init_exclusive(self):
334336
self.exclusive = False
335337

338+
336339
class TestCUDNNAvgInclude(TestCUDNNCase3):
337340
def init_exclusive(self):
338341
self.exclusive = False

python/paddle/fluid/tests/unittests/test_pool3d_op.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def avg_pool3D_forward_naive(x,
8989

9090
field_size = (d_end - d_start) * (h_end - h_start) * (w_end - w_start) \
9191
if exclusive else ksize[0] * ksize[1] * ksize[2]
92-
out[:, :, k, i, j] = np.sum(x_masked, axis=(2, 3, 4)) / field_size
92+
out[:, :, k, i, j] = np.sum(x_masked, axis=(2, 3,
93+
4)) / field_size
9394
return out
9495

9596

@@ -108,9 +109,9 @@ def setUp(self):
108109
if self.global_pool:
109110
self.paddings = [0 for _ in range(len(self.paddings))]
110111
input = np.random.random(self.shape).astype(self.dtype)
111-
output = self.pool3D_forward_naive(input, self.ksize, self.strides,
112-
self.paddings, self.global_pool,
113-
self.ceil_mode, self.exclusive).astype(self.dtype)
112+
output = self.pool3D_forward_naive(
113+
input, self.ksize, self.strides, self.paddings, self.global_pool,
114+
self.ceil_mode, self.exclusive).astype(self.dtype)
114115
self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)}
115116

116117
self.attrs = {
@@ -121,8 +122,9 @@ def setUp(self):
121122
'global_pooling': self.global_pool,
122123
'use_cudnn': self.use_cudnn,
123124
'ceil_mode': self.ceil_mode,
124-
'data_format': 'AnyLayout', # TODO(dzhwinter) : should be fix latter
125-
'exclusive': self.exclusive
125+
'data_format':
126+
'AnyLayout', # TODO(dzhwinter) : should be fix latter
127+
'exclusive': self.exclusive
126128
}
127129

128130
self.outputs = {'Out': output}
@@ -167,7 +169,7 @@ def init_ceil_mode(self):
167169
self.ceil_mode = False
168170

169171
def init_exclusive(self):
170-
self.exclusive = True
172+
self.exclusive = True
171173

172174

173175
class TestCase1(TestPool3d_Op):
@@ -340,10 +342,12 @@ class TestCeilModeCase4(TestCase2):
340342
def init_ceil_mode(self):
341343
self.ceil_mode = True
342344

345+
343346
class TestAvgInclude(TestCase2):
344347
def init_exclusive(self):
345348
self.exclusive = False
346349

350+
347351
class TestCUDNNAvgInclude(TestCUDNNCase3):
348352
def init_exclusive(self):
349353
self.exclusive = False

0 commit comments

Comments
 (0)