Skip to content

Commit

Permalink
unique_consecutive add 0d (#50213)
Browse files Browse the repository at this point in the history
  • Loading branch information
firestonelib authored Feb 6, 2023
1 parent 26a8b5b commit eb8353a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions python/paddle/fluid/tests/unittests/test_zero_dim_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit eb8353a

Please sign in to comment.