Skip to content

Commit

Permalink
[Zero-Dim] Support input 0D Tensor for masked_select (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZzSean authored Jan 17, 2023
1 parent 0168f32 commit 6332308
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions backends/mlu/tests/unittests/test_zero_dim_tensor_mlu.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,20 @@ def test_argsort(self):
self.assertEqual(x1.grad.numpy(), 0)
self.assertEqual(x2.grad.numpy(), 0)

def test_maseked_select(self):
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.
class TestNoBackwardAPI(unittest.TestCase):
Expand Down
14 changes: 14 additions & 0 deletions backends/npu/tests/unittests/test_zero_dim_tensor_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,20 @@ def test_argsort(self):
self.assertEqual(x1.grad.numpy(), 0)
self.assertEqual(x2.grad.numpy(), 0)

def test_maseked_select(self):
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.
class TestNoBackwardAPI(unittest.TestCase):
Expand Down

0 comments on commit 6332308

Please sign in to comment.