From 8f1e39882483127cbf8985818dd8a65149c7ea17 Mon Sep 17 00:00:00 2001 From: dengkaipeng Date: Mon, 29 Oct 2018 13:37:07 +0800 Subject: [PATCH] move param exclusive to the last in pool2d/pool3d for forward compatibility:. test=develop --- paddle/fluid/API.spec | 4 +-- paddle/fluid/operators/math/pooling.cc | 28 +++++++++-------- paddle/fluid/operators/math/pooling.cu | 30 +++++++++---------- paddle/fluid/operators/pool_cudnn_op.cu.cc | 6 ++-- python/paddle/fluid/layers/nn.py | 16 +++++----- .../fluid/tests/unittests/test_pool2d_op.py | 11 ++++--- .../fluid/tests/unittests/test_pool3d_op.py | 18 ++++++----- 7 files changed, 62 insertions(+), 51 deletions(-) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 0d90bf3cc12..a7b9ba261ce 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -67,8 +67,8 @@ paddle.fluid.layers.conv3d ArgSpec(args=['input', 'num_filters', 'filter_size', paddle.fluid.layers.sequence_pool ArgSpec(args=['input', 'pool_type'], varargs=None, keywords=None, defaults=None) paddle.fluid.layers.sequence_softmax ArgSpec(args=['input', 'use_cudnn', 'name'], varargs=None, keywords=None, defaults=(False, None)) paddle.fluid.layers.softmax ArgSpec(args=['input', 'use_cudnn', 'name'], varargs=None, keywords=None, defaults=(True, None)) -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)) -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)) +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)) +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)) 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)) paddle.fluid.layers.beam_search_decode ArgSpec(args=['ids', 'scores', 'beam_size', 'end_id', 'name'], varargs=None, keywords=None, defaults=(None,)) 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)) diff --git a/paddle/fluid/operators/math/pooling.cc b/paddle/fluid/operators/math/pooling.cc index dba687be951..8df43bb6161 100644 --- a/paddle/fluid/operators/math/pooling.cc +++ b/paddle/fluid/operators/math/pooling.cc @@ -29,9 +29,9 @@ class Pool2dFunctor { public: void operator()(const platform::CPUDeviceContext& context, const framework::Tensor& input, const std::vector& ksize, - const std::vector& strides, const std::vector& paddings, - PoolProcess pool_process, bool exclusive, - framework::Tensor* output) { + const std::vector& strides, + const std::vector& paddings, PoolProcess pool_process, + bool exclusive, framework::Tensor* output) { const int batch_size = input.dims()[0]; const int input_height = input.dims()[2]; const int input_width = input.dims()[3]; @@ -69,7 +69,7 @@ class Pool2dFunctor { } } int pool_size = exclusive ? (hend - hstart) * (wend - wstart) - : ksize_height * ksize_width; + : ksize_height * ksize_width; pool_process.finalize(static_cast(pool_size), &ele); output_data[ph * output_width + pw] = ele; } @@ -126,7 +126,7 @@ class Pool2dGradFunctor { int wend = std::min(wstart + ksize_width, input_width); wstart = std::max(wstart, 0); int pool_size = exclusive ? (hend - hstart) * (wend - wstart) - : ksize_height * ksize_width; + : ksize_height * ksize_width; float scale = 1.0 / pool_size; for (int h = hstart; h < hend; ++h) { for (int w = wstart; w < wend; ++w) { @@ -249,8 +249,8 @@ class Pool3dFunctor { public: void operator()(const platform::CPUDeviceContext& context, const framework::Tensor& input, const std::vector& ksize, - const std::vector& strides, const std::vector& paddings, - PoolProcess pool_process, + const std::vector& strides, + const std::vector& paddings, PoolProcess pool_process, bool exclusive, framework::Tensor* output) { const int batch_size = input.dims()[0]; const int input_depth = input.dims()[2]; @@ -301,9 +301,10 @@ class Pool3dFunctor { } } } - int pool_size = exclusive ? - (dend - dstart) * (hend - hstart) * (wend - wstart) - : ksize_depth * ksize_height * ksize_width; + int pool_size = + exclusive + ? (dend - dstart) * (hend - hstart) * (wend - wstart) + : ksize_depth * ksize_height * ksize_width; pool_process.finalize(static_cast(pool_size), &ele); output_data[output_idx] = ele; } @@ -371,9 +372,10 @@ class Pool3dGradFunctor { int wend = std::min(wstart + ksize_width, input_width); wstart = std::max(wstart, 0); - int pool_size = exclusive ? - (dend - dstart) * (hend - hstart) * (wend - wstart) - : ksize_depth * ksize_height * ksize_width; + int pool_size = + exclusive + ? (dend - dstart) * (hend - hstart) * (wend - wstart) + : ksize_depth * ksize_height * ksize_width; float scale = 1.0 / pool_size; for (int d = dstart; d < dend; ++d) { for (int h = hstart; h < hend; ++h) { diff --git a/paddle/fluid/operators/math/pooling.cu b/paddle/fluid/operators/math/pooling.cu index 437d7039abb..a689eb42242 100644 --- a/paddle/fluid/operators/math/pooling.cu +++ b/paddle/fluid/operators/math/pooling.cu @@ -53,7 +53,7 @@ __global__ void KernelPool2D(const int nthreads, const T* input_data, } } int pool_size = exclusive ? (hend - hstart) * (wend - wstart) - : ksize_height * ksize_width; + : ksize_height * ksize_width; pool_process.finalize(static_cast(pool_size), &ele); output_data[index] = ele; } @@ -97,7 +97,7 @@ __global__ void KernelPool2DGrad( hstart = max(hstart, 0); wstart = max(wstart, 0); int pool_size = exclusive ? (hend - hstart) * (wend - wstart) - : ksize_height * ksize_width; + : ksize_height * ksize_width; int output_sub_idx = ph * output_width + pw; pool_process.compute(input, output_data[output_sub_idx], output_grad[output_sub_idx], @@ -191,7 +191,7 @@ class Pool2dFunctor { KernelPool2D<<>>( nthreads, input_data, input_channels, input_height, input_width, output_height, output_width, ksize_height, ksize_width, stride_height, - stride_width, padding_height, padding_width, pool_process, exclusive, + stride_width, padding_height, padding_width, pool_process, exclusive, output_data); } }; @@ -317,11 +317,11 @@ template class Pool2dGradFunctor __global__ void KernelPool3D( - const int nthreads, const T* input_data, const int channels, - const int input_depth, const int input_height, const int input_width, - const int output_depth, const int output_height, const int output_width, + const int nthreads, const T* input_data, const int channels, + const int input_depth, const int input_height, const int input_width, + const int output_depth, const int output_height, const int output_width, const int ksize_depth, const int ksize_height, const int ksize_width, - const int stride_depth, const int stride_height, const int stride_width, + const int stride_depth, const int stride_height, const int stride_width, const int padding_depth, const int padding_height, const int padding_width, PoolProcess pool_process, bool exclusive, T* output_data) { for (int index = blockIdx.x * blockDim.x + threadIdx.x; index < nthreads; @@ -352,9 +352,9 @@ __global__ void KernelPool3D( } } } - int pool_size = exclusive ? - (dend - dstart) * (hend - hstart) * (wend - wstart) - : ksize_depth * ksize_height * ksize_width; + int pool_size = exclusive + ? (dend - dstart) * (hend - hstart) * (wend - wstart) + : ksize_depth * ksize_height * ksize_width; pool_process.finalize(static_cast(pool_size), &ele); output_data[index] = ele; } @@ -412,9 +412,9 @@ __global__ void KernelPool3DGrad( dstart = max(dstart, 0); hstart = max(hstart, 0); wstart = max(wstart, 0); - int pool_size = exclusive ? - (dend - dstart) * (hend - hstart) * (wend - wstart) - : ksize_depth * ksize_height * ksize_width; + int pool_size = + exclusive ? (dend - dstart) * (hend - hstart) * (wend - wstart) + : ksize_depth * ksize_height * ksize_width; int output_sub_idx = (pd * output_height + ph) * output_width + pw; pool_process.compute(input, output_data[output_sub_idx], output_grad[output_sub_idx], @@ -522,8 +522,8 @@ class Pool3dFunctor { nthreads, input_data, input_channels, input_depth, input_height, input_width, output_depth, output_height, output_width, ksize_depth, ksize_height, ksize_width, stride_depth, stride_height, stride_width, - padding_depth, padding_height, padding_width, pool_process, - exclusive, output_data); + padding_depth, padding_height, padding_width, pool_process, exclusive, + output_data); } }; diff --git a/paddle/fluid/operators/pool_cudnn_op.cu.cc b/paddle/fluid/operators/pool_cudnn_op.cu.cc index 4365805b96d..1f090dc3d54 100644 --- a/paddle/fluid/operators/pool_cudnn_op.cu.cc +++ b/paddle/fluid/operators/pool_cudnn_op.cu.cc @@ -73,7 +73,8 @@ class PoolCUDNNOpKernel : public framework::OpKernel { if (pooling_type == "max") { pooling_mode = PoolingMode::kMaximum; } else { - pooling_mode = exclusive ? PoolingMode::kAverageExclusive : PoolingMode::kAverageInclusive; + pooling_mode = exclusive ? PoolingMode::kAverageExclusive + : PoolingMode::kAverageInclusive; } cudnnPoolingDescriptor_t cudnn_pool_desc = @@ -143,7 +144,8 @@ class PoolCUDNNGradOpKernel : public framework::OpKernel { pooling_mode = PoolingMode::kMaximum; } } else { - pooling_mode = exclusive ? PoolingMode::kAverageExclusive : PoolingMode::kAverageInclusive; + pooling_mode = exclusive ? PoolingMode::kAverageExclusive + : PoolingMode::kAverageInclusive; } cudnnPoolingDescriptor_t cudnn_pool_desc = diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 6920848132c..de6610571c5 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -2067,8 +2067,8 @@ def pool2d(input, global_pooling=False, use_cudnn=True, ceil_mode=False, - exclusive=True, - name=None): + name=None, + exclusive=True): """ ${comment} @@ -2085,10 +2085,10 @@ def pool2d(input, global_pooling (bool): ${global_pooling_comment} use_cudnn (bool): ${use_cudnn_comment} ceil_mode (bool): ${ceil_mode_comment} - exclusive (bool): Whether to exclude padding points in average pooling - mode, default is true name (str|None): A name for this layer(optional). If set None, the layer will be named automatically. + exclusive (bool): Whether to exclude padding points in average pooling + mode, default is true Returns: Variable: The pooling result. @@ -2161,8 +2161,8 @@ def pool3d(input, global_pooling=False, use_cudnn=True, ceil_mode=False, - exclusive=True, - name=None): + name=None, + exclusive=True): """ This function adds the operator for pooling in 3-dimensions, using the pooling configurations mentioned in input parameters. @@ -2176,10 +2176,10 @@ def pool3d(input, global_pooling (bool): ${global_pooling_comment} use_cudnn (bool): ${use_cudnn_comment} ceil_mode (bool): ${ceil_mode_comment} - exclusive (bool): Whether to exclude padding points in average pooling - mode, default is true name (str): A name for this layer(optional). If set None, the layer will be named automatically. + exclusive (bool): Whether to exclude padding points in average pooling + mode, default is true Returns: Variable: output of pool3d layer. diff --git a/python/paddle/fluid/tests/unittests/test_pool2d_op.py b/python/paddle/fluid/tests/unittests/test_pool2d_op.py index c627336f463..634df65bb5a 100644 --- a/python/paddle/fluid/tests/unittests/test_pool2d_op.py +++ b/python/paddle/fluid/tests/unittests/test_pool2d_op.py @@ -96,9 +96,9 @@ class TestPool2d_Op(OpTest): if self.global_pool: self.paddings = [0 for _ in range(len(self.paddings))] input = np.random.random(self.shape).astype(self.dtype) - output = self.pool2D_forward_naive(input, self.ksize, self.strides, - self.paddings, self.global_pool, - self.ceil_mode, self.exclusive).astype(self.dtype) + output = self.pool2D_forward_naive( + input, self.ksize, self.strides, self.paddings, self.global_pool, + self.ceil_mode, self.exclusive).astype(self.dtype) self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)} self.attrs = { @@ -110,7 +110,8 @@ class TestPool2d_Op(OpTest): 'use_cudnn': self.use_cudnn, 'use_mkldnn': self.use_mkldnn, 'ceil_mode': self.ceil_mode, - 'data_format': 'AnyLayout', # TODO(dzhwinter) : should be fix latter + 'data_format': + 'AnyLayout', # TODO(dzhwinter) : should be fix latter 'exclusive': self.exclusive } @@ -329,10 +330,12 @@ class TestCeilModeCase4(TestCase2): def init_ceil_mode(self): self.ceil_mode = True + class TestAvgInclude(TestCase2): def init_exclusive(self): self.exclusive = False + class TestCUDNNAvgInclude(TestCUDNNCase3): def init_exclusive(self): self.exclusive = False diff --git a/python/paddle/fluid/tests/unittests/test_pool3d_op.py b/python/paddle/fluid/tests/unittests/test_pool3d_op.py index 20dc2eefa0f..f05f8ccb398 100644 --- a/python/paddle/fluid/tests/unittests/test_pool3d_op.py +++ b/python/paddle/fluid/tests/unittests/test_pool3d_op.py @@ -89,7 +89,8 @@ def avg_pool3D_forward_naive(x, field_size = (d_end - d_start) * (h_end - h_start) * (w_end - w_start) \ if exclusive else ksize[0] * ksize[1] * ksize[2] - out[:, :, k, i, j] = np.sum(x_masked, axis=(2, 3, 4)) / field_size + out[:, :, k, i, j] = np.sum(x_masked, axis=(2, 3, + 4)) / field_size return out @@ -108,9 +109,9 @@ class TestPool3d_Op(OpTest): if self.global_pool: self.paddings = [0 for _ in range(len(self.paddings))] input = np.random.random(self.shape).astype(self.dtype) - output = self.pool3D_forward_naive(input, self.ksize, self.strides, - self.paddings, self.global_pool, - self.ceil_mode, self.exclusive).astype(self.dtype) + output = self.pool3D_forward_naive( + input, self.ksize, self.strides, self.paddings, self.global_pool, + self.ceil_mode, self.exclusive).astype(self.dtype) self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)} self.attrs = { @@ -121,8 +122,9 @@ class TestPool3d_Op(OpTest): 'global_pooling': self.global_pool, 'use_cudnn': self.use_cudnn, 'ceil_mode': self.ceil_mode, - 'data_format': 'AnyLayout', # TODO(dzhwinter) : should be fix latter - 'exclusive': self.exclusive + 'data_format': + 'AnyLayout', # TODO(dzhwinter) : should be fix latter + 'exclusive': self.exclusive } self.outputs = {'Out': output} @@ -167,7 +169,7 @@ class TestPool3d_Op(OpTest): self.ceil_mode = False def init_exclusive(self): - self.exclusive = True + self.exclusive = True class TestCase1(TestPool3d_Op): @@ -340,10 +342,12 @@ class TestCeilModeCase4(TestCase2): def init_ceil_mode(self): self.ceil_mode = True + class TestAvgInclude(TestCase2): def init_exclusive(self): self.exclusive = False + class TestCUDNNAvgInclude(TestCUDNNCase3): def init_exclusive(self): self.exclusive = False -- GitLab