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_gather_op.py 5.1 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright 2019-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. import mindspore.nn as nn
  19. import mindspore as ms
  20. from mindspore import Tensor
  21. from mindspore.ops import operations as P
  22. class GatherNet(nn.Cell):
  23. def __init__(self, dim=0):
  24. super(GatherNet, self).__init__()
  25. self.gather = P.GatherD()
  26. self.dim = dim
  27. def construct(self, x, index):
  28. return self.gather(x, self.dim, index)
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_gpu_training
  31. @pytest.mark.env_onecard
  32. def test_gather_pynative_fp32_int32():
  33. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  34. error = 1e-3
  35. x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float32)
  36. dim = 1
  37. index = Tensor(np.array([[0, 0], [1, 0]]), ms.int32)
  38. expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float32)
  39. output = P.GatherD()(x, dim, index)
  40. diff = output.asnumpy() - expect
  41. assert np.all(diff < error)
  42. @pytest.mark.level0
  43. @pytest.mark.platform_x86_gpu_training
  44. @pytest.mark.env_onecard
  45. def test_gather_pynative_fp32_int64():
  46. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  47. error = 1e-3
  48. x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float32)
  49. dim = 1
  50. index = Tensor(np.array([[0, 0], [1, 0]]), ms.int64)
  51. expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float32)
  52. output = P.GatherD()(x, dim, index)
  53. diff = output.asnumpy() - expect
  54. assert np.all(diff < error)
  55. @pytest.mark.level0
  56. @pytest.mark.platform_x86_gpu_training
  57. @pytest.mark.env_onecard
  58. def test_gather_pynative_fp16_int32():
  59. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  60. error = 1e-3
  61. x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float16)
  62. dim = 1
  63. index = Tensor(np.array([[0, 0], [1, 0]]), ms.int32)
  64. expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float16)
  65. output = P.GatherD()(x, dim, index)
  66. diff = output.asnumpy() - expect
  67. assert np.all(diff < error)
  68. @pytest.mark.level0
  69. @pytest.mark.platform_x86_gpu_training
  70. @pytest.mark.env_onecard
  71. def test_gather_pynative_fp16_int64():
  72. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  73. error = 1e-3
  74. x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float16)
  75. dim = 1
  76. index = Tensor(np.array([[0, 0], [1, 0]]), ms.int64)
  77. expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float16)
  78. output = P.GatherD()(x, dim, index)
  79. diff = output.asnumpy() - expect
  80. assert np.all(diff < error)
  81. def test_gather_graph_fp32_int32():
  82. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  83. error = 1e-3
  84. x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float32)
  85. dim = 1
  86. index = Tensor(np.array([[0, 0], [1, 0]]), ms.int32)
  87. expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float32)
  88. gather = GatherNet(dim)
  89. output = gather(x, index)
  90. diff = output.asnumpy() - expect
  91. assert np.all(diff < error)
  92. def test_gather_graph_fp32_int64():
  93. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  94. error = 1e-3
  95. x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float32)
  96. dim = 1
  97. index = Tensor(np.array([[0, 0], [1, 0]]), ms.int64)
  98. expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float32)
  99. gather = GatherNet(dim)
  100. output = gather(x, index)
  101. diff = output.asnumpy() - expect
  102. assert np.all(diff < error)
  103. def test_gather_graph_fp16_int32():
  104. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  105. error = 1e-3
  106. x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float16)
  107. dim = 1
  108. index = Tensor(np.array([[0, 0], [1, 0]]), ms.int32)
  109. expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float16)
  110. gather = GatherNet(dim)
  111. output = gather(x, index)
  112. diff = output.asnumpy() - expect
  113. assert np.all(diff < error)
  114. def test_gather_graph_fp16_int64():
  115. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  116. error = 1e-3
  117. x = Tensor(np.array([[1.303, 2.333], [3.232, 4.235]]), ms.float16)
  118. dim = 1
  119. index = Tensor(np.array([[0, 0], [1, 0]]), ms.int64)
  120. expect = np.array([[1.303, 1.303], [4.235, 3.232]], np.float16)
  121. gather = GatherNet(dim)
  122. output = gather(x, index)
  123. diff = output.asnumpy() - expect
  124. assert np.all(diff < error)