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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. import numpy.random as rand
  19. from mindspore import ms_function, context, Tensor
  20. context.set_context(mode=context.GRAPH_MODE)
  21. def test_np_array_1():
  22. """
  23. Feature: JIT Fallback
  24. Description: Test numpy with ndarray in graph mode.
  25. Expectation: No exception.
  26. """
  27. @ms_function
  28. def np_array_1():
  29. a = np.array([1, 2, 3])
  30. return Tensor(a)
  31. res = np_array_1()
  32. expect_res = np.array([1, 2, 3])
  33. assert np.all(res.asnumpy() == expect_res)
  34. def test_np_array_2():
  35. """
  36. Feature: JIT Fallback
  37. Description: Test numpy with ndarray in graph mode.
  38. Expectation: No exception.
  39. """
  40. @ms_function
  41. def np_array_2():
  42. a = np.array([[1, 2], [3, 4]])
  43. return Tensor(a)
  44. res = np_array_2()
  45. expect_res = np.array([[1, 2], [3, 4]])
  46. assert np.all(res.asnumpy() == expect_res)
  47. def test_np_array_3():
  48. """
  49. Feature: JIT Fallback
  50. Description: Test numpy with ndarray in graph mode.
  51. Expectation: No exception.
  52. """
  53. @ms_function
  54. def np_array_3():
  55. a = np.array([1, 2, 3, 4, 5], ndmin=2)
  56. return Tensor(a)
  57. res = np_array_3()
  58. expect_res = np.array([[1, 2, 3, 4, 5]])
  59. assert np.all(res.asnumpy() == expect_res)
  60. def test_np_array_4():
  61. """
  62. Feature: JIT Fallback
  63. Description: Test numpy with ndarray in graph mode.
  64. Expectation: No exception.
  65. """
  66. @ms_function
  67. def np_array_4():
  68. a = np.array([1, 2, 3], dtype=complex)
  69. return Tensor(a)
  70. res = np_array_4()
  71. expect_res = np.array([1+0j, 2+0j, 3+0j])
  72. assert np.all(res.asnumpy() == expect_res)
  73. def test_np_dtype_1():
  74. """
  75. Feature: JIT Fallback
  76. Description: Test numpy with dtype in graph mode.
  77. Expectation: No exception.
  78. """
  79. @ms_function
  80. def np_dtype_1():
  81. t = np.dtype(np.int32)
  82. return Tensor(np.array([1, 2, 3], dtype=t))
  83. res = np_dtype_1()
  84. expect_res = np.array([1, 2, 3], dtype=np.int32)
  85. assert np.all(res.asnumpy() == expect_res)
  86. def test_np_dtype_2():
  87. """
  88. Feature: JIT Fallback
  89. Description: Test numpy with dtype in graph mode.
  90. Expectation: No exception.
  91. """
  92. @ms_function
  93. def np_dtype_2():
  94. t = np.dtype('i4')
  95. return Tensor(np.array([1, 2, 3], dtype=t))
  96. res = np_dtype_2()
  97. expect_res = np.array([1, 2, 3], dtype=np.int32)
  98. assert np.all(res.asnumpy() == expect_res)
  99. def test_np_array_ndim():
  100. """
  101. Feature: JIT Fallback
  102. Description: Test numpy with array ndim in graph mode.
  103. Expectation: No exception.
  104. """
  105. @ms_function
  106. def np_array_ndim():
  107. a = np.arange(24)
  108. return Tensor(a.ndim)
  109. res = np_array_ndim()
  110. assert res == 1
  111. def test_np_array_reshape_1():
  112. """
  113. Feature: JIT Fallback
  114. Description: Test numpy with array reshape in graph mode.
  115. Expectation: No exception.
  116. """
  117. @ms_function
  118. def np_array_reshape_1():
  119. a = np.array([[1, 2, 3], [4, 5, 6]])
  120. b = a.reshape(3, 2)
  121. return Tensor(b.ndim)
  122. res = np_array_reshape_1()
  123. assert res == 2
  124. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  125. def test_np_array_reshape_2():
  126. """
  127. Feature: JIT Fallback
  128. Description: Test numpy with array reshape in graph mode.
  129. Expectation: No exception.
  130. """
  131. @ms_function
  132. def np_array_reshape_2():
  133. a = np.array([[1, 2, 3], [4, 5, 6]])
  134. a.shape = (3, 2)
  135. return a
  136. res = np_array_reshape_2()
  137. print("res:", res)
  138. def test_np_array_itemsize():
  139. """
  140. Feature: JIT Fallback
  141. Description: Test numpy with array reshape in graph mode.
  142. Expectation: No exception.
  143. """
  144. @ms_function
  145. def np_array_itemsize():
  146. a = np.array([1, 2, 3, 4, 5], dtype=np.int8)
  147. return Tensor(a.itemsize)
  148. res = np_array_itemsize()
  149. print("res:", res)
  150. assert res == 1
  151. def test_np_empty_zeros_ones():
  152. """
  153. Feature: JIT Fallback
  154. Description: Test numpy with array empty, zeros, ones in graph mode.
  155. Expectation: No exception.
  156. """
  157. @ms_function
  158. def np_empty_zeros_ones():
  159. x = np.empty([3, 2], dtype=np.int)
  160. y = np.zeros(x.shape, dtype=np.int)
  161. z = np.ones(x.shape, dtype=np.int)
  162. return Tensor(y + z)
  163. res = np_empty_zeros_ones()
  164. except_res = np.ones([3, 2], dtype=np.int)
  165. assert np.all(res.asnumpy() == except_res)
  166. def test_np_asarray_list():
  167. """
  168. Feature: JIT Fallback
  169. Description: Test numpy with list to array in graph mode.
  170. Expectation: No exception.
  171. """
  172. @ms_function
  173. def np_asarray_list():
  174. x = [1, 2, 3]
  175. y = np.asarray(x)
  176. return Tensor(y)
  177. res = np_asarray_list()
  178. except_res = np.asarray([1, 2, 3])
  179. assert np.all(res.asnumpy() == except_res)
  180. def test_np_asarray_tuple():
  181. """
  182. Feature: JIT Fallback
  183. Description: Test numpy with tuple to array in graph mode.
  184. Expectation: No exception.
  185. """
  186. @ms_function
  187. def np_asarray_tuple():
  188. x = (1, 2, 3)
  189. y = np.asarray(x)
  190. return Tensor(y)
  191. res = np_asarray_tuple()
  192. except_res = np.asarray((1, 2, 3))
  193. assert np.all(res.asnumpy() == except_res)
  194. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  195. def test_np_frombuffer():
  196. """
  197. Feature: JIT Fallback
  198. Description: Test numpy with frombuffer in graph mode.
  199. Expectation: No exception.
  200. """
  201. @ms_function
  202. def np_frombuffer():
  203. s = b'Hello World'
  204. a = np.frombuffer(s, dtype='S1')
  205. return a
  206. res = np_frombuffer()
  207. print("res:", res)
  208. def test_np_fromiter():
  209. """
  210. Feature: JIT Fallback
  211. Description: Test numpy with fromiter in graph mode.
  212. Expectation: No exception.
  213. """
  214. @ms_function
  215. def np_fromiter():
  216. l = range(5)
  217. it = iter(l)
  218. x = np.fromiter(it, dtype=float)
  219. return Tensor(x)
  220. res = np_fromiter()
  221. except_res = np.asarray([0., 1., 2., 3., 4.])
  222. assert np.all(res.asnumpy() == except_res)
  223. def test_np_arange():
  224. """
  225. Feature: JIT Fallback
  226. Description: Test numpy with arange in graph mode.
  227. Expectation: No exception.
  228. """
  229. @ms_function
  230. def np_arange():
  231. x = np.arange(5, dtype=float)
  232. y = np.arange(10, 20, 2)
  233. return Tensor(x + y)
  234. res = np_arange()
  235. except_res = np.asarray([10., 13., 16., 19., 22.])
  236. assert np.all(res.asnumpy() == except_res)
  237. def test_np_logspace():
  238. """
  239. Feature: JIT Fallback
  240. Description: Test numpy with logspace in graph mode.
  241. Expectation: No exception.
  242. """
  243. @ms_function
  244. def np_logspace():
  245. a = np.logspace(0, 9, 10, base=2)
  246. return Tensor(a)
  247. res = np_logspace()
  248. except_res = np.array([1., 2., 4., 8., 16., 32., 64., 128., 256., 512.])
  249. assert np.all(res.asnumpy() == except_res)
  250. def test_np_array_shape():
  251. """
  252. Feature: JIT Fallback
  253. Description: Test numpy with array shape in graph mode.
  254. Expectation: No exception.
  255. """
  256. @ms_function
  257. def np_array_shape():
  258. a = np.array([[1, 2, 3], [4, 5, 6]])
  259. return Tensor(a.shape)
  260. res = np_array_shape()
  261. print("res:", res)
  262. def test_np_array_size():
  263. """
  264. Feature: JIT Fallback
  265. Description: Test numpy with array size in graph mode.
  266. Expectation: No exception.
  267. """
  268. @ms_function
  269. def np_array_size():
  270. a = np.array([[1, 2, 3], [4, 5, 6]])
  271. return Tensor(a.size)
  272. res = np_array_size()
  273. print("res:", res)
  274. def test_np_array_real():
  275. """
  276. Feature: JIT Fallback
  277. Description: Test numpy with complex in graph mode.
  278. Expectation: No exception.
  279. """
  280. @ms_function
  281. def np_array_real():
  282. a = np.array([1, 2, 3], dtype=complex)
  283. return Tensor(a.real)
  284. res = np_array_real()
  285. print("res:", res)
  286. def test_np_array_imag():
  287. """
  288. Feature: JIT Fallback
  289. Description: Test numpy with complex in graph mode.
  290. Expectation: No exception.
  291. """
  292. @ms_function
  293. def np_array_imag():
  294. a = np.array([1, 2, 3], dtype=complex)
  295. return Tensor(a.imag)
  296. res = np_array_imag()
  297. print("res:", res)
  298. def test_np_binop():
  299. """
  300. Feature: JIT Fallback
  301. Description: Test numpy's binary operation in graph mode.
  302. Expectation: No exception.
  303. """
  304. @ms_function
  305. def np_binop():
  306. a = np.array([1, 2, 3])
  307. b = np.array([4, 5, 6])
  308. c = a + b
  309. return Tensor(c)
  310. res = np_binop()
  311. assert np.all(res.asnumpy() == np.array([5, 7, 9]))
  312. def test_np_binop_2():
  313. """
  314. Feature: JIT Fallback
  315. Description: Test numpy's binary operation in graph mode.
  316. Expectation: No exception.
  317. """
  318. @ms_function
  319. def np_binop():
  320. a = np.int_(1)
  321. b = 4 + a
  322. return Tensor(b)
  323. res = np_binop()
  324. assert res == 5
  325. def test_np_compare():
  326. """
  327. Feature: JIT Fallback
  328. Description: Test numpy's compare operation in graph mode.
  329. Expectation: No exception.
  330. """
  331. @ms_function
  332. def np_compare():
  333. a = np.array([1, 2, 3])
  334. b = np.array([0, 2, 4])
  335. c = a > b
  336. return Tensor(c)
  337. res = np_compare()
  338. assert np.all(res.asnumpy() == np.array([True, False, False]))
  339. def test_np_compare_2():
  340. """
  341. Feature: JIT Fallback
  342. Description: Test numpy's compare operation in graph mode.
  343. Expectation: No exception.
  344. """
  345. @ms_function
  346. def np_compare():
  347. a = 1
  348. b = np.int_(3)
  349. c = a < b
  350. return Tensor(c)
  351. res = np_compare()
  352. assert res
  353. def test_np_bool_and():
  354. """
  355. Feature: JIT Fallback
  356. Description: Test AND operation in graph mode.
  357. Expectation: No exception.
  358. """
  359. @ms_function
  360. def np_bool_and():
  361. a = np.bool_(True)
  362. b = np.bool_(False)
  363. c = a and b
  364. return Tensor(c)
  365. res = np_bool_and()
  366. assert not res
  367. def test_np_bool_or():
  368. """
  369. Feature: JIT Fallback
  370. Description: Test OR operation in graph mode.
  371. Expectation: No exception.
  372. """
  373. @ms_function
  374. def np_bool_or():
  375. a = np.bool_(True)
  376. b = np.bool_(False)
  377. c = a or b
  378. return Tensor(c)
  379. res = np_bool_or()
  380. assert res
  381. def test_np_bool_or_2():
  382. """
  383. Feature: JIT Fallback
  384. Description: Test OR operation in graph mode.
  385. Expectation: No exception.
  386. """
  387. @ms_function
  388. def np_bool_or():
  389. out = 0 or np.bool_(True)
  390. return Tensor(out)
  391. res = np_bool_or()
  392. assert res
  393. def test_np_bool_not():
  394. """
  395. Feature: JIT Fallback
  396. Description: Test NOT operation in graph mode.
  397. Expectation: No exception.
  398. """
  399. @ms_function
  400. def np_bool_not():
  401. a = np.bool_(True)
  402. b = not a
  403. return Tensor(b)
  404. res = np_bool_not()
  405. assert not res
  406. def test_np_augassign():
  407. """
  408. Feature: JIT Fallback
  409. Description: Test augassign method in graph mode.
  410. Expectation: No exception.
  411. """
  412. @ms_function
  413. def np_augassign():
  414. value_add = np.array([1, 2, 3])
  415. value_add += np.array([4, 5, 6])
  416. value_sub = np.array([5, 5, 5])
  417. value_sub -= np.array([1, 2, 3])
  418. value_mul = np.int_(2)
  419. value_mul *= np.int_(3)
  420. value_div = np.int_(10)
  421. value_div /= np.int_(5)
  422. value_floordiv = np.int_(5)
  423. value_floordiv //= np.int_(2)
  424. return Tensor(value_add), Tensor(value_sub), Tensor(value_mul), Tensor(value_div), Tensor(value_floordiv)
  425. out_add, out_sub, out_mul, out_div, out_floordiv = np_augassign()
  426. assert np.all(out_add.asnumpy() == np.array([5, 7, 9]))
  427. assert np.all(out_sub.asnumpy() == np.array([4, 3, 2]))
  428. assert out_mul == 6
  429. assert out_div == 2
  430. assert out_floordiv == 2
  431. def test_np_augassign_2():
  432. """
  433. Feature: JIT Fallback
  434. Description: Test augassign method in graph mode.
  435. Expectation: No exception.
  436. """
  437. @ms_function
  438. def np_augassign():
  439. value_mod = np.int_(5)
  440. value_mod %= np.int_(2)
  441. value_pow = np.int_(3)
  442. value_pow **= np.int_(2)
  443. value_lshift = np.int_(4)
  444. value_lshift <<= 1
  445. value_rshift = np.int_(4)
  446. value_rshift >>= 1
  447. value_bitxor = np.int_(0)
  448. value_bitxor ^= 1
  449. return Tensor(value_mod), Tensor(value_pow), Tensor(value_lshift), Tensor(value_rshift), Tensor(value_bitxor)
  450. out_mod, out_pow, out_lshift, out_rshift, out_bitxor = np_augassign()
  451. assert out_mod == 1
  452. assert out_pow == 9
  453. assert out_lshift == 8
  454. assert out_rshift == 2
  455. assert out_bitxor == 1
  456. def test_np_subscript():
  457. """
  458. Feature: JIT Fallback
  459. Description: Test subscript method in graph mode.
  460. Expectation: No exception.
  461. """
  462. @ms_function
  463. def np_subscript():
  464. a = np.array([1, 2, 3])
  465. b = a[np.int32(1)]
  466. return Tensor(b)
  467. res = np_subscript()
  468. assert res == 2
  469. def test_np_slice():
  470. """
  471. Feature: JIT Fallback
  472. Description: Test slice method in graph mode.
  473. Expectation: No exception.
  474. """
  475. @ms_function
  476. def np_slice():
  477. a = np.arange(10)
  478. b = a[1:5]
  479. return Tensor(b)
  480. res = np_slice()
  481. assert np.all(res.asnumpy() == np.array([1, 2, 3, 4]))
  482. def test_np_random():
  483. """
  484. Feature: JIT Fallback
  485. Description: Test numpy.random module in graph mode.
  486. Expectation: No exception.
  487. """
  488. @ms_function
  489. def np_random():
  490. a = rand.randint(100, size=(5))
  491. b = a[1:5]
  492. return Tensor(b)
  493. res = np_random()
  494. print(res)