From eb8353a4de1887c42287968113086ff0e4ea9633 Mon Sep 17 00:00:00 2001 From: duanboqiang Date: Mon, 6 Feb 2023 11:20:38 +0800 Subject: [PATCH] unique_consecutive add 0d (#50213) --- paddle/phi/infermeta/unary.cc | 8 +++++ .../tests/unittests/test_zero_dim_tensor.py | 35 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index f2fcb3162081f..6b5cadc9d2fdd 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -4581,6 +4581,14 @@ void UniqueConsecutiveInferMeta(const MetaTensor& x, "unique_consecutive should have output tensor out.")); auto in_dims = x.dims(); + if (x.dims().size() == 0) { + PADDLE_ENFORCE_EQ(axis.empty(), + true, + phi::errors::InvalidArgument( + "The Input(X) with 0-D Tensor, axis must be None" + "But now the axis is %d.", + axis[0])); + } if (return_inverse) { PADDLE_ENFORCE_NE( index, 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 f66da6933ed2d..5486fba80498a 100644 --- a/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py @@ -2607,6 +2607,26 @@ def test_one_hot_label(self): self.assertEqual(one_hot_label.shape, [4]) self.assertEqual(one_hot_label.numpy()[2], 1) + def test_unique_consecutive(self): + places = ['cpu'] + if paddle.is_compiled_with_cuda(): + places.append('gpu') + for place in places: + paddle.set_device(place) + x = paddle.rand([]) + y, inverse, counts = paddle.unique_consecutive( + x, + return_inverse=True, + return_counts=True, + ) + + self.assertEqual(y, x) + self.assertEqual(inverse, 0) + self.assertEqual(counts, 1) + self.assertEqual(y.shape, [1]) + self.assertEqual(inverse.shape, [1]) + self.assertEqual(counts.shape, [1]) + def test_unique(self): places = ['cpu'] if paddle.is_compiled_with_cuda(): @@ -2832,6 +2852,21 @@ def test_one_hot_label(self): self.assertEqual(res[0].shape, (4,)) self.assertEqual(res[0][2], 1) + def test_unique_consecutive(self): + x = paddle.rand([]) + y, inverse, counts = paddle.unique_consecutive( + x, return_inverse=True, return_counts=True + ) + + prog = paddle.static.default_main_program() + res = self.exe.run(prog, fetch_list=[y, inverse, counts]) + self.assertEqual(y, x) + self.assertEqual(inverse, 0) + self.assertEqual(counts, 1) + self.assertEqual(res[0].shape, (1,)) + self.assertEqual(res[1].shape, (1,)) + self.assertEqual(res[2].shape, (1,)) + def test_unique(self): x = paddle.rand([]) y, index, inverse, counts = paddle.unique(