• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
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
16import numpy as np
17import pytest
18
19import mindspore.context as context
20from mindspore import Tensor
21from mindspore.ops import operations as P
22
23
24@pytest.mark.level0
25@pytest.mark.platform_x86_cpu
26@pytest.mark.env_onecard
27def test_topk():
28    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
29
30    x_np = np.random.rand(3, 4).astype(np.float32)
31    k = 4
32    ms_output = P.TopK(True)(Tensor(x_np), k)
33    np_output = np.sort(x_np, axis=-1)[..., ::-1][..., 0:k]
34    assert np.allclose(ms_output[0].asnumpy(), np_output)
35
36    x_np = np.random.rand(3, 4).astype(np.float32)
37    k = 4
38    ms_output = P.TopK(False)(Tensor(x_np), k)
39    assert np.allclose(ms_output[0].asnumpy(), x_np)
40
41    x_np = np.random.rand(2, 3, 4).astype(np.float32)
42    k = 2
43    ms_output = P.TopK(True)(Tensor(x_np), k)
44    np_output = np.sort(x_np, axis=-1)[..., ::-1][..., 0:k]
45    assert np.allclose(ms_output[0].asnumpy(), np_output)
46
47    x_np = np.random.rand(512, 1024).astype(np.float32)
48    k = 512
49    ms_output = P.TopK(True)(Tensor(x_np), k)
50    np_output = np.sort(x_np, axis=-1)[..., ::-1][..., 0:k]
51    assert np.allclose(ms_output[0].asnumpy(), np_output)
52
53    # sorted elements num greater than max thread per block
54    x_np = np.random.rand(512, 2048).astype(np.float32)
55    k = 1
56    ms_output = P.TopK(True)(Tensor(x_np), k)
57    np_output = np.sort(x_np, axis=-1)[..., ::-1][..., 0:k]
58    assert np.allclose(ms_output[0].asnumpy(), np_output)
59
60    x_np = np.random.rand(512, 2048).astype(np.float32)
61    k = 2048
62    ms_output = P.TopK(True)(Tensor(x_np), k)
63    np_output = np.sort(x_np, axis=-1)[..., ::-1][..., 0:k]
64    assert np.allclose(ms_output[0].asnumpy(), np_output)
65
66    # sorted elements num greater than max share memory per block
67    x_np = np.random.rand(512, 40960).astype(np.float32)
68    k = 1
69    ms_output = P.TopK(True)(Tensor(x_np), k)
70    np_output = np.sort(x_np, axis=-1)[..., ::-1][..., 0:k]
71    assert np.allclose(ms_output[0].asnumpy(), np_output)
72
73    x_np = np.random.rand(512, 40960).astype(np.float32)
74    k = 40960
75    ms_output = P.TopK(True)(Tensor(x_np), k)
76    np_output = np.sort(x_np, axis=-1)[..., ::-1][..., 0:k]
77    assert np.allclose(ms_output[0].asnumpy(), np_output)
78
79    x_np = np.random.rand(512, 40960).astype(np.float32)
80    k = 40960
81    ms_output = P.TopK(False)(Tensor(x_np), k)
82    assert np.allclose(ms_output[0].asnumpy(), x_np)
83