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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.asnumpy() == ret_expect.asnumpy()).all()
  75. def test_float_tensor_and_str_add():
  76. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  77. y = "ok"
  78. with pytest.raises(TypeError) as er:
  79. ret = x + y
  80. assert "For 'TensorAdd', the 1th input is a not support type: str" in str(er.value)
  81. def test_float_tensor_and_tuple_add():
  82. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  83. y = (1, 2, 3)
  84. with pytest.raises(TypeError) as er:
  85. ret = x + y
  86. assert "For 'TensorAdd', the 1th input is a not support type: tuple" in str(er.value)
  87. def test_float_tensor_and_list_add():
  88. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  89. y = [1, 2, 3]
  90. with pytest.raises(TypeError) as er:
  91. ret = x + y
  92. assert "For 'TensorAdd', the 1th input is a not support type: list" in str(er.value)
  93. def test_float_tensor_and_bool_tensors_add_grad():
  94. class Net(nn.Cell):
  95. def __init__(self):
  96. super(Net, self).__init__()
  97. def construct(self, x, y):
  98. return x + y
  99. class GradNet(nn.Cell):
  100. def __init__(self, net):
  101. super(GradNet, self).__init__()
  102. self.net = net
  103. def construct(self, x, y, sens):
  104. return C.grad_all_with_sens(self.net)(x, y, sens)
  105. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  106. y = Tensor(np.array([[True, True, True], [False, False, False]], dtype=np.bool_))
  107. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  108. net = Net()
  109. grad_net = GradNet(net)
  110. ret = grad_net(x, y, sens)
  111. assert ret[0].dtype == x.dtype
  112. assert ret[1].dtype == y.dtype
  113. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  114. assert (ret[1].asnumpy() == sens.asnumpy().astype(np.bool_)).all()
  115. def test_float_tensor_and_int_tensors_sub_grad():
  116. class Net(nn.Cell):
  117. def __init__(self):
  118. super(Net, self).__init__()
  119. def construct(self, x, y):
  120. return x - y
  121. class GradNet(nn.Cell):
  122. def __init__(self, net):
  123. super(GradNet, self).__init__()
  124. self.net = net
  125. def construct(self, x, y, sens):
  126. return C.grad_all_with_sens(self.net)(x, y, sens)
  127. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  128. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  129. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  130. net = Net()
  131. grad_net = GradNet(net)
  132. ret = grad_net(x, y, sens)
  133. print(ret)
  134. assert ret[0].dtype == x.dtype
  135. assert ret[1].dtype == y.dtype
  136. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  137. assert (ret[1].asnumpy() == sens.asnumpy() * -1).all()
  138. def test_float16_tensor_and_float32_tensors_sub_grad():
  139. class Net(nn.Cell):
  140. def __init__(self):
  141. super(Net, self).__init__()
  142. def construct(self, x, y):
  143. return x - y
  144. class GradNet(nn.Cell):
  145. def __init__(self, net):
  146. super(GradNet, self).__init__()
  147. self.net = net
  148. def construct(self, x, y, sens):
  149. return C.grad_all_with_sens(self.net)(x, y, sens)
  150. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.int32))
  151. y = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32))
  152. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  153. net = Net()
  154. grad_net = GradNet(net)
  155. ret = grad_net(x, y, sens)
  156. print(ret)
  157. assert ret[0].dtype == x.dtype
  158. assert ret[1].dtype == y.dtype
  159. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  160. assert (ret[1].asnumpy() == sens.asnumpy() * -1).all()
  161. def test_float_tensor_and_int_add_grad():
  162. class Net(nn.Cell):
  163. def __init__(self):
  164. super(Net, self).__init__()
  165. def construct(self, x):
  166. return x + 2
  167. class GradNet(nn.Cell):
  168. def __init__(self, net):
  169. super(GradNet, self).__init__()
  170. self.net = net
  171. def construct(self, x, sens):
  172. return C.grad_all_with_sens(self.net)(x, sens)
  173. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  174. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  175. net = Net()
  176. grad_net = GradNet(net)
  177. ret = grad_net(x, sens)
  178. assert ret[0].dtype == x.dtype
  179. assert (ret[0].asnumpy() == sens.asnumpy()).all()