Skip to content

Commit cb81162

Browse files
authored
[API compatibility] add msort api (#74421)
* add msort api * change msort api without out param * replace msort param x with input * change copyright time
1 parent 0b7e62c commit cb81162

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

python/paddle/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@
579579
kthvalue,
580580
masked_select,
581581
mode,
582+
msort,
582583
nonzero,
583584
searchsorted,
584585
sort,
@@ -879,6 +880,7 @@
879880
'summary',
880881
'flops',
881882
'sort',
883+
'msort',
882884
'searchsorted',
883885
'bucketize',
884886
'split',

python/paddle/tensor/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@
459459
kthvalue,
460460
masked_select,
461461
mode,
462+
msort,
462463
nonzero,
463464
searchsorted,
464465
sort,
@@ -726,6 +727,7 @@
726727
'index_select',
727728
'nonzero',
728729
'sort',
730+
'msort',
729731
'index_sample',
730732
'mean',
731733
'std',

python/paddle/tensor/search.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,44 @@ def sort(
676676
return out
677677

678678

679+
def msort(input: Tensor) -> Tensor:
680+
"""
681+
682+
Sorts the input along the given axis = 0, and returns the sorted output tensor. The sort algorithm is ascending.
683+
684+
Args:
685+
input (Tensor): An input N-D Tensor with type float32, float64, int16,
686+
int32, int64, uint8.
687+
688+
Returns:
689+
Tensor, sorted tensor(with the same shape and data type as ``input``).
690+
691+
Examples:
692+
693+
.. code-block:: python
694+
695+
>>> import paddle
696+
697+
>>> x = paddle.to_tensor([[[5,8,9,5],
698+
... [0,0,1,7],
699+
... [6,9,2,4]],
700+
... [[5,2,4,2],
701+
... [4,7,7,9],
702+
... [1,7,0,6]]],
703+
... dtype='float32')
704+
>>> out1 = paddle.msort(input=x)
705+
>>> print(out1.numpy())
706+
[[[5. 2. 4. 2.]
707+
[0. 0. 1. 7.]
708+
[1. 7. 0. 4.]]
709+
[[5. 8. 9. 5.]
710+
[4. 7. 7. 9.]
711+
[6. 9. 2. 6.]]]
712+
"""
713+
714+
return sort(input, axis=0)
715+
716+
679717
def mode(
680718
x: Tensor, axis: int = -1, keepdim: bool = False, name: str | None = None
681719
) -> tuple[Tensor, Tensor]:

test/legacy_test/test_msort_op.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
17+
import numpy as np
18+
19+
import paddle
20+
from paddle import base
21+
from paddle.base import core
22+
23+
24+
class TestMsortOnCPU(unittest.TestCase):
25+
def setUp(self):
26+
self.place = core.CPUPlace()
27+
28+
def test_api_0(self):
29+
with base.program_guard(base.Program()):
30+
input = paddle.static.data(
31+
name="input", shape=[2, 3, 4], dtype="float32"
32+
)
33+
output = paddle.msort(input=input)
34+
exe = base.Executor(self.place)
35+
data = np.array(
36+
[
37+
[[5, 8, 9, 5], [0, 0, 1, 7], [6, 9, 2, 4]],
38+
[[5, 2, 4, 2], [4, 7, 7, 9], [1, 7, 0, 6]],
39+
],
40+
dtype='float32',
41+
)
42+
(result,) = exe.run(feed={'input': data}, fetch_list=[output])
43+
np_result = np.sort(result, axis=0)
44+
self.assertEqual((result == np_result).all(), True)
45+
46+
47+
class TestMsortOnGPU(TestMsortOnCPU):
48+
def init_place(self):
49+
if core.is_compiled_with_cuda():
50+
self.place = core.CUDAPlace(0)
51+
else:
52+
self.place = core.CPUPlace()
53+
54+
55+
class TestMsortDygraph(unittest.TestCase):
56+
def setUp(self):
57+
self.input_data = np.random.rand(10, 10)
58+
if core.is_compiled_with_cuda():
59+
self.place = core.CUDAPlace(0)
60+
else:
61+
self.place = core.CPUPlace()
62+
63+
def test_api_0(self):
64+
paddle.disable_static(self.place)
65+
var_x = paddle.to_tensor(self.input_data)
66+
out = paddle.msort(input=var_x)
67+
self.assertEqual(
68+
(np.sort(self.input_data, axis=0) == out.numpy()).all(), True
69+
)
70+
paddle.enable_static()
71+
72+
73+
if __name__ == '__main__':
74+
unittest.main()

0 commit comments

Comments
 (0)