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_array_creations.py 28 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 array operations"""
  16. import pytest
  17. import numpy as onp
  18. import mindspore.numpy as mnp
  19. from .utils import rand_int, rand_bool, match_array, match_res, match_meta, \
  20. match_all_arrays
  21. class Cases():
  22. def __init__(self):
  23. self.all_shapes = [
  24. 0, 1, 2, (), (1,), (2,), (1, 2, 3), [], [1], [2], [1, 2, 3]
  25. ]
  26. self.onp_dtypes = [onp.int32, 'int32', int,
  27. onp.float32, 'float32', float,
  28. onp.uint32, 'uint32',
  29. onp.bool_, 'bool', bool]
  30. self.mnp_dtypes = [mnp.int32, 'int32', int,
  31. mnp.float32, 'float32', float,
  32. mnp.uint32, 'uint32',
  33. mnp.bool_, 'bool', bool]
  34. self.array_sets = [1, 1.1, True, [1, 0, True], [1, 1.0, 2], (1,),
  35. [(1, 2, 3), (4, 5, 6)], onp.random.random( # pylint: disable=no-member
  36. (100, 100)).astype(onp.float32),
  37. onp.random.random((100, 100)).astype(onp.bool)]
  38. self.arrs = [
  39. rand_int(2),
  40. rand_int(2, 3),
  41. rand_int(2, 3, 4),
  42. rand_int(2, 3, 4, 5),
  43. ]
  44. # scalars expanded across the 0th dimension
  45. self.scalars = [
  46. rand_int(),
  47. rand_int(1),
  48. rand_int(1, 1),
  49. rand_int(1, 1, 1),
  50. ]
  51. # arrays of the same size expanded across the 0th dimension
  52. self.expanded_arrs = [
  53. rand_int(2, 3),
  54. rand_int(1, 2, 3),
  55. rand_int(1, 1, 2, 3),
  56. rand_int(1, 1, 1, 2, 3),
  57. ]
  58. # arrays with dimensions of size 1
  59. self.nested_arrs = [
  60. rand_int(1),
  61. rand_int(1, 2),
  62. rand_int(3, 1, 8),
  63. rand_int(1, 3, 9, 1),
  64. ]
  65. # arrays which can be broadcast
  66. self.broadcastables = [
  67. rand_int(5),
  68. rand_int(6, 1),
  69. rand_int(7, 1, 5),
  70. rand_int(8, 1, 6, 1)
  71. ]
  72. # boolean arrays which can be broadcast
  73. self.bool_broadcastables = [
  74. rand_bool(),
  75. rand_bool(1),
  76. rand_bool(5),
  77. rand_bool(6, 1),
  78. rand_bool(7, 1, 5),
  79. rand_bool(8, 1, 6, 1),
  80. ]
  81. self.mnp_prototypes = [
  82. mnp.ones((2, 3, 4)),
  83. mnp.ones((0, 3, 0, 2, 5)),
  84. mnp.ones((2, 7, 0)),
  85. mnp.ones(()),
  86. [mnp.ones(3), (1, 2, 3), mnp.ones(3), [4, 5, 6]],
  87. ([(1, 2), mnp.ones(2)], (mnp.ones(2), [3, 4])),
  88. ]
  89. self.onp_prototypes = [
  90. onp.ones((2, 3, 4)),
  91. onp.ones((0, 3, 0, 2, 5)),
  92. onp.ones((2, 7, 0)),
  93. onp.ones(()),
  94. [onp.ones(3), (1, 2, 3), onp.ones(3), [4, 5, 6]],
  95. ([(1, 2), onp.ones(2)], (onp.ones(2), [3, 4])),
  96. ]
  97. @pytest.mark.level1
  98. @pytest.mark.platform_arm_ascend_training
  99. @pytest.mark.platform_x86_ascend_training
  100. @pytest.mark.platform_x86_gpu_training
  101. @pytest.mark.platform_x86_cpu
  102. @pytest.mark.env_onecard
  103. def test_asarray():
  104. test_case = Cases()
  105. for array in test_case.array_sets:
  106. # Check for dtype matching
  107. actual = onp.asarray(array)
  108. expected = mnp.asarray(array).asnumpy()
  109. # Since we set float32/int32 as the default dtype in mindspore, we need
  110. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  111. if actual.dtype is onp.dtype('float64'):
  112. assert expected.dtype == onp.dtype('float32')
  113. elif actual.dtype is onp.dtype('int64'):
  114. assert expected.dtype == onp.dtype('int32')
  115. else:
  116. assert actual.dtype == expected.dtype
  117. match_array(actual, expected, error=7)
  118. for i in range(len(test_case.onp_dtypes)):
  119. actual = onp.asarray(array, test_case.onp_dtypes[i])
  120. expected = mnp.asarray(array, test_case.mnp_dtypes[i]).asnumpy()
  121. match_array(actual, expected, error=7)
  122. # Additional tests for nested tensor/numpy_array mixture
  123. mnp_input = [(onp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  124. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  125. actual = onp.asarray(onp_input)
  126. expected = mnp.asarray(mnp_input).asnumpy()
  127. match_array(actual, expected, error=7)
  128. @pytest.mark.level1
  129. @pytest.mark.platform_arm_ascend_training
  130. @pytest.mark.platform_x86_ascend_training
  131. @pytest.mark.platform_x86_gpu_training
  132. @pytest.mark.platform_x86_cpu
  133. @pytest.mark.env_onecard
  134. def test_array():
  135. # array's function is very similar to asarray, so we mainly test the
  136. # `copy` argument.
  137. test_case = Cases()
  138. for array in test_case.array_sets:
  139. arr1 = mnp.asarray(array)
  140. arr2 = mnp.array(arr1, copy=False)
  141. arr3 = mnp.array(arr1)
  142. arr4 = mnp.asarray(array, dtype='int32')
  143. arr5 = mnp.asarray(arr4, dtype=mnp.int32)
  144. assert arr1 is arr2
  145. assert arr1 is not arr3
  146. assert arr4 is arr5
  147. # Additional tests for nested tensor/numpy_array mixture
  148. mnp_input = [(onp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  149. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  150. actual = onp.asarray(onp_input)
  151. expected = mnp.asarray(mnp_input).asnumpy()
  152. match_array(actual, expected, error=7)
  153. @pytest.mark.level1
  154. @pytest.mark.platform_arm_ascend_training
  155. @pytest.mark.platform_x86_ascend_training
  156. @pytest.mark.platform_x86_gpu_training
  157. @pytest.mark.platform_x86_cpu
  158. @pytest.mark.env_onecard
  159. def test_asfarray():
  160. test_case = Cases()
  161. for array in test_case.array_sets:
  162. # Check for dtype matching
  163. actual = onp.asfarray(array)
  164. expected = mnp.asfarray(array).asnumpy()
  165. # Since we set float32/int32 as the default dtype in mindspore, we need
  166. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  167. if actual.dtype is onp.dtype('float64'):
  168. assert expected.dtype == onp.dtype('float32')
  169. else:
  170. assert actual.dtype == expected.dtype
  171. match_array(actual, expected, error=7)
  172. for i in range(len(test_case.onp_dtypes)):
  173. actual = onp.asfarray(array, test_case.onp_dtypes[i])
  174. expected = mnp.asfarray(array, test_case.mnp_dtypes[i]).asnumpy()
  175. match_array(actual, expected, error=7)
  176. # Additional tests for nested tensor/numpy_array mixture
  177. mnp_input = [(onp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  178. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  179. actual = onp.asarray(onp_input)
  180. expected = mnp.asarray(mnp_input).asnumpy()
  181. match_array(actual, expected, error=7)
  182. @pytest.mark.level1
  183. @pytest.mark.platform_arm_ascend_training
  184. @pytest.mark.platform_x86_ascend_training
  185. @pytest.mark.platform_x86_gpu_training
  186. @pytest.mark.platform_x86_cpu
  187. @pytest.mark.env_onecard
  188. def test_zeros():
  189. test_case = Cases()
  190. for shape in test_case.all_shapes:
  191. for i in range(len(test_case.onp_dtypes)):
  192. actual = onp.zeros(shape, test_case.onp_dtypes[i])
  193. expected = mnp.zeros(shape, test_case.mnp_dtypes[i]).asnumpy()
  194. match_array(actual, expected)
  195. actual = onp.zeros(shape)
  196. expected = mnp.zeros(shape).asnumpy()
  197. match_array(actual, expected)
  198. @pytest.mark.level1
  199. @pytest.mark.platform_arm_ascend_training
  200. @pytest.mark.platform_x86_ascend_training
  201. @pytest.mark.platform_x86_gpu_training
  202. @pytest.mark.platform_x86_cpu
  203. @pytest.mark.env_onecard
  204. def test_ones():
  205. test_case = Cases()
  206. for shape in test_case.all_shapes:
  207. for i in range(len(test_case.onp_dtypes)):
  208. actual = onp.ones(shape, test_case.onp_dtypes[i])
  209. expected = mnp.ones(shape, test_case.mnp_dtypes[i]).asnumpy()
  210. match_array(actual, expected)
  211. actual = onp.ones(shape)
  212. expected = mnp.ones(shape).asnumpy()
  213. match_array(actual, expected)
  214. @pytest.mark.level1
  215. @pytest.mark.platform_arm_ascend_training
  216. @pytest.mark.platform_x86_ascend_training
  217. @pytest.mark.platform_x86_gpu_training
  218. @pytest.mark.platform_x86_cpu
  219. @pytest.mark.env_onecard
  220. def test_full():
  221. actual = onp.full((2, 2), [1, 2])
  222. expected = mnp.full((2, 2), [1, 2]).asnumpy()
  223. match_array(actual, expected)
  224. actual = onp.full((2, 0), onp.inf)
  225. expected = mnp.full((2, 0), mnp.inf).asnumpy()
  226. match_array(actual, expected)
  227. actual = onp.full((2, 3), True)
  228. expected = mnp.full((2, 3), True).asnumpy()
  229. match_array(actual, expected)
  230. actual = onp.full((3, 4, 5), 7.5)
  231. expected = mnp.full((3, 4, 5), 7.5).asnumpy()
  232. match_array(actual, expected)
  233. @pytest.mark.level1
  234. @pytest.mark.platform_arm_ascend_training
  235. @pytest.mark.platform_x86_ascend_training
  236. @pytest.mark.platform_x86_gpu_training
  237. @pytest.mark.platform_x86_cpu
  238. @pytest.mark.env_onecard
  239. def test_eye():
  240. test_case = Cases()
  241. for i in range(len(test_case.onp_dtypes)):
  242. for m in range(1, 5):
  243. actual = onp.eye(m, dtype=test_case.onp_dtypes[i])
  244. expected = mnp.eye(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
  245. match_array(actual, expected)
  246. for n in range(1, 5):
  247. for k in range(0, 5):
  248. actual = onp.eye(m, n, k, dtype=test_case.onp_dtypes[i])
  249. expected = mnp.eye(
  250. m, n, k, dtype=test_case.mnp_dtypes[i]).asnumpy()
  251. match_array(actual, expected)
  252. @pytest.mark.level1
  253. @pytest.mark.platform_arm_ascend_training
  254. @pytest.mark.platform_x86_ascend_training
  255. @pytest.mark.platform_x86_gpu_training
  256. @pytest.mark.platform_x86_cpu
  257. @pytest.mark.env_onecard
  258. def test_identity():
  259. test_case = Cases()
  260. for i in range(len(test_case.onp_dtypes)):
  261. for m in range(1, 5):
  262. actual = onp.identity(m, dtype=test_case.onp_dtypes[i])
  263. expected = mnp.identity(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
  264. match_array(actual, expected)
  265. @pytest.mark.level1
  266. @pytest.mark.platform_arm_ascend_training
  267. @pytest.mark.platform_x86_ascend_training
  268. @pytest.mark.platform_x86_gpu_training
  269. @pytest.mark.platform_x86_cpu
  270. @pytest.mark.env_onecard
  271. def test_arange():
  272. actual = onp.arange(10)
  273. expected = mnp.arange(10).asnumpy()
  274. match_array(actual, expected)
  275. actual = onp.arange(0, 10)
  276. expected = mnp.arange(0, 10).asnumpy()
  277. match_array(actual, expected)
  278. actual = onp.arange(start=10)
  279. expected = mnp.arange(start=10).asnumpy()
  280. match_array(actual, expected)
  281. actual = onp.arange(start=10, step=0.1)
  282. expected = mnp.arange(start=10, step=0.1).asnumpy()
  283. match_array(actual, expected, error=6)
  284. actual = onp.arange(10, step=0.1)
  285. expected = mnp.arange(10, step=0.1).asnumpy()
  286. match_array(actual, expected, error=6)
  287. actual = onp.arange(0.1, 9.9)
  288. expected = mnp.arange(0.1, 9.9).asnumpy()
  289. match_array(actual, expected, error=6)
  290. @pytest.mark.level1
  291. @pytest.mark.platform_arm_ascend_training
  292. @pytest.mark.platform_x86_ascend_training
  293. @pytest.mark.platform_x86_gpu_training
  294. @pytest.mark.platform_x86_cpu
  295. @pytest.mark.env_onecard
  296. def test_linspace():
  297. actual = onp.linspace(2.0, 3.0, dtype=onp.float32)
  298. expected = mnp.linspace(2.0, 3.0).asnumpy()
  299. match_array(actual, expected, error=6)
  300. actual = onp.linspace(2.0, 3.0, num=5, dtype=onp.float32)
  301. expected = mnp.linspace(2.0, 3.0, num=5).asnumpy()
  302. match_array(actual, expected, error=6)
  303. actual = onp.linspace(
  304. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  305. expected = mnp.linspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  306. match_array(actual, expected, error=6)
  307. actual = onp.linspace(2.0, 3.0, num=5, retstep=True, dtype=onp.float32)
  308. expected = mnp.linspace(2.0, 3.0, num=5, retstep=True)
  309. match_array(actual[0], expected[0].asnumpy())
  310. assert actual[1] == expected[1].asnumpy()
  311. actual = onp.linspace(2.0, [3, 4, 5], num=5,
  312. endpoint=False, dtype=onp.float32)
  313. expected = mnp.linspace(
  314. 2.0, [3, 4, 5], num=5, endpoint=False).asnumpy()
  315. match_array(actual, expected, error=6)
  316. start = onp.random.random([2, 1, 4]).astype("float32")
  317. stop = onp.random.random([1, 5, 1]).astype("float32")
  318. actual = onp.linspace(start, stop, num=20, retstep=True,
  319. endpoint=False, dtype=onp.float32)
  320. expected = mnp.linspace(mnp.asarray(start), mnp.asarray(stop), num=20,
  321. retstep=True, endpoint=False)
  322. match_array(actual[0], expected[0].asnumpy(), error=6)
  323. match_array(actual[1], expected[1].asnumpy(), error=6)
  324. actual = onp.linspace(start, stop, num=20, retstep=True,
  325. endpoint=False, dtype=onp.int16)
  326. expected = mnp.linspace(mnp.asarray(start), mnp.asarray(stop), num=20,
  327. retstep=True, endpoint=False, dtype=mnp.int16)
  328. match_array(actual[0], expected[0].asnumpy(), error=6)
  329. match_array(actual[1], expected[1].asnumpy(), error=6)
  330. for axis in range(2):
  331. actual = onp.linspace(start, stop, num=20, retstep=False,
  332. endpoint=False, dtype=onp.float32, axis=axis)
  333. expected = mnp.linspace(mnp.asarray(start), mnp.asarray(stop), num=20,
  334. retstep=False, endpoint=False, dtype=mnp.float32, axis=axis)
  335. match_array(actual, expected.asnumpy(), error=6)
  336. @pytest.mark.level1
  337. @pytest.mark.platform_arm_ascend_training
  338. @pytest.mark.platform_x86_ascend_training
  339. @pytest.mark.platform_x86_gpu_training
  340. @pytest.mark.platform_x86_cpu
  341. @pytest.mark.env_onecard
  342. def test_logspace():
  343. actual = onp.logspace(2.0, 3.0, dtype=onp.float32)
  344. expected = mnp.logspace(2.0, 3.0).asnumpy()
  345. match_array(actual, expected, error=3)
  346. actual = onp.logspace(2.0, 3.0, num=5, dtype=onp.float32)
  347. expected = mnp.logspace(2.0, 3.0, num=5).asnumpy()
  348. match_array(actual, expected, error=3)
  349. actual = onp.logspace(
  350. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  351. expected = mnp.logspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  352. match_array(actual, expected, error=3)
  353. actual = onp.logspace(2.0, [3, 4, 5], num=5, base=2,
  354. endpoint=False, dtype=onp.float32)
  355. expected = mnp.logspace(
  356. 2.0, [3, 4, 5], num=5, base=2, endpoint=False).asnumpy()
  357. match_array(actual, expected, error=3)
  358. @pytest.mark.level1
  359. @pytest.mark.platform_arm_ascend_training
  360. @pytest.mark.platform_x86_ascend_training
  361. @pytest.mark.platform_x86_gpu_training
  362. @pytest.mark.platform_x86_cpu
  363. @pytest.mark.env_onecard
  364. def test_empty():
  365. test_case = Cases()
  366. for shape in test_case.all_shapes:
  367. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  368. test_case.onp_dtypes):
  369. actual = mnp.empty(shape, mnp_dtype).asnumpy()
  370. expected = onp.empty(shape, onp_dtype)
  371. match_meta(actual, expected)
  372. @pytest.mark.level1
  373. @pytest.mark.platform_arm_ascend_training
  374. @pytest.mark.platform_x86_ascend_training
  375. @pytest.mark.platform_x86_gpu_training
  376. @pytest.mark.platform_x86_cpu
  377. @pytest.mark.env_onecard
  378. def test_empty_like():
  379. test_case = Cases()
  380. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  381. actual = mnp.empty_like(mnp_proto).asnumpy()
  382. expected = onp.empty_like(onp_proto)
  383. assert actual.shape == expected.shape
  384. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  385. test_case.onp_dtypes):
  386. actual = mnp.empty_like(mnp_proto, dtype=mnp_dtype).asnumpy()
  387. expected = onp.empty_like(onp_proto, dtype=onp_dtype)
  388. match_meta(actual, expected)
  389. def run_x_like(mnp_fn, onp_fn):
  390. test_case = Cases()
  391. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  392. actual = mnp_fn(mnp_proto).asnumpy()
  393. expected = onp_fn(onp_proto)
  394. match_array(actual, expected)
  395. for shape in test_case.all_shapes:
  396. actual = mnp_fn(mnp_proto, shape=shape).asnumpy()
  397. expected = onp_fn(onp_proto, shape=shape)
  398. match_array(actual, expected)
  399. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  400. test_case.onp_dtypes):
  401. actual = mnp_fn(mnp_proto, dtype=mnp_dtype).asnumpy()
  402. expected = onp_fn(onp_proto, dtype=onp_dtype)
  403. match_array(actual, expected)
  404. actual = mnp_fn(mnp_proto, dtype=mnp_dtype,
  405. shape=shape).asnumpy()
  406. expected = onp_fn(onp_proto, dtype=onp_dtype, shape=shape)
  407. match_array(actual, expected)
  408. @pytest.mark.level1
  409. @pytest.mark.platform_arm_ascend_training
  410. @pytest.mark.platform_x86_ascend_training
  411. @pytest.mark.platform_x86_gpu_training
  412. @pytest.mark.platform_x86_cpu
  413. @pytest.mark.env_onecard
  414. def test_ones_like():
  415. run_x_like(mnp.ones_like, onp.ones_like)
  416. @pytest.mark.level1
  417. @pytest.mark.platform_arm_ascend_training
  418. @pytest.mark.platform_x86_ascend_training
  419. @pytest.mark.platform_x86_gpu_training
  420. @pytest.mark.platform_x86_cpu
  421. @pytest.mark.env_onecard
  422. def test_zeros_like():
  423. run_x_like(mnp.zeros_like, onp.zeros_like)
  424. @pytest.mark.level1
  425. @pytest.mark.platform_arm_ascend_training
  426. @pytest.mark.platform_x86_ascend_training
  427. @pytest.mark.platform_x86_gpu_training
  428. @pytest.mark.platform_x86_cpu
  429. @pytest.mark.env_onecard
  430. def test_full_like():
  431. test_case = Cases()
  432. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  433. shape = onp.zeros_like(onp_proto).shape
  434. fill_value = rand_int()
  435. actual = mnp.full_like(mnp_proto, mnp.array(fill_value)).asnumpy()
  436. expected = onp.full_like(onp_proto, fill_value)
  437. match_array(actual, expected)
  438. for i in range(len(shape) - 1, 0, -1):
  439. fill_value = rand_int(*shape[i:])
  440. actual = mnp.full_like(mnp_proto, mnp.array(fill_value)).asnumpy()
  441. expected = onp.full_like(onp_proto, fill_value)
  442. match_array(actual, expected)
  443. fill_value = rand_int(1, *shape[i + 1:])
  444. actual = mnp.full_like(mnp_proto, mnp.array(fill_value)).asnumpy()
  445. expected = onp.full_like(onp_proto, fill_value)
  446. match_array(actual, expected)
  447. @pytest.mark.level1
  448. @pytest.mark.platform_arm_ascend_training
  449. @pytest.mark.platform_x86_ascend_training
  450. @pytest.mark.platform_x86_gpu_training
  451. @pytest.mark.platform_x86_cpu
  452. @pytest.mark.env_onecard
  453. def test_tri_triu_tril():
  454. x = mnp.ones((16, 32), dtype="bool")
  455. match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
  456. match_array(mnp.tril(x, -1).asnumpy(), onp.tril(x.asnumpy(), -1))
  457. match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
  458. match_array(mnp.triu(x, -1).asnumpy(), onp.triu(x.asnumpy(), -1))
  459. x = mnp.ones((64, 64), dtype="uint8")
  460. match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
  461. match_array(mnp.tril(x, 25).asnumpy(), onp.tril(x.asnumpy(), 25))
  462. match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
  463. match_array(mnp.triu(x, 25).asnumpy(), onp.triu(x.asnumpy(), 25))
  464. match_array(mnp.tri(64, 64).asnumpy(), onp.tri(64, 64))
  465. match_array(mnp.tri(64, 64, -10).asnumpy(), onp.tri(64, 64, -10))
  466. @pytest.mark.level1
  467. @pytest.mark.platform_arm_ascend_training
  468. @pytest.mark.platform_x86_ascend_training
  469. @pytest.mark.platform_x86_gpu_training
  470. @pytest.mark.platform_x86_cpu
  471. @pytest.mark.env_onecard
  472. def test_cumsum():
  473. x = mnp.ones((16, 16), dtype="bool")
  474. match_array(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy()))
  475. match_array(mnp.cumsum(x, axis=0).asnumpy(),
  476. onp.cumsum(x.asnumpy(), axis=0))
  477. match_meta(mnp.cumsum(x).asnumpy(), onp.cumsum(x.asnumpy()))
  478. x = rand_int(3, 4, 5)
  479. match_array(mnp.cumsum(mnp.asarray(x), dtype="bool").asnumpy(),
  480. onp.cumsum(x, dtype="bool"))
  481. match_array(mnp.cumsum(mnp.asarray(x), axis=-1).asnumpy(),
  482. onp.cumsum(x, axis=-1))
  483. def mnp_diagonal(arr):
  484. return mnp.diagonal(arr, offset=2, axis1=-1, axis2=0)
  485. def onp_diagonal(arr):
  486. return onp.diagonal(arr, offset=2, axis1=-1, axis2=0)
  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_diagonal():
  494. arr = rand_int(0, 0)
  495. match_res(mnp.diagonal, onp.diagonal, arr, offset=1)
  496. arr = rand_int(3, 5)
  497. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  498. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=1)
  499. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=1, axis2=0)
  500. arr = rand_int(7, 4, 9)
  501. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  502. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=-1)
  503. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=-2, axis2=2)
  504. match_res(mnp.diagonal, onp.diagonal, arr,
  505. offset=i, axis1=-1, axis2=-2)
  506. arr = rand_int(2, 5, 8, 1)
  507. match_res(mnp_diagonal, onp_diagonal, arr)
  508. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  509. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=-3, axis2=2)
  510. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=1, axis2=3)
  511. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=-2)
  512. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=2, axis2=-1)
  513. def mnp_trace(arr):
  514. return mnp.trace(arr, offset=4, axis1=1, axis2=2)
  515. def onp_trace(arr):
  516. return onp.trace(arr, offset=4, axis1=1, axis2=2)
  517. @pytest.mark.level1
  518. @pytest.mark.platform_arm_ascend_training
  519. @pytest.mark.platform_x86_ascend_training
  520. @pytest.mark.platform_x86_gpu_training
  521. @pytest.mark.platform_x86_cpu
  522. @pytest.mark.env_onecard
  523. def test_trace():
  524. arr = rand_int(0, 0)
  525. match_res(mnp.trace, onp.trace, arr, offset=1)
  526. arr = rand_int(3, 5)
  527. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  528. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=1)
  529. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=1, axis2=0)
  530. arr = rand_int(7, 4, 9)
  531. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  532. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=-1)
  533. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-2, axis2=2)
  534. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-1, axis2=-2)
  535. arr = rand_int(2, 5, 8, 1)
  536. match_res(mnp_trace, onp_trace, arr)
  537. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  538. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-3, axis2=2)
  539. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=1, axis2=3)
  540. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=-2)
  541. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=2, axis2=-1)
  542. def mnp_meshgrid(*xi):
  543. a = mnp.meshgrid(*xi)
  544. b = mnp.meshgrid(*xi, sparse=True)
  545. c = mnp.meshgrid(*xi, indexing='ij')
  546. d = mnp.meshgrid(*xi, sparse=False, indexing='ij')
  547. return a, b, c, d
  548. def onp_meshgrid(*xi):
  549. a = onp.meshgrid(*xi)
  550. b = onp.meshgrid(*xi, sparse=True)
  551. c = onp.meshgrid(*xi, indexing='ij')
  552. d = onp.meshgrid(*xi, sparse=False, indexing='ij')
  553. return a, b, c, d
  554. @pytest.mark.level1
  555. @pytest.mark.platform_arm_ascend_training
  556. @pytest.mark.platform_x86_ascend_training
  557. @pytest.mark.platform_x86_gpu_training
  558. @pytest.mark.platform_x86_cpu
  559. @pytest.mark.env_onecard
  560. def test_meshgrid():
  561. xi = (onp.full(3, 2), onp.full(1, 5), onp.full(
  562. (2, 3), 9), onp.full((4, 5, 6), 7))
  563. for i in range(len(xi)):
  564. arrs = xi[i:]
  565. mnp_arrs = map(mnp.asarray, arrs)
  566. for mnp_res, onp_res in zip(mnp_meshgrid(*mnp_arrs), onp_meshgrid(*arrs)):
  567. match_all_arrays(mnp_res, onp_res)
  568. @pytest.mark.level1
  569. @pytest.mark.platform_arm_ascend_training
  570. @pytest.mark.platform_x86_ascend_training
  571. @pytest.mark.platform_x86_gpu_training
  572. @pytest.mark.platform_x86_cpu
  573. @pytest.mark.env_onecard
  574. def test_mgrid():
  575. mnp_res = mnp.mgrid[0:5]
  576. onp_res = onp.mgrid[0:5]
  577. match_all_arrays(mnp_res, onp_res, error=5)
  578. mnp_res = mnp.mgrid[2:30:4j, -10:20:7, 2:5:0.5]
  579. onp_res = onp.mgrid[2:30:4j, -10:20:7, 2:5:0.5]
  580. match_all_arrays(mnp_res, onp_res, error=5)
  581. @pytest.mark.level1
  582. @pytest.mark.platform_arm_ascend_training
  583. @pytest.mark.platform_x86_ascend_training
  584. @pytest.mark.platform_x86_gpu_training
  585. @pytest.mark.platform_x86_cpu
  586. @pytest.mark.env_onecard
  587. def test_ogrid():
  588. mnp_res = mnp.ogrid[0:5]
  589. onp_res = onp.ogrid[0:5]
  590. match_all_arrays(mnp_res, onp_res, error=5)
  591. mnp_res = mnp.ogrid[2:30:4j, -10:20:7, 2:5:0.5]
  592. onp_res = onp.ogrid[2:30:4j, -10:20:7, 2:5:0.5]
  593. match_all_arrays(mnp_res, onp_res, error=5)
  594. @pytest.mark.level1
  595. @pytest.mark.platform_arm_ascend_training
  596. @pytest.mark.platform_x86_ascend_training
  597. @pytest.mark.platform_x86_gpu_training
  598. @pytest.mark.platform_x86_cpu
  599. @pytest.mark.env_onecard
  600. def test_diagflat():
  601. arrs = [rand_int(0), rand_int(2, 3), rand_int(3, 5, 0)]
  602. for arr in arrs:
  603. for i in [-2, 0, 7]:
  604. match_res(mnp.diagflat, onp.diagflat, arr, k=i)
  605. @pytest.mark.level1
  606. @pytest.mark.platform_arm_ascend_training
  607. @pytest.mark.platform_x86_ascend_training
  608. @pytest.mark.platform_x86_gpu_training
  609. @pytest.mark.platform_x86_cpu
  610. @pytest.mark.env_onecard
  611. def test_diag():
  612. arrs = [rand_int(0), rand_int(0, 0), rand_int(7), rand_int(5, 5),
  613. rand_int(3, 8), rand_int(9, 6)]
  614. for arr in arrs:
  615. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  616. match_res(mnp.diag, onp.diag, arr, k=i)
  617. @pytest.mark.level1
  618. @pytest.mark.platform_arm_ascend_training
  619. @pytest.mark.platform_x86_ascend_training
  620. @pytest.mark.platform_x86_gpu_training
  621. @pytest.mark.platform_x86_cpu
  622. @pytest.mark.env_onecard
  623. def test_diag_indices():
  624. mnp_res = mnp.diag_indices(0)
  625. onp_res = onp.diag_indices(0)
  626. match_all_arrays(mnp_res, onp_res)
  627. mnp_res = mnp.diag_indices(3, 0)
  628. onp_res = onp.diag_indices(3, 0)
  629. match_all_arrays(mnp_res, onp_res)
  630. mnp_res = mnp.diag_indices(5, 7)
  631. onp_res = onp.diag_indices(5, 7)
  632. match_all_arrays(mnp_res, onp_res)
  633. def mnp_ix_(*args):
  634. return mnp.ix_(*args)
  635. def onp_ix_(*args):
  636. return onp.ix_(*args)
  637. @pytest.mark.level1
  638. @pytest.mark.platform_arm_ascend_training
  639. @pytest.mark.platform_x86_ascend_training
  640. @pytest.mark.platform_x86_gpu_training
  641. @pytest.mark.platform_x86_cpu
  642. @pytest.mark.env_onecard
  643. def test_ix_():
  644. arrs = [rand_int(i + 1) for i in range(10)]
  645. for i in range(10):
  646. test_arrs = arrs[:i + 1]
  647. match_res(mnp_ix_, onp_ix_, *test_arrs)
  648. @pytest.mark.level1
  649. @pytest.mark.platform_arm_ascend_training
  650. @pytest.mark.platform_x86_ascend_training
  651. @pytest.mark.platform_x86_gpu_training
  652. @pytest.mark.platform_x86_cpu
  653. @pytest.mark.env_onecard
  654. def test_asarray_exception():
  655. with pytest.raises(TypeError):
  656. mnp.asarray({1, 2, 3})
  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_linspace_exception():
  664. with pytest.raises(TypeError):
  665. mnp.linspace(0, 1, num=2.5)
  666. @pytest.mark.level1
  667. @pytest.mark.platform_arm_ascend_training
  668. @pytest.mark.platform_x86_ascend_training
  669. @pytest.mark.platform_x86_gpu_training
  670. @pytest.mark.platform_x86_cpu
  671. @pytest.mark.env_onecard
  672. def test_empty_like_exception():
  673. with pytest.raises(ValueError):
  674. mnp.empty_like([[1, 2, 3], [4, 5]])