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

4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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, run_multi_test, to_tensor
  21. class Cases():
  22. def __init__(self):
  23. self.all_shapes = [
  24. 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).tolist(),
  37. onp.random.random((100, 100)).astype(onp.bool).tolist()]
  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((1, 3, 1, 2, 5)),
  84. mnp.ones((2, 7, 1)),
  85. [mnp.ones(3), (1, 2, 3), mnp.ones(3), [4, 5, 6]],
  86. ([(1, 2), mnp.ones(2)], (mnp.ones(2), [3, 4])),
  87. ]
  88. self.onp_prototypes = [
  89. onp.ones((2, 3, 4)),
  90. onp.ones((1, 3, 1, 2, 5)),
  91. onp.ones((2, 7, 1)),
  92. [onp.ones(3), (1, 2, 3), onp.ones(3), [4, 5, 6]],
  93. ([(1, 2), onp.ones(2)], (onp.ones(2), [3, 4])),
  94. ]
  95. @pytest.mark.level1
  96. @pytest.mark.platform_arm_ascend_training
  97. @pytest.mark.platform_x86_ascend_training
  98. @pytest.mark.platform_x86_gpu_training
  99. @pytest.mark.platform_x86_cpu
  100. @pytest.mark.env_onecard
  101. def test_asarray():
  102. test_case = Cases()
  103. for array in test_case.array_sets:
  104. # Check for dtype matching
  105. actual = onp.asarray(array)
  106. expected = mnp.asarray(array).asnumpy()
  107. # Since we set float32/int32 as the default dtype in mindspore, we need
  108. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  109. if actual.dtype is onp.dtype('float64'):
  110. assert expected.dtype == onp.dtype('float32')
  111. elif actual.dtype is onp.dtype('int64'):
  112. assert expected.dtype == onp.dtype('int32')
  113. else:
  114. assert actual.dtype == expected.dtype
  115. match_array(actual, expected, error=7)
  116. for i in range(len(test_case.onp_dtypes)):
  117. actual = onp.asarray(array, test_case.onp_dtypes[i])
  118. expected = mnp.asarray(array, test_case.mnp_dtypes[i]).asnumpy()
  119. match_array(actual, expected, error=7)
  120. # Additional tests for nested tensor mixture
  121. mnp_input = [(mnp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  122. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  123. actual = onp.asarray(onp_input)
  124. expected = mnp.asarray(mnp_input).asnumpy()
  125. match_array(actual, expected, error=7)
  126. @pytest.mark.level1
  127. @pytest.mark.platform_arm_ascend_training
  128. @pytest.mark.platform_x86_ascend_training
  129. @pytest.mark.platform_x86_gpu_training
  130. @pytest.mark.platform_x86_cpu
  131. @pytest.mark.env_onecard
  132. def test_array():
  133. # array's function is very similar to asarray, so we mainly test the
  134. # `copy` argument.
  135. test_case = Cases()
  136. for array in test_case.array_sets:
  137. arr1 = mnp.asarray(array)
  138. arr2 = mnp.array(arr1, copy=False)
  139. arr3 = mnp.array(arr1)
  140. arr4 = mnp.asarray(array, dtype='int32')
  141. arr5 = mnp.asarray(arr4, dtype=mnp.int32)
  142. assert arr1 is arr2
  143. assert arr1 is not arr3
  144. assert arr4 is arr5
  145. # Additional tests for nested tensor/numpy_array mixture
  146. mnp_input = [(mnp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  147. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  148. actual = onp.array(onp_input)
  149. expected = mnp.array(mnp_input).asnumpy()
  150. match_array(actual, expected, error=7)
  151. @pytest.mark.level1
  152. @pytest.mark.platform_arm_ascend_training
  153. @pytest.mark.platform_x86_ascend_training
  154. @pytest.mark.platform_x86_gpu_training
  155. @pytest.mark.platform_x86_cpu
  156. @pytest.mark.env_onecard
  157. def test_asfarray():
  158. test_case = Cases()
  159. for array in test_case.array_sets:
  160. # Check for dtype matching
  161. actual = onp.asfarray(array)
  162. expected = mnp.asfarray(array).asnumpy()
  163. # Since we set float32/int32 as the default dtype in mindspore, we need
  164. # to make a conversion between numpy.asarray and mindspore.numpy.asarray
  165. if actual.dtype is onp.dtype('float64'):
  166. assert expected.dtype == onp.dtype('float32')
  167. else:
  168. assert actual.dtype == expected.dtype
  169. match_array(actual, expected, error=7)
  170. for i in range(len(test_case.onp_dtypes)):
  171. actual = onp.asfarray(array, test_case.onp_dtypes[i])
  172. expected = mnp.asfarray(array, test_case.mnp_dtypes[i]).asnumpy()
  173. match_array(actual, expected, error=7)
  174. # Additional tests for nested tensor/numpy_array mixture
  175. mnp_input = [(mnp.ones(3,), mnp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  176. onp_input = [(onp.ones(3,), onp.ones(3)), [[1, 1, 1], (1, 1, 1)]]
  177. actual = onp.asfarray(onp_input)
  178. expected = mnp.asfarray(mnp_input).asnumpy()
  179. match_array(actual, expected, error=7)
  180. @pytest.mark.level1
  181. @pytest.mark.platform_arm_ascend_training
  182. @pytest.mark.platform_x86_ascend_training
  183. @pytest.mark.platform_x86_gpu_training
  184. @pytest.mark.platform_x86_cpu
  185. @pytest.mark.env_onecard
  186. def test_zeros():
  187. test_case = Cases()
  188. for shape in test_case.all_shapes:
  189. for i in range(len(test_case.onp_dtypes)):
  190. actual = onp.zeros(shape, test_case.onp_dtypes[i])
  191. expected = mnp.zeros(shape, test_case.mnp_dtypes[i]).asnumpy()
  192. match_array(actual, expected)
  193. actual = onp.zeros(shape)
  194. expected = mnp.zeros(shape).asnumpy()
  195. match_array(actual, expected)
  196. @pytest.mark.level1
  197. @pytest.mark.platform_arm_ascend_training
  198. @pytest.mark.platform_x86_ascend_training
  199. @pytest.mark.platform_x86_gpu_training
  200. @pytest.mark.platform_x86_cpu
  201. @pytest.mark.env_onecard
  202. def test_ones():
  203. test_case = Cases()
  204. for shape in test_case.all_shapes:
  205. for i in range(len(test_case.onp_dtypes)):
  206. actual = onp.ones(shape, test_case.onp_dtypes[i])
  207. expected = mnp.ones(shape, test_case.mnp_dtypes[i]).asnumpy()
  208. match_array(actual, expected)
  209. actual = onp.ones(shape)
  210. expected = mnp.ones(shape).asnumpy()
  211. match_array(actual, expected)
  212. @pytest.mark.level1
  213. @pytest.mark.platform_arm_ascend_training
  214. @pytest.mark.platform_x86_ascend_training
  215. @pytest.mark.platform_x86_gpu_training
  216. @pytest.mark.platform_x86_cpu
  217. @pytest.mark.env_onecard
  218. def test_full():
  219. actual = onp.full((2, 2), [1, 2])
  220. expected = mnp.full((2, 2), [1, 2]).asnumpy()
  221. match_array(actual, expected)
  222. actual = onp.full((2, 3), True)
  223. expected = mnp.full((2, 3), True).asnumpy()
  224. match_array(actual, expected)
  225. actual = onp.full((3, 4, 5), 7.5)
  226. expected = mnp.full((3, 4, 5), 7.5).asnumpy()
  227. match_array(actual, expected)
  228. @pytest.mark.level1
  229. @pytest.mark.platform_arm_ascend_training
  230. @pytest.mark.platform_x86_ascend_training
  231. @pytest.mark.platform_x86_gpu_training
  232. @pytest.mark.platform_x86_cpu
  233. @pytest.mark.env_onecard
  234. def test_eye():
  235. test_case = Cases()
  236. for i in range(len(test_case.onp_dtypes)):
  237. for m in range(1, 5):
  238. actual = onp.eye(m, dtype=test_case.onp_dtypes[i])
  239. expected = mnp.eye(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
  240. match_array(actual, expected)
  241. for n in range(1, 5):
  242. for k in range(0, 5):
  243. actual = onp.eye(m, n, k, dtype=test_case.onp_dtypes[i])
  244. expected = mnp.eye(
  245. m, n, k, dtype=test_case.mnp_dtypes[i]).asnumpy()
  246. match_array(actual, expected)
  247. @pytest.mark.level1
  248. @pytest.mark.platform_arm_ascend_training
  249. @pytest.mark.platform_x86_ascend_training
  250. @pytest.mark.platform_x86_gpu_training
  251. @pytest.mark.platform_x86_cpu
  252. @pytest.mark.env_onecard
  253. def test_identity():
  254. test_case = Cases()
  255. for i in range(len(test_case.onp_dtypes)):
  256. for m in range(1, 5):
  257. actual = onp.identity(m, dtype=test_case.onp_dtypes[i])
  258. expected = mnp.identity(m, dtype=test_case.mnp_dtypes[i]).asnumpy()
  259. match_array(actual, expected)
  260. @pytest.mark.level1
  261. @pytest.mark.platform_arm_ascend_training
  262. @pytest.mark.platform_x86_ascend_training
  263. @pytest.mark.platform_x86_gpu_training
  264. @pytest.mark.platform_x86_cpu
  265. @pytest.mark.env_onecard
  266. def test_arange():
  267. actual = onp.arange(10)
  268. expected = mnp.arange(10).asnumpy()
  269. match_array(actual, expected)
  270. actual = onp.arange(0, 10)
  271. expected = mnp.arange(0, 10).asnumpy()
  272. match_array(actual, expected)
  273. actual = onp.arange(start=10)
  274. expected = mnp.arange(start=10).asnumpy()
  275. match_array(actual, expected)
  276. actual = onp.arange(start=10, step=0.1)
  277. expected = mnp.arange(start=10, step=0.1).asnumpy()
  278. match_array(actual, expected, error=6)
  279. actual = onp.arange(10, step=0.1)
  280. expected = mnp.arange(10, step=0.1).asnumpy()
  281. match_array(actual, expected, error=6)
  282. actual = onp.arange(0.1, 9.9)
  283. expected = mnp.arange(0.1, 9.9).asnumpy()
  284. match_array(actual, expected, error=6)
  285. @pytest.mark.level0
  286. @pytest.mark.platform_arm_ascend_training
  287. @pytest.mark.platform_x86_ascend_training
  288. @pytest.mark.platform_x86_gpu_training
  289. @pytest.mark.platform_x86_cpu
  290. @pytest.mark.env_onecard
  291. def test_linspace():
  292. actual = onp.linspace(2.0, 3.0, dtype=onp.float32)
  293. expected = mnp.linspace(2.0, 3.0).asnumpy()
  294. match_array(actual, expected, error=6)
  295. actual = onp.linspace(2.0, 3.0, num=5, dtype=onp.float32)
  296. expected = mnp.linspace(2.0, 3.0, num=5).asnumpy()
  297. match_array(actual, expected, error=6)
  298. actual = onp.linspace(
  299. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  300. expected = mnp.linspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  301. match_array(actual, expected, error=6)
  302. actual = onp.linspace(2.0, 3.0, num=5, retstep=True, dtype=onp.float32)
  303. expected = mnp.linspace(2.0, 3.0, num=5, retstep=True)
  304. match_array(actual[0], expected[0].asnumpy())
  305. assert actual[1] == expected[1].asnumpy()
  306. actual = onp.linspace(2.0, [3, 4, 5], num=5,
  307. endpoint=False, dtype=onp.float32)
  308. expected = mnp.linspace(
  309. 2.0, [3, 4, 5], num=5, endpoint=False).asnumpy()
  310. match_array(actual, expected, error=6)
  311. actual = onp.linspace(2.0, [[3, 4, 5]], num=5, endpoint=False, axis=2)
  312. expected = mnp.linspace(2.0, [[3, 4, 5]], num=5, endpoint=False, axis=2).asnumpy()
  313. match_array(actual, expected, error=6)
  314. start = onp.random.random([2, 1, 4]).astype("float32")
  315. stop = onp.random.random([1, 5, 1]).astype("float32")
  316. actual = onp.linspace(start, stop, num=20, retstep=True,
  317. endpoint=False, dtype=onp.float32)
  318. expected = mnp.linspace(to_tensor(start), to_tensor(stop), num=20,
  319. retstep=True, endpoint=False)
  320. match_array(actual[0], expected[0].asnumpy(), error=6)
  321. match_array(actual[1], expected[1].asnumpy(), error=6)
  322. actual = onp.linspace(start, stop, num=20, retstep=True,
  323. endpoint=False, dtype=onp.int16)
  324. expected = mnp.linspace(to_tensor(start), to_tensor(stop), num=20,
  325. retstep=True, endpoint=False, dtype=mnp.int16)
  326. match_array(actual[0], expected[0].asnumpy(), error=6)
  327. match_array(actual[1], expected[1].asnumpy(), error=6)
  328. for axis in range(2):
  329. actual = onp.linspace(start, stop, num=20, retstep=False,
  330. endpoint=False, dtype=onp.float32, axis=axis)
  331. expected = mnp.linspace(to_tensor(start), to_tensor(stop), num=20,
  332. retstep=False, endpoint=False, dtype=mnp.float32, axis=axis)
  333. match_array(actual, expected.asnumpy(), error=6)
  334. @pytest.mark.level1
  335. @pytest.mark.platform_arm_ascend_training
  336. @pytest.mark.platform_x86_ascend_training
  337. @pytest.mark.platform_x86_gpu_training
  338. @pytest.mark.platform_x86_cpu
  339. @pytest.mark.env_onecard
  340. def test_logspace():
  341. actual = onp.logspace(2.0, 3.0, dtype=onp.float32)
  342. expected = mnp.logspace(2.0, 3.0).asnumpy()
  343. match_array(actual, expected, error=3)
  344. actual = onp.logspace(2.0, 3.0, num=5, dtype=onp.float32)
  345. expected = mnp.logspace(2.0, 3.0, num=5).asnumpy()
  346. match_array(actual, expected, error=3)
  347. actual = onp.logspace(
  348. 2.0, 3.0, num=5, endpoint=False, dtype=onp.float32)
  349. expected = mnp.logspace(2.0, 3.0, num=5, endpoint=False).asnumpy()
  350. match_array(actual, expected, error=3)
  351. actual = onp.logspace(2.0, [3, 4, 5], num=5, base=2,
  352. endpoint=False, dtype=onp.float32)
  353. expected = mnp.logspace(
  354. 2.0, [3, 4, 5], num=5, base=2, endpoint=False).asnumpy()
  355. match_array(actual, expected, error=3)
  356. @pytest.mark.level1
  357. @pytest.mark.platform_arm_ascend_training
  358. @pytest.mark.platform_x86_ascend_training
  359. @pytest.mark.platform_x86_gpu_training
  360. @pytest.mark.platform_x86_cpu
  361. @pytest.mark.env_onecard
  362. def test_empty():
  363. test_case = Cases()
  364. for shape in test_case.all_shapes:
  365. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  366. test_case.onp_dtypes):
  367. actual = mnp.empty(shape, mnp_dtype).asnumpy()
  368. expected = onp.empty(shape, onp_dtype)
  369. match_meta(actual, expected)
  370. @pytest.mark.level1
  371. @pytest.mark.platform_arm_ascend_training
  372. @pytest.mark.platform_x86_ascend_training
  373. @pytest.mark.platform_x86_gpu_training
  374. @pytest.mark.platform_x86_cpu
  375. @pytest.mark.env_onecard
  376. def test_empty_like():
  377. test_case = Cases()
  378. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  379. actual = mnp.empty_like(mnp_proto).asnumpy()
  380. expected = onp.empty_like(onp_proto)
  381. assert actual.shape == expected.shape
  382. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  383. test_case.onp_dtypes):
  384. actual = mnp.empty_like(mnp_proto, dtype=mnp_dtype).asnumpy()
  385. expected = onp.empty_like(onp_proto, dtype=onp_dtype)
  386. match_meta(actual, expected)
  387. def run_x_like(mnp_fn, onp_fn):
  388. test_case = Cases()
  389. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  390. actual = mnp_fn(mnp_proto).asnumpy()
  391. expected = onp_fn(onp_proto)
  392. match_array(actual, expected)
  393. for shape in test_case.all_shapes:
  394. actual = mnp_fn(mnp_proto, shape=shape).asnumpy()
  395. expected = onp_fn(onp_proto, shape=shape)
  396. match_array(actual, expected)
  397. for mnp_dtype, onp_dtype in zip(test_case.mnp_dtypes,
  398. test_case.onp_dtypes):
  399. actual = mnp_fn(mnp_proto, dtype=mnp_dtype).asnumpy()
  400. expected = onp_fn(onp_proto, dtype=onp_dtype)
  401. match_array(actual, expected)
  402. actual = mnp_fn(mnp_proto, dtype=mnp_dtype,
  403. shape=shape).asnumpy()
  404. expected = onp_fn(onp_proto, dtype=onp_dtype, shape=shape)
  405. match_array(actual, expected)
  406. @pytest.mark.level1
  407. @pytest.mark.platform_arm_ascend_training
  408. @pytest.mark.platform_x86_ascend_training
  409. @pytest.mark.platform_x86_gpu_training
  410. @pytest.mark.platform_x86_cpu
  411. @pytest.mark.env_onecard
  412. def test_ones_like():
  413. run_x_like(mnp.ones_like, onp.ones_like)
  414. @pytest.mark.level1
  415. @pytest.mark.platform_arm_ascend_training
  416. @pytest.mark.platform_x86_ascend_training
  417. @pytest.mark.platform_x86_gpu_training
  418. @pytest.mark.platform_x86_cpu
  419. @pytest.mark.env_onecard
  420. def test_zeros_like():
  421. run_x_like(mnp.zeros_like, onp.zeros_like)
  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_full_like():
  429. test_case = Cases()
  430. for mnp_proto, onp_proto in zip(test_case.mnp_prototypes, test_case.onp_prototypes):
  431. shape = onp.zeros_like(onp_proto).shape
  432. fill_value = rand_int()
  433. actual = mnp.full_like(mnp_proto, to_tensor(fill_value)).asnumpy()
  434. expected = onp.full_like(onp_proto, fill_value)
  435. match_array(actual, expected)
  436. for i in range(len(shape) - 1, 0, -1):
  437. fill_value = rand_int(*shape[i:])
  438. actual = mnp.full_like(mnp_proto, to_tensor(fill_value)).asnumpy()
  439. expected = onp.full_like(onp_proto, fill_value)
  440. match_array(actual, expected)
  441. fill_value = rand_int(1, *shape[i + 1:])
  442. actual = mnp.full_like(mnp_proto, to_tensor(fill_value)).asnumpy()
  443. expected = onp.full_like(onp_proto, fill_value)
  444. match_array(actual, expected)
  445. @pytest.mark.level1
  446. @pytest.mark.platform_arm_ascend_training
  447. @pytest.mark.platform_x86_ascend_training
  448. @pytest.mark.platform_x86_gpu_training
  449. @pytest.mark.platform_x86_cpu
  450. @pytest.mark.env_onecard
  451. def test_tri_triu_tril():
  452. x = mnp.ones((16, 32), dtype="bool")
  453. match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
  454. match_array(mnp.tril(x, -1).asnumpy(), onp.tril(x.asnumpy(), -1))
  455. match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
  456. match_array(mnp.triu(x, -1).asnumpy(), onp.triu(x.asnumpy(), -1))
  457. x = mnp.ones((64, 64), dtype="uint8")
  458. match_array(mnp.tril(x).asnumpy(), onp.tril(x.asnumpy()))
  459. match_array(mnp.tril(x, 25).asnumpy(), onp.tril(x.asnumpy(), 25))
  460. match_array(mnp.triu(x).asnumpy(), onp.triu(x.asnumpy()))
  461. match_array(mnp.triu(x, 25).asnumpy(), onp.triu(x.asnumpy(), 25))
  462. match_array(mnp.tri(64, 64).asnumpy(), onp.tri(64, 64))
  463. match_array(mnp.tri(64, 64, -10).asnumpy(), onp.tri(64, 64, -10))
  464. @pytest.mark.level1
  465. @pytest.mark.platform_x86_gpu_training
  466. @pytest.mark.platform_x86_cpu
  467. @pytest.mark.env_onecard
  468. def test_nancumsum():
  469. x = rand_int(2, 3, 4, 5)
  470. x[0][2][1][3] = onp.nan
  471. x[1][0][2][4] = onp.nan
  472. x[1][1][1][1] = onp.nan
  473. match_res(mnp.nancumsum, onp.nancumsum, x)
  474. match_res(mnp.nancumsum, onp.nancumsum, x, axis=-2)
  475. match_res(mnp.nancumsum, onp.nancumsum, x, axis=0)
  476. match_res(mnp.nancumsum, onp.nancumsum, x, axis=3)
  477. def mnp_diagonal(arr):
  478. return mnp.diagonal(arr, offset=2, axis1=-1, axis2=0)
  479. def onp_diagonal(arr):
  480. return onp.diagonal(arr, offset=2, axis1=-1, axis2=0)
  481. @pytest.mark.level1
  482. @pytest.mark.platform_arm_ascend_training
  483. @pytest.mark.platform_x86_ascend_training
  484. @pytest.mark.platform_x86_gpu_training
  485. @pytest.mark.platform_x86_cpu
  486. @pytest.mark.env_onecard
  487. def test_diagonal():
  488. arr = rand_int(3, 5)
  489. for i in [-1, 0, 2]:
  490. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=1)
  491. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=1, axis2=0)
  492. arr = rand_int(7, 4, 9)
  493. for i in [-1, 0, 2]:
  494. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=0, axis2=-1)
  495. match_res(mnp.diagonal, onp.diagonal, arr, offset=i, axis1=-2, axis2=2)
  496. match_res(mnp.diagonal, onp.diagonal, arr,
  497. offset=i, axis1=-1, axis2=-2)
  498. def mnp_trace(arr):
  499. return mnp.trace(arr, offset=4, axis1=1, axis2=2)
  500. def onp_trace(arr):
  501. return onp.trace(arr, offset=4, axis1=1, axis2=2)
  502. @pytest.mark.level1
  503. @pytest.mark.platform_arm_ascend_training
  504. @pytest.mark.platform_x86_ascend_training
  505. @pytest.mark.platform_x86_gpu_training
  506. @pytest.mark.platform_x86_cpu
  507. @pytest.mark.env_onecard
  508. def test_trace():
  509. arr = rand_int(3, 5)
  510. for i in [-1, 0]:
  511. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=1)
  512. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=1, axis2=0)
  513. arr = rand_int(7, 4, 9)
  514. for i in [-1, 0, 2]:
  515. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=0, axis2=-1)
  516. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-2, axis2=2)
  517. match_res(mnp.trace, onp.trace, arr, offset=i, axis1=-1, axis2=-2)
  518. def mnp_meshgrid(*xi):
  519. a = mnp.meshgrid(*xi)
  520. b = mnp.meshgrid(*xi, sparse=True)
  521. c = mnp.meshgrid(*xi, indexing='ij')
  522. d = mnp.meshgrid(*xi, sparse=False, indexing='ij')
  523. return a, b, c, d
  524. def onp_meshgrid(*xi):
  525. a = onp.meshgrid(*xi)
  526. b = onp.meshgrid(*xi, sparse=True)
  527. c = onp.meshgrid(*xi, indexing='ij')
  528. d = onp.meshgrid(*xi, sparse=False, indexing='ij')
  529. return a, b, c, d
  530. @pytest.mark.level1
  531. @pytest.mark.platform_arm_ascend_training
  532. @pytest.mark.platform_x86_ascend_training
  533. @pytest.mark.platform_x86_gpu_training
  534. @pytest.mark.platform_x86_cpu
  535. @pytest.mark.env_onecard
  536. def test_meshgrid():
  537. xi = (onp.full(3, 2), onp.full(1, 5), onp.full(
  538. (2, 3), 9), onp.full((4, 5, 6), 7))
  539. for i in range(len(xi)):
  540. arrs = xi[i:]
  541. mnp_arrs = map(to_tensor, arrs)
  542. for mnp_res, onp_res in zip(mnp_meshgrid(*mnp_arrs), onp_meshgrid(*arrs)):
  543. match_all_arrays(mnp_res, onp_res)
  544. @pytest.mark.level1
  545. @pytest.mark.platform_arm_ascend_training
  546. @pytest.mark.platform_x86_ascend_training
  547. @pytest.mark.platform_x86_gpu_training
  548. @pytest.mark.platform_x86_cpu
  549. @pytest.mark.env_onecard
  550. def test_mgrid():
  551. mnp_res = mnp.mgrid[0:5]
  552. onp_res = onp.mgrid[0:5]
  553. match_all_arrays(mnp_res, onp_res, error=5)
  554. mnp_res = mnp.mgrid[2:30:4j, -10:20:7, 2:5:0.5]
  555. onp_res = onp.mgrid[2:30:4j, -10:20:7, 2:5:0.5]
  556. match_all_arrays(mnp_res, onp_res, error=5)
  557. @pytest.mark.level1
  558. @pytest.mark.platform_arm_ascend_training
  559. @pytest.mark.platform_x86_ascend_training
  560. @pytest.mark.platform_x86_gpu_training
  561. @pytest.mark.platform_x86_cpu
  562. @pytest.mark.env_onecard
  563. def test_ogrid():
  564. mnp_res = mnp.ogrid[0:5]
  565. onp_res = onp.ogrid[0:5]
  566. match_all_arrays(mnp_res, onp_res, error=5)
  567. mnp_res = mnp.ogrid[2:30:4j, -10:20:7, 2:5:0.5]
  568. onp_res = onp.ogrid[2:30:4j, -10:20:7, 2:5:0.5]
  569. match_all_arrays(mnp_res, onp_res, error=5)
  570. @pytest.mark.level1
  571. @pytest.mark.platform_arm_ascend_training
  572. @pytest.mark.platform_x86_ascend_training
  573. @pytest.mark.platform_x86_gpu_training
  574. @pytest.mark.platform_x86_cpu
  575. @pytest.mark.env_onecard
  576. def test_diagflat():
  577. arrs = [rand_int(2, 3)]
  578. for arr in arrs:
  579. for i in [-2, 0, 7]:
  580. match_res(mnp.diagflat, onp.diagflat, arr, k=i)
  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_diag():
  588. arrs = [rand_int(7), rand_int(5, 5), rand_int(3, 8), rand_int(9, 6)]
  589. for arr in arrs:
  590. for i in [-10, -5, -1, 0, 2, 5, 6, 10]:
  591. match_res(mnp.diag, onp.diag, arr, k=i)
  592. @pytest.mark.level1
  593. @pytest.mark.platform_arm_ascend_training
  594. @pytest.mark.platform_x86_ascend_training
  595. @pytest.mark.platform_x86_gpu_training
  596. @pytest.mark.platform_x86_cpu
  597. @pytest.mark.env_onecard
  598. def test_diag_indices():
  599. mnp_res = mnp.diag_indices(5, 7)
  600. onp_res = onp.diag_indices(5, 7)
  601. match_all_arrays(mnp_res, onp_res)
  602. def mnp_ix_(*args):
  603. return mnp.ix_(*args)
  604. def onp_ix_(*args):
  605. return onp.ix_(*args)
  606. @pytest.mark.level1
  607. @pytest.mark.platform_arm_ascend_training
  608. @pytest.mark.platform_x86_ascend_training
  609. @pytest.mark.platform_x86_gpu_training
  610. @pytest.mark.platform_x86_cpu
  611. @pytest.mark.env_onecard
  612. def test_ix_():
  613. arrs = [rand_int(i + 1) for i in range(10)]
  614. for i in range(10):
  615. test_arrs = arrs[:i + 1]
  616. match_res(mnp_ix_, onp_ix_, *test_arrs)
  617. def mnp_indices():
  618. a = mnp.indices((2, 3))
  619. b = mnp.indices((2, 3, 4), sparse=True)
  620. return a, b
  621. def onp_indices():
  622. a = onp.indices((2, 3))
  623. b = onp.indices((2, 3, 4), sparse=True)
  624. return a, b
  625. def test_indices():
  626. run_multi_test(mnp_indices, onp_indices, ())
  627. @pytest.mark.level1
  628. @pytest.mark.platform_arm_ascend_training
  629. @pytest.mark.platform_x86_ascend_training
  630. @pytest.mark.platform_x86_gpu_training
  631. @pytest.mark.platform_x86_cpu
  632. @pytest.mark.env_onecard
  633. def test_geomspace():
  634. start = onp.arange(1, 7).reshape(2, 3)
  635. end = [1000, 2000, 3000]
  636. match_array(mnp.geomspace(1, 256, num=9).asnumpy(),
  637. onp.geomspace(1, 256, num=9), error=1)
  638. match_array(mnp.geomspace(1, 256, num=8, endpoint=False).asnumpy(),
  639. onp.geomspace(1, 256, num=8, endpoint=False), error=1)
  640. match_array(mnp.geomspace(to_tensor(start), end, num=4).asnumpy(),
  641. onp.geomspace(start, end, num=4), error=1)
  642. match_array(mnp.geomspace(to_tensor(start), end, num=4, endpoint=False).asnumpy(),
  643. onp.geomspace(start, end, num=4, endpoint=False), error=1)
  644. match_array(mnp.geomspace(to_tensor(start), end, num=4, axis=-1).asnumpy(),
  645. onp.geomspace(start, end, num=4, axis=-1), error=1)
  646. match_array(mnp.geomspace(to_tensor(start), end, num=4, endpoint=False, axis=-1).asnumpy(),
  647. onp.geomspace(start, end, num=4, endpoint=False, axis=-1), error=1)
  648. start = onp.arange(1, 1 + 2*3*4*5).reshape(2, 3, 4, 5)
  649. end = [1000, 2000, 3000, 4000, 5000]
  650. for i in range(-5, 5):
  651. match_array(mnp.geomspace(to_tensor(start), end, num=4, axis=i).asnumpy(),
  652. onp.geomspace(start, end, num=4, axis=i), error=1)
  653. @pytest.mark.level1
  654. @pytest.mark.platform_arm_ascend_training
  655. @pytest.mark.platform_x86_ascend_training
  656. @pytest.mark.platform_x86_gpu_training
  657. @pytest.mark.platform_x86_cpu
  658. @pytest.mark.env_onecard
  659. def test_vander():
  660. arrs = [rand_int(i + 3) for i in range(3)]
  661. for i in range(3):
  662. mnp_vander = mnp.vander(to_tensor(arrs[i]))
  663. onp_vander = onp.vander(arrs[i])
  664. match_all_arrays(mnp_vander, onp_vander, error=1e-4)
  665. mnp_vander = mnp.vander(to_tensor(arrs[i]), N=2, increasing=True)
  666. onp_vander = onp.vander(arrs[i], N=2, increasing=True)
  667. match_all_arrays(mnp_vander, onp_vander, error=1e-4)
  668. @pytest.mark.level1
  669. @pytest.mark.platform_arm_ascend_training
  670. @pytest.mark.platform_x86_ascend_training
  671. @pytest.mark.platform_x86_gpu_training
  672. @pytest.mark.platform_x86_cpu
  673. @pytest.mark.env_onecard
  674. def test_bartlett():
  675. for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
  676. match_all_arrays(mnp.bartlett(i), onp.bartlett(i), error=3)
  677. @pytest.mark.level1
  678. @pytest.mark.platform_arm_ascend_training
  679. @pytest.mark.platform_x86_ascend_training
  680. @pytest.mark.platform_x86_gpu_training
  681. @pytest.mark.platform_x86_cpu
  682. @pytest.mark.env_onecard
  683. def test_blackman():
  684. for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
  685. match_all_arrays(mnp.blackman(i), onp.blackman(i), error=3)
  686. @pytest.mark.level1
  687. @pytest.mark.platform_arm_ascend_training
  688. @pytest.mark.platform_x86_ascend_training
  689. @pytest.mark.platform_x86_gpu_training
  690. @pytest.mark.platform_x86_cpu
  691. @pytest.mark.env_onecard
  692. def test_hamming():
  693. for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
  694. match_all_arrays(mnp.hamming(i), onp.hamming(i), error=3)
  695. @pytest.mark.level1
  696. @pytest.mark.platform_arm_ascend_training
  697. @pytest.mark.platform_x86_ascend_training
  698. @pytest.mark.platform_x86_gpu_training
  699. @pytest.mark.platform_x86_cpu
  700. @pytest.mark.env_onecard
  701. def test_hanning():
  702. for i in [-3, -1, 0, 1, 5, 6, 10, 15]:
  703. match_all_arrays(mnp.hanning(i), onp.hanning(i), error=3)
  704. @pytest.mark.level1
  705. @pytest.mark.platform_arm_ascend_training
  706. @pytest.mark.platform_x86_ascend_training
  707. @pytest.mark.platform_x86_gpu_training
  708. @pytest.mark.platform_x86_cpu
  709. @pytest.mark.env_onecard
  710. def test_triu_indices():
  711. m = rand_int().tolist()
  712. n = rand_int().tolist()
  713. k = rand_int().tolist()
  714. mnp_res = mnp.triu_indices(n, k, m)
  715. onp_res = onp.triu_indices(n, k, m)
  716. match_all_arrays(mnp_res, onp_res)
  717. @pytest.mark.level1
  718. @pytest.mark.platform_arm_ascend_training
  719. @pytest.mark.platform_x86_ascend_training
  720. @pytest.mark.platform_x86_gpu_training
  721. @pytest.mark.platform_x86_cpu
  722. @pytest.mark.env_onecard
  723. def test_tril_indices():
  724. m = rand_int().tolist()
  725. n = rand_int().tolist()
  726. k = rand_int().tolist()
  727. mnp_res = mnp.tril_indices(n, k, m)
  728. onp_res = onp.tril_indices(n, k, m)
  729. match_all_arrays(mnp_res, onp_res)
  730. @pytest.mark.level1
  731. @pytest.mark.platform_arm_ascend_training
  732. @pytest.mark.platform_x86_ascend_training
  733. @pytest.mark.platform_x86_gpu_training
  734. @pytest.mark.platform_x86_cpu
  735. @pytest.mark.env_onecard
  736. def test_triu_indices_from():
  737. m = int(rand_int().tolist())
  738. n = int(rand_int().tolist())
  739. t = mnp.asarray(rand_int(m, n).tolist())
  740. k = rand_int().tolist()
  741. mnp_res = mnp.triu_indices_from(t, k)
  742. onp_res = onp.triu_indices_from(t.asnumpy(), k)
  743. match_all_arrays(mnp_res, onp_res)
  744. @pytest.mark.level1
  745. @pytest.mark.platform_arm_ascend_training
  746. @pytest.mark.platform_x86_ascend_training
  747. @pytest.mark.platform_x86_gpu_training
  748. @pytest.mark.platform_x86_cpu
  749. @pytest.mark.env_onecard
  750. def test_tril_indices_from():
  751. m = int(rand_int().tolist())
  752. n = int(rand_int().tolist())
  753. t = mnp.asarray(rand_int(m, n).tolist())
  754. k = rand_int().tolist()
  755. mnp_res = mnp.tril_indices_from(t, k)
  756. onp_res = onp.tril_indices_from(t.asnumpy(), k)
  757. match_all_arrays(mnp_res, onp_res)
  758. @pytest.mark.level1
  759. @pytest.mark.platform_arm_ascend_training
  760. @pytest.mark.platform_x86_ascend_training
  761. @pytest.mark.platform_x86_gpu_training
  762. @pytest.mark.platform_x86_cpu
  763. @pytest.mark.env_onecard
  764. def test_histogram_bin_edges():
  765. x = onp.random.randint(-10, 10, 10)
  766. for bins in [(1, 2, 3), [2], 1, 5, 10]:
  767. # pylint: disable=redefined-builtin
  768. for range in [None, (3, 3), (2, 20)]:
  769. match_res(mnp.histogram_bin_edges, onp.histogram_bin_edges, x, bins=bins, range=range, error=3)
  770. match_res(mnp.histogram_bin_edges, onp.histogram_bin_edges, x, onp.arange(5))
  771. @pytest.mark.level1
  772. @pytest.mark.platform_arm_ascend_training
  773. @pytest.mark.platform_x86_ascend_training
  774. @pytest.mark.platform_x86_gpu_training
  775. @pytest.mark.platform_x86_cpu
  776. @pytest.mark.env_onecard
  777. def test_asarray_exception():
  778. with pytest.raises(TypeError):
  779. mnp.asarray({1, 2, 3})
  780. @pytest.mark.level1
  781. @pytest.mark.platform_arm_ascend_training
  782. @pytest.mark.platform_x86_ascend_training
  783. @pytest.mark.platform_x86_gpu_training
  784. @pytest.mark.platform_x86_cpu
  785. @pytest.mark.env_onecard
  786. def test_linspace_exception():
  787. with pytest.raises(TypeError):
  788. mnp.linspace(0, 1, num=2.5)
  789. @pytest.mark.level1
  790. @pytest.mark.platform_arm_ascend_training
  791. @pytest.mark.platform_x86_ascend_training
  792. @pytest.mark.platform_x86_gpu_training
  793. @pytest.mark.platform_x86_cpu
  794. @pytest.mark.env_onecard
  795. def test_empty_like_exception():
  796. with pytest.raises(ValueError):
  797. mnp.empty_like([[1, 2, 3], [4, 5]])
  798. @pytest.mark.level0
  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_pad():
  805. x_np = onp.random.random([2, 3, 4]).astype("float32")
  806. x_ms = mnp.asarray(x_np.tolist())
  807. # pad constant
  808. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)))
  809. onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)))
  810. match_all_arrays(mnp_res, onp_res, error=1e-5)
  811. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 3), (4, 5)), constant_values=((3, 4), (5, 6), (7, 8)))
  812. onp_res = onp.pad(x_np, ((1, 1), (2, 3), (4, 5)), constant_values=((3, 4), (5, 6), (7, 8)))
  813. match_all_arrays(mnp_res, onp_res, error=1e-5)
  814. # pad statistic
  815. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="mean", stat_length=((1, 2), (2, 10), (3, 4)))
  816. onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="mean", stat_length=((1, 2), (2, 10), (3, 4)))
  817. match_all_arrays(mnp_res, onp_res, error=1e-5)
  818. # pad edge
  819. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="edge")
  820. onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="edge")
  821. match_all_arrays(mnp_res, onp_res, error=1e-5)
  822. # pad wrap
  823. mnp_res = mnp.pad(x_ms, ((1, 1), (2, 2), (3, 4)), mode="wrap")
  824. onp_res = onp.pad(x_np, ((1, 1), (2, 2), (3, 4)), mode="wrap")
  825. match_all_arrays(mnp_res, onp_res, error=1e-5)
  826. # pad linear_ramp
  827. mnp_res = mnp.pad(x_ms, ((1, 3), (5, 2), (3, 0)), mode="linear_ramp", end_values=((0, 10), (9, 1), (-10, 99)))
  828. onp_res = onp.pad(x_np, ((1, 3), (5, 2), (3, 0)), mode="linear_ramp", end_values=((0, 10), (9, 1), (-10, 99)))
  829. match_all_arrays(mnp_res, onp_res, error=1e-5)
  830. def pad_with_msfunc(vector, pad_width, iaxis, kwargs):
  831. pad_value = kwargs.get('padder', 10)
  832. vector[:pad_width[0]] = pad_value
  833. vector[-pad_width[1]:] = pad_value
  834. return vector
  835. def pad_with_npfunc(vector, pad_width, iaxis, kwargs):
  836. pad_value = kwargs.get('padder', 10)
  837. vector[:pad_width[0]] = pad_value
  838. vector[-pad_width[1]:] = pad_value
  839. @pytest.mark.level0
  840. @pytest.mark.platform_x86_gpu_training
  841. @pytest.mark.env_onecard
  842. def test_pad_gpu():
  843. x_np = onp.random.random([2, 1, 4, 3]).astype("float32")
  844. x_ms = mnp.asarray(x_np.tolist())
  845. # pad symmetric odd
  846. mnp_res = mnp.pad(x_ms, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='symmetric', reflect_type='odd')
  847. onp_res = onp.pad(x_np, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='symmetric', reflect_type='odd')
  848. match_all_arrays(mnp_res, onp_res, error=1e-5)
  849. # pad symmetric even
  850. mnp_res = mnp.pad(x_ms, ((10, 13), (5, 12), (3, 0), (2, 6)), mode='symmetric', reflect_type='even')
  851. onp_res = onp.pad(x_np, ((10, 13), (5, 12), (3, 0), (2, 6)), mode='symmetric', reflect_type='even')
  852. match_all_arrays(mnp_res, onp_res, error=1e-5)
  853. # pad reflect odd
  854. mnp_res = mnp.pad(x_ms, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='reflect', reflect_type='odd')
  855. onp_res = onp.pad(x_np, ((10, 3), (5, 2), (3, 0), (2, 6)), mode='reflect', reflect_type='odd')
  856. match_all_arrays(mnp_res, onp_res, error=1e-5)
  857. # pad reflect even
  858. mnp_res = mnp.pad(x_ms, ((10, 13)), mode='reflect', reflect_type='even')
  859. onp_res = onp.pad(x_np, ((10, 13)), mode='reflect', reflect_type='even')
  860. match_all_arrays(mnp_res, onp_res, error=1e-5)
  861. # pad func
  862. x_np = onp.random.random([2, 4]).astype("float32")
  863. x_ms = mnp.asarray(x_np.tolist())
  864. mnp_res = mnp.pad(x_ms, ((5, 5)), mode=pad_with_msfunc, padder=99)
  865. onp_res = onp.pad(x_np, ((5, 5)), mode=pad_with_npfunc, padder=99)
  866. match_all_arrays(mnp_res, onp_res, error=1e-5)