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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  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. assert np.all(res.asnumpy() == Tensor(np.array([1+0j, 2+0j, 3+0j])).asnumpy())
  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. assert np.all(res.asnumpy() == Tensor(np.array([1, 2, 3], dtype=np.int32)).asnumpy())
  84. def test_np_dtype_2():
  85. """
  86. Feature: JIT Fallback
  87. Description: Test numpy with dtype in graph mode.
  88. Expectation: No exception.
  89. """
  90. @ms_function
  91. def np_dtype_2():
  92. t = np.dtype('i4')
  93. return Tensor(np.array([1, 2, 3], dtype=t))
  94. res = np_dtype_2()
  95. assert np.all(res.asnumpy() == Tensor(np.array([1, 2, 3], dtype=np.int32)).asnumpy())
  96. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  97. def test_np_dtype_3():
  98. """
  99. Feature: JIT Fallback
  100. Description: Test numpy with dtype in graph mode.
  101. Expectation: No exception.
  102. """
  103. @ms_function
  104. def np_dtype_3():
  105. t = np.dtype([('age', np.int8)])
  106. return Tensor(np.array([1, 2, 3], dtype=t))
  107. res = np_dtype_3()
  108. print("res:", res)
  109. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  110. def test_np_dtype_4():
  111. """
  112. Feature: JIT Fallback
  113. Description: Test numpy with dtype in graph mode.
  114. Expectation: No exception.
  115. """
  116. @ms_function
  117. def np_dtype_4():
  118. student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
  119. a = np.array([('abc', 21, 50), ('xyz', 18, 75)], dtype=student)
  120. return Tensor(a)
  121. res = np_dtype_4()
  122. print("res:", res)
  123. def test_np_array_ndim():
  124. """
  125. Feature: JIT Fallback
  126. Description: Test numpy with array ndim in graph mode.
  127. Expectation: No exception.
  128. """
  129. @ms_function
  130. def np_array_ndim():
  131. a = np.arange(24)
  132. return Tensor(a.ndim)
  133. res = np_array_ndim()
  134. assert res == 1
  135. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  136. def test_np_array_reshape_1():
  137. """
  138. Feature: JIT Fallback
  139. Description: Test numpy with array reshape in graph mode.
  140. Expectation: No exception.
  141. """
  142. @ms_function
  143. def np_array_reshape_1():
  144. a = np.array([[1, 2, 3], [4, 5, 6]])
  145. b = a.reshape(3, 2)
  146. return Tensor(b.ndim)
  147. res = np_array_reshape_1()
  148. assert res == 2
  149. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  150. def test_np_array_reshape_2():
  151. """
  152. Feature: JIT Fallback
  153. Description: Test numpy with array reshape in graph mode.
  154. Expectation: No exception.
  155. """
  156. @ms_function
  157. def np_array_reshape_2():
  158. a = np.array([[1, 2, 3], [4, 5, 6]])
  159. a.shape = (3, 2)
  160. return a
  161. res = np_array_reshape_2()
  162. print("res:", res)
  163. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  164. def test_np_array_itemsize():
  165. """
  166. Feature: JIT Fallback
  167. Description: Test numpy with array reshape in graph mode.
  168. Expectation: No exception.
  169. """
  170. @ms_function
  171. def np_array_itemsize():
  172. a = np.array([1, 2, 3, 4, 5], dtype=np.int8)
  173. return Tensor(a.itemsize)
  174. res = np_array_itemsize()
  175. print("res:", res)
  176. assert res == 1
  177. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  178. def test_np_array_flags():
  179. """
  180. Feature: JIT Fallback
  181. Description: Test numpy with array flags in graph mode.
  182. Expectation: No exception.
  183. """
  184. @ms_function
  185. def np_array_flags():
  186. a = np.array([1, 2, 3, 4, 5])
  187. return a.flags
  188. res = np_array_flags()
  189. print("res:", res)
  190. def test_np_empty_zeros_ones():
  191. """
  192. Feature: JIT Fallback
  193. Description: Test numpy with array empty, zeros, ones in graph mode.
  194. Expectation: No exception.
  195. """
  196. @ms_function
  197. def np_empty_zeros_ones():
  198. x = np.empty([3, 2], dtype=np.int)
  199. y = np.zeros(x.shape, dtype=np.int)
  200. z = np.ones(x.shape, dtype=np.int)
  201. return Tensor(y + z)
  202. res = np_empty_zeros_ones()
  203. except_res = Tensor(np.ones([3, 2], dtype=np.int))
  204. assert np.all(res.asnumpy() == except_res.asnumpy())
  205. def test_np_asarray_list():
  206. """
  207. Feature: JIT Fallback
  208. Description: Test numpy with list to array in graph mode.
  209. Expectation: No exception.
  210. """
  211. @ms_function
  212. def np_asarray_list():
  213. x = [1, 2, 3]
  214. y = np.asarray(x)
  215. return Tensor(y)
  216. res = np_asarray_list()
  217. except_res = Tensor(np.asarray([1, 2, 3]))
  218. assert np.all(res.asnumpy() == except_res.asnumpy())
  219. def test_np_asarray_tuple():
  220. """
  221. Feature: JIT Fallback
  222. Description: Test numpy with tuple to array in graph mode.
  223. Expectation: No exception.
  224. """
  225. @ms_function
  226. def np_asarray_tuple():
  227. x = (1, 2, 3)
  228. y = np.asarray(x)
  229. return Tensor(y)
  230. res = np_asarray_tuple()
  231. except_res = Tensor(np.asarray((1, 2, 3)))
  232. assert np.all(res.asnumpy() == except_res.asnumpy())
  233. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  234. def test_np_asarray_tuple_list():
  235. """
  236. Feature: JIT Fallback
  237. Description: Test numpy with tuple list to array in graph mode.
  238. Expectation: No exception.
  239. """
  240. @ms_function
  241. def np_asarray_tuple_list():
  242. x = [(1, 2, 3), (4, 5)]
  243. y = np.asarray(x)
  244. return Tensor(y)
  245. res = np_asarray_tuple_list()
  246. print("res:", res)
  247. @pytest.mark.skip(reason='Not support graph fallback feature yet')
  248. def test_np_frombuffer():
  249. """
  250. Feature: JIT Fallback
  251. Description: Test numpy with frombuffer in graph mode.
  252. Expectation: No exception.
  253. """
  254. @ms_function
  255. def np_frombuffer():
  256. s = b'Hello World'
  257. a = np.frombuffer(s, dtype='S1')
  258. return a
  259. res = np_frombuffer()
  260. print("res:", res)
  261. def test_np_fromiter():
  262. """
  263. Feature: JIT Fallback
  264. Description: Test numpy with fromiter in graph mode.
  265. Expectation: No exception.
  266. """
  267. @ms_function
  268. def np_fromiter():
  269. l = range(5)
  270. it = iter(l)
  271. x = np.fromiter(it, dtype=float)
  272. return Tensor(x)
  273. res = np_fromiter()
  274. except_res = Tensor(np.asarray([0., 1., 2., 3., 4.]))
  275. assert np.all(res.asnumpy() == except_res.asnumpy())
  276. def test_np_arange():
  277. """
  278. Feature: JIT Fallback
  279. Description: Test numpy with arange in graph mode.
  280. Expectation: No exception.
  281. """
  282. @ms_function
  283. def np_arange():
  284. x = np.arange(5, dtype=float)
  285. y = np.arange(10, 20, 2)
  286. return Tensor(x + y)
  287. res = np_arange()
  288. except_res = Tensor(np.asarray([10., 13., 16., 19., 22.]))
  289. assert np.all(res.asnumpy() == except_res.asnumpy())
  290. def test_np_logspace():
  291. """
  292. Feature: JIT Fallback
  293. Description: Test numpy with logspace in graph mode.
  294. Expectation: No exception.
  295. """
  296. @ms_function
  297. def np_logspace():
  298. a = np.logspace(0, 9, 10, base=2)
  299. return Tensor(a)
  300. res = np_logspace()
  301. except_res = Tensor(np.array([1., 2., 4., 8., 16., 32., 64., 128., 256., 512.]))
  302. assert np.all(res.asnumpy() == except_res.asnumpy())