From fbed9bf8859e62a09d4bd55ffeb7494fdf9c022d Mon Sep 17 00:00:00 2001 From: liuyuang Date: Thu, 24 Nov 2022 14:18:11 +0800 Subject: [PATCH 1/3] ut for cpu/gpu searchsorted --- .../tests/unittests/test_zero_dim_tensor.py | 19 +++++++++++++++++++ 1 file changed, 19 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 dcfee03f40cfa..51413ee494cf8 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -586,6 +586,15 @@ def test_logical_not(self): self.assertEqual(out.shape, []) + def test_searchsorted(self): + x = paddle.to_tensor([1, 3, 5, 7, 9]) + y = paddle.rand([]) + + # only has forward kernel + out = paddle.searchsorted(x, y) + + self.assertEqual(out.shape, []) + class TestSundryAPIStatic(unittest.TestCase): def setUp(self): @@ -656,6 +665,16 @@ def test_logical_not(self): res = self.exe.run(prog, fetch_list=[out]) self.assertEqual(res[0].shape, ()) + @prog_scope() + def test_searchsorted(self): + x = paddle.full([5], 1.0, 'float32') + y = paddle.full([], 1.0, 'float32') + out = paddle.searchsorted(x, y) + + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[out]) + self.assertEqual(res[0].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 6ecd59f9e026b5d20aec5b59e2608871fe3ab5a9 Mon Sep 17 00:00:00 2001 From: liuyuang Date: Thu, 24 Nov 2022 15:14:21 +0800 Subject: [PATCH 2/3] add xpu test case for searchsorted --- .../tests/unittests/xpu/test_zero_dim_tensor_xpu.py | 9 +++++++++ 1 file changed, 9 insertions(+) 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 a0925207c957e..d2a97e0a50cd2 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 @@ -414,6 +414,15 @@ def test_logical_not(self): self.assertEqual(out.shape, []) + def test_searchsorted(self): + x = paddle.to_tensor([1, 3, 5, 7, 9]) + y = paddle.rand([]) + + # only has forward kernel + out = paddle.searchsorted(x, y) + + self.assertEqual(out.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 33a45b2853d23e7f91d24267387a1bc6cbc06bde Mon Sep 17 00:00:00 2001 From: liuyuang Date: Fri, 25 Nov 2022 11:02:12 +0800 Subject: [PATCH 3/3] resolve comments --- python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py | 4 +++- .../fluid/tests/unittests/xpu/test_zero_dim_tensor_xpu.py | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) 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 51413ee494cf8..5911b496a32d2 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -594,6 +594,7 @@ def test_searchsorted(self): out = paddle.searchsorted(x, y) self.assertEqual(out.shape, []) + self.assertEqual(out.numpy(), 0) class TestSundryAPIStatic(unittest.TestCase): @@ -667,13 +668,14 @@ def test_logical_not(self): @prog_scope() def test_searchsorted(self): - x = paddle.full([5], 1.0, 'float32') + x = paddle.full([10], 1.0, 'float32') y = paddle.full([], 1.0, 'float32') out = paddle.searchsorted(x, y) prog = paddle.static.default_main_program() res = self.exe.run(prog, fetch_list=[out]) self.assertEqual(res[0].shape, ()) + self.assertEqual(res[0], 0) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest. 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 d2a97e0a50cd2..3be5c315f3bcc 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 @@ -422,6 +422,7 @@ def test_searchsorted(self): out = paddle.searchsorted(x, y) self.assertEqual(out.shape, []) + self.assertEqual(out.numpy(), 0) # Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.