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_graph_fallback_numpy.py 25 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. # Copyright 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. """ test graph fallback """
  16. import pytest
  17. import numpy as np
  18. from mindspore import ms_function, context, Tensor
  19. context.set_context(mode=context.GRAPH_MODE)
  20. @pytest.mark.level0
  21. @pytest.mark.platform_x86_gpu_training
  22. @pytest.mark.platform_arm_ascend_training
  23. @pytest.mark.platform_x86_ascend_training
  24. @pytest.mark.env_onecard
  25. def test_np_linspace():
  26. """
  27. Feature: JIT Fallback
  28. Description: Test numpy with linspace in graph mode.
  29. Expectation: No exception.
  30. """
  31. @ms_function
  32. def np_linspace():
  33. a = Tensor(np.linspace(1, 10, 10))
  34. b = Tensor(np.linspace(1, 1, 10))
  35. c = Tensor(np.linspace(10, 20, 5, endpoint=False))
  36. d = Tensor(np.linspace(10, 20, 5, endpoint=True))
  37. e = Tensor(np.linspace(1, 10, 10).reshape([10, 1]))
  38. return a, b, c, d, e
  39. a, b, c, d, e = np_linspace()
  40. print("a:", a)
  41. print("b:", b)
  42. print("c:", c)
  43. print("d:", d)
  44. print("e:", e)
  45. @pytest.mark.level0
  46. @pytest.mark.platform_x86_gpu_training
  47. @pytest.mark.platform_arm_ascend_training
  48. @pytest.mark.platform_x86_ascend_training
  49. @pytest.mark.env_onecard
  50. def test_np_arange_slice_1():
  51. """
  52. Feature: JIT Fallback
  53. Description: Test numpy with arange slice in graph mode.
  54. Expectation: No exception.
  55. """
  56. @ms_function
  57. def np_arange_slice_1():
  58. x = np.arange(10)
  59. index = slice(2, 7, 2)
  60. a = Tensor(x[index])
  61. b = Tensor(x[2:7:2])
  62. c = Tensor(x[5])
  63. d = Tensor(x[2:])
  64. e = Tensor(x[2:5])
  65. return a, b, c, d, e
  66. a, b, c, d, e = np_arange_slice_1()
  67. assert np.all(a.asnumpy() == np.array([2, 4, 6]))
  68. assert np.all(b.asnumpy() == np.array([2, 4, 6]))
  69. assert np.all(c.asnumpy() == np.array([5]))
  70. assert np.all(d.asnumpy() == np.array([2, 3, 4, 5, 6, 7, 8, 9]))
  71. assert np.all(e.asnumpy() == np.array([2, 3, 4]))
  72. @pytest.mark.level0
  73. @pytest.mark.platform_x86_gpu_training
  74. @pytest.mark.platform_arm_ascend_training
  75. @pytest.mark.platform_x86_ascend_training
  76. @pytest.mark.env_onecard
  77. def test_np_arange_slice_2():
  78. """
  79. Feature: JIT Fallback
  80. Description: Test numpy with arange slice in graph mode.
  81. Expectation: No exception.
  82. """
  83. @ms_function
  84. def np_arange_slice_2():
  85. x = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
  86. a = Tensor(x[1:])
  87. b = Tensor(x[..., 1])
  88. c = Tensor(x[1, ...])
  89. d = Tensor(x[..., 1:])
  90. return a, b, c, d
  91. a, b, c, d = np_arange_slice_2()
  92. assert np.all(a.asnumpy() == np.array([[3, 4, 5], [4, 5, 6]]))
  93. assert np.all(b.asnumpy() == np.array([2, 4, 5]))
  94. assert np.all(c.asnumpy() == np.array([3, 4, 5]))
  95. assert np.all(d.asnumpy() == np.array([[2, 3], [4, 5], [5, 6]]))
  96. @pytest.mark.level0
  97. @pytest.mark.platform_x86_gpu_training
  98. @pytest.mark.platform_arm_ascend_training
  99. @pytest.mark.platform_x86_ascend_training
  100. @pytest.mark.env_onecard
  101. def test_np_array_advanced_index_1():
  102. """
  103. Feature: JIT Fallback
  104. Description: Test numpy with array advanced index in graph mode.
  105. Expectation: No exception.
  106. """
  107. @ms_function
  108. def np_array_advanced_index_1():
  109. x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
  110. a = Tensor(x[[0, 1, 2], [0, 1, 0]])
  111. rows = np.array([[0, 0], [3, 3]])
  112. cols = np.array([[0, 2], [0, 2]])
  113. b = Tensor(x[rows, cols])
  114. c = Tensor(x[1:3, 1:3])
  115. d = Tensor(x[1:3, [1, 2]])
  116. e = Tensor(x[..., 1:])
  117. return a, b, c, d, e
  118. a, b, c, d, e = np_array_advanced_index_1()
  119. assert np.all(a.asnumpy() == np.array([0, 4, 6]))
  120. assert np.all(b.asnumpy() == np.array([[0, 2], [9, 11]]))
  121. assert np.all(c.asnumpy() == np.array([[4, 5], [7, 8]]))
  122. assert np.all(d.asnumpy() == np.array([[4, 5], [7, 8]]))
  123. assert np.all(e.asnumpy() == np.array([[1, 2], [4, 5], [7, 8], [10, 11]]))
  124. # Not support <class 'complex'> yet.
  125. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  126. def test_np_array_advanced_index_2():
  127. """
  128. Feature: JIT Fallback
  129. Description: Test numpy with array advanced index in graph mode.
  130. Expectation: No exception.
  131. """
  132. @ms_function
  133. def np_array_advanced_index_2():
  134. x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
  135. y = np.array([np.nan, 1, 2, np.nan, 3, 4, 5])
  136. z = np.array([1, 2 + 6j, 5, 3.5 + 5j])
  137. a = Tensor(x[x > 5])
  138. b = Tensor(y[~np.isnan(y)])
  139. c = Tensor(z[np.iscomplex(z)])
  140. return a, b, c
  141. a, b, c = np_array_advanced_index_2()
  142. assert np.all(a.asnumpy() == np.array([6, 7, 8, 9, 10, 11]))
  143. assert np.all(b.asnumpy() == np.array([1., 2., 3., 4., 5.]))
  144. assert np.all(c.asnumpy() == np.array([2. + 6.j, 3.5 + 5.j]))
  145. @pytest.mark.level0
  146. @pytest.mark.platform_x86_gpu_training
  147. @pytest.mark.platform_arm_ascend_training
  148. @pytest.mark.platform_x86_ascend_training
  149. @pytest.mark.env_onecard
  150. def test_np_array_advanced_index_3():
  151. """
  152. Feature: JIT Fallback
  153. Description: Test numpy with array advanced index in graph mode.
  154. Expectation: No exception.
  155. """
  156. @ms_function
  157. def np_array_advanced_index_3():
  158. x = np.arange(32).reshape((8, 4))
  159. a = Tensor(x[[4, 2, 1, 7]])
  160. y = np.arange(32).reshape((8, 4))
  161. b = Tensor(y[[-4, -2, -1, -7]])
  162. z = np.arange(32).reshape((8, 4))
  163. c = Tensor(z[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])])
  164. return a, b, c
  165. a, b, c = np_array_advanced_index_3()
  166. print("a:", a)
  167. print("b:", b)
  168. print("c:", c)
  169. @pytest.mark.level0
  170. @pytest.mark.platform_x86_gpu_training
  171. @pytest.mark.platform_arm_ascend_training
  172. @pytest.mark.platform_x86_ascend_training
  173. @pytest.mark.env_onecard
  174. def test_np_reshape():
  175. """
  176. Feature: JIT Fallback
  177. Description: Test numpy.reshape() method in graph mode.
  178. Expectation: No exception.
  179. """
  180. @ms_function
  181. def np_reshape():
  182. x = np.arange(8)
  183. y = x.reshape(2, 4)
  184. return Tensor(y)
  185. assert np.all(np_reshape().asnumpy() == np.array([[0, 1, 2, 3], [4, 5, 6, 7]]))
  186. @pytest.mark.level0
  187. @pytest.mark.platform_x86_gpu_training
  188. @pytest.mark.platform_arm_ascend_training
  189. @pytest.mark.platform_x86_ascend_training
  190. @pytest.mark.env_onecard
  191. def test_np_ndarray_flatten():
  192. """
  193. Feature: JIT Fallback
  194. Description: Test numpy.flatten() method in graph mode.
  195. Expectation: No exception.
  196. """
  197. @ms_function
  198. def np_ndarray_flatten():
  199. x = np.arange(8).reshape(2, 4)
  200. y = x.flatten()
  201. return Tensor(y)
  202. assert np.all(np_ndarray_flatten().asnumpy() == np.array([0, 1, 2, 3, 4, 5, 6, 7]))
  203. @pytest.mark.level0
  204. @pytest.mark.platform_x86_gpu_training
  205. @pytest.mark.platform_arm_ascend_training
  206. @pytest.mark.platform_x86_ascend_training
  207. @pytest.mark.env_onecard
  208. def test_np_ravel():
  209. """
  210. Feature: JIT Fallback
  211. Description: Test numpy.ravel() method in graph mode.
  212. Expectation: No exception.
  213. """
  214. @ms_function
  215. def np_ravel():
  216. x = np.arange(8).reshape(2, 4)
  217. y = x.ravel(order='F')
  218. return Tensor(y)
  219. assert np.all(np_ravel().asnumpy() == np.array([0, 4, 1, 5, 2, 6, 3, 7]))
  220. @pytest.mark.level0
  221. @pytest.mark.platform_x86_gpu_training
  222. @pytest.mark.platform_arm_ascend_training
  223. @pytest.mark.platform_x86_ascend_training
  224. @pytest.mark.env_onecard
  225. def test_np_transpose():
  226. """
  227. Feature: JIT Fallback
  228. Description: Test numpy.transpose() method in graph mode.
  229. Expectation: No exception.
  230. """
  231. @ms_function
  232. def np_transpose():
  233. x = np.arange(4).reshape(4, 1)
  234. y = np.transpose(x)
  235. return Tensor(y)
  236. assert np.all(np_transpose().asnumpy() == np.array([0, 1, 2, 3]))
  237. @pytest.mark.level0
  238. @pytest.mark.platform_x86_gpu_training
  239. @pytest.mark.platform_arm_ascend_training
  240. @pytest.mark.platform_x86_ascend_training
  241. @pytest.mark.env_onecard
  242. def test_np_rollaxis():
  243. """
  244. Feature: JIT Fallback
  245. Description: Test numpy.rollaxis() method in graph mode.
  246. Expectation: No exception.
  247. """
  248. @ms_function
  249. def np_rollaxis():
  250. x = np.arange(8).reshape(2, 2, 2)
  251. tensor_x = Tensor(x)
  252. y = np.rollaxis(x, 2, 0)
  253. tensor_y = Tensor(y)
  254. return tensor_x[1, 1, 0], tensor_y[1, 1, 0]
  255. x, y = np_rollaxis()
  256. assert x == 6 and y == 5
  257. @pytest.mark.level0
  258. @pytest.mark.platform_x86_gpu_training
  259. @pytest.mark.platform_arm_ascend_training
  260. @pytest.mark.platform_x86_ascend_training
  261. @pytest.mark.env_onecard
  262. def test_np_swapaxes():
  263. """
  264. Feature: JIT Fallback
  265. Description: Test numpy.swapaxes() method in graph mode.
  266. Expectation: No exception.
  267. """
  268. @ms_function
  269. def np_swapaxes():
  270. x = np.arange(8).reshape(2, 2, 2)
  271. tensor_x = Tensor(x)
  272. y = np.swapaxes(x, 2, 0)
  273. tensor_y = Tensor(y)
  274. return tensor_x[1, 1, 0], tensor_y[1, 1, 0]
  275. x, y = np_swapaxes()
  276. assert x == 6 and y == 3
  277. @pytest.mark.level0
  278. @pytest.mark.platform_x86_gpu_training
  279. @pytest.mark.platform_arm_ascend_training
  280. @pytest.mark.platform_x86_ascend_training
  281. @pytest.mark.env_onecard
  282. def test_np_broadcast():
  283. """
  284. Feature: JIT Fallback
  285. Description: Test numpy.broadcast() method in graph mode.
  286. Expectation: No exception.
  287. """
  288. @ms_function
  289. def np_broadcast():
  290. x = np.array([[1], [2], [3]])
  291. y = np.array([4, 5, 6])
  292. z = np.broadcast(x, y)
  293. return Tensor(z.shape)
  294. assert np.all(np_broadcast().asnumpy() == np.array([3, 3]))
  295. @pytest.mark.level0
  296. @pytest.mark.platform_x86_gpu_training
  297. @pytest.mark.platform_arm_ascend_training
  298. @pytest.mark.platform_x86_ascend_training
  299. @pytest.mark.env_onecard
  300. def test_np_broadcast_to():
  301. """
  302. Feature: JIT Fallback
  303. Description: Test numpy.broadcast_to() method in graph mode.
  304. Expectation: No exception.
  305. """
  306. @ms_function
  307. def np_broadcast_to():
  308. x = np.arange(4).reshape(1, 4)
  309. y = np.broadcast_to(x, (2, 4))
  310. return Tensor(y)
  311. assert np.all(np_broadcast_to().asnumpy() == np.array([[0, 1, 2, 3], [0, 1, 2, 3]]))
  312. @pytest.mark.level0
  313. @pytest.mark.platform_x86_gpu_training
  314. @pytest.mark.platform_arm_ascend_training
  315. @pytest.mark.platform_x86_ascend_training
  316. @pytest.mark.env_onecard
  317. def test_np_expand_dims():
  318. """
  319. Feature: JIT Fallback
  320. Description: Test numpy.expand_dims() method in graph mode.
  321. Expectation: No exception.
  322. """
  323. @ms_function
  324. def np_expand_dims():
  325. x = np.array(([1, 2], [3, 4]))
  326. y = np.expand_dims(x, axis=0)
  327. return Tensor(y)
  328. assert np.all(np_expand_dims().asnumpy() == np.array([[[1, 2], [3, 4]]]))
  329. @pytest.mark.level0
  330. @pytest.mark.platform_x86_gpu_training
  331. @pytest.mark.platform_arm_ascend_training
  332. @pytest.mark.platform_x86_ascend_training
  333. @pytest.mark.env_onecard
  334. def test_np_squeeze():
  335. """
  336. Feature: JIT Fallback
  337. Description: Test numpy.squeeze() method in graph mode.
  338. Expectation: No exception.
  339. """
  340. @ms_function
  341. def np_squeeze():
  342. x = np.arange(4).reshape(1, 2, 2)
  343. y = np.squeeze(x)
  344. return Tensor(y)
  345. assert np.all(np_squeeze().asnumpy() == np.array([[0, 1], [2, 3]]))
  346. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  347. def test_np_concat():
  348. """
  349. Feature: JIT Fallback
  350. Description: Test numpy method in graph mode.
  351. Expectation: No exception.
  352. """
  353. @ms_function
  354. def np_concat():
  355. x = np.array([[1, 2], [3, 4]])
  356. y = np.array([[5, 6], [7, 8]])
  357. concatenate = np.concatenate((x, y))
  358. stack = np.stack((x, y), 0)
  359. hstack = np.hstack((x, y))
  360. vstack = np.vstack((x, y))
  361. return Tensor(concatenate), Tensor(stack), Tensor(hstack), Tensor(vstack)
  362. out_concatenate, out_stack, out_hstack, out_vstack = np_concat()
  363. assert np.all(out_concatenate.asnumpy() == np.array([[1, 2], [3, 4], [5, 6], [7, 8]]))
  364. assert np.all(out_stack.asnumpy() == np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]))
  365. assert np.all(out_hstack.asnumpy() == np.array([[1, 2, 5, 6], [3, 4, 7, 8]]))
  366. assert np.all(out_vstack.asnumpy() == np.array([[1, 2], [3, 4], [5, 6], [7, 8]]))
  367. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  368. def test_np_split():
  369. """
  370. Feature: JIT Fallback
  371. Description: Test numpy split method in graph mode.
  372. Expectation: No exception.
  373. """
  374. @ms_function
  375. def np_split():
  376. x = np.arange(4).reshape(2, 2)
  377. split = np.split(x, 2)
  378. hsplit = np.hsplit(x, 2)
  379. vsplit = np.vsplit(x, 2)
  380. return Tensor(split), Tensor(hsplit), Tensor(vsplit)
  381. out_split, out_hsplit, out_vsplit = np_split()
  382. assert np.all(out_split.asnumpy() == np.array([[[0, 1]], [[2, 3]]]))
  383. assert np.all(out_hsplit.asnumpy() == np.array([[[0], [2]], [[1], [3]]]))
  384. assert np.all(out_vsplit.asnumpy() == np.array([[[0, 1]], [[2, 3]]]))
  385. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  386. def test_np_element():
  387. """
  388. Feature: JIT Fallback
  389. Description: Test numpy method in graph mode.
  390. Expectation: No exception.
  391. """
  392. @ms_function
  393. def np_element():
  394. resize = np.resize(np.array([[1, 2, 3], [4, 5, 6]]), (3, 2))
  395. append = np.append(np.array([[1, 2, 3], [4, 5, 6]]), [[7, 8, 9]], axis=0)
  396. insert = np.insert(np.array([[1, 2], [3, 4], [5, 6]]), 3, [7, 8], axis=0)
  397. delete = np.delete(np.arange(6).reshape(2, 3), 0, axis=0)
  398. unique = np.unique(np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9]))
  399. return Tensor(resize), Tensor(append), Tensor(insert), Tensor(delete), Tensor(unique)
  400. out_resize, out_append, out_insert, out_delete, out_unique = np_element()
  401. assert np.all(out_resize.asnumpy() == np.array([[1, 2], [3, 4], [5, 6]]))
  402. assert np.all(out_append.asnumpy() == np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
  403. assert np.all(out_insert.asnumpy() == np.array([[1, 2], [3, 4], [5, 6], [7, 8]]))
  404. assert np.all(out_delete.asnumpy() == np.array([3, 4, 5]))
  405. assert np.all(out_unique.asnumpy() == np.array([2, 5, 6, 7, 8, 9]))
  406. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  407. def test_np_bitwise():
  408. """
  409. Feature: JIT Fallback
  410. Description: Test numpy bitwise method in graph mode.
  411. Expectation: No exception.
  412. """
  413. @ms_function
  414. def np_bitwise():
  415. bitwise_and = np.bitwise_and(13, 17)
  416. bitwise_or = np.bitwise_or(13, 17)
  417. invert = np.invert(np.array([13], dtype=np.uint8))
  418. left_shift = np.left_shift(10, 2)
  419. right_shift = np.right_shift(40, 2)
  420. return Tensor(bitwise_and), Tensor(bitwise_or), Tensor(invert), Tensor(left_shift), Tensor(right_shift)
  421. bitwise_and, bitwise_or, invert, left_shift, right_shift = np_bitwise()
  422. assert bitwise_and.asnumpy() == 1
  423. assert bitwise_or.asnumpy() == 29
  424. assert np.all(invert.asnumpy() == np.array([242]))
  425. assert left_shift.asnumpy() == 40
  426. assert right_shift.asnumpy() == 10
  427. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  428. def test_np_char_1():
  429. """
  430. Feature: JIT Fallback
  431. Description: Test numpy char method in graph mode.
  432. Expectation: No exception.
  433. """
  434. @ms_function
  435. def np_char():
  436. char_add = np.char.add(['MindSpore'], [' fallback'])
  437. char_multiply = np.char.multiply('fallback ', 3)
  438. char_center = np.char.center('fallback', 10, fillchar='*')
  439. char_capitalize = np.char.capitalize('fallback')
  440. char_title = np.char.title('fallback')
  441. char_lower = np.char.lower('FALLBACK')
  442. char_upper = np.char.upper('fallback')
  443. return Tensor(char_add), Tensor(char_multiply), Tensor(char_center), Tensor(char_capitalize), \
  444. Tensor(char_title), Tensor(char_lower), Tensor(char_upper)
  445. char_add, char_multiply, char_center, char_capitalize, char_title, char_lower, char_upper = np_char()
  446. assert char_add.asnumpy() == 'MindSpore fallback'
  447. assert char_multiply.asnumpy() == 'fallback fallback fallback '
  448. assert char_center.asnumpy() == '*fallback*'
  449. assert char_capitalize.asnumpy() == 'Fallback'
  450. assert char_title.asnumpy() == 'Fallback'
  451. assert char_lower.asnumpy() == 'fallback'
  452. assert char_upper.asnumpy() == 'FALLBACK'
  453. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  454. def test_np_char_2():
  455. """
  456. Feature: JIT Fallback
  457. Description: Test numpy char method in graph mode.
  458. Expectation: No exception.
  459. """
  460. @ms_function
  461. def np_char():
  462. char_split = np.char.split('MindSpore fallback')
  463. out_split = np.char.join(' ', char_split)
  464. char_splitlines = np.char.splitlines('MindSpore\nfallback')
  465. out_splitlines = np.char.join(',', char_splitlines)
  466. out_strip = np.char.strip('abc acd', 'a')
  467. out_replace = np.char.replace('faooback', 'oo', 'll')
  468. char_encode = np.char.encode('runoob', 'cp500')
  469. out_decode = np.char.decode(char_encode, 'cp500')
  470. return Tensor(out_split), Tensor(out_splitlines), Tensor(out_strip), Tensor(out_replace), Tensor(out_decode)
  471. char_split, char_splitlines, char_strip, char_replace, char_decode = np_char()
  472. assert char_split.asnumpy() == 'MindSpore fallback'
  473. assert char_splitlines.asnumpy() == 'MindSpore,fallback'
  474. assert char_strip.asnumpy() == 'bc acd'
  475. assert char_replace.asnumpy() == 'fallback'
  476. assert char_decode.asnumpy() == 'runoob'
  477. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  478. def test_np_degree():
  479. """
  480. Feature: JIT Fallback
  481. Description: Test numpy method in graph mode.
  482. Expectation: No exception.
  483. """
  484. @ms_function
  485. def np_degree():
  486. out_sin = np.sin(30 * np.pi / 180)
  487. out_arcsin = np.degrees(np.arcsin(out_sin))
  488. out_cos = np.cos(60 * np.pi / 180)
  489. out_arccos = np.degrees(np.arccos(out_cos))
  490. out_tan = np.tan(45 * np.pi / 180)
  491. out_arctan = np.degrees(np.arctan(out_tan))
  492. return Tensor(out_sin), Tensor(out_arcsin), Tensor(out_cos), \
  493. Tensor(out_arccos), Tensor(out_tan), Tensor(out_arctan)
  494. out_sin, out_arcsin, out_cos, out_arccos, out_tan, out_arctan = np_degree()
  495. assert np.isclose(out_sin.asnumpy(), 0.5)
  496. assert np.isclose(out_arcsin.asnumpy(), 30)
  497. assert np.isclose(out_cos.asnumpy(), 0.5)
  498. assert np.isclose(out_arccos.asnumpy(), 60)
  499. assert np.isclose(out_tan.asnumpy(), 1)
  500. assert np.isclose(out_arctan.asnumpy(), 45)
  501. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  502. def test_np_math_1():
  503. """
  504. Feature: JIT Fallback
  505. Description: Test numpy math method in graph mode.
  506. Expectation: No exception.
  507. """
  508. @ms_function
  509. def np_math():
  510. x = np.array([6, 12])
  511. y = np.array([3, 5])
  512. out_add = np.add(x, y)
  513. out_subtract = np.subtract(x, y)
  514. out_multiply = np.multiply(x, y)
  515. out_divide = np.divide(x, y)
  516. out_mod = np.mod(x, y)
  517. out_remainder = np.remainder(x, y)
  518. return Tensor(out_add), Tensor(out_subtract), Tensor(out_multiply), \
  519. Tensor(out_divide), Tensor(out_mod), Tensor(out_remainder)
  520. out_add, out_subtract, out_multiply, out_divide, out_mod, out_remainder = np_math()
  521. assert np.all(out_add.asnumpy() == np.array([9, 17]))
  522. assert np.all(out_subtract.asnumpy() == np.array([3, 7]))
  523. assert np.all(out_multiply.asnumpy() == np.array([18, 60]))
  524. assert np.allclose(out_divide.asnumpy(), np.array([2, 2.4]))
  525. assert np.all(out_mod.asnumpy() == np.array([0, 2]))
  526. assert np.all(out_remainder.asnumpy() == np.array([0, 2]))
  527. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  528. def test_np_math_2():
  529. """
  530. Feature: JIT Fallback
  531. Description: Test numpy math method in graph mode.
  532. Expectation: No exception.
  533. """
  534. @ms_function
  535. def np_math():
  536. x = np.array([0.1, 1.4, 2.51, 3.3])
  537. out_around = np.around(x)
  538. out_floot = np.floor(x)
  539. out_ceil = np.ceil(x)
  540. out_reciprocal = np.reciprocal(np.array([0.25, 1, 2]))
  541. out_power = np.power(np.array([1.0, 2.0, 3.0]), 2)
  542. return Tensor(out_around), Tensor(out_floot), Tensor(out_ceil), Tensor(out_reciprocal), Tensor(out_power)
  543. out_around, out_floot, out_ceil, out_reciprocal, out_power = np_math()
  544. assert np.allclose(out_around.asnumpy(), np.array([0, 1, 3, 3]))
  545. assert np.allclose(out_floot.asnumpy(), np.array([0, 1, 2, 3]))
  546. assert np.allclose(out_ceil.asnumpy(), np.array([1, 2, 3, 4]))
  547. assert np.allclose(out_reciprocal.asnumpy(), np.array([4, 1, 0.5]))
  548. assert np.allclose(out_power.asnumpy(), np.array([1, 4, 9]))
  549. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  550. def test_np_statistic():
  551. """
  552. Feature: JIT Fallback
  553. Description: Test numpy statistic method in graph mode.
  554. Expectation: No exception.
  555. """
  556. @ms_function
  557. def np_statistic():
  558. x = np.array([1, 2, 3, 4, 5])
  559. out_amin = np.amin(x)
  560. out_amax = np.amax(x)
  561. out_ptp = np.ptp(x)
  562. out_percentile = np.percentile(x, 50)
  563. out_median = np.median(x)
  564. out_mean = np.mean(x)
  565. out_average = np.average(x)
  566. out_sqrt = np.std(x)
  567. out_var = np.var(x)
  568. return Tensor(out_amin), Tensor(out_amax), Tensor(out_ptp), Tensor(out_percentile), \
  569. Tensor(out_median), Tensor(out_mean), Tensor(out_average), Tensor(out_sqrt), Tensor(out_var)
  570. out_amin, out_amax, out_ptp, out_percentile, out_median, out_mean, out_average, out_sqrt, out_var = np_statistic()
  571. assert out_amin.asnumpy() == 1
  572. assert out_amax.asnumpy() == 5
  573. assert out_ptp.asnumpy() == 4
  574. assert np.isclose(out_percentile.asnumpy(), 3.0)
  575. assert out_median.asnumpy() == 3
  576. assert out_mean.asnumpy() == 3
  577. assert out_average.asnumpy() == 3
  578. assert np.allclose(out_sqrt.asnumpy(), np.array([1.41421356]))
  579. assert np.isclose(out_var.asnumpy(), 2.0)
  580. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  581. def test_np_sort():
  582. """
  583. Feature: JIT Fallback
  584. Description: Test numpy method in graph mode.
  585. Expectation: No exception.
  586. """
  587. @ms_function
  588. def np_sort():
  589. x = np.array([3, 1, 2, 4, 5])
  590. out_sort = np.sort(x)
  591. out_argsort = np.argsort(x)
  592. out_argmax = np.argmax(x)
  593. out_argmin = np.argmin(x)
  594. out_nonzero = np.nonzero(x)
  595. out_where = np.where(x > 4)
  596. condition = x % 2 == 0
  597. out_extract = np.extract(condition, x)
  598. return Tensor(out_sort), Tensor(out_argsort), Tensor(out_argmax), \
  599. Tensor(out_argmin), Tensor(out_nonzero), Tensor(out_where), Tensor(out_extract)
  600. out_sort, out_argsort, out_argmax, out_argmin, out_nonzero, out_where, out_extract = np_sort()
  601. assert np.all(out_sort.asnumpy() == np.array([1, 2, 3, 4, 5]))
  602. assert np.all(out_argsort.asnumpy() == np.array([1, 2, 0, 3, 4]))
  603. assert out_argmax.asnumpy() == 4
  604. assert out_argmin.asnumpy() == 1
  605. assert np.all(out_nonzero.asnumpy() == np.array([0, 1, 2, 3, 4]))
  606. assert np.all(out_where.asnumpy() == np.array([4]))
  607. assert np.all(out_extract.asnumpy() == np.array([2, 4]))
  608. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  609. def test_np_matrix():
  610. """
  611. Feature: JIT Fallback
  612. Description: Test numpy matrix method in graph mode.
  613. Expectation: No exception.
  614. """
  615. @ms_function
  616. def np_matrix():
  617. x = np.arange(4).reshape(2, 2)
  618. y = np.array([[2, 2], [3, 3]])
  619. out_t = x.T
  620. out_dot = np.dot(x, y)
  621. out_vdot = np.vdot(x, y)
  622. out_inner = np.inner(x, y)
  623. out_matmul = np.matmul(x, y)
  624. out_det = np.linalg.det(x)
  625. out_inv = np.linalg.inv(x)
  626. return Tensor(out_t), Tensor(out_dot), Tensor(out_vdot), Tensor(out_inner), \
  627. Tensor(out_matmul), Tensor(out_det), Tensor(out_inv)
  628. out_t, out_dot, out_vdot, out_inner, out_matmul, out_det, out_inv = np_matrix()
  629. assert np.all(out_t.asnumpy() == np.array([[0, 2], [1, 3]]))
  630. assert np.all(out_dot.asnumpy() == np.array([[3, 3], [13, 13]]))
  631. assert out_vdot.asnumpy() == 17
  632. assert np.all(out_inner.asnumpy() == np.array([[2, 3], [10, 15]]))
  633. assert np.all(out_matmul.asnumpy() == np.array([[3, 3], [13, 13]]))
  634. assert np.isclose(out_det.asnumpy(), -2.0)
  635. assert np.allclose(out_inv.asnumpy(), np.array([[-1.5, 0.5], [1, 0]]))