Skip to content
Merged
3 changes: 3 additions & 0 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3770,13 +3770,15 @@ def unique(
) -> Tensor | tuple[Tensor, ...]: ...


@param_two_alias(["x", "input"], ["axis", "dim"])
def unique(
x,
return_index=False,
return_inverse=False,
return_counts=False,
axis=None,
dtype="int64",
sorted=True,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个目前应该无法实现 API完全一致吧,参数顺序不同。

group_norm也有这个问题。不过这个没法判断数据类型了,只能新增加一个paddle.compat.unique了?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的那增加paddle.compat.unique

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhwesky2010 已增加paddle.compat.unique CI完成,PaConvert PaddlePaddle/PaConvert#758 修改测试通过

name=None,
):
r"""
Expand All @@ -3793,6 +3795,7 @@ def unique(
Default: None.
dtype(str|paddle.dtype|np.dtype, optional): The date type of `indices` or `inverse` tensor: int32 or int64.
Default: int64.
sorted(bool, optional): Does not affect the return result, used for compatibility.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorted=True
sorted=Fasle

两种情况下结果均一致吗

Copy link
Contributor Author

@co63oc co63oc Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image 是一致的,is_sorted 设置为 true image UniqueRawKernel 的参数 is_sorted 没有使用

name(str|None, optional): Name for the operation. For more information, please refer to
:ref:`api_guide_Name`. Default: None.

Expand Down
45 changes: 45 additions & 0 deletions test/legacy_test/test_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,5 +500,50 @@ def test_dygraph_api_out(self):
np.testing.assert_allclose(out.numpy(), expected_out)


class TestUniqueAPI_Compatibility(unittest.TestCase):
def setUp(self):
self.x_np = np.random.random(size=[3, 5]).astype("float32")
self.place = (
core.CUDAPlace(0)
if core.is_compiled_with_cuda()
else core.CPUPlace()
)

def test_dygraph(self):
paddle.disable_static()
out = paddle.unique(paddle.to_tensor(self.x_np))
expected_out = np.unique(self.x_np)
np.testing.assert_allclose(out.numpy(), expected_out)

def test_static(self):
paddle.enable_static()
x = paddle.static.data(name='x', shape=[-1, 5], dtype='float32')
out1 = paddle.unique(x)
out2 = paddle.unique(input=x)
exe = paddle.static.Executor(self.place)
res = exe.run(
feed={
'x': self.x_np.reshape(3, 5),
},
fetch_list=[out1, out2],
)
expected_out = np.unique(self.x_np)
for result in res:
np.testing.assert_array_equal(result, expected_out)
paddle.disable_static()

def test_dygraph_sorted(self):
paddle.disable_static()
out = paddle.unique(paddle.to_tensor(self.x_np), sorted=True)
expected_out = np.unique(self.x_np)
np.testing.assert_allclose(out.numpy(), expected_out)

def test_dygraph_axis(self):
paddle.disable_static()
out = paddle.unique(paddle.to_tensor(self.x_np), sorted=True, dim=1)
expected_out = np.unique(self.x_np, axis=1)
np.testing.assert_allclose(out.numpy(), expected_out)


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