From 55e63763ecc4a74fc7ab2001ff10aa198ee3a1f2 Mon Sep 17 00:00:00 2001 From: Jacek Czaja Date: Mon, 12 Oct 2020 11:22:23 +0200 Subject: [PATCH] [oneDNN] adaptive pool support (#27747) --- .../fluid/operators/mkldnn/pool_mkldnn_op.cc | 3 ++ paddle/fluid/platform/mkldnn_reuse.h | 24 ++++++++++++++ .../unittests/mkldnn/test_pool2d_mkldnn_op.py | 33 +++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc index bf12c61a4d9..72d2f779f80 100644 --- a/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc @@ -126,6 +126,9 @@ class PoolMKLDNNGradOpKernel : public paddle::framework::OpKernel { UpdatePadding(&paddings, global_pooling, 0, padding_algorithm, data_dims, strides, ksize); + platform::PoolingMKLDNNHandler::ComputeAdaptivePoolParameters( + ctx, paddle::framework::vectorize(in_x->dims()), ksize, strides); + auto& dev_ctx = ctx.template device_context(); diff --git a/paddle/fluid/platform/mkldnn_reuse.h b/paddle/fluid/platform/mkldnn_reuse.h index d1c5480c0f5..785627a09fb 100644 --- a/paddle/fluid/platform/mkldnn_reuse.h +++ b/paddle/fluid/platform/mkldnn_reuse.h @@ -853,6 +853,9 @@ class PoolingMKLDNNHandler : public MKLDNNHandlerTAcquireForwardPrimitiveDescriptor( is_test ? mkldnn::prop_kind::forward_inference : mkldnn::prop_kind::forward_training, @@ -919,6 +922,27 @@ class PoolingMKLDNNHandler : public MKLDNNHandlerT& src_tz, std::vector& ksize, + std::vector& strides) { + if (ctx.Attr("adaptive")) { + // (jczaja): oneDNN is supporting only unchangable in size pool window + PADDLE_ENFORCE_EQ( + src_tz[src_tz.size() - 1] % ksize[1], 0, + platform::errors::Unimplemented( + "Input dim must be divisible by corressponding ksize dim.")); + PADDLE_ENFORCE_EQ( + src_tz[src_tz.size() - 2] % ksize[0], 0, + platform::errors::Unimplemented( + "Input dim must be divisible by corressponding ksize dim.")); + ksize[0] = src_tz[src_tz.size() - 2] / ksize[0]; + ksize[1] = src_tz[src_tz.size() - 1] / ksize[1]; + strides[0] = ksize[0]; + strides[1] = ksize[1]; + } + } + private: static inline int ComputeCeiledOutput(int input_size, int kernel_size, int padding, int stride) { diff --git a/python/paddle/fluid/tests/unittests/mkldnn/test_pool2d_mkldnn_op.py b/python/paddle/fluid/tests/unittests/mkldnn/test_pool2d_mkldnn_op.py index ee917b059b8..467bac67051 100644 --- a/python/paddle/fluid/tests/unittests/mkldnn/test_pool2d_mkldnn_op.py +++ b/python/paddle/fluid/tests/unittests/mkldnn/test_pool2d_mkldnn_op.py @@ -61,6 +61,37 @@ create_test_mkldnn_class(TestCase4) create_test_mkldnn_class(TestCase5) +class TestAvgPoolAdaptive(TestPool2D_Op): + def init_adaptive(self): + self.adaptive = True + + def init_pool_type(self): + self.pool_type = "avg" + self.pool2D_forward_naive = avg_pool2D_forward_naive + + def init_kernel_type(self): + self.use_mkldnn = True + + def init_test_case(self): + self.ksize = [1, 1] + self.strides = [1, 1] + + def init_data_type(self): + self.dtype = np.float32 + + def init_global_pool(self): + self.global_pool = False + + +class TestAvgPoolAdaptive2(TestAvgPoolAdaptive): + def init_test_case(self): + self.ksize = [2, 3] + self.strides = [1, 1] + + def init_shape(self): + self.shape = [2, 3, 6, 6] + + class TestAsymPad(TestPool2D_Op): def init_test_case(self): self.ksize = [3, 3] @@ -160,4 +191,6 @@ class TestAsymPadValidNHWC(TestAsymPadValid): if __name__ == '__main__': + from paddle import enable_static + enable_static() unittest.main() -- GitLab