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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.int32))
  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 'TensorAdd', the 1th input is a not support implicit conversion type: str" 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. with pytest.raises(TypeError) as er:
  96. ret = x + y
  97. assert "For 'TensorAdd', the 1th input is a not support implicit conversion type: tuple" in str(er.value)
  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. with pytest.raises(TypeError) as er:
  102. ret = x + y
  103. assert "For 'TensorAdd', the 1th input is a not support implicit conversion type: list" in str(er.value)
  104. def test_float_tensor_and_bool_tensors_add_grad():
  105. class Net(nn.Cell):
  106. def __init__(self):
  107. super(Net, self).__init__()
  108. def construct(self, x, y):
  109. return x + y
  110. class GradNet(nn.Cell):
  111. def __init__(self, net):
  112. super(GradNet, self).__init__()
  113. self.net = net
  114. def construct(self, x, y, sens):
  115. return grad_all_with_sens(self.net)(x, y, sens)
  116. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  117. y = Tensor(np.array([[True, True, True], [False, False, False]], dtype=np.bool_))
  118. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  119. net = Net()
  120. grad_net = GradNet(net)
  121. ret = grad_net(x, y, sens)
  122. assert ret[0].dtype == x.dtype
  123. assert ret[1].dtype == y.dtype
  124. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  125. assert (ret[1].asnumpy() == sens.asnumpy().astype(np.bool_)).all()
  126. def test_float_tensor_and_int_tensors_sub_grad():
  127. class Net(nn.Cell):
  128. def __init__(self):
  129. super(Net, self).__init__()
  130. def construct(self, x, y):
  131. return x - y
  132. class GradNet(nn.Cell):
  133. def __init__(self, net):
  134. super(GradNet, self).__init__()
  135. self.net = net
  136. def construct(self, x, y, sens):
  137. return grad_all_with_sens(self.net)(x, y, sens)
  138. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  139. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  140. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  141. net = Net()
  142. grad_net = GradNet(net)
  143. ret = grad_net(x, y, sens)
  144. assert ret[0].dtype == x.dtype
  145. assert ret[1].dtype == y.dtype
  146. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  147. assert (ret[1].asnumpy() == sens.asnumpy() * -1).all()
  148. def test_float16_tensor_and_float32_tensors_sub_grad():
  149. class Net(nn.Cell):
  150. def __init__(self):
  151. super(Net, self).__init__()
  152. def construct(self, x, y):
  153. return x - y
  154. class GradNet(nn.Cell):
  155. def __init__(self, net):
  156. super(GradNet, self).__init__()
  157. self.net = net
  158. def construct(self, x, y, sens):
  159. return grad_all_with_sens(self.net)(x, y, sens)
  160. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.int32))
  161. y = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32))
  162. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  163. net = Net()
  164. grad_net = GradNet(net)
  165. ret = grad_net(x, y, sens)
  166. assert ret[0].dtype == x.dtype
  167. assert ret[1].dtype == y.dtype
  168. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  169. assert (ret[1].asnumpy() == sens.asnumpy() * -1).all()
  170. def test_float_tensor_and_int_add_grad():
  171. class Net(nn.Cell):
  172. def __init__(self):
  173. super(Net, self).__init__()
  174. def construct(self, x):
  175. return x + 2
  176. class GradNet(nn.Cell):
  177. def __init__(self, net):
  178. super(GradNet, self).__init__()
  179. self.net = net
  180. def construct(self, x, sens):
  181. return grad_all_with_sens(self.net)(x, sens)
  182. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  183. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  184. net = Net()
  185. grad_net = GradNet(net)
  186. ret = grad_net(x, sens)
  187. assert ret[0].dtype == x.dtype
  188. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  189. def test_int8_tensor_and_uint8_tensors_add_grad():
  190. class Net(nn.Cell):
  191. def __init__(self):
  192. super(Net, self).__init__()
  193. def construct(self, x, y):
  194. return x + y
  195. class GradNet(nn.Cell):
  196. def __init__(self, net):
  197. super(GradNet, self).__init__()
  198. self.net = net
  199. def construct(self, x, y, sens):
  200. return grad_all_with_sens(self.net)(x, y, sens)
  201. x = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int8))
  202. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8))
  203. sens = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int16))
  204. net = Net()
  205. grad_net = GradNet(net)
  206. ret = grad_net(x, y, sens)
  207. assert ret[0].dtype == x.dtype
  208. assert ret[1].dtype == y.dtype
  209. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  210. assert (ret[1].asnumpy() == sens.asnumpy()).all()
  211. class AssignCheck(nn.Cell):
  212. """ NetWithNDarray definition """
  213. def __init__(self):
  214. super(AssignCheck, self).__init__()
  215. self.cov_step = Parameter(0.0, name="cov_step", requires_grad=False)
  216. def construct(self, x, y):
  217. F.assign(self.cov_step, y)
  218. F.assign(x, y)
  219. return x
  220. def test_assign_check_in_sig():
  221. net = AssignCheck()
  222. x = Tensor(2, ms.int8)
  223. y = Tensor(3, ms.uint8)
  224. with pytest.raises(TypeError) as e:
  225. net(x, y)
  226. assert "Parameter" in e.value.args[0]