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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. from mindspore.ops.operations import _inner_ops as inner
  23. class Net(Cell):
  24. def __init__(self, type0, type1):
  25. super(Net, self).__init__()
  26. self.Cast = P.Cast()
  27. self.type0 = type0
  28. self.type1 = type1
  29. def construct(self, x0, x1):
  30. output = (self.Cast(x0, self.type0),
  31. self.Cast(x1, self.type1))
  32. return output
  33. class NetDynamic(Cell):
  34. def __init__(self, type0, type1):
  35. super(NetDynamic, self).__init__()
  36. self.conv = inner.GpuConvertToDynamicShape()
  37. self.Cast = P.Cast()
  38. self.type0 = type0
  39. self.type1 = type1
  40. def construct(self, x0, x1):
  41. x0_conv = self.conv(x0)
  42. x1_conv = self.conv(x1)
  43. output = (self.Cast(x0_conv, self.type0),
  44. self.Cast(x1_conv, self.type1))
  45. return output
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_gpu_training
  48. @pytest.mark.env_onecard
  49. def test_cast():
  50. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  51. t0 = mstype.float16
  52. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float16))
  53. t1 = mstype.float32
  54. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  55. net = Net(t0, t1)
  56. output = net(x0, x1)
  57. type0 = output[0].asnumpy().dtype
  58. assert type0 == 'float16'
  59. type1 = output[1].asnumpy().dtype
  60. assert type1 == 'float32'
  61. @pytest.mark.level0
  62. @pytest.mark.platform_x86_gpu_training
  63. @pytest.mark.env_onecard
  64. def test_cast1():
  65. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  66. t0 = mstype.float32
  67. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  68. t1 = mstype.float32
  69. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  70. net = Net(t0, t1)
  71. output = net(x0, x1)
  72. type0 = output[0].asnumpy().dtype
  73. assert type0 == 'float32'
  74. type1 = output[1].asnumpy().dtype
  75. assert type1 == 'float32'
  76. @pytest.mark.level0
  77. @pytest.mark.platform_x86_gpu_training
  78. @pytest.mark.env_onecard
  79. def test_cast2():
  80. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float16))
  81. t0 = mstype.int32
  82. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float16))
  83. t1 = mstype.float64
  84. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  85. net = Net(t0, t1)
  86. output = net(x0, x1)
  87. type0 = output[0].asnumpy().dtype
  88. assert type0 == 'int32'
  89. type1 = output[1].asnumpy().dtype
  90. assert type1 == 'float64'
  91. @pytest.mark.level0
  92. @pytest.mark.platform_x86_gpu_training
  93. @pytest.mark.env_onecard
  94. def test_cast3():
  95. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int64))
  96. t0 = mstype.int32
  97. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  98. t1 = mstype.int32
  99. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  100. net = Net(t0, t1)
  101. output = net(x0, x1)
  102. type0 = output[0].asnumpy().dtype
  103. assert type0 == 'int32'
  104. type1 = output[1].asnumpy().dtype
  105. assert type1 == 'int32'
  106. @pytest.mark.level0
  107. @pytest.mark.platform_x86_gpu_training
  108. @pytest.mark.env_onecard
  109. def test_cast4():
  110. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  111. t0 = mstype.float16
  112. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  113. t1 = mstype.int8
  114. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  115. net = Net(t0, t1)
  116. output = net(x0, x1)
  117. type0 = output[0].asnumpy().dtype
  118. assert type0 == 'float16'
  119. type1 = output[1].asnumpy().dtype
  120. assert type1 == 'int8'
  121. @pytest.mark.level0
  122. @pytest.mark.platform_x86_gpu_training
  123. @pytest.mark.env_onecard
  124. def test_cast5():
  125. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  126. t0 = mstype.uint8
  127. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  128. t1 = mstype.bool_
  129. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  130. net = Net(t0, t1)
  131. output = net(x0, x1)
  132. type0 = output[0].asnumpy().dtype
  133. assert type0 == 'uint8'
  134. type1 = output[1].asnumpy().dtype
  135. assert type1 == 'bool'
  136. @pytest.mark.level0
  137. @pytest.mark.platform_x86_gpu_training
  138. @pytest.mark.env_onecard
  139. def test_cast6():
  140. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  141. t0 = mstype.float64
  142. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  143. t1 = mstype.float32
  144. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  145. net = Net(t0, t1)
  146. output = net(x0, x1)
  147. type0 = output[0].asnumpy().dtype
  148. assert type0 == 'float64'
  149. type1 = output[1].asnumpy().dtype
  150. assert type1 == 'float32'
  151. @pytest.mark.level0
  152. @pytest.mark.platform_x86_gpu_training
  153. @pytest.mark.env_onecard
  154. def test_cast7():
  155. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  156. t0 = mstype.float32
  157. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  158. t1 = mstype.float16
  159. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  160. net = Net(t0, t1)
  161. output = net(x0, x1)
  162. type0 = output[0].asnumpy().dtype
  163. assert type0 == 'float32'
  164. type1 = output[1].asnumpy().dtype
  165. assert type1 == 'float16'
  166. @pytest.mark.level0
  167. @pytest.mark.platform_x86_gpu_training
  168. @pytest.mark.env_onecard
  169. def test_cast8():
  170. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  171. t0 = mstype.int32
  172. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  173. t1 = mstype.int16
  174. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  175. net = Net(t0, t1)
  176. output = net(x0, x1)
  177. type0 = output[0].asnumpy().dtype
  178. assert type0 == 'int32'
  179. type1 = output[1].asnumpy().dtype
  180. assert type1 == 'int16'
  181. @pytest.mark.level0
  182. @pytest.mark.platform_x86_gpu_training
  183. @pytest.mark.env_onecard
  184. def test_cast9():
  185. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  186. t0 = mstype.int64
  187. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  188. t1 = mstype.float16
  189. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  190. net = Net(t0, t1)
  191. output = net(x0, x1)
  192. type0 = output[0].asnumpy().dtype
  193. assert type0 == 'int64'
  194. type1 = output[1].asnumpy().dtype
  195. assert type1 == 'float16'
  196. @pytest.mark.level0
  197. @pytest.mark.platform_x86_gpu_training
  198. @pytest.mark.env_onecard
  199. def test_cast10():
  200. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  201. t0 = mstype.int8
  202. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  203. t1 = mstype.float64
  204. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  205. net = Net(t0, t1)
  206. output = net(x0, x1)
  207. type0 = output[0].asnumpy().dtype
  208. assert type0 == 'int8'
  209. type1 = output[1].asnumpy().dtype
  210. assert type1 == 'float64'
  211. @pytest.mark.level0
  212. @pytest.mark.platform_x86_gpu_training
  213. @pytest.mark.env_onecard
  214. def test_cast11():
  215. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  216. t0 = mstype.int16
  217. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  218. t1 = mstype.int32
  219. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  220. net = Net(t0, t1)
  221. output = net(x0, x1)
  222. type0 = output[0].asnumpy().dtype
  223. assert type0 == 'int16'
  224. type1 = output[1].asnumpy().dtype
  225. assert type1 == 'int32'
  226. @pytest.mark.level0
  227. @pytest.mark.platform_x86_gpu_training
  228. @pytest.mark.env_onecard
  229. def test_cast12():
  230. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.bool))
  231. t0 = mstype.int64
  232. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.uint8))
  233. t1 = mstype.float32
  234. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  235. net = Net(t0, t1)
  236. output = net(x0, x1)
  237. type0 = output[0].asnumpy().dtype
  238. assert type0 == 'int64'
  239. type1 = output[1].asnumpy().dtype
  240. assert type1 == 'float32'
  241. @pytest.mark.level0
  242. @pytest.mark.platform_x86_gpu_training
  243. @pytest.mark.env_onecard
  244. def test_cast13():
  245. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.uint8))
  246. t0 = mstype.int32
  247. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.uint8))
  248. t1 = mstype.float16
  249. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  250. net = Net(t0, t1)
  251. output = net(x0, x1)
  252. type0 = output[0].asnumpy().dtype
  253. assert type0 == 'int32'
  254. type1 = output[1].asnumpy().dtype
  255. assert type1 == 'float16'
  256. @pytest.mark.level0
  257. @pytest.mark.platform_x86_gpu_training
  258. @pytest.mark.env_onecard
  259. def test_cast14():
  260. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  261. t0 = mstype.float64
  262. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  263. t1 = mstype.float32
  264. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  265. net = Net(t0, t1)
  266. output = net(x0, x1)
  267. type0 = output[0].asnumpy().dtype
  268. assert type0 == 'float64'
  269. type1 = output[1].asnumpy().dtype
  270. assert type1 == 'float32'
  271. @pytest.mark.level0
  272. @pytest.mark.platform_x86_gpu_training
  273. @pytest.mark.env_onecard
  274. def test_cast15():
  275. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  276. t0 = mstype.float16
  277. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  278. t1 = mstype.int32
  279. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  280. net = Net(t0, t1)
  281. output = net(x0, x1)
  282. type0 = output[0].asnumpy().dtype
  283. assert type0 == 'float16'
  284. type1 = output[1].asnumpy().dtype
  285. assert type1 == 'int32'
  286. @pytest.mark.level0
  287. @pytest.mark.platform_x86_gpu_training
  288. @pytest.mark.env_onecard
  289. def test_cast16():
  290. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  291. t0 = mstype.float16
  292. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int64))
  293. t1 = mstype.float64
  294. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  295. net = Net(t0, t1)
  296. output = net(x0, x1)
  297. type0 = output[0].asnumpy().dtype
  298. assert type0 == 'float16'
  299. type1 = output[1].asnumpy().dtype
  300. assert type1 == 'float64'
  301. @pytest.mark.level0
  302. @pytest.mark.platform_x86_gpu_training
  303. @pytest.mark.env_onecard
  304. def test_cast17():
  305. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  306. t0 = mstype.float32
  307. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  308. t1 = mstype.float16
  309. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  310. net = Net(t0, t1)
  311. output = net(x0, x1)
  312. type0 = output[0].asnumpy().dtype
  313. assert type0 == 'float32'
  314. type1 = output[1].asnumpy().dtype
  315. assert type1 == 'float16'
  316. @pytest.mark.level0
  317. @pytest.mark.platform_x86_gpu_training
  318. @pytest.mark.env_onecard
  319. def test_cast18():
  320. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int64))
  321. t0 = mstype.float32
  322. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int64))
  323. t1 = mstype.float16
  324. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  325. net = Net(t0, t1)
  326. output = net(x0, x1)
  327. type0 = output[0].asnumpy().dtype
  328. assert type0 == 'float32'
  329. type1 = output[1].asnumpy().dtype
  330. assert type1 == 'float16'
  331. @pytest.mark.level0
  332. @pytest.mark.platform_x86_gpu_training
  333. @pytest.mark.env_onecard
  334. def test_cast19():
  335. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int8))
  336. t0 = mstype.bool_
  337. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int16))
  338. t1 = mstype.bool_
  339. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  340. net = Net(t0, t1)
  341. output = net(x0, x1)
  342. type0 = output[0].asnumpy().dtype
  343. assert type0 == 'bool'
  344. type1 = output[1].asnumpy().dtype
  345. assert type1 == 'bool'
  346. @pytest.mark.level0
  347. @pytest.mark.platform_x86_gpu_training
  348. @pytest.mark.env_onecard
  349. def test_cast20():
  350. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int64))
  351. t0 = mstype.bool_
  352. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float16))
  353. t1 = mstype.bool_
  354. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  355. net = Net(t0, t1)
  356. output = net(x0, x1)
  357. type0 = output[0].asnumpy().dtype
  358. assert type0 == 'bool'
  359. type1 = output[1].asnumpy().dtype
  360. assert type1 == 'bool'
  361. @pytest.mark.level0
  362. @pytest.mark.platform_x86_gpu_training
  363. @pytest.mark.env_onecard
  364. def test_cast21():
  365. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  366. t0 = mstype.bool_
  367. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float64))
  368. t1 = mstype.bool_
  369. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  370. net = Net(t0, t1)
  371. output = net(x0, x1)
  372. type0 = output[0].asnumpy().dtype
  373. assert type0 == 'bool'
  374. type1 = output[1].asnumpy().dtype
  375. assert type1 == 'bool'
  376. @pytest.mark.level0
  377. @pytest.mark.platform_x86_gpu_training
  378. @pytest.mark.env_onecard
  379. def test_cast22():
  380. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.uint8))
  381. t0 = mstype.bool_
  382. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  383. t1 = mstype.bool_
  384. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  385. net = Net(t0, t1)
  386. output = net(x0, x1)
  387. type0 = output[0].asnumpy().dtype
  388. assert type0 == 'bool'
  389. type1 = output[1].asnumpy().dtype
  390. assert type1 == 'bool'
  391. @pytest.mark.level0
  392. @pytest.mark.platform_x86_gpu_training
  393. @pytest.mark.env_onecard
  394. def test_cast23():
  395. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float64))
  396. t0 = mstype.float32
  397. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float64))
  398. t1 = mstype.float16
  399. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  400. net = Net(t0, t1)
  401. output = net(x0, x1)
  402. type0 = output[0].asnumpy().dtype
  403. assert type0 == 'float32'
  404. type1 = output[1].asnumpy().dtype
  405. assert type1 == 'float16'
  406. @pytest.mark.level0
  407. @pytest.mark.platform_x86_gpu_training
  408. @pytest.mark.env_onecard
  409. def test_cast24():
  410. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float64))
  411. t0 = mstype.int64
  412. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float64))
  413. t1 = mstype.int32
  414. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  415. net = Net(t0, t1)
  416. output = net(x0, x1)
  417. type0 = output[0].asnumpy().dtype
  418. assert type0 == 'int64'
  419. type1 = output[1].asnumpy().dtype
  420. assert type1 == 'int32'
  421. @pytest.mark.level0
  422. @pytest.mark.platform_x86_gpu_training
  423. @pytest.mark.env_onecard
  424. def test_cast25():
  425. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float64))
  426. t0 = mstype.int16
  427. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float64))
  428. t1 = mstype.int8
  429. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  430. net = Net(t0, t1)
  431. output = net(x0, x1)
  432. type0 = output[0].asnumpy().dtype
  433. assert type0 == 'int16'
  434. type1 = output[1].asnumpy().dtype
  435. assert type1 == 'int8'
  436. @pytest.mark.level0
  437. @pytest.mark.platform_x86_gpu_training
  438. @pytest.mark.env_onecard
  439. def test_cast26():
  440. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  441. t0 = mstype.int64
  442. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.int32))
  443. t1 = mstype.float64
  444. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  445. net = Net(t0, t1)
  446. output = net(x0, x1)
  447. type0 = output[0].asnumpy().dtype
  448. assert type0 == 'int64'
  449. type1 = output[1].asnumpy().dtype
  450. assert type1 == 'float64'
  451. @pytest.mark.level0
  452. @pytest.mark.platform_x86_gpu_training
  453. @pytest.mark.env_onecard
  454. def test_cast27():
  455. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  456. t0 = mstype.float64
  457. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  458. t1 = mstype.uint64
  459. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  460. net = Net(t0, t1)
  461. output = net(x0, x1)
  462. type0 = output[0].asnumpy().dtype
  463. assert type0 == 'float64'
  464. type1 = output[1].asnumpy().dtype
  465. assert type1 == 'uint64'
  466. @pytest.mark.level0
  467. @pytest.mark.platform_x86_gpu_training
  468. @pytest.mark.env_onecard
  469. def test_cast28():
  470. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  471. t0 = mstype.int8
  472. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  473. t1 = mstype.int16
  474. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  475. net = Net(t0, t1)
  476. output = net(x0, x1)
  477. type0 = output[0].asnumpy().dtype
  478. assert type0 == 'int8'
  479. type1 = output[1].asnumpy().dtype
  480. assert type1 == 'int16'
  481. @pytest.mark.level0
  482. @pytest.mark.platform_x86_gpu_training
  483. @pytest.mark.env_onecard
  484. def test_cast29():
  485. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  486. t0 = mstype.int64
  487. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  488. t1 = mstype.uint8
  489. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  490. net = Net(t0, t1)
  491. output = net(x0, x1)
  492. type0 = output[0].asnumpy().dtype
  493. assert type0 == 'int64'
  494. type1 = output[1].asnumpy().dtype
  495. assert type1 == 'uint8'
  496. @pytest.mark.level0
  497. @pytest.mark.platform_x86_gpu_training
  498. @pytest.mark.env_onecard
  499. def test_cast30():
  500. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  501. t0 = mstype.uint16
  502. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  503. t1 = mstype.uint32
  504. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  505. net = Net(t0, t1)
  506. output = net(x0, x1)
  507. type0 = output[0].asnumpy().dtype
  508. assert type0 == 'uint16'
  509. type1 = output[1].asnumpy().dtype
  510. assert type1 == 'uint32'
  511. @pytest.mark.level0
  512. @pytest.mark.platform_x86_gpu_training
  513. @pytest.mark.env_onecard
  514. def test_cast31():
  515. x0 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  516. t0 = mstype.uint16
  517. x1 = Tensor(np.arange(24).reshape((4, 3, 2)).astype(np.float32))
  518. t1 = mstype.uint32
  519. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  520. net = NetDynamic(t0, t1)
  521. output = net(x0, x1)
  522. type0 = output[0].asnumpy().dtype
  523. assert type0 == 'uint16'
  524. type1 = output[1].asnumpy().dtype
  525. assert type1 == 'uint32'
  526. @pytest.mark.level0
  527. @pytest.mark.platform_x86_gpu_training
  528. @pytest.mark.env_onecard
  529. def test_cast32():
  530. np.random.seed(10)
  531. x = np.random.uniform(-5, 5, (3, 2)).astype(np.float16)
  532. x0 = Tensor(x)
  533. t0 = mstype.int32
  534. x1 = Tensor(x)
  535. t1 = mstype.float64
  536. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  537. net = Net(t0, t1)
  538. output = net(x0, x1)
  539. type0 = output[0].asnumpy().dtype
  540. assert type0 == 'int32'
  541. expected = x.astype(np.int32)
  542. assert (output[0].asnumpy() == expected).all()
  543. type1 = output[1].asnumpy().dtype
  544. assert type1 == 'float64'