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

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