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