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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # Copyright 2019 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, type0, type1):
  24. super(Net, self).__init__()
  25. self.Cast = P.Cast()
  26. self.type0 = type0
  27. self.type1 = type1
  28. def construct(self, x0, x1):
  29. output = (self.Cast(x0, self.type0),
  30. self.Cast(x1, self.type1))
  31. return output
  32. @pytest.mark.level0
  33. @pytest.mark.platform_x86_gpu_training
  34. @pytest.mark.env_onecard
  35. def test_cast():
  36. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  37. t0 = mstype.float16
  38. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float16))
  39. t1 = mstype.float32
  40. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  41. net = Net(t0, t1)
  42. output = net(x0, x1)
  43. type0 = output[0].asnumpy().dtype
  44. assert type0 == 'float16'
  45. type1 = output[1].asnumpy().dtype
  46. assert type1 == 'float32'
  47. @pytest.mark.level0
  48. @pytest.mark.platform_x86_gpu_training
  49. @pytest.mark.env_onecard
  50. def test_cast1():
  51. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  52. t0 = mstype.float32
  53. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  54. t1 = mstype.float32
  55. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  56. net = Net(t0, t1)
  57. output = net(x0, x1)
  58. type0 = output[0].asnumpy().dtype
  59. assert type0 == 'float32'
  60. type1 = output[1].asnumpy().dtype
  61. assert type1 == 'float32'
  62. @pytest.mark.level0
  63. @pytest.mark.platform_x86_gpu_training
  64. @pytest.mark.env_onecard
  65. def test_cast2():
  66. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float16))
  67. t0 = mstype.int32
  68. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float16))
  69. t1 = mstype.float64
  70. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  71. net = Net(t0, t1)
  72. output = net(x0, x1)
  73. type0 = output[0].asnumpy().dtype
  74. assert type0 == 'int32'
  75. type1 = output[1].asnumpy().dtype
  76. assert type1 == 'float64'
  77. @pytest.mark.level0
  78. @pytest.mark.platform_x86_gpu_training
  79. @pytest.mark.env_onecard
  80. def test_cast3():
  81. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int64))
  82. t0 = mstype.int32
  83. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  84. t1 = mstype.int32
  85. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  86. net = Net(t0, t1)
  87. output = net(x0, x1)
  88. type0 = output[0].asnumpy().dtype
  89. assert type0 == 'int32'
  90. type1 = output[1].asnumpy().dtype
  91. assert type1 == 'int32'
  92. @pytest.mark.level0
  93. @pytest.mark.platform_x86_gpu_training
  94. @pytest.mark.env_onecard
  95. def test_cast4():
  96. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  97. t0 = mstype.float16
  98. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  99. t1 = mstype.int8
  100. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  101. net = Net(t0, t1)
  102. output = net(x0, x1)
  103. type0 = output[0].asnumpy().dtype
  104. assert type0 == 'float16'
  105. type1 = output[1].asnumpy().dtype
  106. assert type1 == 'int8'
  107. @pytest.mark.level0
  108. @pytest.mark.platform_x86_gpu_training
  109. @pytest.mark.env_onecard
  110. def test_cast5():
  111. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  112. t0 = mstype.uint8
  113. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  114. t1 = mstype.bool_
  115. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  116. net = Net(t0, t1)
  117. output = net(x0, x1)
  118. type0 = output[0].asnumpy().dtype
  119. assert type0 == 'uint8'
  120. type1 = output[1].asnumpy().dtype
  121. assert type1 == 'bool'
  122. @pytest.mark.level0
  123. @pytest.mark.platform_x86_gpu_training
  124. @pytest.mark.env_onecard
  125. def test_cast6():
  126. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  127. t0 = mstype.float64
  128. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  129. t1 = mstype.float32
  130. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  131. net = Net(t0, t1)
  132. output = net(x0, x1)
  133. type0 = output[0].asnumpy().dtype
  134. assert type0 == 'float64'
  135. type1 = output[1].asnumpy().dtype
  136. assert type1 == 'float32'
  137. @pytest.mark.level0
  138. @pytest.mark.platform_x86_gpu_training
  139. @pytest.mark.env_onecard
  140. def test_cast7():
  141. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  142. t0 = mstype.float32
  143. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  144. t1 = mstype.float16
  145. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  146. net = Net(t0, t1)
  147. output = net(x0, x1)
  148. type0 = output[0].asnumpy().dtype
  149. assert type0 == 'float32'
  150. type1 = output[1].asnumpy().dtype
  151. assert type1 == 'float16'
  152. @pytest.mark.level0
  153. @pytest.mark.platform_x86_gpu_training
  154. @pytest.mark.env_onecard
  155. def test_cast8():
  156. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  157. t0 = mstype.int32
  158. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  159. t1 = mstype.int16
  160. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  161. net = Net(t0, t1)
  162. output = net(x0, x1)
  163. type0 = output[0].asnumpy().dtype
  164. assert type0 == 'int32'
  165. type1 = output[1].asnumpy().dtype
  166. assert type1 == 'int16'
  167. @pytest.mark.level0
  168. @pytest.mark.platform_x86_gpu_training
  169. @pytest.mark.env_onecard
  170. def test_cast9():
  171. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  172. t0 = mstype.int64
  173. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  174. t1 = mstype.float16
  175. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  176. net = Net(t0, t1)
  177. output = net(x0, x1)
  178. type0 = output[0].asnumpy().dtype
  179. assert type0 == 'int64'
  180. type1 = output[1].asnumpy().dtype
  181. assert type1 == 'float16'
  182. @pytest.mark.level0
  183. @pytest.mark.platform_x86_gpu_training
  184. @pytest.mark.env_onecard
  185. def test_cast10():
  186. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  187. t0 = mstype.int8
  188. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  189. t1 = mstype.float64
  190. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  191. net = Net(t0, t1)
  192. output = net(x0, x1)
  193. type0 = output[0].asnumpy().dtype
  194. assert type0 == 'int8'
  195. type1 = output[1].asnumpy().dtype
  196. assert type1 == 'float64'
  197. @pytest.mark.level0
  198. @pytest.mark.platform_x86_gpu_training
  199. @pytest.mark.env_onecard
  200. def test_cast11():
  201. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  202. t0 = mstype.int16
  203. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  204. t1 = mstype.int32
  205. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  206. net = Net(t0, t1)
  207. output = net(x0, x1)
  208. type0 = output[0].asnumpy().dtype
  209. assert type0 == 'int16'
  210. type1 = output[1].asnumpy().dtype
  211. assert type1 == 'int32'
  212. @pytest.mark.level0
  213. @pytest.mark.platform_x86_gpu_training
  214. @pytest.mark.env_onecard
  215. def test_cast12():
  216. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  217. t0 = mstype.int64
  218. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.uint8))
  219. t1 = mstype.float32
  220. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  221. net = Net(t0, t1)
  222. output = net(x0, x1)
  223. type0 = output[0].asnumpy().dtype
  224. assert type0 == 'int64'
  225. type1 = output[1].asnumpy().dtype
  226. assert type1 == 'float32'
  227. @pytest.mark.level0
  228. @pytest.mark.platform_x86_gpu_training
  229. @pytest.mark.env_onecard
  230. def test_cast13():
  231. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.uint8))
  232. t0 = mstype.int32
  233. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.uint8))
  234. t1 = mstype.float16
  235. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  236. net = Net(t0, t1)
  237. output = net(x0, x1)
  238. type0 = output[0].asnumpy().dtype
  239. assert type0 == 'int32'
  240. type1 = output[1].asnumpy().dtype
  241. assert type1 == 'float16'
  242. @pytest.mark.level0
  243. @pytest.mark.platform_x86_gpu_training
  244. @pytest.mark.env_onecard
  245. def test_cast14():
  246. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  247. t0 = mstype.float64
  248. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  249. t1 = mstype.float32
  250. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  251. net = Net(t0, t1)
  252. output = net(x0, x1)
  253. type0 = output[0].asnumpy().dtype
  254. assert type0 == 'float64'
  255. type1 = output[1].asnumpy().dtype
  256. assert type1 == 'float32'
  257. @pytest.mark.level0
  258. @pytest.mark.platform_x86_gpu_training
  259. @pytest.mark.env_onecard
  260. def test_cast15():
  261. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  262. t0 = mstype.float16
  263. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  264. t1 = mstype.int32
  265. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  266. net = Net(t0, t1)
  267. output = net(x0, x1)
  268. type0 = output[0].asnumpy().dtype
  269. assert type0 == 'float16'
  270. type1 = output[1].asnumpy().dtype
  271. assert type1 == 'int32'
  272. @pytest.mark.level0
  273. @pytest.mark.platform_x86_gpu_training
  274. @pytest.mark.env_onecard
  275. def test_cast16():
  276. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  277. t0 = mstype.float16
  278. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int64))
  279. t1 = mstype.float64
  280. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  281. net = Net(t0, t1)
  282. output = net(x0, x1)
  283. type0 = output[0].asnumpy().dtype
  284. assert type0 == 'float16'
  285. type1 = output[1].asnumpy().dtype
  286. assert type1 == 'float64'
  287. @pytest.mark.level0
  288. @pytest.mark.platform_x86_gpu_training
  289. @pytest.mark.env_onecard
  290. def test_cast17():
  291. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  292. t0 = mstype.float32
  293. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  294. t1 = mstype.float16
  295. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  296. net = Net(t0, t1)
  297. output = net(x0, x1)
  298. type0 = output[0].asnumpy().dtype
  299. assert type0 == 'float32'
  300. type1 = output[1].asnumpy().dtype
  301. assert type1 == 'float16'
  302. @pytest.mark.level0
  303. @pytest.mark.platform_x86_gpu_training
  304. @pytest.mark.env_onecard
  305. def test_cast18():
  306. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int64))
  307. t0 = mstype.float32
  308. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int64))
  309. t1 = mstype.float16
  310. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  311. net = Net(t0, t1)
  312. output = net(x0, x1)
  313. type0 = output[0].asnumpy().dtype
  314. assert type0 == 'float32'
  315. type1 = output[1].asnumpy().dtype
  316. assert type1 == 'float16'