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_operator.py 7.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # Copyright 2021 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. import sys
  16. import pytest
  17. from mindspore import Tensor, context, Parameter
  18. from mindspore.ops import operations as P
  19. from mindspore.ops import functional as F
  20. from mindspore.nn import Cell
  21. import mindspore as ms
  22. def test_inner_scalar_divisor():
  23. """
  24. Feature: Check whether the divisor of inner scalar is zero.
  25. Description: The divisor of inner scalar must not be zero.
  26. Expectation: The divisor of inner scalar must not be zero.
  27. """
  28. class Net(Cell):
  29. def __init__(self):
  30. super().__init__()
  31. self.param_a = Parameter(Tensor(5, ms.int32), name="param_a")
  32. self.param_b = Parameter(Tensor(5, ms.int32), name="param_b")
  33. def construct(self, x):
  34. return x + self.param_a + 5 / 0
  35. context.set_context(device_target="GPU")
  36. x = Tensor(2, dtype=ms.int32)
  37. net = Net()
  38. with pytest.raises(Exception, match="The divisor could not be zero."):
  39. ret = net(x)
  40. print("ret:", ret)
  41. def test_inner_scalar_mod():
  42. """
  43. Feature: Check the input of inner scalar mod.
  44. Description: The input of inner scalar mod must not be zero.
  45. Expectation: The input of inner scalar mod must not be zero.
  46. """
  47. class Net(Cell):
  48. def __init__(self):
  49. super().__init__()
  50. self.param_a = Parameter(Tensor(5, ms.int32), name="param_a")
  51. def construct(self, x):
  52. return x + self.param_a + 5 % 0
  53. x = Tensor(2, dtype=ms.int32)
  54. net = Net()
  55. with pytest.raises(Exception, match="Could not mod to zero."):
  56. ret = net(x)
  57. print("ret:", ret)
  58. def test_inner_scalar_mod_args_length():
  59. """
  60. Feature: Check the length of input of inner scalar mod.
  61. Description: The length of input of inner scalar mod should not less than 2.
  62. Expectation: The length of input of inner scalar mod should not less than 2.
  63. """
  64. class Net(Cell):
  65. def __init__(self):
  66. super().__init__()
  67. self.param_a = Parameter(Tensor(5, ms.int32), name="param_a")
  68. self.mod = P.Mod()
  69. def construct(self, x):
  70. return x + self.param_a + self.mod(5)
  71. x = Tensor(2, dtype=ms.int32)
  72. net = Net()
  73. with pytest.raises(Exception, match="Function S-Prim-Mod's input length is not equal to Signature length."):
  74. ret = net(x)
  75. print("ret:", ret)
  76. def test_make_range_input_is_empty():
  77. """
  78. Feature: Check the length of inputs of make_range operator.
  79. Description: The inputs of make_range operator could not be empty.
  80. Expectation: The inputs of make_range operator could not be empty.
  81. """
  82. class Net(Cell):
  83. def construct(self, x, y):
  84. for _ in F.make_range():
  85. x += y
  86. return x
  87. x = Tensor(2, dtype=ms.int32)
  88. y = Tensor(4, dtype=ms.int32)
  89. net = Net()
  90. with pytest.raises(Exception, match="The inputs of make_range operator could not be empty."):
  91. ret = net(x, y)
  92. print("ret:", ret)
  93. def test_make_range_input_type():
  94. """
  95. Feature: Check the type of inputs of make_range operator.
  96. Description: The type of inputs of make_range operator must be int64.
  97. Expectation: The type of inputs of make_range operator must be int64.
  98. """
  99. class Net(Cell):
  100. def construct(self, x, y):
  101. for _ in F.make_range(0, 0.02):
  102. x += y
  103. return x
  104. x = Tensor(2, dtype=ms.int32)
  105. y = Tensor(4, dtype=ms.int32)
  106. net = Net()
  107. with pytest.raises(Exception, match="The type of inputs of make_range operator only support int64 number."):
  108. ret = net(x, y)
  109. print("ret:", ret)
  110. def test_make_range_input_size():
  111. """
  112. Feature: Check the size of inputs of make_range operator.
  113. Description: The size of inputs of make_range operator could not exceed 3.
  114. Expectation: The size of inputs of make_range operator could not exceed 3.
  115. """
  116. class Net(Cell):
  117. def construct(self, x, y):
  118. for _ in F.make_range(1, 2, 3, 4):
  119. x += y
  120. return x
  121. x = Tensor(2, dtype=ms.int32)
  122. y = Tensor(4, dtype=ms.int32)
  123. net = Net()
  124. with pytest.raises(Exception, match="The size of inputs of make_range operator could not exceed 3."):
  125. ret = net(x, y)
  126. print("ret:", ret)
  127. def test_make_range_overflow():
  128. """
  129. Feature: Check the size of inputs of make_range operator.
  130. Description: The size of inputs of make_range operator could not exceed 3.
  131. Expectation: The size of inputs of make_range operator could not exceed 3.
  132. """
  133. class Net(Cell):
  134. def construct(self, x, y):
  135. max_index = sys.maxsize
  136. for _ in F.make_range(max_index - 1, max_index, 3):
  137. x += y
  138. return x
  139. x = Tensor(2, dtype=ms.int32)
  140. y = Tensor(4, dtype=ms.int32)
  141. net = Net()
  142. with pytest.raises(Exception, match="For make range, the required cycles number is greater than max cycles number"):
  143. ret = net(x, y)
  144. print("ret:", ret)
  145. def test_typeof():
  146. """
  147. Feature: Check the size of inputs of typeof operator.
  148. Description: The size of inputs of typeof operator must be 1.
  149. Expectation: The size of inputs of typeof operator must be 1.
  150. """
  151. class Net(Cell):
  152. def construct(self, x):
  153. return F.typeof(x, x)
  154. x = Tensor([2, 3, 4, 5], dtype=ms.int32)
  155. net = Net()
  156. with pytest.raises(Exception, match="Typeof evaluator requires 1 parameter, while the input size is 2."):
  157. ret = net(x)
  158. print("ret:", ret)
  159. def test_tuple_div():
  160. """
  161. Feature: Check the size of inputs of tuple_div operator.
  162. Description: The size of inputs of tuple_div operator must be same.
  163. Expectation: The size of inputs of tuple_div operator must be same.
  164. """
  165. class Net(Cell):
  166. def construct(self, x, y):
  167. return F.tuple_div(x, y)
  168. x = (8, 14, 20)
  169. y = (2, 2)
  170. net = Net()
  171. with pytest.raises(Exception, match="The size of inputs of tuple_div operator must be same"):
  172. ret = net(x, y)
  173. print("ret:", ret)
  174. def test_tuple_div_input_is_not_divisible():
  175. """
  176. Feature: Check whether the inputs of tuple_div is divisible.
  177. Description: The inputs of tuple_div could be divisible.
  178. Expectation: The inputs of tuple_div could be divisible.
  179. """
  180. class Net(Cell):
  181. def construct(self, x, y):
  182. return F.tuple_div(x, y)
  183. x = (8, 14)
  184. y = (2, 3)
  185. net = Net()
  186. with pytest.raises(Exception, match="The inputs of tuple_div is not divisible"):
  187. ret = net(x, y)
  188. print("ret:", ret)
  189. def test_make_slice_scalar():
  190. """
  191. Feature: Check whether the scalar input of make_slice is int or bool.
  192. Description: The scalar input of make_slice is int or bool.
  193. Expectation: The scalar input of make_slice is int or bool.
  194. """
  195. class Net(Cell):
  196. def construct(self, data):
  197. return data[F.make_slice(1.01, None, None)]
  198. x = Tensor((8, 10, 12), dtype=ms.int32)
  199. net = Net()
  200. with pytest.raises(Exception, match="The 0th input of scalar should be int or bool"):
  201. ret = net(x)
  202. print("ret:", ret)