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_gatherV2_op.py 46 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. # Copyright 2019-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.operations import _inner_ops as inner
  21. from mindspore.ops import operations as P
  22. class GatherNet(nn.Cell):
  23. def __init__(self):
  24. super(GatherNet, self).__init__()
  25. self.gather = P.GatherV2()
  26. def construct(self, x, indices):
  27. return self.gather(x, indices, 1)
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_gpu_training
  30. @pytest.mark.env_onecard
  31. def test_gather0():
  32. x = Tensor(np.arange(2 * 3 * 4 * 5, dtype=np.float32).reshape(2, 3, 4, 5))
  33. indices = Tensor(np.ones((2, 2, 4, 5), dtype='i4'))
  34. expect = np.array([[[[[[[20., 21., 22., 23., 24.],
  35. [25., 26., 27., 28., 29.],
  36. [30., 31., 32., 33., 34.],
  37. [35., 36., 37., 38., 39.]],
  38. [[20., 21., 22., 23., 24.],
  39. [25., 26., 27., 28., 29.],
  40. [30., 31., 32., 33., 34.],
  41. [35., 36., 37., 38., 39.]],
  42. [[20., 21., 22., 23., 24.],
  43. [25., 26., 27., 28., 29.],
  44. [30., 31., 32., 33., 34.],
  45. [35., 36., 37., 38., 39.]],
  46. [[20., 21., 22., 23., 24.],
  47. [25., 26., 27., 28., 29.],
  48. [30., 31., 32., 33., 34.],
  49. [35., 36., 37., 38., 39.]],
  50. [[20., 21., 22., 23., 24.],
  51. [25., 26., 27., 28., 29.],
  52. [30., 31., 32., 33., 34.],
  53. [35., 36., 37., 38., 39.]]],
  54. [[[20., 21., 22., 23., 24.],
  55. [25., 26., 27., 28., 29.],
  56. [30., 31., 32., 33., 34.],
  57. [35., 36., 37., 38., 39.]],
  58. [[20., 21., 22., 23., 24.],
  59. [25., 26., 27., 28., 29.],
  60. [30., 31., 32., 33., 34.],
  61. [35., 36., 37., 38., 39.]],
  62. [[20., 21., 22., 23., 24.],
  63. [25., 26., 27., 28., 29.],
  64. [30., 31., 32., 33., 34.],
  65. [35., 36., 37., 38., 39.]],
  66. [[20., 21., 22., 23., 24.],
  67. [25., 26., 27., 28., 29.],
  68. [30., 31., 32., 33., 34.],
  69. [35., 36., 37., 38., 39.]],
  70. [[20., 21., 22., 23., 24.],
  71. [25., 26., 27., 28., 29.],
  72. [30., 31., 32., 33., 34.],
  73. [35., 36., 37., 38., 39.]]],
  74. [[[20., 21., 22., 23., 24.],
  75. [25., 26., 27., 28., 29.],
  76. [30., 31., 32., 33., 34.],
  77. [35., 36., 37., 38., 39.]],
  78. [[20., 21., 22., 23., 24.],
  79. [25., 26., 27., 28., 29.],
  80. [30., 31., 32., 33., 34.],
  81. [35., 36., 37., 38., 39.]],
  82. [[20., 21., 22., 23., 24.],
  83. [25., 26., 27., 28., 29.],
  84. [30., 31., 32., 33., 34.],
  85. [35., 36., 37., 38., 39.]],
  86. [[20., 21., 22., 23., 24.],
  87. [25., 26., 27., 28., 29.],
  88. [30., 31., 32., 33., 34.],
  89. [35., 36., 37., 38., 39.]],
  90. [[20., 21., 22., 23., 24.],
  91. [25., 26., 27., 28., 29.],
  92. [30., 31., 32., 33., 34.],
  93. [35., 36., 37., 38., 39.]]],
  94. [[[20., 21., 22., 23., 24.],
  95. [25., 26., 27., 28., 29.],
  96. [30., 31., 32., 33., 34.],
  97. [35., 36., 37., 38., 39.]],
  98. [[20., 21., 22., 23., 24.],
  99. [25., 26., 27., 28., 29.],
  100. [30., 31., 32., 33., 34.],
  101. [35., 36., 37., 38., 39.]],
  102. [[20., 21., 22., 23., 24.],
  103. [25., 26., 27., 28., 29.],
  104. [30., 31., 32., 33., 34.],
  105. [35., 36., 37., 38., 39.]],
  106. [[20., 21., 22., 23., 24.],
  107. [25., 26., 27., 28., 29.],
  108. [30., 31., 32., 33., 34.],
  109. [35., 36., 37., 38., 39.]],
  110. [[20., 21., 22., 23., 24.],
  111. [25., 26., 27., 28., 29.],
  112. [30., 31., 32., 33., 34.],
  113. [35., 36., 37., 38., 39.]]]],
  114. [[[[20., 21., 22., 23., 24.],
  115. [25., 26., 27., 28., 29.],
  116. [30., 31., 32., 33., 34.],
  117. [35., 36., 37., 38., 39.]],
  118. [[20., 21., 22., 23., 24.],
  119. [25., 26., 27., 28., 29.],
  120. [30., 31., 32., 33., 34.],
  121. [35., 36., 37., 38., 39.]],
  122. [[20., 21., 22., 23., 24.],
  123. [25., 26., 27., 28., 29.],
  124. [30., 31., 32., 33., 34.],
  125. [35., 36., 37., 38., 39.]],
  126. [[20., 21., 22., 23., 24.],
  127. [25., 26., 27., 28., 29.],
  128. [30., 31., 32., 33., 34.],
  129. [35., 36., 37., 38., 39.]],
  130. [[20., 21., 22., 23., 24.],
  131. [25., 26., 27., 28., 29.],
  132. [30., 31., 32., 33., 34.],
  133. [35., 36., 37., 38., 39.]]],
  134. [[[20., 21., 22., 23., 24.],
  135. [25., 26., 27., 28., 29.],
  136. [30., 31., 32., 33., 34.],
  137. [35., 36., 37., 38., 39.]],
  138. [[20., 21., 22., 23., 24.],
  139. [25., 26., 27., 28., 29.],
  140. [30., 31., 32., 33., 34.],
  141. [35., 36., 37., 38., 39.]],
  142. [[20., 21., 22., 23., 24.],
  143. [25., 26., 27., 28., 29.],
  144. [30., 31., 32., 33., 34.],
  145. [35., 36., 37., 38., 39.]],
  146. [[20., 21., 22., 23., 24.],
  147. [25., 26., 27., 28., 29.],
  148. [30., 31., 32., 33., 34.],
  149. [35., 36., 37., 38., 39.]],
  150. [[20., 21., 22., 23., 24.],
  151. [25., 26., 27., 28., 29.],
  152. [30., 31., 32., 33., 34.],
  153. [35., 36., 37., 38., 39.]]],
  154. [[[20., 21., 22., 23., 24.],
  155. [25., 26., 27., 28., 29.],
  156. [30., 31., 32., 33., 34.],
  157. [35., 36., 37., 38., 39.]],
  158. [[20., 21., 22., 23., 24.],
  159. [25., 26., 27., 28., 29.],
  160. [30., 31., 32., 33., 34.],
  161. [35., 36., 37., 38., 39.]],
  162. [[20., 21., 22., 23., 24.],
  163. [25., 26., 27., 28., 29.],
  164. [30., 31., 32., 33., 34.],
  165. [35., 36., 37., 38., 39.]],
  166. [[20., 21., 22., 23., 24.],
  167. [25., 26., 27., 28., 29.],
  168. [30., 31., 32., 33., 34.],
  169. [35., 36., 37., 38., 39.]],
  170. [[20., 21., 22., 23., 24.],
  171. [25., 26., 27., 28., 29.],
  172. [30., 31., 32., 33., 34.],
  173. [35., 36., 37., 38., 39.]]],
  174. [[[20., 21., 22., 23., 24.],
  175. [25., 26., 27., 28., 29.],
  176. [30., 31., 32., 33., 34.],
  177. [35., 36., 37., 38., 39.]],
  178. [[20., 21., 22., 23., 24.],
  179. [25., 26., 27., 28., 29.],
  180. [30., 31., 32., 33., 34.],
  181. [35., 36., 37., 38., 39.]],
  182. [[20., 21., 22., 23., 24.],
  183. [25., 26., 27., 28., 29.],
  184. [30., 31., 32., 33., 34.],
  185. [35., 36., 37., 38., 39.]],
  186. [[20., 21., 22., 23., 24.],
  187. [25., 26., 27., 28., 29.],
  188. [30., 31., 32., 33., 34.],
  189. [35., 36., 37., 38., 39.]],
  190. [[20., 21., 22., 23., 24.],
  191. [25., 26., 27., 28., 29.],
  192. [30., 31., 32., 33., 34.],
  193. [35., 36., 37., 38., 39.]]]]],
  194. [[[[[20., 21., 22., 23., 24.],
  195. [25., 26., 27., 28., 29.],
  196. [30., 31., 32., 33., 34.],
  197. [35., 36., 37., 38., 39.]],
  198. [[20., 21., 22., 23., 24.],
  199. [25., 26., 27., 28., 29.],
  200. [30., 31., 32., 33., 34.],
  201. [35., 36., 37., 38., 39.]],
  202. [[20., 21., 22., 23., 24.],
  203. [25., 26., 27., 28., 29.],
  204. [30., 31., 32., 33., 34.],
  205. [35., 36., 37., 38., 39.]],
  206. [[20., 21., 22., 23., 24.],
  207. [25., 26., 27., 28., 29.],
  208. [30., 31., 32., 33., 34.],
  209. [35., 36., 37., 38., 39.]],
  210. [[20., 21., 22., 23., 24.],
  211. [25., 26., 27., 28., 29.],
  212. [30., 31., 32., 33., 34.],
  213. [35., 36., 37., 38., 39.]]],
  214. [[[20., 21., 22., 23., 24.],
  215. [25., 26., 27., 28., 29.],
  216. [30., 31., 32., 33., 34.],
  217. [35., 36., 37., 38., 39.]],
  218. [[20., 21., 22., 23., 24.],
  219. [25., 26., 27., 28., 29.],
  220. [30., 31., 32., 33., 34.],
  221. [35., 36., 37., 38., 39.]],
  222. [[20., 21., 22., 23., 24.],
  223. [25., 26., 27., 28., 29.],
  224. [30., 31., 32., 33., 34.],
  225. [35., 36., 37., 38., 39.]],
  226. [[20., 21., 22., 23., 24.],
  227. [25., 26., 27., 28., 29.],
  228. [30., 31., 32., 33., 34.],
  229. [35., 36., 37., 38., 39.]],
  230. [[20., 21., 22., 23., 24.],
  231. [25., 26., 27., 28., 29.],
  232. [30., 31., 32., 33., 34.],
  233. [35., 36., 37., 38., 39.]]],
  234. [[[20., 21., 22., 23., 24.],
  235. [25., 26., 27., 28., 29.],
  236. [30., 31., 32., 33., 34.],
  237. [35., 36., 37., 38., 39.]],
  238. [[20., 21., 22., 23., 24.],
  239. [25., 26., 27., 28., 29.],
  240. [30., 31., 32., 33., 34.],
  241. [35., 36., 37., 38., 39.]],
  242. [[20., 21., 22., 23., 24.],
  243. [25., 26., 27., 28., 29.],
  244. [30., 31., 32., 33., 34.],
  245. [35., 36., 37., 38., 39.]],
  246. [[20., 21., 22., 23., 24.],
  247. [25., 26., 27., 28., 29.],
  248. [30., 31., 32., 33., 34.],
  249. [35., 36., 37., 38., 39.]],
  250. [[20., 21., 22., 23., 24.],
  251. [25., 26., 27., 28., 29.],
  252. [30., 31., 32., 33., 34.],
  253. [35., 36., 37., 38., 39.]]],
  254. [[[20., 21., 22., 23., 24.],
  255. [25., 26., 27., 28., 29.],
  256. [30., 31., 32., 33., 34.],
  257. [35., 36., 37., 38., 39.]],
  258. [[20., 21., 22., 23., 24.],
  259. [25., 26., 27., 28., 29.],
  260. [30., 31., 32., 33., 34.],
  261. [35., 36., 37., 38., 39.]],
  262. [[20., 21., 22., 23., 24.],
  263. [25., 26., 27., 28., 29.],
  264. [30., 31., 32., 33., 34.],
  265. [35., 36., 37., 38., 39.]],
  266. [[20., 21., 22., 23., 24.],
  267. [25., 26., 27., 28., 29.],
  268. [30., 31., 32., 33., 34.],
  269. [35., 36., 37., 38., 39.]],
  270. [[20., 21., 22., 23., 24.],
  271. [25., 26., 27., 28., 29.],
  272. [30., 31., 32., 33., 34.],
  273. [35., 36., 37., 38., 39.]]]],
  274. [[[[20., 21., 22., 23., 24.],
  275. [25., 26., 27., 28., 29.],
  276. [30., 31., 32., 33., 34.],
  277. [35., 36., 37., 38., 39.]],
  278. [[20., 21., 22., 23., 24.],
  279. [25., 26., 27., 28., 29.],
  280. [30., 31., 32., 33., 34.],
  281. [35., 36., 37., 38., 39.]],
  282. [[20., 21., 22., 23., 24.],
  283. [25., 26., 27., 28., 29.],
  284. [30., 31., 32., 33., 34.],
  285. [35., 36., 37., 38., 39.]],
  286. [[20., 21., 22., 23., 24.],
  287. [25., 26., 27., 28., 29.],
  288. [30., 31., 32., 33., 34.],
  289. [35., 36., 37., 38., 39.]],
  290. [[20., 21., 22., 23., 24.],
  291. [25., 26., 27., 28., 29.],
  292. [30., 31., 32., 33., 34.],
  293. [35., 36., 37., 38., 39.]]],
  294. [[[20., 21., 22., 23., 24.],
  295. [25., 26., 27., 28., 29.],
  296. [30., 31., 32., 33., 34.],
  297. [35., 36., 37., 38., 39.]],
  298. [[20., 21., 22., 23., 24.],
  299. [25., 26., 27., 28., 29.],
  300. [30., 31., 32., 33., 34.],
  301. [35., 36., 37., 38., 39.]],
  302. [[20., 21., 22., 23., 24.],
  303. [25., 26., 27., 28., 29.],
  304. [30., 31., 32., 33., 34.],
  305. [35., 36., 37., 38., 39.]],
  306. [[20., 21., 22., 23., 24.],
  307. [25., 26., 27., 28., 29.],
  308. [30., 31., 32., 33., 34.],
  309. [35., 36., 37., 38., 39.]],
  310. [[20., 21., 22., 23., 24.],
  311. [25., 26., 27., 28., 29.],
  312. [30., 31., 32., 33., 34.],
  313. [35., 36., 37., 38., 39.]]],
  314. [[[20., 21., 22., 23., 24.],
  315. [25., 26., 27., 28., 29.],
  316. [30., 31., 32., 33., 34.],
  317. [35., 36., 37., 38., 39.]],
  318. [[20., 21., 22., 23., 24.],
  319. [25., 26., 27., 28., 29.],
  320. [30., 31., 32., 33., 34.],
  321. [35., 36., 37., 38., 39.]],
  322. [[20., 21., 22., 23., 24.],
  323. [25., 26., 27., 28., 29.],
  324. [30., 31., 32., 33., 34.],
  325. [35., 36., 37., 38., 39.]],
  326. [[20., 21., 22., 23., 24.],
  327. [25., 26., 27., 28., 29.],
  328. [30., 31., 32., 33., 34.],
  329. [35., 36., 37., 38., 39.]],
  330. [[20., 21., 22., 23., 24.],
  331. [25., 26., 27., 28., 29.],
  332. [30., 31., 32., 33., 34.],
  333. [35., 36., 37., 38., 39.]]],
  334. [[[20., 21., 22., 23., 24.],
  335. [25., 26., 27., 28., 29.],
  336. [30., 31., 32., 33., 34.],
  337. [35., 36., 37., 38., 39.]],
  338. [[20., 21., 22., 23., 24.],
  339. [25., 26., 27., 28., 29.],
  340. [30., 31., 32., 33., 34.],
  341. [35., 36., 37., 38., 39.]],
  342. [[20., 21., 22., 23., 24.],
  343. [25., 26., 27., 28., 29.],
  344. [30., 31., 32., 33., 34.],
  345. [35., 36., 37., 38., 39.]],
  346. [[20., 21., 22., 23., 24.],
  347. [25., 26., 27., 28., 29.],
  348. [30., 31., 32., 33., 34.],
  349. [35., 36., 37., 38., 39.]],
  350. [[20., 21., 22., 23., 24.],
  351. [25., 26., 27., 28., 29.],
  352. [30., 31., 32., 33., 34.],
  353. [35., 36., 37., 38., 39.]]]]]],
  354. [[[[[[80., 81., 82., 83., 84.],
  355. [85., 86., 87., 88., 89.],
  356. [90., 91., 92., 93., 94.],
  357. [95., 96., 97., 98., 99.]],
  358. [[80., 81., 82., 83., 84.],
  359. [85., 86., 87., 88., 89.],
  360. [90., 91., 92., 93., 94.],
  361. [95., 96., 97., 98., 99.]],
  362. [[80., 81., 82., 83., 84.],
  363. [85., 86., 87., 88., 89.],
  364. [90., 91., 92., 93., 94.],
  365. [95., 96., 97., 98., 99.]],
  366. [[80., 81., 82., 83., 84.],
  367. [85., 86., 87., 88., 89.],
  368. [90., 91., 92., 93., 94.],
  369. [95., 96., 97., 98., 99.]],
  370. [[80., 81., 82., 83., 84.],
  371. [85., 86., 87., 88., 89.],
  372. [90., 91., 92., 93., 94.],
  373. [95., 96., 97., 98., 99.]]],
  374. [[[80., 81., 82., 83., 84.],
  375. [85., 86., 87., 88., 89.],
  376. [90., 91., 92., 93., 94.],
  377. [95., 96., 97., 98., 99.]],
  378. [[80., 81., 82., 83., 84.],
  379. [85., 86., 87., 88., 89.],
  380. [90., 91., 92., 93., 94.],
  381. [95., 96., 97., 98., 99.]],
  382. [[80., 81., 82., 83., 84.],
  383. [85., 86., 87., 88., 89.],
  384. [90., 91., 92., 93., 94.],
  385. [95., 96., 97., 98., 99.]],
  386. [[80., 81., 82., 83., 84.],
  387. [85., 86., 87., 88., 89.],
  388. [90., 91., 92., 93., 94.],
  389. [95., 96., 97., 98., 99.]],
  390. [[80., 81., 82., 83., 84.],
  391. [85., 86., 87., 88., 89.],
  392. [90., 91., 92., 93., 94.],
  393. [95., 96., 97., 98., 99.]]],
  394. [[[80., 81., 82., 83., 84.],
  395. [85., 86., 87., 88., 89.],
  396. [90., 91., 92., 93., 94.],
  397. [95., 96., 97., 98., 99.]],
  398. [[80., 81., 82., 83., 84.],
  399. [85., 86., 87., 88., 89.],
  400. [90., 91., 92., 93., 94.],
  401. [95., 96., 97., 98., 99.]],
  402. [[80., 81., 82., 83., 84.],
  403. [85., 86., 87., 88., 89.],
  404. [90., 91., 92., 93., 94.],
  405. [95., 96., 97., 98., 99.]],
  406. [[80., 81., 82., 83., 84.],
  407. [85., 86., 87., 88., 89.],
  408. [90., 91., 92., 93., 94.],
  409. [95., 96., 97., 98., 99.]],
  410. [[80., 81., 82., 83., 84.],
  411. [85., 86., 87., 88., 89.],
  412. [90., 91., 92., 93., 94.],
  413. [95., 96., 97., 98., 99.]]],
  414. [[[80., 81., 82., 83., 84.],
  415. [85., 86., 87., 88., 89.],
  416. [90., 91., 92., 93., 94.],
  417. [95., 96., 97., 98., 99.]],
  418. [[80., 81., 82., 83., 84.],
  419. [85., 86., 87., 88., 89.],
  420. [90., 91., 92., 93., 94.],
  421. [95., 96., 97., 98., 99.]],
  422. [[80., 81., 82., 83., 84.],
  423. [85., 86., 87., 88., 89.],
  424. [90., 91., 92., 93., 94.],
  425. [95., 96., 97., 98., 99.]],
  426. [[80., 81., 82., 83., 84.],
  427. [85., 86., 87., 88., 89.],
  428. [90., 91., 92., 93., 94.],
  429. [95., 96., 97., 98., 99.]],
  430. [[80., 81., 82., 83., 84.],
  431. [85., 86., 87., 88., 89.],
  432. [90., 91., 92., 93., 94.],
  433. [95., 96., 97., 98., 99.]]]],
  434. [[[[80., 81., 82., 83., 84.],
  435. [85., 86., 87., 88., 89.],
  436. [90., 91., 92., 93., 94.],
  437. [95., 96., 97., 98., 99.]],
  438. [[80., 81., 82., 83., 84.],
  439. [85., 86., 87., 88., 89.],
  440. [90., 91., 92., 93., 94.],
  441. [95., 96., 97., 98., 99.]],
  442. [[80., 81., 82., 83., 84.],
  443. [85., 86., 87., 88., 89.],
  444. [90., 91., 92., 93., 94.],
  445. [95., 96., 97., 98., 99.]],
  446. [[80., 81., 82., 83., 84.],
  447. [85., 86., 87., 88., 89.],
  448. [90., 91., 92., 93., 94.],
  449. [95., 96., 97., 98., 99.]],
  450. [[80., 81., 82., 83., 84.],
  451. [85., 86., 87., 88., 89.],
  452. [90., 91., 92., 93., 94.],
  453. [95., 96., 97., 98., 99.]]],
  454. [[[80., 81., 82., 83., 84.],
  455. [85., 86., 87., 88., 89.],
  456. [90., 91., 92., 93., 94.],
  457. [95., 96., 97., 98., 99.]],
  458. [[80., 81., 82., 83., 84.],
  459. [85., 86., 87., 88., 89.],
  460. [90., 91., 92., 93., 94.],
  461. [95., 96., 97., 98., 99.]],
  462. [[80., 81., 82., 83., 84.],
  463. [85., 86., 87., 88., 89.],
  464. [90., 91., 92., 93., 94.],
  465. [95., 96., 97., 98., 99.]],
  466. [[80., 81., 82., 83., 84.],
  467. [85., 86., 87., 88., 89.],
  468. [90., 91., 92., 93., 94.],
  469. [95., 96., 97., 98., 99.]],
  470. [[80., 81., 82., 83., 84.],
  471. [85., 86., 87., 88., 89.],
  472. [90., 91., 92., 93., 94.],
  473. [95., 96., 97., 98., 99.]]],
  474. [[[80., 81., 82., 83., 84.],
  475. [85., 86., 87., 88., 89.],
  476. [90., 91., 92., 93., 94.],
  477. [95., 96., 97., 98., 99.]],
  478. [[80., 81., 82., 83., 84.],
  479. [85., 86., 87., 88., 89.],
  480. [90., 91., 92., 93., 94.],
  481. [95., 96., 97., 98., 99.]],
  482. [[80., 81., 82., 83., 84.],
  483. [85., 86., 87., 88., 89.],
  484. [90., 91., 92., 93., 94.],
  485. [95., 96., 97., 98., 99.]],
  486. [[80., 81., 82., 83., 84.],
  487. [85., 86., 87., 88., 89.],
  488. [90., 91., 92., 93., 94.],
  489. [95., 96., 97., 98., 99.]],
  490. [[80., 81., 82., 83., 84.],
  491. [85., 86., 87., 88., 89.],
  492. [90., 91., 92., 93., 94.],
  493. [95., 96., 97., 98., 99.]]],
  494. [[[80., 81., 82., 83., 84.],
  495. [85., 86., 87., 88., 89.],
  496. [90., 91., 92., 93., 94.],
  497. [95., 96., 97., 98., 99.]],
  498. [[80., 81., 82., 83., 84.],
  499. [85., 86., 87., 88., 89.],
  500. [90., 91., 92., 93., 94.],
  501. [95., 96., 97., 98., 99.]],
  502. [[80., 81., 82., 83., 84.],
  503. [85., 86., 87., 88., 89.],
  504. [90., 91., 92., 93., 94.],
  505. [95., 96., 97., 98., 99.]],
  506. [[80., 81., 82., 83., 84.],
  507. [85., 86., 87., 88., 89.],
  508. [90., 91., 92., 93., 94.],
  509. [95., 96., 97., 98., 99.]],
  510. [[80., 81., 82., 83., 84.],
  511. [85., 86., 87., 88., 89.],
  512. [90., 91., 92., 93., 94.],
  513. [95., 96., 97., 98., 99.]]]]],
  514. [[[[[80., 81., 82., 83., 84.],
  515. [85., 86., 87., 88., 89.],
  516. [90., 91., 92., 93., 94.],
  517. [95., 96., 97., 98., 99.]],
  518. [[80., 81., 82., 83., 84.],
  519. [85., 86., 87., 88., 89.],
  520. [90., 91., 92., 93., 94.],
  521. [95., 96., 97., 98., 99.]],
  522. [[80., 81., 82., 83., 84.],
  523. [85., 86., 87., 88., 89.],
  524. [90., 91., 92., 93., 94.],
  525. [95., 96., 97., 98., 99.]],
  526. [[80., 81., 82., 83., 84.],
  527. [85., 86., 87., 88., 89.],
  528. [90., 91., 92., 93., 94.],
  529. [95., 96., 97., 98., 99.]],
  530. [[80., 81., 82., 83., 84.],
  531. [85., 86., 87., 88., 89.],
  532. [90., 91., 92., 93., 94.],
  533. [95., 96., 97., 98., 99.]]],
  534. [[[80., 81., 82., 83., 84.],
  535. [85., 86., 87., 88., 89.],
  536. [90., 91., 92., 93., 94.],
  537. [95., 96., 97., 98., 99.]],
  538. [[80., 81., 82., 83., 84.],
  539. [85., 86., 87., 88., 89.],
  540. [90., 91., 92., 93., 94.],
  541. [95., 96., 97., 98., 99.]],
  542. [[80., 81., 82., 83., 84.],
  543. [85., 86., 87., 88., 89.],
  544. [90., 91., 92., 93., 94.],
  545. [95., 96., 97., 98., 99.]],
  546. [[80., 81., 82., 83., 84.],
  547. [85., 86., 87., 88., 89.],
  548. [90., 91., 92., 93., 94.],
  549. [95., 96., 97., 98., 99.]],
  550. [[80., 81., 82., 83., 84.],
  551. [85., 86., 87., 88., 89.],
  552. [90., 91., 92., 93., 94.],
  553. [95., 96., 97., 98., 99.]]],
  554. [[[80., 81., 82., 83., 84.],
  555. [85., 86., 87., 88., 89.],
  556. [90., 91., 92., 93., 94.],
  557. [95., 96., 97., 98., 99.]],
  558. [[80., 81., 82., 83., 84.],
  559. [85., 86., 87., 88., 89.],
  560. [90., 91., 92., 93., 94.],
  561. [95., 96., 97., 98., 99.]],
  562. [[80., 81., 82., 83., 84.],
  563. [85., 86., 87., 88., 89.],
  564. [90., 91., 92., 93., 94.],
  565. [95., 96., 97., 98., 99.]],
  566. [[80., 81., 82., 83., 84.],
  567. [85., 86., 87., 88., 89.],
  568. [90., 91., 92., 93., 94.],
  569. [95., 96., 97., 98., 99.]],
  570. [[80., 81., 82., 83., 84.],
  571. [85., 86., 87., 88., 89.],
  572. [90., 91., 92., 93., 94.],
  573. [95., 96., 97., 98., 99.]]],
  574. [[[80., 81., 82., 83., 84.],
  575. [85., 86., 87., 88., 89.],
  576. [90., 91., 92., 93., 94.],
  577. [95., 96., 97., 98., 99.]],
  578. [[80., 81., 82., 83., 84.],
  579. [85., 86., 87., 88., 89.],
  580. [90., 91., 92., 93., 94.],
  581. [95., 96., 97., 98., 99.]],
  582. [[80., 81., 82., 83., 84.],
  583. [85., 86., 87., 88., 89.],
  584. [90., 91., 92., 93., 94.],
  585. [95., 96., 97., 98., 99.]],
  586. [[80., 81., 82., 83., 84.],
  587. [85., 86., 87., 88., 89.],
  588. [90., 91., 92., 93., 94.],
  589. [95., 96., 97., 98., 99.]],
  590. [[80., 81., 82., 83., 84.],
  591. [85., 86., 87., 88., 89.],
  592. [90., 91., 92., 93., 94.],
  593. [95., 96., 97., 98., 99.]]]],
  594. [[[[80., 81., 82., 83., 84.],
  595. [85., 86., 87., 88., 89.],
  596. [90., 91., 92., 93., 94.],
  597. [95., 96., 97., 98., 99.]],
  598. [[80., 81., 82., 83., 84.],
  599. [85., 86., 87., 88., 89.],
  600. [90., 91., 92., 93., 94.],
  601. [95., 96., 97., 98., 99.]],
  602. [[80., 81., 82., 83., 84.],
  603. [85., 86., 87., 88., 89.],
  604. [90., 91., 92., 93., 94.],
  605. [95., 96., 97., 98., 99.]],
  606. [[80., 81., 82., 83., 84.],
  607. [85., 86., 87., 88., 89.],
  608. [90., 91., 92., 93., 94.],
  609. [95., 96., 97., 98., 99.]],
  610. [[80., 81., 82., 83., 84.],
  611. [85., 86., 87., 88., 89.],
  612. [90., 91., 92., 93., 94.],
  613. [95., 96., 97., 98., 99.]]],
  614. [[[80., 81., 82., 83., 84.],
  615. [85., 86., 87., 88., 89.],
  616. [90., 91., 92., 93., 94.],
  617. [95., 96., 97., 98., 99.]],
  618. [[80., 81., 82., 83., 84.],
  619. [85., 86., 87., 88., 89.],
  620. [90., 91., 92., 93., 94.],
  621. [95., 96., 97., 98., 99.]],
  622. [[80., 81., 82., 83., 84.],
  623. [85., 86., 87., 88., 89.],
  624. [90., 91., 92., 93., 94.],
  625. [95., 96., 97., 98., 99.]],
  626. [[80., 81., 82., 83., 84.],
  627. [85., 86., 87., 88., 89.],
  628. [90., 91., 92., 93., 94.],
  629. [95., 96., 97., 98., 99.]],
  630. [[80., 81., 82., 83., 84.],
  631. [85., 86., 87., 88., 89.],
  632. [90., 91., 92., 93., 94.],
  633. [95., 96., 97., 98., 99.]]],
  634. [[[80., 81., 82., 83., 84.],
  635. [85., 86., 87., 88., 89.],
  636. [90., 91., 92., 93., 94.],
  637. [95., 96., 97., 98., 99.]],
  638. [[80., 81., 82., 83., 84.],
  639. [85., 86., 87., 88., 89.],
  640. [90., 91., 92., 93., 94.],
  641. [95., 96., 97., 98., 99.]],
  642. [[80., 81., 82., 83., 84.],
  643. [85., 86., 87., 88., 89.],
  644. [90., 91., 92., 93., 94.],
  645. [95., 96., 97., 98., 99.]],
  646. [[80., 81., 82., 83., 84.],
  647. [85., 86., 87., 88., 89.],
  648. [90., 91., 92., 93., 94.],
  649. [95., 96., 97., 98., 99.]],
  650. [[80., 81., 82., 83., 84.],
  651. [85., 86., 87., 88., 89.],
  652. [90., 91., 92., 93., 94.],
  653. [95., 96., 97., 98., 99.]]],
  654. [[[80., 81., 82., 83., 84.],
  655. [85., 86., 87., 88., 89.],
  656. [90., 91., 92., 93., 94.],
  657. [95., 96., 97., 98., 99.]],
  658. [[80., 81., 82., 83., 84.],
  659. [85., 86., 87., 88., 89.],
  660. [90., 91., 92., 93., 94.],
  661. [95., 96., 97., 98., 99.]],
  662. [[80., 81., 82., 83., 84.],
  663. [85., 86., 87., 88., 89.],
  664. [90., 91., 92., 93., 94.],
  665. [95., 96., 97., 98., 99.]],
  666. [[80., 81., 82., 83., 84.],
  667. [85., 86., 87., 88., 89.],
  668. [90., 91., 92., 93., 94.],
  669. [95., 96., 97., 98., 99.]],
  670. [[80., 81., 82., 83., 84.],
  671. [85., 86., 87., 88., 89.],
  672. [90., 91., 92., 93., 94.],
  673. [95., 96., 97., 98., 99.]]]]]]])
  674. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  675. gather = GatherNet()
  676. output = gather(x, indices)
  677. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  678. diff = output.asnumpy() - expect
  679. assert np.all(diff < error)
  680. assert np.all(-diff < error)
  681. class GatherNet1(nn.Cell):
  682. def __init__(self):
  683. super(GatherNet1, self).__init__()
  684. self.gather = P.GatherV2()
  685. def construct(self, x, indices):
  686. return self.gather(x, indices, -1)
  687. @pytest.mark.level0
  688. @pytest.mark.platform_x86_gpu_training
  689. @pytest.mark.env_onecard
  690. def test_gather1():
  691. x = Tensor(np.arange(2 * 3 * 4 * 5, dtype=np.float32).reshape(2, 3, 4, 5))
  692. indices = Tensor(np.array([1, 3, 4], dtype='i4'))
  693. expect = np.array([[[[1., 3., 4.],
  694. [6., 8., 9.],
  695. [11., 13., 14.],
  696. [16., 18., 19.]],
  697. [[21., 23., 24.],
  698. [26., 28., 29.],
  699. [31., 33., 34.],
  700. [36., 38., 39.]],
  701. [[41., 43., 44.],
  702. [46., 48., 49.],
  703. [51., 53., 54.],
  704. [56., 58., 59.]]],
  705. [[[61., 63., 64.],
  706. [66., 68., 69.],
  707. [71., 73., 74.],
  708. [76., 78., 79.]],
  709. [[81., 83., 84.],
  710. [86., 88., 89.],
  711. [91., 93., 94.],
  712. [96., 98., 99.]],
  713. [[101., 103., 104.],
  714. [106., 108., 109.],
  715. [111., 113., 114.],
  716. [116., 118., 119.]]]])
  717. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  718. gather = GatherNet1()
  719. output = gather(x, indices)
  720. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  721. diff = output.asnumpy() - expect
  722. assert np.all(diff < error)
  723. assert np.all(-diff < error)
  724. class GatherNet2(nn.Cell):
  725. def __init__(self):
  726. super(GatherNet2, self).__init__()
  727. self.gather = P.GatherV2()
  728. def construct(self, x, indices):
  729. return self.gather(x, indices, 0)
  730. @pytest.mark.level0
  731. @pytest.mark.platform_x86_gpu_training
  732. @pytest.mark.env_onecard
  733. def test_gather2():
  734. x = Tensor(np.array([[4., 5., 4., 1., 5.,],
  735. [4., 9., 5., 6., 4.,],
  736. [9., 8., 4., 3., 6.,],
  737. [0., 4., 2., 2., 8.,],
  738. [1., 8., 6., 2., 8.,],
  739. [8., 1., 9., 7., 3.,],
  740. [7., 9., 2., 5., 7.,],
  741. [9., 8., 6., 8., 5.,],
  742. [3., 7., 2., 7., 4.,],
  743. [4., 2., 8., 2., 9.,]]
  744. ).astype(np.float32))
  745. indices = Tensor(np.array([[4000, 1, 300000]]).astype(np.int32))
  746. expect = np.array([[[0., 0., 0., 0., 0.],
  747. [4., 9., 5., 6., 4.],
  748. [0., 0., 0., 0., 0.]]])
  749. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  750. gather = GatherNet2()
  751. output = gather(x, indices)
  752. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  753. diff = output.asnumpy() - expect
  754. assert np.all(diff < error)
  755. assert np.all(-diff < error)
  756. # Dynamic Shape testing ahead
  757. class GatherNetDynamic1(nn.Cell):
  758. def __init__(self):
  759. super(GatherNetDynamic1, self).__init__()
  760. self.gather = P.GatherV2()
  761. self.gpu_convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
  762. def construct(self, x, indices):
  763. # Testing only second input dynamic
  764. indices_dyn = self.gpu_convert_to_dynamic_shape(indices)
  765. return self.gather(x, indices_dyn, 0)
  766. @pytest.mark.level0
  767. @pytest.mark.platform_x86_gpu_training
  768. @pytest.mark.env_onecard
  769. def test_gather_dynamic_1():
  770. x = Tensor(np.array([[4., 5., 4., 1., 5.,],
  771. [4., 9., 5., 6., 4.,],
  772. [9., 8., 4., 3., 6.,],
  773. [0., 4., 2., 2., 8.,],
  774. [1., 8., 6., 2., 8.,],
  775. [8., 1., 9., 7., 3.,],
  776. [7., 9., 2., 5., 7.,],
  777. [9., 8., 6., 8., 5.,],
  778. [3., 7., 2., 7., 4.,],
  779. [4., 2., 8., 2., 9.,]]
  780. ).astype(np.float32))
  781. indices = Tensor(np.array([[4000, 1, 300000]]).astype(np.int32))
  782. expect = np.array([[[0., 0., 0., 0., 0.],
  783. [4., 9., 5., 6., 4.],
  784. [0., 0., 0., 0., 0.]]])
  785. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  786. gather = GatherNetDynamic1()
  787. output = gather(x, indices)
  788. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  789. diff = output.asnumpy() - expect
  790. assert np.all(diff < error)
  791. assert np.all(-diff < error)
  792. class GatherNetDynamic2(nn.Cell):
  793. def __init__(self):
  794. super(GatherNetDynamic2, self).__init__()
  795. self.gather = P.GatherV2()
  796. self.gpu_convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
  797. def construct(self, x, indices):
  798. # Testing only first input dynamic
  799. x_dyn = self.gpu_convert_to_dynamic_shape(x)
  800. return self.gather(x_dyn, indices, -1)
  801. @pytest.mark.level0
  802. @pytest.mark.platform_x86_gpu_training
  803. @pytest.mark.env_onecard
  804. def test_gather_dynamic_2():
  805. x = Tensor(np.arange(2 * 3 * 4 * 5, dtype=np.float32).reshape(2, 3, 4, 5))
  806. indices = Tensor(np.array([1, 3, 4], dtype='i4'))
  807. expect = np.array([[[[1., 3., 4.],
  808. [6., 8., 9.],
  809. [11., 13., 14.],
  810. [16., 18., 19.]],
  811. [[21., 23., 24.],
  812. [26., 28., 29.],
  813. [31., 33., 34.],
  814. [36., 38., 39.]],
  815. [[41., 43., 44.],
  816. [46., 48., 49.],
  817. [51., 53., 54.],
  818. [56., 58., 59.]]],
  819. [[[61., 63., 64.],
  820. [66., 68., 69.],
  821. [71., 73., 74.],
  822. [76., 78., 79.]],
  823. [[81., 83., 84.],
  824. [86., 88., 89.],
  825. [91., 93., 94.],
  826. [96., 98., 99.]],
  827. [[101., 103., 104.],
  828. [106., 108., 109.],
  829. [111., 113., 114.],
  830. [116., 118., 119.]]]])
  831. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  832. gather = GatherNetDynamic2()
  833. output = gather(x, indices)
  834. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  835. diff = output.asnumpy() - expect
  836. assert np.all(diff < error)
  837. assert np.all(-diff < error)
  838. class GatherNetDynamic3(nn.Cell):
  839. def __init__(self):
  840. super(GatherNetDynamic3, self).__init__()
  841. self.gather = P.GatherV2()
  842. self.gpu_convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
  843. def construct(self, x, indices):
  844. # Testing both inputs dynamic shapes
  845. x_dyn = self.gpu_convert_to_dynamic_shape(x)
  846. indices_dyn = self.gpu_convert_to_dynamic_shape(indices)
  847. return self.gather(x_dyn, indices_dyn, -1)
  848. @pytest.mark.level0
  849. @pytest.mark.platform_x86_gpu_training
  850. @pytest.mark.env_onecard
  851. def test_gather_dynamic_3():
  852. x = Tensor(np.arange(2 * 3 * 4 * 5, dtype=np.float32).reshape(2, 3, 4, 5))
  853. indices = Tensor(np.array([1, 3, 4], dtype='i4'))
  854. expect = np.array([[[[1., 3., 4.],
  855. [6., 8., 9.],
  856. [11., 13., 14.],
  857. [16., 18., 19.]],
  858. [[21., 23., 24.],
  859. [26., 28., 29.],
  860. [31., 33., 34.],
  861. [36., 38., 39.]],
  862. [[41., 43., 44.],
  863. [46., 48., 49.],
  864. [51., 53., 54.],
  865. [56., 58., 59.]]],
  866. [[[61., 63., 64.],
  867. [66., 68., 69.],
  868. [71., 73., 74.],
  869. [76., 78., 79.]],
  870. [[81., 83., 84.],
  871. [86., 88., 89.],
  872. [91., 93., 94.],
  873. [96., 98., 99.]],
  874. [[101., 103., 104.],
  875. [106., 108., 109.],
  876. [111., 113., 114.],
  877. [116., 118., 119.]]]])
  878. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  879. gather = GatherNetDynamic3()
  880. output = gather(x, indices)
  881. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  882. diff = output.asnumpy() - expect
  883. assert np.all(diff < error)
  884. assert np.all(-diff < error)