From d89a759bba8dacd2da2a27e8142e4b37bbfd3954 Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Tue, 19 Oct 2021 14:57:23 +0800 Subject: [PATCH] fix replicate pad when input size is 0 (#36510) * fix replicate pad when input size is 0 * add unit test --- paddle/fluid/operators/pad3d_op.cc | 12 +++++------- paddle/fluid/operators/pad3d_op.cu | 12 +++++------- python/paddle/fluid/tests/unittests/test_pad3d_op.py | 10 ++++++++++ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/paddle/fluid/operators/pad3d_op.cc b/paddle/fluid/operators/pad3d_op.cc index c2be9ac97ff..e84b5a9d9ba 100644 --- a/paddle/fluid/operators/pad3d_op.cc +++ b/paddle/fluid/operators/pad3d_op.cc @@ -565,13 +565,11 @@ class Pad3dCPUKernel : public framework::OpKernel { " in reflect mode" ", but received depth(%d) and pad_right(%d).", in_width, pads[1])); - } - - if (mode == "circular") { - PADDLE_ENFORCE_NE( - in_depth * in_height * in_width, 0, - platform::errors::InvalidArgument( - "The input tensor size can not be 0 for circular padding mode.")); + } else if (mode == "circular" || mode == "replicate") { + PADDLE_ENFORCE_NE(in_depth * in_height * in_width, 0, + platform::errors::InvalidArgument( + "The input tensor size can not be 0 for circular " + "or replicate padding mode.")); } const int pad_left = pads[0]; diff --git a/paddle/fluid/operators/pad3d_op.cu b/paddle/fluid/operators/pad3d_op.cu index ed936c10755..f243a78e557 100644 --- a/paddle/fluid/operators/pad3d_op.cu +++ b/paddle/fluid/operators/pad3d_op.cu @@ -618,13 +618,11 @@ class Pad3dCUDAKernel : public framework::OpKernel { " in reflect mode" ", but received depth(%d) and pad_right(%d).", in_width, pads[1])); - } - - if (mode == "circular") { - PADDLE_ENFORCE_NE( - in_depth * in_height * in_width, 0, - platform::errors::InvalidArgument( - "The input tensor size can not be 0 for circular padding mode.")); + } else if (mode == "circular" || mode == "replicate") { + PADDLE_ENFORCE_NE(in_depth * in_height * in_width, 0, + platform::errors::InvalidArgument( + "The input tensor size can not be 0 for circular " + "or replicate padding mode.")); } const int pad_left = pads[0]; diff --git a/python/paddle/fluid/tests/unittests/test_pad3d_op.py b/python/paddle/fluid/tests/unittests/test_pad3d_op.py index 5ec7bdc66fe..7abc314bc1b 100644 --- a/python/paddle/fluid/tests/unittests/test_pad3d_op.py +++ b/python/paddle/fluid/tests/unittests/test_pad3d_op.py @@ -732,6 +732,15 @@ class TestPad3dOpError(unittest.TestCase): mode='circular', data_format="NCDHW") + def test_replicate_1(): + input_shape = (1, 2, 0, 4, 5) + data = np.random.rand(*input_shape).astype(np.float32) + x = paddle.to_tensor(data) + y = F.pad(x, + pad=[1, 1, 1, 1, 2, 3], + mode='replicate', + data_format="NCDHW") + paddle.disable_static() for place in self.places: self.assertRaises(ValueError, test_variable) @@ -739,6 +748,7 @@ class TestPad3dOpError(unittest.TestCase): self.assertRaises(Exception, test_reflect_2) self.assertRaises(Exception, test_reflect_3) self.assertRaises(Exception, test_circular_1) + self.assertRaises(Exception, test_replicate_1) paddle.enable_static() -- GitLab