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_scatter_func_op.py 27 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. # Copyright 2020-2021 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, Parameter
  20. from mindspore.ops import operations as P
  21. from mindspore.ops.operations import _inner_ops as inner
  22. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  23. # all cases tested against dchip
  24. func_map = {
  25. "update": P.ScatterUpdate,
  26. "add": P.ScatterAdd,
  27. "sub": P.ScatterSub,
  28. }
  29. class TestScatterFuncNet(nn.Cell):
  30. def __init__(self, func, lock, inputx, indices, updates):
  31. super(TestScatterFuncNet, self).__init__()
  32. self.scatter_func = func_map[func](use_locking=lock)
  33. self.inputx = Parameter(inputx, name="inputx")
  34. self.indices = Parameter(indices, name="indices")
  35. self.updates = Parameter(updates, name="updates")
  36. def construct(self):
  37. out = self.scatter_func(self.inputx, self.indices, self.updates)
  38. return out
  39. def scatter_func_net(func, inputx, indices, updates):
  40. lock = True
  41. net = TestScatterFuncNet(func, lock, inputx, indices, updates)
  42. return net()
  43. def scatter_func_use_locking_false_net(func, inputx, indices, updates):
  44. lock = False
  45. net = TestScatterFuncNet(func, lock, inputx, indices, updates)
  46. return net()
  47. class TestScatterFuncDynamicNet(nn.Cell):
  48. def __init__(self, func, inputx, indices, updates):
  49. super(TestScatterFuncDynamicNet, self).__init__()
  50. self.scatter_func = func_map[func]()
  51. self.test_dynamic = inner.GpuConvertToDynamicShape()
  52. self.inputx = Parameter(inputx, name="inputx")
  53. self.indices = Parameter(indices, name="indices")
  54. self.updates = Parameter(updates, name="updates")
  55. def construct(self):
  56. indices = self.test_dynamic(self.indices)
  57. updates = self.test_dynamic(self.updates)
  58. out = self.scatter_func(self.inputx, indices, updates)
  59. return out
  60. def scatter_func_d_net(func, inputx, indices, updates):
  61. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  62. net = TestScatterFuncDynamicNet(func, inputx, indices, updates)
  63. return net()
  64. class TestScatterFuncDynamicNet2(nn.Cell):
  65. def __init__(self, func, inputx):
  66. super(TestScatterFuncDynamicNet2, self).__init__()
  67. self.scatter_func = func_map[func]()
  68. self.test_dynamic = inner.GpuConvertToDynamicShape()
  69. self.inputx = Parameter(inputx, name="inputx")
  70. def construct(self, indices, updates):
  71. indices = self.test_dynamic(indices)
  72. updates = self.test_dynamic(updates)
  73. out = self.scatter_func(self.inputx, indices, updates)
  74. return out
  75. def scatter_func_d2_net(func, inputx, indices_1, updates_1, indices_2, updates_2):
  76. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  77. net = TestScatterFuncDynamicNet2(func, inputx)
  78. out1 = net(indices_1, updates_1)
  79. out2 = net(indices_2, updates_2)
  80. return (out1, out2)
  81. @pytest.mark.level0
  82. @pytest.mark.platform_x86_gpu_training
  83. @pytest.mark.env_onecard
  84. def test_scatter_func_small_float32():
  85. inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
  86. indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
  87. updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
  88. # update
  89. output = scatter_func_net("update", inputx, indices, updates)
  90. expected = np.array([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]])
  91. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  92. # add
  93. output = scatter_func_net("add", inputx, indices, updates)
  94. expected = np.array([[6.0, 8.0, 10.0], [12.0, 14.0, 16.0]])
  95. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  96. # sub
  97. output = scatter_func_net("sub", inputx, indices, updates)
  98. expected = np.array([[-6.0, -8.0, -10.0], [-12.0, -14.0, -16.0]])
  99. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  100. @pytest.mark.level0
  101. @pytest.mark.platform_x86_gpu_training
  102. @pytest.mark.env_onecard
  103. def test_scatter_func_input_updated():
  104. inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
  105. indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
  106. updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
  107. lock = True
  108. # update
  109. net = TestScatterFuncNet("update", lock, inputx, indices, updates)
  110. net()
  111. expected = np.array([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]])
  112. np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
  113. # add
  114. net = TestScatterFuncNet("add", lock, inputx, indices, updates)
  115. net()
  116. expected = np.array([[6.0, 8.0, 10.0], [12.0, 14.0, 16.0]])
  117. np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
  118. # sub
  119. net = TestScatterFuncNet("sub", lock, inputx, indices, updates)
  120. net()
  121. expected = np.array([[-6.0, -8.0, -10.0], [-12.0, -14.0, -16.0]])
  122. np.testing.assert_array_almost_equal(net.inputx.asnumpy(), expected)
  123. @pytest.mark.level0
  124. @pytest.mark.platform_x86_gpu_training
  125. @pytest.mark.env_onecard
  126. def test_scatter_func_large_shape_float32():
  127. inputx = Tensor(np.ones((4, 2, 3, 4)).astype(np.float32))
  128. indices = Tensor(np.array([[0, 2], [3, 1]]).astype(np.int32))
  129. updates = Tensor(np.arange(96).reshape((2, 2, 2, 3, 4)).astype(np.float32))
  130. # update
  131. output = scatter_func_net("update", inputx, indices, updates)
  132. expected = np.array(
  133. [
  134. [
  135. [[0.0, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0], [8.0, 9.0, 10.0, 11.0]],
  136. [[12.0, 13.0, 14.0, 15.0], [16.0, 17.0, 18.0, 19.0], [20.0, 21.0, 22.0, 23.0]],
  137. ],
  138. [
  139. [[72.0, 73.0, 74.0, 75.0], [76.0, 77.0, 78.0, 79.0], [80.0, 81.0, 82.0, 83.0]],
  140. [[84.0, 85.0, 86.0, 87.0], [88.0, 89.0, 90.0, 91.0], [92.0, 93.0, 94.0, 95.0]],
  141. ],
  142. [
  143. [[24.0, 25.0, 26.0, 27.0], [28.0, 29.0, 30.0, 31.0], [32.0, 33.0, 34.0, 35.0]],
  144. [[36.0, 37.0, 38.0, 39.0], [40.0, 41.0, 42.0, 43.0], [44.0, 45.0, 46.0, 47.0]],
  145. ],
  146. [
  147. [[48.0, 49.0, 50.0, 51.0], [52.0, 53.0, 54.0, 55.0], [56.0, 57.0, 58.0, 59.0]],
  148. [[60.0, 61.0, 62.0, 63.0], [64.0, 65.0, 66.0, 67.0], [68.0, 69.0, 70.0, 71.0]],
  149. ],
  150. ]
  151. )
  152. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  153. # add
  154. output = scatter_func_net("add", inputx, indices, updates)
  155. expected = np.array(
  156. [
  157. [
  158. [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0]],
  159. [[13.0, 14.0, 15.0, 16.0], [17.0, 18.0, 19.0, 20.0], [21.0, 22.0, 23.0, 24.0]],
  160. ],
  161. [
  162. [[73.0, 74.0, 75.0, 76.0], [77.0, 78.0, 79.0, 80.0], [81.0, 82.0, 83.0, 84.0]],
  163. [[85.0, 86.0, 87.0, 88.0], [89.0, 90.0, 91.0, 92.0], [93.0, 94.0, 95.0, 96.0]],
  164. ],
  165. [
  166. [[25.0, 26.0, 27.0, 28.0], [29.0, 30.0, 31.0, 32.0], [33.0, 34.0, 35.0, 36.0]],
  167. [[37.0, 38.0, 39.0, 40.0], [41.0, 42.0, 43.0, 44.0], [45.0, 46.0, 47.0, 48.0]],
  168. ],
  169. [
  170. [[49.0, 50.0, 51.0, 52.0], [53.0, 54.0, 55.0, 56.0], [57.0, 58.0, 59.0, 60.0]],
  171. [[61.0, 62.0, 63.0, 64.0], [65.0, 66.0, 67.0, 68.0], [69.0, 70.0, 71.0, 72.0]],
  172. ],
  173. ]
  174. )
  175. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  176. # sub
  177. output = scatter_func_net("sub", inputx, indices, updates)
  178. expected = np.array(
  179. [
  180. [
  181. [[1.0, 0.0, -1.0, -2.0], [-3.0, -4.0, -5.0, -6.0], [-7.0, -8.0, -9.0, -10.0]],
  182. [
  183. [-11.0, -12.0, -13.0, -14.0],
  184. [-15.0, -16.0, -17.0, -18.0],
  185. [-19.0, -20.0, -21.0, -22.0],
  186. ],
  187. ],
  188. [
  189. [
  190. [-71.0, -72.0, -73.0, -74.0],
  191. [-75.0, -76.0, -77.0, -78.0],
  192. [-79.0, -80.0, -81.0, -82.0],
  193. ],
  194. [
  195. [-83.0, -84.0, -85.0, -86.0],
  196. [-87.0, -88.0, -89.0, -90.0],
  197. [-91.0, -92.0, -93.0, -94.0],
  198. ],
  199. ],
  200. [
  201. [
  202. [-23.0, -24.0, -25.0, -26.0],
  203. [-27.0, -28.0, -29.0, -30.0],
  204. [-31.0, -32.0, -33.0, -34.0],
  205. ],
  206. [
  207. [-35.0, -36.0, -37.0, -38.0],
  208. [-39.0, -40.0, -41.0, -42.0],
  209. [-43.0, -44.0, -45.0, -46.0],
  210. ],
  211. ],
  212. [
  213. [
  214. [-47.0, -48.0, -49.0, -50.0],
  215. [-51.0, -52.0, -53.0, -54.0],
  216. [-55.0, -56.0, -57.0, -58.0],
  217. ],
  218. [
  219. [-59.0, -60.0, -61.0, -62.0],
  220. [-63.0, -64.0, -65.0, -66.0],
  221. [-67.0, -68.0, -69.0, -70.0],
  222. ],
  223. ],
  224. ]
  225. )
  226. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  227. @pytest.mark.level0
  228. @pytest.mark.platform_x86_gpu_training
  229. @pytest.mark.env_onecard
  230. def test_scatter_func_small_float32_use_locking_false():
  231. inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
  232. indices = Tensor(np.array([1, 0]).astype(np.int32))
  233. updates = Tensor(np.arange(6).reshape((2, 3)).astype(np.float32))
  234. # update
  235. output = scatter_func_use_locking_false_net("update", inputx, indices, updates)
  236. expected = np.array([[3.0, 4.0, 5.0], [0.0, 1.0, 2.0]])
  237. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  238. # add
  239. output = scatter_func_use_locking_false_net("add", inputx, indices, updates)
  240. expected = np.array([[3.0, 4.0, 5.0], [0.0, 1.0, 2.0]])
  241. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  242. # sub
  243. output = scatter_func_use_locking_false_net("sub", inputx, indices, updates)
  244. expected = np.array([[-3.0, -4.0, -5.0], [0.0, -1.0, -2.0]])
  245. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  246. @pytest.mark.level0
  247. @pytest.mark.platform_x86_gpu_training
  248. @pytest.mark.env_onecard
  249. def test_scatter_func_input_less_than_1_float32():
  250. inputx = Tensor(
  251. np.array(
  252. [
  253. [0.214141, 0.415151, 0.51516],
  254. [0.876542, 0.451611, 0.55112],
  255. [0.111244, 0.633333, 0.34444],
  256. ]
  257. ).astype(np.float32)
  258. )
  259. indices = Tensor(np.array([[[1, 0, 2], [2, 2, 0]], [[1, 0, 1], [2, 1, 2]]]).astype(np.int32))
  260. updates = Tensor(np.arange(34, 70).reshape((2, 2, 3, 3)).astype(np.float32))
  261. # update
  262. output = scatter_func_net("update", inputx, indices, updates)
  263. expected = np.array(
  264. [[37.0, 38.0, 39.0], [34.0, 35.0, 66.0], [67.0, 68.0, 69.0],], dtype=np.float32,
  265. )
  266. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  267. # add
  268. output = scatter_func_net("add", inputx, indices, updates)
  269. expected = np.array(
  270. [
  271. [141.21414, 144.41515, 147.51517],
  272. [208.87654, 212.45161, 216.55112],
  273. [257.11124, 262.63333, 267.34442],
  274. ],
  275. dtype=np.float32,
  276. )
  277. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  278. # sub
  279. output = scatter_func_net("sub", inputx, indices, updates)
  280. expected = np.array(
  281. [
  282. [-140.78586, -143.58485, -146.48483],
  283. [-207.12346, -211.54839, -215.44888],
  284. [-256.88876, -261.36667, -266.65558],
  285. ],
  286. dtype=np.float32,
  287. )
  288. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  289. @pytest.mark.level0
  290. @pytest.mark.platform_x86_gpu_training
  291. @pytest.mark.env_onecard
  292. def test_scatter_func_float16():
  293. inputx = Tensor(np.zeros((2, 3)).astype(np.float16))
  294. indices = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
  295. updates = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float16))
  296. # update
  297. output = scatter_func_net("update", inputx, indices, updates)
  298. expected = np.array([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]])
  299. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  300. # add
  301. output = scatter_func_net("add", inputx, indices, updates)
  302. expected = np.array([[6.0, 8.0, 10.0], [12.0, 14.0, 16.0]])
  303. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  304. # sub
  305. output = scatter_func_net("sub", inputx, indices, updates)
  306. expected = np.array([[-6.0, -8.0, -10.0], [-12.0, -14.0, -16.0]])
  307. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  308. @pytest.mark.level0
  309. @pytest.mark.platform_x86_gpu_training
  310. @pytest.mark.env_onecard
  311. def test_scatter_func_large_float16():
  312. inputx = Tensor(np.zeros((2, 3, 4)).astype(np.float16))
  313. indices = Tensor(np.array([[0, 0], [1, 1]]).astype(np.int32))
  314. updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.float16))
  315. # update
  316. output = scatter_func_net("update", inputx, indices, updates)
  317. expected = np.array(
  318. [
  319. [[63.0, 64.0, 65.0, 66.0], [67.0, 68.0, 69.0, 70.0], [71.0, 72.0, 73.0, 74.0],],
  320. [[99.0, 100.0, 101.0, 102.0], [103.0, 104.0, 105.0, 106.0], [95.0, 96.0, 97.0, 98.0],],
  321. ]
  322. )
  323. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  324. # add
  325. output = scatter_func_net("add", inputx, indices, updates)
  326. expected = np.array(
  327. [
  328. [
  329. [138.0, 140.0, 142.0, 144.0],
  330. [146.0, 148.0, 150.0, 152.0],
  331. [154.0, 156.0, 158.0, 160.0],
  332. ],
  333. [
  334. [186.0, 188.0, 190.0, 192.0],
  335. [194.0, 196.0, 198.0, 200.0],
  336. [202.0, 204.0, 206.0, 208.0],
  337. ],
  338. ]
  339. )
  340. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  341. # sub
  342. output = scatter_func_net("sub", inputx, indices, updates)
  343. expected = np.array(
  344. [
  345. [
  346. [-138.0, -140.0, -142.0, -144.0],
  347. [-146.0, -148.0, -150.0, -152.0],
  348. [-154.0, -156.0, -158.0, -160.0],
  349. ],
  350. [
  351. [-186.0, -188.0, -190.0, -192.0],
  352. [-194.0, -196.0, -198.0, -200.0],
  353. [-202.0, -204.0, -206.0, -208.0],
  354. ],
  355. ]
  356. )
  357. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  358. @pytest.mark.level0
  359. @pytest.mark.platform_x86_gpu_training
  360. @pytest.mark.env_onecard
  361. def test_scatter_func_disordered_float16():
  362. inputx = Tensor(np.flip(np.arange(34, 46).reshape(3, 4).astype(np.float16)))
  363. indices = Tensor(np.array([[[0, 1, 2], [2, 1, 0]], [[0, 0, 0], [2, 2, 2]]]).astype(np.int32))
  364. updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.float16))
  365. # update
  366. output = scatter_func_net("update", inputx, indices, updates)
  367. expected = np.array(
  368. [[95.0, 96.0, 97.0, 98.0], [67.0, 68.0, 69.0, 70.0], [99.0, 100.0, 101.0, 102.0]]
  369. )
  370. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  371. # add
  372. output = scatter_func_net("add", inputx, indices, updates)
  373. expected = np.array(
  374. [[464.0, 468.0, 472.0, 476.0], [187.0, 188.0, 189.0, 190.0], [492.0, 496.0, 500.0, 504.0]]
  375. )
  376. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  377. # sub
  378. output = scatter_func_net("sub", inputx, indices, updates)
  379. expected = np.array(
  380. [
  381. [-374.0, -380.0, -386.0, -392.0],
  382. [-105.0, -108.0, -111.0, -114.0],
  383. [-418.0, -424.0, -430.0, -436.0],
  384. ]
  385. )
  386. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  387. @pytest.mark.level0
  388. @pytest.mark.platform_x86_gpu_training
  389. @pytest.mark.env_onecard
  390. def test_scatter_func_large_int32():
  391. inputx = Tensor(np.zeros((2, 3, 4)).astype(np.int32))
  392. indices = Tensor(np.array([[0, 0], [1, 1]]).astype(np.int32))
  393. updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.int32))
  394. # update
  395. output = scatter_func_net("update", inputx, indices, updates)
  396. expected = np.array(
  397. [
  398. [[63.0, 64.0, 65.0, 66.0], [67.0, 68.0, 69.0, 70.0], [71.0, 72.0, 73.0, 74.0],],
  399. [[99.0, 100.0, 101.0, 102.0], [103.0, 104.0, 105.0, 106.0], [95.0, 96.0, 97.0, 98.0],],
  400. ]
  401. ).astype(np.int32)
  402. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  403. # add
  404. output = scatter_func_net("add", inputx, indices, updates)
  405. expected = np.array(
  406. [
  407. [
  408. [138.0, 140.0, 142.0, 144.0],
  409. [146.0, 148.0, 150.0, 152.0],
  410. [154.0, 156.0, 158.0, 160.0],
  411. ],
  412. [
  413. [186.0, 188.0, 190.0, 192.0],
  414. [194.0, 196.0, 198.0, 200.0],
  415. [202.0, 204.0, 206.0, 208.0],
  416. ],
  417. ]
  418. ).astype(np.int32)
  419. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  420. # sub
  421. output = scatter_func_net("sub", inputx, indices, updates)
  422. expected = np.array(
  423. [
  424. [
  425. [-138.0, -140.0, -142.0, -144.0],
  426. [-146.0, -148.0, -150.0, -152.0],
  427. [-154.0, -156.0, -158.0, -160.0],
  428. ],
  429. [
  430. [-186.0, -188.0, -190.0, -192.0],
  431. [-194.0, -196.0, -198.0, -200.0],
  432. [-202.0, -204.0, -206.0, -208.0],
  433. ],
  434. ]
  435. ).astype(np.int32)
  436. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  437. @pytest.mark.level0
  438. @pytest.mark.platform_x86_gpu_training
  439. @pytest.mark.env_onecard
  440. def test_scatter_func_disordered_int32():
  441. inputx = Tensor(np.flip(np.arange(34, 46).reshape(3, 4).astype(np.int32)))
  442. indices = Tensor(np.array([[[0, 1, 2], [2, 1, 0]], [[0, 0, 0], [2, 2, 2]]]).astype(np.int32))
  443. updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.int32))
  444. # update
  445. output = scatter_func_net("update", inputx, indices, updates)
  446. expected = np.array(
  447. [[95.0, 96.0, 97.0, 98.0], [67.0, 68.0, 69.0, 70.0], [99.0, 100.0, 101.0, 102.0]]
  448. ).astype(np.int32)
  449. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  450. # add
  451. output = scatter_func_net("add", inputx, indices, updates)
  452. expected = np.array(
  453. [[464.0, 468.0, 472.0, 476.0], [187.0, 188.0, 189.0, 190.0], [492.0, 496.0, 500.0, 504.0]]
  454. ).astype(np.int32)
  455. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  456. # sub
  457. output = scatter_func_net("sub", inputx, indices, updates)
  458. expected = np.array(
  459. [
  460. [-374.0, -380.0, -386.0, -392.0],
  461. [-105.0, -108.0, -111.0, -114.0],
  462. [-418.0, -424.0, -430.0, -436.0],
  463. ]
  464. ).astype(np.int32)
  465. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  466. @pytest.mark.level0
  467. @pytest.mark.platform_x86_gpu_training
  468. @pytest.mark.env_onecard
  469. def test_scatter_func_disordered_dynamic_int32():
  470. inputx = Tensor(np.flip(np.arange(34, 46).reshape(3, 4).astype(np.int32)))
  471. indices = Tensor(np.array([[[0, 1, 2], [2, 1, 0]], [[0, 0, 0], [2, 2, 2]]]).astype(np.int32))
  472. updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.int32))
  473. # update
  474. output = scatter_func_d_net("update", inputx, indices, updates)
  475. expected = np.array(
  476. [[95.0, 96.0, 97.0, 98.0], [67.0, 68.0, 69.0, 70.0], [99.0, 100.0, 101.0, 102.0]]
  477. ).astype(np.int32)
  478. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  479. # add
  480. output = scatter_func_d_net("add", inputx, indices, updates)
  481. expected = np.array(
  482. [[464.0, 468.0, 472.0, 476.0], [187.0, 188.0, 189.0, 190.0], [492.0, 496.0, 500.0, 504.0]]
  483. ).astype(np.int32)
  484. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  485. # sub
  486. output = scatter_func_d_net("sub", inputx, indices, updates)
  487. expected = np.array(
  488. [
  489. [-374.0, -380.0, -386.0, -392.0],
  490. [-105.0, -108.0, -111.0, -114.0],
  491. [-418.0, -424.0, -430.0, -436.0],
  492. ]
  493. ).astype(np.int32)
  494. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  495. @pytest.mark.level0
  496. @pytest.mark.platform_x86_gpu_training
  497. @pytest.mark.env_onecard
  498. def test_scatter_func_disordered_dynamic_int8():
  499. inputx = Tensor(np.flip(np.arange(34, 46).reshape(3, 4).astype(np.int8)))
  500. indices = Tensor(np.array([[[0, 1, 2], [2, 1, 0]], [[0, 0, 0], [2, 2, 2]]]).astype(np.int32))
  501. updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.int8))
  502. # update
  503. output = scatter_func_d_net("update", inputx, indices, updates)
  504. expected = np.array(
  505. [[95.0, 96.0, 97.0, 98.0], [67.0, 68.0, 69.0, 70.0], [99.0, 100.0, 101.0, 102.0]]
  506. ).astype(np.int8)
  507. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  508. # add
  509. output = scatter_func_d_net("add", inputx, indices, updates)
  510. expected = np.array(
  511. [[464.0, 468.0, 472.0, 476.0], [187.0, 188.0, 189.0, 190.0], [492.0, 496.0, 500.0, 504.0]]
  512. ).astype(np.int8)
  513. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  514. # sub
  515. output = scatter_func_d_net("sub", inputx, indices, updates)
  516. expected = np.array(
  517. [
  518. [-118.0, -124.0, 126.0, 120.0],
  519. [-105.0, -108.0, -111.0, -114.0],
  520. [94.0, 88.0, 82.0, 76.0],
  521. ]
  522. ).astype(np.int8)
  523. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  524. @pytest.mark.level0
  525. @pytest.mark.platform_x86_gpu_training
  526. @pytest.mark.env_onecard
  527. def test_scatter_func_disordered_dynamic_uint8():
  528. inputx = Tensor(np.flip(np.arange(34, 46).reshape(3, 4).astype(np.uint8)))
  529. indices = Tensor(np.array([[[0, 1, 2], [2, 1, 0]], [[0, 0, 0], [2, 2, 2]]]).astype(np.int32))
  530. updates = Tensor(np.arange(63, 111).reshape((2, 2, 3, 4)).astype(np.uint8))
  531. # update
  532. output = scatter_func_d_net("update", inputx, indices, updates)
  533. expected = np.array(
  534. [[95.0, 96.0, 97.0, 98.0], [67.0, 68.0, 69.0, 70.0], [99.0, 100.0, 101.0, 102.0]]
  535. ).astype(np.uint8)
  536. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  537. # add
  538. output = scatter_func_d_net("add", inputx, indices, updates)
  539. expected = np.array(
  540. [[464.0, 468.0, 472.0, 476.0], [187.0, 188.0, 189.0, 190.0], [492.0, 496.0, 500.0, 504.0]]
  541. ).astype(np.uint8)
  542. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  543. # sub
  544. output = scatter_func_d_net("sub", inputx, indices, updates)
  545. expected = np.array(
  546. [[138.0, 132.0, 126.0, 120.0], [151.0, 148.0, 145.0, 142.0], [94.0, 88.0, 82.0, 76.0]]
  547. ).astype(np.uint8)
  548. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  549. @pytest.mark.level0
  550. @pytest.mark.platform_x86_gpu_training
  551. @pytest.mark.env_onecard
  552. def test_scatter_func_input_less_than_1_dynamic_float32():
  553. inputx = Tensor(
  554. np.array(
  555. [
  556. [0.214141, 0.415151, 0.51516],
  557. [0.876542, 0.451611, 0.55112],
  558. [0.111244, 0.633333, 0.34444],
  559. ]
  560. ).astype(np.float32)
  561. )
  562. indices = Tensor(np.array([[[1, 0, 2], [2, 2, 0]], [[1, 0, 1], [2, 1, 2]]]).astype(np.int32))
  563. updates = Tensor(np.arange(34, 70).reshape((2, 2, 3, 3)).astype(np.float32))
  564. # update
  565. output = scatter_func_d_net("update", inputx, indices, updates)
  566. expected = np.array(
  567. [[37.0, 38.0, 39.0], [34.0, 35.0, 66.0], [67.0, 68.0, 69.0],], dtype=np.float32,
  568. )
  569. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  570. # add
  571. output = scatter_func_d_net("add", inputx, indices, updates)
  572. expected = np.array(
  573. [
  574. [141.21414, 144.41515, 147.51517],
  575. [208.87654, 212.45161, 216.55112],
  576. [257.11124, 262.63333, 267.34442],
  577. ],
  578. dtype=np.float32,
  579. )
  580. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  581. # sub
  582. output = scatter_func_d_net("sub", inputx, indices, updates)
  583. expected = np.array(
  584. [
  585. [-140.78586, -143.58485, -146.48483],
  586. [-207.12346, -211.54839, -215.44888],
  587. [-256.88876, -261.36667, -266.65558],
  588. ],
  589. dtype=np.float32,
  590. )
  591. np.testing.assert_array_almost_equal(output.asnumpy(), expected)
  592. @pytest.mark.level0
  593. @pytest.mark.platform_x86_gpu_training
  594. @pytest.mark.env_onecard
  595. def test_scatter_func_dynamic_two_inputs():
  596. inputx = Tensor(np.zeros((2, 3)).astype(np.float32))
  597. indices_1 = Tensor(np.array([[0, 1], [0, 1]]).astype(np.int32))
  598. updates_1 = Tensor(np.arange(12).reshape((2, 2, 3)).astype(np.float32))
  599. indices_2 = Tensor(np.array([[0, 0], [1, 1], [1, 0]]).astype(np.int32))
  600. updates_2 = Tensor(np.flip(np.arange(18).reshape((3, 2, 3)).astype(np.float32)))
  601. # update
  602. output_1, output_2 = scatter_func_d2_net(
  603. "update", inputx, indices_1, updates_1, indices_2, updates_2
  604. )
  605. expected_1 = np.array([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]])
  606. expected_2 = np.array([[17.0, 16.0, 15.0], [11.0, 10.0, 9.0]])
  607. np.testing.assert_array_almost_equal(output_1.asnumpy(), expected_1)
  608. np.testing.assert_array_almost_equal(output_2.asnumpy(), expected_2)
  609. # add
  610. output_1, output_2 = scatter_func_d2_net(
  611. "add", inputx, indices_1, updates_1, indices_2, updates_2
  612. )
  613. expected_1 = np.array([[6.0, 8.0, 10.0], [12.0, 14.0, 16.0]])
  614. expected_2 = np.array([[39.0, 38.0, 37.0], [36.0, 35.0, 34.0]])
  615. np.testing.assert_array_almost_equal(output_1.asnumpy(), expected_1)
  616. np.testing.assert_array_almost_equal(output_2.asnumpy(), expected_2)
  617. # sub
  618. output_1, output_2 = scatter_func_d2_net(
  619. "sub", inputx, indices_1, updates_1, indices_2, updates_2
  620. )
  621. expected_1 = np.array([[-6.0, -8.0, -10.0], [-12.0, -14.0, -16.0]])
  622. expected_2 = np.array([[-39.0, -38.0, -37.0], [-36.0, -35.0, -34.0]])
  623. np.testing.assert_array_almost_equal(output_1.asnumpy(), expected_1)
  624. np.testing.assert_array_almost_equal(output_2.asnumpy(), expected_2)