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_math_ops.py 77 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705
  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. """unit tests for numpy math operations"""
  16. import pytest
  17. import numpy as onp
  18. import mindspore.numpy as mnp
  19. from mindspore import context
  20. from mindspore.common.dtype import dtype_to_nptype
  21. from .utils import rand_int, rand_bool, run_binop_test, run_unary_test, run_multi_test, \
  22. run_single_test, match_res, match_array, match_meta, match_all_arrays, to_tensor
  23. context.set_context(mode=context.PYNATIVE_MODE)
  24. class Cases():
  25. def __init__(self):
  26. self.arrs = [
  27. rand_int(2),
  28. rand_int(2, 3),
  29. rand_int(2, 3, 4),
  30. ]
  31. # scalars expanded across the 0th dimension
  32. self.scalars = [
  33. rand_int(),
  34. rand_int(1),
  35. rand_int(1, 1),
  36. ]
  37. # arrays of the same size expanded across the 0th dimension
  38. self.expanded_arrs = [
  39. rand_int(2, 3),
  40. rand_int(1, 2, 3),
  41. rand_int(1, 1, 2, 3),
  42. ]
  43. # arrays with last dimension aligned
  44. self.aligned_arrs = [
  45. rand_int(2, 3),
  46. rand_int(1, 4, 3),
  47. rand_int(5, 1, 2, 3),
  48. rand_int(4, 2, 1, 1, 3),
  49. ]
  50. # arrays which can be broadcast
  51. self.broadcastables = [
  52. rand_int(5),
  53. rand_int(6, 1),
  54. rand_int(7, 1, 5),
  55. ]
  56. # boolean arrays which can be broadcast
  57. self.bool_broadcastables = [
  58. rand_bool(),
  59. rand_bool(1),
  60. rand_bool(5),
  61. rand_bool(6, 1),
  62. rand_bool(7, 1, 5),
  63. rand_bool(8, 1, 6, 1),
  64. ]
  65. # core dimension 0 is matched for each
  66. # pair of array[i] and array[i + 1]
  67. self.core_broadcastables = [
  68. rand_int(3),
  69. rand_int(3),
  70. rand_int(6),
  71. rand_int(6, 4),
  72. rand_int(5, 2),
  73. rand_int(2),
  74. rand_int(2, 9),
  75. rand_int(9, 8),
  76. rand_int(6),
  77. rand_int(2, 6, 5),
  78. rand_int(9, 2, 7),
  79. rand_int(7),
  80. rand_int(5, 2, 4),
  81. rand_int(6, 1, 4, 9),
  82. rand_int(7, 1, 5, 3, 2),
  83. rand_int(8, 1, 6, 1, 2, 9),
  84. ]
  85. # arrays with dimensions of size 1
  86. self.nested_arrs = [
  87. rand_int(1),
  88. rand_int(1, 2),
  89. rand_int(3, 1, 8),
  90. rand_int(1, 3, 9, 1),
  91. ]
  92. test_case = Cases()
  93. def mnp_add(x1, x2):
  94. return mnp.add(x1, x2)
  95. def onp_add(x1, x2):
  96. return onp.add(x1, x2)
  97. def mnp_subtract(x1, x2):
  98. return mnp.subtract(x1, x2)
  99. def onp_subtract(x1, x2):
  100. return onp.subtract(x1, x2)
  101. def mnp_mutiply(x1, x2):
  102. return mnp.multiply(x1, x2)
  103. def onp_multiply(x1, x2):
  104. return onp.multiply(x1, x2)
  105. def mnp_divide(x1, x2):
  106. return mnp.divide(x1, x2)
  107. def onp_divide(x1, x2):
  108. return onp.divide(x1, x2)
  109. def mnp_true_divide(x1, x2):
  110. return mnp.true_divide(x1, x2)
  111. def onp_true_divide(x1, x2):
  112. return onp.true_divide(x1, x2)
  113. def mnp_power(x1, x2):
  114. return mnp.power(x1, x2)
  115. def onp_power(x1, x2):
  116. return onp.power(x1, x2)
  117. def mnp_float_power(x1, x2):
  118. return mnp.float_power(x1, x2)
  119. def onp_float_power(x1, x2):
  120. return onp.float_power(x1, x2)
  121. def mnp_minimum(a, b):
  122. return mnp.minimum(a, b)
  123. def onp_minimum(a, b):
  124. return onp.minimum(a, b)
  125. @pytest.mark.level1
  126. @pytest.mark.platform_arm_ascend_training
  127. @pytest.mark.platform_x86_ascend_training
  128. @pytest.mark.platform_x86_gpu_training
  129. @pytest.mark.platform_x86_cpu
  130. @pytest.mark.env_onecard
  131. def test_add():
  132. run_binop_test(mnp_add, onp_add, test_case)
  133. @pytest.mark.level1
  134. @pytest.mark.platform_arm_ascend_training
  135. @pytest.mark.platform_x86_ascend_training
  136. @pytest.mark.platform_x86_gpu_training
  137. @pytest.mark.platform_x86_cpu
  138. @pytest.mark.env_onecard
  139. def test_subtract():
  140. run_binop_test(mnp_subtract, onp_subtract, test_case)
  141. @pytest.mark.level1
  142. @pytest.mark.platform_arm_ascend_training
  143. @pytest.mark.platform_x86_ascend_training
  144. @pytest.mark.platform_x86_gpu_training
  145. @pytest.mark.platform_x86_cpu
  146. @pytest.mark.env_onecard
  147. def test_multiply():
  148. run_binop_test(mnp_mutiply, onp_multiply, test_case)
  149. @pytest.mark.level1
  150. @pytest.mark.platform_arm_ascend_training
  151. @pytest.mark.platform_x86_ascend_training
  152. @pytest.mark.platform_x86_gpu_training
  153. @pytest.mark.platform_x86_cpu
  154. @pytest.mark.env_onecard
  155. def test_divide():
  156. run_binop_test(mnp_divide, onp_divide, test_case)
  157. @pytest.mark.level1
  158. @pytest.mark.platform_arm_ascend_training
  159. @pytest.mark.platform_x86_ascend_training
  160. @pytest.mark.platform_x86_gpu_training
  161. @pytest.mark.platform_x86_cpu
  162. @pytest.mark.env_onecard
  163. def test_true_divide():
  164. run_binop_test(mnp_true_divide, onp_true_divide, test_case)
  165. @pytest.mark.level1
  166. @pytest.mark.platform_arm_ascend_training
  167. @pytest.mark.platform_x86_ascend_training
  168. @pytest.mark.platform_x86_gpu_training
  169. @pytest.mark.platform_x86_cpu
  170. @pytest.mark.env_onecard
  171. def test_power():
  172. run_binop_test(mnp_power, onp_power, test_case, error=1e-5)
  173. @pytest.mark.level1
  174. @pytest.mark.platform_arm_ascend_training
  175. @pytest.mark.platform_x86_ascend_training
  176. @pytest.mark.platform_x86_gpu_training
  177. @pytest.mark.platform_x86_cpu
  178. @pytest.mark.env_onecard
  179. def test_float_power():
  180. run_binop_test(mnp_float_power, onp_float_power, test_case, error=1e-5)
  181. @pytest.mark.level1
  182. @pytest.mark.platform_x86_gpu_training
  183. @pytest.mark.platform_x86_cpu
  184. @pytest.mark.env_onecard
  185. def test_minimum():
  186. run_binop_test(mnp_minimum, onp_minimum, test_case)
  187. x = onp.random.randint(-10, 10, 20).astype(onp.float32)
  188. y = onp.random.randint(-10, 10, 20).astype(onp.float32)
  189. x[onp.random.randint(0, 10, 3)] = onp.nan
  190. y[onp.random.randint(0, 10, 3)] = onp.nan
  191. x[onp.random.randint(0, 10, 3)] = onp.NINF
  192. y[onp.random.randint(0, 10, 3)] = onp.NINF
  193. x[onp.random.randint(0, 10, 3)] = onp.PINF
  194. y[onp.random.randint(0, 10, 3)] = onp.PINF
  195. match_res(mnp_minimum, onp_minimum, x, y)
  196. match_res(mnp_minimum, onp_minimum, y, x)
  197. def mnp_tensordot(x, y):
  198. a = mnp.tensordot(x, y)
  199. b = mnp.tensordot(x, y, axes=0)
  200. c = mnp.tensordot(x, y, axes=1)
  201. d = mnp.tensordot(x, y, axes=2)
  202. e = mnp.tensordot(x, y, axes=(3, 0))
  203. f = mnp.tensordot(x, y, axes=[2, 1])
  204. g = mnp.tensordot(x, y, axes=((2, 3), (0, 1)))
  205. h = mnp.tensordot(x, y, axes=[[3, 2], [1, 0]])
  206. return a, b, c, d, e, f, g, h
  207. def onp_tensordot(x, y):
  208. a = onp.tensordot(x, y)
  209. b = onp.tensordot(x, y, axes=0)
  210. c = onp.tensordot(x, y, axes=1)
  211. d = onp.tensordot(x, y, axes=2)
  212. e = onp.tensordot(x, y, axes=(3, 0))
  213. f = onp.tensordot(x, y, axes=[2, 1])
  214. g = onp.tensordot(x, y, axes=((2, 3), (0, 1)))
  215. h = onp.tensordot(x, y, axes=[[3, 2], [1, 0]])
  216. return a, b, c, d, e, f, g, h
  217. @pytest.mark.level1
  218. @pytest.mark.platform_arm_ascend_training
  219. @pytest.mark.platform_x86_ascend_training
  220. @pytest.mark.platform_x86_gpu_training
  221. @pytest.mark.platform_x86_cpu
  222. @pytest.mark.env_onecard
  223. def test_tensordot():
  224. x = rand_int(4, 2, 7, 7)
  225. y = rand_int(7, 7, 6)
  226. run_multi_test(mnp_tensordot, onp_tensordot, (x, y))
  227. def mnp_std(x):
  228. a = mnp.std(x)
  229. b = mnp.std(x, axis=None)
  230. c = mnp.std(x, axis=0)
  231. d = mnp.std(x, axis=1)
  232. e = mnp.std(x, axis=(-1, 1))
  233. f = mnp.std(x, axis=(0, 1, 2))
  234. g = mnp.std(x, axis=None, ddof=1, keepdims=True)
  235. h = mnp.std(x, axis=0, ddof=1, keepdims=True)
  236. i = mnp.std(x, axis=(2), ddof=1, keepdims=True)
  237. return a, b, c, d, e, f, g, h, i
  238. def onp_std(x):
  239. a = onp.std(x)
  240. b = onp.std(x, axis=None)
  241. c = onp.std(x, axis=0)
  242. d = onp.std(x, axis=1)
  243. e = onp.std(x, axis=(-1, 1))
  244. f = onp.std(x, axis=(0, 1, 2))
  245. g = onp.std(x, axis=None, ddof=1, keepdims=True)
  246. h = onp.std(x, axis=0, ddof=1, keepdims=True)
  247. i = onp.std(x, axis=(2), ddof=1, keepdims=True)
  248. return a, b, c, d, e, f, g, h, i
  249. @pytest.mark.level1
  250. @pytest.mark.platform_arm_ascend_training
  251. @pytest.mark.platform_x86_ascend_training
  252. @pytest.mark.platform_x86_gpu_training
  253. @pytest.mark.platform_x86_cpu
  254. @pytest.mark.env_onecard
  255. def test_std():
  256. arr1 = rand_int(2, 3, 4, 5)
  257. arr2 = rand_int(4, 5, 4, 3, 3)
  258. run_single_test(mnp_std, onp_std, arr1, error=1e-5)
  259. run_single_test(mnp_std, onp_std, arr2, error=1e-5)
  260. def mnp_nanstd(x):
  261. a = mnp.nanstd(x)
  262. b = mnp.nanstd(x, axis=None)
  263. c = mnp.nanstd(x, axis=0)
  264. d = mnp.nanstd(x, axis=1)
  265. e = mnp.nanstd(x, axis=(-1, 1))
  266. f = mnp.nanstd(x, axis=(0, 1, 2))
  267. g = mnp.nanstd(x, axis=None, ddof=1, keepdims=True)
  268. h = mnp.nanstd(x, axis=0, ddof=1, keepdims=True)
  269. i = mnp.nanstd(x, axis=(2), ddof=1, keepdims=True)
  270. return a, b, c, d, e, f, g, h, i
  271. def onp_nanstd(x):
  272. a = onp.nanstd(x)
  273. b = onp.nanstd(x, axis=None)
  274. c = onp.nanstd(x, axis=0)
  275. d = onp.nanstd(x, axis=1)
  276. e = onp.nanstd(x, axis=(-1, 1))
  277. f = onp.nanstd(x, axis=(0, 1, 2))
  278. g = onp.nanstd(x, axis=None, ddof=1, keepdims=True)
  279. h = onp.nanstd(x, axis=0, ddof=1, keepdims=True)
  280. i = onp.nanstd(x, axis=(2), ddof=1, keepdims=True)
  281. return a, b, c, d, e, f, g, h, i
  282. @pytest.mark.level1
  283. @pytest.mark.platform_x86_gpu_training
  284. @pytest.mark.platform_x86_cpu
  285. @pytest.mark.env_onecard
  286. def test_nanstd():
  287. arr1 = rand_int(2, 3, 4, 5)
  288. arr1[0][2][1][3] = onp.nan
  289. arr1[1][0][2][4] = onp.nan
  290. arr1[1][1][1][1] = onp.nan
  291. arr2 = rand_int(4, 5, 4, 3, 3)
  292. arr2[3][1][2][1][0] = onp.nan
  293. arr2[1][1][1][1][1] = onp.nan
  294. arr2[0][4][3][0][2] = onp.nan
  295. run_single_test(mnp_nanstd, onp_nanstd, arr1, error=1e-5)
  296. run_single_test(mnp_nanstd, onp_nanstd, arr2, error=1e-5)
  297. match_res(mnp.nanstd, onp.nanstd, rand_int())
  298. def mnp_var(x):
  299. a = mnp.var(x)
  300. b = mnp.var(x, axis=0)
  301. c = mnp.var(x, axis=(0))
  302. d = mnp.var(x, axis=(0, 1, 2))
  303. e = mnp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True)
  304. return a, b, c, d, e
  305. def onp_var(x):
  306. a = onp.var(x)
  307. b = onp.var(x, axis=0)
  308. c = onp.var(x, axis=(0))
  309. d = onp.var(x, axis=(0, 1, 2))
  310. e = onp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True)
  311. return a, b, c, d, e
  312. @pytest.mark.level1
  313. @pytest.mark.platform_arm_ascend_training
  314. @pytest.mark.platform_x86_ascend_training
  315. @pytest.mark.platform_x86_gpu_training
  316. @pytest.mark.platform_x86_cpu
  317. @pytest.mark.env_onecard
  318. def test_var():
  319. arr1 = rand_int(2, 3, 4, 5)
  320. arr2 = rand_int(4, 5, 4, 3, 3)
  321. run_single_test(mnp_var, onp_var, arr1, error=1e-5)
  322. run_single_test(mnp_var, onp_var, arr2, error=1e-5)
  323. def mnp_nanvar(x):
  324. a = mnp.var(x)
  325. b = mnp.var(x, axis=0)
  326. c = mnp.var(x, axis=(0))
  327. d = mnp.var(x, axis=(0, 1, 2))
  328. e = mnp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True)
  329. return a, b, c, d, e
  330. def onp_nanvar(x):
  331. a = onp.var(x)
  332. b = onp.var(x, axis=0)
  333. c = onp.var(x, axis=(0))
  334. d = onp.var(x, axis=(0, 1, 2))
  335. e = onp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True)
  336. return a, b, c, d, e
  337. @pytest.mark.level1
  338. @pytest.mark.platform_x86_gpu_training
  339. @pytest.mark.platform_x86_cpu
  340. @pytest.mark.env_onecard
  341. def test_nanvar():
  342. arr1 = rand_int(2, 3, 4, 5)
  343. arr1[0][2][1][3] = onp.nan
  344. arr1[1][0][2][4] = onp.nan
  345. arr1[1][1][1][1] = onp.nan
  346. arr2 = rand_int(4, 5, 4, 3, 3)
  347. arr2[3][1][2][1][0] = onp.nan
  348. arr2[1][1][1][1][1] = onp.nan
  349. arr2[0][4][3][0][2] = onp.nan
  350. run_single_test(mnp_nanvar, onp_nanvar, arr1, error=1e-5)
  351. run_single_test(mnp_nanvar, onp_nanvar, arr2, error=1e-5)
  352. match_res(mnp.nanvar, onp.nanvar, rand_int())
  353. def mnp_average(x):
  354. a = mnp.average(x)
  355. b = mnp.average(x, axis=None)
  356. c = mnp.average(x, axis=0)
  357. d = mnp.average(x, axis=1)
  358. e = mnp.average(x, axis=(-2, 1))
  359. f = mnp.average(x, axis=(0, 1, 2, 3))
  360. g = mnp.average(x, axis=None, weights=x)
  361. h = mnp.average(x, axis=0, weights=x)
  362. i = mnp.average(x, axis=(1, 2, 3), weights=x)
  363. return a, b, c, d, e, f, g, h, i
  364. def onp_average(x):
  365. a = onp.average(x)
  366. b = onp.average(x, axis=None)
  367. c = onp.average(x, axis=0)
  368. d = onp.average(x, axis=1)
  369. e = onp.average(x, axis=(-2, 1))
  370. f = onp.average(x, axis=(0, 1, 2, 3))
  371. g = onp.average(x, axis=None, weights=x)
  372. h = onp.average(x, axis=0, weights=x)
  373. i = onp.average(x, axis=(1, 2, 3), weights=x)
  374. return a, b, c, d, e, f, g, h, i
  375. @pytest.mark.level0
  376. @pytest.mark.platform_arm_ascend_training
  377. @pytest.mark.platform_x86_ascend_training
  378. @pytest.mark.platform_x86_gpu_training
  379. @pytest.mark.platform_x86_cpu
  380. @pytest.mark.env_onecard
  381. def test_average():
  382. arr1 = rand_int(2, 3, 4, 5)
  383. arr2 = rand_int(4, 5, 1, 3, 1)
  384. run_single_test(mnp_average, onp_average, arr1, error=1e-5)
  385. run_single_test(mnp_average, onp_average, arr2, error=1e-5)
  386. def mnp_count_nonzero(x):
  387. a = mnp.count_nonzero(x)
  388. b = mnp.count_nonzero(x, axis=None)
  389. c = mnp.count_nonzero(x, axis=0)
  390. d = mnp.count_nonzero(x, axis=1)
  391. e = mnp.count_nonzero(x, axis=(-2, 1))
  392. f = mnp.count_nonzero(x, axis=(0, 1, 2, 3))
  393. return a, b, c, d, e, f
  394. def onp_count_nonzero(x):
  395. a = onp.count_nonzero(x)
  396. b = onp.count_nonzero(x, axis=None)
  397. c = onp.count_nonzero(x, axis=0)
  398. d = onp.count_nonzero(x, axis=1)
  399. e = onp.count_nonzero(x, axis=(-2, 1))
  400. f = onp.count_nonzero(x, axis=(0, 1, 2, 3))
  401. return a, b, c, d, e, f
  402. @pytest.mark.level1
  403. @pytest.mark.platform_arm_ascend_training
  404. @pytest.mark.platform_x86_ascend_training
  405. @pytest.mark.platform_x86_gpu_training
  406. @pytest.mark.platform_x86_cpu
  407. @pytest.mark.env_onecard
  408. def test_count_nonzero():
  409. # minus 5 to make some values below zero
  410. arr1 = rand_int(2, 3, 4, 5) - 5
  411. arr2 = rand_int(4, 5, 4, 3, 3) - 5
  412. run_single_test(mnp_count_nonzero, onp_count_nonzero, arr1)
  413. run_single_test(mnp_count_nonzero, onp_count_nonzero, arr2)
  414. def mnp_inner(a, b):
  415. return mnp.inner(a, b)
  416. def onp_inner(a, b):
  417. return onp.inner(a, b)
  418. @pytest.mark.level1
  419. @pytest.mark.platform_arm_ascend_training
  420. @pytest.mark.platform_x86_ascend_training
  421. @pytest.mark.platform_x86_gpu_training
  422. @pytest.mark.platform_x86_cpu
  423. @pytest.mark.env_onecard
  424. def test_inner():
  425. for arr1 in test_case.aligned_arrs:
  426. for arr2 in test_case.aligned_arrs:
  427. match_res(mnp_inner, onp_inner, arr1, arr2)
  428. for scalar1 in test_case.scalars:
  429. for scalar2 in test_case.scalars:
  430. match_res(mnp_inner, onp_inner,
  431. scalar1, scalar2)
  432. def mnp_dot(a, b):
  433. return mnp.dot(a, b)
  434. def onp_dot(a, b):
  435. return onp.dot(a, b)
  436. @pytest.mark.level1
  437. @pytest.mark.platform_arm_ascend_training
  438. @pytest.mark.platform_x86_ascend_training
  439. @pytest.mark.platform_x86_gpu_training
  440. @pytest.mark.platform_x86_cpu
  441. @pytest.mark.env_onecard
  442. def test_dot():
  443. # test case (1D, 1D)
  444. match_res(mnp_dot, onp_dot, rand_int(3), rand_int(3))
  445. # test case (2D, 2D)
  446. match_res(mnp_dot, onp_dot, rand_int(4, 7), rand_int(7, 2))
  447. # test case (0D, _) (_, 0D)
  448. match_res(mnp_dot, onp_dot, rand_int(), rand_int(1, 9, 3))
  449. match_res(mnp_dot, onp_dot, rand_int(8, 5, 6, 3), rand_int())
  450. # test case (ND, 1D)
  451. match_res(mnp_dot, onp_dot, rand_int(2, 4, 5), rand_int(5))
  452. # test case (ND, MD)
  453. match_res(mnp_dot, onp_dot, rand_int(5, 4, 1, 8), rand_int(8, 3))
  454. for i in range(8):
  455. match_res(mnp_dot, onp_dot,
  456. test_case.core_broadcastables[2*i], test_case.core_broadcastables[2*i + 1])
  457. def mnp_outer(a, b):
  458. return mnp.outer(a, b)
  459. def onp_outer(a, b):
  460. return onp.outer(a, b)
  461. @pytest.mark.level1
  462. @pytest.mark.platform_arm_ascend_training
  463. @pytest.mark.platform_x86_ascend_training
  464. @pytest.mark.platform_x86_gpu_training
  465. @pytest.mark.platform_x86_cpu
  466. @pytest.mark.env_onecard
  467. def test_outer():
  468. run_binop_test(mnp_outer, onp_outer, test_case)
  469. @pytest.mark.level1
  470. @pytest.mark.platform_arm_ascend_training
  471. @pytest.mark.platform_x86_ascend_training
  472. @pytest.mark.platform_x86_gpu_training
  473. @pytest.mark.platform_x86_cpu
  474. @pytest.mark.env_onecard
  475. def test_type_promotion():
  476. arr = rand_int(2, 3)
  477. onp_res = onp_add(arr, arr)
  478. a = to_tensor(arr, dtype=mnp.float16)
  479. b = to_tensor(arr, dtype=mnp.float32)
  480. c = to_tensor(arr, dtype=mnp.int32)
  481. match_array(mnp_add(a, b).asnumpy(), onp_res)
  482. match_array(mnp_add(b, c).asnumpy(), onp_res)
  483. def mnp_absolute(x):
  484. return mnp.absolute(x)
  485. def onp_absolute(x):
  486. return onp.absolute(x)
  487. @pytest.mark.level1
  488. @pytest.mark.platform_arm_ascend_training
  489. @pytest.mark.platform_x86_ascend_training
  490. @pytest.mark.platform_x86_gpu_training
  491. @pytest.mark.platform_x86_cpu
  492. @pytest.mark.env_onecard
  493. def test_absolute():
  494. arr = rand_int(2, 3)
  495. a = to_tensor(arr, dtype=mnp.float16)
  496. b = to_tensor(arr, dtype=mnp.float32)
  497. c = to_tensor(arr, dtype=mnp.uint8)
  498. d = to_tensor(arr, dtype=mnp.bool_)
  499. match_array(mnp_absolute(a).asnumpy(), onp_absolute(a.asnumpy()))
  500. match_array(mnp_absolute(b).asnumpy(), onp_absolute(b.asnumpy()))
  501. match_array(mnp_absolute(c).asnumpy(), onp_absolute(c.asnumpy()))
  502. match_array(mnp_absolute(d).asnumpy(), onp_absolute(d.asnumpy()))
  503. @pytest.mark.level1
  504. @pytest.mark.platform_arm_ascend_training
  505. @pytest.mark.platform_x86_ascend_training
  506. @pytest.mark.platform_x86_gpu_training
  507. @pytest.mark.platform_x86_cpu
  508. @pytest.mark.env_onecard
  509. def test_deg2rad_rad2deg():
  510. arrs = [rand_int(2, 3), rand_int(1, 2, 4), rand_int(2, 4)]
  511. for arr in arrs:
  512. match_res(mnp.deg2rad, onp.deg2rad, arr)
  513. match_res(mnp.rad2deg, onp.rad2deg, arr)
  514. def mnp_ptp(x):
  515. a = mnp.ptp(x)
  516. b = mnp.ptp(x, keepdims=True)
  517. c = mnp.ptp(x, axis=(0, 1))
  518. d = mnp.ptp(x, axis=-1)
  519. return a, b, c, d
  520. def onp_ptp(x):
  521. a = onp.ptp(x)
  522. b = onp.ptp(x, keepdims=True)
  523. c = onp.ptp(x, axis=(0, 1))
  524. d = onp.ptp(x, axis=-1)
  525. return a, b, c, d
  526. @pytest.mark.level1
  527. @pytest.mark.platform_arm_ascend_training
  528. @pytest.mark.platform_x86_ascend_training
  529. @pytest.mark.platform_x86_gpu_training
  530. @pytest.mark.platform_x86_cpu
  531. @pytest.mark.env_onecard
  532. def test_ptp():
  533. arrs = [rand_int(2, 3), rand_int(1, 2, 4), rand_int(2, 4)]
  534. for arr in arrs:
  535. match_res(mnp_ptp, onp_ptp, arr)
  536. def mnp_add_dtype(x1, x2):
  537. return mnp.add(x1, x2, dtype=mnp.float32)
  538. def onp_add_dtype(x1, x2):
  539. return onp.add(x1, x2, dtype=onp.float32)
  540. @pytest.mark.level1
  541. @pytest.mark.platform_arm_ascend_training
  542. @pytest.mark.platform_x86_ascend_training
  543. @pytest.mark.platform_x86_gpu_training
  544. @pytest.mark.platform_x86_cpu
  545. @pytest.mark.env_onecard
  546. def test_add_dtype():
  547. x1 = rand_int(2, 3).astype('int32')
  548. x2 = rand_int(2, 3).astype('int32')
  549. arrs = (x1, x2)
  550. mnp_arrs = map(to_tensor, arrs)
  551. mnp_res = mnp_add_dtype(*mnp_arrs)
  552. onp_res = onp_add_dtype(*arrs)
  553. for actual, expected in zip(mnp_res, onp_res):
  554. assert actual.asnumpy().dtype == expected.dtype
  555. def mnp_matmul(x1, x2):
  556. return mnp.matmul(x1, x2)
  557. def onp_matmul(x1, x2):
  558. return onp.matmul(x1, x2)
  559. @pytest.mark.level1
  560. @pytest.mark.platform_arm_ascend_training
  561. @pytest.mark.platform_x86_ascend_training
  562. @pytest.mark.platform_x86_gpu_training
  563. @pytest.mark.platform_x86_cpu
  564. @pytest.mark.env_onecard
  565. def test_matmul():
  566. for scalar1 in test_case.scalars[1:]:
  567. for scalar2 in test_case.scalars[1:]:
  568. match_res(mnp_matmul, onp_matmul,
  569. scalar1, scalar2)
  570. for i in range(8):
  571. match_res(mnp_matmul, onp_matmul,
  572. test_case.core_broadcastables[2*i],
  573. test_case.core_broadcastables[2*i + 1])
  574. def mnp_square(x):
  575. return mnp.square(x)
  576. def onp_square(x):
  577. return onp.square(x)
  578. @pytest.mark.level1
  579. @pytest.mark.platform_arm_ascend_training
  580. @pytest.mark.platform_x86_ascend_training
  581. @pytest.mark.platform_x86_gpu_training
  582. @pytest.mark.platform_x86_cpu
  583. @pytest.mark.env_onecard
  584. def test_square():
  585. run_unary_test(mnp_square, onp_square, test_case)
  586. def mnp_sqrt(x):
  587. return mnp.sqrt(x)
  588. def onp_sqrt(x):
  589. return onp.sqrt(x)
  590. @pytest.mark.level1
  591. @pytest.mark.platform_arm_ascend_training
  592. @pytest.mark.platform_x86_ascend_training
  593. @pytest.mark.platform_x86_gpu_training
  594. @pytest.mark.platform_x86_cpu
  595. @pytest.mark.env_onecard
  596. def test_sqrt():
  597. run_unary_test(mnp_sqrt, onp_sqrt, test_case)
  598. def mnp_reciprocal(x):
  599. return mnp.reciprocal(x)
  600. def onp_reciprocal(x):
  601. return onp.reciprocal(x)
  602. @pytest.mark.level1
  603. @pytest.mark.platform_arm_ascend_training
  604. @pytest.mark.platform_x86_ascend_training
  605. @pytest.mark.platform_x86_gpu_training
  606. @pytest.mark.platform_x86_cpu
  607. @pytest.mark.env_onecard
  608. def test_reciprocal():
  609. run_unary_test(mnp_reciprocal, onp_reciprocal, test_case)
  610. def mnp_log(x):
  611. return mnp.log(x)
  612. def onp_log(x):
  613. return onp.log(x)
  614. @pytest.mark.level1
  615. @pytest.mark.platform_arm_ascend_training
  616. @pytest.mark.platform_x86_ascend_training
  617. @pytest.mark.platform_x86_gpu_training
  618. @pytest.mark.platform_x86_cpu
  619. @pytest.mark.env_onecard
  620. def test_log():
  621. run_unary_test(mnp.log, onp.log, test_case, error=1e-5)
  622. def mnp_log1p(x):
  623. return mnp.log1p(x)
  624. def onp_log1p(x):
  625. return onp.log1p(x)
  626. @pytest.mark.level1
  627. @pytest.mark.platform_arm_ascend_training
  628. @pytest.mark.platform_x86_ascend_training
  629. @pytest.mark.platform_x86_gpu_training
  630. @pytest.mark.platform_x86_cpu
  631. @pytest.mark.env_onecard
  632. def test_log1p():
  633. run_unary_test(mnp_log1p, onp_log1p, test_case, error=1e-5)
  634. def mnp_logaddexp(x1, x2):
  635. return mnp.logaddexp(x1, x2)
  636. def onp_logaddexp(x1, x2):
  637. return onp.logaddexp(x1, x2)
  638. @pytest.mark.level0
  639. @pytest.mark.platform_arm_ascend_training
  640. @pytest.mark.platform_x86_ascend_training
  641. @pytest.mark.platform_x86_gpu_training
  642. @pytest.mark.platform_x86_cpu
  643. @pytest.mark.env_onecard
  644. def test_logaddexp():
  645. test_cases = [
  646. onp.random.randint(1, 5, (5, 6, 3, 2)).astype('float16')]
  647. for _, x1 in enumerate(test_cases):
  648. for _, x2 in enumerate(test_cases):
  649. expected = onp_logaddexp(x1, x2)
  650. actual = mnp_logaddexp(to_tensor(x1), to_tensor(x2))
  651. onp.testing.assert_almost_equal(actual.asnumpy().tolist(), expected.tolist(),
  652. decimal=2)
  653. def mnp_log2(x):
  654. return mnp.log2(x)
  655. def onp_log2(x):
  656. return onp.log2(x)
  657. @pytest.mark.level1
  658. @pytest.mark.platform_arm_ascend_training
  659. @pytest.mark.platform_x86_ascend_training
  660. @pytest.mark.platform_x86_gpu_training
  661. @pytest.mark.platform_x86_cpu
  662. @pytest.mark.env_onecard
  663. def test_log2():
  664. run_unary_test(mnp_log2, onp_log2, test_case, error=1e-5)
  665. def mnp_logaddexp2(x1, x2):
  666. return mnp.logaddexp2(x1, x2)
  667. def onp_logaddexp2(x1, x2):
  668. return onp.logaddexp2(x1, x2)
  669. @pytest.mark.level1
  670. @pytest.mark.platform_arm_ascend_training
  671. @pytest.mark.platform_x86_ascend_training
  672. @pytest.mark.platform_x86_gpu_training
  673. @pytest.mark.platform_x86_cpu
  674. @pytest.mark.env_onecard
  675. def test_logaddexp2():
  676. test_cases = [
  677. onp.random.randint(1, 5, (2)).astype('float16'),
  678. onp.random.randint(1, 5, (3, 2)).astype('float16'),
  679. onp.random.randint(1, 5, (1, 3, 2)).astype('float16'),
  680. onp.random.randint(1, 5, (5, 6, 3, 2)).astype('float16')]
  681. for _, x1 in enumerate(test_cases):
  682. for _, x2 in enumerate(test_cases):
  683. expected = onp_logaddexp2(x1, x2)
  684. actual = mnp_logaddexp2(to_tensor(x1), to_tensor(x2))
  685. onp.testing.assert_almost_equal(actual.asnumpy().tolist(), expected.tolist(),
  686. decimal=2)
  687. def mnp_log10(x):
  688. return mnp.log10(x)
  689. def onp_log10(x):
  690. return onp.log10(x)
  691. @pytest.mark.level1
  692. @pytest.mark.platform_arm_ascend_training
  693. @pytest.mark.platform_x86_ascend_training
  694. @pytest.mark.platform_x86_gpu_training
  695. @pytest.mark.platform_x86_cpu
  696. @pytest.mark.env_onecard
  697. def test_log10():
  698. run_unary_test(mnp_log10, onp_log10, test_case, error=1e-5)
  699. def mnp_maximum(x1, x2):
  700. return mnp.maximum(x1, x2)
  701. def onp_maximum(x1, x2):
  702. return onp.maximum(x1, x2)
  703. @pytest.mark.level0
  704. @pytest.mark.platform_x86_gpu_training
  705. @pytest.mark.platform_x86_cpu
  706. @pytest.mark.env_onecard
  707. def test_maximum():
  708. run_binop_test(mnp_maximum, onp_maximum, test_case)
  709. x = onp.random.randint(-10, 10, 20).astype(onp.float32)
  710. y = onp.random.randint(-10, 10, 20).astype(onp.float32)
  711. x[onp.random.randint(0, 10, 3)] = onp.nan
  712. y[onp.random.randint(0, 10, 3)] = onp.nan
  713. x[onp.random.randint(0, 10, 3)] = onp.NINF
  714. y[onp.random.randint(0, 10, 3)] = onp.NINF
  715. x[onp.random.randint(0, 10, 3)] = onp.PINF
  716. y[onp.random.randint(0, 10, 3)] = onp.PINF
  717. match_res(mnp_maximum, onp_maximum, x, y)
  718. match_res(mnp_maximum, onp_maximum, y, x)
  719. def mnp_clip(x):
  720. a = mnp.clip(x, to_tensor(10.0), to_tensor([2,]))
  721. b = mnp.clip(x, 0, 1)
  722. c = mnp.clip(x, to_tensor(0), to_tensor(10), dtype=mnp.float32)
  723. d = x.clip(to_tensor(10.0), to_tensor([2,]))
  724. e = x.clip(0, 1)
  725. f = x.clip(to_tensor(0), to_tensor(10), dtype=mnp.float32)
  726. return a, b, c, d, e, f
  727. def onp_clip(x):
  728. a = onp.clip(x, onp.asarray(10.0), onp.asarray([2,]))
  729. b = onp.clip(x, 0, 1)
  730. c = onp.clip(x, onp.asarray(0), onp.asarray(10), dtype=onp.float32)
  731. d = x.clip(onp.asarray(10.0), onp.asarray([2,]))
  732. e = x.clip(0, 1)
  733. f = x.clip(onp.asarray(0), onp.asarray(10), dtype=onp.float32)
  734. return a, b, c, d, e, f
  735. @pytest.mark.level0
  736. @pytest.mark.platform_arm_ascend_training
  737. @pytest.mark.platform_x86_ascend_training
  738. @pytest.mark.platform_x86_gpu_training
  739. @pytest.mark.platform_x86_cpu
  740. @pytest.mark.env_onecard
  741. def test_clip():
  742. run_unary_test(mnp_clip, onp_clip, test_case)
  743. def mnp_amax(x, mask):
  744. a = mnp.amax(x)
  745. b = mnp.amax(x, axis=-3)
  746. c = mnp.amax(x, keepdims=True)
  747. d = mnp.amax(x, initial=3)
  748. e = mnp.amax(x, axis=(0, 1), keepdims=True)
  749. f = mnp.amax(x, initial=4, where=mask)
  750. g = mnp.amax(x, initial=5, where=mask, keepdims=True)
  751. h = mnp.amax(x, axis=(1, 2, 3), initial=6, where=mask)
  752. return a, b, c, d, e, f, g, h
  753. def onp_amax(x, mask):
  754. a = onp.amax(x)
  755. b = onp.amax(x, axis=-3)
  756. c = onp.amax(x, keepdims=True)
  757. d = onp.amax(x, initial=3)
  758. e = onp.amax(x, axis=(0, 1), keepdims=True)
  759. f = onp.amax(x, initial=4, where=mask)
  760. g = onp.amax(x, initial=5, where=mask, keepdims=True)
  761. h = onp.amax(x, axis=(1, 2, 3), initial=6, where=mask)
  762. return a, b, c, d, e, f, g, h
  763. @pytest.mark.level1
  764. @pytest.mark.platform_arm_ascend_training
  765. @pytest.mark.platform_x86_ascend_training
  766. @pytest.mark.platform_x86_gpu_training
  767. @pytest.mark.platform_x86_cpu
  768. @pytest.mark.env_onecard
  769. def test_amax():
  770. a = rand_int(2, 3, 4, 5).astype('float32')
  771. mask = rand_bool(2, 3, 4, 5)
  772. run_multi_test(mnp_amax, onp_amax, (a, mask))
  773. match_res(mnp.amax, onp.amax, rand_int())
  774. def mnp_amin(x, mask):
  775. a = mnp.amin(x)
  776. b = mnp.amin(x, axis=-3)
  777. c = mnp.amin(x, keepdims=True)
  778. d = mnp.amin(x, initial=-1)
  779. e = mnp.amin(x, axis=(0, 1), keepdims=True)
  780. f = mnp.amin(x, initial=-2)
  781. g = mnp.amin(x, initial=-3, keepdims=True)
  782. h = mnp.amin(x, axis=(1, 2, 3), initial=-4, where=mask)
  783. return a, b, c, d, e, f, g, h
  784. def onp_amin(x, mask):
  785. a = onp.amin(x)
  786. b = onp.amin(x, axis=-3)
  787. c = onp.amin(x, keepdims=True)
  788. d = onp.amin(x, initial=-1)
  789. e = onp.amin(x, axis=(0, 1), keepdims=True)
  790. f = onp.amin(x, initial=-2)
  791. g = onp.amin(x, initial=-3, keepdims=True)
  792. h = onp.amin(x, axis=(1, 2, 3), initial=-4, where=mask)
  793. return a, b, c, d, e, f, g, h
  794. @pytest.mark.level1
  795. @pytest.mark.platform_arm_ascend_training
  796. @pytest.mark.platform_x86_ascend_training
  797. @pytest.mark.platform_x86_gpu_training
  798. @pytest.mark.platform_x86_cpu
  799. @pytest.mark.env_onecard
  800. def test_amin():
  801. a = rand_int(2, 3, 4, 5).astype('float32')
  802. mask = rand_bool(2, 3, 4, 5)
  803. run_multi_test(mnp_amin, onp_amin, (a, mask))
  804. match_res(mnp.amin, onp.amin, rand_int())
  805. def mnp_hypot(x1, x2):
  806. return mnp.hypot(x1, x2)
  807. def onp_hypot(x1, x2):
  808. return onp.hypot(x1, x2)
  809. @pytest.mark.level1
  810. @pytest.mark.platform_arm_ascend_training
  811. @pytest.mark.platform_x86_ascend_training
  812. @pytest.mark.platform_x86_gpu_training
  813. @pytest.mark.platform_x86_cpu
  814. @pytest.mark.env_onecard
  815. def test_hypot():
  816. run_binop_test(mnp_hypot, onp_hypot, test_case)
  817. def mnp_heaviside(x1, x2):
  818. return mnp.heaviside(x1, x2)
  819. def onp_heaviside(x1, x2):
  820. return onp.heaviside(x1, x2)
  821. @pytest.mark.level1
  822. @pytest.mark.platform_arm_ascend_training
  823. @pytest.mark.platform_x86_ascend_training
  824. @pytest.mark.platform_x86_gpu_training
  825. @pytest.mark.platform_x86_cpu
  826. @pytest.mark.env_onecard
  827. def test_heaviside():
  828. broadcastables = test_case.broadcastables
  829. for b1 in broadcastables:
  830. for b2 in broadcastables:
  831. b = onp.subtract(b1, b2)
  832. match_res(mnp_heaviside, onp_heaviside, b, b1)
  833. match_res(mnp_heaviside, onp_heaviside, b, b2)
  834. def mnp_floor(x):
  835. return mnp.floor(x)
  836. def onp_floor(x):
  837. return onp.floor(x)
  838. @pytest.mark.level1
  839. @pytest.mark.platform_arm_ascend_training
  840. @pytest.mark.platform_x86_ascend_training
  841. @pytest.mark.platform_x86_gpu_training
  842. @pytest.mark.platform_x86_cpu
  843. @pytest.mark.env_onecard
  844. def test_floor():
  845. run_unary_test(mnp_floor, onp_floor, test_case)
  846. x = rand_int(2, 3) * onp.random.rand(2, 3)
  847. match_res(mnp_floor, onp_floor, x)
  848. match_res(mnp_floor, onp_floor, -x)
  849. def mnp_floor_divide(x, y):
  850. return mnp.floor_divide(x, y)
  851. def onp_floor_divde(x, y):
  852. return onp.floor_divide(x, y)
  853. @pytest.mark.level1
  854. @pytest.mark.platform_arm_ascend_training
  855. @pytest.mark.platform_x86_ascend_training
  856. @pytest.mark.platform_x86_gpu_training
  857. @pytest.mark.platform_x86_cpu
  858. @pytest.mark.env_onecard
  859. def test_floor_divide():
  860. run_binop_test(mnp_floor_divide, onp_floor_divde, test_case)
  861. def mnp_remainder(x, y):
  862. return mnp.remainder(x, y)
  863. def onp_remainder(x, y):
  864. return onp.remainder(x, y)
  865. @pytest.mark.level0
  866. @pytest.mark.platform_arm_ascend_training
  867. @pytest.mark.platform_x86_ascend_training
  868. @pytest.mark.platform_x86_gpu_training
  869. @pytest.mark.platform_x86_cpu
  870. @pytest.mark.env_onecard
  871. def test_remainder():
  872. x = rand_int(2, 3)
  873. y = rand_int(2, 3)
  874. match_res(mnp_remainder, onp_remainder, x, y)
  875. def mnp_mod(x, y):
  876. return mnp.mod(x, y)
  877. def onp_mod(x, y):
  878. return onp.mod(x, y)
  879. @pytest.mark.level1
  880. @pytest.mark.platform_arm_ascend_training
  881. @pytest.mark.platform_x86_ascend_training
  882. @pytest.mark.platform_x86_gpu_training
  883. @pytest.mark.platform_x86_cpu
  884. @pytest.mark.env_onecard
  885. def test_mod():
  886. x = rand_int(2, 3)
  887. y = rand_int(2, 3)
  888. match_res(mnp_mod, onp_mod, x, y)
  889. def mnp_fmod(x, y):
  890. return mnp.fmod(x, y)
  891. def onp_fmod(x, y):
  892. return onp.fmod(x, y)
  893. @pytest.mark.level1
  894. @pytest.mark.platform_x86_gpu_training
  895. @pytest.mark.platform_x86_cpu
  896. @pytest.mark.env_onecard
  897. def test_fmod():
  898. x = rand_int(2, 3)
  899. y = rand_int(2, 3)
  900. match_res(mnp_fmod, onp_fmod, x, y)
  901. def mnp_fix(x):
  902. return mnp.fix(x)
  903. def onp_fix(x):
  904. return onp.fix(x)
  905. @pytest.mark.level1
  906. @pytest.mark.platform_arm_ascend_training
  907. @pytest.mark.platform_x86_ascend_training
  908. @pytest.mark.platform_x86_gpu_training
  909. @pytest.mark.platform_x86_cpu
  910. @pytest.mark.env_onecard
  911. def test_fix():
  912. x = rand_int(2, 3)
  913. y = rand_int(2, 3)
  914. floats = onp.divide(onp.subtract(x, y), y)
  915. match_res(mnp_fix, onp_fix, floats, error=1e-5)
  916. def mnp_trunc(x):
  917. return mnp.trunc(x)
  918. def onp_trunc(x):
  919. return onp.trunc(x)
  920. @pytest.mark.level1
  921. @pytest.mark.platform_arm_ascend_training
  922. @pytest.mark.platform_x86_ascend_training
  923. @pytest.mark.platform_x86_gpu_training
  924. @pytest.mark.platform_x86_cpu
  925. @pytest.mark.env_onecard
  926. def test_trunc():
  927. x = rand_int(2, 3)
  928. y = rand_int(2, 3)
  929. floats = onp.divide(onp.subtract(x, y), y)
  930. match_res(mnp_trunc, onp_trunc, floats, error=1e-5)
  931. def mnp_exp(x):
  932. return mnp.exp(x)
  933. def onp_exp(x):
  934. return onp.exp(x)
  935. @pytest.mark.level1
  936. @pytest.mark.platform_arm_ascend_training
  937. @pytest.mark.platform_x86_ascend_training
  938. @pytest.mark.platform_x86_gpu_training
  939. @pytest.mark.platform_x86_cpu
  940. @pytest.mark.env_onecard
  941. def test_exp():
  942. run_unary_test(mnp_exp, onp_exp, test_case, error=5)
  943. def mnp_expm1(x):
  944. return mnp.expm1(x)
  945. def onp_expm1(x):
  946. return onp.expm1(x)
  947. @pytest.mark.level1
  948. @pytest.mark.platform_arm_ascend_training
  949. @pytest.mark.platform_x86_ascend_training
  950. @pytest.mark.platform_x86_gpu_training
  951. @pytest.mark.platform_x86_cpu
  952. @pytest.mark.env_onecard
  953. def test_expm1():
  954. run_unary_test(mnp_expm1, onp_expm1, test_case, error=5)
  955. def mnp_exp2(x):
  956. return mnp.exp2(x)
  957. def onp_exp2(x):
  958. return onp.exp2(x)
  959. @pytest.mark.level1
  960. @pytest.mark.platform_arm_ascend_training
  961. @pytest.mark.platform_x86_ascend_training
  962. @pytest.mark.platform_x86_gpu_training
  963. @pytest.mark.platform_x86_cpu
  964. @pytest.mark.env_onecard
  965. def test_exp2():
  966. run_unary_test(mnp_exp2, onp_exp2, test_case, error=5)
  967. def mnp_kron(x, y):
  968. return mnp.kron(x, y)
  969. def onp_kron(x, y):
  970. return onp.kron(x, y)
  971. @pytest.mark.level0
  972. @pytest.mark.platform_arm_ascend_training
  973. @pytest.mark.platform_x86_ascend_training
  974. @pytest.mark.platform_x86_gpu_training
  975. @pytest.mark.platform_x86_cpu
  976. @pytest.mark.env_onecard
  977. def test_kron():
  978. x = rand_int()
  979. y = rand_int(2, 3, 4)
  980. match_res(mnp.kron, onp.kron, x, y)
  981. x = rand_int(6, 1)
  982. y = rand_int(7, 1, 5)
  983. match_res(mnp.kron, onp.kron, x, y)
  984. x = rand_int(1, 1, 2, 3)
  985. y = rand_int(1, 1, 2, 3)
  986. match_res(mnp.kron, onp.kron, x, y)
  987. @pytest.mark.level0
  988. @pytest.mark.platform_arm_ascend_training
  989. @pytest.mark.platform_x86_ascend_training
  990. @pytest.mark.platform_x86_gpu_training
  991. @pytest.mark.platform_x86_cpu
  992. @pytest.mark.env_onecard
  993. def test_cross():
  994. x = onp.arange(8).reshape(2, 2, 1, 2)
  995. y = onp.arange(4).reshape(1, 2, 2)
  996. match_res(mnp.cross, onp.cross, x, y)
  997. match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2)
  998. match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2, axis=1)
  999. x = onp.arange(18).reshape(2, 3, 1, 3)
  1000. y = onp.arange(9).reshape(1, 3, 3)
  1001. match_res(mnp.cross, onp.cross, x, y)
  1002. match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2)
  1003. match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2, axis=1)
  1004. def mnp_ceil(x):
  1005. return mnp.ceil(x)
  1006. def onp_ceil(x):
  1007. return onp.ceil(x)
  1008. @pytest.mark.platform_arm_ascend_training
  1009. @pytest.mark.platform_x86_ascend_training
  1010. @pytest.mark.platform_x86_gpu_training
  1011. @pytest.mark.platform_x86_cpu
  1012. @pytest.mark.env_onecard
  1013. def test_ceil():
  1014. run_unary_test(mnp_ceil, onp_ceil, test_case)
  1015. def mnp_positive(x):
  1016. return mnp.positive(x)
  1017. def onp_positive(x):
  1018. return onp.positive(x)
  1019. @pytest.mark.level1
  1020. @pytest.mark.platform_arm_ascend_training
  1021. @pytest.mark.platform_x86_ascend_training
  1022. @pytest.mark.platform_x86_gpu_training
  1023. @pytest.mark.platform_x86_cpu
  1024. @pytest.mark.env_onecard
  1025. def test_positive():
  1026. arr = onp.arange(-6, 6).reshape((2, 2, 3)).astype('float32')
  1027. onp_pos = onp_positive(arr)
  1028. mnp_pos = mnp_positive(to_tensor(arr))
  1029. match_array(mnp_pos.asnumpy(), onp_pos)
  1030. def mnp_negative(x):
  1031. return mnp.negative(x)
  1032. def onp_negative(x):
  1033. return onp.negative(x)
  1034. @pytest.mark.level1
  1035. @pytest.mark.platform_arm_ascend_training
  1036. @pytest.mark.platform_x86_ascend_training
  1037. @pytest.mark.platform_x86_gpu_training
  1038. @pytest.mark.platform_x86_cpu
  1039. @pytest.mark.env_onecard
  1040. def test_negative():
  1041. arr = onp.arange(-6, 6).reshape((2, 2, 3)).astype('float32')
  1042. onp_neg = onp_negative(arr)
  1043. mnp_neg = mnp_negative(to_tensor(arr))
  1044. match_array(mnp_neg.asnumpy(), onp_neg, 1e-5)
  1045. @pytest.mark.level1
  1046. @pytest.mark.platform_arm_ascend_training
  1047. @pytest.mark.platform_x86_ascend_training
  1048. @pytest.mark.platform_x86_gpu_training
  1049. @pytest.mark.platform_x86_cpu
  1050. @pytest.mark.env_onecard
  1051. def test_cumsum():
  1052. x = mnp.ones((16, 16), dtype="bool")
  1053. match_array(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy()))
  1054. match_array(mnp.cumsum(x, axis=0).asnumpy(),
  1055. onp.cumsum(x.asnumpy(), axis=0))
  1056. match_meta(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy()))
  1057. x = rand_int(3, 4, 5)
  1058. match_array(mnp.cumsum(to_tensor(x), dtype="bool").asnumpy(),
  1059. onp.cumsum(x, dtype="bool"))
  1060. match_array(mnp.cumsum(to_tensor(x), axis=-1).asnumpy(),
  1061. onp.cumsum(x, axis=-1))
  1062. @pytest.mark.level1
  1063. @pytest.mark.platform_arm_ascend_training
  1064. @pytest.mark.platform_x86_ascend_training
  1065. @pytest.mark.platform_x86_gpu_training
  1066. @pytest.mark.platform_x86_cpu
  1067. @pytest.mark.env_onecard
  1068. def test_promote_types():
  1069. assert mnp.promote_types(mnp.int32, mnp.bool_) == mnp.int32
  1070. assert mnp.promote_types(int, mnp.bool_) == mnp.int32
  1071. assert mnp.promote_types("float32", mnp.int64) == mnp.float32
  1072. assert mnp.promote_types(mnp.int64, mnp.float16) == mnp.float16
  1073. assert mnp.promote_types(int, float) == mnp.float32
  1074. def mnp_diff(input_tensor):
  1075. a = mnp.diff(input_tensor, 2, append=3.0)
  1076. b = mnp.diff(input_tensor, 4, prepend=6, axis=-2)
  1077. c = mnp.diff(input_tensor, 0, append=3.0, axis=-1)
  1078. d = mnp.diff(input_tensor, 1, prepend=input_tensor)
  1079. e = mnp.ediff1d(input_tensor, to_end=input_tensor)
  1080. f = mnp.ediff1d(input_tensor)
  1081. g = mnp.ediff1d(input_tensor, to_begin=3)
  1082. return a, b, c, d, e, f, g
  1083. def onp_diff(input_array):
  1084. a = onp.diff(input_array, 2, append=3.0)
  1085. b = onp.diff(input_array, 4, prepend=6, axis=-2)
  1086. c = onp.diff(input_array, 0, append=3.0, axis=-1)
  1087. d = onp.diff(input_array, 1, prepend=input_array)
  1088. e = onp.ediff1d(input_array, to_end=input_array)
  1089. f = onp.ediff1d(input_array)
  1090. g = onp.ediff1d(input_array, to_begin=3)
  1091. return a, b, c, d, e, f, g
  1092. @pytest.mark.level0
  1093. @pytest.mark.platform_arm_ascend_training
  1094. @pytest.mark.platform_x86_ascend_training
  1095. @pytest.mark.platform_x86_gpu_training
  1096. @pytest.mark.platform_x86_cpu
  1097. @pytest.mark.env_onecard
  1098. def test_diff():
  1099. arr = rand_int(3, 4, 5)
  1100. match_res(mnp_diff, onp_diff, arr)
  1101. arr = rand_int(1, 4, 6, 3)
  1102. match_res(mnp_diff, onp_diff, arr)
  1103. def mnp_sin(x):
  1104. return mnp.sin(x)
  1105. def onp_sin(x):
  1106. return onp.sin(x)
  1107. @pytest.mark.level1
  1108. @pytest.mark.platform_arm_ascend_training
  1109. @pytest.mark.platform_x86_ascend_training
  1110. @pytest.mark.platform_x86_gpu_training
  1111. @pytest.mark.platform_x86_cpu
  1112. @pytest.mark.env_onecard
  1113. def test_sin():
  1114. arr = onp.random.rand(2, 3, 4).astype('float32')
  1115. expect = onp_sin(arr)
  1116. actual = mnp_sin(to_tensor(arr))
  1117. match_array(actual.asnumpy(), expect, error=5)
  1118. def mnp_cos(x):
  1119. return mnp.cos(x)
  1120. def onp_cos(x):
  1121. return onp.cos(x)
  1122. @pytest.mark.level1
  1123. @pytest.mark.platform_arm_ascend_training
  1124. @pytest.mark.platform_x86_ascend_training
  1125. @pytest.mark.platform_x86_gpu_training
  1126. @pytest.mark.platform_x86_cpu
  1127. @pytest.mark.env_onecard
  1128. def test_cos():
  1129. arr = onp.random.rand(2, 3, 4).astype('float32')
  1130. expect = onp_cos(arr)
  1131. actual = mnp_cos(to_tensor(arr))
  1132. match_array(actual.asnumpy(), expect, error=5)
  1133. def mnp_tan(x):
  1134. return mnp.tan(x)
  1135. def onp_tan(x):
  1136. return onp.tan(x)
  1137. @pytest.mark.level1
  1138. @pytest.mark.platform_arm_ascend_training
  1139. @pytest.mark.platform_x86_ascend_training
  1140. @pytest.mark.platform_x86_cpu
  1141. @pytest.mark.env_onecard
  1142. def test_tan():
  1143. arr = onp.array([-0.75, -0.5, 0, 0.5, 0.75]).astype('float32')
  1144. expect = onp_tan(arr)
  1145. actual = mnp_tan(to_tensor(arr))
  1146. match_array(actual.asnumpy(), expect, error=5)
  1147. def mnp_arcsin(x):
  1148. return mnp.arcsin(x)
  1149. def onp_arcsin(x):
  1150. return onp.arcsin(x)
  1151. @pytest.mark.level1
  1152. @pytest.mark.platform_arm_ascend_training
  1153. @pytest.mark.platform_x86_ascend_training
  1154. @pytest.mark.platform_x86_gpu_training
  1155. @pytest.mark.platform_x86_cpu
  1156. @pytest.mark.env_onecard
  1157. def test_arcsin():
  1158. arr = onp.random.uniform(-1, 1, 12).astype('float32')
  1159. onp_asin = onp_arcsin(arr)
  1160. mnp_asin = mnp_arcsin(to_tensor(arr))
  1161. match_array(mnp_asin.asnumpy(), onp_asin, error=3)
  1162. def mnp_arccos(x):
  1163. return mnp.arccos(x)
  1164. def onp_arccos(x):
  1165. return onp.arccos(x)
  1166. @pytest.mark.level1
  1167. @pytest.mark.platform_arm_ascend_training
  1168. @pytest.mark.platform_x86_ascend_training
  1169. @pytest.mark.platform_x86_gpu_training
  1170. @pytest.mark.platform_x86_cpu
  1171. @pytest.mark.env_onecard
  1172. def test_arccos():
  1173. arr = onp.random.uniform(-1, 1, 12).astype('float32')
  1174. onp_acos = onp_arccos(arr)
  1175. mnp_acos = mnp_arccos(to_tensor(arr))
  1176. match_array(mnp_acos.asnumpy(), onp_acos, error=2)
  1177. def mnp_arctan(x):
  1178. return mnp.arctan(x)
  1179. def onp_arctan(x):
  1180. return onp.arctan(x)
  1181. @pytest.mark.level1
  1182. @pytest.mark.platform_arm_ascend_training
  1183. @pytest.mark.platform_x86_ascend_training
  1184. @pytest.mark.platform_x86_gpu_training
  1185. @pytest.mark.platform_x86_cpu
  1186. @pytest.mark.env_onecard
  1187. def test_arctan():
  1188. arr = onp.random.uniform(-1, 1, 12).astype('float32')
  1189. onp_atan = onp_arctan(arr)
  1190. mnp_atan = mnp_arctan(to_tensor(arr))
  1191. match_array(mnp_atan.asnumpy(), onp_atan, error=5)
  1192. def mnp_sinh(x):
  1193. return mnp.sinh(x)
  1194. def onp_sinh(x):
  1195. return onp.sinh(x)
  1196. @pytest.mark.level1
  1197. @pytest.mark.platform_arm_ascend_training
  1198. @pytest.mark.platform_x86_ascend_training
  1199. @pytest.mark.platform_x86_cpu
  1200. @pytest.mark.env_onecard
  1201. def test_sinh():
  1202. arr = onp.random.rand(2, 3, 4).astype('float32')
  1203. expect = onp_sinh(arr)
  1204. actual = mnp_sinh(to_tensor(arr))
  1205. match_array(actual.asnumpy(), expect, error=5)
  1206. def mnp_cosh(x):
  1207. return mnp.cosh(x)
  1208. def onp_cosh(x):
  1209. return onp.cosh(x)
  1210. @pytest.mark.level1
  1211. @pytest.mark.platform_arm_ascend_training
  1212. @pytest.mark.platform_x86_ascend_training
  1213. @pytest.mark.platform_x86_cpu
  1214. @pytest.mark.env_onecard
  1215. def test_cosh():
  1216. arr = onp.random.rand(2, 3, 4).astype('float32')
  1217. expect = onp_cosh(arr)
  1218. actual = mnp_cosh(to_tensor(arr))
  1219. match_array(actual.asnumpy(), expect, error=5)
  1220. def mnp_tanh(x):
  1221. return mnp.tanh(x)
  1222. def onp_tanh(x):
  1223. return onp.tanh(x)
  1224. @pytest.mark.level1
  1225. @pytest.mark.platform_arm_ascend_training
  1226. @pytest.mark.platform_x86_ascend_training
  1227. @pytest.mark.platform_x86_gpu_training
  1228. @pytest.mark.platform_x86_cpu
  1229. @pytest.mark.env_onecard
  1230. def test_tanh():
  1231. arr = onp.random.rand(2, 3, 4).astype('float32')
  1232. expect = onp_tanh(arr)
  1233. actual = mnp_tanh(to_tensor(arr))
  1234. match_array(actual.asnumpy(), expect, error=5)
  1235. def mnp_arcsinh(x):
  1236. return mnp.arcsinh(x)
  1237. def onp_arcsinh(x):
  1238. return onp.arcsinh(x)
  1239. @pytest.mark.level1
  1240. @pytest.mark.platform_arm_ascend_training
  1241. @pytest.mark.platform_x86_ascend_training
  1242. @pytest.mark.platform_x86_gpu_training
  1243. @pytest.mark.platform_x86_cpu
  1244. @pytest.mark.env_onecard
  1245. def test_arcsinh():
  1246. arr = onp.random.rand(2, 3, 4).astype('float32')
  1247. expect = onp_arcsinh(arr)
  1248. actual = mnp_arcsinh(to_tensor(arr))
  1249. match_array(actual.asnumpy(), expect, error=5)
  1250. def mnp_arccosh(x):
  1251. return mnp.arccosh(x)
  1252. def onp_arccosh(x):
  1253. return onp.arccosh(x)
  1254. @pytest.mark.level1
  1255. @pytest.mark.platform_arm_ascend_training
  1256. @pytest.mark.platform_x86_ascend_training
  1257. @pytest.mark.platform_x86_gpu_training
  1258. @pytest.mark.platform_x86_cpu
  1259. @pytest.mark.env_onecard
  1260. def test_arccosh():
  1261. arr = onp.random.randint(1, 100, size=(2, 3)).astype('float32')
  1262. expect = onp_arccosh(arr)
  1263. actual = mnp_arccosh(to_tensor(arr))
  1264. match_array(actual.asnumpy(), expect, error=5)
  1265. def mnp_arctanh(x):
  1266. return mnp.arctanh(x)
  1267. def onp_arctanh(x):
  1268. return onp.arctanh(x)
  1269. @pytest.mark.level1
  1270. @pytest.mark.platform_arm_ascend_training
  1271. @pytest.mark.platform_x86_ascend_training
  1272. @pytest.mark.platform_x86_cpu
  1273. @pytest.mark.env_onecard
  1274. def test_arctanh():
  1275. arr = onp.random.uniform(-0.9, 1, 10).astype('float32')
  1276. expect = onp_arctanh(arr)
  1277. actual = mnp_arctanh(to_tensor(arr))
  1278. match_array(actual.asnumpy(), expect, error=5)
  1279. def mnp_arctan2(x, y):
  1280. return mnp.arctan2(x, y)
  1281. def onp_arctan2(x, y):
  1282. return onp.arctan2(x, y)
  1283. @pytest.mark.level1
  1284. @pytest.mark.platform_arm_ascend_training
  1285. @pytest.mark.platform_x86_ascend_training
  1286. @pytest.mark.platform_x86_cpu
  1287. @pytest.mark.env_onecard
  1288. def test_arctan2():
  1289. run_binop_test(mnp_arctan2, onp_arctan2, test_case, error=5)
  1290. def mnp_convolve(mode):
  1291. a = mnp.convolve([1, 2, 3, 4, 5], 2, mode=mode)
  1292. b = mnp.convolve([1, 2, 3, 4, 5], [2, 3], mode=mode)
  1293. c = mnp.convolve([1, 2], [2, 5, 10], mode=mode)
  1294. d = mnp.convolve(mnp.array([1, 2, 3, 4, 5]), mnp.array([1, 2, 3, 4, 5]), mode=mode)
  1295. e = mnp.convolve([1, 2, 3, 4, 5], 2, mode=mode)
  1296. return a, b, c, d, e
  1297. def onp_convolve(mode):
  1298. a = onp.convolve([1, 2, 3, 4, 5], 2, mode=mode)
  1299. b = onp.convolve([1, 2, 3, 4, 5], [2, 3], mode=mode)
  1300. c = onp.convolve([1, 2], [2, 5, 10], mode=mode)
  1301. d = onp.convolve(onp.array([1, 2, 3, 4, 5]), onp.array([1, 2, 3, 4, 5]), mode=mode)
  1302. e = onp.convolve([1, 2, 3, 4, 5], 2, mode=mode)
  1303. return a, b, c, d, e
  1304. @pytest.mark.level0
  1305. @pytest.mark.platform_x86_gpu_training
  1306. @pytest.mark.env_onecard
  1307. def test_convolve():
  1308. for mode in ['full', 'same', 'valid']:
  1309. mnp_res = mnp_convolve(mode)
  1310. onp_res = onp_convolve(mode)
  1311. match_all_arrays(mnp_res, onp_res)
  1312. @pytest.mark.level0
  1313. @pytest.mark.platform_arm_ascend_training
  1314. @pytest.mark.platform_x86_ascend_training
  1315. @pytest.mark.platform_x86_gpu_training
  1316. @pytest.mark.platform_x86_cpu
  1317. @pytest.mark.env_onecard
  1318. def test_cov():
  1319. x = onp.random.random((3, 4)).tolist()
  1320. w1 = [0, 1, 2, 3]
  1321. w2 = [4, 5, 6, 7]
  1322. mnp_res = mnp.cov(x, fweights=w1, aweights=w2, ddof=3)
  1323. onp_res = onp.cov(x, fweights=w1, aweights=w2, ddof=3)
  1324. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1325. mnp_res = mnp.cov(x, fweights=w1, aweights=w2, bias=True)
  1326. onp_res = onp.cov(x, fweights=w1, aweights=w2, bias=True)
  1327. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1328. mnp_res = mnp.cov(x, fweights=w1[0:3], aweights=w2[0:3], rowvar=False, bias=True)
  1329. onp_res = onp.cov(x, fweights=w1[0:3], aweights=w2[0:3], rowvar=False, bias=True)
  1330. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1331. @pytest.mark.level0
  1332. @pytest.mark.platform_arm_ascend_training
  1333. @pytest.mark.platform_x86_ascend_training
  1334. @pytest.mark.platform_x86_gpu_training
  1335. @pytest.mark.platform_x86_cpu
  1336. @pytest.mark.env_onecard
  1337. def test_trapz():
  1338. y = rand_int(2, 3, 4, 5)
  1339. match_res(mnp.trapz, onp.trapz, y)
  1340. match_res(mnp.trapz, onp.trapz, y, x=[-5, -3, 0, 7, 10])
  1341. match_res(mnp.trapz, onp.trapz, y, dx=2, axis=3)
  1342. match_res(mnp.trapz, onp.trapz, y, x=[1, 5, 6, 9], dx=3, axis=-2)
  1343. def mnp_gcd(x, y):
  1344. return mnp.gcd(x, y)
  1345. def onp_gcd(x, y):
  1346. return onp.gcd(x, y)
  1347. @pytest.mark.level1
  1348. @pytest.mark.platform_arm_ascend_training
  1349. @pytest.mark.platform_x86_ascend_training
  1350. @pytest.mark.platform_x86_gpu_training
  1351. @pytest.mark.platform_x86_cpu
  1352. @pytest.mark.env_onecard
  1353. def test_gcd():
  1354. x = onp.arange(-12, 12).reshape(2, 3, 4)
  1355. y = onp.arange(24).reshape(2, 3, 4)
  1356. match_res(mnp_gcd, onp_gcd, x, y)
  1357. def mnp_lcm(x, y):
  1358. return mnp.lcm(x, y)
  1359. def onp_lcm(x, y):
  1360. return onp.lcm(x, y)
  1361. @pytest.mark.level0
  1362. @pytest.mark.platform_arm_ascend_training
  1363. @pytest.mark.platform_x86_ascend_training
  1364. @pytest.mark.platform_x86_gpu_training
  1365. @pytest.mark.platform_x86_cpu
  1366. @pytest.mark.env_onecard
  1367. def test_lcm():
  1368. x = onp.arange(-12, 12).reshape(2, 3, 4)
  1369. y = onp.arange(24).reshape(2, 3, 4)
  1370. match_res(mnp_lcm, onp_lcm, x, y)
  1371. @pytest.mark.level1
  1372. @pytest.mark.platform_arm_ascend_training
  1373. @pytest.mark.platform_x86_ascend_training
  1374. @pytest.mark.platform_x86_gpu_training
  1375. @pytest.mark.platform_x86_cpu
  1376. @pytest.mark.env_onecard
  1377. def test_exception_innner():
  1378. with pytest.raises(ValueError):
  1379. mnp.inner(to_tensor(test_case.arrs[0]),
  1380. to_tensor(test_case.arrs[1]))
  1381. @pytest.mark.level1
  1382. @pytest.mark.platform_arm_ascend_training
  1383. @pytest.mark.platform_x86_ascend_training
  1384. @pytest.mark.platform_x86_gpu_training
  1385. @pytest.mark.platform_x86_cpu
  1386. @pytest.mark.env_onecard
  1387. def test_exception_add():
  1388. with pytest.raises(ValueError):
  1389. mnp.add(to_tensor(test_case.arrs[1]), to_tensor(test_case.arrs[2]))
  1390. def mnp_nanmax(x):
  1391. a = mnp.nanmax(x)
  1392. b = mnp.nanmax(x, keepdims=True)
  1393. c = mnp.nanmax(x, axis=-2)
  1394. d = mnp.nanmax(x, axis=0, keepdims=True)
  1395. e = mnp.nanmax(x, axis=(-2, 3))
  1396. f = mnp.nanmax(x, axis=(-3, -1), keepdims=True)
  1397. return a, b, c, d, e, f
  1398. def onp_nanmax(x):
  1399. a = onp.nanmax(x)
  1400. b = onp.nanmax(x, keepdims=True)
  1401. c = onp.nanmax(x, axis=-2)
  1402. d = onp.nanmax(x, axis=0, keepdims=True)
  1403. e = onp.nanmax(x, axis=(-2, 3))
  1404. f = onp.nanmax(x, axis=(-3, -1), keepdims=True)
  1405. return a, b, c, d, e, f
  1406. @pytest.mark.level1
  1407. @pytest.mark.platform_x86_gpu_training
  1408. @pytest.mark.platform_x86_cpu
  1409. @pytest.mark.env_onecard
  1410. def test_nanmax():
  1411. x = rand_int(2, 3, 4, 5)
  1412. x[0][2][1][3] = onp.nan
  1413. x[1][0][2][4] = onp.nan
  1414. x[1][1][1][1] = onp.nan
  1415. run_multi_test(mnp_nanmax, onp_nanmax, (x,))
  1416. def mnp_nanmin(x):
  1417. a = mnp.nanmin(x)
  1418. b = mnp.nanmin(x, keepdims=True)
  1419. c = mnp.nanmin(x, axis=-2)
  1420. d = mnp.nanmin(x, axis=0, keepdims=True)
  1421. e = mnp.nanmin(x, axis=(-2, 3))
  1422. f = mnp.nanmin(x, axis=(-3, -1), keepdims=True)
  1423. return a, b, c, d, e, f
  1424. def onp_nanmin(x):
  1425. a = onp.nanmin(x)
  1426. b = onp.nanmin(x, keepdims=True)
  1427. c = onp.nanmin(x, axis=-2)
  1428. d = onp.nanmin(x, axis=0, keepdims=True)
  1429. e = onp.nanmin(x, axis=(-2, 3))
  1430. f = onp.nanmin(x, axis=(-3, -1), keepdims=True)
  1431. return a, b, c, d, e, f
  1432. @pytest.mark.level1
  1433. @pytest.mark.platform_x86_gpu_training
  1434. @pytest.mark.platform_x86_cpu
  1435. @pytest.mark.env_onecard
  1436. def test_nanmin():
  1437. x = rand_int(2, 3, 4, 5)
  1438. x[0][2][1][3] = onp.nan
  1439. x[1][0][2][4] = onp.nan
  1440. x[1][1][1][1] = onp.nan
  1441. run_multi_test(mnp_nanmin, onp_nanmin, (x,))
  1442. def mnp_nansum(x):
  1443. a = mnp.nansum(x)
  1444. b = mnp.nansum(x, keepdims=True)
  1445. c = mnp.nansum(x, axis=-2)
  1446. d = mnp.nansum(x, axis=0, keepdims=True)
  1447. e = mnp.nansum(x, axis=(-2, 3))
  1448. f = mnp.nansum(x, axis=(-3, -1), keepdims=True)
  1449. return a, b, c, d, e, f
  1450. def onp_nansum(x):
  1451. a = onp.nansum(x)
  1452. b = onp.nansum(x, keepdims=True)
  1453. c = onp.nansum(x, axis=-2)
  1454. d = onp.nansum(x, axis=0, keepdims=True)
  1455. e = onp.nansum(x, axis=(-2, 3))
  1456. f = onp.nansum(x, axis=(-3, -1), keepdims=True)
  1457. return a, b, c, d, e, f
  1458. @pytest.mark.level1
  1459. @pytest.mark.platform_x86_gpu_training
  1460. @pytest.mark.platform_x86_cpu
  1461. @pytest.mark.env_onecard
  1462. def test_nansum():
  1463. x = rand_int(2, 3, 4, 5)
  1464. x[0][2][1][3] = onp.nan
  1465. x[1][0][2][4] = onp.nan
  1466. x[1][1][1][1] = onp.nan
  1467. run_multi_test(mnp_nansum, onp_nansum, (x,))
  1468. match_res(mnp.nansum, onp.nansum, rand_int())
  1469. def mnp_nanmean(x):
  1470. a = mnp.nanmean(x, keepdims=True)
  1471. b = mnp.nanmean(x, axis=(-2, 3))
  1472. c = mnp.nanmean(x, axis=(-3, -1), keepdims=True)
  1473. return a, b, c
  1474. def onp_nanmean(x):
  1475. a = onp.nanmean(x, keepdims=True)
  1476. b = onp.nanmean(x, axis=(-2, 3))
  1477. c = onp.nanmean(x, axis=(-3, -1), keepdims=True)
  1478. return a, b, c
  1479. @pytest.mark.level0
  1480. @pytest.mark.platform_x86_gpu_training
  1481. @pytest.mark.platform_x86_cpu
  1482. @pytest.mark.env_onecard
  1483. def test_nanmean():
  1484. x = rand_int(2, 3, 4, 5)
  1485. x[0][2][1][3] = onp.nan
  1486. x[1][0][2][4] = onp.nan
  1487. x[1][1][1][1] = onp.nan
  1488. run_multi_test(mnp_nanmean, onp_nanmean, (x,))
  1489. match_res(mnp.nanmean, onp.nanmean, rand_int())
  1490. def mnp_mean(*arrs):
  1491. arr1 = arrs[0]
  1492. arr2 = arrs[1]
  1493. arr3 = arrs[2]
  1494. a = mnp.mean(arr1)
  1495. b = mnp.mean(arr2, keepdims=True)
  1496. c = mnp.mean(arr3, keepdims=False)
  1497. d = mnp.mean(arr2, axis=0, keepdims=True)
  1498. e = mnp.mean(arr3, axis=(0, -1))
  1499. f = mnp.mean(arr3, axis=-1, keepdims=True)
  1500. return a, b, c, d, e, f
  1501. def onp_mean(*arrs):
  1502. arr1 = arrs[0]
  1503. arr2 = arrs[1]
  1504. arr3 = arrs[2]
  1505. a = onp.mean(arr1)
  1506. b = onp.mean(arr2, keepdims=True)
  1507. c = onp.mean(arr3, keepdims=False)
  1508. d = onp.mean(arr2, axis=0, keepdims=True)
  1509. e = onp.mean(arr3, axis=(0, -1))
  1510. f = onp.mean(arr3, axis=-1, keepdims=True)
  1511. return a, b, c, d, e, f
  1512. @pytest.mark.level1
  1513. @pytest.mark.platform_arm_ascend_training
  1514. @pytest.mark.platform_x86_ascend_training
  1515. @pytest.mark.platform_x86_gpu_training
  1516. @pytest.mark.platform_x86_cpu
  1517. @pytest.mark.env_onecard
  1518. def test_mean():
  1519. run_multi_test(mnp_mean, onp_mean, test_case.arrs, error=3)
  1520. run_multi_test(mnp_mean, onp_mean, test_case.expanded_arrs, error=3)
  1521. run_multi_test(mnp_mean, onp_mean, test_case.scalars, error=3)
  1522. @pytest.mark.level0
  1523. @pytest.mark.platform_arm_ascend_training
  1524. @pytest.mark.platform_x86_ascend_training
  1525. @pytest.mark.platform_x86_gpu_training
  1526. @pytest.mark.platform_x86_cpu
  1527. @pytest.mark.env_onecard
  1528. def test_corrcoef():
  1529. x = onp.random.random((3, 4)).tolist()
  1530. mnp_res = mnp.corrcoef(x)
  1531. onp_res = onp.corrcoef(x)
  1532. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1533. mnp_res = mnp.corrcoef(x, rowvar=False)
  1534. onp_res = onp.corrcoef(x, rowvar=False)
  1535. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1536. @pytest.mark.level1
  1537. @pytest.mark.platform_arm_ascend_training
  1538. @pytest.mark.platform_x86_ascend_training
  1539. @pytest.mark.platform_x86_gpu_training
  1540. @pytest.mark.platform_x86_cpu
  1541. @pytest.mark.env_onecard
  1542. def test_multi_dot():
  1543. arrays = [rand_int(3), rand_int(3, 5), rand_int(5, 2), rand_int(2, 7), rand_int(7)]
  1544. mnp_arrays = [to_tensor(arr) for arr in arrays]
  1545. match_all_arrays(mnp.multi_dot(mnp_arrays), onp.linalg.multi_dot(arrays))
  1546. match_all_arrays(mnp.multi_dot(mnp_arrays[1:]), onp.linalg.multi_dot(arrays[1:]))
  1547. match_all_arrays(mnp.multi_dot(mnp_arrays[:-1]), onp.linalg.multi_dot(arrays[:-1]))
  1548. match_all_arrays(mnp.multi_dot(mnp_arrays[1:-1]), onp.linalg.multi_dot(arrays[1:-1]))
  1549. @pytest.mark.level0
  1550. @pytest.mark.platform_arm_ascend_training
  1551. @pytest.mark.platform_x86_ascend_training
  1552. @pytest.mark.platform_x86_gpu_training
  1553. @pytest.mark.platform_x86_cpu
  1554. @pytest.mark.env_onecard
  1555. def test_gradient():
  1556. f = onp.random.random((3, 4, 5)).tolist()
  1557. mnp_res = mnp.gradient(f, -3, axis=(-1, 1))
  1558. onp_res = onp.gradient(f, -3, axis=(-1, 1))
  1559. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1560. mnp_res = mnp.gradient(f, -3, 5, axis=(-1, 0))
  1561. onp_res = onp.gradient(f, -3, 5, axis=(-1, 0))
  1562. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1563. @pytest.mark.level1
  1564. @pytest.mark.platform_arm_ascend_training
  1565. @pytest.mark.platform_x86_ascend_training
  1566. @pytest.mark.platform_x86_gpu_training
  1567. @pytest.mark.platform_x86_cpu
  1568. @pytest.mark.env_onecard
  1569. def test_argmax():
  1570. match_res(mnp.argmax, onp.argmax, rand_int())
  1571. match_res(mnp.argmax, onp.argmax, rand_int(3))
  1572. match_res(mnp.argmax, onp.argmax, rand_int(1, 1, 1))
  1573. x = onp.random.choice(onp.arange(-100, 100), size=(2, 3, 4, 5), replace=False)
  1574. match_res(mnp.argmax, onp.argmax, x)
  1575. for i in range(-4, 4):
  1576. match_res(mnp.argmax, onp.argmax, x, axis=i)
  1577. @pytest.mark.level1
  1578. @pytest.mark.platform_arm_ascend_training
  1579. @pytest.mark.platform_x86_ascend_training
  1580. @pytest.mark.platform_x86_gpu_training
  1581. @pytest.mark.platform_x86_cpu
  1582. @pytest.mark.env_onecard
  1583. def test_argmin():
  1584. match_res(mnp.argmin, onp.argmin, rand_int())
  1585. match_res(mnp.argmin, onp.argmin, rand_int(3))
  1586. match_res(mnp.argmin, onp.argmin, rand_int(1, 1, 1))
  1587. x = rand_int(2, 3, 4, 5)
  1588. match_res(mnp.argmin, onp.argmin, x)
  1589. for i in range(-4, 4):
  1590. match_res(mnp.argmin, onp.argmin, x, axis=i)
  1591. @pytest.mark.level0
  1592. @pytest.mark.platform_arm_ascend_training
  1593. @pytest.mark.platform_x86_ascend_training
  1594. @pytest.mark.platform_x86_gpu_training
  1595. @pytest.mark.platform_x86_cpu
  1596. @pytest.mark.env_onecard
  1597. def test_searchsorted():
  1598. x = onp.arange(-10, 10)
  1599. y = onp.random.randint(-15, 15, size=(2, 3, 4)) + onp.random.choice([0, 0.5], (2, 3, 4))
  1600. sorter = onp.random.shuffle(onp.arange(20))
  1601. match_res(mnp.searchsorted, onp.searchsorted, x, y, side='right', sorter=sorter)
  1602. @pytest.mark.level2
  1603. @pytest.mark.platform_arm_ascend_training
  1604. @pytest.mark.platform_x86_ascend_training
  1605. @pytest.mark.platform_x86_gpu_training
  1606. @pytest.mark.platform_x86_cpu
  1607. @pytest.mark.env_onecard
  1608. def test_interp():
  1609. x = onp.random.randint(-15, 15, size=(2, 3, 4)) + onp.random.choice([0, 0.5], (2, 3, 4))
  1610. xp = onp.arange(-10, 10)
  1611. fp = onp.random.uniform(-50, 50, 20)
  1612. match_res(mnp.interp, onp.interp, x, xp, fp, error=3)
  1613. match_res(mnp.interp, onp.interp, x, xp, fp, left=onp.random.rand(), error=3)
  1614. match_res(mnp.interp, onp.interp, x, xp, fp, right=onp.random.rand(), error=3)
  1615. match_res(mnp.interp, onp.interp, x, xp, fp, left=onp.random.rand(), right=onp.random.rand(), error=3)
  1616. @pytest.mark.level2
  1617. @pytest.mark.platform_arm_ascend_training
  1618. @pytest.mark.platform_x86_ascend_training
  1619. @pytest.mark.platform_x86_gpu_training
  1620. @pytest.mark.platform_x86_cpu
  1621. @pytest.mark.env_onecard
  1622. def test_digitize():
  1623. bins = onp.random.randint(-10, 10, size=10)
  1624. bins.sort()
  1625. x = onp.random.randint(-15, 15, size=(2, 3, 4)) + onp.random.choice([0, 0.5], (2, 3, 4))
  1626. match_res(mnp.digitize, onp.digitize, x, [])
  1627. match_res(mnp.digitize, onp.digitize, [], [])
  1628. match_res(mnp.digitize, onp.digitize, [], bins)
  1629. match_res(mnp.digitize, onp.digitize, x, bins)
  1630. match_res(mnp.digitize, onp.digitize, x, bins, right=True)
  1631. bins = onp.flip(bins)
  1632. match_res(mnp.digitize, onp.digitize, x, bins)
  1633. match_res(mnp.digitize, onp.digitize, x, bins, right=True)
  1634. @pytest.mark.level1
  1635. @pytest.mark.platform_arm_ascend_training
  1636. @pytest.mark.platform_x86_ascend_training
  1637. @pytest.mark.platform_x86_gpu_training
  1638. @pytest.mark.platform_x86_cpu
  1639. @pytest.mark.env_onecard
  1640. def test_bincount():
  1641. x = onp.random.randint(0, 10, 20)
  1642. weights = onp.random.randn(20)
  1643. match_res(mnp.bincount, onp.bincount, x, dtype=mnp.int32)
  1644. match_res(mnp.bincount, onp.bincount, x, minlength=25, dtype=mnp.int32)
  1645. match_all_arrays(mnp.bincount(to_tensor(x), to_tensor(weights)),
  1646. onp.bincount(x, weights), error=3)
  1647. match_all_arrays(mnp.bincount(to_tensor(x), to_tensor(weights), minlength=25),
  1648. onp.bincount(x, weights, minlength=25), error=3)
  1649. @pytest.mark.level0
  1650. @pytest.mark.platform_arm_ascend_training
  1651. @pytest.mark.platform_x86_ascend_training
  1652. @pytest.mark.platform_x86_gpu_training
  1653. @pytest.mark.platform_x86_cpu
  1654. @pytest.mark.env_onecard
  1655. def test_histogram():
  1656. x = onp.random.randint(-10, 10, 10)
  1657. weights = onp.random.randn(10)
  1658. for bins in [(1, 2, 3), [2], 1, 5]:
  1659. # pylint: disable=redefined-builtin
  1660. for range in [None, (2, 20)]:
  1661. match_res(mnp.histogram, onp.histogram, x, bins=bins, range=range, error=1)
  1662. match_res(mnp.histogram, onp.histogram, x, bins=bins, range=range, density=True, error=1)
  1663. mnp_res = mnp.histogram(to_tensor(x), bins=bins, range=range,
  1664. weights=to_tensor(weights), density=True)
  1665. onp_res = onp.histogram(x, bins=bins, range=range, weights=weights, density=True)
  1666. match_all_arrays(mnp_res, onp_res, error=1)
  1667. @pytest.mark.level0
  1668. @pytest.mark.platform_arm_ascend_training
  1669. @pytest.mark.platform_x86_ascend_training
  1670. @pytest.mark.platform_x86_gpu_training
  1671. @pytest.mark.platform_x86_cpu
  1672. @pytest.mark.env_onecard
  1673. def test_histogramdd():
  1674. x = onp.random.randint(-10, 10, (5, 3))
  1675. y = [onp.random.randint(-10, 10, 5), onp.random.randint(-10, 10, 5), onp.random.randint(-10, 10, 5)]
  1676. mnp_y = list(map(to_tensor, y))
  1677. weights = onp.random.randn(5)
  1678. for bins in [(15, 4, 9)]:
  1679. # pylint: disable=redefined-builtin
  1680. for range in [[[0, 5], [2, 7], [1, 3]]]:
  1681. mnp_res = mnp.histogramdd(to_tensor(x), bins=bins, range=range)
  1682. onp_res = onp.histogramdd(x, bins=bins, range=range)
  1683. match_all_arrays(mnp_res[0], onp_res[0], error=1)
  1684. match_all_arrays(mnp_res[1], onp_res[1], error=1)
  1685. mnp_res = mnp.histogramdd(mnp_y, bins=bins, range=range, weights=to_tensor(weights),
  1686. density=True)
  1687. onp_res = onp.histogramdd(y, bins, range=range, weights=weights, density=True)
  1688. match_all_arrays(mnp_res[0], onp_res[0], error=1)
  1689. match_all_arrays(mnp_res[1], onp_res[1], error=1)
  1690. bins = onp.arange(24).reshape(3, 8)
  1691. mnp_res = mnp.histogramdd(to_tensor(x), bins=to_tensor(bins))
  1692. onp_res = onp.histogramdd(x, bins=bins)
  1693. match_all_arrays(mnp_res[0], onp_res[0], error=1)
  1694. match_all_arrays(mnp_res[1], onp_res[1], error=1)
  1695. @pytest.mark.level1
  1696. @pytest.mark.platform_arm_ascend_training
  1697. @pytest.mark.platform_x86_ascend_training
  1698. @pytest.mark.platform_x86_gpu_training
  1699. @pytest.mark.platform_x86_cpu
  1700. @pytest.mark.env_onecard
  1701. def test_histogram2d():
  1702. x = onp.random.randint(-10, 10, 10)
  1703. y = onp.random.randint(-10, 10, 10)
  1704. weights = onp.random.randn(10)
  1705. for bins in [4, [8, [1, 2, 3]]]:
  1706. # pylint: disable=redefined-builtin
  1707. for range in [None, [(3, 3), (2, 20)]]:
  1708. match_res(mnp.histogram2d, onp.histogram2d, x, y, bins=bins, range=range, error=1)
  1709. mnp_res = mnp.histogram2d(to_tensor(x), to_tensor(y), bins=bins, range=range,
  1710. weights=to_tensor(weights), density=True)
  1711. onp_res = onp.histogram2d(x, y, bins=bins, range=range, weights=weights, density=True)
  1712. match_all_arrays(mnp_res, onp_res, error=1)
  1713. @pytest.mark.level1
  1714. @pytest.mark.platform_arm_ascend_training
  1715. @pytest.mark.platform_x86_ascend_training
  1716. @pytest.mark.platform_x86_gpu_training
  1717. @pytest.mark.platform_x86_cpu
  1718. @pytest.mark.env_onecard
  1719. def test_exception_mean():
  1720. with pytest.raises(ValueError):
  1721. mnp.mean(to_tensor(test_case.arrs[0]), (-1, 0))
  1722. def mnp_sum(x):
  1723. a = mnp.sum(x)
  1724. b = mnp.sum(x, axis=0)
  1725. c = mnp.sum(x, axis=(0, 1))
  1726. d = mnp.sum(x, keepdims=True)
  1727. e = mnp.sum(x, initial=-1)
  1728. f = mnp.sum(x, initial=1)
  1729. g = mnp.sum(x, axis=(0, 2, -2), keepdims=True, initial=0.5, dtype=mnp.float64)
  1730. return a, b, c, d, e, f, g
  1731. def onp_sum(x):
  1732. a = onp.sum(x)
  1733. b = onp.sum(x, axis=0)
  1734. c = onp.sum(x, axis=(0, 1))
  1735. d = onp.sum(x, keepdims=True)
  1736. e = onp.sum(x, initial=-1)
  1737. f = onp.sum(x, initial=1)
  1738. g = onp.sum(x, axis=(0, 2, -2), keepdims=True, initial=0.5, dtype=onp.float64)
  1739. return a, b, c, d, e, f, g
  1740. @pytest.mark.level1
  1741. @pytest.mark.platform_arm_ascend_training
  1742. @pytest.mark.platform_x86_ascend_training
  1743. @pytest.mark.platform_x86_gpu_training
  1744. @pytest.mark.platform_x86_cpu
  1745. @pytest.mark.env_onecard
  1746. def test_sum():
  1747. onp_arr = onp.random.rand(2, 3, 4).astype('float32')
  1748. mnp_arr = to_tensor(onp_arr)
  1749. for actual, expected in zip(mnp_sum(mnp_arr), onp_sum(onp_arr)):
  1750. match_array(actual.asnumpy(), expected, error=5)
  1751. def mnp_sign(x):
  1752. return mnp.sign(x)
  1753. def onp_sign(x):
  1754. return onp.sign(x)
  1755. @pytest.mark.level1
  1756. @pytest.mark.platform_arm_ascend_training
  1757. @pytest.mark.platform_x86_ascend_training
  1758. @pytest.mark.platform_x86_gpu_training
  1759. @pytest.mark.platform_x86_cpu
  1760. @pytest.mark.env_onecard
  1761. def test_sign():
  1762. onp_arr = [
  1763. onp.array(3.5).astype('float32'),
  1764. onp.arange(-5, 5).astype('float32'),
  1765. onp.random.rand(2, 3, 4).astype('float32')
  1766. ]
  1767. mnp_arr = list(map(to_tensor, onp_arr))
  1768. for onp_x, mnp_x in zip(onp_arr, mnp_arr):
  1769. expected = onp_sign(onp_x)
  1770. actual = mnp_sign(mnp_x)
  1771. match_array(actual.asnumpy(), expected, error=5)
  1772. def mnp_copysign(x, y):
  1773. return mnp.copysign(x, y)
  1774. def onp_copysign(x, y):
  1775. return onp.copysign(x, y)
  1776. @pytest.mark.level1
  1777. @pytest.mark.platform_arm_ascend_training
  1778. @pytest.mark.platform_x86_ascend_training
  1779. @pytest.mark.platform_x86_gpu_training
  1780. @pytest.mark.platform_x86_cpu
  1781. @pytest.mark.env_onecard
  1782. def test_copysign():
  1783. onp_arr = [[onp.array([1, -1, 2, -3]).astype('float32'),
  1784. onp.array([1, -1, -1, 1]).astype('float32')],
  1785. [onp.random.rand(2, 3, 4).astype('float32'),
  1786. onp.random.rand(2, 3, 4).astype('float32')]]
  1787. mnp_arr = list(map(to_tensor, onp_arr))
  1788. for onp_x, mnp_x in zip(onp_arr, mnp_arr):
  1789. expected = onp_copysign(onp_x[0], onp_x[1])
  1790. actual = mnp_copysign(mnp_x[0], mnp_x[1])
  1791. match_array(actual.asnumpy(), expected, error=5)
  1792. def mnp_matrix_power(x):
  1793. a = mnp.matrix_power(x, 0)
  1794. b = mnp.matrix_power(x, 1)
  1795. c = mnp.matrix_power(x, 2)
  1796. d = mnp.matrix_power(x, 3)
  1797. return a, b, c, d
  1798. def onp_matrix_power(x):
  1799. a = onp.linalg.matrix_power(x, 0)
  1800. b = onp.linalg.matrix_power(x, 1)
  1801. c = onp.linalg.matrix_power(x, 2)
  1802. d = onp.linalg.matrix_power(x, 3)
  1803. return a, b, c, d
  1804. @pytest.mark.level1
  1805. @pytest.mark.platform_arm_ascend_training
  1806. @pytest.mark.platform_x86_ascend_training
  1807. @pytest.mark.platform_x86_gpu_training
  1808. @pytest.mark.platform_x86_cpu
  1809. @pytest.mark.env_onecard
  1810. def test_matrix_power():
  1811. arrs = [
  1812. onp.random.rand(2, 2).astype('float32'),
  1813. onp.random.rand(3, 2, 2).astype('float32'),
  1814. onp.random.rand(5, 4, 3, 3).astype('float32'),
  1815. ]
  1816. for x in arrs:
  1817. onp_res = onp_matrix_power(x)
  1818. mnp_res = mnp_matrix_power(to_tensor(x))
  1819. for expected, actual in zip(onp_res, mnp_res):
  1820. match_array(actual.asnumpy(), expected, error=5)
  1821. def mnp_around(x):
  1822. a = mnp.around(x)
  1823. b = mnp.around(x, 1)
  1824. c = mnp.around(x, 2)
  1825. d = mnp.around(x, 3)
  1826. return a, b, c, d
  1827. def onp_around(x):
  1828. a = onp.around(x)
  1829. b = onp.around(x, 1)
  1830. c = onp.around(x, 2)
  1831. d = onp.around(x, 3)
  1832. return a, b, c, d
  1833. @pytest.mark.level1
  1834. @pytest.mark.platform_arm_ascend_training
  1835. @pytest.mark.platform_x86_ascend_training
  1836. @pytest.mark.platform_x86_gpu_training
  1837. @pytest.mark.platform_x86_cpu
  1838. @pytest.mark.env_onecard
  1839. def test_around():
  1840. arrs = [
  1841. onp.random.rand(2, 2).astype('float32'),
  1842. onp.random.rand(3, 2, 2).astype('float32'),
  1843. onp.random.rand(5, 4, 3, 3).astype('float32'),
  1844. ]
  1845. for x in arrs:
  1846. onp_res = onp_around(x)
  1847. mnp_res = mnp_around(to_tensor(x))
  1848. for expected, actual in zip(onp_res, mnp_res):
  1849. match_array(actual.asnumpy(), expected, error=5)
  1850. @pytest.mark.level1
  1851. @pytest.mark.platform_arm_ascend_training
  1852. @pytest.mark.platform_x86_ascend_training
  1853. @pytest.mark.platform_x86_gpu_training
  1854. @pytest.mark.platform_x86_cpu
  1855. @pytest.mark.env_onecard
  1856. def test_polyadd():
  1857. arrs = [rand_int(), rand_int(1), rand_int(3), rand_int(7)]
  1858. for x in arrs:
  1859. for y in arrs:
  1860. match_res(mnp.polyadd, onp.polyadd, x, y)
  1861. @pytest.mark.level1
  1862. @pytest.mark.platform_arm_ascend_training
  1863. @pytest.mark.platform_x86_ascend_training
  1864. @pytest.mark.platform_x86_gpu_training
  1865. @pytest.mark.platform_x86_cpu
  1866. @pytest.mark.env_onecard
  1867. def test_polysub():
  1868. arrs = [rand_int(), rand_int(1), rand_int(3), rand_int(7)]
  1869. for x in arrs:
  1870. for y in arrs:
  1871. match_res(mnp.polysub, onp.polysub, x, y, error=1)
  1872. @pytest.mark.level1
  1873. @pytest.mark.platform_arm_ascend_training
  1874. @pytest.mark.platform_x86_ascend_training
  1875. @pytest.mark.platform_x86_gpu_training
  1876. @pytest.mark.platform_x86_cpu
  1877. @pytest.mark.env_onecard
  1878. def test_polyval():
  1879. polys = [rand_int(1), rand_int(3), rand_int(7)]
  1880. arrs = [rand_int(), rand_int(1), rand_int(3), rand_int(2, 3, 1), rand_int(1, 5, 4)]
  1881. for p in polys:
  1882. for x in arrs:
  1883. match_res(mnp.polyval, onp.polyval, p, x, error=3)
  1884. @pytest.mark.level1
  1885. @pytest.mark.platform_arm_ascend_training
  1886. @pytest.mark.platform_x86_ascend_training
  1887. @pytest.mark.platform_x86_gpu_training
  1888. @pytest.mark.platform_x86_cpu
  1889. @pytest.mark.env_onecard
  1890. def test_polyder():
  1891. poly = rand_int(7)
  1892. for i in range(5):
  1893. match_res(mnp.polyder, onp.polyder, poly, m=i)
  1894. @pytest.mark.level2
  1895. @pytest.mark.platform_arm_ascend_training
  1896. @pytest.mark.platform_x86_ascend_training
  1897. @pytest.mark.platform_x86_gpu_training
  1898. @pytest.mark.platform_x86_cpu
  1899. @pytest.mark.env_onecard
  1900. def test_polymul():
  1901. arrs = [rand_int(), rand_int(1), rand_int(3), rand_int(7)]
  1902. for x in arrs:
  1903. for y in arrs:
  1904. match_res(mnp.polymul, onp.polymul, x, y)
  1905. @pytest.mark.level1
  1906. @pytest.mark.platform_arm_ascend_training
  1907. @pytest.mark.platform_x86_ascend_training
  1908. @pytest.mark.platform_x86_gpu_training
  1909. @pytest.mark.platform_x86_cpu
  1910. @pytest.mark.env_onecard
  1911. def test_polyint():
  1912. poly = rand_int(7)
  1913. match_res(mnp.polyint, onp.polyint, poly, m=1, k=7, error=3)
  1914. match_res(mnp.polyint, onp.polyint, poly, m=1, k=[9], error=3)
  1915. match_res(mnp.polyint, onp.polyint, poly, m=3, k=2, error=3)
  1916. for i in range(5):
  1917. match_res(mnp.polyint, onp.polyint, poly, m=i, k=rand_int(i).tolist(), error=3)
  1918. @pytest.mark.level1
  1919. @pytest.mark.platform_arm_ascend_training
  1920. @pytest.mark.platform_x86_ascend_training
  1921. @pytest.mark.platform_x86_gpu_training
  1922. @pytest.mark.platform_x86_cpu
  1923. @pytest.mark.env_onecard
  1924. def test_result_type():
  1925. x = ('?', True, mnp.uint16, mnp.ones((2, 3)).astype(mnp.int32), 'float')
  1926. y = ('?', True, onp.uint16, onp.ones((2, 3)).astype(onp.int32), 'float')
  1927. for i in range(4):
  1928. mnp_args = x[:i + 1]
  1929. actual = dtype_to_nptype(mnp.result_type(*mnp_args))
  1930. onp_args = y[:i + 1]
  1931. expected = onp.result_type(*onp_args)
  1932. if expected == onp.int64:
  1933. expected = onp.int
  1934. elif expected == onp.float64:
  1935. expected = onp.float32
  1936. assert actual == expected
  1937. @pytest.mark.level0
  1938. @pytest.mark.platform_arm_ascend_training
  1939. @pytest.mark.platform_x86_ascend_training
  1940. @pytest.mark.platform_x86_gpu_training
  1941. @pytest.mark.platform_x86_cpu
  1942. @pytest.mark.env_onecard
  1943. def test_unwrap():
  1944. x = onp.linspace(onp.linspace((0, 1), (10, 15), 5), onp.linspace((0, 2), (3*onp.pi, 7*onp.pi), 5), 7)
  1945. x[5:2] += onp.pi
  1946. match_res(mnp.unwrap, onp.unwrap, x, axis=0, error=3)
  1947. match_res(mnp.unwrap, onp.unwrap, x, axis=-1, error=3)
  1948. @pytest.mark.level1
  1949. @pytest.mark.platform_arm_ascend_training
  1950. @pytest.mark.platform_x86_ascend_training
  1951. @pytest.mark.platform_x86_gpu_training
  1952. @pytest.mark.platform_x86_cpu
  1953. @pytest.mark.env_onecard
  1954. def test_exception_amax():
  1955. with pytest.raises(TypeError):
  1956. mnp.amax(mnp.array([[1, 2], [3, 4]]).astype(mnp.float32), initial=[1.0, 2.0])
  1957. def mnp_cumprod(x):
  1958. a = mnp.cumprod(x)
  1959. b = mnp.cumprod(x, axis=0)
  1960. c = mnp.cumprod(x, axis=1)
  1961. return a, b, c
  1962. def onp_cumprod(x):
  1963. a = onp.cumprod(x)
  1964. b = onp.cumprod(x, axis=0)
  1965. c = onp.cumprod(x, axis=1)
  1966. return a, b, c
  1967. @pytest.mark.level1
  1968. @pytest.mark.platform_arm_ascend_training
  1969. @pytest.mark.platform_x86_ascend_training
  1970. @pytest.mark.env_onecard
  1971. def test_cumprod():
  1972. mnp_x = mnp.arange(1, 7).reshape(2, 3)
  1973. tensors = [mnp_x.astype('bool'),
  1974. mnp_x.astype('uint8'),
  1975. mnp_x.astype('int16'),
  1976. mnp_x.astype('float16'),
  1977. mnp_x.astype('float32')]
  1978. for x in tensors:
  1979. onp_res = onp_cumprod(x.asnumpy())
  1980. mnp_res = mnp_cumprod(x)
  1981. for expected, actual in zip(onp_res, mnp_res):
  1982. match_array(actual.asnumpy(), expected, error=5)
  1983. def mnp_ravel_multi_index(x):
  1984. a = mnp.ravel_multi_index(x, (7, 6))
  1985. b = mnp.ravel_multi_index(x, (7, 6), order='F')
  1986. c = mnp.ravel_multi_index(x, (4, 6), mode='clip')
  1987. d = mnp.ravel_multi_index(x, (4, 4), mode='wrap')
  1988. return a, b, c, d
  1989. def onp_ravel_multi_index(x):
  1990. a = onp.ravel_multi_index(x, (7, 6))
  1991. b = onp.ravel_multi_index(x, (7, 6), order='F')
  1992. c = onp.ravel_multi_index(x, (4, 6), mode='clip')
  1993. d = onp.ravel_multi_index(x, (4, 4), mode='wrap')
  1994. return a, b, c, d
  1995. @pytest.mark.level1
  1996. @pytest.mark.platform_x86_gpu_training
  1997. @pytest.mark.env_onecard
  1998. def test_ravel_multi_index():
  1999. x = mnp.array([[3, 6, 6], [4, 5, 1]])
  2000. onp_res = onp_ravel_multi_index(x.asnumpy())
  2001. mnp_res = mnp_ravel_multi_index(x)
  2002. for expected, actual in zip(onp_res, mnp_res):
  2003. match_array(actual.asnumpy(), expected, error=5)
  2004. @pytest.mark.level0
  2005. @pytest.mark.platform_arm_ascend_training
  2006. @pytest.mark.platform_x86_ascend_training
  2007. @pytest.mark.platform_x86_gpu_training
  2008. @pytest.mark.platform_x86_cpu
  2009. @pytest.mark.env_onecard
  2010. def test_norm():
  2011. arrs = [rand_int(5, 2, 3, 7)]
  2012. for x in arrs:
  2013. for keepdims in [True, False]:
  2014. match_res(mnp.norm, onp.linalg.norm, x, keepdims=keepdims, error=3)
  2015. x = rand_int(3, 6, 4, 5)
  2016. axes = [(0, 1), (0, 3), (1, 3), (2, 3)]
  2017. order = [None, 'fro', float('inf'), -float('inf'), 1, -1]
  2018. for axis in axes:
  2019. # pylint: disable=redefined-builtin
  2020. for ord in order:
  2021. for keepdims in [True, False]:
  2022. match_res(mnp.norm, onp.linalg.norm, x, ord=ord, axis=axis, keepdims=keepdims, error=3)
  2023. @pytest.mark.level1
  2024. @pytest.mark.platform_arm_ascend_training
  2025. @pytest.mark.platform_x86_ascend_training
  2026. @pytest.mark.env_onecard
  2027. def test_bitwise_and():
  2028. arrs = [onp.random.randint(-100, 100, ()), onp.random.randint(-100, 100, (1,)),
  2029. onp.random.randint(-100, 100, (5,)), onp.random.randint(-100, 100, (3, 1)),
  2030. onp.random.randint(-100, 100, (4, 1, 5))]
  2031. for x in arrs:
  2032. for y in arrs:
  2033. match_res(mnp.bitwise_and, onp.bitwise_and, x, y, dtype=mnp.int32)
  2034. @pytest.mark.level1
  2035. @pytest.mark.platform_arm_ascend_training
  2036. @pytest.mark.platform_x86_ascend_training
  2037. @pytest.mark.env_onecard
  2038. def test_bitwise_or():
  2039. arrs = [onp.random.randint(-100, 100, ()), onp.random.randint(-100, 100, (1,)),
  2040. onp.random.randint(-100, 100, (5,)), onp.random.randint(-100, 100, (3, 1)),
  2041. onp.random.randint(-100, 100, (4, 1, 5))]
  2042. for x in arrs:
  2043. for y in arrs:
  2044. match_res(mnp.bitwise_or, onp.bitwise_or, x, y, dtype=mnp.int32)
  2045. @pytest.mark.level1
  2046. @pytest.mark.platform_arm_ascend_training
  2047. @pytest.mark.platform_x86_ascend_training
  2048. @pytest.mark.env_onecard
  2049. def test_bitwise_xor():
  2050. arrs = [onp.random.randint(-100, 100, ()), onp.random.randint(-100, 100, (1,)),
  2051. onp.random.randint(-100, 100, (5,)), onp.random.randint(-100, 100, (3, 1)),
  2052. onp.random.randint(-100, 100, (4, 1, 5))]
  2053. for x in arrs:
  2054. for y in arrs:
  2055. match_res(mnp.bitwise_xor, onp.bitwise_xor, x, y, dtype=mnp.int32)
  2056. @pytest.mark.level1
  2057. @pytest.mark.platform_arm_ascend_training
  2058. @pytest.mark.platform_x86_ascend_training
  2059. @pytest.mark.env_onecard
  2060. def test_invert():
  2061. x = onp.random.randint(-100, 100, (2, 3))
  2062. match_res(mnp.invert, onp.invert, x, dtype=mnp.int16)
  2063. match_res(mnp.invert, onp.invert, x.astype(onp.uint16), dtype=mnp.uint16)
  2064. @pytest.mark.level1
  2065. @pytest.mark.platform_arm_ascend_training
  2066. @pytest.mark.platform_x86_ascend_training
  2067. @pytest.mark.platform_x86_gpu_training
  2068. @pytest.mark.platform_x86_cpu
  2069. @pytest.mark.env_onecard
  2070. def test_rint():
  2071. arrs = [
  2072. onp.random.rand(2, 2).astype('float32'),
  2073. onp.random.rand(3, 2, 2).astype('float32'),
  2074. onp.random.rand(5, 4, 3, 3).astype('float32'),
  2075. ]
  2076. for x in arrs:
  2077. for expected, actual in zip(onp.rint(x), mnp.rint(to_tensor(x))):
  2078. match_array(actual.asnumpy(), expected, error=5)
  2079. def mnp_correlate(a, v):
  2080. a = mnp.correlate(a, v, mode="valid")
  2081. b = mnp.correlate(a, v, mode="full")
  2082. c = mnp.correlate(a, v, mode="same")
  2083. d = mnp.correlate(a, v)
  2084. return a, b, c, d
  2085. def onp_correlate(a, v):
  2086. a = onp.correlate(a, v, mode="valid")
  2087. b = onp.correlate(a, v, mode="full")
  2088. c = onp.correlate(a, v, mode="same")
  2089. d = onp.correlate(a, v)
  2090. return a, b, c, d
  2091. @pytest.mark.level0
  2092. @pytest.mark.platform_x86_gpu_training
  2093. @pytest.mark.env_onecard
  2094. def test_correlate():
  2095. mnp_res = mnp_correlate([1, 2, 3, 4, 5], [1, 2, 3])
  2096. onp_res = onp_correlate([1, 2, 3, 4, 5], [1, 2, 3])
  2097. match_all_arrays(mnp_res, onp_res)
  2098. @pytest.mark.level1
  2099. @pytest.mark.platform_arm_ascend_training
  2100. @pytest.mark.platform_x86_ascend_training
  2101. @pytest.mark.platform_x86_gpu_training
  2102. @pytest.mark.platform_x86_cpu
  2103. @pytest.mark.env_onecard
  2104. def test_tensor_searchsorted():
  2105. x = onp.arange(-10, 10)
  2106. mnp_x = to_tensor(x)
  2107. y = onp.random.randint(-15, 15, size=(2, 3, 4)) + onp.random.choice([0, 0.5], (2, 3, 4))
  2108. sorter = onp.random.shuffle(onp.arange(20))
  2109. match_res(mnp_x.searchsorted, x.searchsorted, y)
  2110. match_res(mnp_x.searchsorted, x.searchsorted, y, side='right')
  2111. match_res(mnp_x.searchsorted, x.searchsorted, y, sorter=sorter)
  2112. match_res(mnp_x.searchsorted, x.searchsorted, y, side='right', sorter=sorter)