From 8418c22a8a45e8c1a63374ea90e1cf6a3cf335b6 Mon Sep 17 00:00:00 2001 From: heliqi <1101791222@qq.com> Date: Mon, 16 Jan 2023 09:15:28 +0000 Subject: [PATCH 1/7] add unsqueeze test case --- .../tests/unittests/test_zero_dim_tensor.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 2927f4a23e728..aae5de154d8f3 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1173,6 +1173,29 @@ def test_atan2(self): self.assertEqual(x1.grad.numpy(), 0.5) self.assertEqual(x2.grad.numpy(), 0) + def test_unsqueeze(self): + x1 = paddle.full([], 2) + x1.stop_gradient = False + out1 = paddle.unsqueeze(x1, axis=0) + out1.backward() + self.assertEqual( + out1.shape, + [ + 1, + ], + ) + self.assertEqual(x1.grad.shape, []) + + x2 = paddle.full([], 0, dtype='int32') + out2 = paddle.unsqueeze(x1, axis=x2) + out2.backward() + self.assertEqual( + out2.shape, + [ + 1, + ], + ) + class TestSundryAPIStatic(unittest.TestCase): def setUp(self): @@ -1803,6 +1826,34 @@ def test_atan2(self): self.assertEqual(res[0].shape, ()) + @prog_scope() + def test_unsqueeze(self): + x1 = paddle.full([], 2) + out1 = paddle.unsqueeze(x1, axis=0) + x1.stop_gradient = False + paddle.static.append_backward(out1) + + x2 = paddle.full([], 3) + x3 = paddle.full([], 0, dtype='int32') + x2.stop_gradient = False + out2 = paddle.unsqueeze(x2, axis=x3) + paddle.static.append_backward(out2) + + prog = paddle.static.default_main_program() + res = self.exe.run( + prog, + fetch_list=[ + out1, + out2, + x1.grad_name, + x2.grad_name, + ], + ) + self.assertEqual(res[0].shape, [1]) + self.assertEqual(res[1].shape, [21]) + self.assertEqual(res[2].shape, []) + self.assertEqual(res[3].shape, []) + # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. class TestNoBackwardAPI(unittest.TestCase): From 21ef92260ec8da033be523e917a4d58202f2de69 Mon Sep 17 00:00:00 2001 From: heliqi <1101791222@qq.com> Date: Mon, 16 Jan 2023 09:26:54 +0000 Subject: [PATCH 2/7] add unsqueeze xpu test case --- .../tests/unittests/test_zero_dim_tensor.py | 16 +++------------- .../unittests/xpu/test_zero_dim_tensor_xpu.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index aae5de154d8f3..9bd1d3a42a72d 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1178,23 +1178,13 @@ def test_unsqueeze(self): x1.stop_gradient = False out1 = paddle.unsqueeze(x1, axis=0) out1.backward() - self.assertEqual( - out1.shape, - [ - 1, - ], - ) + self.assertEqual(out1.shape, [1]) self.assertEqual(x1.grad.shape, []) x2 = paddle.full([], 0, dtype='int32') out2 = paddle.unsqueeze(x1, axis=x2) out2.backward() - self.assertEqual( - out2.shape, - [ - 1, - ], - ) + self.assertEqual(out2.shape, [1]) class TestSundryAPIStatic(unittest.TestCase): @@ -1850,7 +1840,7 @@ def test_unsqueeze(self): ], ) self.assertEqual(res[0].shape, [1]) - self.assertEqual(res[1].shape, [21]) + self.assertEqual(res[1].shape, [1]) self.assertEqual(res[2].shape, []) self.assertEqual(res[3].shape, []) diff --git a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py index 74bc37152a34e..998bc8445d491 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py @@ -758,6 +758,19 @@ def test_allclose(self): y = paddle.full([], 0.6) self.assertFalse(paddle.allclose(x, y)) + def test_unsqueeze(self): + x1 = paddle.full([], 2) + x1.stop_gradient = False + out1 = paddle.unsqueeze(x1, axis=0) + out1.backward() + self.assertEqual(out1.shape, [1]) + self.assertEqual(x1.grad.shape, []) + + x2 = paddle.full([], 0, dtype='int32') + out2 = paddle.unsqueeze(x1, axis=x2) + out2.backward() + self.assertEqual(out2.shape, [1]) + # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. From 35f072adcdb80d49bce80bda245fd9f8f6e0f3a6 Mon Sep 17 00:00:00 2001 From: heliqi <1101791222@qq.com> Date: Tue, 17 Jan 2023 02:51:06 +0000 Subject: [PATCH 3/7] fix unsqueeze test case --- python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 9bd1d3a42a72d..05be06f7f186b 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1185,6 +1185,7 @@ def test_unsqueeze(self): out2 = paddle.unsqueeze(x1, axis=x2) out2.backward() self.assertEqual(out2.shape, [1]) + self.assertEqual(x1.grad.shape, []) class TestSundryAPIStatic(unittest.TestCase): @@ -1821,13 +1822,13 @@ def test_unsqueeze(self): x1 = paddle.full([], 2) out1 = paddle.unsqueeze(x1, axis=0) x1.stop_gradient = False - paddle.static.append_backward(out1) + paddle.static.append_backward(out1.sum()) x2 = paddle.full([], 3) x3 = paddle.full([], 0, dtype='int32') x2.stop_gradient = False out2 = paddle.unsqueeze(x2, axis=x3) - paddle.static.append_backward(out2) + paddle.static.append_backward(out2.sum()) prog = paddle.static.default_main_program() res = self.exe.run( From 40d61d21fe0b4268c189ebc6de44999682509f03 Mon Sep 17 00:00:00 2001 From: heliqi <1101791222@qq.com> Date: Tue, 17 Jan 2023 03:58:48 +0000 Subject: [PATCH 4/7] fix unsqueeze test case --- .../paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 05be06f7f186b..6b71eeeedf384 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1178,13 +1178,13 @@ def test_unsqueeze(self): x1.stop_gradient = False out1 = paddle.unsqueeze(x1, axis=0) out1.backward() - self.assertEqual(out1.shape, [1]) + self.assertEqual(out1.shape, (1,)) self.assertEqual(x1.grad.shape, []) x2 = paddle.full([], 0, dtype='int32') out2 = paddle.unsqueeze(x1, axis=x2) out2.backward() - self.assertEqual(out2.shape, [1]) + self.assertEqual(out2.shape, (1,)) self.assertEqual(x1.grad.shape, []) @@ -1840,8 +1840,8 @@ def test_unsqueeze(self): x2.grad_name, ], ) - self.assertEqual(res[0].shape, [1]) - self.assertEqual(res[1].shape, [1]) + self.assertEqual(res[0].shape, (1,)) + self.assertEqual(res[1].shape, (1,)) self.assertEqual(res[2].shape, []) self.assertEqual(res[3].shape, []) From c4f1ce8911ad915dab43da6d36441cad036dc887 Mon Sep 17 00:00:00 2001 From: heliqi <1101791222@qq.com> Date: Tue, 17 Jan 2023 04:54:30 +0000 Subject: [PATCH 5/7] fix unsqueeze test case --- .../paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 6b71eeeedf384..47b19447a3286 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1179,13 +1179,13 @@ def test_unsqueeze(self): out1 = paddle.unsqueeze(x1, axis=0) out1.backward() self.assertEqual(out1.shape, (1,)) - self.assertEqual(x1.grad.shape, []) + self.assertEqual(x1.grad.shape, ()) x2 = paddle.full([], 0, dtype='int32') out2 = paddle.unsqueeze(x1, axis=x2) out2.backward() self.assertEqual(out2.shape, (1,)) - self.assertEqual(x1.grad.shape, []) + self.assertEqual(x1.grad.shape, ()) class TestSundryAPIStatic(unittest.TestCase): @@ -1842,8 +1842,8 @@ def test_unsqueeze(self): ) self.assertEqual(res[0].shape, (1,)) self.assertEqual(res[1].shape, (1,)) - self.assertEqual(res[2].shape, []) - self.assertEqual(res[3].shape, []) + self.assertEqual(res[2].shape, ()) + self.assertEqual(res[3].shape, ()) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. From 2cff3961b38bdfd02a4eec17963a4f687faac9dc Mon Sep 17 00:00:00 2001 From: heliqi <1101791222@qq.com> Date: Tue, 17 Jan 2023 06:54:49 +0000 Subject: [PATCH 6/7] fix unsqueeze test case --- .../paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 47b19447a3286..473854b8169f5 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1178,14 +1178,14 @@ def test_unsqueeze(self): x1.stop_gradient = False out1 = paddle.unsqueeze(x1, axis=0) out1.backward() - self.assertEqual(out1.shape, (1,)) - self.assertEqual(x1.grad.shape, ()) + self.assertEqual(out1.shape, [1]) + self.assertEqual(x1.grad.shape, []) x2 = paddle.full([], 0, dtype='int32') out2 = paddle.unsqueeze(x1, axis=x2) out2.backward() - self.assertEqual(out2.shape, (1,)) - self.assertEqual(x1.grad.shape, ()) + self.assertEqual(out2.shape, [1]) + self.assertEqual(x1.grad.shape, []) class TestSundryAPIStatic(unittest.TestCase): From e3106b2740acf2f93b551ab316f08e7424acfd98 Mon Sep 17 00:00:00 2001 From: heliqi <1101791222@qq.com> Date: Wed, 18 Jan 2023 03:04:23 +0000 Subject: [PATCH 7/7] add retain_grads --- python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py index 435f4e1fe6540..362a0cea168f4 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1315,13 +1315,16 @@ def test_maseked_select(self): def test_unsqueeze(self): x1 = paddle.full([], 2) x1.stop_gradient = False + x1.retain_grads() out1 = paddle.unsqueeze(x1, axis=0) + out1.retain_grads() out1.backward() self.assertEqual(out1.shape, [1]) self.assertEqual(x1.grad.shape, []) x2 = paddle.full([], 0, dtype='int32') out2 = paddle.unsqueeze(x1, axis=x2) + out2.retain_grads() out2.backward() self.assertEqual(out2.shape, [1]) self.assertEqual(x1.grad.shape, [])