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_unique_op.py 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. class NetUnique(nn.Cell):
  22. def __init__(self):
  23. super(NetUnique, self).__init__()
  24. self.unique = P.Unique()
  25. def construct(self, x):
  26. x_unique, x_idx = self.unique(x)
  27. return x_unique, x_idx
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_gpu_training
  30. @pytest.mark.env_onecard
  31. def test_unique_1d():
  32. x = Tensor(np.array([4, 5, 1, 2, 3, 3, 4, 5]).astype(np.float32))
  33. exp_output = np.array([1, 2, 3, 4, 5]).astype(np.float32)
  34. exp_idx = np.array([3, 4, 0, 1, 2, 2, 3, 4]).astype(np.int32)
  35. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  36. net = NetUnique()
  37. x_unique, x_idx = net(x)
  38. assert (x_unique.asnumpy() == exp_output).all()
  39. assert (x_idx.asnumpy() == exp_idx).all()
  40. @pytest.mark.level0
  41. @pytest.mark.platform_x86_gpu_training
  42. @pytest.mark.env_onecard
  43. def test_unique_1d_float():
  44. x = Tensor(np.array([0.4, 0.5, 1.23, 2.2, 12.43, 12.43, 0.4, 0.5]).astype(np.float32))
  45. exp_output = np.array([0.4, 0.5, 1.23, 2.2, 12.43]).astype(np.float32)
  46. exp_idx = np.array([0, 1, 2, 3, 4, 4, 0, 1]).astype(np.int32)
  47. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  48. net = NetUnique()
  49. x_unique, x_idx = net(x)
  50. assert (x_unique.asnumpy() == exp_output).all()
  51. assert (x_idx.asnumpy() == exp_idx).all()
  52. @pytest.mark.level0
  53. @pytest.mark.platform_x86_gpu_training
  54. @pytest.mark.env_onecard
  55. def test_unique_1d_sorted():
  56. x = Tensor(np.array([1, 1, 2, 4, 4, 4, 7, 8, 8]).astype(np.float32))
  57. exp_output = np.array([1, 2, 4, 7, 8]).astype(np.float32)
  58. exp_idx = np.array([0, 0, 1, 2, 2, 2, 3, 4, 4]).astype(np.int32)
  59. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  60. net = NetUnique()
  61. x_unique, x_idx = net(x)
  62. assert (x_unique.asnumpy() == exp_output).all()
  63. assert (x_idx.asnumpy() == exp_idx).all()
  64. @pytest.mark.level0
  65. @pytest.mark.platform_x86_gpu_training
  66. @pytest.mark.env_onecard
  67. def test_unique_zeros():
  68. x = Tensor(np.zeros(1000).astype(np.float32))
  69. exp_output = np.zeros(1).astype(np.float32)
  70. exp_idx = np.zeros(1000).astype(np.int32)
  71. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  72. net = NetUnique()
  73. x_unique, x_idx = net(x)
  74. assert (x_unique.asnumpy() == exp_output).all()
  75. assert (x_idx.asnumpy() == exp_idx).all()
  76. @pytest.mark.level0
  77. @pytest.mark.platform_x86_gpu_training
  78. @pytest.mark.env_onecard
  79. def test_unique_large():
  80. x_np1 = np.arange(100)
  81. x_np2 = np.arange(100, 200)
  82. x_np3 = np.arange(200, 300)
  83. x_np = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3))
  84. x = Tensor(x_np.astype(np.float32))
  85. exp_output = np.arange(300).astype(np.float32)
  86. exp_idx = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3)).astype(np.int32)
  87. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  88. net = NetUnique()
  89. x_unique, x_idx = net(x)
  90. assert (x_unique.asnumpy() == exp_output).all()
  91. assert (x_idx.asnumpy() == exp_idx).all()
  92. @pytest.mark.level0
  93. @pytest.mark.platform_x86_gpu_training
  94. @pytest.mark.env_onecard
  95. def test_unique_1d_half():
  96. x = Tensor(np.array([0.4, 0.5, 1.23, 2.2, 12.43, 12.43, 0.4, 0.5]).astype(np.float16))
  97. exp_output = np.array([0.4, 0.5, 1.23, 2.2, 12.43]).astype(np.float16)
  98. exp_idx = np.array([0, 1, 2, 3, 4, 4, 0, 1]).astype(np.int32)
  99. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  100. net = NetUnique()
  101. x_unique, x_idx = net(x)
  102. assert (x_unique.asnumpy() == exp_output).all()
  103. assert (x_idx.asnumpy() == exp_idx).all()
  104. @pytest.mark.level0
  105. @pytest.mark.platform_x86_gpu_training
  106. @pytest.mark.env_onecard
  107. def test_unique_1d_sorted_half():
  108. x = Tensor(np.array([1, 1, 2, 4, 4, 4, 7, 8, 8]).astype(np.float16))
  109. exp_output = np.array([1, 2, 4, 7, 8]).astype(np.float16)
  110. exp_idx = np.array([0, 0, 1, 2, 2, 2, 3, 4, 4]).astype(np.int32)
  111. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  112. net = NetUnique()
  113. x_unique, x_idx = net(x)
  114. assert (x_unique.asnumpy() == exp_output).all()
  115. assert (x_idx.asnumpy() == exp_idx).all()
  116. @pytest.mark.level0
  117. @pytest.mark.platform_x86_gpu_training
  118. @pytest.mark.env_onecard
  119. def test_unique_zeros_half():
  120. x = Tensor(np.zeros(1000).astype(np.float16))
  121. exp_output = np.zeros(1).astype(np.float16)
  122. exp_idx = np.zeros(1000).astype(np.int32)
  123. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  124. net = NetUnique()
  125. x_unique, x_idx = net(x)
  126. assert (x_unique.asnumpy() == exp_output).all()
  127. assert (x_idx.asnumpy() == exp_idx).all()
  128. @pytest.mark.level0
  129. @pytest.mark.platform_x86_gpu_training
  130. @pytest.mark.env_onecard
  131. def test_unique_large_half():
  132. x_np1 = np.arange(100)
  133. x_np2 = np.arange(100, 200)
  134. x_np3 = np.arange(200, 300)
  135. x_np = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3))
  136. x = Tensor(x_np.astype(np.float16))
  137. exp_output = np.arange(300).astype(np.float16)
  138. exp_idx = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3)).astype(np.int32)
  139. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  140. net = NetUnique()
  141. x_unique, x_idx = net(x)
  142. assert (x_unique.asnumpy() == exp_output).all()
  143. assert (x_idx.asnumpy() == exp_idx).all()
  144. @pytest.mark.level0
  145. @pytest.mark.platform_x86_gpu_training
  146. @pytest.mark.env_onecard
  147. def test_unique_1d_int32():
  148. x = Tensor(np.array([4, 5, 1, 2, 3, 3, 4, 5]).astype(np.int32))
  149. exp_output = np.array([1, 2, 3, 4, 5]).astype(np.int32)
  150. exp_idx = np.array([3, 4, 0, 1, 2, 2, 3, 4]).astype(np.int32)
  151. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  152. net = NetUnique()
  153. x_unique, x_idx = net(x)
  154. assert (x_unique.asnumpy() == exp_output).all()
  155. assert (x_idx.asnumpy() == exp_idx).all()
  156. @pytest.mark.level0
  157. @pytest.mark.platform_x86_gpu_training
  158. @pytest.mark.env_onecard
  159. def test_unique_1d_sorted_int32():
  160. x = Tensor(np.array([1, 1, 2, 4, 4, 4, 7, 8, 8]).astype(np.int32))
  161. exp_output = np.array([1, 2, 4, 7, 8]).astype(np.int32)
  162. exp_idx = np.array([0, 0, 1, 2, 2, 2, 3, 4, 4]).astype(np.int32)
  163. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  164. net = NetUnique()
  165. x_unique, x_idx = net(x)
  166. assert (x_unique.asnumpy() == exp_output).all()
  167. assert (x_idx.asnumpy() == exp_idx).all()
  168. @pytest.mark.level0
  169. @pytest.mark.platform_x86_gpu_training
  170. @pytest.mark.env_onecard
  171. def test_unique_zeros_int32():
  172. x = Tensor(np.zeros(1000).astype(np.int32))
  173. exp_output = np.zeros(1).astype(np.int32)
  174. exp_idx = np.zeros(1000).astype(np.int32)
  175. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  176. net = NetUnique()
  177. x_unique, x_idx = net(x)
  178. assert (x_unique.asnumpy() == exp_output).all()
  179. assert (x_idx.asnumpy() == exp_idx).all()
  180. @pytest.mark.level0
  181. @pytest.mark.platform_x86_gpu_training
  182. @pytest.mark.env_onecard
  183. def test_unique_large_int32():
  184. x_np1 = np.arange(100)
  185. x_np2 = np.arange(100, 200)
  186. x_np3 = np.arange(200, 300)
  187. x_np = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3))
  188. x = Tensor(x_np.astype(np.int32))
  189. exp_output = np.arange(300).astype(np.int32)
  190. exp_idx = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3)).astype(np.int32)
  191. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  192. net = NetUnique()
  193. x_unique, x_idx = net(x)
  194. assert (x_unique.asnumpy() == exp_output).all()
  195. assert (x_idx.asnumpy() == exp_idx).all()