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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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="Cannot perform modulo operation on 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="For 'S-Prim-Mod', the size of input should be 2"):
  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 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="For 'range', the arguments could not be empty."):
  91. ret = net(x, y)
  92. print("ret:", ret)
  93. def test_make_range_step_zero():
  94. """
  95. Feature: Check the length of inputs of make_range operator.
  96. Description: The step value of MakeRange operator could not be 0.
  97. Expectation: The step value of MakeRange operator could not be 0.
  98. """
  99. class Net(Cell):
  100. def construct(self, x, y):
  101. for _ in range(1, 2, 0):
  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="For 'range', the argument 'step' could not be 0."):
  108. ret = net(x, y)
  109. print("ret:", ret)
  110. def test_make_range_error_input_1():
  111. """
  112. Feature: Check the inputs of make_range operator.
  113. Description: If start > stop, the step need smaller than zero.
  114. Expectation: If start > stop, the step need smaller than zero.
  115. """
  116. class Net(Cell):
  117. def construct(self, x, y):
  118. for _ in range(1, -1, 3):
  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="For 'range', while the argument 'start'"):
  125. ret = net(x, y)
  126. print("ret:", ret)
  127. def test_make_range_error_input_2():
  128. """
  129. Feature: Check the length of inputs of make_range operator.
  130. Description: If start < stop, the step need greater than zero.
  131. Expectation: If start < stop, the step need greater than zero.
  132. """
  133. class Net(Cell):
  134. def construct(self, x, y):
  135. for _ in range(-1, 1, -3):
  136. x += y
  137. return x
  138. x = Tensor(2, dtype=ms.int32)
  139. y = Tensor(4, dtype=ms.int32)
  140. net = Net()
  141. with pytest.raises(Exception, match="For 'range', while the argument 'start'"):
  142. ret = net(x, y)
  143. print("ret:", ret)
  144. def test_make_range_input_type():
  145. """
  146. Feature: Check the type of inputs of make_range operator.
  147. Description: The type of inputs of make_range operator must be int64.
  148. Expectation: The type of inputs of make_range operator must be int64.
  149. """
  150. class Net(Cell):
  151. def construct(self, x, y):
  152. for _ in range(0, 0.02):
  153. x += y
  154. return x
  155. x = Tensor(2, dtype=ms.int32)
  156. y = Tensor(4, dtype=ms.int32)
  157. net = Net()
  158. with pytest.raises(Exception, match="The type of inputs in range operator only support int64 number."):
  159. ret = net(x, y)
  160. print("ret:", ret)
  161. def test_make_range_input_type_2():
  162. """
  163. Feature: Check the type of inputs of make_range operator.
  164. Description: The type of inputs of make_range operator must be int64.
  165. Expectation: The type of inputs of make_range operator must be int64.
  166. """
  167. class Net(Cell):
  168. def construct(self, x, y):
  169. for _ in range(0, 1, 3.00):
  170. x += y
  171. return x
  172. x = Tensor(2, dtype=ms.int32)
  173. y = Tensor(4, dtype=ms.int32)
  174. net = Net()
  175. with pytest.raises(Exception, match="The type of inputs in range operator only support int64 number."):
  176. ret = net(x, y)
  177. print("ret:", ret)
  178. def test_make_range_input_type_3():
  179. """
  180. Feature: Check the type of inputs of make_range operator.
  181. Description: The type of inputs of make_range operator must be int64.
  182. Expectation: The type of inputs of make_range operator must be int64.
  183. """
  184. class Net(Cell):
  185. def construct(self, x, y):
  186. for _ in range(3.00):
  187. x += y
  188. return x
  189. x = Tensor(2, dtype=ms.int32)
  190. y = Tensor(4, dtype=ms.int32)
  191. net = Net()
  192. with pytest.raises(Exception, match="The type of inputs in range operator only support int64 number."):
  193. ret = net(x, y)
  194. print("ret:", ret)
  195. def test_make_range_input_size():
  196. """
  197. Feature: Check the size of inputs of make_range operator.
  198. Description: The size of inputs of make_range operator could not exceed 3.
  199. Expectation: The size of inputs of make_range operator could not exceed 3.
  200. """
  201. class Net(Cell):
  202. def construct(self, x, y):
  203. for _ in range(1, 2, 3, 4):
  204. x += y
  205. return x
  206. x = Tensor(2, dtype=ms.int32)
  207. y = Tensor(4, dtype=ms.int32)
  208. net = Net()
  209. with pytest.raises(Exception, match="For 'range', the size of arguments could not exceed 3."):
  210. ret = net(x, y)
  211. print("ret:", ret)
  212. def test_make_range_overflow():
  213. """
  214. Feature: Check the size of inputs of range operator.
  215. Description: The size of inputs of make_range operator could not exceed 3.
  216. Expectation: The size of inputs of make_range operator could not exceed 3.
  217. """
  218. class Net(Cell):
  219. def construct(self, x, y):
  220. max_index = sys.maxsize
  221. for _ in range(max_index - 1, max_index, 3):
  222. x += y
  223. return x
  224. x = Tensor(2, dtype=ms.int32)
  225. y = Tensor(4, dtype=ms.int32)
  226. net = Net()
  227. with pytest.raises(Exception, match="Integer overflow error occurred when traversing the range."):
  228. ret = net(x, y)
  229. print("ret:", ret)
  230. def test_make_range_overflow_2():
  231. """
  232. Feature: Check the size of inputs of make_range operator.
  233. Description: The size of inputs of make_range operator could not exceed 3.
  234. Expectation: The size of inputs of make_range operator could not exceed 3.
  235. """
  236. class Net(Cell):
  237. def construct(self, x, y):
  238. min_index = -sys.maxsize
  239. for _ in range(min_index, min_index - 1, -3):
  240. x += y
  241. return x
  242. x = Tensor(2, dtype=ms.int32)
  243. y = Tensor(4, dtype=ms.int32)
  244. net = Net()
  245. with pytest.raises(Exception, match="Integer overflow error occurred when traversing the range."):
  246. ret = net(x, y)
  247. print("ret:", ret)
  248. def test_typeof():
  249. """
  250. Feature: Check the size of inputs of typeof operator.
  251. Description: The size of inputs of typeof operator must be 1.
  252. Expectation: The size of inputs of typeof operator must be 1.
  253. """
  254. class Net(Cell):
  255. def construct(self, x):
  256. return F.typeof(x, x)
  257. x = Tensor([2, 3, 4, 5], dtype=ms.int32)
  258. net = Net()
  259. with pytest.raises(Exception, match="The Typeof operator must requires 1 argument, "
  260. "but the size of arguments is 2."):
  261. ret = net(x)
  262. print("ret:", ret)
  263. def test_tuple_div():
  264. """
  265. Feature: Check the size of inputs of tuple_div operator.
  266. Description: The size of inputs of tuple_div operator must be same.
  267. Expectation: The size of inputs of tuple_div operator must be same.
  268. """
  269. class Net(Cell):
  270. def construct(self, x, y):
  271. return F.tuple_div(x, y)
  272. x = (8, 14, 20)
  273. y = (2, 2)
  274. net = Net()
  275. with pytest.raises(Exception, match="The size of inputs of 'tuple_div' operator must be the same"):
  276. ret = net(x, y)
  277. print("ret:", ret)
  278. def test_tuple_div_type():
  279. """
  280. Feature: Check the size of inputs of tuple_div operator.
  281. Description: The type of inputs of tuple_div operator must be int64 number.
  282. Expectation: The type of inputs of tuple_div operator must be int64 number.
  283. """
  284. class Net(Cell):
  285. def construct(self, x, y):
  286. return F.tuple_div(x, y)
  287. x = (8, 14, 20)
  288. y = (2, 2, 2.0)
  289. net = Net()
  290. with pytest.raises(Exception, match="The data type of inputs of 'tuple_div' operator should be an int64 number,"):
  291. ret = net(x, y)
  292. print("ret:", ret)
  293. def test_tuple_div_zero():
  294. """
  295. Feature: Check the size of inputs of tuple_div operator.
  296. Description: The divisor value should not be 0.
  297. Expectation: The divisor value should not be 0.
  298. """
  299. class Net(Cell):
  300. def construct(self, x, y):
  301. return F.tuple_div(x, y)
  302. x = (8, 14, 20)
  303. y = (2, 2, 0)
  304. net = Net()
  305. with pytest.raises(Exception, match="The divisor value should not be 0"):
  306. ret = net(x, y)
  307. print("ret:", ret)
  308. def test_tuple_div_input_is_not_divisible():
  309. """
  310. Feature: Check whether the inputs of tuple_div is divisible.
  311. Description: The inputs of tuple_div could be divisible.
  312. Expectation: The inputs of tuple_div could be divisible.
  313. """
  314. class Net(Cell):
  315. def construct(self, x, y):
  316. return F.tuple_div(x, y)
  317. x = (8, 14)
  318. y = (2, 3)
  319. net = Net()
  320. with pytest.raises(Exception, match="The inputs of 'tuple_div' operator should be divisible,"):
  321. ret = net(x, y)
  322. print("ret:", ret)
  323. def test_make_slice_scalar():
  324. """
  325. Feature: Check whether the scalar input of make_slice is int or bool.
  326. Description: The scalar input of make_slice is int or bool.
  327. Expectation: The scalar input of make_slice is int or bool.
  328. """
  329. class Net(Cell):
  330. def construct(self, data):
  331. return data[1.01:None:None]
  332. x = Tensor((8, 10, 12), dtype=ms.int32)
  333. net = Net()
  334. with pytest.raises(Exception, match="Slice indices must be integers or bool."):
  335. ret = net(x)
  336. print("ret:", ret)