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] support input 0D Tensor for unique #49260

Merged
merged 1 commit into from
Jan 20, 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
21 changes: 15 additions & 6 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4596,17 +4596,26 @@ void UniqueRawInferMeta(const MetaTensor& x,
MetaTensor* index,
MetaTensor* counts) {
if (!is_sorted) {
PADDLE_ENFORCE_EQ(
x.dims().size(),
1,
phi::errors::InvalidArgument("The Input(X) should be 1-D Tensor, "
"But now the dims of Input(X) is %d.",
x.dims().size()));
PADDLE_ENFORCE_EQ(x.dims().size() == 1 || x.dims().size() == 0,
true,
phi::errors::InvalidArgument(
"The Input(X) should be 0-D or 1-D Tensor, "
"But now the dims of Input(X) is %d.",
x.dims().size()));
out->set_dims(phi::make_ddim({-1}));
index->set_dims(x.dims());
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()) {
out->set_dims(phi::make_ddim({-1}));
if (return_inverse) {
Expand Down
40 changes: 40 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 @@ -2430,6 +2430,29 @@ def test_one_hot_label(self):
self.assertEqual(one_hot_label.shape, [4])
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):
def setUp(self):
Expand Down Expand Up @@ -2632,6 +2655,23 @@ def test_one_hot_label(self):
self.assertEqual(res[0].shape, (4,))
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,))


if __name__ == "__main__":
unittest.main()