From b72854389e3d7e24f24a82f2608ee470cd9b43ff Mon Sep 17 00:00:00 2001 From: chengduoZH Date: Mon, 25 Sep 2017 20:14:12 +0800 Subject: [PATCH] Fix (According to the review) --- paddle/operators/math/pooling.cc | 32 ++++++++++++------- paddle/operators/math/pooling.cu | 32 ++++++++++++------- paddle/operators/math/pooling.h | 17 +++++++--- paddle/operators/pool_op.cc | 8 ++--- paddle/operators/pool_op.h | 32 +++++++++---------- .../v2/framework/tests/test_pool2d_op.py | 14 ++++---- .../v2/framework/tests/test_pool3d_op.py | 14 ++++---- 7 files changed, 87 insertions(+), 62 deletions(-) diff --git a/paddle/operators/math/pooling.cc b/paddle/operators/math/pooling.cc index 5ce748ff08b..72b260e7298 100644 --- a/paddle/operators/math/pooling.cc +++ b/paddle/operators/math/pooling.cc @@ -137,19 +137,23 @@ class Pool2dBackwardFunctor { template class Pool2dForwardFunctor< platform::CPUPlace, paddle::operators::math::pool::maxPool, float>; template class Pool2dForwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, float>; + platform::CPUPlace, paddle::operators::math::pool::avgPool, float>; template class Pool2dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::maxPool, float>; + platform::CPUPlace, paddle::operators::math::pool::maxPoolGrad, + float>; template class Pool2dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, float>; + platform::CPUPlace, paddle::operators::math::pool::avgPoolGrad, + float>; template class Pool2dForwardFunctor< platform::CPUPlace, paddle::operators::math::pool::maxPool, double>; template class Pool2dForwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, double>; + platform::CPUPlace, paddle::operators::math::pool::avgPool, double>; template class Pool2dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::maxPool, double>; + platform::CPUPlace, paddle::operators::math::pool::maxPoolGrad, + double>; template class Pool2dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, double>; + platform::CPUPlace, paddle::operators::math::pool::avgPoolGrad, + double>; template class Pool3dForwardFunctor { @@ -302,19 +306,23 @@ class Pool3dBackwardFunctor { template class Pool3dForwardFunctor< platform::CPUPlace, paddle::operators::math::pool::maxPool, float>; template class Pool3dForwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, float>; + platform::CPUPlace, paddle::operators::math::pool::avgPool, float>; template class Pool3dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::maxPool, float>; + platform::CPUPlace, paddle::operators::math::pool::maxPoolGrad, + float>; template class Pool3dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, float>; + platform::CPUPlace, paddle::operators::math::pool::avgPoolGrad, + float>; template class Pool3dForwardFunctor< platform::CPUPlace, paddle::operators::math::pool::maxPool, double>; template class Pool3dForwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, double>; + platform::CPUPlace, paddle::operators::math::pool::avgPool, double>; template class Pool3dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::maxPool, double>; + platform::CPUPlace, paddle::operators::math::pool::maxPoolGrad, + double>; template class Pool3dBackwardFunctor< - platform::CPUPlace, paddle::operators::math::pool::avePool, double>; + platform::CPUPlace, paddle::operators::math::pool::avgPoolGrad, + double>; } // namespace math } // namespace operators } // namespace paddle diff --git a/paddle/operators/math/pooling.cu b/paddle/operators/math/pooling.cu index 09e6bd9000a..619fd64b979 100644 --- a/paddle/operators/math/pooling.cu +++ b/paddle/operators/math/pooling.cu @@ -190,19 +190,23 @@ class Pool2dBackwardFunctor { template class Pool2dForwardFunctor< platform::GPUPlace, paddle::operators::math::pool::maxPool, float>; template class Pool2dForwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, float>; + platform::GPUPlace, paddle::operators::math::pool::avgPool, float>; template class Pool2dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::maxPool, float>; + platform::GPUPlace, paddle::operators::math::pool::maxPoolGrad, + float>; template class Pool2dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, float>; + platform::GPUPlace, paddle::operators::math::pool::avgPoolGrad, + float>; template class Pool2dForwardFunctor< platform::GPUPlace, paddle::operators::math::pool::maxPool, double>; template class Pool2dForwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, double>; + platform::GPUPlace, paddle::operators::math::pool::avgPool, double>; template class Pool2dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::maxPool, double>; + platform::GPUPlace, paddle::operators::math::pool::maxPoolGrad, + double>; template class Pool2dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, double>; + platform::GPUPlace, paddle::operators::math::pool::avgPoolGrad, + double>; template __global__ void KernelPool3DForward( @@ -414,19 +418,23 @@ class Pool3dBackwardFunctor { template class Pool3dForwardFunctor< platform::GPUPlace, paddle::operators::math::pool::maxPool, float>; template class Pool3dForwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, float>; + platform::GPUPlace, paddle::operators::math::pool::avgPool, float>; template class Pool3dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::maxPool, float>; + platform::GPUPlace, paddle::operators::math::pool::maxPoolGrad, + float>; template class Pool3dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, float>; + platform::GPUPlace, paddle::operators::math::pool::avgPoolGrad, + float>; template class Pool3dForwardFunctor< platform::GPUPlace, paddle::operators::math::pool::maxPool, double>; template class Pool3dForwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, double>; + platform::GPUPlace, paddle::operators::math::pool::avgPool, double>; template class Pool3dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::maxPool, double>; + platform::GPUPlace, paddle::operators::math::pool::maxPoolGrad, + double>; template class Pool3dBackwardFunctor< - platform::GPUPlace, paddle::operators::math::pool::avePool, double>; + platform::GPUPlace, paddle::operators::math::pool::avgPoolGrad, + double>; } // namespace math } // namespace operators diff --git a/paddle/operators/math/pooling.h b/paddle/operators/math/pooling.h index 627ece2ca40..b975ad31af8 100644 --- a/paddle/operators/math/pooling.h +++ b/paddle/operators/math/pooling.h @@ -33,6 +33,18 @@ class maxPool { DEVICE inline T initial() { return static_cast(-FLT_MAX); } DEVICE inline void process(T& y, const T& x) { y = y > x ? y : x; } DEVICE inline void finalize(T& y, const T& poo_size) {} +}; + +template +class avgPool { + public: + DEVICE inline T initial() { return static_cast(0); } + DEVICE inline void process(T& y, const T& x) { y += x; } + DEVICE inline void finalize(T& y, const T& poo_size) { y /= poo_size; } +}; +template +class maxPoolGrad { + public: DEVICE inline void gradProcess(const T& x, const T& y, const T& dy, T& dx, T scale) { dx += dy * (x == y); @@ -40,11 +52,8 @@ class maxPool { }; template -class avePool { +class avgPoolGrad { public: - DEVICE inline T initial() { return static_cast(0); } - DEVICE inline void process(T& y, const T& x) { y += x; } - DEVICE inline void finalize(T& y, const T& poo_size) { y /= poo_size; } DEVICE inline void gradProcess(const T& x, const T& y, const T& dy, T& dx, T scale) { dx += (scale * dy); diff --git a/paddle/operators/pool_op.cc b/paddle/operators/pool_op.cc index c57922373f8..e51249beb91 100644 --- a/paddle/operators/pool_op.cc +++ b/paddle/operators/pool_op.cc @@ -41,8 +41,8 @@ class PoolOp : public framework::OperatorWithKernel { std::vector strides = Attr>("strides"); std::vector paddings = Attr>("paddings"); - PADDLE_ENFORCE(pooling_type == "max" || pooling_type == "ave", - "pooling_type should be 'max' or 'ave'"); + PADDLE_ENFORCE(pooling_type == "max" || pooling_type == "avg", + "pooling_type should be 'max' or 'avg'"); PADDLE_ENFORCE(in_X->dims().size() == 4 || in_X->dims().size() == 5, "Pooling intput should be 4-D or 5-D"); @@ -99,7 +99,7 @@ class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker { AddAttr("poolingType", "poolingType of pooling operator." - "str constant equal to 'max' or 'ave'"); + "str constant equal to 'max' or 'avg'"); AddAttr>( "ksize", "pooling size(height, width) of pooling operator."); AddAttr( @@ -140,7 +140,7 @@ class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker { AddAttr("poolingType", "poolingType of pooling operator." - "str constant equal to 'max' or 'ave'"); + "str constant equal to 'max' or 'avg'"); AddAttr>( "ksize", "pooling size(depth, height, width) of pooling operator."); AddAttr( diff --git a/paddle/operators/pool_op.h b/paddle/operators/pool_op.h index 5a40f76172b..6f1d05272cd 100644 --- a/paddle/operators/pool_op.h +++ b/paddle/operators/pool_op.h @@ -52,11 +52,11 @@ class PoolKernel : public framework::OpKernel { pool2d_forward(context.device_context(), *in_X, *out, ksize, strides, paddings, pool_process); - } else if (pooling_type == "ave") { + } else if (pooling_type == "avg") { paddle::operators::math::Pool2dForwardFunctor< - Place, paddle::operators::math::pool::avePool, T> + Place, paddle::operators::math::pool::avgPool, T> pool2d_forward; - paddle::operators::math::pool::avePool pool_process; + paddle::operators::math::pool::avgPool pool_process; pool2d_forward(context.device_context(), *in_X, *out, ksize, strides, paddings, pool_process); } @@ -69,11 +69,11 @@ class PoolKernel : public framework::OpKernel { paddle::operators::math::pool::maxPool pool_process; pool3d_forward(context.device_context(), *in_X, *out, ksize, strides, paddings, pool_process); - } else if (pooling_type == "ave") { + } else if (pooling_type == "avg") { paddle::operators::math::Pool3dForwardFunctor< - Place, paddle::operators::math::pool::avePool, T> + Place, paddle::operators::math::pool::avgPool, T> pool3d_forward; - paddle::operators::math::pool::avePool pool_process; + paddle::operators::math::pool::avgPool pool_process; pool3d_forward(context.device_context(), *in_X, *out, ksize, strides, paddings, pool_process); } @@ -112,16 +112,16 @@ class PoolGradKernel : public framework::OpKernel { case 2: { if (pooling_type == "max") { paddle::operators::math::Pool2dBackwardFunctor< - Place, paddle::operators::math::pool::maxPool, T> + Place, paddle::operators::math::pool::maxPoolGrad, T> pool2d_backward; - paddle::operators::math::pool::maxPool pool_process; + paddle::operators::math::pool::maxPoolGrad pool_process; pool2d_backward(context.device_context(), *in_X, *in_X_grad, *out, *out_grad, ksize, strides, paddings, pool_process); - } else if (pooling_type == "ave") { + } else if (pooling_type == "avg") { paddle::operators::math::Pool2dBackwardFunctor< - Place, paddle::operators::math::pool::avePool, T> + Place, paddle::operators::math::pool::avgPoolGrad, T> pool2d_backward; - paddle::operators::math::pool::avePool pool_process; + paddle::operators::math::pool::avgPoolGrad pool_process; pool2d_backward(context.device_context(), *in_X, *in_X_grad, *out, *out_grad, ksize, strides, paddings, pool_process); } @@ -129,16 +129,16 @@ class PoolGradKernel : public framework::OpKernel { case 3: { if (pooling_type == "max") { paddle::operators::math::Pool3dBackwardFunctor< - Place, paddle::operators::math::pool::maxPool, T> + Place, paddle::operators::math::pool::maxPoolGrad, T> pool3d_backward; - paddle::operators::math::pool::maxPool pool_process; + paddle::operators::math::pool::maxPoolGrad pool_process; pool3d_backward(context.device_context(), *in_X, *in_X_grad, *out, *out_grad, ksize, strides, paddings, pool_process); - } else if (pooling_type == "ave") { + } else if (pooling_type == "avg") { paddle::operators::math::Pool3dBackwardFunctor< - Place, paddle::operators::math::pool::avePool, T> + Place, paddle::operators::math::pool::avgPoolGrad, T> pool3d_backward; - paddle::operators::math::pool::avePool pool_process; + paddle::operators::math::pool::avgPoolGrad pool_process; pool3d_backward(context.device_context(), *in_X, *in_X_grad, *out, *out_grad, ksize, strides, paddings, pool_process); } diff --git a/python/paddle/v2/framework/tests/test_pool2d_op.py b/python/paddle/v2/framework/tests/test_pool2d_op.py index 0efddc28814..e8e5218073e 100644 --- a/python/paddle/v2/framework/tests/test_pool2d_op.py +++ b/python/paddle/v2/framework/tests/test_pool2d_op.py @@ -23,7 +23,7 @@ def max_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): return out -def ave_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): +def avg_pool2D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): N, C, H, W = x.shape if global_pool == 1: @@ -72,8 +72,8 @@ class TestPool2d_Op(OpTest): def initTestCase(self): self.global_pool = 0 - self.pool_type = "ave" - self.pool2D_forward_naive = ave_pool2D_forward_naive + self.pool_type = "avg" + self.pool2D_forward_naive = avg_pool2D_forward_naive self.shape = [2, 3, 5, 5] self.ksize = [3, 3] self.strides = [1, 1] @@ -84,8 +84,8 @@ class TestCase1(TestPool2d_Op): def initTestCase(self): self.global_pool = 0 self.op_type = "pool2d" - self.pool_type = "ave" - self.pool2D_forward_naive = ave_pool2D_forward_naive + self.pool_type = "avg" + self.pool2D_forward_naive = avg_pool2D_forward_naive self.shape = [2, 3, 5, 5] self.ksize = [3, 3] self.strides = [1, 1] @@ -96,8 +96,8 @@ class TestCase2(TestPool2d_Op): def initTestCase(self): self.global_pool = 1 self.op_type = "pool2d" - self.pool_type = "ave" - self.pool2D_forward_naive = ave_pool2D_forward_naive + self.pool_type = "avg" + self.pool2D_forward_naive = avg_pool2D_forward_naive self.shape = [2, 3, 5, 5] self.ksize = [3, 3] self.strides = [1, 1] diff --git a/python/paddle/v2/framework/tests/test_pool3d_op.py b/python/paddle/v2/framework/tests/test_pool3d_op.py index 4ba3d754d53..2a3de89a709 100644 --- a/python/paddle/v2/framework/tests/test_pool3d_op.py +++ b/python/paddle/v2/framework/tests/test_pool3d_op.py @@ -27,7 +27,7 @@ def max_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): return out -def ave_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): +def avg_pool3D_forward_naive(x, ksize, strides, paddings=[0, 0], global_pool=0): N, C, D, H, W = x.shape if global_pool == 1: @@ -80,8 +80,8 @@ class TestPool3d_Op(OpTest): def initTestCase(self): self.global_pool = 0 - self.pool_type = "ave" - self.pool3D_forward_naive = ave_pool3D_forward_naive + self.pool_type = "avg" + self.pool3D_forward_naive = avg_pool3D_forward_naive self.shape = [2, 3, 5, 5, 5] self.ksize = [3, 3, 3] self.strides = [1, 1, 1] @@ -92,8 +92,8 @@ class TestCase1(TestPool3d_Op): def initTestCase(self): self.global_pool = 0 self.op_type = "pool3d" - self.pool_type = "ave" - self.pool3D_forward_naive = ave_pool3D_forward_naive + self.pool_type = "avg" + self.pool3D_forward_naive = avg_pool3D_forward_naive self.shape = [2, 3, 7, 7, 7] self.ksize = [3, 3, 3] self.strides = [1, 1, 1] @@ -104,8 +104,8 @@ class TestCase2(TestPool3d_Op): def initTestCase(self): self.global_pool = 1 self.op_type = "pool3d" - self.pool_type = "ave" - self.pool3D_forward_naive = ave_pool3D_forward_naive + self.pool_type = "avg" + self.pool3D_forward_naive = avg_pool3D_forward_naive self.shape = [2, 3, 7, 7, 7] self.ksize = [3, 3, 3] self.strides = [1, 1, 1] -- GitLab