You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_topk_op.py 3.1 kB

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