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_gathernd_op.py 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. from mindspore import Tensor
  18. from mindspore.ops import operations as P
  19. import mindspore.nn as nn
  20. import mindspore.context as context
  21. class GatherNdNet(nn.Cell):
  22. def __init__(self):
  23. super(GatherNdNet, self).__init__()
  24. self.gathernd = P.GatherNd()
  25. def construct(self, x, indices):
  26. return self.gathernd(x, indices)
  27. @pytest.mark.level0
  28. @pytest.mark.platform_x86_gpu_training
  29. @pytest.mark.env_onecard
  30. def test_gathernd0():
  31. x = Tensor(np.arange(3 * 2, dtype=np.float32).reshape(3, 2))
  32. indices = Tensor(np.array([[1, 1], [0, 1]]).astype(np.int32))
  33. expect = np.array([3., 1.])
  34. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  35. gathernd = GatherNdNet()
  36. output = gathernd(x, indices)
  37. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  38. diff = output.asnumpy() - expect
  39. assert np.all(diff < error)
  40. assert np.all(-diff < error)
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_gpu_traning
  43. @pytest.mark.env_onecard
  44. def test_gathernd1():
  45. x = Tensor(np.arange(2 * 3 * 4 * 5, dtype=np.float32).reshape(2, 3, 4, 5))
  46. indices = Tensor(np.array([[[[[l, k, j, i] for i in [1, 3, 4]] for j in range(4)]
  47. for k in range(3)] for l in range(2)], dtype='i4'))
  48. expect = np.array([[[[1., 3., 4.],
  49. [6., 8., 9.],
  50. [11., 13., 14.],
  51. [16., 18., 19.]],
  52. [[21., 23., 24.],
  53. [26., 28., 29.],
  54. [31., 33., 34.],
  55. [36., 38., 39.]],
  56. [[41., 43., 44.],
  57. [46., 48., 49.],
  58. [51., 53., 54.],
  59. [56., 58., 59.]]],
  60. [[[61., 63., 64.],
  61. [66., 68., 69.],
  62. [71., 73., 74.],
  63. [76., 78., 79.]],
  64. [[81., 83., 84.],
  65. [86., 88., 89.],
  66. [91., 93., 94.],
  67. [96., 98., 99.]],
  68. [[101., 103., 104.],
  69. [106., 108., 109.],
  70. [111., 113., 114.],
  71. [116., 118., 119.]]]])
  72. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  73. gather = GatherNdNet()
  74. output = gather(x, indices)
  75. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  76. diff = output.asnumpy() - expect
  77. assert np.all(diff < error)
  78. assert np.all(-diff < error)
  79. @pytest.mark.level0
  80. @pytest.mark.platform_x86_gpu_traning
  81. @pytest.mark.env_onecard
  82. def test_gathernd2():
  83. x = Tensor(np.array([[4., 5., 4., 1., 5.],
  84. [4., 9., 5., 6., 4.],
  85. [9., 8., 4., 3., 6.],
  86. [0., 4., 2., 2., 8.],
  87. [1., 8., 6., 2., 8.],
  88. [8., 1., 9., 7., 3.],
  89. [7., 9., 2., 5., 7.],
  90. [9., 8., 6., 8., 5.],
  91. [3., 7., 2., 7., 4.],
  92. [4., 2., 8., 2., 9.]]).astype(np.float16))
  93. indices = Tensor(np.array([[4000], [1], [300000]]).astype(np.int32))
  94. expect = np.array([[0., 0., 0., 0., 0.],
  95. [4., 9., 5., 6., 4.],
  96. [0., 0., 0., 0., 0.]])
  97. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  98. gathernd = GatherNdNet()
  99. output = gathernd(x, indices)
  100. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  101. diff = output.asnumpy() - expect
  102. assert np.all(diff < error)
  103. assert np.all(-diff < error)
  104. @pytest.mark.level0
  105. @pytest.mark.platform_x86_gpu_traning
  106. @pytest.mark.env_onecard
  107. def test_gathernd3():
  108. x = Tensor(np.array([[4, 5, 4, 1, 5],
  109. [4, 9, 5, 6, 4],
  110. [9, 8, 4, 3, 6],
  111. [0, 4, 2, 2, 8],
  112. [1, 8, 6, 2, 8],
  113. [8, 1, 9, 7, 3],
  114. [7, 9, 2, 5, 7],
  115. [9, 8, 6, 8, 5],
  116. [3, 7, 2, 7, 4],
  117. [4, 2, 8, 2, 9]]
  118. ).astype(np.int32))
  119. indices = Tensor(np.array([[4000], [1], [300000]]).astype(np.int32))
  120. expect = np.array([[0, 0, 0, 0, 0],
  121. [4, 9, 5, 6, 4],
  122. [0, 0, 0, 0, 0]])
  123. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  124. gathernd = GatherNdNet()
  125. output = gathernd(x, indices)
  126. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  127. diff = output.asnumpy() - expect
  128. assert np.all(diff < error)
  129. assert np.all(-diff < error)