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

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. """ test implicit conversion """
  16. import numpy as np
  17. import pytest
  18. import mindspore as ms
  19. from mindspore import Tensor, nn, Parameter
  20. from mindspore.ops import composite as C
  21. from mindspore.ops import functional as F
  22. grad_all_with_sens = C.GradOperation(get_all=True, sens_param=True)
  23. def test_float_tensor_and_int_add():
  24. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  25. y = 2
  26. ret_actual = x + y
  27. ret_expect = Tensor(np.array([[2.1, 2.2, 2.3], [2.4, 2.5, 2.6]], dtype=np.float32))
  28. assert ret_actual.dtype == ret_expect.dtype
  29. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  30. def test_bool_tensor_and_float_add():
  31. x = Tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
  32. y = 3.3
  33. ret_actual = x + y
  34. ret_expect = Tensor(np.array([[4.3, 3.3], [3.3, 4.3]], dtype=np.float32))
  35. assert ret_actual.dtype == ret_expect.dtype
  36. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  37. def test_bool_tensor_and_int_add():
  38. x = Tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
  39. y = 3
  40. ret_actual = x + y
  41. ret_expect = Tensor(np.array([[4, 3], [3, 4]], dtype=np.int64))
  42. assert ret_actual.dtype == ret_expect.dtype
  43. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  44. def test_bool_and_int_tensor_add():
  45. x = True
  46. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  47. ret_actual = x + y
  48. ret_expect = Tensor(np.array([[2, 3, 4], [5, 6, 7]], dtype=np.int32))
  49. assert ret_actual.dtype == ret_expect.dtype
  50. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  51. def test_float_tensor_and_int_tensor_add():
  52. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  53. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  54. ret_actual = x + y
  55. ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float32))
  56. assert ret_actual.dtype == ret_expect.dtype
  57. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  58. def test_float_tensor_and_float_tensor_add():
  59. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  60. y = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float16))
  61. ret_actual = x + y
  62. ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float32))
  63. assert ret_actual.dtype == ret_expect.dtype
  64. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  65. def test_int_tensor_and_int_tensor_add():
  66. x = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int8))
  67. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  68. ret_actual = x + y
  69. ret_expect = Tensor(np.array([[2, 4, 6], [8, 10, 12]], dtype=np.int32))
  70. assert ret_actual.dtype == ret_expect.dtype
  71. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  72. def test_float_tensor_and_bool_tensors_add():
  73. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  74. y = Tensor(np.array([[True, True, True], [False, False, False]], dtype=np.bool_))
  75. ret_actual = x + y
  76. ret_expect = Tensor(np.array([[1.1, 1.2, 1.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  77. assert ret_actual.dtype == ret_expect.dtype
  78. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  79. def test_int8_tensor_and_uint8_tensors_add():
  80. x = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int8))
  81. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8))
  82. ret_actual = x + y
  83. ret_expect = Tensor(np.array([[2, 4, 6], [8, 10, 12]], dtype=np.int16))
  84. assert ret_actual.dtype == ret_expect.dtype
  85. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  86. def test_float_tensor_and_str_add():
  87. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  88. y = "ok"
  89. with pytest.raises(TypeError) as er:
  90. ret = x + y
  91. assert "For 'Add', the 1th input var is a not support implicit conversion. Its type is" in str(er.value)
  92. def test_float_tensor_and_tuple_add():
  93. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  94. y = (1, 2, 3)
  95. ret_actual = x + y
  96. ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [1.4, 2.5, 3.6]], dtype=np.float32))
  97. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  98. def test_float_tensor_and_list_add():
  99. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  100. y = [1, 2, 3]
  101. ret_actual = x + y
  102. ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [1.4, 2.5, 3.6]], dtype=np.float32))
  103. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  104. def test_float_tensor_and_bool_tensors_add_grad():
  105. class Net(nn.Cell):
  106. def construct(self, x, y):
  107. return x + y
  108. class GradNet(nn.Cell):
  109. def __init__(self, net):
  110. super(GradNet, self).__init__()
  111. self.net = net
  112. def construct(self, x, y, sens):
  113. return grad_all_with_sens(self.net)(x, y, sens)
  114. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  115. y = Tensor(np.array([[True, True, True], [False, False, False]], dtype=np.bool_))
  116. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  117. net = Net()
  118. grad_net = GradNet(net)
  119. ret = grad_net(x, y, sens)
  120. assert ret[0].dtype == x.dtype
  121. assert ret[1].dtype == y.dtype
  122. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  123. assert (ret[1].asnumpy() == sens.asnumpy().astype(np.bool_)).all()
  124. def test_float_tensor_and_int_tensors_sub_grad():
  125. class Net(nn.Cell):
  126. def construct(self, x, y):
  127. return x - y
  128. class GradNet(nn.Cell):
  129. def __init__(self, net):
  130. super(GradNet, self).__init__()
  131. self.net = net
  132. def construct(self, x, y, sens):
  133. return grad_all_with_sens(self.net)(x, y, sens)
  134. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  135. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  136. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  137. net = Net()
  138. grad_net = GradNet(net)
  139. ret = grad_net(x, y, sens)
  140. assert ret[0].dtype == x.dtype
  141. assert ret[1].dtype == y.dtype
  142. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  143. assert (ret[1].asnumpy() == sens.asnumpy() * -1).all()
  144. def test_float16_tensor_and_float32_tensors_sub_grad():
  145. class Net(nn.Cell):
  146. def construct(self, x, y):
  147. return x - y
  148. class GradNet(nn.Cell):
  149. def __init__(self, net):
  150. super(GradNet, self).__init__()
  151. self.net = net
  152. def construct(self, x, y, sens):
  153. return grad_all_with_sens(self.net)(x, y, sens)
  154. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.int32))
  155. y = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32))
  156. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  157. net = Net()
  158. grad_net = GradNet(net)
  159. ret = grad_net(x, y, sens)
  160. assert ret[0].dtype == x.dtype
  161. assert ret[1].dtype == y.dtype
  162. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  163. assert (ret[1].asnumpy() == sens.asnumpy() * -1).all()
  164. def test_float_tensor_and_int_add_grad():
  165. class Net(nn.Cell):
  166. def construct(self, x):
  167. return x + 2
  168. class GradNet(nn.Cell):
  169. def __init__(self, net):
  170. super(GradNet, self).__init__()
  171. self.net = net
  172. def construct(self, x, sens):
  173. return grad_all_with_sens(self.net)(x, sens)
  174. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  175. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  176. net = Net()
  177. grad_net = GradNet(net)
  178. ret = grad_net(x, sens)
  179. assert ret[0].dtype == x.dtype
  180. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  181. def test_int8_tensor_and_uint8_tensors_add_grad():
  182. class Net(nn.Cell):
  183. def construct(self, x, y):
  184. return x + y
  185. class GradNet(nn.Cell):
  186. def __init__(self, net):
  187. super(GradNet, self).__init__()
  188. self.net = net
  189. def construct(self, x, y, sens):
  190. return grad_all_with_sens(self.net)(x, y, sens)
  191. x = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int8))
  192. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8))
  193. sens = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16))
  194. net = Net()
  195. grad_net = GradNet(net)
  196. ret = grad_net(x, y, sens)
  197. assert ret[0].dtype == x.dtype
  198. assert ret[1].dtype == y.dtype
  199. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  200. assert (ret[1].asnumpy() == sens.asnumpy()).all()
  201. class AssignCheck(nn.Cell):
  202. """ NetWithNDarray definition """
  203. def __init__(self):
  204. super(AssignCheck, self).__init__()
  205. self.cov_step = Parameter(0.0, name="cov_step", requires_grad=False)
  206. def construct(self, x, y):
  207. F.assign(self.cov_step, y)
  208. F.assign(x, y)
  209. return x
  210. def test_assign_check_in_sig():
  211. net = AssignCheck()
  212. x = Tensor(2, ms.int8)
  213. y = Tensor(3, ms.uint8)
  214. with pytest.raises(RuntimeError) as e:
  215. net(x, y)
  216. assert "Data type conversion of 'Parameter' is not supported" in e.value.args[0]