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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. from mindspore import Tensor, nn
  18. from mindspore.ops import composite as C
  19. def test_float_tensor_and_int_add():
  20. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  21. y = 2
  22. ret_actual = x + y
  23. ret_expect = Tensor(np.array([[2.1, 2.2, 2.3], [2.4, 2.5, 2.6]], dtype=np.float32))
  24. assert ret_actual.dtype == ret_expect.dtype
  25. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  26. def test_bool_tensor_and_float_add():
  27. x = Tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
  28. y = 3.3
  29. ret_actual = x + y
  30. ret_expect = Tensor(np.array([[4.3, 3.3], [3.3, 4.3]], dtype=np.float32))
  31. assert ret_actual.dtype == ret_expect.dtype
  32. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  33. def test_bool_tensor_and_int_add():
  34. x = Tensor(np.array([[True, False], [False, True]], dtype=np.bool_))
  35. y = 3
  36. ret_actual = x + y
  37. ret_expect = Tensor(np.array([[4, 3], [3, 4]], dtype=np.int32))
  38. assert ret_actual.dtype == ret_expect.dtype
  39. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  40. def test_bool_and_int_tensor_add():
  41. x = True
  42. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  43. ret_actual = x + y
  44. ret_expect = Tensor(np.array([[2, 3, 4], [5, 6, 7]], dtype=np.int32))
  45. assert ret_actual.dtype == ret_expect.dtype
  46. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  47. def test_float_tensor_and_int_tensor_add():
  48. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  49. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  50. ret_actual = x + y
  51. ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float32))
  52. assert ret_actual.dtype == ret_expect.dtype
  53. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  54. def test_float_tensor_and_float_tensor_add():
  55. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  56. y = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float16))
  57. ret_actual = x + y
  58. ret_expect = Tensor(np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]], dtype=np.float32))
  59. assert ret_actual.dtype == ret_expect.dtype
  60. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  61. def test_int_tensor_and_int_tensor_add():
  62. x = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int8))
  63. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  64. ret_actual = x + y
  65. ret_expect = Tensor(np.array([[2, 4, 6], [8, 10, 12]], dtype=np.int32))
  66. assert ret_actual.dtype == ret_expect.dtype
  67. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  68. def test_float_tensor_and_bool_tensors_add():
  69. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  70. y = Tensor(np.array([[True, True, True], [False, False, False]], dtype=np.bool_))
  71. ret_actual = x + y
  72. ret_expect = Tensor(np.array([[1.1, 1.2, 1.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  73. assert (ret_actual.asnumpy() == ret_expect.asnumpy()).all()
  74. def test_float_tensor_and_bool_tensors_add_grad():
  75. class Net(nn.Cell):
  76. def __init__(self):
  77. super(Net, self).__init__()
  78. def construct(self, x, y):
  79. return x + y
  80. class GradNet(nn.Cell):
  81. def __init__(self, net):
  82. super(GradNet, self).__init__()
  83. self.net = net
  84. def construct(self, x, y, sens):
  85. return C.grad_all_with_sens(self.net)(x, y, sens)
  86. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  87. y = Tensor(np.array([[True, True, True], [False, False, False]], dtype=np.bool_))
  88. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  89. net = Net()
  90. grad_net = GradNet(net)
  91. ret = grad_net(x, y, sens)
  92. assert ret[0].dtype == x.dtype
  93. assert ret[1].dtype == y.dtype
  94. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  95. assert (ret[1].asnumpy() == sens.asnumpy().astype(np.bool_)).all()
  96. def test_float_tensor_and_int_tensors_sub_grad():
  97. class Net(nn.Cell):
  98. def __init__(self):
  99. super(Net, self).__init__()
  100. def construct(self, x, y):
  101. return x - y
  102. class GradNet(nn.Cell):
  103. def __init__(self, net):
  104. super(GradNet, self).__init__()
  105. self.net = net
  106. def construct(self, x, y, sens):
  107. return C.grad_all_with_sens(self.net)(x, y, sens)
  108. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  109. y = Tensor(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32))
  110. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  111. net = Net()
  112. grad_net = GradNet(net)
  113. ret = grad_net(x, y, sens)
  114. print(ret)
  115. assert ret[0].dtype == x.dtype
  116. assert ret[1].dtype == y.dtype
  117. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  118. assert (ret[1].asnumpy() == sens.asnumpy() * -1).all()
  119. def test_float16_tensor_and_float32_tensors_sub_grad():
  120. class Net(nn.Cell):
  121. def __init__(self):
  122. super(Net, self).__init__()
  123. def construct(self, x, y):
  124. return x - y
  125. class GradNet(nn.Cell):
  126. def __init__(self, net):
  127. super(GradNet, self).__init__()
  128. self.net = net
  129. def construct(self, x, y, sens):
  130. return C.grad_all_with_sens(self.net)(x, y, sens)
  131. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.int32))
  132. y = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32))
  133. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  134. net = Net()
  135. grad_net = GradNet(net)
  136. ret = grad_net(x, y, sens)
  137. print(ret)
  138. assert ret[0].dtype == x.dtype
  139. assert ret[1].dtype == y.dtype
  140. assert (ret[0].asnumpy() == sens.asnumpy()).all()
  141. assert (ret[1].asnumpy() == sens.asnumpy() * -1).all()
  142. def test_float_tensor_and_int_add_grad():
  143. class Net(nn.Cell):
  144. def __init__(self):
  145. super(Net, self).__init__()
  146. def construct(self, x):
  147. return x + 2
  148. class GradNet(nn.Cell):
  149. def __init__(self, net):
  150. super(GradNet, self).__init__()
  151. self.net = net
  152. def construct(self, x, sens):
  153. return C.grad_all_with_sens(self.net)(x, sens)
  154. x = Tensor(np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float32))
  155. sens = Tensor(np.array([[1.0, 2.0, 0.0], [0.0, 3.0, 4.0]], dtype=np.float32))
  156. net = Net()
  157. grad_net = GradNet(net)
  158. ret = grad_net(x, sens)
  159. assert ret[0].dtype == x.dtype
  160. assert (ret[0].asnumpy() == sens.asnumpy()).all()