|
| 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