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