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

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