From 01d05bc0d6ebc364e6010ea1b7debf34ffd6b185 Mon Sep 17 00:00:00 2001 From: helen88 Date: Wed, 10 Aug 2022 15:54:01 +0800 Subject: [PATCH] fix bug of adaptive pool2d_grad, *test=kunlun (#45031) * fix bug of adaptive pool2d_grad, *test=kunlun * fix bug of adaptive pool2d_grad, *test=kunlun * fix bug of adaptive pool2d_grad, *test=kunlun --- cmake/external/xpu.cmake | 4 +-- paddle/fluid/operators/pool_op_xpu.cc | 30 ++++++++++++++----- .../tests/unittests/xpu/test_pool2d_op_xpu.py | 18 +++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/cmake/external/xpu.cmake b/cmake/external/xpu.cmake index 87f686a8ab0..c47dbe88edc 100644 --- a/cmake/external/xpu.cmake +++ b/cmake/external/xpu.cmake @@ -10,7 +10,7 @@ set(XPU_RT_LIB_NAME "libxpurt.so") if(NOT DEFINED XPU_BASE_URL) set(XPU_BASE_URL_WITHOUT_DATE "https://baidu-kunlun-product.cdn.bcebos.com/KL-SDK/klsdk-dev") - set(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220802") + set(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220810") else() set(XPU_BASE_URL "${XPU_BASE_URL}") endif() @@ -19,7 +19,7 @@ endif() if(NOT DEFINED XPU_XDNN_BASE_URL) set(XPU_XDNN_BASE_URL_WITHOUT_DATE "https://klx-sdk-release-public.su.bcebos.com/xdnn/dev") - set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL_WITHOUT_DATE}/20220802") + set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL_WITHOUT_DATE}/20220810") else() set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL}") endif() diff --git a/paddle/fluid/operators/pool_op_xpu.cc b/paddle/fluid/operators/pool_op_xpu.cc index bfa638e5bde..60f05e6d8e7 100644 --- a/paddle/fluid/operators/pool_op_xpu.cc +++ b/paddle/fluid/operators/pool_op_xpu.cc @@ -44,6 +44,15 @@ class PoolXPUKernel : public framework::OpKernel { platform::errors::InvalidArgument( "The Pool2d XPU OP only support 2 dimension pooling!")); + std::string data_format = context.Attr("data_format"); + PADDLE_ENFORCE_EQ( + data_format, + "NCHW", + platform::errors::InvalidArgument("The Pool2d XPU OP only support" + "data_format is 'NCHW', but received " + "%s", + data_format)); + int* index_data = nullptr; bool global_pooling = context.Attr("global_pooling") || (adaptive && (ksize[0] * ksize[1] == 1)); @@ -173,6 +182,16 @@ class PoolGradXPUKernel : public framework::OpKernel { bool exclusive = context.Attr("exclusive"); bool adaptive = context.Attr("adaptive"); bool ceil_mode = context.Attr("ceil_mode"); + + std::string data_format = context.Attr("data_format"); + PADDLE_ENFORCE_EQ( + data_format, + "NCHW", + platform::errors::InvalidArgument("The Pool2d_grad XPU OP only support" + "data_format is 'NCHW', but received " + "%s", + data_format)); + std::string padding_algorithm = context.Attr("padding_algorithm"); const int* index_data = nullptr; @@ -202,13 +221,6 @@ class PoolGradXPUKernel : public framework::OpKernel { const int out_h = out->dims()[2]; const int out_w = out->dims()[3]; - PADDLE_ENFORCE_EQ(!adaptive || (ksize[0] * ksize[1] == 1) || - (in_h % out_h == 0 && in_w % out_w == 0), - true, - platform::errors::InvalidArgument( - "The Pool2d XPU OP does not support (adaptive == " - "true && output_size != 1)")); - framework::DDim data_dims; data_dims = phi::slice_ddim(in_x->dims(), 2, in_x->dims().size()); @@ -234,7 +246,8 @@ class PoolGradXPUKernel : public framework::OpKernel { auto input_grad = reinterpret_cast(in_x_grad->data()); auto& dev_ctx = context.template device_context(); int r = xpu::Error_t::SUCCESS; - if (adaptive && in_h % out_h == 0 && in_w % out_w == 0) { + if (adaptive) { + // floor for stride strides = {in_h / out_h, in_w / out_w}; int kh = in_h - (out_h - 1) * strides[0]; int kw = in_w - (out_w - 1) * strides[1]; @@ -243,6 +256,7 @@ class PoolGradXPUKernel : public framework::OpKernel { } if (pooling_type == "max") { + // TODO(zhanghuan05) to bind max_pool2d_grad_indices xpu api r = xpu::max_pool2d_grad(dev_ctx.x_context(), input, output, diff --git a/python/paddle/fluid/tests/unittests/xpu/test_pool2d_op_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_pool2d_op_xpu.py index 370d7645a81..f5e8b491168 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_pool2d_op_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_pool2d_op_xpu.py @@ -341,6 +341,24 @@ class XPUTestPool2D_Op(XPUOpTestWrapper): def init_adaptive(self): self.adaptive = False + class TestAvgPoolAdaptive(TestPool2D_Op): + + def init_adaptive(self): + self.adaptive = True + + class TestAvgPoolAdaptiveAsyOutSize(TestPool2D_Op): + + def init_adaptive(self): + self.adaptive = True + + def init_shape(self): + self.shape = [8, 3, 6, 6] + + def init_test_case(self): + self.ksize = [2, 3] + self.strides = [1, 1] + self.paddings = [0, 0, 0, 0] + class TestCase1(TestPool2D_Op): def init_test_case(self): -- GitLab