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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. from mindspore.ops.operations import _inner_ops as inner
  22. class NetUnique(nn.Cell):
  23. def __init__(self):
  24. super(NetUnique, self).__init__()
  25. self.unique = P.Unique()
  26. def construct(self, x):
  27. x_unique, x_idx = self.unique(x)
  28. return x_unique, x_idx
  29. class NetUniqueDynamic(nn.Cell):
  30. def __init__(self):
  31. super(NetUniqueDynamic, self).__init__()
  32. self.convert = inner.GpuConvertToDynamicShape()
  33. self.unique = P.Unique()
  34. self.split = P.Split(0, 2)
  35. def construct(self, x):
  36. x_convert = self.convert(x)
  37. x_unique, x_idx = self.unique(x_convert)
  38. x_split = self.split(x_unique)
  39. return x_unique, x_idx, x_split
  40. @pytest.mark.level0
  41. @pytest.mark.platform_x86_gpu_training
  42. @pytest.mark.env_onecard
  43. def test_unique_1d():
  44. x = Tensor(np.array([4, 5, 1, 2, 3, 3, 4, 5]).astype(np.float32))
  45. exp_output = np.array([1, 2, 3, 4, 5]).astype(np.float32)
  46. exp_idx = np.array([3, 4, 0, 1, 2, 2, 3, 4]).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_float():
  56. x = Tensor(np.array([0.4, 0.5, 1.23, 2.2, 12.43, 12.43, 0.4, 0.5]).astype(np.float32))
  57. exp_output = np.array([0.4, 0.5, 1.23, 2.2, 12.43]).astype(np.float32)
  58. exp_idx = np.array([0, 1, 2, 3, 4, 4, 0, 1]).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_1d_sorted():
  68. x = Tensor(np.array([1, 1, 2, 4, 4, 4, 7, 8, 8]).astype(np.float32))
  69. exp_output = np.array([1, 2, 4, 7, 8]).astype(np.float32)
  70. exp_idx = np.array([0, 0, 1, 2, 2, 2, 3, 4, 4]).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_zeros():
  80. x = Tensor(np.zeros(1000).astype(np.float32))
  81. exp_output = np.zeros(1).astype(np.float32)
  82. exp_idx = np.zeros(1000).astype(np.int32)
  83. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  84. net = NetUnique()
  85. x_unique, x_idx = net(x)
  86. assert (x_unique.asnumpy() == exp_output).all()
  87. assert (x_idx.asnumpy() == exp_idx).all()
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_gpu_training
  90. @pytest.mark.env_onecard
  91. def test_unique_large():
  92. x_np1 = np.arange(100)
  93. x_np2 = np.arange(100, 200)
  94. x_np3 = np.arange(200, 300)
  95. x_np = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3))
  96. x = Tensor(x_np.astype(np.float32))
  97. exp_output = np.arange(300).astype(np.float32)
  98. 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)
  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_half():
  108. x = Tensor(np.array([0.4, 0.5, 1.23, 2.2, 12.43, 12.43, 0.4, 0.5]).astype(np.float16))
  109. exp_output = np.array([0.4, 0.5, 1.23, 2.2, 12.43]).astype(np.float16)
  110. exp_idx = np.array([0, 1, 2, 3, 4, 4, 0, 1]).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_1d_sorted_half():
  120. x = Tensor(np.array([1, 1, 2, 4, 4, 4, 7, 8, 8]).astype(np.float16))
  121. exp_output = np.array([1, 2, 4, 7, 8]).astype(np.float16)
  122. exp_idx = np.array([0, 0, 1, 2, 2, 2, 3, 4, 4]).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_zeros_half():
  132. x = Tensor(np.zeros(1000).astype(np.float16))
  133. exp_output = np.zeros(1).astype(np.float16)
  134. exp_idx = np.zeros(1000).astype(np.int32)
  135. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  136. net = NetUnique()
  137. x_unique, x_idx = net(x)
  138. assert (x_unique.asnumpy() == exp_output).all()
  139. assert (x_idx.asnumpy() == exp_idx).all()
  140. @pytest.mark.level0
  141. @pytest.mark.platform_x86_gpu_training
  142. @pytest.mark.env_onecard
  143. def test_unique_large_half():
  144. x_np1 = np.arange(100)
  145. x_np2 = np.arange(100, 200)
  146. x_np3 = np.arange(200, 300)
  147. x_np = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3))
  148. x = Tensor(x_np.astype(np.float16))
  149. exp_output = np.arange(300).astype(np.float16)
  150. 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)
  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_int32():
  160. x = Tensor(np.array([4, 5, 1, 2, 3, 3, 4, 5]).astype(np.int32))
  161. exp_output = np.array([1, 2, 3, 4, 5]).astype(np.int32)
  162. exp_idx = np.array([3, 4, 0, 1, 2, 2, 3, 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_1d_sorted_int32():
  172. x = Tensor(np.array([1, 1, 2, 4, 4, 4, 7, 8, 8]).astype(np.int32))
  173. exp_output = np.array([1, 2, 4, 7, 8]).astype(np.int32)
  174. exp_idx = np.array([0, 0, 1, 2, 2, 2, 3, 4, 4]).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_zeros_int32():
  184. x = Tensor(np.zeros(1000).astype(np.int32))
  185. exp_output = np.zeros(1).astype(np.int32)
  186. exp_idx = np.zeros(1000).astype(np.int32)
  187. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  188. net = NetUnique()
  189. x_unique, x_idx = net(x)
  190. assert (x_unique.asnumpy() == exp_output).all()
  191. assert (x_idx.asnumpy() == exp_idx).all()
  192. @pytest.mark.level0
  193. @pytest.mark.platform_x86_gpu_training
  194. @pytest.mark.env_onecard
  195. def test_unique_large_int32():
  196. x_np1 = np.arange(100)
  197. x_np2 = np.arange(100, 200)
  198. x_np3 = np.arange(200, 300)
  199. x_np = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3))
  200. x = Tensor(x_np.astype(np.int32))
  201. exp_output = np.arange(300).astype(np.int32)
  202. 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)
  203. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  204. net = NetUnique()
  205. x_unique, x_idx = net(x)
  206. assert (x_unique.asnumpy() == exp_output).all()
  207. assert (x_idx.asnumpy() == exp_idx).all()
  208. @pytest.mark.level0
  209. @pytest.mark.platform_x86_gpu_training
  210. @pytest.mark.env_onecard
  211. def test_unique_dynamic():
  212. x = Tensor(np.array([4, 5, 1, 2, 3, 3, 4, 5, 6]).astype(np.float32))
  213. expt_unique = np.array([1, 2, 3, 4, 5, 6]).astype(np.float32)
  214. expt_index = np.array([3, 4, 0, 1, 2, 2, 3, 4, 5]).astype(np.int32)
  215. expt_split = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
  216. x2 = Tensor(np.array([1, 1, 4, 4, 7, 8, 8]).astype(np.float32))
  217. expt_unique2 = np.array([1, 4, 7, 8]).astype(np.float32)
  218. expt_index2 = np.array([0, 0, 1, 1, 2, 3, 3]).astype(np.int32)
  219. expt_split2 = np.array([[1, 4], [7, 8]]).astype(np.float32)
  220. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  221. net = NetUniqueDynamic()
  222. x_unique, x_idx, x_split = net(x)
  223. assert (x_unique.asnumpy() == expt_unique).all()
  224. assert (x_idx.asnumpy() == expt_index).all()
  225. for i, out in enumerate(x_split):
  226. assert (out.asnumpy() == expt_split[i]).all()
  227. x_unique2, x_idx2, x_split2 = net(x2)
  228. assert (x_unique2.asnumpy() == expt_unique2).all()
  229. assert (x_idx2.asnumpy() == expt_index2).all()
  230. for i, out in enumerate(x_split2):
  231. assert (out.asnumpy() == expt_split2[i]).all()
  232. @pytest.mark.level0
  233. @pytest.mark.platform_x86_gpu_training
  234. @pytest.mark.env_onecard
  235. def test_unique_1d_int64():
  236. x = Tensor(np.array([4, 5, 1, 2, 3, 3, 4, 5]).astype(np.int64))
  237. exp_output = np.array([1, 2, 3, 4, 5]).astype(np.int64)
  238. exp_idx = np.array([3, 4, 0, 1, 2, 2, 3, 4]).astype(np.int64)
  239. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  240. net = NetUnique()
  241. x_unique, x_idx = net(x)
  242. print(x_unique)
  243. print(x_idx)
  244. assert (x_unique.asnumpy() == exp_output).all()
  245. assert (x_idx.asnumpy() == exp_idx).all()
  246. @pytest.mark.level0
  247. @pytest.mark.platform_x86_gpu_training
  248. @pytest.mark.env_onecard
  249. def test_unique_1d_sorted_int64():
  250. x = Tensor(np.array([1, 1, 2, 4, 4, 4, 7, 8, 8]).astype(np.int64))
  251. exp_output = np.array([1, 2, 4, 7, 8]).astype(np.int64)
  252. exp_idx = np.array([0, 0, 1, 2, 2, 2, 3, 4, 4]).astype(np.int64)
  253. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  254. net = NetUnique()
  255. x_unique, x_idx = net(x)
  256. assert (x_unique.asnumpy() == exp_output).all()
  257. assert (x_idx.asnumpy() == exp_idx).all()
  258. @pytest.mark.level0
  259. @pytest.mark.platform_x86_gpu_training
  260. @pytest.mark.env_onecard
  261. def test_unique_zeros_int64():
  262. x = Tensor(np.zeros(1000).astype(np.int64))
  263. exp_output = np.zeros(1).astype(np.int64)
  264. exp_idx = np.zeros(1000).astype(np.int64)
  265. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  266. net = NetUnique()
  267. x_unique, x_idx = net(x)
  268. assert (x_unique.asnumpy() == exp_output).all()
  269. assert (x_idx.asnumpy() == exp_idx).all()
  270. @pytest.mark.level0
  271. @pytest.mark.platform_x86_gpu_training
  272. @pytest.mark.env_onecard
  273. def test_unique_large_int64():
  274. x_np1 = np.arange(100)
  275. x_np2 = np.arange(100, 200)
  276. x_np3 = np.arange(200, 300)
  277. x_np = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3))
  278. x = Tensor(x_np.astype(np.int64))
  279. exp_output = np.arange(300).astype(np.int64)
  280. exp_idx = np.concatenate((x_np1, x_np2, x_np3, x_np1, x_np2, x_np3, x_np1, x_np2, x_np3)).astype(np.int64)
  281. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  282. net = NetUnique()
  283. x_unique, x_idx = net(x)
  284. assert (x_unique.asnumpy() == exp_output).all()
  285. assert (x_idx.asnumpy() == exp_idx).all()