From 3f25ac0241abe7c9c1ec68a11a12f6cb0c2fc08e Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Wed, 13 Apr 2022 08:02:57 +0000 Subject: [PATCH 1/7] fix pad3d infer shape --- paddle/phi/infermeta/unary.cc | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index f79a78ecd61f9..ef5b8cac9e492 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1279,6 +1279,11 @@ void Pad3dInferMeta(const MetaTensor& x, std::vector out_dims(x_dim.size()); out_dims[0] = x_dim[0]; + if (data_format == "NCDHW") { + out_dims[1] = x_dim[1]; + } else { + out_dims[4] = x_dim[4]; + } if (paddings_int_array.FromTensor()) { if (config.is_runtime) { PADDLE_ENFORCE_EQ( @@ -1288,13 +1293,8 @@ void Pad3dInferMeta(const MetaTensor& x, "[6], but received [%d].", paddings_int_array.GetData().size())); } - out_dims[1] = x_dim[1]; - out_dims[2] = x_dim[2]; - out_dims[3] = x_dim[3]; - out_dims[4] = x_dim[4]; } else { auto paddings = paddings_int_array.GetData(); - PADDLE_ENFORCE_EQ( paddings.size(), 6, @@ -1302,7 +1302,6 @@ void Pad3dInferMeta(const MetaTensor& x, "Size of paddings should be equal to 6, but received %d.", static_cast(paddings.size()))); if (data_format == "NCDHW") { - out_dims[1] = x_dim[1]; // channel out_dims[2] = ((!config.is_runtime) && (x_dim[2] < 0)) ? x_dim[2] : (x_dim[2] + paddings[4] + paddings[5]); // depth @@ -1315,8 +1314,6 @@ void Pad3dInferMeta(const MetaTensor& x, ? x_dim[4] : (x_dim[4] + paddings[0] + paddings[1]); // width } else { // NDHWC - out_dims[4] = x_dim[4]; // channel - out_dims[1] = ((!config.is_runtime) && (x_dim[1] < 0)) ? x_dim[1] : (x_dim[1] + paddings[4] + paddings[5]); // depth From f1331e9d6569f93990e0d699ab4322ea31ecf779 Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Wed, 13 Apr 2022 08:54:21 +0000 Subject: [PATCH 2/7] fix pad3d --- paddle/phi/infermeta/unary.cc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index ef5b8cac9e492..0c2c6b915e1b8 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1279,6 +1279,7 @@ void Pad3dInferMeta(const MetaTensor& x, std::vector out_dims(x_dim.size()); out_dims[0] = x_dim[0]; + auto& paddings = paddings_int_array.GetData(); if (data_format == "NCDHW") { out_dims[1] = x_dim[1]; } else { @@ -1287,14 +1288,22 @@ void Pad3dInferMeta(const MetaTensor& x, if (paddings_int_array.FromTensor()) { if (config.is_runtime) { PADDLE_ENFORCE_EQ( - paddings_int_array.GetData().size(), + paddings.size(), 6, errors::InvalidArgument("Shape of Input(Paddings) should be equal to " "[6], but received [%d].", - paddings_int_array.GetData().size())); + paddings.size())); + if (data_format == "NCDHW") { + out_dims[1] = x_dim[1] + paddings[4] + paddings[5]; + out_dims[2] = x_dim[2] + paddings[2] + paddings[3]; + out_dims[3] = x_dim[3] + paddings[0] + paddings[1]; + } else { + out_dims[2] = x_dim[2] + paddings[4] + paddings[5]; + out_dims[3] = x_dim[3] + paddings[2] + paddings[3]; + out_dims[4] = x_dim[4] + paddings[0] + paddings[1]; + } } } else { - auto paddings = paddings_int_array.GetData(); PADDLE_ENFORCE_EQ( paddings.size(), 6, From 06f50ee1da849ffe2ccbe8921abf6372127e6d19 Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Wed, 13 Apr 2022 09:00:22 +0000 Subject: [PATCH 3/7] fix pad default value --- paddle/phi/infermeta/unary.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 0c2c6b915e1b8..7adbcdcb093e4 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1277,7 +1277,7 @@ void Pad3dInferMeta(const MetaTensor& x, "5, but received %d. ", x_dim.size())); - std::vector out_dims(x_dim.size()); + std::vector out_dims(x_dim.size(), -1); out_dims[0] = x_dim[0]; auto& paddings = paddings_int_array.GetData(); if (data_format == "NCDHW") { From 6cfe7c2dad321a8445b859bad27cdb2d9bfce046 Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Wed, 13 Apr 2022 10:46:18 +0000 Subject: [PATCH 4/7] fix order --- paddle/phi/infermeta/unary.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 7adbcdcb093e4..6973417499653 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -1294,13 +1294,13 @@ void Pad3dInferMeta(const MetaTensor& x, "[6], but received [%d].", paddings.size())); if (data_format == "NCDHW") { - out_dims[1] = x_dim[1] + paddings[4] + paddings[5]; - out_dims[2] = x_dim[2] + paddings[2] + paddings[3]; - out_dims[3] = x_dim[3] + paddings[0] + paddings[1]; - } else { out_dims[2] = x_dim[2] + paddings[4] + paddings[5]; out_dims[3] = x_dim[3] + paddings[2] + paddings[3]; out_dims[4] = x_dim[4] + paddings[0] + paddings[1]; + } else { + out_dims[1] = x_dim[1] + paddings[4] + paddings[5]; + out_dims[2] = x_dim[2] + paddings[2] + paddings[3]; + out_dims[3] = x_dim[3] + paddings[0] + paddings[1]; } } } else { From b48a79b14f4511ba08e5bd2698ca6109ffdf6e9c Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Mon, 18 Apr 2022 06:57:45 +0000 Subject: [PATCH 5/7] add unit test --- .../fluid/tests/unittests/test_pad3d_op.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_pad3d_op.py b/python/paddle/fluid/tests/unittests/test_pad3d_op.py index 12f6f7b572108..e3b9a10d8ec1a 100644 --- a/python/paddle/fluid/tests/unittests/test_pad3d_op.py +++ b/python/paddle/fluid/tests/unittests/test_pad3d_op.py @@ -681,6 +681,30 @@ def test_class(self): input_data, pad, "circular", data_format="NCDHW") self.assertTrue(np.allclose(output.numpy(), np_out)) + def test_pad_tensor(self): + paddle.disable_static() + for place in self.places: + input_shape = (3, 4, 5, 6, 7) + pad = [1, 2, 2, 1, 1, 0] + pad_tensor = paddle.to_tensor(pad) + input_data = np.random.rand(*input_shape).astype(np.float32) + + pad_reflection_ncdhw = nn.Pad3D( + padding=pad_tensor, mode="reflect", data_format="NCDHW") + pad_reflection_ndhwc = nn.Pad3D( + padding=pad_tensor, mode="reflect", data_format="NDHWC") + data = paddle.to_tensor(input_data) + + output = pad_reflection_ncdhw(data) + np_out = self._get_numpy_out( + input_data, pad, "reflect", data_format="NCDHW") + self.assertTrue(np.allclose(output.numpy(), np_out)) + + output = pad_reflection_ndhwc(data) + np_out = self._get_numpy_out( + input_data, pad, "reflect", data_format="NDHWC") + self.assertTrue(np.allclose(output.numpy(), np_out)) + class TestPad3dOpError(unittest.TestCase): def setUp(self): From ee606936e5430e55cdc73c46398e7076fe88e3b9 Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Mon, 18 Apr 2022 16:22:12 +0000 Subject: [PATCH 6/7] fix unittest for ci coverage --- .../fluid/tests/unittests/test_pad3d_op.py | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_pad3d_op.py b/python/paddle/fluid/tests/unittests/test_pad3d_op.py index e3b9a10d8ec1a..302adc7a6afc7 100644 --- a/python/paddle/fluid/tests/unittests/test_pad3d_op.py +++ b/python/paddle/fluid/tests/unittests/test_pad3d_op.py @@ -27,7 +27,6 @@ class TestPad3dOp(OpTest): def setUp(self): paddle.enable_static() self.value = 0.0 - self.variable_paddings = False self.initTestCase() self.op_type = "pad3d" self.python_api = paddle.nn.functional.pad @@ -84,6 +83,7 @@ def initTestCase(self): self.mode = "constant" self.data_format = "NCDHW" self.pad_value = 0.0 + self.variable_paddings = False class TestCase1(TestPad3dOp): @@ -93,6 +93,7 @@ def initTestCase(self): self.mode = "constant" self.data_format = "NCDHW" self.value = 1.0 + self.variable_paddings = False class TestCase2(TestPad3dOp): @@ -102,6 +103,7 @@ def initTestCase(self): self.mode = "constant" self.data_format = "NDHWC" self.value = 1.0 + self.variable_paddings = False class TestCase3(TestPad3dOp): @@ -110,6 +112,7 @@ def initTestCase(self): self.paddings = [0, 1, 1, 0, 2, 3] self.mode = "reflect" self.data_format = "NCDHW" + self.variable_paddings = False class TestCase4(TestPad3dOp): @@ -118,6 +121,7 @@ def initTestCase(self): self.paddings = [0, 1, 2, 1, 2, 3] self.mode = "reflect" self.data_format = "NDHWC" + self.variable_paddings = False class TestCase5(TestPad3dOp): @@ -126,6 +130,7 @@ def initTestCase(self): self.paddings = [0, 1, 2, 3, 2, 1] self.mode = "replicate" self.data_format = "NCDHW" + self.variable_paddings = False class TestCase6(TestPad3dOp): @@ -134,6 +139,7 @@ def initTestCase(self): self.paddings = [5, 4, 2, 1, 2, 3] self.mode = "replicate" self.data_format = "NDHWC" + self.variable_paddings = False class TestCase7(TestPad3dOp): @@ -142,6 +148,7 @@ def initTestCase(self): self.paddings = [0, 1, 2, 3, 2, 1] self.mode = "circular" self.data_format = "NCDHW" + self.variable_paddings = False class TestCase8(TestPad3dOp): @@ -150,6 +157,27 @@ def initTestCase(self): self.paddings = [0, 1, 2, 1, 2, 3] self.mode = "circular" self.data_format = "NDHWC" + self.variable_paddings = False + + +class TestCase9(TestPad3dOp): + def initTestCase(self): + self.shape = (2, 3, 4, 5, 6) + self.paddings = [0, 1, 2, 3, 4, 5] + self.mode = "constant" + self.data_format = "NCDHW" + self.value = 1.0 + self.variable_paddings = True + + +class TestCase10(TestPad3dOp): + def initTestCase(self): + self.shape = (2, 3, 4, 5, 6) + self.paddings = [0, 1, 2, 3, 4, 5] + self.mode = "constant" + self.data_format = "NCDHW" + self.value = 1.0 + self.variable_paddings = True class TestPadAPI(unittest.TestCase): From 9a3db8aa713cf125ceb902541e3be302403e4351 Mon Sep 17 00:00:00 2001 From: littletomatodonkey Date: Tue, 19 Apr 2022 01:10:55 +0000 Subject: [PATCH 7/7] add ndhwc check --- python/paddle/fluid/tests/unittests/test_pad3d_op.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_pad3d_op.py b/python/paddle/fluid/tests/unittests/test_pad3d_op.py index 302adc7a6afc7..eabff5f0021c5 100644 --- a/python/paddle/fluid/tests/unittests/test_pad3d_op.py +++ b/python/paddle/fluid/tests/unittests/test_pad3d_op.py @@ -175,7 +175,7 @@ def initTestCase(self): self.shape = (2, 3, 4, 5, 6) self.paddings = [0, 1, 2, 3, 4, 5] self.mode = "constant" - self.data_format = "NCDHW" + self.data_format = "NDHWC" self.value = 1.0 self.variable_paddings = True