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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 = Tensor(np.array([1, 2, 3]))
  32. assert np.all(res.asnumpy() == expect_res.asnumpy())
  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 = Tensor(np.array([[1, 2], [3, 4]]))
  45. assert np.all(res.asnumpy() == expect_res.asnumpy())
  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 = Tensor(np.array([[1, 2, 3, 4, 5]]))
  58. assert np.all(res.asnumpy() == expect_res.asnumpy())
  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. assert np.all(res.asnumpy() == Tensor(np.array([1+0j, 2+0j, 3+0j])).asnumpy())
  71. def test_np_dtype_1():
  72. """
  73. Feature: JIT Fallback
  74. Description: Test numpy with dtype in graph mode.
  75. Expectation: No exception.
  76. """
  77. @ms_function
  78. def np_dtype_1():
  79. t = np.dtype(np.int32)
  80. return Tensor(np.array([1, 2, 3], dtype=t))
  81. res = np_dtype_1()
  82. assert np.all(res.asnumpy() == Tensor(np.array([1, 2, 3], dtype=np.int32)).asnumpy())
  83. def test_np_dtype_2():
  84. """
  85. Feature: JIT Fallback
  86. Description: Test numpy with dtype in graph mode.
  87. Expectation: No exception.
  88. """
  89. @ms_function
  90. def np_dtype_2():
  91. t = np.dtype('i4')
  92. return Tensor(np.array([1, 2, 3], dtype=t))
  93. res = np_dtype_2()
  94. assert np.all(res.asnumpy() == Tensor(np.array([1, 2, 3], dtype=np.int32)).asnumpy())
  95. def test_np_array_ndim():
  96. """
  97. Feature: JIT Fallback
  98. Description: Test numpy with array ndim in graph mode.
  99. Expectation: No exception.
  100. """
  101. @ms_function
  102. def np_array_ndim():
  103. a = np.arange(24)
  104. return Tensor(a.ndim)
  105. res = np_array_ndim()
  106. assert res == 1
  107. def test_np_array_reshape_1():
  108. """
  109. Feature: JIT Fallback
  110. Description: Test numpy with array reshape in graph mode.
  111. Expectation: No exception.
  112. """
  113. @ms_function
  114. def np_array_reshape_1():
  115. a = np.array([[1, 2, 3], [4, 5, 6]])
  116. b = a.reshape(3, 2)
  117. return Tensor(b.ndim)
  118. res = np_array_reshape_1()
  119. assert res == 2
  120. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  121. def test_np_array_reshape_2():
  122. """
  123. Feature: JIT Fallback
  124. Description: Test numpy with array reshape in graph mode.
  125. Expectation: No exception.
  126. """
  127. @ms_function
  128. def np_array_reshape_2():
  129. a = np.array([[1, 2, 3], [4, 5, 6]])
  130. a.shape = (3, 2)
  131. return a
  132. res = np_array_reshape_2()
  133. print("res:", res)
  134. def test_np_array_itemsize():
  135. """
  136. Feature: JIT Fallback
  137. Description: Test numpy with array reshape in graph mode.
  138. Expectation: No exception.
  139. """
  140. @ms_function
  141. def np_array_itemsize():
  142. a = np.array([1, 2, 3, 4, 5], dtype=np.int8)
  143. return Tensor(a.itemsize)
  144. res = np_array_itemsize()
  145. print("res:", res)
  146. assert res == 1
  147. def test_np_empty_zeros_ones():
  148. """
  149. Feature: JIT Fallback
  150. Description: Test numpy with array empty, zeros, ones in graph mode.
  151. Expectation: No exception.
  152. """
  153. @ms_function
  154. def np_empty_zeros_ones():
  155. x = np.empty([3, 2], dtype=np.int)
  156. y = np.zeros(x.shape, dtype=np.int)
  157. z = np.ones(x.shape, dtype=np.int)
  158. return Tensor(y + z)
  159. res = np_empty_zeros_ones()
  160. except_res = Tensor(np.ones([3, 2], dtype=np.int))
  161. assert np.all(res.asnumpy() == except_res.asnumpy())
  162. def test_np_asarray_list():
  163. """
  164. Feature: JIT Fallback
  165. Description: Test numpy with list to array in graph mode.
  166. Expectation: No exception.
  167. """
  168. @ms_function
  169. def np_asarray_list():
  170. x = [1, 2, 3]
  171. y = np.asarray(x)
  172. return Tensor(y)
  173. res = np_asarray_list()
  174. except_res = Tensor(np.asarray([1, 2, 3]))
  175. assert np.all(res.asnumpy() == except_res.asnumpy())
  176. def test_np_asarray_tuple():
  177. """
  178. Feature: JIT Fallback
  179. Description: Test numpy with tuple to array in graph mode.
  180. Expectation: No exception.
  181. """
  182. @ms_function
  183. def np_asarray_tuple():
  184. x = (1, 2, 3)
  185. y = np.asarray(x)
  186. return Tensor(y)
  187. res = np_asarray_tuple()
  188. except_res = Tensor(np.asarray((1, 2, 3)))
  189. assert np.all(res.asnumpy() == except_res.asnumpy())
  190. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  191. def test_np_frombuffer():
  192. """
  193. Feature: JIT Fallback
  194. Description: Test numpy with frombuffer in graph mode.
  195. Expectation: No exception.
  196. """
  197. @ms_function
  198. def np_frombuffer():
  199. s = b'Hello World'
  200. a = np.frombuffer(s, dtype='S1')
  201. return a
  202. res = np_frombuffer()
  203. print("res:", res)
  204. def test_np_fromiter():
  205. """
  206. Feature: JIT Fallback
  207. Description: Test numpy with fromiter in graph mode.
  208. Expectation: No exception.
  209. """
  210. @ms_function
  211. def np_fromiter():
  212. l = range(5)
  213. it = iter(l)
  214. x = np.fromiter(it, dtype=float)
  215. return Tensor(x)
  216. res = np_fromiter()
  217. except_res = Tensor(np.asarray([0., 1., 2., 3., 4.]))
  218. assert np.all(res.asnumpy() == except_res.asnumpy())
  219. def test_np_arange():
  220. """
  221. Feature: JIT Fallback
  222. Description: Test numpy with arange in graph mode.
  223. Expectation: No exception.
  224. """
  225. @ms_function
  226. def np_arange():
  227. x = np.arange(5, dtype=float)
  228. y = np.arange(10, 20, 2)
  229. return Tensor(x + y)
  230. res = np_arange()
  231. except_res = Tensor(np.asarray([10., 13., 16., 19., 22.]))
  232. assert np.all(res.asnumpy() == except_res.asnumpy())
  233. def test_np_logspace():
  234. """
  235. Feature: JIT Fallback
  236. Description: Test numpy with logspace in graph mode.
  237. Expectation: No exception.
  238. """
  239. @ms_function
  240. def np_logspace():
  241. a = np.logspace(0, 9, 10, base=2)
  242. return Tensor(a)
  243. res = np_logspace()
  244. except_res = Tensor(np.array([1., 2., 4., 8., 16., 32., 64., 128., 256., 512.]))
  245. assert np.all(res.asnumpy() == except_res.asnumpy())
  246. def test_np_array_shape():
  247. """
  248. Feature: JIT Fallback
  249. Description: Test numpy with array shape in graph mode.
  250. Expectation: No exception.
  251. """
  252. @ms_function
  253. def np_array_shape():
  254. a = np.array([[1, 2, 3], [4, 5, 6]])
  255. return Tensor(a.shape)
  256. res = np_array_shape()
  257. print("res:", res)
  258. def test_np_array_size():
  259. """
  260. Feature: JIT Fallback
  261. Description: Test numpy with array size in graph mode.
  262. Expectation: No exception.
  263. """
  264. @ms_function
  265. def np_array_size():
  266. a = np.array([[1, 2, 3], [4, 5, 6]])
  267. return Tensor(a.size)
  268. res = np_array_size()
  269. print("res:", res)
  270. def test_np_array_real():
  271. """
  272. Feature: JIT Fallback
  273. Description: Test numpy with complex in graph mode.
  274. Expectation: No exception.
  275. """
  276. @ms_function
  277. def np_array_real():
  278. a = np.array([1, 2, 3], dtype=complex)
  279. return Tensor(a.real)
  280. res = np_array_real()
  281. print("res:", res)
  282. def test_np_array_imag():
  283. """
  284. Feature: JIT Fallback
  285. Description: Test numpy with complex in graph mode.
  286. Expectation: No exception.
  287. """
  288. @ms_function
  289. def np_array_imag():
  290. a = np.array([1, 2, 3], dtype=complex)
  291. return Tensor(a.imag)
  292. res = np_array_imag()
  293. print("res:", res)
  294. def test_np_binop():
  295. """
  296. Feature: JIT Fallback
  297. Description: Test numpy's binary operation in graph mode.
  298. Expectation: No exception.
  299. """
  300. @ms_function
  301. def np_binop():
  302. a = np.array([1, 2, 3])
  303. b = np.array([4, 5, 6])
  304. c = a + b
  305. return Tensor(c)
  306. res = np_binop()
  307. assert np.all(res.asnumpy() == np.array([5, 7, 9]))
  308. def test_np_compare():
  309. """
  310. Feature: JIT Fallback
  311. Description: Test numpy's compare operation in graph mode.
  312. Expectation: No exception.
  313. """
  314. @ms_function
  315. def np_compare():
  316. a = np.array([1, 2, 3])
  317. b = np.array([0, 2, 4])
  318. c = a > b
  319. return Tensor(c)
  320. res = np_compare()
  321. assert np.all(res.asnumpy() == np.array([True, False, False]))
  322. def test_np_bool_and():
  323. """
  324. Feature: JIT Fallback
  325. Description: Test AND operation in graph mode.
  326. Expectation: No exception.
  327. """
  328. @ms_function
  329. def np_bool_and():
  330. a = np.bool_(True)
  331. b = np.bool_(False)
  332. c = a and b
  333. return Tensor(c)
  334. res = np_bool_and()
  335. assert not res.asnumpy()
  336. def test_np_bool_or():
  337. """
  338. Feature: JIT Fallback
  339. Description: Test OR operation in graph mode.
  340. Expectation: No exception.
  341. """
  342. @ms_function
  343. def np_bool_or():
  344. a = np.bool_(True)
  345. b = np.bool_(False)
  346. c = a or b
  347. return Tensor(c)
  348. res = np_bool_or()
  349. assert res.asnumpy()
  350. def test_np_bool_not():
  351. """
  352. Feature: JIT Fallback
  353. Description: Test NOT operation in graph mode.
  354. Expectation: No exception.
  355. """
  356. @ms_function
  357. def np_bool_not():
  358. a = np.bool_(True)
  359. b = not a
  360. return Tensor(b)
  361. res = np_bool_not()
  362. assert not res.asnumpy()