Skip to content
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
34 changes: 34 additions & 0 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3502,6 +3502,24 @@ def unique_consecutive(
):
attr_dtype = convert_np_dtype_to_dtype_(dtype)

if in_dynamic_mode():
if math.prod(x.shape) == 0:
if axis == []:
outs = [paddle.to_tensor([], dtype=x.dtype)]
else:
outs = [x.clone()]
if dtype == 'int32' or dtype == paddle.int32:
return_dtype = paddle.int32
else:
return_dtype = paddle.int64
if return_inverse:
outs.append(paddle.to_tensor([], dtype=return_dtype))
if return_counts:
outs.append(paddle.to_tensor([], dtype=return_dtype))
if len(outs) == 1:
return outs[0]
return tuple(outs)

if in_dynamic_or_pir_mode():
out, inverse, counts = _C_ops.unique_consecutive(
x, return_inverse, return_counts, axis, attr_dtype
Expand Down Expand Up @@ -3738,6 +3756,22 @@ def unique(
axis = [axis]
attr_dtype = convert_np_dtype_to_dtype_(dtype)
if in_dynamic_mode():
if math.prod(x.shape) == 0:
outs = [x.clone()]
if dtype == 'int32' or dtype == paddle.int32:
return_dtype = paddle.int32
else:
return_dtype = paddle.int64
if return_index:
outs.append(paddle.to_tensor([], dtype=return_dtype))
if return_inverse:
outs.append(paddle.to_tensor([], dtype=return_dtype))
if return_counts:
outs.append(paddle.to_tensor([], dtype=return_dtype))
if len(outs) == 1:
return outs[0]
return tuple(outs)

out, indices, inverse, counts = _C_ops.unique(
x, return_index, return_inverse, return_counts, axis, attr_dtype
)
Expand Down
10 changes: 10 additions & 0 deletions test/legacy_test/test_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,5 +482,15 @@ def test_dtype():
self.assertRaises(TypeError, test_axis)


class TestUniqueAPI_ZeroSize(unittest.TestCase):
def test_dygraph_api_out(self):
paddle.disable_static()
x_data = np.random.randint(0, 10, (0, 2))
x = paddle.to_tensor(x_data)
out = paddle.unique(x)
expected_out = np.random.random([0, 2])
np.testing.assert_allclose(out.numpy(), expected_out)


if __name__ == "__main__":
unittest.main()
27 changes: 27 additions & 0 deletions test/legacy_test/test_unique_consecutive_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,33 @@ def test_check_output(self):
self.check_output(check_pir=True, check_symbol_infer=False)


class TestUniqueConsecutive_ZeroSize(OpTest):
def config(self):
self.python_api = paddle.unique_consecutive

def init_kernel_type(self):
self.dtype = "float32" if core.is_compiled_with_rocm() else "float64"

def setUp(self):
paddle.disable_static()
self.init_kernel_type()
self.config()
self.op_type = "unique_consecutive"
x = np.random.random([2, 0]).astype(self.dtype)
out = np.array([]).astype(self.dtype)
self.inputs = {
'X': x,
}
self.python_out_sig = ["Out"]
self.attrs = {'dtype': paddle.int32}
self.outputs = {
'Out': out,
}

def test_check_output(self):
self.check_output(check_pir=True, check_symbol_infer=False)


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