From c1d0875d071ff787523623018ae023f813b9240f Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Wed, 12 Apr 2023 11:54:41 +0000 Subject: [PATCH 01/11] test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 59 +++++++++++++++++++ 1 file changed, 59 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 985a62f931195..9f72045c0fc37 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1973,6 +1973,48 @@ def test_upsample(self): self.assertEqual(out1.shape, [2, 3, 12, 12]) self.assertEqual(input_x.grad.shape, [2, 3, 6, 6]) + def test_unstack(self): + x1 = paddle.full([1], 0) + x2 = paddle.full([2], 2) + x1.retain_grads() + x2.retain_grads() + x1.stop_gradient = False + x2.stop_gradient = False + out1 = paddle.unstack(x1, 0) + out1.retain_grads() + out1.backward() + [out2_1, out2_2] = paddle.unstack(x2, 0) + self.assertEqual(out1.shape, []) + self.assertEqual(out1.numpy(), 0) + self.assertEqual(out1.grad.shape, []) + self.assertEqual(out2_1.shape, []) + self.assertEqual(out2_1.numpy(), 2) + self.assertEqual(out2_1.grad.shape, []) + self.assertEqual(out2_2.shape, []) + self.assertEqual(out2_2.numpy(), 2) + self.assertEqual(out2_2.grad.shape, []) + + def test_unbind(self): + x1 = paddle.full([1], 0) + x2 = paddle.full([2], 2) + x1.retain_grads() + x2.retain_grads() + x1.stop_gradient = False + x2.stop_gradient = False + out1 = paddle.unbind(x1, 0) + out1.retain_grads() + out1.backward() + [out2_1, out2_2] = paddle.unstack(x2, 0) + self.assertEqual(out1.shape, []) + self.assertEqual(out1.numpy(), 0) + self.assertEqual(out1.grad.shape, []) + self.assertEqual(out2_1.shape, []) + self.assertEqual(out2_1.numpy(), 2) + self.assertEqual(out2_1.grad.shape, []) + self.assertEqual(out2_2.shape, []) + self.assertEqual(out2_2.numpy(), 2) + self.assertEqual(out2_2.grad.shape, []) + def test_maseked_select(self): x = paddle.rand([]) x.stop_gradient = False @@ -1987,6 +2029,23 @@ def test_maseked_select(self): self.assertEqual(x.grad.shape, []) self.assertEqual(x.grad.numpy(), 1) + def test_squeeze(self): + x1 = paddle.full([], 2) + x1.stop_gradient = False + x1.retain_grads() + out1 = paddle.squeeze(x1, axis=0) + out1.retain_grads() + out1.backward() + self.assertEqual(out1.shape, []) + self.assertEqual(x1.grad.shape, []) + + x2 = paddle.full([], 0, dtype='int32') + out2 = paddle.squeeze(x1, axis=x2) + out2.retain_grads() + out2.backward() + self.assertEqual(out2.shape, []) + self.assertEqual(x1.grad.shape, []) + def test_unsqueeze(self): x1 = paddle.full([], 2) x1.stop_gradient = False From b8d781639f9cbc6c7354ce7fec778ed0ec682d43 Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Thu, 13 Apr 2023 06:27:48 +0000 Subject: [PATCH 02/11] test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 80 ++++++++++++++++++- 1 file changed, 77 insertions(+), 3 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 9f72045c0fc37..606a5fc2f5072 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1980,10 +1980,14 @@ def test_unstack(self): x2.retain_grads() x1.stop_gradient = False x2.stop_gradient = False - out1 = paddle.unstack(x1, 0) + [out1] = paddle.unstack(x1, 0) out1.retain_grads() out1.backward() [out2_1, out2_2] = paddle.unstack(x2, 0) + out2_1.retain_grads() + out2_1.backward() + out2_2.retain_grads() + out2_2.backward() self.assertEqual(out1.shape, []) self.assertEqual(out1.numpy(), 0) self.assertEqual(out1.grad.shape, []) @@ -2001,10 +2005,14 @@ def test_unbind(self): x2.retain_grads() x1.stop_gradient = False x2.stop_gradient = False - out1 = paddle.unbind(x1, 0) + [out1] = paddle.unbind(x1, 0) out1.retain_grads() out1.backward() - [out2_1, out2_2] = paddle.unstack(x2, 0) + [out2_1, out2_2] = paddle.unbind(x2, 0) + out2_1.retain_grads() + out2_1.backward() + out2_2.retain_grads() + out2_2.backward() self.assertEqual(out1.shape, []) self.assertEqual(out1.numpy(), 0) self.assertEqual(out1.grad.shape, []) @@ -3418,6 +3426,44 @@ def test_upsample(self): self.assertEqual(res1[0].shape, (2, 3, 12, 12)) self.assertEqual(res1[1].shape, (2, 3, 6, 6)) + @prog_scope() + def test_unstack(self): + x1 = paddle.full([], 0, 'float32') + x2 = paddle.full([2], 2, 'float32') + x1.stop_gradient = False + x2.stop_gradient = False + [out] = paddle.unstack(x1, 0) + paddle.static.append_backward(out) + [out2_1, out2_2] = paddle.unstack(x2, 0) + paddle.static.append_backward(out2_1) + paddle.static.append_backward(out2_2) + + prog = paddle.static.default_main_program() + res = self.exe.run(prog, feed={}, fetch_list=[out, out2_1, out2_2]) + + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, ()) + self.assertEqual(res[2].shape, ()) + + @prog_scope() + def test_unbind(self): + x1 = paddle.full([], 0, 'float32') + x2 = paddle.full([2], 2, 'float32') + x1.stop_gradient = False + x2.stop_gradient = False + [out] = paddle.unbind(x1, 0) + paddle.static.append_backward(out) + [out2_1, out2_2] = paddle.unstack(x2, 0) + paddle.static.append_backward(out2_1) + paddle.static.append_backward(out2_2) + + prog = paddle.static.default_main_program() + res = self.exe.run(prog, feed={}, fetch_list=[out, out2_1, out2_2]) + + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, ()) + self.assertEqual(res[2].shape, ()) + @prog_scope() def test_maseked_select(self): x = paddle.rand([]) @@ -3434,6 +3480,34 @@ def test_maseked_select(self): self.assertEqual(res[3].shape, ()) self.assertEqual(res[3], 1) + @prog_scope() + def test_squeeze(self): + x1 = paddle.full([], 2) + x1.stop_gradient = False + out1 = paddle.squeeze(x1, axis=0) + paddle.static.append_backward(out1.sum()) + + x2 = paddle.full([], 3) + x3 = paddle.full([], 0, dtype='int32') + x2.stop_gradient = False + out2 = paddle.squeeze(x2, axis=x3) + paddle.static.append_backward(out2.sum()) + + 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, ()) + self.assertEqual(res[1].shape, ()) + self.assertEqual(res[2].shape, ()) + self.assertEqual(res[3].shape, ()) + @prog_scope() def test_unsqueeze(self): x1 = paddle.full([], 2) From 5744bf6bc7a94607fb9cdce04a5618b42d19f72b Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Thu, 13 Apr 2023 08:09:29 +0000 Subject: [PATCH 03/11] test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 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 606a5fc2f5072..66f4f2ffc69ac 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3429,40 +3429,40 @@ def test_upsample(self): @prog_scope() def test_unstack(self): x1 = paddle.full([], 0, 'float32') - x2 = paddle.full([2], 2, 'float32') x1.stop_gradient = False - x2.stop_gradient = False - [out] = paddle.unstack(x1, 0) - paddle.static.append_backward(out) - [out2_1, out2_2] = paddle.unstack(x2, 0) - paddle.static.append_backward(out2_1) - paddle.static.append_backward(out2_2) - + out1 = paddle.unstack(x1, 0) + paddle.static.append_backward(out1) prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out, out2_1, out2_2]) + res = self.exe.run(prog, feed={}, fetch_list=[out1]) + self.assertEqual(res[0][0].shape, ()) - self.assertEqual(res[0].shape, ()) - self.assertEqual(res[1].shape, ()) - self.assertEqual(res[2].shape, ()) + x2 = paddle.full([2], 2, 'float32') + x2.stop_gradient = False + out2 = paddle.unstack(x2, 0) + paddle.static.append_backward(out2) + prog = paddle.static.default_main_program() + res = self.exe.run(prog, feed={}, fetch_list=[out2]) + self.assertEqual(res[0][0].shape, ()) + self.assertEqual(res[0][1].shape, ()) @prog_scope() def test_unbind(self): x1 = paddle.full([], 0, 'float32') - x2 = paddle.full([2], 2, 'float32') x1.stop_gradient = False - x2.stop_gradient = False - [out] = paddle.unbind(x1, 0) - paddle.static.append_backward(out) - [out2_1, out2_2] = paddle.unstack(x2, 0) - paddle.static.append_backward(out2_1) - paddle.static.append_backward(out2_2) - + out1 = paddle.unbind(x1, 0) + paddle.static.append_backward(out1) prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out, out2_1, out2_2]) + res = self.exe.run(prog, feed={}, fetch_list=[out1]) + self.assertEqual(res[0][0].shape, ()) - self.assertEqual(res[0].shape, ()) - self.assertEqual(res[1].shape, ()) - self.assertEqual(res[2].shape, ()) + x2 = paddle.full([2], 2, 'float32') + x2.stop_gradient = False + out2 = paddle.unbind(x2, 0) + paddle.static.append_backward(out2) + prog = paddle.static.default_main_program() + res = self.exe.run(prog, feed={}, fetch_list=[out2]) + self.assertEqual(res[0][0].shape, ()) + self.assertEqual(res[0][1].shape, ()) @prog_scope() def test_maseked_select(self): From 9e162b48051bcc30d81a4846fa7ec3edf0bd6412 Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Thu, 13 Apr 2023 11:02:51 +0000 Subject: [PATCH 04/11] test=allcase --- .../fluid/tests/unittests/test_zero_dim_tensor.py | 14 ++++++++++---- 1 file changed, 10 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 66f4f2ffc69ac..39ca55cdc8e08 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3486,6 +3486,16 @@ def test_squeeze(self): x1.stop_gradient = False out1 = paddle.squeeze(x1, axis=0) paddle.static.append_backward(out1.sum()) + prog = paddle.static.default_main_program() + res = self.exe.run( + prog, + fetch_list=[ + out1, + x1.grad_name, + ], + ) + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, ()) x2 = paddle.full([], 3) x3 = paddle.full([], 0, dtype='int32') @@ -3497,16 +3507,12 @@ def test_squeeze(self): res = self.exe.run( prog, fetch_list=[ - out1, out2, - x1.grad_name, x2.grad_name, ], ) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) - self.assertEqual(res[2].shape, ()) - self.assertEqual(res[3].shape, ()) @prog_scope() def test_unsqueeze(self): From f4cef7f1b6233a8bb8e70dbb8cdcdff23ca98db1 Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Mon, 17 Apr 2023 12:05:26 +0000 Subject: [PATCH 05/11] test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 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 39ca55cdc8e08..7aa84a4325028 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3428,18 +3428,14 @@ def test_upsample(self): @prog_scope() def test_unstack(self): - x1 = paddle.full([], 0, 'float32') - x1.stop_gradient = False + x1 = paddle.full([1], 0, 'float32') out1 = paddle.unstack(x1, 0) - paddle.static.append_backward(out1) prog = paddle.static.default_main_program() res = self.exe.run(prog, feed={}, fetch_list=[out1]) self.assertEqual(res[0][0].shape, ()) x2 = paddle.full([2], 2, 'float32') - x2.stop_gradient = False out2 = paddle.unstack(x2, 0) - paddle.static.append_backward(out2) prog = paddle.static.default_main_program() res = self.exe.run(prog, feed={}, fetch_list=[out2]) self.assertEqual(res[0][0].shape, ()) @@ -3447,18 +3443,14 @@ def test_unstack(self): @prog_scope() def test_unbind(self): - x1 = paddle.full([], 0, 'float32') - x1.stop_gradient = False + x1 = paddle.full([1], 0, 'float32') out1 = paddle.unbind(x1, 0) - paddle.static.append_backward(out1) prog = paddle.static.default_main_program() res = self.exe.run(prog, feed={}, fetch_list=[out1]) self.assertEqual(res[0][0].shape, ()) x2 = paddle.full([2], 2, 'float32') - x2.stop_gradient = False out2 = paddle.unbind(x2, 0) - paddle.static.append_backward(out2) prog = paddle.static.default_main_program() res = self.exe.run(prog, feed={}, fetch_list=[out2]) self.assertEqual(res[0][0].shape, ()) @@ -3486,16 +3478,6 @@ def test_squeeze(self): x1.stop_gradient = False out1 = paddle.squeeze(x1, axis=0) paddle.static.append_backward(out1.sum()) - prog = paddle.static.default_main_program() - res = self.exe.run( - prog, - fetch_list=[ - out1, - x1.grad_name, - ], - ) - self.assertEqual(res[0].shape, ()) - self.assertEqual(res[1].shape, ()) x2 = paddle.full([], 3) x3 = paddle.full([], 0, dtype='int32') @@ -3507,12 +3489,16 @@ def test_squeeze(self): res = self.exe.run( prog, fetch_list=[ + out1, out2, + x1.grad_name, x2.grad_name, ], ) self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) + self.assertEqual(res[2].shape, ()) + self.assertEqual(res[3].shape, ()) @prog_scope() def test_unsqueeze(self): From a8729389491719962f5a64be5f9c987bcb0a569c Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Sun, 23 Apr 2023 07:43:19 +0000 Subject: [PATCH 06/11] fix test cases, test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 32 ++++--------------- 1 file changed, 6 insertions(+), 26 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 7aa84a4325028..958b680bdd049 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -1976,52 +1976,32 @@ def test_upsample(self): def test_unstack(self): x1 = paddle.full([1], 0) x2 = paddle.full([2], 2) - x1.retain_grads() - x2.retain_grads() - x1.stop_gradient = False - x2.stop_gradient = False + [out1] = paddle.unstack(x1, 0) - out1.retain_grads() - out1.backward() [out2_1, out2_2] = paddle.unstack(x2, 0) - out2_1.retain_grads() - out2_1.backward() - out2_2.retain_grads() - out2_2.backward() + self.assertEqual(out1.shape, []) self.assertEqual(out1.numpy(), 0) - self.assertEqual(out1.grad.shape, []) + self.assertEqual(out2_1.shape, []) self.assertEqual(out2_1.numpy(), 2) - self.assertEqual(out2_1.grad.shape, []) self.assertEqual(out2_2.shape, []) self.assertEqual(out2_2.numpy(), 2) - self.assertEqual(out2_2.grad.shape, []) def test_unbind(self): x1 = paddle.full([1], 0) x2 = paddle.full([2], 2) - x1.retain_grads() - x2.retain_grads() - x1.stop_gradient = False - x2.stop_gradient = False + [out1] = paddle.unbind(x1, 0) - out1.retain_grads() - out1.backward() [out2_1, out2_2] = paddle.unbind(x2, 0) - out2_1.retain_grads() - out2_1.backward() - out2_2.retain_grads() - out2_2.backward() + self.assertEqual(out1.shape, []) self.assertEqual(out1.numpy(), 0) - self.assertEqual(out1.grad.shape, []) + self.assertEqual(out2_1.shape, []) self.assertEqual(out2_1.numpy(), 2) - self.assertEqual(out2_1.grad.shape, []) self.assertEqual(out2_2.shape, []) self.assertEqual(out2_2.numpy(), 2) - self.assertEqual(out2_2.grad.shape, []) def test_maseked_select(self): x = paddle.rand([]) From 9b35eaf2f6954c6b3caff1363a24a9cd94b94316 Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Sun, 23 Apr 2023 10:00:32 +0000 Subject: [PATCH 07/11] fix test cases, test=allcase --- .../fluid/tests/unittests/test_zero_dim_tensor.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 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 958b680bdd049..128319856af96 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -3412,14 +3412,14 @@ def test_unstack(self): out1 = paddle.unstack(x1, 0) prog = paddle.static.default_main_program() res = self.exe.run(prog, feed={}, fetch_list=[out1]) - self.assertEqual(res[0][0].shape, ()) + self.assertEqual(res[0].shape, ()) x2 = paddle.full([2], 2, 'float32') out2 = paddle.unstack(x2, 0) prog = paddle.static.default_main_program() res = self.exe.run(prog, feed={}, fetch_list=[out2]) - self.assertEqual(res[0][0].shape, ()) - self.assertEqual(res[0][1].shape, ()) + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, ()) @prog_scope() def test_unbind(self): @@ -3427,14 +3427,14 @@ def test_unbind(self): out1 = paddle.unbind(x1, 0) prog = paddle.static.default_main_program() res = self.exe.run(prog, feed={}, fetch_list=[out1]) - self.assertEqual(res[0][0].shape, ()) + self.assertEqual(res[0].shape, ()) x2 = paddle.full([2], 2, 'float32') out2 = paddle.unbind(x2, 0) prog = paddle.static.default_main_program() res = self.exe.run(prog, feed={}, fetch_list=[out2]) - self.assertEqual(res[0][0].shape, ()) - self.assertEqual(res[0][1].shape, ()) + self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, ()) @prog_scope() def test_maseked_select(self): From bd0aef1c46d8a8d2cadda8782344fce45678bc76 Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Mon, 24 Apr 2023 03:58:44 +0000 Subject: [PATCH 08/11] modify the test_squeeze to not use Tensor type axis, test=allcase --- .../fluid/tests/unittests/test_zero_dim_tensor.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 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 a4096a940810d..8a7829880a958 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -2346,12 +2346,14 @@ def test_squeeze(self): self.assertEqual(out1.shape, []) self.assertEqual(x1.grad.shape, []) - x2 = paddle.full([], 0, dtype='int32') - out2 = paddle.squeeze(x1, axis=x2) + x2 = paddle.full([1, 1], 1, dtype='int32') + x2.stop_gradient = False + x2.retain_grads() + out2 = paddle.squeeze(x2, axis=[0, 1]) out2.retain_grads() out2.backward() self.assertEqual(out2.shape, []) - self.assertEqual(x1.grad.shape, []) + self.assertEqual(x2.grad.shape, [1, 1]) def test_unsqueeze(self): x1 = paddle.full([], 2) @@ -4173,10 +4175,9 @@ def test_squeeze(self): out1 = paddle.squeeze(x1, axis=0) paddle.static.append_backward(out1.sum()) - x2 = paddle.full([], 3) - x3 = paddle.full([], 0, dtype='int32') + x2 = paddle.full([1, 1], 1, dtype='int32') x2.stop_gradient = False - out2 = paddle.squeeze(x2, axis=x3) + out2 = paddle.squeeze(x2, axis=[0, 1]) paddle.static.append_backward(out2.sum()) prog = paddle.static.default_main_program() @@ -4192,7 +4193,7 @@ def test_squeeze(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) - self.assertEqual(res[3].shape, ()) + self.assertEqual(res[3].shape, (1, 1)) @prog_scope() def test_unsqueeze(self): From e20314923dc29390a8bce211a989f2c93102f2cc Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Mon, 24 Apr 2023 10:23:01 +0000 Subject: [PATCH 09/11] add grad check for unbind and unstack, test=allcase --- .../tests/unittests/test_zero_dim_tensor.py | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 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 8a7829880a958..52ae78fc14fb3 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -2295,9 +2295,18 @@ def test_upsample(self): def test_unstack(self): x1 = paddle.full([1], 0) x2 = paddle.full([2], 2) + x1.retain_grads() + x2.retain_grads() + x1.stop_gradient = False + x2.stop_gradient = False [out1] = paddle.unstack(x1, 0) + out1.retain_grads() + out1.backward() [out2_1, out2_2] = paddle.unstack(x2, 0) + out2 = paddle.add_n([out2_1, out2_2]) + out2.retain_grads() + out2.backward() self.assertEqual(out1.shape, []) self.assertEqual(out1.numpy(), 0) @@ -2306,13 +2315,23 @@ def test_unstack(self): self.assertEqual(out2_1.numpy(), 2) self.assertEqual(out2_2.shape, []) self.assertEqual(out2_2.numpy(), 2) + self.assertEqual(x2.grad.shape, [2]) def test_unbind(self): x1 = paddle.full([1], 0) x2 = paddle.full([2], 2) + x1.retain_grads() + x2.retain_grads() + x1.stop_gradient = False + x2.stop_gradient = False [out1] = paddle.unbind(x1, 0) + out1.retain_grads() + out1.backward() [out2_1, out2_2] = paddle.unbind(x2, 0) + out2 = paddle.add_n([out2_1, out2_2]) + out2.retain_grads() + out2.backward() self.assertEqual(out1.shape, []) self.assertEqual(out1.numpy(), 0) @@ -2321,6 +2340,7 @@ def test_unbind(self): self.assertEqual(out2_1.numpy(), 2) self.assertEqual(out2_2.shape, []) self.assertEqual(out2_2.numpy(), 2) + self.assertEqual(x2.grad.shape, [2]) def test_maseked_select(self): x = paddle.rand([]) @@ -4125,32 +4145,46 @@ def test_upsample(self): @prog_scope() def test_unstack(self): x1 = paddle.full([1], 0, 'float32') + x1.stop_gradient = False out1 = paddle.unstack(x1, 0) + out1 = paddle.add_n(out1) + paddle.static.append_backward(out1) prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out1]) + res = self.exe.run(prog, feed={}, fetch_list=[out1, x1.grad_name]) self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, (1,)) x2 = paddle.full([2], 2, 'float32') + x2.stop_gradient = False out2 = paddle.unstack(x2, 0) + out2_sum = paddle.add_n(out2) + paddle.static.append_backward(out2_sum) prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out2]) + res = self.exe.run(prog, feed={}, fetch_list=[out2_sum, x2.grad_name]) self.assertEqual(res[0].shape, ()) - self.assertEqual(res[1].shape, ()) + self.assertEqual(res[1].shape, (2,)) @prog_scope() def test_unbind(self): x1 = paddle.full([1], 0, 'float32') + x1.stop_gradient = False out1 = paddle.unbind(x1, 0) + out1 = paddle.add_n(out1) + paddle.static.append_backward(out1) prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out1]) + res = self.exe.run(prog, feed={}, fetch_list=[out1, x1.grad_name]) self.assertEqual(res[0].shape, ()) + self.assertEqual(res[1].shape, (1,)) x2 = paddle.full([2], 2, 'float32') + x2.stop_gradient = False out2 = paddle.unbind(x2, 0) + out2_sum = paddle.add_n(out2) + paddle.static.append_backward(out2_sum) prog = paddle.static.default_main_program() - res = self.exe.run(prog, feed={}, fetch_list=[out2]) + res = self.exe.run(prog, feed={}, fetch_list=[out2_sum, x2.grad_name]) self.assertEqual(res[0].shape, ()) - self.assertEqual(res[1].shape, ()) + self.assertEqual(res[1].shape, (2,)) @prog_scope() def test_maseked_select(self): From 531e35d066943fbb9951d932888b44d2959b1eab Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Mon, 24 Apr 2023 12:11:10 +0000 Subject: [PATCH 10/11] check for squeeze axis tensor type, test=allcase --- .../fluid/tests/unittests/test_zero_dim_tensor.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 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 52ae78fc14fb3..81acee04c0e21 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -2366,14 +2366,15 @@ def test_squeeze(self): self.assertEqual(out1.shape, []) self.assertEqual(x1.grad.shape, []) - x2 = paddle.full([1, 1], 1, dtype='int32') + x2 = paddle.full([], 3) + x3 = paddle.full([], 0, dtype='int32') x2.stop_gradient = False x2.retain_grads() - out2 = paddle.squeeze(x2, axis=[0, 1]) + out2 = paddle.squeeze(x2, axis=x3) out2.retain_grads() out2.backward() self.assertEqual(out2.shape, []) - self.assertEqual(x2.grad.shape, [1, 1]) + self.assertEqual(x2.grad.shape, []) def test_unsqueeze(self): x1 = paddle.full([], 2) @@ -4209,9 +4210,10 @@ def test_squeeze(self): out1 = paddle.squeeze(x1, axis=0) paddle.static.append_backward(out1.sum()) - x2 = paddle.full([1, 1], 1, dtype='int32') + x2 = paddle.full([], 3) + x3 = paddle.full([], 0, dtype='int32') x2.stop_gradient = False - out2 = paddle.squeeze(x2, axis=[0, 1]) + out2 = paddle.squeeze(x2, axis=x3) paddle.static.append_backward(out2.sum()) prog = paddle.static.default_main_program() @@ -4227,7 +4229,7 @@ def test_squeeze(self): self.assertEqual(res[0].shape, ()) self.assertEqual(res[1].shape, ()) self.assertEqual(res[2].shape, ()) - self.assertEqual(res[3].shape, (1, 1)) + self.assertEqual(res[3].shape, ()) @prog_scope() def test_unsqueeze(self): From fce40371e4ab466773767bac8ccedb1948fb7741 Mon Sep 17 00:00:00 2001 From: zhengqiwen1997 Date: Thu, 27 Apr 2023 08:19:43 +0000 Subject: [PATCH 11/11] fix bug, test=allcase --- paddle/phi/infermeta/unary.cc | 3 +++ python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index bfe744446a97b..6a7b87465e428 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -3761,6 +3761,9 @@ void SqueezeInferMeta(const MetaTensor& x, if (!config.is_runtime && axes.FromTensor()) { // compile time infershape, set all elements to -1. int output_size = x.dims().size() - axes.GetData().size(); + if (x.dims().size() == 0 && output_size == -1) { + output_size = 0; + } std::vector vec_out_dims(output_size, -1); out->set_dims(phi::make_ddim(vec_out_dims)); } else { 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 81acee04c0e21..2407789b2173d 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -2367,7 +2367,7 @@ def test_squeeze(self): self.assertEqual(x1.grad.shape, []) x2 = paddle.full([], 3) - x3 = paddle.full([], 0, dtype='int32') + x3 = paddle.full([1], 0, dtype='int32') x2.stop_gradient = False x2.retain_grads() out2 = paddle.squeeze(x2, axis=x3)