Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Zero-Dim] unique_consecutive support 0d input #50213

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2606,6 +2606,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 @@ -2831,6 +2851,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