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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. from mindspore import Tensor
  21. from mindspore.ops import operations as P
  22. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  23. class OpNetWrapper(nn.Cell):
  24. def __init__(self, op):
  25. super(OpNetWrapper, self).__init__()
  26. self.op = op
  27. def construct(self, *inputs):
  28. return self.op(*inputs)
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_cpu
  31. @pytest.mark.env_onecard
  32. def test_case1_basic_func():
  33. op = P.GatherNd()
  34. op_wrapper = OpNetWrapper(op)
  35. indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
  36. params = Tensor(np.array([[0, 1], [2, 3]]), mindspore.float32)
  37. outputs = op_wrapper(params, indices)
  38. print(outputs)
  39. expected = [0, 3]
  40. assert np.allclose(outputs.asnumpy(), np.array(expected))
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_cpu
  43. @pytest.mark.env_onecard
  44. def test_case2_indices_to_matrix():
  45. op = P.GatherNd()
  46. op_wrapper = OpNetWrapper(op)
  47. indices = Tensor(np.array([[1], [0]]), mindspore.int32)
  48. params = Tensor(np.array([[0, 1], [2, 3]]), mindspore.float32)
  49. outputs = op_wrapper(params, indices)
  50. print(outputs)
  51. expected = [[2, 3], [0, 1]]
  52. assert np.allclose(outputs.asnumpy(), np.array(expected))
  53. @pytest.mark.level0
  54. @pytest.mark.platform_x86_cpu
  55. @pytest.mark.env_onecard
  56. def test_case3_indices_to_3d_tensor():
  57. op = P.GatherNd()
  58. op_wrapper = OpNetWrapper(op)
  59. indices = Tensor(np.array([[1]]), mindspore.int32) # (1, 1)
  60. params = Tensor(np.array([[[0, 1], [2, 3]],
  61. [[4, 5], [6, 7]]]), mindspore.float32) # (2, 2, 2)
  62. outputs = op_wrapper(params, indices)
  63. print(outputs)
  64. expected = [[[4, 5], [6, 7]]] # (1, 2, 2)
  65. assert np.allclose(outputs.asnumpy(), np.array(expected))
  66. @pytest.mark.level0
  67. @pytest.mark.platform_x86_cpu
  68. @pytest.mark.env_onecard
  69. def test_case4():
  70. op = P.GatherNd()
  71. op_wrapper = OpNetWrapper(op)
  72. indices = Tensor(np.array([[0, 1], [1, 0]]), mindspore.int32) # (2, 2)
  73. params = Tensor(np.array([[[0, 1], [2, 3]],
  74. [[4, 5], [6, 7]]]), mindspore.float32) # (2, 2, 2)
  75. outputs = op_wrapper(params, indices)
  76. print(outputs)
  77. expected = [[2, 3], [4, 5]] # (2, 2)
  78. assert np.allclose(outputs.asnumpy(), np.array(expected))
  79. @pytest.mark.level0
  80. @pytest.mark.platform_x86_cpu
  81. @pytest.mark.env_onecard
  82. def test_case5():
  83. op = P.GatherNd()
  84. op_wrapper = OpNetWrapper(op)
  85. indices = Tensor(np.array([[0, 0, 1], [1, 0, 1]]), mindspore.int32) # (2, 3)
  86. params = Tensor(np.array([[[0, 1], [2, 3]],
  87. [[4, 5], [6, 7]]]), mindspore.float32) # (2, 2, 2)
  88. outputs = op_wrapper(params, indices)
  89. print(outputs)
  90. expected = [1, 5] # (2,)
  91. assert np.allclose(outputs.asnumpy(), np.array(expected))
  92. @pytest.mark.level0
  93. @pytest.mark.platform_x86_cpu
  94. @pytest.mark.env_onecard
  95. def test_case6():
  96. op = P.GatherNd()
  97. op_wrapper = OpNetWrapper(op)
  98. indices = Tensor(np.array([[[0, 0]], [[0, 1]]]), mindspore.int32) # (2, 1, 2)
  99. params = Tensor(np.array([[[0, 1], [2, 3]],
  100. [[4, 5], [6, 7]]]), mindspore.float32) # (2, 2, 2)
  101. outputs = op_wrapper(params, indices)
  102. print(outputs)
  103. expected = [[[0, 1]], [[2, 3]]] # (2, 1, 2)
  104. assert np.allclose(outputs.asnumpy(), np.array(expected))
  105. @pytest.mark.level0
  106. @pytest.mark.platform_x86_cpu
  107. @pytest.mark.env_onecard
  108. def test_case7():
  109. op = P.GatherNd()
  110. op_wrapper = OpNetWrapper(op)
  111. indices = Tensor(np.array([[[1]], [[0]]]), mindspore.int32) # (2, 1, 1)
  112. params = Tensor(np.array([[[0, 1], [2, 3]],
  113. [[4, 5], [6, 7]]]), mindspore.float32) # (2, 2, 2)
  114. outputs = op_wrapper(params, indices)
  115. print(outputs)
  116. expected = [[[[4, 5], [6, 7]]], [[[0, 1], [2, 3]]]] # (2, 1, 2, 2)
  117. assert np.allclose(outputs.asnumpy(), np.array(expected))
  118. @pytest.mark.level0
  119. @pytest.mark.platform_x86_cpu
  120. @pytest.mark.env_onecard
  121. def test_case8():
  122. op = P.GatherNd()
  123. op_wrapper = OpNetWrapper(op)
  124. indices = Tensor(np.array([[[0, 1], [1, 0]], [[0, 0], [1, 1]]]), mindspore.int32) # (2, 2, 2)
  125. params = Tensor(np.array([[[0, 1], [2, 3]],
  126. [[4, 5], [6, 7]]]), mindspore.float32) # (2, 2, 2)
  127. outputs = op_wrapper(params, indices)
  128. print(outputs)
  129. expected = [[[2, 3], [4, 5]], [[0, 1], [6, 7]]] # (2, 2, 2)
  130. assert np.allclose(outputs.asnumpy(), np.array(expected))
  131. @pytest.mark.level0
  132. @pytest.mark.platform_x86_cpu
  133. @pytest.mark.env_onecard
  134. def test_case9():
  135. op = P.GatherNd()
  136. op_wrapper = OpNetWrapper(op)
  137. indices = Tensor(np.array([[[0, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 1, 0]]]), mindspore.int32) # (2, 2, 3)
  138. params = Tensor(np.array([[[0, 1], [2, 3]],
  139. [[4, 5], [6, 7]]]), mindspore.int64) # (2, 2, 2)
  140. outputs = op_wrapper(params, indices)
  141. print(outputs)
  142. expected = [[1, 5], [3, 6]] # (2, 2, 2)
  143. assert np.allclose(outputs.asnumpy(), np.array(expected))
  144. if __name__ == '__main__':
  145. test_case1_basic_func()
  146. test_case2_indices_to_matrix()
  147. test_case3_indices_to_3d_tensor()
  148. test_case4()
  149. test_case5()
  150. test_case6()
  151. test_case7()
  152. test_case8()
  153. test_case9()