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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. import mindspore.ops.functional as F
  20. from mindspore import context
  21. from mindspore import set_seed
  22. from mindspore.common import dtype as mstype
  23. from .utils import rand_int, rand_bool, match_array, match_res, match_meta, \
  24. match_all_arrays, run_multi_test, to_tensor
  25. context.set_context(mode=context.PYNATIVE_MODE)
  26. class Cases():
  27. def __init__(self):
  28. self.all_shapes = [
  29. 1, 2, (1,), (2,), (1, 2, 3), [1], [2], [1, 2, 3]
  30. ]
  31. self.onp_dtypes = [onp.int32, 'int32', int,
  32. onp.float32, 'float32', float,
  33. onp.uint32, 'uint32',
  34. onp.bool_, 'bool', bool]
  35. self.mnp_dtypes = [mnp.int32, 'int32', int,
  36. mnp.float32, 'float32', float,
  37. mnp.uint32, 'uint32',
  38. mnp.bool_, 'bool', bool]
  39. self.array_sets = [1, 1.1, True, [1, 0, True], [1, 1.0, 2], (1,),
  40. [(1, 2, 3), (4, 5, 6)], onp.random.random( # pylint: disable=no-member
  41. (100, 100)).astype(onp.float32).tolist(),
  42. onp.random.random((100, 100)).astype(onp.bool).tolist()]
  43. self.arrs = [
  44. rand_int(2),
  45. rand_int(2, 3),
  46. rand_int(2, 3, 4),
  47. rand_int(2, 3, 4, 5),
  48. ]
  49. # scalars expanded across the 0th dimension
  50. self.scalars = [
  51. rand_int(),
  52. rand_int(1),
  53. rand_int(1, 1),
  54. rand_int(1, 1, 1),
  55. ]
  56. # arrays of the same size expanded across the 0th dimension
  57. self.expanded_arrs = [
  58. rand_int(2, 3),
  59. rand_int(1, 2, 3),
  60. rand_int(1, 1, 2, 3),
  61. rand_int(1, 1, 1, 2, 3),
  62. ]
  63. # arrays with dimensions of size 1
  64. self.nested_arrs = [
  65. rand_int(1),
  66. rand_int(1, 2),
  67. rand_int(3, 1, 8),
  68. rand_int(1, 3, 9, 1),
  69. ]
  70. # arrays which can be broadcast
  71. self.broadcastables = [
  72. rand_int(5),
  73. rand_int(6, 1),
  74. rand_int(7, 1, 5),
  75. rand_int(8, 1, 6, 1)
  76. ]
  77. # boolean arrays which can be broadcast
  78. self.bool_broadcastables = [
  79. rand_bool(),
  80. rand_bool(1),
  81. rand_bool(5),
  82. rand_bool(6, 1),
  83. rand_bool(7, 1, 5),
  84. rand_bool(8, 1, 6, 1),
  85. ]
  86. self.mnp_prototypes = [
  87. mnp.ones((2, 3, 4)),
  88. mnp.ones((1, 3, 1, 2, 5)),
  89. mnp.ones((2, 7, 1)),
  90. [mnp.ones(3), (1, 2, 3), mnp.ones(3), [4, 5, 6]],
  91. ([(1, 2), mnp.ones(2)], (mnp.ones(2), [3, 4])),
  92. ]
  93. self.onp_prototypes = [
  94. onp.ones((2, 3, 4)),
  95. onp.ones((1, 3, 1, 2, 5)),
  96. onp.ones((2, 7, 1)),
  97. [onp.ones(3), (1, 2, 3), onp.ones(3), [4, 5, 6]],
  98. ([(1, 2), onp.ones(2)], (onp.ones(2), [3, 4])),
  99. ]
  100. @pytest.mark.level1
  101. @pytest.mark.platform_arm_ascend_training
  102. @pytest.mark.platform_x86_ascend_training
  103. @pytest.mark.platform_x86_gpu_training
  104. @pytest.mark.platform_x86_cpu
  105. @pytest.mark.env_onecard
  106. def test_asarray():
  107. test_case = Cases()
  108. for array in test_case.array_sets:
  109. # Check for dtype matching
  110. actual = onp.asarray(array)
  111. expected = mnp.asarray(array).asnumpy()
  112. # Since we set float32/int32 as the default dtype in mindspore, we need
  113. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  114. if actual.dtype is onp.dtype('float64'):
  115. assert expected.dtype == onp.dtype('float32')
  116. elif actual.dtype is onp.dtype('int64'):
  117. assert expected.dtype == onp.dtype('int32')
  118. else:
  119. assert actual.dtype == expected.dtype
  120. match_array(actual, expected, error=7)
  121. for i in range(len(test_case.onp_dtypes)):
  122. actual = onp.asarray(array, test_case.onp_dtypes[i])
  123. expected = mnp.asarray(array, test_case.mnp_dtypes[i]).asnumpy()
  124. match_array(actual, expected, error=7)
  125. # Additional tests for nested tensor mixture
  126. mnp_input = [(mnp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  127. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  128. actual = onp.asarray(onp_input)
  129. expected = mnp.asarray(mnp_input).asnumpy()
  130. match_array(actual, expected, error=7)
  131. @pytest.mark.level1
  132. @pytest.mark.platform_arm_ascend_training
  133. @pytest.mark.platform_x86_ascend_training
  134. @pytest.mark.platform_x86_gpu_training
  135. @pytest.mark.platform_x86_cpu
  136. @pytest.mark.env_onecard
  137. def test_array():
  138. # array's function is very similar to asarray, so we mainly test the
  139. # `copy` argument.
  140. test_case = Cases()
  141. for array in test_case.array_sets:
  142. arr1 = mnp.asarray(array)
  143. arr2 = mnp.array(arr1, copy=False)
  144. arr3 = mnp.array(arr1)
  145. arr4 = mnp.asarray(array, dtype='int32')
  146. arr5 = mnp.asarray(arr4, dtype=mnp.int32)
  147. assert arr1 is arr2
  148. assert arr1 is not arr3
  149. assert arr4 is arr5
  150. # Additional tests for nested tensor/numpy_array mixture
  151. mnp_input = [(mnp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  152. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  153. actual = onp.array(onp_input)
  154. expected = mnp.array(mnp_input).asnumpy()
  155. match_array(actual, expected, error=7)
  156. @pytest.mark.level1
  157. @pytest.mark.platform_arm_ascend_training
  158. @pytest.mark.platform_x86_ascend_training
  159. @pytest.mark.platform_x86_gpu_training
  160. @pytest.mark.platform_x86_cpu
  161. @pytest.mark.env_onecard
  162. def test_asfarray():
  163. test_case = Cases()
  164. for array in test_case.array_sets:
  165. # Check for dtype matching
  166. actual = onp.asfarray(array)
  167. expected = mnp.asfarray(array).asnumpy()
  168. # Since we set float32/int32 as the default dtype in mindspore, we need
  169. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  170. if actual.dtype is onp.dtype('float64'):
  171. assert expected.dtype == onp.dtype('float32')
  172. else:
  173. assert actual.dtype == expected.dtype
  174. match_array(actual, expected, error=7)
  175. for i in range(len(test_case.onp_dtypes)):
  176. actual = onp.asfarray(array, test_case.onp_dtypes[i])
  177. expected = mnp.asfarray(array, test_case.mnp_dtypes[i]).asnumpy()
  178. match_array(actual, expected, error=7)
  179. # Additional tests for nested tensor/numpy_array mixture
  180. mnp_input = [(mnp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  181. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  182. actual = onp.asfarray(onp_input)
  183. expected = mnp.asfarray(mnp_input).asnumpy()
  184. match_array(actual, expected, error=7)
  185. @pytest.mark.level1
  186. @pytest.mark.platform_arm_ascend_training
  187. @pytest.mark.platform_x86_ascend_training
  188. @pytest.mark.platform_x86_gpu_training
  189. @pytest.mark.platform_x86_cpu
  190. @pytest.mark.env_onecard
  191. def test_zeros():
  192. test_case = Cases()
  193. for shape in test_case.all_shapes:
  194. for i in range(len(test_case.onp_dtypes)):
  195. actual = onp.zeros(shape, test_case.onp_dtypes[i])
  196. expected = mnp.zeros(shape, test_case.mnp_dtypes[i]).asnumpy()
  197. match_array(actual, expected)
  198. actual = onp.zeros(shape)
  199. expected = mnp.zeros(shape).asnumpy()
  200. match_array(actual, expected)
  201. @pytest.mark.level1
  202. @pytest.mark.platform_arm_ascend_training
  203. @pytest.mark.platform_x86_ascend_training
  204. @pytest.mark.platform_x86_gpu_training
  205. @pytest.mark.platform_x86_cpu
  206. @pytest.mark.env_onecard
  207. def test_ones():
  208. test_case = Cases()
  209. for shape in test_case.all_shapes:
  210. for i in range(len(test_case.onp_dtypes)):
  211. actual = onp.ones(shape, test_case.onp_dtypes[i])
  212. expected = mnp.ones(shape, test_case.mnp_dtypes[i]).asnumpy()
  213. match_array(actual, expected)
  214. actual = onp.ones(shape)
  215. expected = mnp.ones(shape).asnumpy()
  216. match_array(actual, expected)
  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_full():
  224. actual = onp.full((2, 2), [1, 2])
  225. expected = mnp.full((2, 2), [1, 2]).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(10, step=0.1)
  279. expected = mnp.arange(10, step=0.1).asnumpy()
  280. match_array(actual, expected, error=6)
  281. actual = onp.arange(0.1, 9.9)
  282. expected = mnp.arange(0.1, 9.9).asnumpy()
  283. match_array(actual, expected, error=6)
  284. @pytest.mark.level0
  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_linspace():
  291. actual = onp.linspace(2.0, 3.0, dtype=onp.float32)
  292. expected = mnp.linspace(2.0, 3.0).asnumpy()
  293. match_array(actual, expected, error=6)
  294. actual = onp.linspace(2.0, 3.0, num=5, dtype=onp.float32)
  295. expected = mnp.linspace(2.0, 3.0, num=5).asnumpy()
  296. match_array(actual, expected, error=6)
  297. actual = onp.linspace(
  298. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  299. expected = mnp.linspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  300. match_array(actual, expected, error=6)
  301. actual = onp.linspace(2.0, 3.0, num=5, retstep=True, dtype=onp.float32)
  302. expected = mnp.linspace(2.0, 3.0, num=5, retstep=True)
  303. match_array(actual[0], expected[0].asnumpy())
  304. assert actual[1] == expected[1].asnumpy()
  305. actual = onp.linspace(2.0, [3, 4, 5], num=5,
  306. endpoint=False, dtype=onp.float32)
  307. expected = mnp.linspace(
  308. 2.0, [3, 4, 5], num=5, endpoint=False).asnumpy()
  309. match_array(actual, expected, error=6)
  310. actual = onp.linspace(2.0, [[3, 4, 5]], num=5, endpoint=False, axis=2)
  311. expected = mnp.linspace(2.0, [[3, 4, 5]], num=5, endpoint=False, axis=2).asnumpy()
  312. match_array(actual, expected, error=6)
  313. start = onp.random.random([2, 1, 4]).astype("float32")
  314. stop = onp.random.random([1, 5, 1]).astype("float32")
  315. actual = onp.linspace(start, stop, num=20, retstep=True,
  316. endpoint=False, dtype=onp.float32)
  317. expected = mnp.linspace(to_tensor(start), to_tensor(stop), num=20,
  318. retstep=True, endpoint=False)
  319. match_array(actual[0], expected[0].asnumpy(), error=6)
  320. match_array(actual[1], expected[1].asnumpy(), error=6)
  321. actual = onp.linspace(start, stop, num=20, retstep=True,
  322. endpoint=False, dtype=onp.int16)
  323. expected = mnp.linspace(to_tensor(start), to_tensor(stop), num=20,
  324. retstep=True, endpoint=False, dtype=mnp.int16)
  325. match_array(actual[0], expected[0].asnumpy(), error=6)
  326. match_array(actual[1], expected[1].asnumpy(), error=6)
  327. for axis in range(2):
  328. actual = onp.linspace(start, stop, num=20, retstep=False,
  329. endpoint=False, dtype=onp.float32, axis=axis)
  330. expected = mnp.linspace(to_tensor(start), to_tensor(stop), num=20,
  331. retstep=False, endpoint=False, dtype=mnp.float32, axis=axis)
  332. match_array(actual, expected.asnumpy(), error=6)
  333. @pytest.mark.level1
  334. @pytest.mark.platform_arm_ascend_training
  335. @pytest.mark.platform_x86_ascend_training
  336. @pytest.mark.platform_x86_gpu_training
  337. @pytest.mark.platform_x86_cpu
  338. @pytest.mark.env_onecard
  339. def test_logspace():
  340. actual = onp.logspace(2.0, 3.0, dtype=onp.float32)
  341. expected = mnp.logspace(2.0, 3.0).asnumpy()
  342. match_array(actual, expected, error=3)
  343. actual = onp.logspace(2.0, 3.0, num=5, dtype=onp.float32)
  344. expected = mnp.logspace(2.0, 3.0, num=5).asnumpy()
  345. match_array(actual, expected, error=3)
  346. actual = onp.logspace(
  347. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  348. expected = mnp.logspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  349. match_array(actual, expected, error=3)
  350. actual = onp.logspace(2.0, [3, 4, 5], num=5, base=2,
  351. endpoint=False, dtype=onp.float32)
  352. expected = mnp.logspace(
  353. 2.0, [3, 4, 5], num=5, base=2, endpoint=False).asnumpy()
  354. match_array(actual, expected, error=3)
  355. @pytest.mark.level1
  356. @pytest.mark.platform_arm_ascend_training
  357. @pytest.mark.platform_x86_ascend_training
  358. @pytest.mark.platform_x86_gpu_training
  359. @pytest.mark.platform_x86_cpu
  360. @pytest.mark.env_onecard
  361. def test_empty():
  362. test_case = Cases()
  363. for shape in test_case.all_shapes:
  364. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  365. test_case.onp_dtypes):
  366. actual = mnp.empty(shape, mnp_dtype).asnumpy()
  367. expected = onp.empty(shape, onp_dtype)
  368. match_meta(actual, expected)
  369. @pytest.mark.level1
  370. @pytest.mark.platform_arm_ascend_training
  371. @pytest.mark.platform_x86_ascend_training
  372. @pytest.mark.platform_x86_gpu_training
  373. @pytest.mark.platform_x86_cpu
  374. @pytest.mark.env_onecard
  375. def test_empty_like():
  376. test_case = Cases()
  377. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  378. actual = mnp.empty_like(mnp_proto).asnumpy()
  379. expected = onp.empty_like(onp_proto)
  380. assert actual.shape == expected.shape
  381. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  382. test_case.onp_dtypes):
  383. actual = mnp.empty_like(mnp_proto, dtype=mnp_dtype).asnumpy()
  384. expected = onp.empty_like(onp_proto, dtype=onp_dtype)
  385. match_meta(actual, expected)
  386. def run_x_like(mnp_fn, onp_fn):
  387. test_case = Cases()
  388. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  389. actual = mnp_fn(mnp_proto).asnumpy()
  390. expected = onp_fn(onp_proto)
  391. match_array(actual, expected)
  392. for shape in test_case.all_shapes:
  393. actual = mnp_fn(mnp_proto, shape=shape).asnumpy()
  394. expected = onp_fn(onp_proto, shape=shape)
  395. match_array(actual, expected)
  396. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  397. test_case.onp_dtypes):
  398. actual = mnp_fn(mnp_proto, dtype=mnp_dtype).asnumpy()
  399. expected = onp_fn(onp_proto, dtype=onp_dtype)
  400. match_array(actual, expected)
  401. actual = mnp_fn(mnp_proto, dtype=mnp_dtype,
  402. shape=shape).asnumpy()
  403. expected = onp_fn(onp_proto, dtype=onp_dtype, shape=shape)
  404. match_array(actual, expected)
  405. @pytest.mark.level1
  406. @pytest.mark.platform_arm_ascend_training
  407. @pytest.mark.platform_x86_ascend_training
  408. @pytest.mark.platform_x86_gpu_training
  409. @pytest.mark.platform_x86_cpu
  410. @pytest.mark.env_onecard
  411. def test_ones_like():
  412. run_x_like(mnp.ones_like, onp.ones_like)
  413. @pytest.mark.level1
  414. @pytest.mark.platform_arm_ascend_training
  415. @pytest.mark.platform_x86_ascend_training
  416. @pytest.mark.platform_x86_gpu_training
  417. @pytest.mark.platform_x86_cpu
  418. @pytest.mark.env_onecard
  419. def test_zeros_like():
  420. run_x_like(mnp.zeros_like, onp.zeros_like)
  421. @pytest.mark.level1
  422. @pytest.mark.platform_arm_ascend_training
  423. @pytest.mark.platform_x86_ascend_training
  424. @pytest.mark.platform_x86_gpu_training
  425. @pytest.mark.platform_x86_cpu
  426. @pytest.mark.env_onecard
  427. def test_full_like():
  428. test_case = Cases()
  429. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  430. shape = onp.zeros_like(onp_proto).shape
  431. fill_value = rand_int()
  432. actual = mnp.full_like(mnp_proto, to_tensor(fill_value)).asnumpy()
  433. expected = onp.full_like(onp_proto, fill_value)
  434. match_array(actual, expected)
  435. for i in range(len(shape) - 1, 0, -1):
  436. fill_value = rand_int(*shape[i:])
  437. actual = mnp.full_like(mnp_proto, to_tensor(fill_value)).asnumpy()
  438. expected = onp.full_like(onp_proto, fill_value)
  439. match_array(actual, expected)
  440. fill_value = rand_int(1, *shape[i + 1:])
  441. actual = mnp.full_like(mnp_proto, to_tensor(fill_value)).asnumpy()
  442. expected = onp.full_like(onp_proto, fill_value)
  443. match_array(actual, expected)
  444. @pytest.mark.level1
  445. @pytest.mark.platform_arm_ascend_training
  446. @pytest.mark.platform_x86_ascend_training
  447. @pytest.mark.platform_x86_gpu_training
  448. @pytest.mark.platform_x86_cpu
  449. @pytest.mark.env_onecard
  450. def test_tri_triu_tril():
  451. x = mnp.ones((16, 32), dtype="bool")
  452. match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
  453. match_array(mnp.tril(x, -1).asnumpy(), onp.tril(x.asnumpy(), -1))
  454. match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
  455. match_array(mnp.triu(x, -1).asnumpy(), onp.triu(x.asnumpy(), -1))
  456. x = mnp.ones((64, 64), dtype="uint8")
  457. match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
  458. match_array(mnp.tril(x, 25).asnumpy(), onp.tril(x.asnumpy(), 25))
  459. match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
  460. match_array(mnp.triu(x, 25).asnumpy(), onp.triu(x.asnumpy(), 25))
  461. match_array(mnp.tri(64, 64).asnumpy(), onp.tri(64, 64))
  462. match_array(mnp.tri(64, 64, -10).asnumpy(), onp.tri(64, 64, -10))
  463. @pytest.mark.level1
  464. @pytest.mark.platform_x86_gpu_training
  465. @pytest.mark.platform_x86_cpu
  466. @pytest.mark.env_onecard
  467. def test_nancumsum():
  468. x = rand_int(2, 3, 4, 5)
  469. x[0][2][1][3] = onp.nan
  470. x[1][0][2][4] = onp.nan
  471. x[1][1][1][1] = onp.nan
  472. match_res(mnp.nancumsum, onp.nancumsum, x)
  473. match_res(mnp.nancumsum, onp.nancumsum, x, axis=-2)
  474. match_res(mnp.nancumsum, onp.nancumsum, x, axis=0)
  475. match_res(mnp.nancumsum, onp.nancumsum, x, axis=3)
  476. def mnp_diagonal(arr):
  477. return mnp.diagonal(arr, offset=2, axis1=-1, axis2=0)
  478. def onp_diagonal(arr):
  479. return onp.diagonal(arr, offset=2, axis1=-1, axis2=0)
  480. @pytest.mark.level1
  481. @pytest.mark.platform_arm_ascend_training
  482. @pytest.mark.platform_x86_ascend_training
  483. @pytest.mark.platform_x86_gpu_training
  484. @pytest.mark.platform_x86_cpu
  485. @pytest.mark.env_onecard
  486. def test_diagonal():
  487. arr = rand_int(3, 5)
  488. for i in [-1, 0, 2]:
  489. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=1)
  490. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=1, axis2=0)
  491. arr = rand_int(7, 4, 9)
  492. for i in [-1, 0, 2]:
  493. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=-1)
  494. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=-2, axis2=2)
  495. match_res(mnp.diagonal, onp.diagonal, arr,
  496. offset=i, axis1=-1, axis2=-2)
  497. def mnp_trace(arr):
  498. return mnp.trace(arr, offset=4, axis1=1, axis2=2)
  499. def onp_trace(arr):
  500. return onp.trace(arr, offset=4, axis1=1, axis2=2)
  501. @pytest.mark.level0
  502. @pytest.mark.platform_arm_ascend_training
  503. @pytest.mark.platform_x86_ascend_training
  504. @pytest.mark.platform_x86_gpu_training
  505. @pytest.mark.platform_x86_cpu
  506. @pytest.mark.env_onecard
  507. def test_trace():
  508. arr = rand_int(3, 5)
  509. match_res(mnp.trace, onp.trace, arr, offset=-1, axis1=0, axis2=1)
  510. arr = rand_int(7, 4, 9)
  511. match_res(mnp.trace, onp.trace, arr, offset=0, axis1=-2, axis2=2)
  512. def mnp_meshgrid(*xi):
  513. a = mnp.meshgrid(*xi)
  514. b = mnp.meshgrid(*xi, sparse=True)
  515. c = mnp.meshgrid(*xi, indexing='ij')
  516. d = mnp.meshgrid(*xi, sparse=False, indexing='ij')
  517. return a, b, c, d
  518. def onp_meshgrid(*xi):
  519. a = onp.meshgrid(*xi)
  520. b = onp.meshgrid(*xi, sparse=True)
  521. c = onp.meshgrid(*xi, indexing='ij')
  522. d = onp.meshgrid(*xi, sparse=False, indexing='ij')
  523. return a, b, c, d
  524. @pytest.mark.level0
  525. @pytest.mark.platform_arm_ascend_training
  526. @pytest.mark.platform_x86_ascend_training
  527. @pytest.mark.platform_x86_gpu_training
  528. @pytest.mark.platform_x86_cpu
  529. @pytest.mark.env_onecard
  530. def test_meshgrid():
  531. xi = (onp.full(3, 2), onp.full(1, 5), onp.full(
  532. (2, 3), 9), onp.full((4, 5, 6), 7))
  533. for i in range(len(xi)):
  534. arrs = xi[i:]
  535. mnp_arrs = map(to_tensor, arrs)
  536. for mnp_res, onp_res in zip(mnp_meshgrid(*mnp_arrs), onp_meshgrid(*arrs)):
  537. match_all_arrays(mnp_res, onp_res)
  538. @pytest.mark.level1
  539. @pytest.mark.platform_arm_ascend_training
  540. @pytest.mark.platform_x86_ascend_training
  541. @pytest.mark.platform_x86_gpu_training
  542. @pytest.mark.platform_x86_cpu
  543. @pytest.mark.env_onecard
  544. def test_diagflat():
  545. arrs = [rand_int(2, 3)]
  546. for arr in arrs:
  547. for i in [-2, 0, 7]:
  548. match_res(mnp.diagflat, onp.diagflat, arr, k=i)
  549. @pytest.mark.level1
  550. @pytest.mark.platform_arm_ascend_training
  551. @pytest.mark.platform_x86_ascend_training
  552. @pytest.mark.platform_x86_gpu_training
  553. @pytest.mark.platform_x86_cpu
  554. @pytest.mark.env_onecard
  555. def test_diag():
  556. arrs = [rand_int(7), rand_int(5, 5), rand_int(3, 8), rand_int(9, 6)]
  557. for arr in arrs:
  558. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  559. match_res(mnp.diag, onp.diag, arr, k=i)
  560. @pytest.mark.level1
  561. @pytest.mark.platform_arm_ascend_training
  562. @pytest.mark.platform_x86_ascend_training
  563. @pytest.mark.platform_x86_gpu_training
  564. @pytest.mark.platform_x86_cpu
  565. @pytest.mark.env_onecard
  566. def test_diag_indices():
  567. mnp_res = mnp.diag_indices(5, 7)
  568. onp_res = onp.diag_indices(5, 7)
  569. match_all_arrays(mnp_res, onp_res)
  570. def mnp_ix_(*args):
  571. return mnp.ix_(*args)
  572. def onp_ix_(*args):
  573. return onp.ix_(*args)
  574. @pytest.mark.level1
  575. @pytest.mark.platform_arm_ascend_training
  576. @pytest.mark.platform_x86_ascend_training
  577. @pytest.mark.platform_x86_gpu_training
  578. @pytest.mark.platform_x86_cpu
  579. @pytest.mark.env_onecard
  580. def test_ix_():
  581. arrs = [rand_int(i + 1) for i in range(10)]
  582. for i in range(10):
  583. test_arrs = arrs[:i + 1]
  584. match_res(mnp_ix_, onp_ix_, *test_arrs)
  585. def mnp_indices():
  586. a = mnp.indices((2, 3))
  587. b = mnp.indices((2, 3, 4), sparse=True)
  588. return a, b
  589. def onp_indices():
  590. a = onp.indices((2, 3))
  591. b = onp.indices((2, 3, 4), sparse=True)
  592. return a, b
  593. def test_indices():
  594. run_multi_test(mnp_indices, onp_indices, ())
  595. @pytest.mark.level1
  596. @pytest.mark.platform_arm_ascend_training
  597. @pytest.mark.platform_x86_ascend_training
  598. @pytest.mark.platform_x86_gpu_training
  599. @pytest.mark.platform_x86_cpu
  600. @pytest.mark.env_onecard
  601. def test_geomspace():
  602. start = onp.arange(1, 7).reshape(2, 3)
  603. end = [1000, 2000, 3000]
  604. match_array(mnp.geomspace(1, 256, num=9).asnumpy(),
  605. onp.geomspace(1, 256, num=9), error=1)
  606. match_array(mnp.geomspace(1, 256, num=8, endpoint=False).asnumpy(),
  607. onp.geomspace(1, 256, num=8, endpoint=False), error=1)
  608. match_array(mnp.geomspace(to_tensor(start), end, num=4).asnumpy(),
  609. onp.geomspace(start, end, num=4), error=1)
  610. match_array(mnp.geomspace(to_tensor(start), end, num=4, endpoint=False).asnumpy(),
  611. onp.geomspace(start, end, num=4, endpoint=False), error=1)
  612. match_array(mnp.geomspace(to_tensor(start), end, num=4, axis=-1).asnumpy(),
  613. onp.geomspace(start, end, num=4, axis=-1), error=1)
  614. match_array(mnp.geomspace(to_tensor(start), end, num=4, endpoint=False, axis=-1).asnumpy(),
  615. onp.geomspace(start, end, num=4, endpoint=False, axis=-1), error=1)
  616. start = onp.arange(1, 1 + 2*3*4*5).reshape(2, 3, 4, 5)
  617. end = [1000, 2000, 3000, 4000, 5000]
  618. for i in range(-5, 5):
  619. match_array(mnp.geomspace(to_tensor(start), end, num=4, axis=i).asnumpy(),
  620. onp.geomspace(start, end, num=4, axis=i), error=1)
  621. @pytest.mark.level1
  622. @pytest.mark.platform_arm_ascend_training
  623. @pytest.mark.platform_x86_ascend_training
  624. @pytest.mark.platform_x86_gpu_training
  625. @pytest.mark.platform_x86_cpu
  626. @pytest.mark.env_onecard
  627. def test_vander():
  628. arrs = [rand_int(i + 3) for i in range(3)]
  629. for i in range(3):
  630. mnp_vander = mnp.vander(to_tensor(arrs[i]))
  631. onp_vander = onp.vander(arrs[i])
  632. match_all_arrays(mnp_vander, onp_vander, error=1e-4)
  633. mnp_vander = mnp.vander(to_tensor(arrs[i]), N=2, increasing=True)
  634. onp_vander = onp.vander(arrs[i], N=2, increasing=True)
  635. match_all_arrays(mnp_vander, onp_vander, error=1e-4)
  636. @pytest.mark.level0
  637. @pytest.mark.platform_arm_ascend_training
  638. @pytest.mark.platform_x86_ascend_training
  639. @pytest.mark.platform_x86_gpu_training
  640. @pytest.mark.platform_x86_cpu
  641. @pytest.mark.env_onecard
  642. def test_tensor_fill():
  643. x = rand_int(2, 1, 4).astype(onp.float32)
  644. mnp_x = to_tensor(x)
  645. x.fill(6)
  646. match_all_arrays(mnp_x.fill(6), x)
  647. x.fill(None)
  648. match_all_arrays(mnp_x.fill(None), x)
  649. @pytest.mark.level1
  650. @pytest.mark.platform_arm_ascend_training
  651. @pytest.mark.platform_x86_ascend_training
  652. @pytest.mark.platform_x86_gpu_training
  653. @pytest.mark.platform_x86_cpu
  654. @pytest.mark.env_onecard
  655. def test_bartlett():
  656. for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
  657. match_all_arrays(mnp.bartlett(i), onp.bartlett(i), error=3)
  658. @pytest.mark.level1
  659. @pytest.mark.platform_arm_ascend_training
  660. @pytest.mark.platform_x86_ascend_training
  661. @pytest.mark.platform_x86_gpu_training
  662. @pytest.mark.platform_x86_cpu
  663. @pytest.mark.env_onecard
  664. def test_blackman():
  665. for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
  666. match_all_arrays(mnp.blackman(i), onp.blackman(i), error=3)
  667. @pytest.mark.level1
  668. @pytest.mark.platform_arm_ascend_training
  669. @pytest.mark.platform_x86_ascend_training
  670. @pytest.mark.platform_x86_gpu_training
  671. @pytest.mark.platform_x86_cpu
  672. @pytest.mark.env_onecard
  673. def test_hamming():
  674. for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
  675. match_all_arrays(mnp.hamming(i), onp.hamming(i), error=3)
  676. @pytest.mark.level1
  677. @pytest.mark.platform_arm_ascend_training
  678. @pytest.mark.platform_x86_ascend_training
  679. @pytest.mark.platform_x86_gpu_training
  680. @pytest.mark.platform_x86_cpu
  681. @pytest.mark.env_onecard
  682. def test_hanning():
  683. for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
  684. match_all_arrays(mnp.hanning(i), onp.hanning(i), error=3)
  685. @pytest.mark.level1
  686. @pytest.mark.platform_arm_ascend_training
  687. @pytest.mark.platform_x86_ascend_training
  688. @pytest.mark.platform_x86_gpu_training
  689. @pytest.mark.platform_x86_cpu
  690. @pytest.mark.env_onecard
  691. def test_triu_indices():
  692. m = rand_int().tolist()
  693. n = rand_int().tolist()
  694. k = rand_int().tolist()
  695. mnp_res = mnp.triu_indices(n, k, m)
  696. onp_res = onp.triu_indices(n, k, m)
  697. match_all_arrays(mnp_res, onp_res)
  698. @pytest.mark.level1
  699. @pytest.mark.platform_arm_ascend_training
  700. @pytest.mark.platform_x86_ascend_training
  701. @pytest.mark.platform_x86_gpu_training
  702. @pytest.mark.platform_x86_cpu
  703. @pytest.mark.env_onecard
  704. def test_tril_indices():
  705. m = rand_int().tolist()
  706. n = rand_int().tolist()
  707. k = rand_int().tolist()
  708. mnp_res = mnp.tril_indices(n, k, m)
  709. onp_res = onp.tril_indices(n, k, m)
  710. match_all_arrays(mnp_res, onp_res)
  711. @pytest.mark.level1
  712. @pytest.mark.platform_arm_ascend_training
  713. @pytest.mark.platform_x86_ascend_training
  714. @pytest.mark.platform_x86_gpu_training
  715. @pytest.mark.platform_x86_cpu
  716. @pytest.mark.env_onecard
  717. def test_triu_indices_from():
  718. m = int(rand_int().tolist())
  719. n = int(rand_int().tolist())
  720. t = mnp.asarray(rand_int(m, n).tolist())
  721. k = rand_int().tolist()
  722. mnp_res = mnp.triu_indices_from(t, k)
  723. onp_res = onp.triu_indices_from(t.asnumpy(), k)
  724. match_all_arrays(mnp_res, onp_res)
  725. @pytest.mark.level1
  726. @pytest.mark.platform_arm_ascend_training
  727. @pytest.mark.platform_x86_ascend_training
  728. @pytest.mark.platform_x86_gpu_training
  729. @pytest.mark.platform_x86_cpu
  730. @pytest.mark.env_onecard
  731. def test_tril_indices_from():
  732. m = int(rand_int().tolist())
  733. n = int(rand_int().tolist())
  734. t = mnp.asarray(rand_int(m, n).tolist())
  735. k = rand_int().tolist()
  736. mnp_res = mnp.tril_indices_from(t, k)
  737. onp_res = onp.tril_indices_from(t.asnumpy(), k)
  738. match_all_arrays(mnp_res, onp_res)
  739. @pytest.mark.level0
  740. @pytest.mark.platform_arm_ascend_training
  741. @pytest.mark.platform_x86_ascend_training
  742. @pytest.mark.platform_x86_gpu_training
  743. @pytest.mark.platform_x86_cpu
  744. @pytest.mark.env_onecard
  745. def test_histogram_bin_edges():
  746. x = onp.random.randint(-10, 10, 10)
  747. match_res(mnp.histogram_bin_edges, onp.histogram_bin_edges, x, onp.arange(5))
  748. match_res(mnp.histogram_bin_edges, onp.histogram_bin_edges, x, bins=(1, 2, 3), range=None, error=3)
  749. match_res(mnp.histogram_bin_edges, onp.histogram_bin_edges, x, bins=10, range=(2, 20), error=3)
  750. @pytest.mark.level1
  751. @pytest.mark.platform_arm_ascend_training
  752. @pytest.mark.platform_x86_ascend_training
  753. @pytest.mark.platform_x86_gpu_training
  754. @pytest.mark.platform_x86_cpu
  755. @pytest.mark.env_onecard
  756. def test_randn():
  757. """
  758. Feature: Numpy method randn.
  759. Description: Test numpy method randn.
  760. Expectation: No exception.
  761. """
  762. set_seed(1)
  763. t1 = mnp.randn(1, 2, 3)
  764. t2 = mnp.randn(1, 2, 3)
  765. assert (t1.asnumpy() == t2.asnumpy()).all()
  766. with pytest.raises(ValueError):
  767. mnp.randn(dtype="int32")
  768. with pytest.raises(ValueError):
  769. mnp.randn(dtype=mstype.int32)
  770. with pytest.raises(TypeError):
  771. mnp.randn({1})
  772. with pytest.raises(TypeError):
  773. mnp.randn(1, 1.2, 2)
  774. @pytest.mark.level1
  775. @pytest.mark.platform_arm_ascend_training
  776. @pytest.mark.platform_x86_ascend_training
  777. @pytest.mark.platform_x86_gpu_training
  778. @pytest.mark.platform_x86_cpu
  779. @pytest.mark.env_onecard
  780. def test_rand():
  781. """
  782. Feature: Numpy method rand.
  783. Description: Test numpy method rand.
  784. Expectation: No exception.
  785. """
  786. set_seed(1)
  787. t1 = mnp.rand(1, 2, 3)
  788. t2 = mnp.rand(1, 2, 3)
  789. assert (t1.asnumpy() == t2.asnumpy()).all()
  790. with pytest.raises(ValueError):
  791. mnp.rand(dtype="int32")
  792. with pytest.raises(ValueError):
  793. mnp.rand(dtype=mstype.int32)
  794. with pytest.raises(TypeError):
  795. mnp.rand({1})
  796. with pytest.raises(TypeError):
  797. mnp.rand(1, 1.2, 2)
  798. @pytest.mark.level1
  799. @pytest.mark.platform_arm_ascend_training
  800. @pytest.mark.platform_x86_ascend_training
  801. @pytest.mark.platform_x86_gpu_training
  802. @pytest.mark.platform_x86_cpu
  803. @pytest.mark.env_onecard
  804. def test_randint():
  805. """
  806. Feature: Numpy method randint.
  807. Description: Test numpy method randint.
  808. Expectation: No exception.
  809. """
  810. set_seed(1)
  811. t1 = mnp.randint(1, 5, 3)
  812. t2 = mnp.randint(1, 5, 3)
  813. assert (t1.asnumpy() == t2.asnumpy()).all()
  814. with pytest.raises(TypeError):
  815. mnp.randint(1.2)
  816. with pytest.raises(ValueError):
  817. mnp.randint(0)
  818. with pytest.raises(TypeError):
  819. mnp.randint(1, 1.2)
  820. with pytest.raises(ValueError):
  821. mnp.randint(2, 1)
  822. with pytest.raises(ValueError):
  823. mnp.randint(1, dtype="float")
  824. with pytest.raises(ValueError):
  825. mnp.randint(1, dtype=mstype.float32)
  826. @pytest.mark.level1
  827. @pytest.mark.platform_arm_ascend_training
  828. @pytest.mark.platform_x86_ascend_training
  829. @pytest.mark.platform_x86_gpu_training
  830. @pytest.mark.platform_x86_cpu
  831. @pytest.mark.env_onecard
  832. def test_ops_arange():
  833. """
  834. Feature: Ops function arange.
  835. Description: Test ops function arange.
  836. Expectation: No exception.
  837. """
  838. actual = onp.arange(5)
  839. expected = F.arange(5).asnumpy()
  840. match_array(actual, expected)
  841. actual = onp.arange(0, 5)
  842. expected = F.arange(0, 5).asnumpy()
  843. match_array(actual, expected)
  844. actual = onp.arange(5, step=0.2)
  845. expected = F.arange(5, step=0.2).asnumpy()
  846. match_array(actual, expected)
  847. actual = onp.arange(0.1, 0.9)
  848. expected = F.arange(0.1, 0.9).asnumpy()
  849. match_array(actual, expected)
  850. with pytest.raises(TypeError):
  851. F.arange([1])
  852. with pytest.raises(ValueError):
  853. F.arange(10, 1)
  854. @pytest.mark.level1
  855. @pytest.mark.platform_arm_ascend_training
  856. @pytest.mark.platform_x86_ascend_training
  857. @pytest.mark.platform_x86_gpu_training
  858. @pytest.mark.platform_x86_cpu
  859. @pytest.mark.env_onecard
  860. def test_asarray_exception():
  861. with pytest.raises(TypeError):
  862. mnp.asarray({1, 2, 3})
  863. @pytest.mark.level1
  864. @pytest.mark.platform_arm_ascend_training
  865. @pytest.mark.platform_x86_ascend_training
  866. @pytest.mark.platform_x86_gpu_training
  867. @pytest.mark.platform_x86_cpu
  868. @pytest.mark.env_onecard
  869. def test_linspace_exception():
  870. with pytest.raises(TypeError):
  871. mnp.linspace(0, 1, num=2.5)
  872. @pytest.mark.level1
  873. @pytest.mark.platform_arm_ascend_training
  874. @pytest.mark.platform_x86_ascend_training
  875. @pytest.mark.platform_x86_gpu_training
  876. @pytest.mark.platform_x86_cpu
  877. @pytest.mark.env_onecard
  878. def test_empty_like_exception():
  879. with pytest.raises(ValueError):
  880. mnp.empty_like([[1, 2, 3], [4, 5]])
  881. @pytest.mark.level0
  882. @pytest.mark.platform_arm_ascend_training
  883. @pytest.mark.platform_x86_ascend_training
  884. @pytest.mark.platform_x86_gpu_training
  885. @pytest.mark.platform_x86_cpu
  886. @pytest.mark.env_onecard
  887. def test_pad():
  888. x_np = onp.random.random([2, 3, 4]).astype("float32")
  889. x_ms = mnp.asarray(x_np.tolist())
  890. # pad constant
  891. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)))
  892. onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)))
  893. match_all_arrays(mnp_res, onp_res, error=1e-5)
  894. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 3), (4, 5)), constant_values=((3, 4), (5, 6), (7, 8)))
  895. onp_res = onp.pad(x_np, ((1, 1), (2, 3), (4, 5)), constant_values=((3, 4), (5, 6), (7, 8)))
  896. match_all_arrays(mnp_res, onp_res, error=1e-5)
  897. # pad statistic
  898. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="mean", stat_length=((1, 2), (2, 10), (3, 4)))
  899. onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="mean", stat_length=((1, 2), (2, 10), (3, 4)))
  900. match_all_arrays(mnp_res, onp_res, error=1e-5)
  901. # pad edge
  902. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="edge")
  903. onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="edge")
  904. match_all_arrays(mnp_res, onp_res, error=1e-5)
  905. # pad wrap
  906. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="wrap")
  907. onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="wrap")
  908. match_all_arrays(mnp_res, onp_res, error=1e-5)
  909. # pad linear_ramp
  910. mnp_res = mnp.pad(x_ms, ((1, 3), (5, 2), (3, 0)), mode="linear_ramp", end_values=((0, 10), (9, 1), (-10, 99)))
  911. onp_res = onp.pad(x_np, ((1, 3), (5, 2), (3, 0)), mode="linear_ramp", end_values=((0, 10), (9, 1), (-10, 99)))
  912. match_all_arrays(mnp_res, onp_res, error=1e-5)
  913. def pad_with_msfunc(vector, pad_width, iaxis, kwargs):
  914. pad_value = kwargs.get('padder', 10)
  915. vector[:pad_width[0]] = pad_value
  916. vector[-pad_width[1]:] = pad_value
  917. return vector
  918. def pad_with_npfunc(vector, pad_width, iaxis, kwargs):
  919. pad_value = kwargs.get('padder', 10)
  920. vector[:pad_width[0]] = pad_value
  921. vector[-pad_width[1]:] = pad_value
  922. @pytest.mark.level0
  923. @pytest.mark.platform_x86_gpu_training
  924. @pytest.mark.env_onecard
  925. def test_pad_gpu():
  926. x_np = onp.random.random([2, 1, 4, 3]).astype("float32")
  927. x_ms = mnp.asarray(x_np.tolist())
  928. # pad symmetric odd
  929. mnp_res = mnp.pad(x_ms, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='symmetric', reflect_type='odd')
  930. onp_res = onp.pad(x_np, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='symmetric', reflect_type='odd')
  931. match_all_arrays(mnp_res, onp_res, error=1e-5)
  932. # pad symmetric even
  933. mnp_res = mnp.pad(x_ms, ((10, 13), (5, 12), (3, 0), (2, 6)), mode='symmetric', reflect_type='even')
  934. onp_res = onp.pad(x_np, ((10, 13), (5, 12), (3, 0), (2, 6)), mode='symmetric', reflect_type='even')
  935. match_all_arrays(mnp_res, onp_res, error=1e-5)
  936. # pad reflect odd
  937. mnp_res = mnp.pad(x_ms, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='reflect', reflect_type='odd')
  938. onp_res = onp.pad(x_np, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='reflect', reflect_type='odd')
  939. match_all_arrays(mnp_res, onp_res, error=1e-5)
  940. # pad reflect even
  941. mnp_res = mnp.pad(x_ms, ((10, 13)), mode='reflect', reflect_type='even')
  942. onp_res = onp.pad(x_np, ((10, 13)), mode='reflect', reflect_type='even')
  943. match_all_arrays(mnp_res, onp_res, error=1e-5)
  944. # pad func
  945. x_np = onp.random.random([2, 4]).astype("float32")
  946. x_ms = mnp.asarray(x_np.tolist())
  947. mnp_res = mnp.pad(x_ms, ((5, 5)), mode=pad_with_msfunc, padder=99)
  948. onp_res = onp.pad(x_np, ((5, 5)), mode=pad_with_npfunc, padder=99)
  949. match_all_arrays(mnp_res, onp_res, error=1e-5)