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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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="The second input of ScalarMod operator could not be 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="The size of input in the operator should be equal to the size of the "
  74. "operator's signature."):
  75. ret = net(x)
  76. print("ret:", ret)
  77. def test_make_range_input_is_empty():
  78. """
  79. Feature: Check the length of inputs of make_range operator.
  80. Description: The inputs of make_range operator could not be empty.
  81. Expectation: The inputs of make_range operator could not be empty.
  82. """
  83. class Net(Cell):
  84. def construct(self, x, y):
  85. for _ in F.make_range():
  86. x += y
  87. return x
  88. x = Tensor(2, dtype=ms.int32)
  89. y = Tensor(4, dtype=ms.int32)
  90. net = Net()
  91. with pytest.raises(Exception, match="The inputs of MakeRange operator could not be empty."):
  92. ret = net(x, y)
  93. print("ret:", ret)
  94. def test_make_range_step_zero():
  95. """
  96. Feature: Check the length of inputs of make_range operator.
  97. Description: The step value of MakeRange operator could not be 0.
  98. Expectation: The step value of MakeRange operator could not be 0.
  99. """
  100. class Net(Cell):
  101. def construct(self, x, y):
  102. for _ in F.make_range(1, 2, 0):
  103. x += y
  104. return x
  105. x = Tensor(2, dtype=ms.int32)
  106. y = Tensor(4, dtype=ms.int32)
  107. net = Net()
  108. with pytest.raises(Exception, match="The step value of MakeRange operator could not be 0."):
  109. ret = net(x, y)
  110. print("ret:", ret)
  111. def test_make_range_error_input_1():
  112. """
  113. Feature: Check the inputs of make_range operator.
  114. Description: If start > stop, the step need smaller than zero.
  115. Expectation: If start > stop, the step need smaller than zero.
  116. """
  117. class Net(Cell):
  118. def construct(self, x, y):
  119. for _ in F.make_range(1, -1, 3):
  120. x += y
  121. return x
  122. x = Tensor(2, dtype=ms.int32)
  123. y = Tensor(4, dtype=ms.int32)
  124. net = Net()
  125. with pytest.raises(Exception, match="Error slice"):
  126. ret = net(x, y)
  127. print("ret:", ret)
  128. def test_make_range_error_input_2():
  129. """
  130. Feature: Check the length of inputs of make_range operator.
  131. Description: If start < stop, the step need greater than zero.
  132. Expectation: If start < stop, the step need greater than zero.
  133. """
  134. class Net(Cell):
  135. def construct(self, x, y):
  136. for _ in F.make_range(-1, 1, -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="Error slice"):
  143. ret = net(x, y)
  144. print("ret:", ret)
  145. def test_make_range_input_type():
  146. """
  147. Feature: Check the type of inputs of make_range operator.
  148. Description: The type of inputs of make_range operator must be int64.
  149. Expectation: The type of inputs of make_range operator must be int64.
  150. """
  151. class Net(Cell):
  152. def construct(self, x, y):
  153. for _ in F.make_range(0, 0.02):
  154. x += y
  155. return x
  156. x = Tensor(2, dtype=ms.int32)
  157. y = Tensor(4, dtype=ms.int32)
  158. net = Net()
  159. with pytest.raises(Exception, match="The type of inputs in MakeRange operator only support int64 number."):
  160. ret = net(x, y)
  161. print("ret:", ret)
  162. def test_make_range_input_size():
  163. """
  164. Feature: Check the size of inputs of make_range operator.
  165. Description: The size of inputs of make_range operator could not exceed 3.
  166. Expectation: The size of inputs of make_range operator could not exceed 3.
  167. """
  168. class Net(Cell):
  169. def construct(self, x, y):
  170. for _ in F.make_range(1, 2, 3, 4):
  171. x += y
  172. return x
  173. x = Tensor(2, dtype=ms.int32)
  174. y = Tensor(4, dtype=ms.int32)
  175. net = Net()
  176. with pytest.raises(Exception, match="The size of inputs of MakeRange operator could not exceed 3."):
  177. ret = net(x, y)
  178. print("ret:", ret)
  179. def test_make_range_overflow():
  180. """
  181. Feature: Check the size of inputs of make_range operator.
  182. Description: The size of inputs of make_range operator could not exceed 3.
  183. Expectation: The size of inputs of make_range operator could not exceed 3.
  184. """
  185. class Net(Cell):
  186. def construct(self, x, y):
  187. max_index = sys.maxsize
  188. for _ in F.make_range(max_index - 1, max_index, 3):
  189. x += y
  190. return x
  191. x = Tensor(2, dtype=ms.int32)
  192. y = Tensor(4, dtype=ms.int32)
  193. net = Net()
  194. with pytest.raises(Exception, match="For MakeRange operator, the required cycles number is greater than max cycles"
  195. "number"):
  196. ret = net(x, y)
  197. print("ret:", ret)
  198. def test_typeof():
  199. """
  200. Feature: Check the size of inputs of typeof operator.
  201. Description: The size of inputs of typeof operator must be 1.
  202. Expectation: The size of inputs of typeof operator must be 1.
  203. """
  204. class Net(Cell):
  205. def construct(self, x):
  206. return F.typeof(x, x)
  207. x = Tensor([2, 3, 4, 5], dtype=ms.int32)
  208. net = Net()
  209. with pytest.raises(Exception, match="The Typeof operator must requires 1 argument, "
  210. "but the size of arguments is 2."):
  211. ret = net(x)
  212. print("ret:", ret)
  213. def test_tuple_div():
  214. """
  215. Feature: Check the size of inputs of tuple_div operator.
  216. Description: The size of inputs of tuple_div operator must be same.
  217. Expectation: The size of inputs of tuple_div operator must be same.
  218. """
  219. class Net(Cell):
  220. def construct(self, x, y):
  221. return F.tuple_div(x, y)
  222. x = (8, 14, 20)
  223. y = (2, 2)
  224. net = Net()
  225. with pytest.raises(Exception, match="The size of inputs of TupleDiv operator must be the same"):
  226. ret = net(x, y)
  227. print("ret:", ret)
  228. def test_tuple_div_type():
  229. """
  230. Feature: Check the size of inputs of tuple_div operator.
  231. Description: The type of inputs of tuple_div operator must be int64 number.
  232. Expectation: The type of inputs of tuple_div operator must be int64 number.
  233. """
  234. class Net(Cell):
  235. def construct(self, x, y):
  236. return F.tuple_div(x, y)
  237. x = (8, 14, 20)
  238. y = (2, 2, 2.0)
  239. net = Net()
  240. with pytest.raises(Exception, match="The data type of inputs of TupleDiv operator should be an int64 number"):
  241. ret = net(x, y)
  242. print("ret:", ret)
  243. def test_tuple_div_zero():
  244. """
  245. Feature: Check the size of inputs of tuple_div operator.
  246. Description: The divisor value should not be 0.
  247. Expectation: The divisor value should not be 0.
  248. """
  249. class Net(Cell):
  250. def construct(self, x, y):
  251. return F.tuple_div(x, y)
  252. x = (8, 14, 20)
  253. y = (2, 2, 0)
  254. net = Net()
  255. with pytest.raises(Exception, match="The divisor value should not be 0"):
  256. ret = net(x, y)
  257. print("ret:", ret)
  258. def test_tuple_div_input_is_not_divisible():
  259. """
  260. Feature: Check whether the inputs of tuple_div is divisible.
  261. Description: The inputs of tuple_div could be divisible.
  262. Expectation: The inputs of tuple_div could be divisible.
  263. """
  264. class Net(Cell):
  265. def construct(self, x, y):
  266. return F.tuple_div(x, y)
  267. x = (8, 14)
  268. y = (2, 3)
  269. net = Net()
  270. with pytest.raises(Exception, match="The inputs of TupleDiv is not divisible"):
  271. ret = net(x, y)
  272. print("ret:", ret)
  273. def test_make_slice_scalar():
  274. """
  275. Feature: Check whether the scalar input of make_slice is int or bool.
  276. Description: The scalar input of make_slice is int or bool.
  277. Expectation: The scalar input of make_slice is int or bool.
  278. """
  279. class Net(Cell):
  280. def construct(self, data):
  281. return data[F.make_slice(1.01, None, None)]
  282. x = Tensor((8, 10, 12), dtype=ms.int32)
  283. net = Net()
  284. with pytest.raises(Exception, match="The 0th input of scalar should be int or bool"):
  285. ret = net(x)
  286. print("ret:", ret)