From e7223f5b9a89107b8e3559d8689ccbdf1604d1cf Mon Sep 17 00:00:00 2001 From: ZzSean <18818272991@163.com> Date: Fri, 13 Jan 2023 06:56:09 +0000 Subject: [PATCH 1/4] [Zero-Dim] Support input 0D Tensor for masked_select --- .../unittests/test_zero_dim_tensor_mlu.py | 18 ++++++++++++++++++ .../unittests/test_zero_dim_tensor_npu.py | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py b/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py index b54baffba..584c055d8 100644 --- a/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py +++ b/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py @@ -681,6 +681,24 @@ def test_argsort(self): self.assertEqual(x1.grad.numpy(), 0) self.assertEqual(x2.grad.numpy(), 0) + def test_maseked_select(self): + x1 = paddle.full([], 1.0) + x2 = paddle.full([], 1.0) + x1.stop_gradient = False + x2.stop_gradient = False + mask1 = paddle.full([], False, dtype='bool') + mask2 = paddle.full([], True, dtype='bool') + y1 = paddle.masked_select(x1, mask1) + y2 = paddle.masked_select(x2, mask2) + + y1.backward() + y2.backward() + + self.assertEqual(y1.shape, [0]) + self.assertEqual(y2.shape, [1]) + self.assertEqual(x1.grad.shape, []) + self.assertEqual(x2.grad.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): diff --git a/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py b/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py index 4a6fa5742..3b7567e4f 100644 --- a/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py +++ b/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py @@ -706,6 +706,24 @@ def test_argsort(self): self.assertEqual(x1.grad.numpy(), 0) self.assertEqual(x2.grad.numpy(), 0) + def test_maseked_select(self): + x1 = paddle.full([], 1.0) + x2 = paddle.full([], 1.0) + x1.stop_gradient = False + x2.stop_gradient = False + mask1 = paddle.full([], False, dtype='bool') + mask2 = paddle.full([], True, dtype='bool') + y1 = paddle.masked_select(x1, mask1) + y2 = paddle.masked_select(x2, mask2) + + y1.backward() + y2.backward() + + self.assertEqual(y1.shape, [0]) + self.assertEqual(y2.shape, [1]) + self.assertEqual(x1.grad.shape, []) + self.assertEqual(x2.grad.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 cfb2881ad943bea4fb9e1ee9c8c3b6722352fe81 Mon Sep 17 00:00:00 2001 From: ZzSean <18818272991@163.com> Date: Sun, 15 Jan 2023 05:03:11 +0000 Subject: [PATCH 2/4] fixx format --- backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py | 4 ++-- backends/npu/tests/unittests/test_zero_dim_tensor_npu.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py b/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py index 584c055d8..67a49fd6e 100644 --- a/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py +++ b/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py @@ -686,8 +686,8 @@ def test_maseked_select(self): x2 = paddle.full([], 1.0) x1.stop_gradient = False x2.stop_gradient = False - mask1 = paddle.full([], False, dtype='bool') - mask2 = paddle.full([], True, dtype='bool') + mask1 = paddle.full([], False, dtype="bool") + mask2 = paddle.full([], True, dtype="bool") y1 = paddle.masked_select(x1, mask1) y2 = paddle.masked_select(x2, mask2) diff --git a/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py b/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py index 3b7567e4f..030f18faf 100644 --- a/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py +++ b/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py @@ -711,8 +711,8 @@ def test_maseked_select(self): x2 = paddle.full([], 1.0) x1.stop_gradient = False x2.stop_gradient = False - mask1 = paddle.full([], False, dtype='bool') - mask2 = paddle.full([], True, dtype='bool') + mask1 = paddle.full([], False, dtype="bool") + mask2 = paddle.full([], True, dtype="bool") y1 = paddle.masked_select(x1, mask1) y2 = paddle.masked_select(x2, mask2) From 947a8a036ad9407e92687b2a70355be97ae4d9cf Mon Sep 17 00:00:00 2001 From: ZzSean <18818272991@163.com> Date: Mon, 16 Jan 2023 09:44:28 +0000 Subject: [PATCH 3/4] fix --- .../unittests/test_zero_dim_tensor_mlu.py | 21 +++++++------------ .../unittests/test_zero_dim_tensor_npu.py | 21 +++++++------------ 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py b/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py index 67a49fd6e..2cbd12060 100644 --- a/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py +++ b/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py @@ -682,22 +682,15 @@ def test_argsort(self): self.assertEqual(x2.grad.numpy(), 0) def test_maseked_select(self): - x1 = paddle.full([], 1.0) - x2 = paddle.full([], 1.0) - x1.stop_gradient = False - x2.stop_gradient = False - mask1 = paddle.full([], False, dtype="bool") - mask2 = paddle.full([], True, dtype="bool") - y1 = paddle.masked_select(x1, mask1) - y2 = paddle.masked_select(x2, mask2) + x = paddle.full([], 1.0) + x.stop_gradient = False + mask = paddle.full([], True, dtype="bool") + y = paddle.masked_select(x, mask) - y1.backward() - y2.backward() + y.backward() - self.assertEqual(y1.shape, [0]) - self.assertEqual(y2.shape, [1]) - self.assertEqual(x1.grad.shape, []) - self.assertEqual(x2.grad.shape, []) + self.assertEqual(y.shape, [1]) + self.assertEqual(x.grad.shape, []) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. diff --git a/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py b/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py index 030f18faf..ef193affa 100644 --- a/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py +++ b/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py @@ -707,22 +707,15 @@ def test_argsort(self): self.assertEqual(x2.grad.numpy(), 0) def test_maseked_select(self): - x1 = paddle.full([], 1.0) - x2 = paddle.full([], 1.0) - x1.stop_gradient = False - x2.stop_gradient = False - mask1 = paddle.full([], False, dtype="bool") - mask2 = paddle.full([], True, dtype="bool") - y1 = paddle.masked_select(x1, mask1) - y2 = paddle.masked_select(x2, mask2) + x = paddle.full([], 1.0) + x.stop_gradient = False + mask = paddle.full([], True, dtype="bool") + y = paddle.masked_select(x, mask) - y1.backward() - y2.backward() + y.backward() - self.assertEqual(y1.shape, [0]) - self.assertEqual(y2.shape, [1]) - self.assertEqual(x1.grad.shape, []) - self.assertEqual(x2.grad.shape, []) + self.assertEqual(y.shape, [1]) + self.assertEqual(x.grad.shape, []) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. From 993a70a9f63a003ce61cd618a657599ceb646e41 Mon Sep 17 00:00:00 2001 From: ZzSean <18818272991@163.com> Date: Tue, 17 Jan 2023 08:59:47 +0000 Subject: [PATCH 4/4] fix --- backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py | 7 +++++-- backends/npu/tests/unittests/test_zero_dim_tensor_npu.py | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py b/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py index 2cbd12060..e6a0e5b53 100644 --- a/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py +++ b/backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py @@ -682,15 +682,18 @@ def test_argsort(self): self.assertEqual(x2.grad.numpy(), 0) def test_maseked_select(self): - x = paddle.full([], 1.0) + x = paddle.rand([]) x.stop_gradient = False mask = paddle.full([], True, dtype="bool") y = paddle.masked_select(x, mask) + y.retain_grads() y.backward() - self.assertEqual(y.shape, [1]) + self.assertEqual(y.numpy(), x.numpy()) + self.assertEqual(y.grad.shape, [1]) self.assertEqual(x.grad.shape, []) + self.assertEqual(x.grad.numpy(), 1) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. diff --git a/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py b/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py index ef193affa..81a5f4350 100644 --- a/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py +++ b/backends/npu/tests/unittests/test_zero_dim_tensor_npu.py @@ -707,15 +707,18 @@ def test_argsort(self): self.assertEqual(x2.grad.numpy(), 0) def test_maseked_select(self): - x = paddle.full([], 1.0) + x = paddle.rand([]) x.stop_gradient = False mask = paddle.full([], True, dtype="bool") y = paddle.masked_select(x, mask) + y.retain_grads() y.backward() - self.assertEqual(y.shape, [1]) + self.assertEqual(y.numpy(), x.numpy()) + self.assertEqual(y.grad.shape, [1]) self.assertEqual(x.grad.shape, []) + self.assertEqual(x.grad.numpy(), 1) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.