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

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