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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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.Gather()
  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.Gather()
  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.Gather()
  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.int64))
  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 GatherNetDynamic(nn.Cell):
  758. def __init__(self, axis=0, dyn_a=True, dyn_b=True):
  759. super(GatherNetDynamic, self).__init__()
  760. self.gather = P.Gather()
  761. self.gpu_convert_to_dynamic_shape = inner.GpuConvertToDynamicShape()
  762. self.to_dyn_1 = dyn_a
  763. self.to_dyn_2 = dyn_b
  764. self.axis = axis
  765. def construct(self, x, indices):
  766. # testing selective inputs being dynamic
  767. if self.to_dyn_1:
  768. x = self.gpu_convert_to_dynamic_shape(x)
  769. if self.to_dyn_2:
  770. indices = self.gpu_convert_to_dynamic_shape(indices)
  771. return self.gather(x, indices, self.axis)
  772. @pytest.mark.level0
  773. @pytest.mark.platform_x86_gpu_training
  774. @pytest.mark.env_onecard
  775. def test_gatherV2_dyn_ab():
  776. """
  777. Tests for Dynamic shape with both inputs dynamic
  778. """
  779. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  780. gather = GatherNetDynamic()
  781. x = Tensor(np.array([[4., 5., 4., 1., 5.,],
  782. [4., 9., 5., 6., 4.,],
  783. [9., 8., 4., 3., 6.,],
  784. [0., 4., 2., 2., 8.,],
  785. [1., 8., 6., 2., 8.,],
  786. [8., 1., 9., 7., 3.,],
  787. [7., 9., 2., 5., 7.,],
  788. [9., 8., 6., 8., 5.,],
  789. [3., 7., 2., 7., 4.,],
  790. [4., 2., 8., 2., 9.,]]
  791. ).astype(np.float32))
  792. indices = Tensor(np.array([[4000, 1, 300000]]).astype(np.int32))
  793. expect = np.array([[[0., 0., 0., 0., 0.],
  794. [4., 9., 5., 6., 4.],
  795. [0., 0., 0., 0., 0.]]])
  796. output = gather(x, indices)
  797. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  798. diff = output.asnumpy() - expect
  799. assert np.all(diff < error)
  800. assert np.all(-diff < error)
  801. @pytest.mark.level0
  802. @pytest.mark.platform_x86_gpu_training
  803. @pytest.mark.env_onecard
  804. def test_gatherV2_dyn_a():
  805. """
  806. Tests for Dynamic shape with only first input dynamic
  807. """
  808. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  809. gather = GatherNetDynamic(-1, True, False)
  810. # test 1
  811. x = Tensor(np.array([[4., 5., 4., 1., 5.,],
  812. [4., 9., 5., 6., 4.,],
  813. [9., 8., 4., 3., 6.,],
  814. [0., 4., 2., 2., 8.,],
  815. [1., 8., 6., 2., 8.,],
  816. [8., 1., 9., 7., 3.,],
  817. [7., 9., 2., 5., 7.,],
  818. [9., 8., 6., 8., 5.,],
  819. [3., 7., 2., 7., 4.,],
  820. [4., 2., 8., 2., 9.,]]
  821. ).astype(np.float32))
  822. indices = Tensor(np.array([[4000, 1, 300000]]).astype(np.int64))
  823. expect = np.array([[[0., 5., 0.]],
  824. [[0., 9., 0.]],
  825. [[0., 8., 0.]],
  826. [[0., 4., 0.]],
  827. [[0., 8., 0.]],
  828. [[0., 1., 0.]],
  829. [[0., 9., 0.]],
  830. [[0., 8., 0.]],
  831. [[0., 7., 0.]],
  832. [[0., 2., 0.]]]).astype(np.float32)
  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. # test 2
  839. x = Tensor(np.arange(2 * 3 * 4 * 5, dtype=np.float32).reshape(2, 3, 4, 5))
  840. indices = Tensor(np.array([1, 3, 4], dtype='i4'))
  841. expect = np.array([[[[1., 3., 4.],
  842. [6., 8., 9.],
  843. [11., 13., 14.],
  844. [16., 18., 19.]],
  845. [[21., 23., 24.],
  846. [26., 28., 29.],
  847. [31., 33., 34.],
  848. [36., 38., 39.]],
  849. [[41., 43., 44.],
  850. [46., 48., 49.],
  851. [51., 53., 54.],
  852. [56., 58., 59.]]],
  853. [[[61., 63., 64.],
  854. [66., 68., 69.],
  855. [71., 73., 74.],
  856. [76., 78., 79.]],
  857. [[81., 83., 84.],
  858. [86., 88., 89.],
  859. [91., 93., 94.],
  860. [96., 98., 99.]],
  861. [[101., 103., 104.],
  862. [106., 108., 109.],
  863. [111., 113., 114.],
  864. [116., 118., 119.]]]])
  865. output = gather(x, indices)
  866. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  867. diff = output.asnumpy() - expect
  868. assert np.all(diff < error)
  869. assert np.all(-diff < error)
  870. @pytest.mark.level0
  871. @pytest.mark.platform_x86_gpu_training
  872. @pytest.mark.env_onecard
  873. def test_gatherV2_dyn_b():
  874. """
  875. Tests for Dynamic shape with only second input dynamic
  876. """
  877. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  878. gather = GatherNetDynamic(-1, False, True)
  879. # test 1
  880. x = Tensor(np.array([[4., 5., 4., 1., 5.,],
  881. [4., 9., 5., 6., 4.,],
  882. [9., 8., 4., 3., 6.,],
  883. [0., 4., 2., 2., 8.,],
  884. [1., 8., 6., 2., 8.,],
  885. [8., 1., 9., 7., 3.,],
  886. [7., 9., 2., 5., 7.,],
  887. [9., 8., 6., 8., 5.,],
  888. [3., 7., 2., 7., 4.,],
  889. [4., 2., 8., 2., 9.,]]
  890. ).astype(np.float32))
  891. indices = Tensor(np.array([[4000, 1, 300000]]).astype(np.int32))
  892. expect = np.array([[[0., 5., 0.]],
  893. [[0., 9., 0.]],
  894. [[0., 8., 0.]],
  895. [[0., 4., 0.]],
  896. [[0., 8., 0.]],
  897. [[0., 1., 0.]],
  898. [[0., 9., 0.]],
  899. [[0., 8., 0.]],
  900. [[0., 7., 0.]],
  901. [[0., 2., 0.]]]).astype(np.float32)
  902. output = gather(x, indices)
  903. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  904. diff = output.asnumpy() - expect
  905. assert np.all(diff < error)
  906. assert np.all(-diff < error)
  907. # test 2
  908. x = Tensor(np.arange(2 * 3 * 4 * 5, dtype=np.float32).reshape(2, 3, 4, 5))
  909. indices = Tensor(np.array([1, 3, 4], dtype='i4'))
  910. expect = np.array([[[[1., 3., 4.],
  911. [6., 8., 9.],
  912. [11., 13., 14.],
  913. [16., 18., 19.]],
  914. [[21., 23., 24.],
  915. [26., 28., 29.],
  916. [31., 33., 34.],
  917. [36., 38., 39.]],
  918. [[41., 43., 44.],
  919. [46., 48., 49.],
  920. [51., 53., 54.],
  921. [56., 58., 59.]]],
  922. [[[61., 63., 64.],
  923. [66., 68., 69.],
  924. [71., 73., 74.],
  925. [76., 78., 79.]],
  926. [[81., 83., 84.],
  927. [86., 88., 89.],
  928. [91., 93., 94.],
  929. [96., 98., 99.]],
  930. [[101., 103., 104.],
  931. [106., 108., 109.],
  932. [111., 113., 114.],
  933. [116., 118., 119.]]]])
  934. output = gather(x, indices)
  935. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  936. diff = output.asnumpy() - expect
  937. assert np.all(diff < error)
  938. assert np.all(-diff < error)