Skip to content
Merged
Prev Previous commit
Next Next commit
fix
  • Loading branch information
co63oc committed Nov 24, 2025
commit 46814a072aacb39e4088feeaa4d089ca2a12cf1d
70 changes: 70 additions & 0 deletions python/paddle/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
'max',
'median',
'nanmedian',
'unique',
]


Expand Down Expand Up @@ -913,6 +914,75 @@ def GetShapeOnDimInRange(shape, dim: int) -> int:
return tuple(_C_ops.split(tensor, split_size_or_sections, dim))


@ForbidKeywordsDecorator(
illegal_keys={"x", "axis", "name"},
func_name="paddle.compat.unique",
correct_name="paddle.unique",
)
def unique(
input: Tensor,
sorted: bool = True,
return_inverse: bool = False,
return_counts: bool = False,
dim=None,
) -> Tensor | tuple[Tensor, ...]:
r"""
Returns the unique elements of `input` in ascending order.

Args:
input(Tensor): The input tensor, it's data type should be float32, float64, int32, int64.
sorted(bool, optional): Does not affect the return result, used for compatibility.
return_inverse(bool, optional): If True, also return the indices for where elements in
the original input ended up in the returned unique tensor.
return_counts(bool, optional): If True, also return the counts for each unique element.
dim(int, optional): The axis to apply unique. If None, the input will be flattened.
Default: None.

Returns:
tuple (output, inverse_indices, counts). `output` is the unique tensor for `input`. \
`inverse_indices` is provided only if `return_inverse` \
is True. `counts` is provided only if `return_counts` is True.

Examples:
.. code-block:: python

>>> import paddle

>>> x = paddle.to_tensor([2, 3, 3, 1, 5, 3])
>>> unique = paddle.compat.unique(x)
>>> print(unique)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 2, 3, 5])

>>> _, inverse_indices, counts = paddle.compat.unique(x, return_inverse=True, return_counts=True)
>>> print(inverse_indices)
Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 2, 2, 0, 3, 2])
>>> print(counts)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 1, 3, 1])

>>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3]])
>>> unique = paddle.compat.unique(x)
>>> print(unique)
Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 1, 2, 3])

>>> unique = paddle.compat.unique(x, dim=0)
>>> print(unique)
Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
[[2, 1, 3],
[3, 0, 1]])
"""
return paddle.unique(
input,
return_inverse=return_inverse,
return_counts=return_counts,
axis=dim,
sorted=sorted,
)


def warning_about_fake_interface(name: str):
warnings.warn(
f"The interface '{name}' is a fake implementation for torch compatibility. "
Expand Down
85 changes: 85 additions & 0 deletions test/legacy_test/test_compat_unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np

import paddle
from paddle import base


class TestCompatUniqueAPI(unittest.TestCase):
def test_basic(self):
paddle.disable_static()
x = paddle.to_tensor([2, 3, 3, 1, 5, 3])
result = paddle.compat.unique(x)
expected = paddle.to_tensor([1, 2, 3, 5], dtype='int64')
np.testing.assert_allclose(result.numpy(), expected.numpy())

_, inverse_indices, counts = paddle.compat.unique(
x, return_inverse=True, return_counts=True
)
expected_indices = paddle.to_tensor([1, 2, 2, 0, 3, 2], dtype='int64')
expected_counts = paddle.to_tensor([1, 1, 3, 1], dtype='int64')
np.testing.assert_allclose(
inverse_indices.numpy(), expected_indices.numpy()
)
np.testing.assert_allclose(counts.numpy(), expected_counts.numpy())

x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3]])
result = paddle.compat.unique(x)
expected = paddle.to_tensor([0, 1, 2, 3], dtype='int64')
np.testing.assert_allclose(result.numpy(), expected.numpy())
paddle.enable_static()

def test_static(self):
paddle.enable_static()

with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
x = paddle.static.data(name='input', shape=[6], dtype='int64')
out, inverse_indices, counts = paddle.compat.unique(
x, return_inverse=True, return_counts=True
)

exe = base.Executor(base.CPUPlace())
x_data = np.array([2, 3, 3, 1, 5, 3], dtype='int64')
result = exe.run(
feed={'input': x_data},
fetch_list=[out, inverse_indices, counts],
)

np.testing.assert_allclose(result[1], [1, 2, 2, 0, 3, 2])
np.testing.assert_allclose(result[2], [1, 1, 3, 1])

with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
x = paddle.static.data(name='input', shape=[3, 3], dtype='int64')
out = paddle.compat.unique(x)

exe = base.Executor(base.CPUPlace())
x_data = np.array([[2, 1, 3], [3, 0, 1], [2, 1, 3]], dtype='int64')
result = exe.run(feed={'input': x_data}, fetch_list=[out])

expected = np.array([0, 1, 2, 3], dtype='int64')
np.testing.assert_allclose(result[0], expected)

paddle.disable_static()


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