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_tensor.py 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. # Copyright 2020 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. """
  16. @File : test_tensor.py
  17. @Author:
  18. @Date : 2019-03-14
  19. @Desc : test mindspore tensor's operation
  20. """
  21. import numpy as np
  22. import pytest
  23. import mindspore as ms
  24. import mindspore.common.api as me
  25. import mindspore.nn as nn
  26. from ..ut_filter import non_graph_engine
  27. ndarr = np.ones((2, 3))
  28. def test_tensor_flatten():
  29. with pytest.raises(AttributeError):
  30. lst = [1, 2, 3, 4,]
  31. tensor_list = ms.Tensor(lst, ms.float32)
  32. tensor_list = tensor_list.Flatten()
  33. print(tensor_list)
  34. def test_tensor_list():
  35. lst = [[1.0, 2.0, 1.0], [1.0, 10.0, 9.0]]
  36. tensor_list = ms.Tensor(lst, ms.float32)
  37. print(tensor_list)
  38. def test_tensor():
  39. """test_tensor"""
  40. t1 = ms.Tensor(ndarr)
  41. assert isinstance(t1, ms.Tensor)
  42. assert t1.dtype() == ms.float64
  43. t2 = ms.Tensor(np.zeros([1, 2, 3]), ms.float32)
  44. assert isinstance(t2, ms.Tensor)
  45. assert t2.shape() == (1, 2, 3)
  46. assert t2.dtype() == ms.float32
  47. t3 = ms.Tensor(0.1)
  48. assert isinstance(t3, ms.Tensor)
  49. assert t3.dtype() == ms.float64
  50. t4 = ms.Tensor(1)
  51. assert isinstance(t4, ms.Tensor)
  52. assert t4.dtype() == ms.int64
  53. def test_tensor_type_float16():
  54. t_float16 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float16))
  55. assert isinstance(t_float16, ms.Tensor)
  56. assert t_float16.shape() == (2, 3)
  57. assert t_float16.dtype() == ms.float16
  58. def test_tensor_type_float32():
  59. t_float32 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32))
  60. assert isinstance(t_float32, ms.Tensor)
  61. assert t_float32.shape() == (2, 3)
  62. assert t_float32.dtype() == ms.float32
  63. def test_tensor_type_float32_user_define():
  64. t = ms.Tensor(np.zeros([1, 2, 3]), ms.float32)
  65. assert isinstance(t, ms.Tensor)
  66. assert t.shape() == (1, 2, 3)
  67. assert t.dtype() == ms.float32
  68. def test_tensor_type_float64():
  69. t = ms.Tensor([[1.0, 2, 3], [4, 5, 6]])
  70. assert isinstance(t, ms.Tensor)
  71. assert t.shape() == (2, 3)
  72. assert t.dtype() == ms.float64
  73. t_zero = ms.Tensor(np.zeros([1, 2, 3]))
  74. assert isinstance(t_zero, ms.Tensor)
  75. assert t_zero.shape() == (1, 2, 3)
  76. assert t_zero.dtype() == ms.float64
  77. def test_tensor_type_float64_user_define():
  78. t = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=float))
  79. assert isinstance(t, ms.Tensor)
  80. assert t.shape() == (2, 3)
  81. assert t.dtype() == ms.float64
  82. t_float64 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]]), ms.float64)
  83. assert isinstance(t_float64, ms.Tensor)
  84. assert t_float64.shape() == (2, 3)
  85. assert t_float64.dtype() == ms.float64
  86. def test_tensor_type_bool():
  87. # init a tensor with bool type
  88. ts_bool_array = ms.Tensor(np.zeros([2, 3], np.bool), ms.bool_)
  89. assert isinstance(ts_bool_array, ms.Tensor)
  90. assert ts_bool_array.dtype() == ms.bool_
  91. t_bool = ms.Tensor(True)
  92. assert isinstance(t_bool, ms.Tensor)
  93. assert t_bool.dtype() == ms.bool_
  94. t_bool_array = ms.Tensor(np.array([[True, False, True], [False, False, False]]))
  95. assert isinstance(t_bool_array, ms.Tensor)
  96. assert t_bool_array.shape() == (2, 3)
  97. assert t_bool_array.dtype() == ms.bool_
  98. def test_tensor_type_int8():
  99. t_int8_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int8))
  100. assert isinstance(t_int8_array, ms.Tensor)
  101. assert t_int8_array.shape() == (2, 3)
  102. assert t_int8_array.dtype() == ms.int8
  103. def test_tensor_type_int16():
  104. t_int16_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16))
  105. assert isinstance(t_int16_array, ms.Tensor)
  106. assert t_int16_array.shape() == (2, 3)
  107. assert t_int16_array.dtype() == ms.int16
  108. def test_tensor_type_int32():
  109. t_int = ms.Tensor([[1, 2, 3], [4, 5, 6]])
  110. assert isinstance(t_int, ms.Tensor)
  111. assert t_int.shape() == (2, 3)
  112. assert t_int.dtype() == ms.int64
  113. t_int_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  114. assert isinstance(t_int_array, ms.Tensor)
  115. assert t_int_array.shape() == (2, 3)
  116. assert t_int_array.dtype() == ms.int32
  117. def test_tensor_type_int64():
  118. t_int64 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64))
  119. assert isinstance(t_int64, ms.Tensor)
  120. assert t_int64.shape() == (2, 3)
  121. assert t_int64.dtype() == ms.int64
  122. def test_tensor_type_uint8():
  123. t_uint8_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8))
  124. assert isinstance(t_uint8_array, ms.Tensor)
  125. assert t_uint8_array.shape() == (2, 3)
  126. assert t_uint8_array.dtype() == ms.uint8
  127. def test_tensor_type_uint16():
  128. t_uint16_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint16))
  129. assert isinstance(t_uint16_array, ms.Tensor)
  130. assert t_uint16_array.shape() == (2, 3)
  131. assert t_uint16_array.dtype() == ms.uint16
  132. def test_tensor_type_uint32():
  133. t_uint32_array = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint32))
  134. assert isinstance(t_uint32_array, ms.Tensor)
  135. assert t_uint32_array.shape() == (2, 3)
  136. assert t_uint32_array.dtype() == ms.uint32
  137. def test_tensor_type_uint64():
  138. t_uint64 = ms.Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint64))
  139. assert isinstance(t_uint64, ms.Tensor)
  140. assert t_uint64.shape() == (2, 3)
  141. assert t_uint64.dtype() == ms.uint64
  142. def test_set_type():
  143. t = ms.Tensor(ndarr)
  144. t.set_dtype(ms.float32)
  145. assert t.dtype() == ms.float32
  146. @non_graph_engine
  147. def test_add():
  148. x = ms.Tensor(ndarr)
  149. y = ms.Tensor(ndarr)
  150. z = x + y
  151. assert isinstance(z, ms.Tensor)
  152. @non_graph_engine
  153. def test_sub():
  154. x = ms.Tensor(ndarr)
  155. y = ms.Tensor(ndarr)
  156. z = x - y
  157. assert isinstance(z, ms.Tensor)
  158. class Net(nn.Cell):
  159. """Net definition"""
  160. def __init__(self, dim):
  161. super(Net, self).__init__()
  162. self.dim = dim
  163. def construct(self, input_x):
  164. return input_x
  165. @non_graph_engine
  166. def test_return_tensor():
  167. """test_return_tensor"""
  168. net = Net(0)
  169. input_data = ms.Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32'))
  170. input_data.set_dtype(ms.float32)
  171. exe = me._executor
  172. exe.compile(net, input_data)
  173. tensor_ = exe(net, input_data)
  174. # get shape
  175. shape_ = tensor_.shape()
  176. print("shape = ", shape_)
  177. # get type
  178. type_ = tensor_.dtype()
  179. print("type = ", type_)
  180. # get value
  181. value_ = tensor_.asnumpy()
  182. print("numpy value = ", value_)
  183. def test_tensor_contiguous():
  184. """test_tensor_contiguous"""
  185. input_c = np.arange(6).reshape(2, 3)
  186. input_f = input_c.T
  187. np.ascontiguousarray(input_c, dtype=np.float32)
  188. assert True, input_c.flags['C_CONTIGUOUS']
  189. print("input_f flags = ", input_f.flags)
  190. assert True, input_f.flags['F_CONTIGUOUS']
  191. tensor_f_float32 = ms.Tensor(input_f)
  192. rt_f = tensor_f_float32.asnumpy()
  193. assert True, rt_f.flags['C_CONTIGUOUS']
  194. print("rt_f flags = ", rt_f.flags)
  195. def test_tensor_contiguous2():
  196. input_data = np.random.randn(32, 112, 112, 3).astype(np.float32)
  197. input_me = input_data.transpose(0, 3, 1, 2)
  198. print("input_me flags = ", input_me.flags)
  199. tensor_f_float32 = ms.Tensor(input_me)
  200. out_f = tensor_f_float32.asnumpy()
  201. print("out_f flags = ", out_f.flags)
  202. def test_tensor_input_string():
  203. with pytest.raises(TypeError):
  204. input_data = 'ccc'
  205. ms.Tensor(input_data)
  206. def test_tensor_input_tuple_string():
  207. with pytest.raises(TypeError):
  208. input_data = (2, 3, '4', 5)
  209. ms.Tensor(input_data)
  210. def test_tensor_input_list_string():
  211. with pytest.raises(TypeError):
  212. input_data = [[2, 3, '4', 5], [1, 2, 3, 4]]
  213. ms.Tensor(input_data)
  214. def test_tensor_input_none():
  215. with pytest.raises(TypeError):
  216. input_data = None
  217. ms.Tensor(input_data, np.int64)
  218. # pylint: disable=no-value-for-parameter
  219. def test_tensor_input_empty():
  220. with pytest.raises(TypeError):
  221. ms.Tensor()
  222. def test_tensor_input_ndarray_str():
  223. with pytest.raises(TypeError):
  224. inp = np.array(["88", 2, 4])
  225. ms.Tensor(inp)
  226. def test_tensor_input_ndarray_bool():
  227. inp = np.array([True, 2, 4])
  228. ms.Tensor(inp)
  229. inp = np.array([False, 2, 4])
  230. ms.Tensor(inp)
  231. def test_tensor_input_ndarray_complex():
  232. with pytest.raises(TypeError):
  233. inp = np.array([20j, 2, 4])
  234. ms.Tensor(inp)
  235. def test_tensor_input_ndarray_none():
  236. with pytest.raises(TypeError):
  237. inp = np.array([None, 2, 4])
  238. ms.Tensor(inp)
  239. def test_tensor_input_ndarray_dict():
  240. with pytest.raises(TypeError):
  241. inp = {'a': 6, 'b': 7}
  242. ms.Tensor(inp)
  243. def test_tensor_input_np_nan():
  244. with pytest.raises(TypeError):
  245. input_data = (1, 2, 3, np.nan)
  246. ms.Tensor(input_data, np.int64)
  247. def test_tensor_input_tuple_inf():
  248. with pytest.raises(TypeError):
  249. input_data = (1, 2, 3, float("inf"))
  250. ms.Tensor(input_data, np.int64)
  251. def test_tensor_input_dict():
  252. with pytest.raises(TypeError):
  253. input_data = {'a': 6, 'b': 7}
  254. ms.Tensor(input_data, np.int64)
  255. def test_tensor_input_complex():
  256. with pytest.raises(TypeError):
  257. input_data = (1, 2j, 3)
  258. ms.Tensor(input_data, np.int64)
  259. def test_tensor_dtype_np_float():
  260. with pytest.raises(TypeError):
  261. input_data = np.random.randn(32, 112, 112, 3).astype(np.float)
  262. ms.Tensor(input_data, np.float)
  263. def test_tensor_dtype_np_float16():
  264. with pytest.raises(TypeError):
  265. input_data = np.random.randn(32, 112, 112, 3).astype(np.float16)
  266. ms.Tensor(input_data, np.float16)
  267. def test_tensor_dtype_np_float32():
  268. with pytest.raises(TypeError):
  269. input_data = np.random.randn(32, 112, 112, 3).astype(np.float32)
  270. ms.Tensor(input_data, np.float32)
  271. def test_tensor_dtype_np_float64():
  272. with pytest.raises(TypeError):
  273. input_data = np.random.randn(32, 112, 112, 3).astype(np.float64)
  274. ms.Tensor(input_data, np.float64)
  275. def test_tensor_dtype_np_int():
  276. with pytest.raises(TypeError):
  277. input_data = np.random.randn(32, 112, 112, 3).astype(np.int)
  278. ms.Tensor(input_data, np.int)
  279. def test_tensor_dtype_np_int8():
  280. with pytest.raises(TypeError):
  281. input_data = np.random.randn(32, 112, 112, 3).astype(np.int8)
  282. ms.Tensor(input_data, np.int8)
  283. def test_tensor_dtype_np_int16():
  284. with pytest.raises(TypeError):
  285. input_data = np.random.randn(32, 112, 112, 3).astype(np.int16)
  286. ms.Tensor(input_data, np.int16)
  287. def test_tensor_dtype_np_int32():
  288. with pytest.raises(TypeError):
  289. input_data = np.random.randn(32, 112, 112, 3).astype(np.int32)
  290. ms.Tensor(input_data, np.int32)
  291. def test_tensor_dtype_np_int64():
  292. with pytest.raises(TypeError):
  293. input_data = np.random.randn(32, 112, 112, 3).astype(np.int64)
  294. ms.Tensor(input_data, np.int64)
  295. def test_tensor_dtype_fp32_to_bool():
  296. with pytest.raises(RuntimeError):
  297. input = np.random.randn(2, 3, 4, 5).astype(np.float32)
  298. input = ms.Tensor(input)
  299. input_me = ms.Tensor(input, dtype=ms.bool_)