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_cast_op.py 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. import numpy as np
  16. import pytest
  17. import mindspore.common.dtype as mstype
  18. import mindspore.context as context
  19. from mindspore.common.tensor import Tensor
  20. from mindspore.nn import Cell
  21. from mindspore.ops import operations as P
  22. class Net(Cell):
  23. def __init__(self, dtype):
  24. super(Net, self).__init__()
  25. self.Cast = P.Cast()
  26. self.dtype = dtype
  27. def construct(self, x):
  28. return self.Cast(x, self.dtype)
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_cpu
  31. @pytest.mark.env_onecard
  32. def test_cast_int32():
  33. x0 = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.float32))
  34. x1 = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.int32))
  35. x2 = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.bool))
  36. t = mstype.int32
  37. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  38. net = Net(t)
  39. output = net(x0)
  40. type0 = output.asnumpy().dtype
  41. assert type0 == 'int32'
  42. output = net(x1)
  43. type1 = output.asnumpy().dtype
  44. assert type1 == 'int32'
  45. output = net(x2)
  46. type2 = output.asnumpy().dtype
  47. assert type2 == 'int32'
  48. @pytest.mark.level0
  49. @pytest.mark.platform_x86_cpu
  50. @pytest.mark.env_onecard
  51. def test_cast_float32():
  52. x0 = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.float32))
  53. x1 = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.int32))
  54. x2 = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.bool))
  55. t = mstype.float32
  56. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  57. net = Net(t)
  58. output = net(x0)
  59. type0 = output.asnumpy().dtype
  60. assert type0 == 'float32'
  61. output = net(x1)
  62. type1 = output.asnumpy().dtype
  63. assert type1 == 'float32'
  64. output = net(x2)
  65. type2 = output.asnumpy().dtype
  66. assert type2 == 'float32'
  67. @pytest.mark.level0
  68. @pytest.mark.platform_x86_cpu
  69. @pytest.mark.env_onecard
  70. def test_cast_int8_to_int16():
  71. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.int8))
  72. t = mstype.int16
  73. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  74. net = Net(t)
  75. output = net(x)
  76. dtype = output.asnumpy().dtype
  77. assert dtype == 'int16'
  78. @pytest.mark.level0
  79. @pytest.mark.platform_x86_cpu
  80. @pytest.mark.env_onecard
  81. def test_cast_int8_to_int32():
  82. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.int8))
  83. t = mstype.int32
  84. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  85. net = Net(t)
  86. output = net(x)
  87. dtype = output.asnumpy().dtype
  88. assert dtype == 'int32'
  89. @pytest.mark.level0
  90. @pytest.mark.platform_x86_cpu
  91. @pytest.mark.env_onecard
  92. def test_cast_int8_to_int64():
  93. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.int8))
  94. t = mstype.int64
  95. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  96. net = Net(t)
  97. output = net(x)
  98. dtype = output.asnumpy().dtype
  99. assert dtype == 'int64'
  100. @pytest.mark.level0
  101. @pytest.mark.platform_x86_cpu
  102. @pytest.mark.env_onecard
  103. def test_cast_uint8_to_int16():
  104. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint8))
  105. t = mstype.int16
  106. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  107. net = Net(t)
  108. output = net(x)
  109. dtype = output.asnumpy().dtype
  110. assert dtype == 'int16'
  111. @pytest.mark.level0
  112. @pytest.mark.platform_x86_cpu
  113. @pytest.mark.env_onecard
  114. def test_cast_uint8_to_int32():
  115. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint8))
  116. t = mstype.int32
  117. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  118. net = Net(t)
  119. output = net(x)
  120. dtype = output.asnumpy().dtype
  121. assert dtype == 'int32'
  122. @pytest.mark.level0
  123. @pytest.mark.platform_x86_cpu
  124. @pytest.mark.env_onecard
  125. def test_cast_uint8_to_int64():
  126. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint8))
  127. t = mstype.int64
  128. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  129. net = Net(t)
  130. output = net(x)
  131. dtype = output.asnumpy().dtype
  132. assert dtype == 'int64'
  133. @pytest.mark.level0
  134. @pytest.mark.platform_x86_cpu
  135. @pytest.mark.env_onecard
  136. def test_cast_uint8_to_uint16():
  137. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint8))
  138. t = mstype.uint16
  139. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  140. net = Net(t)
  141. output = net(x)
  142. dtype = output.asnumpy().dtype
  143. assert dtype == 'uint16'
  144. @pytest.mark.level0
  145. @pytest.mark.platform_x86_cpu
  146. @pytest.mark.env_onecard
  147. def test_cast_uint8_to_uint32():
  148. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint8))
  149. t = mstype.uint32
  150. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  151. net = Net(t)
  152. output = net(x)
  153. dtype = output.asnumpy().dtype
  154. assert dtype == 'uint32'
  155. @pytest.mark.level0
  156. @pytest.mark.platform_x86_cpu
  157. @pytest.mark.env_onecard
  158. def test_cast_uint8_to_uint64():
  159. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint8))
  160. t = mstype.uint64
  161. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  162. net = Net(t)
  163. output = net(x)
  164. dtype = output.asnumpy().dtype
  165. assert dtype == 'uint64'
  166. @pytest.mark.level0
  167. @pytest.mark.platform_x86_cpu
  168. @pytest.mark.env_onecard
  169. def test_cast_int16_to_int32():
  170. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.int16))
  171. t = mstype.int32
  172. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  173. net = Net(t)
  174. output = net(x)
  175. dtype = output.asnumpy().dtype
  176. assert dtype == 'int32'
  177. @pytest.mark.level0
  178. @pytest.mark.platform_x86_cpu
  179. @pytest.mark.env_onecard
  180. def test_cast_int16_to_int64():
  181. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.int16))
  182. t = mstype.int64
  183. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  184. net = Net(t)
  185. output = net(x)
  186. dtype = output.asnumpy().dtype
  187. assert dtype == 'int64'
  188. @pytest.mark.level0
  189. @pytest.mark.platform_x86_cpu
  190. @pytest.mark.env_onecard
  191. def test_cast_uint16_to_int32():
  192. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint16))
  193. t = mstype.int32
  194. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  195. net = Net(t)
  196. output = net(x)
  197. dtype = output.asnumpy().dtype
  198. assert dtype == 'int32'
  199. @pytest.mark.level0
  200. @pytest.mark.platform_x86_cpu
  201. @pytest.mark.env_onecard
  202. def test_cast_uint16_to_int64():
  203. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint16))
  204. t = mstype.int64
  205. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  206. net = Net(t)
  207. output = net(x)
  208. dtype = output.asnumpy().dtype
  209. assert dtype == 'int64'
  210. @pytest.mark.level0
  211. @pytest.mark.platform_x86_cpu
  212. @pytest.mark.env_onecard
  213. def test_cast_uint16_to_uint32():
  214. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint16))
  215. t = mstype.uint32
  216. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  217. net = Net(t)
  218. output = net(x)
  219. dtype = output.asnumpy().dtype
  220. assert dtype == 'uint32'
  221. @pytest.mark.level0
  222. @pytest.mark.platform_x86_cpu
  223. @pytest.mark.env_onecard
  224. def test_cast_uint16_to_uint64():
  225. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint16))
  226. t = mstype.uint64
  227. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  228. net = Net(t)
  229. output = net(x)
  230. dtype = output.asnumpy().dtype
  231. assert dtype == 'uint64'
  232. @pytest.mark.level0
  233. @pytest.mark.platform_x86_cpu
  234. @pytest.mark.env_onecard
  235. def test_cast_int32_to_int64():
  236. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.int32))
  237. t = mstype.int64
  238. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  239. net = Net(t)
  240. output = net(x)
  241. dtype = output.asnumpy().dtype
  242. assert dtype == 'int64'
  243. @pytest.mark.level0
  244. @pytest.mark.platform_x86_cpu
  245. @pytest.mark.env_onecard
  246. def test_cast_uint32_to_int64():
  247. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint32))
  248. t = mstype.int64
  249. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  250. net = Net(t)
  251. output = net(x)
  252. dtype = output.asnumpy().dtype
  253. assert dtype == 'int64'
  254. @pytest.mark.level0
  255. @pytest.mark.platform_x86_cpu
  256. @pytest.mark.env_onecard
  257. def test_cast_uint32_to_uint64():
  258. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.uint32))
  259. t = mstype.uint64
  260. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  261. net = Net(t)
  262. output = net(x)
  263. dtype = output.asnumpy().dtype
  264. assert dtype == 'uint64'
  265. @pytest.mark.level0
  266. @pytest.mark.platform_x86_cpu
  267. @pytest.mark.env_onecard
  268. def test_cast_float16_to_float32():
  269. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.float16))
  270. t = mstype.float32
  271. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  272. net = Net(t)
  273. output = net(x)
  274. dtype = output.asnumpy().dtype
  275. assert dtype == 'float32'
  276. @pytest.mark.level0
  277. @pytest.mark.platform_x86_cpu
  278. @pytest.mark.env_onecard
  279. def test_cast_float16_to_float64():
  280. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.float16))
  281. t = mstype.float64
  282. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  283. net = Net(t)
  284. output = net(x)
  285. dtype = output.asnumpy().dtype
  286. assert dtype == 'float64'
  287. @pytest.mark.level0
  288. @pytest.mark.platform_x86_cpu
  289. @pytest.mark.env_onecard
  290. def test_cast_float32_to_float64():
  291. x = Tensor(np.random.uniform(-2, 2, (3, 2)).astype(np.float32))
  292. t = mstype.float64
  293. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  294. net = Net(t)
  295. output = net(x)
  296. dtype = output.asnumpy().dtype
  297. assert dtype == 'float64'