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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  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 .utils import rand_int, rand_bool, run_binop_test, run_unary_test, run_multi_test, \
  20. run_single_test, match_res, match_array, match_meta, match_all_arrays, to_tensor
  21. class Cases():
  22. def __init__(self):
  23. self.arrs = [
  24. rand_int(2),
  25. rand_int(2, 3),
  26. rand_int(2, 3, 4),
  27. ]
  28. # scalars expanded across the 0th dimension
  29. self.scalars = [
  30. rand_int(),
  31. rand_int(1),
  32. rand_int(1, 1),
  33. ]
  34. # arrays of the same size expanded across the 0th dimension
  35. self.expanded_arrs = [
  36. rand_int(2, 3),
  37. rand_int(1, 2, 3),
  38. rand_int(1, 1, 2, 3),
  39. ]
  40. # arrays with last dimension aligned
  41. self.aligned_arrs = [
  42. rand_int(2, 3),
  43. rand_int(1, 4, 3),
  44. rand_int(5, 1, 2, 3),
  45. rand_int(4, 2, 1, 1, 3),
  46. ]
  47. # arrays which can be broadcast
  48. self.broadcastables = [
  49. rand_int(5),
  50. rand_int(6, 1),
  51. rand_int(7, 1, 5),
  52. ]
  53. # boolean arrays which can be broadcast
  54. self.bool_broadcastables = [
  55. rand_bool(),
  56. rand_bool(1),
  57. rand_bool(5),
  58. rand_bool(6, 1),
  59. rand_bool(7, 1, 5),
  60. rand_bool(8, 1, 6, 1),
  61. ]
  62. # core dimension 0 is matched for each
  63. # pair of array[i] and array[i + 1]
  64. self.core_broadcastables = [
  65. rand_int(3),
  66. rand_int(3),
  67. rand_int(6),
  68. rand_int(6, 4),
  69. rand_int(5, 2),
  70. rand_int(2),
  71. rand_int(2, 9),
  72. rand_int(9, 8),
  73. rand_int(6),
  74. rand_int(2, 6, 5),
  75. rand_int(9, 2, 7),
  76. rand_int(7),
  77. rand_int(5, 2, 4),
  78. rand_int(6, 1, 4, 9),
  79. rand_int(7, 1, 5, 3, 2),
  80. rand_int(8, 1, 6, 1, 2, 9),
  81. ]
  82. # arrays with dimensions of size 1
  83. self.nested_arrs = [
  84. rand_int(1),
  85. rand_int(1, 2),
  86. rand_int(3, 1, 8),
  87. rand_int(1, 3, 9, 1),
  88. ]
  89. test_case = Cases()
  90. def mnp_add(x1, x2):
  91. return mnp.add(x1, x2)
  92. def onp_add(x1, x2):
  93. return onp.add(x1, x2)
  94. def mnp_subtract(x1, x2):
  95. return mnp.subtract(x1, x2)
  96. def onp_subtract(x1, x2):
  97. return onp.subtract(x1, x2)
  98. def mnp_mutiply(x1, x2):
  99. return mnp.multiply(x1, x2)
  100. def onp_multiply(x1, x2):
  101. return onp.multiply(x1, x2)
  102. def mnp_divide(x1, x2):
  103. return mnp.divide(x1, x2)
  104. def onp_divide(x1, x2):
  105. return onp.divide(x1, x2)
  106. def mnp_true_divide(x1, x2):
  107. return mnp.true_divide(x1, x2)
  108. def onp_true_divide(x1, x2):
  109. return onp.true_divide(x1, x2)
  110. def mnp_power(x1, x2):
  111. return mnp.power(x1, x2)
  112. def onp_power(x1, x2):
  113. return onp.power(x1, x2)
  114. def mnp_float_power(x1, x2):
  115. return mnp.float_power(x1, x2)
  116. def onp_float_power(x1, x2):
  117. return onp.float_power(x1, x2)
  118. def mnp_minimum(a, b):
  119. return mnp.minimum(a, b)
  120. def onp_minimum(a, b):
  121. return onp.minimum(a, b)
  122. @pytest.mark.level1
  123. @pytest.mark.platform_arm_ascend_training
  124. @pytest.mark.platform_x86_ascend_training
  125. @pytest.mark.platform_x86_gpu_training
  126. @pytest.mark.platform_x86_cpu
  127. @pytest.mark.env_onecard
  128. def test_add():
  129. run_binop_test(mnp_add, onp_add, test_case)
  130. @pytest.mark.level1
  131. @pytest.mark.platform_arm_ascend_training
  132. @pytest.mark.platform_x86_ascend_training
  133. @pytest.mark.platform_x86_gpu_training
  134. @pytest.mark.platform_x86_cpu
  135. @pytest.mark.env_onecard
  136. def test_subtract():
  137. run_binop_test(mnp_subtract, onp_subtract, test_case)
  138. @pytest.mark.level1
  139. @pytest.mark.platform_arm_ascend_training
  140. @pytest.mark.platform_x86_ascend_training
  141. @pytest.mark.platform_x86_gpu_training
  142. @pytest.mark.platform_x86_cpu
  143. @pytest.mark.env_onecard
  144. def test_multiply():
  145. run_binop_test(mnp_mutiply, onp_multiply, test_case)
  146. @pytest.mark.level1
  147. @pytest.mark.platform_arm_ascend_training
  148. @pytest.mark.platform_x86_ascend_training
  149. @pytest.mark.platform_x86_gpu_training
  150. @pytest.mark.platform_x86_cpu
  151. @pytest.mark.env_onecard
  152. def test_divide():
  153. run_binop_test(mnp_divide, onp_divide, test_case)
  154. @pytest.mark.level1
  155. @pytest.mark.platform_arm_ascend_training
  156. @pytest.mark.platform_x86_ascend_training
  157. @pytest.mark.platform_x86_gpu_training
  158. @pytest.mark.platform_x86_cpu
  159. @pytest.mark.env_onecard
  160. def test_true_divide():
  161. run_binop_test(mnp_true_divide, onp_true_divide, test_case)
  162. @pytest.mark.level1
  163. @pytest.mark.platform_arm_ascend_training
  164. @pytest.mark.platform_x86_ascend_training
  165. @pytest.mark.platform_x86_gpu_training
  166. @pytest.mark.platform_x86_cpu
  167. @pytest.mark.env_onecard
  168. def test_power():
  169. run_binop_test(mnp_power, onp_power, test_case, error=1e-5)
  170. @pytest.mark.level1
  171. @pytest.mark.platform_arm_ascend_training
  172. @pytest.mark.platform_x86_ascend_training
  173. @pytest.mark.platform_x86_gpu_training
  174. @pytest.mark.platform_x86_cpu
  175. @pytest.mark.env_onecard
  176. def test_float_power():
  177. run_binop_test(mnp_float_power, onp_float_power, test_case, error=1e-5)
  178. @pytest.mark.level1
  179. @pytest.mark.platform_x86_gpu_training
  180. @pytest.mark.platform_x86_cpu
  181. @pytest.mark.env_onecard
  182. def test_minimum():
  183. run_binop_test(mnp_minimum, onp_minimum, test_case)
  184. x = onp.random.randint(-10, 10, 20).astype(onp.float32)
  185. y = onp.random.randint(-10, 10, 20).astype(onp.float32)
  186. x[onp.random.randint(0, 10, 3)] = onp.nan
  187. y[onp.random.randint(0, 10, 3)] = onp.nan
  188. x[onp.random.randint(0, 10, 3)] = onp.NINF
  189. y[onp.random.randint(0, 10, 3)] = onp.NINF
  190. x[onp.random.randint(0, 10, 3)] = onp.PINF
  191. y[onp.random.randint(0, 10, 3)] = onp.PINF
  192. match_res(mnp_minimum, onp_minimum, x, y)
  193. match_res(mnp_minimum, onp_minimum, y, x)
  194. def mnp_tensordot(x, y):
  195. a = mnp.tensordot(x, y)
  196. b = mnp.tensordot(x, y, axes=0)
  197. c = mnp.tensordot(x, y, axes=1)
  198. d = mnp.tensordot(x, y, axes=2)
  199. e = mnp.tensordot(x, y, axes=(3, 0))
  200. f = mnp.tensordot(x, y, axes=[2, 1])
  201. g = mnp.tensordot(x, y, axes=((2, 3), (0, 1)))
  202. h = mnp.tensordot(x, y, axes=[[3, 2], [1, 0]])
  203. return a, b, c, d, e, f, g, h
  204. def onp_tensordot(x, y):
  205. a = onp.tensordot(x, y)
  206. b = onp.tensordot(x, y, axes=0)
  207. c = onp.tensordot(x, y, axes=1)
  208. d = onp.tensordot(x, y, axes=2)
  209. e = onp.tensordot(x, y, axes=(3, 0))
  210. f = onp.tensordot(x, y, axes=[2, 1])
  211. g = onp.tensordot(x, y, axes=((2, 3), (0, 1)))
  212. h = onp.tensordot(x, y, axes=[[3, 2], [1, 0]])
  213. return a, b, c, d, e, f, g, h
  214. @pytest.mark.level1
  215. @pytest.mark.platform_arm_ascend_training
  216. @pytest.mark.platform_x86_ascend_training
  217. @pytest.mark.platform_x86_gpu_training
  218. @pytest.mark.platform_x86_cpu
  219. @pytest.mark.env_onecard
  220. def test_tensordot():
  221. x = rand_int(4, 2, 7, 7)
  222. y = rand_int(7, 7, 6)
  223. run_multi_test(mnp_tensordot, onp_tensordot, (x, y))
  224. def mnp_std(x):
  225. a = mnp.std(x)
  226. b = mnp.std(x, axis=None)
  227. c = mnp.std(x, axis=0)
  228. d = mnp.std(x, axis=1)
  229. e = mnp.std(x, axis=(-1, 1))
  230. f = mnp.std(x, axis=(0, 1, 2))
  231. g = mnp.std(x, axis=None, ddof=1, keepdims=True)
  232. h = mnp.std(x, axis=0, ddof=1, keepdims=True)
  233. i = mnp.std(x, axis=(2), ddof=1, keepdims=True)
  234. return a, b, c, d, e, f, g, h, i
  235. def onp_std(x):
  236. a = onp.std(x)
  237. b = onp.std(x, axis=None)
  238. c = onp.std(x, axis=0)
  239. d = onp.std(x, axis=1)
  240. e = onp.std(x, axis=(-1, 1))
  241. f = onp.std(x, axis=(0, 1, 2))
  242. g = onp.std(x, axis=None, ddof=1, keepdims=True)
  243. h = onp.std(x, axis=0, ddof=1, keepdims=True)
  244. i = onp.std(x, axis=(2), ddof=1, keepdims=True)
  245. return a, b, c, d, e, f, g, h, i
  246. @pytest.mark.level1
  247. @pytest.mark.platform_arm_ascend_training
  248. @pytest.mark.platform_x86_ascend_training
  249. @pytest.mark.platform_x86_gpu_training
  250. @pytest.mark.platform_x86_cpu
  251. @pytest.mark.env_onecard
  252. def test_std():
  253. arr1 = rand_int(2, 3, 4, 5)
  254. arr2 = rand_int(4, 5, 4, 3, 3)
  255. run_single_test(mnp_std, onp_std, arr1, error=1e-5)
  256. run_single_test(mnp_std, onp_std, arr2, error=1e-5)
  257. def mnp_nanstd(x):
  258. a = mnp.nanstd(x)
  259. b = mnp.nanstd(x, axis=None)
  260. c = mnp.nanstd(x, axis=0)
  261. d = mnp.nanstd(x, axis=1)
  262. e = mnp.nanstd(x, axis=(-1, 1))
  263. f = mnp.nanstd(x, axis=(0, 1, 2))
  264. g = mnp.nanstd(x, axis=None, ddof=1, keepdims=True)
  265. h = mnp.nanstd(x, axis=0, ddof=1, keepdims=True)
  266. i = mnp.nanstd(x, axis=(2), ddof=1, keepdims=True)
  267. return a, b, c, d, e, f, g, h, i
  268. def onp_nanstd(x):
  269. a = onp.nanstd(x)
  270. b = onp.nanstd(x, axis=None)
  271. c = onp.nanstd(x, axis=0)
  272. d = onp.nanstd(x, axis=1)
  273. e = onp.nanstd(x, axis=(-1, 1))
  274. f = onp.nanstd(x, axis=(0, 1, 2))
  275. g = onp.nanstd(x, axis=None, ddof=1, keepdims=True)
  276. h = onp.nanstd(x, axis=0, ddof=1, keepdims=True)
  277. i = onp.nanstd(x, axis=(2), ddof=1, keepdims=True)
  278. return a, b, c, d, e, f, g, h, i
  279. @pytest.mark.level1
  280. @pytest.mark.platform_x86_gpu_training
  281. @pytest.mark.platform_x86_cpu
  282. @pytest.mark.env_onecard
  283. def test_nanstd():
  284. arr1 = rand_int(2, 3, 4, 5)
  285. arr1[0][2][1][3] = onp.nan
  286. arr1[1][0][2][4] = onp.nan
  287. arr1[1][1][1][1] = onp.nan
  288. arr2 = rand_int(4, 5, 4, 3, 3)
  289. arr2[3][1][2][1][0] = onp.nan
  290. arr2[1][1][1][1][1] = onp.nan
  291. arr2[0][4][3][0][2] = onp.nan
  292. run_single_test(mnp_nanstd, onp_nanstd, arr1, error=1e-5)
  293. run_single_test(mnp_nanstd, onp_nanstd, arr2, error=1e-5)
  294. def mnp_var(x):
  295. a = mnp.var(x)
  296. b = mnp.var(x, axis=0)
  297. c = mnp.var(x, axis=(0))
  298. d = mnp.var(x, axis=(0, 1, 2))
  299. e = mnp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True)
  300. return a, b, c, d, e
  301. def onp_var(x):
  302. a = onp.var(x)
  303. b = onp.var(x, axis=0)
  304. c = onp.var(x, axis=(0))
  305. d = onp.var(x, axis=(0, 1, 2))
  306. e = onp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True)
  307. return a, b, c, d, e
  308. @pytest.mark.level1
  309. @pytest.mark.platform_arm_ascend_training
  310. @pytest.mark.platform_x86_ascend_training
  311. @pytest.mark.platform_x86_gpu_training
  312. @pytest.mark.platform_x86_cpu
  313. @pytest.mark.env_onecard
  314. def test_var():
  315. arr1 = rand_int(2, 3, 4, 5)
  316. arr2 = rand_int(4, 5, 4, 3, 3)
  317. run_single_test(mnp_var, onp_var, arr1, error=1e-5)
  318. run_single_test(mnp_var, onp_var, arr2, error=1e-5)
  319. def mnp_nanvar(x):
  320. a = mnp.var(x)
  321. b = mnp.var(x, axis=0)
  322. c = mnp.var(x, axis=(0))
  323. d = mnp.var(x, axis=(0, 1, 2))
  324. e = mnp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True)
  325. return a, b, c, d, e
  326. def onp_nanvar(x):
  327. a = onp.var(x)
  328. b = onp.var(x, axis=0)
  329. c = onp.var(x, axis=(0))
  330. d = onp.var(x, axis=(0, 1, 2))
  331. e = onp.var(x, axis=(-1, 1, 2), ddof=1, keepdims=True)
  332. return a, b, c, d, e
  333. @pytest.mark.level1
  334. @pytest.mark.platform_x86_gpu_training
  335. @pytest.mark.platform_x86_cpu
  336. @pytest.mark.env_onecard
  337. def test_nanvar():
  338. arr1 = rand_int(2, 3, 4, 5)
  339. arr1[0][2][1][3] = onp.nan
  340. arr1[1][0][2][4] = onp.nan
  341. arr1[1][1][1][1] = onp.nan
  342. arr2 = rand_int(4, 5, 4, 3, 3)
  343. arr2[3][1][2][1][0] = onp.nan
  344. arr2[1][1][1][1][1] = onp.nan
  345. arr2[0][4][3][0][2] = onp.nan
  346. run_single_test(mnp_nanvar, onp_nanvar, arr1, error=1e-5)
  347. run_single_test(mnp_nanvar, onp_nanvar, arr2, error=1e-5)
  348. def mnp_average(x):
  349. a = mnp.average(x)
  350. b = mnp.average(x, axis=None)
  351. c = mnp.average(x, axis=0)
  352. d = mnp.average(x, axis=1)
  353. e = mnp.average(x, axis=(-2, 1))
  354. f = mnp.average(x, axis=(0, 1, 2, 3))
  355. g = mnp.average(x, axis=None, weights=x)
  356. h = mnp.average(x, axis=0, weights=x)
  357. i = mnp.average(x, axis=(1, 2, 3), weights=x)
  358. return a, b, c, d, e, f, g, h, i
  359. def onp_average(x):
  360. a = onp.average(x)
  361. b = onp.average(x, axis=None)
  362. c = onp.average(x, axis=0)
  363. d = onp.average(x, axis=1)
  364. e = onp.average(x, axis=(-2, 1))
  365. f = onp.average(x, axis=(0, 1, 2, 3))
  366. g = onp.average(x, axis=None, weights=x)
  367. h = onp.average(x, axis=0, weights=x)
  368. i = onp.average(x, axis=(1, 2, 3), weights=x)
  369. return a, b, c, d, e, f, g, h, i
  370. @pytest.mark.level1
  371. @pytest.mark.platform_arm_ascend_training
  372. @pytest.mark.platform_x86_ascend_training
  373. @pytest.mark.platform_x86_gpu_training
  374. @pytest.mark.platform_x86_cpu
  375. @pytest.mark.env_onecard
  376. def test_average():
  377. arr1 = rand_int(2, 3, 4, 5)
  378. arr2 = rand_int(4, 5, 1, 3, 1)
  379. run_single_test(mnp_average, onp_average, arr1, error=1e-5)
  380. run_single_test(mnp_average, onp_average, arr2, error=1e-5)
  381. def mnp_count_nonzero(x):
  382. a = mnp.count_nonzero(x)
  383. b = mnp.count_nonzero(x, axis=None)
  384. c = mnp.count_nonzero(x, axis=0)
  385. d = mnp.count_nonzero(x, axis=1)
  386. e = mnp.count_nonzero(x, axis=(-2, 1))
  387. f = mnp.count_nonzero(x, axis=(0, 1, 2, 3))
  388. return a, b, c, d, e, f
  389. def onp_count_nonzero(x):
  390. a = onp.count_nonzero(x)
  391. b = onp.count_nonzero(x, axis=None)
  392. c = onp.count_nonzero(x, axis=0)
  393. d = onp.count_nonzero(x, axis=1)
  394. e = onp.count_nonzero(x, axis=(-2, 1))
  395. f = onp.count_nonzero(x, axis=(0, 1, 2, 3))
  396. return a, b, c, d, e, f
  397. @pytest.mark.level1
  398. @pytest.mark.platform_arm_ascend_training
  399. @pytest.mark.platform_x86_ascend_training
  400. @pytest.mark.platform_x86_gpu_training
  401. @pytest.mark.platform_x86_cpu
  402. @pytest.mark.env_onecard
  403. def test_count_nonzero():
  404. # minus 5 to make some values below zero
  405. arr1 = rand_int(2, 3, 4, 5) - 5
  406. arr2 = rand_int(4, 5, 4, 3, 3) - 5
  407. run_single_test(mnp_count_nonzero, onp_count_nonzero, arr1)
  408. run_single_test(mnp_count_nonzero, onp_count_nonzero, arr2)
  409. def mnp_inner(a, b):
  410. return mnp.inner(a, b)
  411. def onp_inner(a, b):
  412. return onp.inner(a, b)
  413. @pytest.mark.level1
  414. @pytest.mark.platform_arm_ascend_training
  415. @pytest.mark.platform_x86_ascend_training
  416. @pytest.mark.platform_x86_gpu_training
  417. @pytest.mark.platform_x86_cpu
  418. @pytest.mark.env_onecard
  419. def test_inner():
  420. for arr1 in test_case.aligned_arrs:
  421. for arr2 in test_case.aligned_arrs:
  422. match_res(mnp_inner, onp_inner, arr1, arr2)
  423. for scalar1 in test_case.scalars:
  424. for scalar2 in test_case.scalars:
  425. match_res(mnp_inner, onp_inner,
  426. scalar1, scalar2)
  427. def mnp_dot(a, b):
  428. return mnp.dot(a, b)
  429. def onp_dot(a, b):
  430. return onp.dot(a, b)
  431. @pytest.mark.level1
  432. @pytest.mark.platform_arm_ascend_training
  433. @pytest.mark.platform_x86_ascend_training
  434. @pytest.mark.platform_x86_gpu_training
  435. @pytest.mark.platform_x86_cpu
  436. @pytest.mark.env_onecard
  437. def test_dot():
  438. # test case (1D, 1D)
  439. match_res(mnp_dot, onp_dot, rand_int(3), rand_int(3))
  440. # test case (2D, 2D)
  441. match_res(mnp_dot, onp_dot, rand_int(4, 7), rand_int(7, 2))
  442. # test case (0D, _) (_, 0D)
  443. match_res(mnp_dot, onp_dot, rand_int(), rand_int(1, 9, 3))
  444. match_res(mnp_dot, onp_dot, rand_int(8, 5, 6, 3), rand_int())
  445. # test case (ND, 1D)
  446. match_res(mnp_dot, onp_dot, rand_int(2, 4, 5), rand_int(5))
  447. # test case (ND, MD)
  448. match_res(mnp_dot, onp_dot, rand_int(5, 4, 1, 8), rand_int(8, 3))
  449. for i in range(8):
  450. match_res(mnp_dot, onp_dot,
  451. test_case.core_broadcastables[2*i], test_case.core_broadcastables[2*i + 1])
  452. def mnp_outer(a, b):
  453. return mnp.outer(a, b)
  454. def onp_outer(a, b):
  455. return onp.outer(a, b)
  456. @pytest.mark.level1
  457. @pytest.mark.platform_arm_ascend_training
  458. @pytest.mark.platform_x86_ascend_training
  459. @pytest.mark.platform_x86_gpu_training
  460. @pytest.mark.platform_x86_cpu
  461. @pytest.mark.env_onecard
  462. def test_outer():
  463. run_binop_test(mnp_outer, onp_outer, test_case)
  464. @pytest.mark.level1
  465. @pytest.mark.platform_arm_ascend_training
  466. @pytest.mark.platform_x86_ascend_training
  467. @pytest.mark.platform_x86_gpu_training
  468. @pytest.mark.platform_x86_cpu
  469. @pytest.mark.env_onecard
  470. def test_type_promotion():
  471. arr = rand_int(2, 3)
  472. onp_sum = onp_add(arr, arr)
  473. a = to_tensor(arr, dtype=mnp.float16)
  474. b = to_tensor(arr, dtype=mnp.float32)
  475. c = to_tensor(arr, dtype=mnp.int32)
  476. match_array(mnp_add(a, b).asnumpy(), onp_sum)
  477. match_array(mnp_add(b, c).asnumpy(), onp_sum)
  478. def mnp_absolute(x):
  479. return mnp.absolute(x)
  480. def onp_absolute(x):
  481. return onp.absolute(x)
  482. @pytest.mark.level1
  483. @pytest.mark.platform_arm_ascend_training
  484. @pytest.mark.platform_x86_ascend_training
  485. @pytest.mark.platform_x86_gpu_training
  486. @pytest.mark.platform_x86_cpu
  487. @pytest.mark.env_onecard
  488. def test_absolute():
  489. arr = rand_int(2, 3)
  490. a = to_tensor(arr, dtype=mnp.float16)
  491. b = to_tensor(arr, dtype=mnp.float32)
  492. c = to_tensor(arr, dtype=mnp.uint8)
  493. d = to_tensor(arr, dtype=mnp.bool_)
  494. match_array(mnp_absolute(a).asnumpy(), onp_absolute(a.asnumpy()))
  495. match_array(mnp_absolute(b).asnumpy(), onp_absolute(b.asnumpy()))
  496. match_array(mnp_absolute(c).asnumpy(), onp_absolute(c.asnumpy()))
  497. match_array(mnp_absolute(d).asnumpy(), onp_absolute(d.asnumpy()))
  498. @pytest.mark.level1
  499. @pytest.mark.platform_arm_ascend_training
  500. @pytest.mark.platform_x86_ascend_training
  501. @pytest.mark.platform_x86_gpu_training
  502. @pytest.mark.platform_x86_cpu
  503. @pytest.mark.env_onecard
  504. def test_deg2rad_rad2deg():
  505. arrs = [rand_int(2, 3), rand_int(1, 2, 4), rand_int(2, 4)]
  506. for arr in arrs:
  507. match_res(mnp.deg2rad, onp.deg2rad, arr)
  508. match_res(mnp.rad2deg, onp.rad2deg, arr)
  509. def mnp_ptp(x):
  510. a = mnp.ptp(x)
  511. b = mnp.ptp(x, keepdims=True)
  512. c = mnp.ptp(x, axis=(0, 1))
  513. d = mnp.ptp(x, axis=-1)
  514. return a, b, c, d
  515. def onp_ptp(x):
  516. a = onp.ptp(x)
  517. b = onp.ptp(x, keepdims=True)
  518. c = onp.ptp(x, axis=(0, 1))
  519. d = onp.ptp(x, axis=-1)
  520. return a, b, c, d
  521. @pytest.mark.level1
  522. @pytest.mark.platform_arm_ascend_training
  523. @pytest.mark.platform_x86_ascend_training
  524. @pytest.mark.platform_x86_gpu_training
  525. @pytest.mark.platform_x86_cpu
  526. @pytest.mark.env_onecard
  527. def test_ptp():
  528. arrs = [rand_int(2, 3), rand_int(1, 2, 4), rand_int(2, 4)]
  529. for arr in arrs:
  530. match_res(mnp_ptp, onp_ptp, arr)
  531. def mnp_add_dtype(x1, x2):
  532. return mnp.add(x1, x2, dtype=mnp.float32)
  533. def onp_add_dtype(x1, x2):
  534. return onp.add(x1, x2, dtype=onp.float32)
  535. @pytest.mark.level1
  536. @pytest.mark.platform_arm_ascend_training
  537. @pytest.mark.platform_x86_ascend_training
  538. @pytest.mark.platform_x86_gpu_training
  539. @pytest.mark.platform_x86_cpu
  540. @pytest.mark.env_onecard
  541. def test_add_dtype():
  542. x1 = rand_int(2, 3).astype('int32')
  543. x2 = rand_int(2, 3).astype('int32')
  544. arrs = (x1, x2)
  545. mnp_arrs = map(to_tensor, arrs)
  546. mnp_res = mnp_add_dtype(*mnp_arrs)
  547. onp_res = onp_add_dtype(*arrs)
  548. for actual, expected in zip(mnp_res, onp_res):
  549. assert actual.asnumpy().dtype == expected.dtype
  550. def mnp_matmul(x1, x2):
  551. return mnp.matmul(x1, x2)
  552. def onp_matmul(x1, x2):
  553. return onp.matmul(x1, x2)
  554. @pytest.mark.level1
  555. @pytest.mark.platform_arm_ascend_training
  556. @pytest.mark.platform_x86_ascend_training
  557. @pytest.mark.platform_x86_gpu_training
  558. @pytest.mark.platform_x86_cpu
  559. @pytest.mark.env_onecard
  560. def test_matmul():
  561. for scalar1 in test_case.scalars[1:]:
  562. for scalar2 in test_case.scalars[1:]:
  563. match_res(mnp_matmul, onp_matmul,
  564. scalar1, scalar2)
  565. for i in range(8):
  566. match_res(mnp_matmul, onp_matmul,
  567. test_case.core_broadcastables[2*i],
  568. test_case.core_broadcastables[2*i + 1])
  569. def mnp_square(x):
  570. return mnp.square(x)
  571. def onp_square(x):
  572. return onp.square(x)
  573. @pytest.mark.level1
  574. @pytest.mark.platform_arm_ascend_training
  575. @pytest.mark.platform_x86_ascend_training
  576. @pytest.mark.platform_x86_gpu_training
  577. @pytest.mark.platform_x86_cpu
  578. @pytest.mark.env_onecard
  579. def test_square():
  580. run_unary_test(mnp_square, onp_square, test_case)
  581. def mnp_sqrt(x):
  582. return mnp.sqrt(x)
  583. def onp_sqrt(x):
  584. return onp.sqrt(x)
  585. @pytest.mark.level1
  586. @pytest.mark.platform_arm_ascend_training
  587. @pytest.mark.platform_x86_ascend_training
  588. @pytest.mark.platform_x86_gpu_training
  589. @pytest.mark.platform_x86_cpu
  590. @pytest.mark.env_onecard
  591. def test_sqrt():
  592. run_unary_test(mnp_sqrt, onp_sqrt, test_case)
  593. def mnp_reciprocal(x):
  594. return mnp.reciprocal(x)
  595. def onp_reciprocal(x):
  596. return onp.reciprocal(x)
  597. @pytest.mark.level1
  598. @pytest.mark.platform_arm_ascend_training
  599. @pytest.mark.platform_x86_ascend_training
  600. @pytest.mark.platform_x86_gpu_training
  601. @pytest.mark.platform_x86_cpu
  602. @pytest.mark.env_onecard
  603. def test_reciprocal():
  604. run_unary_test(mnp_reciprocal, onp_reciprocal, test_case)
  605. def mnp_log(x):
  606. return mnp.log(x)
  607. def onp_log(x):
  608. return onp.log(x)
  609. @pytest.mark.level1
  610. @pytest.mark.platform_arm_ascend_training
  611. @pytest.mark.platform_x86_ascend_training
  612. @pytest.mark.platform_x86_gpu_training
  613. @pytest.mark.platform_x86_cpu
  614. @pytest.mark.env_onecard
  615. def test_log():
  616. run_unary_test(mnp.log, onp.log, test_case, error=1e-5)
  617. def mnp_log1p(x):
  618. return mnp.log1p(x)
  619. def onp_log1p(x):
  620. return onp.log1p(x)
  621. @pytest.mark.level1
  622. @pytest.mark.platform_arm_ascend_training
  623. @pytest.mark.platform_x86_ascend_training
  624. @pytest.mark.platform_x86_gpu_training
  625. @pytest.mark.platform_x86_cpu
  626. @pytest.mark.env_onecard
  627. def test_log1p():
  628. run_unary_test(mnp_log1p, onp_log1p, test_case, error=1e-5)
  629. def mnp_logaddexp(x1, x2):
  630. return mnp.logaddexp(x1, x2)
  631. def onp_logaddexp(x1, x2):
  632. return onp.logaddexp(x1, x2)
  633. @pytest.mark.level1
  634. @pytest.mark.platform_arm_ascend_training
  635. @pytest.mark.platform_x86_ascend_training
  636. @pytest.mark.platform_x86_gpu_training
  637. @pytest.mark.platform_x86_cpu
  638. @pytest.mark.env_onecard
  639. def test_logaddexp():
  640. test_cases = [
  641. onp.random.randint(1, 5, (2)).astype('float16'),
  642. onp.random.randint(1, 5, (3, 2)).astype('float16'),
  643. onp.random.randint(1, 5, (1, 3, 2)).astype('float16'),
  644. onp.random.randint(1, 5, (5, 6, 3, 2)).astype('float16')]
  645. for _, x1 in enumerate(test_cases):
  646. for _, x2 in enumerate(test_cases):
  647. expected = onp_logaddexp(x1, x2)
  648. actual = mnp_logaddexp(to_tensor(x1), to_tensor(x2))
  649. onp.testing.assert_almost_equal(actual.asnumpy().tolist(), expected.tolist(),
  650. decimal=2)
  651. def mnp_log2(x):
  652. return mnp.log2(x)
  653. def onp_log2(x):
  654. return onp.log2(x)
  655. @pytest.mark.level1
  656. @pytest.mark.platform_arm_ascend_training
  657. @pytest.mark.platform_x86_ascend_training
  658. @pytest.mark.platform_x86_gpu_training
  659. @pytest.mark.platform_x86_cpu
  660. @pytest.mark.env_onecard
  661. def test_log2():
  662. run_unary_test(mnp_log2, onp_log2, test_case, error=1e-5)
  663. def mnp_logaddexp2(x1, x2):
  664. return mnp.logaddexp2(x1, x2)
  665. def onp_logaddexp2(x1, x2):
  666. return onp.logaddexp2(x1, x2)
  667. @pytest.mark.level1
  668. @pytest.mark.platform_arm_ascend_training
  669. @pytest.mark.platform_x86_ascend_training
  670. @pytest.mark.platform_x86_gpu_training
  671. @pytest.mark.platform_x86_cpu
  672. @pytest.mark.env_onecard
  673. def test_logaddexp2():
  674. test_cases = [
  675. onp.random.randint(1, 5, (2)).astype('float16'),
  676. onp.random.randint(1, 5, (3, 2)).astype('float16'),
  677. onp.random.randint(1, 5, (1, 3, 2)).astype('float16'),
  678. onp.random.randint(1, 5, (5, 6, 3, 2)).astype('float16')]
  679. for _, x1 in enumerate(test_cases):
  680. for _, x2 in enumerate(test_cases):
  681. expected = onp_logaddexp2(x1, x2)
  682. actual = mnp_logaddexp2(to_tensor(x1), to_tensor(x2))
  683. onp.testing.assert_almost_equal(actual.asnumpy().tolist(), expected.tolist(),
  684. decimal=2)
  685. def mnp_log10(x):
  686. return mnp.log10(x)
  687. def onp_log10(x):
  688. return onp.log10(x)
  689. @pytest.mark.level1
  690. @pytest.mark.platform_arm_ascend_training
  691. @pytest.mark.platform_x86_ascend_training
  692. @pytest.mark.platform_x86_gpu_training
  693. @pytest.mark.platform_x86_cpu
  694. @pytest.mark.env_onecard
  695. def test_log10():
  696. run_unary_test(mnp_log10, onp_log10, test_case, error=1e-5)
  697. def mnp_maximum(x1, x2):
  698. return mnp.maximum(x1, x2)
  699. def onp_maximum(x1, x2):
  700. return onp.maximum(x1, x2)
  701. @pytest.mark.level1
  702. @pytest.mark.platform_x86_gpu_training
  703. @pytest.mark.platform_x86_cpu
  704. @pytest.mark.env_onecard
  705. def test_maximum():
  706. run_binop_test(mnp_maximum, onp_maximum, test_case)
  707. x = onp.random.randint(-10, 10, 20).astype(onp.float32)
  708. y = onp.random.randint(-10, 10, 20).astype(onp.float32)
  709. x[onp.random.randint(0, 10, 3)] = onp.nan
  710. y[onp.random.randint(0, 10, 3)] = onp.nan
  711. x[onp.random.randint(0, 10, 3)] = onp.NINF
  712. y[onp.random.randint(0, 10, 3)] = onp.NINF
  713. x[onp.random.randint(0, 10, 3)] = onp.PINF
  714. y[onp.random.randint(0, 10, 3)] = onp.PINF
  715. match_res(mnp_maximum, onp_maximum, x, y)
  716. match_res(mnp_maximum, onp_maximum, y, x)
  717. def mnp_clip(x):
  718. a = mnp.clip(x, to_tensor(10.0), to_tensor([2,]))
  719. b = mnp.clip(x, 0, 1)
  720. c = mnp.clip(x, to_tensor(0), to_tensor(10), dtype=mnp.float32)
  721. return a, b, c
  722. def onp_clip(x):
  723. a = onp.clip(x, onp.asarray(10.0), onp.asarray([2,]))
  724. b = onp.clip(x, 0, 1)
  725. c = onp.clip(x, onp.asarray(0), onp.asarray(10), dtype=onp.float32)
  726. return a, b, c
  727. @pytest.mark.level1
  728. @pytest.mark.platform_arm_ascend_training
  729. @pytest.mark.platform_x86_ascend_training
  730. @pytest.mark.platform_x86_gpu_training
  731. @pytest.mark.platform_x86_cpu
  732. @pytest.mark.env_onecard
  733. def test_clip():
  734. run_unary_test(mnp_clip, onp_clip, test_case)
  735. def mnp_amax(x, mask):
  736. a = mnp.amax(x)
  737. b = mnp.amax(x, axis=-3)
  738. c = mnp.amax(x, keepdims=True)
  739. d = mnp.amax(x, initial=3)
  740. e = mnp.amax(x, axis=(0, 1), keepdims=True)
  741. f = mnp.amax(x, initial=4, where=mask)
  742. g = mnp.amax(x, initial=5, where=mask, keepdims=True)
  743. h = mnp.amax(x, axis=(1, 2, 3), initial=6, where=mask)
  744. return a, b, c, d, e, f, g, h
  745. def onp_amax(x, mask):
  746. a = onp.amax(x)
  747. b = onp.amax(x, axis=-3)
  748. c = onp.amax(x, keepdims=True)
  749. d = onp.amax(x, initial=3)
  750. e = onp.amax(x, axis=(0, 1), keepdims=True)
  751. f = onp.amax(x, initial=4, where=mask)
  752. g = onp.amax(x, initial=5, where=mask, keepdims=True)
  753. h = onp.amax(x, axis=(1, 2, 3), initial=6, where=mask)
  754. return a, b, c, d, e, f, g, h
  755. @pytest.mark.level1
  756. @pytest.mark.platform_arm_ascend_training
  757. @pytest.mark.platform_x86_ascend_training
  758. @pytest.mark.platform_x86_gpu_training
  759. @pytest.mark.platform_x86_cpu
  760. @pytest.mark.env_onecard
  761. def test_amax():
  762. a = rand_int(2, 3, 4, 5).astype('float32')
  763. mask = rand_bool(2, 3, 4, 5)
  764. run_multi_test(mnp_amax, onp_amax, (a, mask))
  765. def mnp_amin(x, mask):
  766. a = mnp.amin(x)
  767. b = mnp.amin(x, axis=-3)
  768. c = mnp.amin(x, keepdims=True)
  769. d = mnp.amin(x, initial=-1)
  770. e = mnp.amin(x, axis=(0, 1), keepdims=True)
  771. f = mnp.amin(x, initial=-2)
  772. g = mnp.amin(x, initial=-3, keepdims=True)
  773. h = mnp.amin(x, axis=(1, 2, 3), initial=-4, where=mask)
  774. return a, b, c, d, e, f, g, h
  775. def onp_amin(x, mask):
  776. a = onp.amin(x)
  777. b = onp.amin(x, axis=-3)
  778. c = onp.amin(x, keepdims=True)
  779. d = onp.amin(x, initial=-1)
  780. e = onp.amin(x, axis=(0, 1), keepdims=True)
  781. f = onp.amin(x, initial=-2)
  782. g = onp.amin(x, initial=-3, keepdims=True)
  783. h = onp.amin(x, axis=(1, 2, 3), initial=-4, where=mask)
  784. return a, b, c, d, e, f, g, h
  785. @pytest.mark.level1
  786. @pytest.mark.platform_arm_ascend_training
  787. @pytest.mark.platform_x86_ascend_training
  788. @pytest.mark.platform_x86_gpu_training
  789. @pytest.mark.platform_x86_cpu
  790. @pytest.mark.env_onecard
  791. def test_amin():
  792. a = rand_int(2, 3, 4, 5).astype('float32')
  793. mask = rand_bool(2, 3, 4, 5)
  794. run_multi_test(mnp_amin, onp_amin, (a, mask))
  795. def mnp_hypot(x1, x2):
  796. return mnp.hypot(x1, x2)
  797. def onp_hypot(x1, x2):
  798. return onp.hypot(x1, x2)
  799. @pytest.mark.level1
  800. @pytest.mark.platform_arm_ascend_training
  801. @pytest.mark.platform_x86_ascend_training
  802. @pytest.mark.platform_x86_gpu_training
  803. @pytest.mark.platform_x86_cpu
  804. @pytest.mark.env_onecard
  805. def test_hypot():
  806. run_binop_test(mnp_hypot, onp_hypot, test_case)
  807. def mnp_heaviside(x1, x2):
  808. return mnp.heaviside(x1, x2)
  809. def onp_heaviside(x1, x2):
  810. return onp.heaviside(x1, x2)
  811. @pytest.mark.level1
  812. @pytest.mark.platform_arm_ascend_training
  813. @pytest.mark.platform_x86_ascend_training
  814. @pytest.mark.platform_x86_gpu_training
  815. @pytest.mark.platform_x86_cpu
  816. @pytest.mark.env_onecard
  817. def test_heaviside():
  818. broadcastables = test_case.broadcastables
  819. for b1 in broadcastables:
  820. for b2 in broadcastables:
  821. b = onp.subtract(b1, b2)
  822. match_res(mnp_heaviside, onp_heaviside, b, b1)
  823. match_res(mnp_heaviside, onp_heaviside, b, b2)
  824. def mnp_floor(x):
  825. return mnp.floor(x)
  826. def onp_floor(x):
  827. return onp.floor(x)
  828. @pytest.mark.level1
  829. @pytest.mark.platform_arm_ascend_training
  830. @pytest.mark.platform_x86_ascend_training
  831. @pytest.mark.platform_x86_gpu_training
  832. @pytest.mark.platform_x86_cpu
  833. @pytest.mark.env_onecard
  834. def test_floor():
  835. run_unary_test(mnp_floor, onp_floor, test_case)
  836. x = rand_int(2, 3) * onp.random.rand(2, 3)
  837. match_res(mnp_floor, onp_floor, x)
  838. match_res(mnp_floor, onp_floor, -x)
  839. def mnp_floor_divide(x, y):
  840. return mnp.floor_divide(x, y)
  841. def onp_floor_divde(x, y):
  842. return onp.floor_divide(x, y)
  843. @pytest.mark.level1
  844. @pytest.mark.platform_arm_ascend_training
  845. @pytest.mark.platform_x86_ascend_training
  846. @pytest.mark.platform_x86_gpu_training
  847. @pytest.mark.platform_x86_cpu
  848. @pytest.mark.env_onecard
  849. def test_floor_divide():
  850. run_binop_test(mnp_floor_divide, onp_floor_divde, test_case)
  851. def mnp_remainder(x, y):
  852. return mnp.remainder(x, y)
  853. def onp_remainder(x, y):
  854. return onp.remainder(x, y)
  855. @pytest.mark.level1
  856. @pytest.mark.platform_arm_ascend_training
  857. @pytest.mark.platform_x86_ascend_training
  858. @pytest.mark.platform_x86_gpu_training
  859. @pytest.mark.platform_x86_cpu
  860. @pytest.mark.env_onecard
  861. def test_remainder():
  862. x = rand_int(2, 3)
  863. y = rand_int(2, 3)
  864. match_res(mnp_remainder, onp_remainder, x, y)
  865. def mnp_mod(x, y):
  866. return mnp.mod(x, y)
  867. def onp_mod(x, y):
  868. return onp.mod(x, y)
  869. @pytest.mark.level1
  870. @pytest.mark.platform_arm_ascend_training
  871. @pytest.mark.platform_x86_ascend_training
  872. @pytest.mark.platform_x86_gpu_training
  873. @pytest.mark.platform_x86_cpu
  874. @pytest.mark.env_onecard
  875. def test_mod():
  876. x = rand_int(2, 3)
  877. y = rand_int(2, 3)
  878. match_res(mnp_mod, onp_mod, x, y)
  879. def mnp_fmod(x, y):
  880. return mnp.fmod(x, y)
  881. def onp_fmod(x, y):
  882. return onp.fmod(x, y)
  883. @pytest.mark.level1
  884. @pytest.mark.platform_x86_gpu_training
  885. @pytest.mark.platform_x86_cpu
  886. @pytest.mark.env_onecard
  887. def test_fmod():
  888. x = rand_int(2, 3)
  889. y = rand_int(2, 3)
  890. match_res(mnp_fmod, onp_fmod, x, y)
  891. def mnp_fix(x):
  892. return mnp.fix(x)
  893. def onp_fix(x):
  894. return onp.fix(x)
  895. @pytest.mark.level1
  896. @pytest.mark.platform_arm_ascend_training
  897. @pytest.mark.platform_x86_ascend_training
  898. @pytest.mark.platform_x86_gpu_training
  899. @pytest.mark.platform_x86_cpu
  900. @pytest.mark.env_onecard
  901. def test_fix():
  902. x = rand_int(2, 3)
  903. y = rand_int(2, 3)
  904. floats = onp.divide(onp.subtract(x, y), y)
  905. match_res(mnp_fix, onp_fix, floats, error=1e-5)
  906. def mnp_trunc(x):
  907. return mnp.trunc(x)
  908. def onp_trunc(x):
  909. return onp.trunc(x)
  910. @pytest.mark.level1
  911. @pytest.mark.platform_arm_ascend_training
  912. @pytest.mark.platform_x86_ascend_training
  913. @pytest.mark.platform_x86_gpu_training
  914. @pytest.mark.platform_x86_cpu
  915. @pytest.mark.env_onecard
  916. def test_trunc():
  917. x = rand_int(2, 3)
  918. y = rand_int(2, 3)
  919. floats = onp.divide(onp.subtract(x, y), y)
  920. match_res(mnp_trunc, onp_trunc, floats, error=1e-5)
  921. def mnp_exp(x):
  922. return mnp.exp(x)
  923. def onp_exp(x):
  924. return onp.exp(x)
  925. @pytest.mark.level1
  926. @pytest.mark.platform_arm_ascend_training
  927. @pytest.mark.platform_x86_ascend_training
  928. @pytest.mark.platform_x86_gpu_training
  929. @pytest.mark.platform_x86_cpu
  930. @pytest.mark.env_onecard
  931. def test_exp():
  932. run_unary_test(mnp_exp, onp_exp, test_case, error=5)
  933. def mnp_expm1(x):
  934. return mnp.expm1(x)
  935. def onp_expm1(x):
  936. return onp.expm1(x)
  937. @pytest.mark.level1
  938. @pytest.mark.platform_arm_ascend_training
  939. @pytest.mark.platform_x86_ascend_training
  940. @pytest.mark.platform_x86_gpu_training
  941. @pytest.mark.platform_x86_cpu
  942. @pytest.mark.env_onecard
  943. def test_expm1():
  944. run_unary_test(mnp_expm1, onp_expm1, test_case, error=5)
  945. def mnp_exp2(x):
  946. return mnp.exp2(x)
  947. def onp_exp2(x):
  948. return onp.exp2(x)
  949. @pytest.mark.level1
  950. @pytest.mark.platform_arm_ascend_training
  951. @pytest.mark.platform_x86_ascend_training
  952. @pytest.mark.platform_x86_gpu_training
  953. @pytest.mark.platform_x86_cpu
  954. @pytest.mark.env_onecard
  955. def test_exp2():
  956. run_unary_test(mnp_exp2, onp_exp2, test_case, error=5)
  957. def mnp_kron(x, y):
  958. return mnp.kron(x, y)
  959. def onp_kron(x, y):
  960. return onp.kron(x, y)
  961. @pytest.mark.level1
  962. @pytest.mark.platform_arm_ascend_training
  963. @pytest.mark.platform_x86_ascend_training
  964. @pytest.mark.platform_x86_gpu_training
  965. @pytest.mark.platform_x86_cpu
  966. @pytest.mark.env_onecard
  967. def test_kron():
  968. run_binop_test(mnp_kron, onp_kron, test_case)
  969. @pytest.mark.level1
  970. @pytest.mark.platform_arm_ascend_training
  971. @pytest.mark.platform_x86_ascend_training
  972. @pytest.mark.platform_x86_gpu_training
  973. @pytest.mark.platform_x86_cpu
  974. @pytest.mark.env_onecard
  975. def test_cross():
  976. x = onp.arange(8).reshape(2, 2, 1, 2)
  977. y = onp.arange(4).reshape(1, 2, 2)
  978. match_res(mnp.cross, onp.cross, x, y)
  979. match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2)
  980. match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2, axis=1)
  981. x = onp.arange(18).reshape(2, 3, 1, 3)
  982. y = onp.arange(9).reshape(1, 3, 3)
  983. match_res(mnp.cross, onp.cross, x, y)
  984. match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2)
  985. match_res(mnp.cross, onp.cross, x, y, axisa=-3, axisb=1, axisc=2, axis=1)
  986. def mnp_ceil(x):
  987. return mnp.ceil(x)
  988. def onp_ceil(x):
  989. return onp.ceil(x)
  990. @pytest.mark.platform_arm_ascend_training
  991. @pytest.mark.platform_x86_ascend_training
  992. @pytest.mark.platform_x86_gpu_training
  993. @pytest.mark.platform_x86_cpu
  994. @pytest.mark.env_onecard
  995. def test_ceil():
  996. run_unary_test(mnp_ceil, onp_ceil, test_case)
  997. def mnp_positive(x):
  998. return mnp.positive(x)
  999. def onp_positive(x):
  1000. return onp.positive(x)
  1001. @pytest.mark.level1
  1002. @pytest.mark.platform_arm_ascend_training
  1003. @pytest.mark.platform_x86_ascend_training
  1004. @pytest.mark.platform_x86_gpu_training
  1005. @pytest.mark.platform_x86_cpu
  1006. @pytest.mark.env_onecard
  1007. def test_positive():
  1008. arr = onp.arange(-6, 6).reshape((2, 2, 3)).astype('float32')
  1009. onp_pos = onp_positive(arr)
  1010. mnp_pos = mnp_positive(to_tensor(arr))
  1011. match_array(mnp_pos.asnumpy(), onp_pos)
  1012. def mnp_negative(x):
  1013. return mnp.negative(x)
  1014. def onp_negative(x):
  1015. return onp.negative(x)
  1016. @pytest.mark.level1
  1017. @pytest.mark.platform_arm_ascend_training
  1018. @pytest.mark.platform_x86_ascend_training
  1019. @pytest.mark.platform_x86_gpu_training
  1020. @pytest.mark.platform_x86_cpu
  1021. @pytest.mark.env_onecard
  1022. def test_negative():
  1023. arr = onp.arange(-6, 6).reshape((2, 2, 3)).astype('float32')
  1024. onp_neg = onp_negative(arr)
  1025. mnp_neg = mnp_negative(to_tensor(arr))
  1026. match_array(mnp_neg.asnumpy(), onp_neg, 1e-5)
  1027. @pytest.mark.level1
  1028. @pytest.mark.platform_arm_ascend_training
  1029. @pytest.mark.platform_x86_ascend_training
  1030. @pytest.mark.platform_x86_gpu_training
  1031. @pytest.mark.platform_x86_cpu
  1032. @pytest.mark.env_onecard
  1033. def test_cumsum():
  1034. x = mnp.ones((16, 16), dtype="bool")
  1035. match_array(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy()))
  1036. match_array(mnp.cumsum(x, axis=0).asnumpy(),
  1037. onp.cumsum(x.asnumpy(), axis=0))
  1038. match_meta(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy()))
  1039. x = rand_int(3, 4, 5)
  1040. match_array(mnp.cumsum(to_tensor(x), dtype="bool").asnumpy(),
  1041. onp.cumsum(x, dtype="bool"))
  1042. match_array(mnp.cumsum(to_tensor(x), axis=-1).asnumpy(),
  1043. onp.cumsum(x, axis=-1))
  1044. @pytest.mark.level1
  1045. @pytest.mark.platform_arm_ascend_training
  1046. @pytest.mark.platform_x86_ascend_training
  1047. @pytest.mark.platform_x86_gpu_training
  1048. @pytest.mark.platform_x86_cpu
  1049. @pytest.mark.env_onecard
  1050. def test_promote_types():
  1051. assert mnp.promote_types(mnp.int32, mnp.bool_) == mnp.int32
  1052. assert mnp.promote_types(int, mnp.bool_) == mnp.int32
  1053. assert mnp.promote_types("float32", mnp.int64) == mnp.float32
  1054. assert mnp.promote_types(mnp.int64, mnp.float16) == mnp.float16
  1055. assert mnp.promote_types(int, float) == mnp.float32
  1056. def mnp_diff(input_tensor):
  1057. a = mnp.diff(input_tensor, 2, append=3.0)
  1058. b = mnp.diff(input_tensor, 4, prepend=6, axis=-2)
  1059. c = mnp.diff(input_tensor, 0, append=3.0, axis=-1)
  1060. d = mnp.diff(input_tensor, 1, prepend=input_tensor)
  1061. e = mnp.ediff1d(input_tensor, to_end=input_tensor)
  1062. f = mnp.ediff1d(input_tensor)
  1063. g = mnp.ediff1d(input_tensor, to_begin=3)
  1064. return a, b, c, d, e, f, g
  1065. def onp_diff(input_array):
  1066. a = onp.diff(input_array, 2, append=3.0)
  1067. b = onp.diff(input_array, 4, prepend=6, axis=-2)
  1068. c = onp.diff(input_array, 0, append=3.0, axis=-1)
  1069. d = onp.diff(input_array, 1, prepend=input_array)
  1070. e = onp.ediff1d(input_array, to_end=input_array)
  1071. f = onp.ediff1d(input_array)
  1072. g = onp.ediff1d(input_array, to_begin=3)
  1073. return a, b, c, d, e, f, g
  1074. @pytest.mark.level1
  1075. @pytest.mark.platform_arm_ascend_training
  1076. @pytest.mark.platform_x86_ascend_training
  1077. @pytest.mark.platform_x86_gpu_training
  1078. @pytest.mark.platform_x86_cpu
  1079. @pytest.mark.env_onecard
  1080. def test_diff():
  1081. arr = rand_int(3, 4, 5)
  1082. match_res(mnp_diff, onp_diff, arr)
  1083. arr = rand_int(1, 4, 6, 3)
  1084. match_res(mnp_diff, onp_diff, arr)
  1085. def mnp_sin(x):
  1086. return mnp.sin(x)
  1087. def onp_sin(x):
  1088. return onp.sin(x)
  1089. @pytest.mark.level1
  1090. @pytest.mark.platform_arm_ascend_training
  1091. @pytest.mark.platform_x86_ascend_training
  1092. @pytest.mark.platform_x86_gpu_training
  1093. @pytest.mark.platform_x86_cpu
  1094. @pytest.mark.env_onecard
  1095. def test_sin():
  1096. arr = onp.random.rand(2, 3, 4).astype('float32')
  1097. expect = onp_sin(arr)
  1098. actual = mnp_sin(to_tensor(arr))
  1099. match_array(actual.asnumpy(), expect, error=5)
  1100. def mnp_cos(x):
  1101. return mnp.cos(x)
  1102. def onp_cos(x):
  1103. return onp.cos(x)
  1104. @pytest.mark.level1
  1105. @pytest.mark.platform_arm_ascend_training
  1106. @pytest.mark.platform_x86_ascend_training
  1107. @pytest.mark.platform_x86_gpu_training
  1108. @pytest.mark.platform_x86_cpu
  1109. @pytest.mark.env_onecard
  1110. def test_cos():
  1111. arr = onp.random.rand(2, 3, 4).astype('float32')
  1112. expect = onp_cos(arr)
  1113. actual = mnp_cos(to_tensor(arr))
  1114. match_array(actual.asnumpy(), expect, error=5)
  1115. def mnp_tan(x):
  1116. return mnp.tan(x)
  1117. def onp_tan(x):
  1118. return onp.tan(x)
  1119. @pytest.mark.level1
  1120. @pytest.mark.platform_arm_ascend_training
  1121. @pytest.mark.platform_x86_ascend_training
  1122. @pytest.mark.platform_x86_cpu
  1123. @pytest.mark.env_onecard
  1124. def test_tan():
  1125. arr = onp.array([-0.75, -0.5, 0, 0.5, 0.75]).astype('float32')
  1126. expect = onp_tan(arr)
  1127. actual = mnp_tan(to_tensor(arr))
  1128. match_array(actual.asnumpy(), expect, error=5)
  1129. def mnp_arcsin(x):
  1130. return mnp.arcsin(x)
  1131. def onp_arcsin(x):
  1132. return onp.arcsin(x)
  1133. @pytest.mark.level1
  1134. @pytest.mark.platform_arm_ascend_training
  1135. @pytest.mark.platform_x86_ascend_training
  1136. @pytest.mark.platform_x86_gpu_training
  1137. @pytest.mark.platform_x86_cpu
  1138. @pytest.mark.env_onecard
  1139. def test_arcsin():
  1140. arr = onp.random.uniform(-1, 1, 12).astype('float32')
  1141. onp_asin = onp_arcsin(arr)
  1142. mnp_asin = mnp_arcsin(to_tensor(arr))
  1143. match_array(mnp_asin.asnumpy(), onp_asin, error=3)
  1144. def mnp_arccos(x):
  1145. return mnp.arccos(x)
  1146. def onp_arccos(x):
  1147. return onp.arccos(x)
  1148. @pytest.mark.level1
  1149. @pytest.mark.platform_arm_ascend_training
  1150. @pytest.mark.platform_x86_ascend_training
  1151. @pytest.mark.platform_x86_gpu_training
  1152. @pytest.mark.platform_x86_cpu
  1153. @pytest.mark.env_onecard
  1154. def test_arccos():
  1155. arr = onp.random.uniform(-1, 1, 12).astype('float32')
  1156. onp_acos = onp_arccos(arr)
  1157. mnp_acos = mnp_arccos(to_tensor(arr))
  1158. match_array(mnp_acos.asnumpy(), onp_acos, error=2)
  1159. def mnp_arctan(x):
  1160. return mnp.arctan(x)
  1161. def onp_arctan(x):
  1162. return onp.arctan(x)
  1163. @pytest.mark.level1
  1164. @pytest.mark.platform_arm_ascend_training
  1165. @pytest.mark.platform_x86_ascend_training
  1166. @pytest.mark.platform_x86_gpu_training
  1167. @pytest.mark.platform_x86_cpu
  1168. @pytest.mark.env_onecard
  1169. def test_arctan():
  1170. arr = onp.random.uniform(-1, 1, 12).astype('float32')
  1171. onp_atan = onp_arctan(arr)
  1172. mnp_atan = mnp_arctan(to_tensor(arr))
  1173. match_array(mnp_atan.asnumpy(), onp_atan, error=5)
  1174. def mnp_sinh(x):
  1175. return mnp.sinh(x)
  1176. def onp_sinh(x):
  1177. return onp.sinh(x)
  1178. @pytest.mark.level1
  1179. @pytest.mark.platform_arm_ascend_training
  1180. @pytest.mark.platform_x86_ascend_training
  1181. @pytest.mark.platform_x86_cpu
  1182. @pytest.mark.env_onecard
  1183. def test_sinh():
  1184. arr = onp.random.rand(2, 3, 4).astype('float32')
  1185. expect = onp_sinh(arr)
  1186. actual = mnp_sinh(to_tensor(arr))
  1187. match_array(actual.asnumpy(), expect, error=5)
  1188. def mnp_cosh(x):
  1189. return mnp.cosh(x)
  1190. def onp_cosh(x):
  1191. return onp.cosh(x)
  1192. @pytest.mark.level1
  1193. @pytest.mark.platform_arm_ascend_training
  1194. @pytest.mark.platform_x86_ascend_training
  1195. @pytest.mark.platform_x86_cpu
  1196. @pytest.mark.env_onecard
  1197. def test_cosh():
  1198. arr = onp.random.rand(2, 3, 4).astype('float32')
  1199. expect = onp_cosh(arr)
  1200. actual = mnp_cosh(to_tensor(arr))
  1201. match_array(actual.asnumpy(), expect, error=5)
  1202. def mnp_tanh(x):
  1203. return mnp.tanh(x)
  1204. def onp_tanh(x):
  1205. return onp.tanh(x)
  1206. @pytest.mark.level1
  1207. @pytest.mark.platform_arm_ascend_training
  1208. @pytest.mark.platform_x86_ascend_training
  1209. @pytest.mark.platform_x86_gpu_training
  1210. @pytest.mark.platform_x86_cpu
  1211. @pytest.mark.env_onecard
  1212. def test_tanh():
  1213. arr = onp.random.rand(2, 3, 4).astype('float32')
  1214. expect = onp_tanh(arr)
  1215. actual = mnp_tanh(to_tensor(arr))
  1216. match_array(actual.asnumpy(), expect, error=5)
  1217. def mnp_arcsinh(x):
  1218. return mnp.arcsinh(x)
  1219. def onp_arcsinh(x):
  1220. return onp.arcsinh(x)
  1221. @pytest.mark.level1
  1222. @pytest.mark.platform_arm_ascend_training
  1223. @pytest.mark.platform_x86_ascend_training
  1224. @pytest.mark.platform_x86_gpu_training
  1225. @pytest.mark.platform_x86_cpu
  1226. @pytest.mark.env_onecard
  1227. def test_arcsinh():
  1228. arr = onp.random.rand(2, 3, 4).astype('float32')
  1229. expect = onp_arcsinh(arr)
  1230. actual = mnp_arcsinh(to_tensor(arr))
  1231. match_array(actual.asnumpy(), expect, error=5)
  1232. def mnp_arccosh(x):
  1233. return mnp.arccosh(x)
  1234. def onp_arccosh(x):
  1235. return onp.arccosh(x)
  1236. @pytest.mark.level1
  1237. @pytest.mark.platform_arm_ascend_training
  1238. @pytest.mark.platform_x86_ascend_training
  1239. @pytest.mark.platform_x86_gpu_training
  1240. @pytest.mark.platform_x86_cpu
  1241. @pytest.mark.env_onecard
  1242. def test_arccosh():
  1243. arr = onp.random.randint(1, 100, size=(2, 3)).astype('float32')
  1244. expect = onp_arccosh(arr)
  1245. actual = mnp_arccosh(to_tensor(arr))
  1246. match_array(actual.asnumpy(), expect, error=5)
  1247. def mnp_arctanh(x):
  1248. return mnp.arctanh(x)
  1249. def onp_arctanh(x):
  1250. return onp.arctanh(x)
  1251. @pytest.mark.level1
  1252. @pytest.mark.platform_arm_ascend_training
  1253. @pytest.mark.platform_x86_ascend_training
  1254. @pytest.mark.platform_x86_cpu
  1255. @pytest.mark.env_onecard
  1256. def test_arctanh():
  1257. arr = onp.random.uniform(-0.9, 1, 10).astype('float32')
  1258. expect = onp_arctanh(arr)
  1259. actual = mnp_arctanh(to_tensor(arr))
  1260. match_array(actual.asnumpy(), expect, error=5)
  1261. def mnp_arctan2(x, y):
  1262. return mnp.arctan2(x, y)
  1263. def onp_arctan2(x, y):
  1264. return onp.arctan2(x, y)
  1265. @pytest.mark.level1
  1266. @pytest.mark.platform_arm_ascend_training
  1267. @pytest.mark.platform_x86_ascend_training
  1268. @pytest.mark.platform_x86_cpu
  1269. @pytest.mark.env_onecard
  1270. def test_arctan2():
  1271. run_binop_test(mnp_arctan2, onp_arctan2, test_case, error=5)
  1272. def mnp_convolve(mode):
  1273. a = mnp.convolve([1, 2, 3, 4, 5], 2, mode=mode)
  1274. b = mnp.convolve([1, 2, 3, 4, 5], [2, 3], mode=mode)
  1275. c = mnp.convolve([1, 2], [2, 5, 10], mode=mode)
  1276. d = mnp.convolve(mnp.array([1, 2, 3, 4, 5]), mnp.array([1, 2, 3, 4, 5]), mode=mode)
  1277. e = mnp.convolve([1, 2, 3, 4, 5], 2, mode=mode)
  1278. return a, b, c, d, e
  1279. def onp_convolve(mode):
  1280. a = onp.convolve([1, 2, 3, 4, 5], 2, mode=mode)
  1281. b = onp.convolve([1, 2, 3, 4, 5], [2, 3], mode=mode)
  1282. c = onp.convolve([1, 2], [2, 5, 10], mode=mode)
  1283. d = onp.convolve(onp.array([1, 2, 3, 4, 5]), onp.array([1, 2, 3, 4, 5]), mode=mode)
  1284. e = onp.convolve([1, 2, 3, 4, 5], 2, mode=mode)
  1285. return a, b, c, d, e
  1286. @pytest.mark.level1
  1287. @pytest.mark.platform_x86_gpu_training
  1288. @pytest.mark.env_onecard
  1289. def test_convolve():
  1290. for mode in ['full', 'same', 'valid']:
  1291. mnp_res = mnp_convolve(mode)
  1292. onp_res = onp_convolve(mode)
  1293. match_all_arrays(mnp_res, onp_res)
  1294. @pytest.mark.level1
  1295. @pytest.mark.platform_arm_ascend_training
  1296. @pytest.mark.platform_x86_ascend_training
  1297. @pytest.mark.platform_x86_gpu_training
  1298. @pytest.mark.platform_x86_cpu
  1299. @pytest.mark.env_onecard
  1300. def test_cov():
  1301. x = onp.random.random((3, 4)).tolist()
  1302. mnp_res = mnp.cov(x)
  1303. onp_res = onp.cov(x)
  1304. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1305. mnp_res = mnp.cov(x[0])
  1306. onp_res = onp.cov(x[0])
  1307. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1308. w1 = [0, 1, 2, 3]
  1309. w2 = [4, 5, 6, 7]
  1310. mnp_res = mnp.cov(x, fweights=w1)
  1311. onp_res = onp.cov(x, fweights=w1)
  1312. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1313. mnp_res = mnp.cov(x, aweights=w2)
  1314. onp_res = onp.cov(x, aweights=w2)
  1315. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1316. mnp_res = mnp.cov(x, fweights=w1, aweights=w2)
  1317. onp_res = onp.cov(x, fweights=w1, aweights=w2)
  1318. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1319. mnp_res = mnp.cov(x, fweights=w1, aweights=w2, ddof=3)
  1320. onp_res = onp.cov(x, fweights=w1, aweights=w2, ddof=3)
  1321. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1322. mnp_res = mnp.cov(x, fweights=w1, aweights=w2, bias=True)
  1323. onp_res = onp.cov(x, fweights=w1, aweights=w2, bias=True)
  1324. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1325. mnp_res = mnp.cov(x, fweights=w1[0:3], aweights=w2[0:3], rowvar=False, bias=True)
  1326. onp_res = onp.cov(x, fweights=w1[0:3], aweights=w2[0:3], rowvar=False, bias=True)
  1327. match_all_arrays(mnp_res, onp_res, error=1e-5)
  1328. @pytest.mark.level1
  1329. @pytest.mark.platform_arm_ascend_training
  1330. @pytest.mark.platform_x86_ascend_training
  1331. @pytest.mark.platform_x86_gpu_training
  1332. @pytest.mark.platform_x86_cpu
  1333. @pytest.mark.env_onecard
  1334. def test_trapz():
  1335. y = rand_int(2, 3, 4, 5)
  1336. match_res(mnp.trapz, onp.trapz, y)
  1337. match_res(mnp.trapz, onp.trapz, y, x=[-5, -3, 0, 7, 10])
  1338. match_res(mnp.trapz, onp.trapz, y, dx=2, axis=3)
  1339. match_res(mnp.trapz, onp.trapz, y, x=[1, 5, 6, 9], dx=3, axis=-2)
  1340. def mnp_gcd(x, y):
  1341. return mnp.gcd(x, y)
  1342. def onp_gcd(x, y):
  1343. return onp.gcd(x, y)
  1344. @pytest.mark.level1
  1345. @pytest.mark.platform_arm_ascend_training
  1346. @pytest.mark.platform_x86_ascend_training
  1347. @pytest.mark.platform_x86_gpu_training
  1348. @pytest.mark.platform_x86_cpu
  1349. @pytest.mark.env_onecard
  1350. def test_gcd():
  1351. x = onp.arange(-12, 12).reshape(2, 3, 4)
  1352. y = onp.arange(24).reshape(2, 3, 4)
  1353. match_res(mnp_gcd, onp_gcd, x, y)
  1354. def mnp_lcm(x, y):
  1355. return mnp.lcm(x, y)
  1356. def onp_lcm(x, y):
  1357. return onp.lcm(x, y)
  1358. @pytest.mark.level1
  1359. @pytest.mark.platform_arm_ascend_training
  1360. @pytest.mark.platform_x86_ascend_training
  1361. @pytest.mark.platform_x86_gpu_training
  1362. @pytest.mark.platform_x86_cpu
  1363. @pytest.mark.env_onecard
  1364. def test_lcm():
  1365. x = onp.arange(-12, 12).reshape(2, 3, 4)
  1366. y = onp.arange(24).reshape(2, 3, 4)
  1367. match_res(mnp_lcm, onp_lcm, x, y)
  1368. def mnp_nansum(x):
  1369. a = mnp.nansum(x)
  1370. b = mnp.nansum(x, keepdims=True)
  1371. c = mnp.nansum(x, axis=-2)
  1372. d = mnp.nansum(x, axis=0, keepdims=True)
  1373. e = mnp.nansum(x, axis=(-2, 3))
  1374. f = mnp.nansum(x, axis=(-3, -1), keepdims=True)
  1375. return a, b, c, d, e, f
  1376. def onp_nansum(x):
  1377. a = onp.nansum(x)
  1378. b = onp.nansum(x, keepdims=True)
  1379. c = onp.nansum(x, axis=-2)
  1380. d = onp.nansum(x, axis=0, keepdims=True)
  1381. e = onp.nansum(x, axis=(-2, 3))
  1382. f = onp.nansum(x, axis=(-3, -1), keepdims=True)
  1383. return a, b, c, d, e, f
  1384. @pytest.mark.level1
  1385. @pytest.mark.platform_x86_gpu_training
  1386. @pytest.mark.platform_x86_cpu
  1387. @pytest.mark.env_onecard
  1388. def test_nansum():
  1389. x = rand_int(2, 3, 4, 5)
  1390. x[0][2][1][3] = onp.nan
  1391. x[1][0][2][4] = onp.nan
  1392. x[1][1][1][1] = onp.nan
  1393. run_multi_test(mnp_nansum, onp_nansum, (x,))
  1394. def mnp_nanmean(x):
  1395. a = mnp.nanmean(x)
  1396. b = mnp.nanmean(x, keepdims=True)
  1397. c = mnp.nanmean(x, axis=-2)
  1398. d = mnp.nanmean(x, axis=0, keepdims=True)
  1399. e = mnp.nanmean(x, axis=(-2, 3))
  1400. f = mnp.nanmean(x, axis=(-3, -1), keepdims=True)
  1401. return a, b, c, d, e, f
  1402. def onp_nanmean(x):
  1403. a = onp.nanmean(x)
  1404. b = onp.nanmean(x, keepdims=True)
  1405. c = onp.nanmean(x, axis=-2)
  1406. d = onp.nanmean(x, axis=0, keepdims=True)
  1407. e = onp.nanmean(x, axis=(-2, 3))
  1408. f = onp.nanmean(x, axis=(-3, -1), keepdims=True)
  1409. return a, b, c, d, e, f
  1410. @pytest.mark.level1
  1411. @pytest.mark.platform_x86_gpu_training
  1412. @pytest.mark.platform_x86_cpu
  1413. @pytest.mark.env_onecard
  1414. def test_nanmean():
  1415. x = rand_int(2, 3, 4, 5)
  1416. x[0][2][1][3] = onp.nan
  1417. x[1][0][2][4] = onp.nan
  1418. x[1][1][1][1] = onp.nan
  1419. run_multi_test(mnp_nanmean, onp_nanmean, (x,))
  1420. def mnp_mean(*arrs):
  1421. arr1 = arrs[0]
  1422. arr2 = arrs[1]
  1423. arr3 = arrs[2]
  1424. a = mnp.mean(arr1)
  1425. b = mnp.mean(arr2, keepdims=True)
  1426. c = mnp.mean(arr3, keepdims=False)
  1427. d = mnp.mean(arr2, axis=0, keepdims=True)
  1428. e = mnp.mean(arr3, axis=(0, -1))
  1429. f = mnp.mean(arr3, axis=-1, keepdims=True)
  1430. return a, b, c, d, e, f
  1431. def onp_mean(*arrs):
  1432. arr1 = arrs[0]
  1433. arr2 = arrs[1]
  1434. arr3 = arrs[2]
  1435. a = onp.mean(arr1)
  1436. b = onp.mean(arr2, keepdims=True)
  1437. c = onp.mean(arr3, keepdims=False)
  1438. d = onp.mean(arr2, axis=0, keepdims=True)
  1439. e = onp.mean(arr3, axis=(0, -1))
  1440. f = onp.mean(arr3, axis=-1, keepdims=True)
  1441. return a, b, c, d, e, f
  1442. @pytest.mark.level1
  1443. @pytest.mark.platform_arm_ascend_training
  1444. @pytest.mark.platform_x86_ascend_training
  1445. @pytest.mark.platform_x86_gpu_training
  1446. @pytest.mark.platform_x86_cpu
  1447. @pytest.mark.env_onecard
  1448. def test_mean():
  1449. run_multi_test(mnp_mean, onp_mean, test_case.arrs, error=3)
  1450. run_multi_test(mnp_mean, onp_mean, test_case.expanded_arrs, error=3)
  1451. run_multi_test(mnp_mean, onp_mean, test_case.scalars, error=3)
  1452. @pytest.mark.level1
  1453. @pytest.mark.platform_arm_ascend_training
  1454. @pytest.mark.platform_x86_ascend_training
  1455. @pytest.mark.platform_x86_gpu_training
  1456. @pytest.mark.platform_x86_cpu
  1457. @pytest.mark.env_onecard
  1458. def test_exception_innner():
  1459. with pytest.raises(ValueError):
  1460. mnp.inner(to_tensor(test_case.arrs[0]),
  1461. to_tensor(test_case.arrs[1]))
  1462. @pytest.mark.level1
  1463. @pytest.mark.platform_arm_ascend_training
  1464. @pytest.mark.platform_x86_ascend_training
  1465. @pytest.mark.platform_x86_gpu_training
  1466. @pytest.mark.platform_x86_cpu
  1467. @pytest.mark.env_onecard
  1468. def test_exception_add():
  1469. with pytest.raises(ValueError):
  1470. mnp.add(to_tensor(test_case.arrs[1]), to_tensor(test_case.arrs[2]))
  1471. @pytest.mark.level1
  1472. @pytest.mark.platform_arm_ascend_training
  1473. @pytest.mark.platform_x86_ascend_training
  1474. @pytest.mark.platform_x86_gpu_training
  1475. @pytest.mark.platform_x86_cpu
  1476. @pytest.mark.env_onecard
  1477. def test_exception_mean():
  1478. with pytest.raises(ValueError):
  1479. mnp.mean(to_tensor(test_case.arrs[0]), (-1, 0))
  1480. @pytest.mark.level1
  1481. @pytest.mark.platform_arm_ascend_training
  1482. @pytest.mark.platform_x86_ascend_training
  1483. @pytest.mark.platform_x86_gpu_training
  1484. @pytest.mark.platform_x86_cpu
  1485. @pytest.mark.env_onecard
  1486. def test_exception_amax():
  1487. with pytest.raises(TypeError):
  1488. mnp.amax(mnp.array([[1, 2], [3, 4]]).astype(mnp.float32), initial=[1.0, 2.0])