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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 functools
  17. import pytest
  18. import numpy as onp
  19. import mindspore.context as context
  20. import mindspore.numpy as mnp
  21. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  22. class Cases():
  23. def __init__(self):
  24. self.all_shapes = [
  25. 0, 1, 2, (), (1,), (2,), (1, 2, 3), [], [1], [2], [1, 2, 3]
  26. ]
  27. self.onp_dtypes = [onp.int32, 'int32', int,
  28. onp.float32, 'float32', float,
  29. onp.uint32, 'uint32',
  30. onp.bool_, 'bool', bool]
  31. self.mnp_dtypes = [mnp.int32, 'int32', int,
  32. mnp.float32, 'float32', float,
  33. mnp.uint32, 'uint32',
  34. mnp.bool_, 'bool', bool]
  35. self.array_sets = [1, 1.1, True, [1, 0, True], [1, 1.0, 2], (1,),
  36. [(1, 2, 3), (4, 5, 6)], onp.random.random( # pylint: disable=no-member
  37. (100, 100)).astype(onp.float32),
  38. onp.random.random((100, 100)).astype(onp.bool)]
  39. self.arrs = [
  40. rand_int(2),
  41. rand_int(2, 3),
  42. rand_int(2, 3, 4),
  43. rand_int(2, 3, 4, 5),
  44. ]
  45. # scalars expanded across the 0th dimension
  46. self.scalars = [
  47. rand_int(),
  48. rand_int(1),
  49. rand_int(1, 1),
  50. rand_int(1, 1, 1),
  51. ]
  52. # arrays of the same size expanded across the 0th dimension
  53. self.expanded_arrs = [
  54. rand_int(2, 3),
  55. rand_int(1, 2, 3),
  56. rand_int(1, 1, 2, 3),
  57. rand_int(1, 1, 1, 2, 3),
  58. ]
  59. # arrays with dimensions of size 1
  60. self.nested_arrs = [
  61. rand_int(1),
  62. rand_int(1, 2),
  63. rand_int(3, 1, 8),
  64. rand_int(1, 3, 9, 1),
  65. ]
  66. # arrays which can be broadcast
  67. self.broadcastables = [
  68. rand_int(5),
  69. rand_int(6, 1),
  70. rand_int(7, 1, 5),
  71. rand_int(8, 1, 6, 1)
  72. ]
  73. # boolean arrays which can be broadcast
  74. self.bool_broadcastables = [
  75. rand_bool(),
  76. rand_bool(1),
  77. rand_bool(5),
  78. rand_bool(6, 1),
  79. rand_bool(7, 1, 5),
  80. rand_bool(8, 1, 6, 1),
  81. ]
  82. self.mnp_prototypes = [
  83. mnp.ones((2, 3, 4)),
  84. mnp.ones((0, 3, 0, 2, 5)),
  85. onp.ones((2, 7, 0)),
  86. onp.ones(()),
  87. [mnp.ones(3), (1, 2, 3), onp.ones(3), [4, 5, 6]],
  88. ([(1, 2), mnp.ones(2)], (onp.ones(2), [3, 4])),
  89. ]
  90. self.onp_prototypes = [
  91. onp.ones((2, 3, 4)),
  92. onp.ones((0, 3, 0, 2, 5)),
  93. onp.ones((2, 7, 0)),
  94. onp.ones(()),
  95. [onp.ones(3), (1, 2, 3), onp.ones(3), [4, 5, 6]],
  96. ([(1, 2), onp.ones(2)], (onp.ones(2), [3, 4])),
  97. ]
  98. def match_array(actual, expected, error=0):
  99. if error > 0:
  100. onp.testing.assert_almost_equal(actual.tolist(), expected.tolist(),
  101. decimal=error)
  102. else:
  103. onp.testing.assert_equal(actual.tolist(), expected.tolist())
  104. def check_all_results(onp_results, mnp_results, error=0):
  105. """Check all results from numpy and mindspore.numpy"""
  106. for i, _ in enumerate(onp_results):
  107. match_array(onp_results[i], mnp_results[i].asnumpy())
  108. def check_all_unique_results(onp_results, mnp_results):
  109. """
  110. Check all results from numpy and mindspore.numpy.
  111. Args:
  112. onp_results (Union[tuple of numpy.arrays, numpy.array])
  113. mnp_results (Union[tuple of Tensors, Tensor])
  114. """
  115. for i, _ in enumerate(onp_results):
  116. if isinstance(onp_results[i], tuple):
  117. for j in range(len(onp_results[i])):
  118. match_array(onp_results[i][j],
  119. mnp_results[i][j].asnumpy(), error=7)
  120. else:
  121. match_array(onp_results[i], mnp_results[i].asnumpy(), error=7)
  122. def run_non_kw_test(mnp_fn, onp_fn):
  123. """Run tests on functions with non keyword arguments"""
  124. test_case = Cases()
  125. for i in range(len(test_case.arrs)):
  126. arrs = test_case.arrs[:i]
  127. match_res(mnp_fn, onp_fn, *arrs)
  128. for i in range(len(test_case.scalars)):
  129. arrs = test_case.scalars[:i]
  130. match_res(mnp_fn, onp_fn, *arrs)
  131. for i in range(len(test_case.expanded_arrs)):
  132. arrs = test_case.expanded_arrs[:i]
  133. match_res(mnp_fn, onp_fn, *arrs)
  134. for i in range(len(test_case.nested_arrs)):
  135. arrs = test_case.nested_arrs[:i]
  136. match_res(mnp_fn, onp_fn, *arrs)
  137. def rand_int(*shape):
  138. """return an random integer array with parameter shape"""
  139. res = onp.random.randint(low=1, high=5, size=shape)
  140. if isinstance(res, onp.ndarray):
  141. return res.astype(onp.float32)
  142. return float(res)
  143. # return an random boolean array
  144. def rand_bool(*shape):
  145. return onp.random.rand(*shape) > 0.5
  146. def match_res(mnp_fn, onp_fn, *arrs, **kwargs):
  147. """Checks results from applying mnp_fn and onp_fn on arrs respectively"""
  148. mnp_arrs = map(functools.partial(mnp.asarray, dtype='float32'), arrs)
  149. mnp_res = mnp_fn(*mnp_arrs, **kwargs)
  150. onp_res = onp_fn(*arrs, **kwargs)
  151. match_all_arrays(mnp_res, onp_res)
  152. def match_all_arrays(mnp_res, onp_res, error=0):
  153. if isinstance(mnp_res, (tuple, list)):
  154. for actual, expected in zip(mnp_res, onp_res):
  155. match_array(actual.asnumpy(), expected, error)
  156. else:
  157. match_array(mnp_res.asnumpy(), onp_res, error)
  158. def match_meta(actual, expected):
  159. # float64 and int64 are not supported, and the default type for
  160. # float and int are float32 and int32, respectively
  161. if expected.dtype == onp.float64:
  162. expected = expected.astype(onp.float32)
  163. elif expected.dtype == onp.int64:
  164. expected = expected.astype(onp.int32)
  165. assert actual.shape == expected.shape
  166. assert actual.dtype == expected.dtype
  167. @pytest.mark.level1
  168. @pytest.mark.platform_arm_ascend_training
  169. @pytest.mark.platform_x86_ascend_training
  170. @pytest.mark.platform_x86_gpu_training
  171. @pytest.mark.platform_x86_cpu
  172. @pytest.mark.env_onecard
  173. def test_asarray():
  174. test_case = Cases()
  175. for array in test_case.array_sets:
  176. # Check for dtype matching
  177. actual = onp.asarray(array)
  178. expected = mnp.asarray(array).asnumpy()
  179. # Since we set float32/int32 as the default dtype in mindspore, we need
  180. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  181. if actual.dtype is onp.dtype('float64'):
  182. assert expected.dtype == onp.dtype('float32')
  183. elif actual.dtype is onp.dtype('int64'):
  184. assert expected.dtype == onp.dtype('int32')
  185. else:
  186. assert actual.dtype == expected.dtype
  187. match_array(actual, expected, error=7)
  188. for i in range(len(test_case.onp_dtypes)):
  189. actual = onp.asarray(array, test_case.onp_dtypes[i])
  190. expected = mnp.asarray(array, test_case.mnp_dtypes[i]).asnumpy()
  191. match_array(actual, expected, error=7)
  192. # Additional tests for nested tensor/numpy_array mixture
  193. mnp_input = [(onp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  194. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  195. actual = onp.asarray(onp_input)
  196. expected = mnp.asarray(mnp_input).asnumpy()
  197. match_array(actual, expected, error=7)
  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_array():
  205. # array's function is very similar to asarray, so we mainly test the
  206. # `copy` argument.
  207. test_case = Cases()
  208. for array in test_case.array_sets:
  209. arr1 = mnp.asarray(array)
  210. arr2 = mnp.array(arr1, copy=False)
  211. arr3 = mnp.array(arr1)
  212. arr4 = mnp.asarray(array, dtype='int32')
  213. arr5 = mnp.asarray(arr4, dtype=mnp.int32)
  214. assert arr1 is arr2
  215. assert arr1 is not arr3
  216. assert arr4 is arr5
  217. # Additional tests for nested tensor/numpy_array mixture
  218. mnp_input = [(onp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  219. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  220. actual = onp.asarray(onp_input)
  221. expected = mnp.asarray(mnp_input).asnumpy()
  222. match_array(actual, expected, error=7)
  223. @pytest.mark.level1
  224. @pytest.mark.platform_arm_ascend_training
  225. @pytest.mark.platform_x86_ascend_training
  226. @pytest.mark.platform_x86_gpu_training
  227. @pytest.mark.platform_x86_cpu
  228. @pytest.mark.env_onecard
  229. def test_asfarray():
  230. test_case = Cases()
  231. for array in test_case.array_sets:
  232. # Check for dtype matching
  233. actual = onp.asfarray(array)
  234. expected = mnp.asfarray(array).asnumpy()
  235. # Since we set float32/int32 as the default dtype in mindspore, we need
  236. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  237. if actual.dtype is onp.dtype('float64'):
  238. assert expected.dtype == onp.dtype('float32')
  239. else:
  240. assert actual.dtype == expected.dtype
  241. match_array(actual, expected, error=7)
  242. for i in range(len(test_case.onp_dtypes)):
  243. actual = onp.asfarray(array, test_case.onp_dtypes[i])
  244. expected = mnp.asfarray(array, test_case.mnp_dtypes[i]).asnumpy()
  245. match_array(actual, expected, error=7)
  246. # Additional tests for nested tensor/numpy_array mixture
  247. mnp_input = [(onp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  248. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  249. actual = onp.asarray(onp_input)
  250. expected = mnp.asarray(mnp_input).asnumpy()
  251. match_array(actual, expected, error=7)
  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_zeros():
  259. test_case = Cases()
  260. for shape in test_case.all_shapes:
  261. for i in range(len(test_case.onp_dtypes)):
  262. actual = onp.zeros(shape, test_case.onp_dtypes[i])
  263. expected = mnp.zeros(shape, test_case.mnp_dtypes[i]).asnumpy()
  264. match_array(actual, expected)
  265. actual = onp.zeros(shape)
  266. expected = mnp.zeros(shape).asnumpy()
  267. match_array(actual, expected)
  268. @pytest.mark.level1
  269. @pytest.mark.platform_arm_ascend_training
  270. @pytest.mark.platform_x86_ascend_training
  271. @pytest.mark.platform_x86_gpu_training
  272. @pytest.mark.platform_x86_cpu
  273. @pytest.mark.env_onecard
  274. def test_ones():
  275. test_case = Cases()
  276. for shape in test_case.all_shapes:
  277. for i in range(len(test_case.onp_dtypes)):
  278. actual = onp.ones(shape, test_case.onp_dtypes[i])
  279. expected = mnp.ones(shape, test_case.mnp_dtypes[i]).asnumpy()
  280. match_array(actual, expected)
  281. actual = onp.ones(shape)
  282. expected = mnp.ones(shape).asnumpy()
  283. match_array(actual, expected)
  284. @pytest.mark.level1
  285. @pytest.mark.platform_arm_ascend_training
  286. @pytest.mark.platform_x86_ascend_training
  287. @pytest.mark.platform_x86_gpu_training
  288. @pytest.mark.platform_x86_cpu
  289. @pytest.mark.env_onecard
  290. def test_full():
  291. actual = onp.full((2, 2), [1, 2])
  292. expected = mnp.full((2, 2), [1, 2]).asnumpy()
  293. match_array(actual, expected)
  294. actual = onp.full((2, 0), onp.inf)
  295. expected = mnp.full((2, 0), mnp.inf).asnumpy()
  296. match_array(actual, expected)
  297. actual = onp.full((2, 3), True)
  298. expected = mnp.full((2, 3), True).asnumpy()
  299. match_array(actual, expected)
  300. actual = onp.full((3, 4, 5), 7.5)
  301. expected = mnp.full((3, 4, 5), 7.5).asnumpy()
  302. match_array(actual, expected)
  303. @pytest.mark.level1
  304. @pytest.mark.platform_arm_ascend_training
  305. @pytest.mark.platform_x86_ascend_training
  306. @pytest.mark.platform_x86_gpu_training
  307. @pytest.mark.platform_x86_cpu
  308. @pytest.mark.env_onecard
  309. def test_eye():
  310. test_case = Cases()
  311. for i in range(len(test_case.onp_dtypes)):
  312. for m in range(1, 5):
  313. actual = onp.eye(m, dtype=test_case.onp_dtypes[i])
  314. expected = mnp.eye(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
  315. match_array(actual, expected)
  316. for n in range(1, 5):
  317. for k in range(0, 5):
  318. actual = onp.eye(m, n, k, dtype=test_case.onp_dtypes[i])
  319. expected = mnp.eye(
  320. m, n, k, dtype=test_case.mnp_dtypes[i]).asnumpy()
  321. match_array(actual, expected)
  322. @pytest.mark.level1
  323. @pytest.mark.platform_arm_ascend_training
  324. @pytest.mark.platform_x86_ascend_training
  325. @pytest.mark.platform_x86_gpu_training
  326. @pytest.mark.platform_x86_cpu
  327. @pytest.mark.env_onecard
  328. def test_identity():
  329. test_case = Cases()
  330. for i in range(len(test_case.onp_dtypes)):
  331. for m in range(1, 5):
  332. actual = onp.identity(m, dtype=test_case.onp_dtypes[i])
  333. expected = mnp.identity(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
  334. match_array(actual, expected)
  335. @pytest.mark.level1
  336. @pytest.mark.platform_arm_ascend_training
  337. @pytest.mark.platform_x86_ascend_training
  338. @pytest.mark.platform_x86_gpu_training
  339. @pytest.mark.platform_x86_cpu
  340. @pytest.mark.env_onecard
  341. def test_arange():
  342. actual = onp.arange(10)
  343. expected = mnp.arange(10).asnumpy()
  344. match_array(actual, expected)
  345. actual = onp.arange(0, 10)
  346. expected = mnp.arange(0, 10).asnumpy()
  347. match_array(actual, expected)
  348. actual = onp.arange(start=10)
  349. expected = mnp.arange(start=10).asnumpy()
  350. match_array(actual, expected)
  351. actual = onp.arange(start=10, step=0.1)
  352. expected = mnp.arange(start=10, step=0.1).asnumpy()
  353. match_array(actual, expected, error=6)
  354. actual = onp.arange(10, step=0.1)
  355. expected = mnp.arange(10, step=0.1).asnumpy()
  356. match_array(actual, expected, error=6)
  357. actual = onp.arange(0.1, 9.9)
  358. expected = mnp.arange(0.1, 9.9).asnumpy()
  359. match_array(actual, expected, error=6)
  360. @pytest.mark.level1
  361. @pytest.mark.platform_arm_ascend_training
  362. @pytest.mark.platform_x86_ascend_training
  363. @pytest.mark.platform_x86_gpu_training
  364. @pytest.mark.platform_x86_cpu
  365. @pytest.mark.env_onecard
  366. def test_linspace():
  367. actual = onp.linspace(2.0, 3.0, dtype=onp.float32)
  368. expected = mnp.linspace(2.0, 3.0).asnumpy()
  369. match_array(actual, expected, error=7)
  370. actual = onp.linspace(2.0, 3.0, num=5, dtype=onp.float32)
  371. expected = mnp.linspace(2.0, 3.0, num=5).asnumpy()
  372. match_array(actual, expected, error=7)
  373. actual = onp.linspace(
  374. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  375. expected = mnp.linspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  376. match_array(actual, expected, error=7)
  377. actual = onp.linspace(2.0, 3.0, num=5, retstep=True, dtype=onp.float32)
  378. expected = mnp.linspace(2.0, 3.0, num=5, retstep=True)
  379. match_array(actual[0], expected[0].asnumpy())
  380. assert actual[1] == expected[1]
  381. actual = onp.linspace(2.0, [3, 4, 5], num=5,
  382. endpoint=False, dtype=onp.float32)
  383. expected = mnp.linspace(
  384. 2.0, [3, 4, 5], num=5, endpoint=False).asnumpy()
  385. match_array(actual, expected)
  386. @pytest.mark.level1
  387. @pytest.mark.platform_arm_ascend_training
  388. @pytest.mark.platform_x86_ascend_training
  389. @pytest.mark.platform_x86_gpu_training
  390. @pytest.mark.platform_x86_cpu
  391. @pytest.mark.env_onecard
  392. def test_logspace():
  393. actual = onp.logspace(2.0, 3.0, dtype=onp.float32)
  394. expected = mnp.logspace(2.0, 3.0).asnumpy()
  395. match_array(actual, expected)
  396. actual = onp.logspace(2.0, 3.0, num=5, dtype=onp.float32)
  397. expected = mnp.logspace(2.0, 3.0, num=5).asnumpy()
  398. match_array(actual, expected)
  399. actual = onp.logspace(
  400. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  401. expected = mnp.logspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  402. match_array(actual, expected)
  403. actual = onp.logspace(2.0, [3, 4, 5], num=5,
  404. endpoint=False, dtype=onp.float32)
  405. expected = mnp.logspace(
  406. 2.0, [3, 4, 5], num=5, endpoint=False).asnumpy()
  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_empty():
  415. test_case = Cases()
  416. for shape in test_case.all_shapes:
  417. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  418. test_case.onp_dtypes):
  419. actual = mnp.empty(shape, mnp_dtype).asnumpy()
  420. expected = onp.empty(shape, onp_dtype)
  421. match_meta(actual, expected)
  422. @pytest.mark.level1
  423. @pytest.mark.platform_arm_ascend_training
  424. @pytest.mark.platform_x86_ascend_training
  425. @pytest.mark.platform_x86_gpu_training
  426. @pytest.mark.platform_x86_cpu
  427. @pytest.mark.env_onecard
  428. def test_empty_like():
  429. test_case = Cases()
  430. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  431. actual = mnp.empty_like(mnp_proto).asnumpy()
  432. expected = onp.empty_like(onp_proto)
  433. assert actual.shape == expected.shape
  434. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  435. test_case.onp_dtypes):
  436. actual = mnp.empty_like(mnp_proto, dtype=mnp_dtype).asnumpy()
  437. expected = onp.empty_like(onp_proto, dtype=onp_dtype)
  438. match_meta(actual, expected)
  439. def run_x_like(mnp_fn, onp_fn):
  440. test_case = Cases()
  441. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  442. actual = mnp_fn(mnp_proto).asnumpy()
  443. expected = onp_fn(onp_proto)
  444. match_array(actual, expected)
  445. for shape in test_case.all_shapes:
  446. actual = mnp_fn(mnp_proto, shape=shape).asnumpy()
  447. expected = onp_fn(onp_proto, shape=shape)
  448. match_array(actual, expected)
  449. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  450. test_case.onp_dtypes):
  451. actual = mnp_fn(mnp_proto, dtype=mnp_dtype).asnumpy()
  452. expected = onp_fn(onp_proto, dtype=onp_dtype)
  453. match_array(actual, expected)
  454. actual = mnp_fn(mnp_proto, dtype=mnp_dtype,
  455. shape=shape).asnumpy()
  456. expected = onp_fn(onp_proto, dtype=onp_dtype, shape=shape)
  457. match_array(actual, expected)
  458. @pytest.mark.level1
  459. @pytest.mark.platform_arm_ascend_training
  460. @pytest.mark.platform_x86_ascend_training
  461. @pytest.mark.platform_x86_gpu_training
  462. @pytest.mark.platform_x86_cpu
  463. @pytest.mark.env_onecard
  464. def test_ones_like():
  465. run_x_like(mnp.ones_like, onp.ones_like)
  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_zeros_like():
  473. run_x_like(mnp.zeros_like, onp.zeros_like)
  474. @pytest.mark.level1
  475. @pytest.mark.platform_arm_ascend_training
  476. @pytest.mark.platform_x86_ascend_training
  477. @pytest.mark.platform_x86_gpu_training
  478. @pytest.mark.platform_x86_cpu
  479. @pytest.mark.env_onecard
  480. def test_full_like():
  481. test_case = Cases()
  482. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  483. shape = onp.zeros_like(onp_proto).shape
  484. fill_value = rand_int()
  485. actual = mnp.full_like(mnp_proto, fill_value).asnumpy()
  486. expected = onp.full_like(onp_proto, fill_value)
  487. match_array(actual, expected)
  488. for i in range(len(shape) - 1, 0, -1):
  489. fill_value = rand_int(*shape[i:])
  490. actual = mnp.full_like(mnp_proto, fill_value).asnumpy()
  491. expected = onp.full_like(onp_proto, fill_value)
  492. match_array(actual, expected)
  493. fill_value = rand_int(1, *shape[i + 1:])
  494. actual = mnp.full_like(mnp_proto, fill_value).asnumpy()
  495. expected = onp.full_like(onp_proto, fill_value)
  496. match_array(actual, expected)
  497. @pytest.mark.level1
  498. @pytest.mark.platform_arm_ascend_training
  499. @pytest.mark.platform_x86_ascend_training
  500. @pytest.mark.platform_x86_gpu_training
  501. @pytest.mark.platform_x86_cpu
  502. @pytest.mark.env_onecard
  503. def test_tri_triu_tril():
  504. x = mnp.ones((16, 32), dtype="bool")
  505. match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
  506. match_array(mnp.tril(x, -1).asnumpy(), onp.tril(x.asnumpy(), -1))
  507. match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
  508. match_array(mnp.triu(x, -1).asnumpy(), onp.triu(x.asnumpy(), -1))
  509. x = mnp.ones((64, 64), dtype="uint8")
  510. match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
  511. match_array(mnp.tril(x, 25).asnumpy(), onp.tril(x.asnumpy(), 25))
  512. match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
  513. match_array(mnp.triu(x, 25).asnumpy(), onp.triu(x.asnumpy(), 25))
  514. match_array(mnp.tri(64, 64).asnumpy(), onp.tri(64, 64))
  515. match_array(mnp.tri(64, 64, -10).asnumpy(), onp.tri(64, 64, -10))
  516. def mnp_diagonal(arr):
  517. return mnp.diagonal(arr, offset=2, axis1=-1, axis2=0)
  518. def onp_diagonal(arr):
  519. return onp.diagonal(arr, offset=2, axis1=-1, axis2=0)
  520. @pytest.mark.level1
  521. @pytest.mark.platform_arm_ascend_training
  522. @pytest.mark.platform_x86_ascend_training
  523. @pytest.mark.platform_x86_gpu_training
  524. @pytest.mark.platform_x86_cpu
  525. @pytest.mark.env_onecard
  526. def test_diagonal():
  527. arr = rand_int(0, 0)
  528. match_res(mnp.diagonal, onp.diagonal, arr, offset=1)
  529. arr = rand_int(3, 5)
  530. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  531. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=1)
  532. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=1, axis2=0)
  533. arr = rand_int(7, 4, 9)
  534. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  535. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=-1)
  536. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=-2, axis2=2)
  537. match_res(mnp.diagonal, onp.diagonal, arr,
  538. offset=i, axis1=-1, axis2=-2)
  539. arr = rand_int(2, 5, 8, 1)
  540. match_res(mnp_diagonal, onp_diagonal, arr)
  541. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  542. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=-3, axis2=2)
  543. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=1, axis2=3)
  544. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=-2)
  545. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=2, axis2=-1)
  546. def mnp_trace(arr):
  547. return mnp.trace(arr, offset=4, axis1=1, axis2=2)
  548. def onp_trace(arr):
  549. return onp.trace(arr, offset=4, axis1=1, axis2=2)
  550. @pytest.mark.level1
  551. @pytest.mark.platform_arm_ascend_training
  552. @pytest.mark.platform_x86_ascend_training
  553. @pytest.mark.platform_x86_gpu_training
  554. @pytest.mark.platform_x86_cpu
  555. @pytest.mark.env_onecard
  556. def test_trace():
  557. arr = rand_int(0, 0)
  558. match_res(mnp.trace, onp.trace, arr, offset=1)
  559. arr = rand_int(3, 5)
  560. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  561. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=1)
  562. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=1, axis2=0)
  563. arr = rand_int(7, 4, 9)
  564. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  565. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=-1)
  566. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-2, axis2=2)
  567. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-1, axis2=-2)
  568. arr = rand_int(2, 5, 8, 1)
  569. match_res(mnp_trace, onp_trace, arr)
  570. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  571. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-3, axis2=2)
  572. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=1, axis2=3)
  573. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=-2)
  574. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=2, axis2=-1)
  575. @pytest.mark.level1
  576. @pytest.mark.platform_arm_ascend_training
  577. @pytest.mark.platform_x86_ascend_training
  578. @pytest.mark.platform_x86_gpu_training
  579. @pytest.mark.platform_x86_cpu
  580. @pytest.mark.env_onecard
  581. def test_asarray_exception():
  582. with pytest.raises(TypeError):
  583. mnp.asarray({1, 2, 3})
  584. @pytest.mark.level1
  585. @pytest.mark.platform_arm_ascend_training
  586. @pytest.mark.platform_x86_ascend_training
  587. @pytest.mark.platform_x86_gpu_training
  588. @pytest.mark.platform_x86_cpu
  589. @pytest.mark.env_onecard
  590. def test_linspace_exception():
  591. with pytest.raises(TypeError):
  592. mnp.linspace(0, 1, num=2.5)
  593. @pytest.mark.level1
  594. @pytest.mark.platform_arm_ascend_training
  595. @pytest.mark.platform_x86_ascend_training
  596. @pytest.mark.platform_x86_gpu_training
  597. @pytest.mark.platform_x86_cpu
  598. @pytest.mark.env_onecard
  599. def test_empty_like_exception():
  600. with pytest.raises(ValueError):
  601. mnp.empty_like([[1, 2, 3], [4, 5]])