未验证 提交 ee4e5323 编写于 作者: S sprouteer 提交者: GitHub

add unique support zero dim (#49260)

上级 073f7ced
...@@ -4596,10 +4596,10 @@ void UniqueRawInferMeta(const MetaTensor& x, ...@@ -4596,10 +4596,10 @@ void UniqueRawInferMeta(const MetaTensor& x,
MetaTensor* index, MetaTensor* index,
MetaTensor* counts) { MetaTensor* counts) {
if (!is_sorted) { if (!is_sorted) {
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(x.dims().size() == 1 || x.dims().size() == 0,
x.dims().size(), true,
1, phi::errors::InvalidArgument(
phi::errors::InvalidArgument("The Input(X) should be 1-D Tensor, " "The Input(X) should be 0-D or 1-D Tensor, "
"But now the dims of Input(X) is %d.", "But now the dims of Input(X) is %d.",
x.dims().size())); x.dims().size()));
out->set_dims(phi::make_ddim({-1})); out->set_dims(phi::make_ddim({-1}));
...@@ -4607,6 +4607,15 @@ void UniqueRawInferMeta(const MetaTensor& x, ...@@ -4607,6 +4607,15 @@ void UniqueRawInferMeta(const MetaTensor& x,
return; return;
} }
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 (axis.empty()) { if (axis.empty()) {
out->set_dims(phi::make_ddim({-1})); out->set_dims(phi::make_ddim({-1}));
if (return_inverse) { if (return_inverse) {
......
...@@ -2445,6 +2445,29 @@ class TestNoBackwardAPI(unittest.TestCase): ...@@ -2445,6 +2445,29 @@ class TestNoBackwardAPI(unittest.TestCase):
self.assertEqual(one_hot_label.shape, [4]) self.assertEqual(one_hot_label.shape, [4])
self.assertEqual(one_hot_label.numpy()[2], 1) self.assertEqual(one_hot_label.numpy()[2], 1)
def test_unique(self):
places = ['cpu']
if paddle.is_compiled_with_cuda():
places.append('gpu')
for place in places:
paddle.set_device(place)
x = paddle.rand([])
y, index, inverse, counts = paddle.unique(
x,
return_index=True,
return_inverse=True,
return_counts=True,
)
self.assertEqual(y, x)
self.assertEqual(index, 0)
self.assertEqual(inverse, 0)
self.assertEqual(counts, 1)
self.assertEqual(y.shape, [1])
self.assertEqual(index.shape, [1])
self.assertEqual(inverse.shape, [1])
self.assertEqual(counts.shape, [1])
class TestNoBackwardAPIStatic(unittest.TestCase): class TestNoBackwardAPIStatic(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -2647,6 +2670,23 @@ class TestNoBackwardAPIStatic(unittest.TestCase): ...@@ -2647,6 +2670,23 @@ class TestNoBackwardAPIStatic(unittest.TestCase):
self.assertEqual(res[0].shape, (4,)) self.assertEqual(res[0].shape, (4,))
self.assertEqual(res[0][2], 1) self.assertEqual(res[0][2], 1)
def test_unique(self):
x = paddle.rand([])
y, index, inverse, counts = paddle.unique(
x, return_index=True, return_inverse=True, return_counts=True
)
prog = paddle.static.default_main_program()
res = self.exe.run(prog, fetch_list=[y, index, inverse, counts])
self.assertEqual(y, x)
self.assertEqual(index, 0)
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,))
self.assertEqual(res[3].shape, (1,))
unary_apis_with_complex_input = [ unary_apis_with_complex_input = [
paddle.real, paddle.real,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册