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

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