Skip to content

Commit 28db7b8

Browse files
authored
[0-size Tensor No.354、355] Add 0-size Tensor support for unique [fluid_ops] (#74305)
* Fix * Fix
1 parent 9a7ec49 commit 28db7b8

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

python/paddle/tensor/manipulation.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3502,6 +3502,24 @@ def unique_consecutive(
35023502
):
35033503
attr_dtype = convert_np_dtype_to_dtype_(dtype)
35043504

3505+
if in_dynamic_mode():
3506+
if math.prod(x.shape) == 0:
3507+
if axis == []:
3508+
outs = [paddle.to_tensor([], dtype=x.dtype)]
3509+
else:
3510+
outs = [x.clone()]
3511+
if dtype == 'int32' or dtype == paddle.int32:
3512+
return_dtype = paddle.int32
3513+
else:
3514+
return_dtype = paddle.int64
3515+
if return_inverse:
3516+
outs.append(paddle.to_tensor([], dtype=return_dtype))
3517+
if return_counts:
3518+
outs.append(paddle.to_tensor([], dtype=return_dtype))
3519+
if len(outs) == 1:
3520+
return outs[0]
3521+
return tuple(outs)
3522+
35053523
if in_dynamic_or_pir_mode():
35063524
out, inverse, counts = _C_ops.unique_consecutive(
35073525
x, return_inverse, return_counts, axis, attr_dtype
@@ -3738,6 +3756,22 @@ def unique(
37383756
axis = [axis]
37393757
attr_dtype = convert_np_dtype_to_dtype_(dtype)
37403758
if in_dynamic_mode():
3759+
if math.prod(x.shape) == 0:
3760+
outs = [x.clone()]
3761+
if dtype == 'int32' or dtype == paddle.int32:
3762+
return_dtype = paddle.int32
3763+
else:
3764+
return_dtype = paddle.int64
3765+
if return_index:
3766+
outs.append(paddle.to_tensor([], dtype=return_dtype))
3767+
if return_inverse:
3768+
outs.append(paddle.to_tensor([], dtype=return_dtype))
3769+
if return_counts:
3770+
outs.append(paddle.to_tensor([], dtype=return_dtype))
3771+
if len(outs) == 1:
3772+
return outs[0]
3773+
return tuple(outs)
3774+
37413775
out, indices, inverse, counts = _C_ops.unique(
37423776
x, return_index, return_inverse, return_counts, axis, attr_dtype
37433777
)

test/legacy_test/test_unique.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,5 +482,15 @@ def test_dtype():
482482
self.assertRaises(TypeError, test_axis)
483483

484484

485+
class TestUniqueAPI_ZeroSize(unittest.TestCase):
486+
def test_dygraph_api_out(self):
487+
paddle.disable_static()
488+
x_data = np.random.randint(0, 10, (0, 2))
489+
x = paddle.to_tensor(x_data)
490+
out = paddle.unique(x)
491+
expected_out = np.random.random([0, 2])
492+
np.testing.assert_allclose(out.numpy(), expected_out)
493+
494+
485495
if __name__ == "__main__":
486496
unittest.main()

test/legacy_test/test_unique_consecutive_op.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,33 @@ def test_check_output(self):
347347
self.check_output(check_pir=True, check_symbol_infer=False)
348348

349349

350+
class TestUniqueConsecutive_ZeroSize(OpTest):
351+
def config(self):
352+
self.python_api = paddle.unique_consecutive
353+
354+
def init_kernel_type(self):
355+
self.dtype = "float32" if core.is_compiled_with_rocm() else "float64"
356+
357+
def setUp(self):
358+
paddle.disable_static()
359+
self.init_kernel_type()
360+
self.config()
361+
self.op_type = "unique_consecutive"
362+
x = np.random.random([2, 0]).astype(self.dtype)
363+
out = np.array([]).astype(self.dtype)
364+
self.inputs = {
365+
'X': x,
366+
}
367+
self.python_out_sig = ["Out"]
368+
self.attrs = {'dtype': paddle.int32}
369+
self.outputs = {
370+
'Out': out,
371+
}
372+
373+
def test_check_output(self):
374+
self.check_output(check_pir=True, check_symbol_infer=False)
375+
376+
350377
if __name__ == "__main__":
351378
paddle.enable_static()
352379
unittest.main()

0 commit comments

Comments
 (0)