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_grammar_constraints.py 6.1 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. """
  16. test mindspore grammar constraints
  17. 1. function must have return statement
  18. 2. raise statement can not be used
  19. """
  20. # pylint: disable=R1705, R1710, W0223
  21. import numpy as np
  22. import pytest
  23. import mindspore.nn as nn
  24. from mindspore import Tensor
  25. from mindspore import context
  26. from mindspore import dtype as mstype
  27. context.set_context(mode=context.GRAPH_MODE)
  28. def test_missing_return():
  29. class NetMissReturn(nn.Cell):
  30. def __init__(self):
  31. super(NetMissReturn, self).__init__()
  32. def construct(self, x, y, z):
  33. if x == 1:
  34. return 10
  35. elif x == 20:
  36. if y == 1:
  37. return 3
  38. elif y == 2:
  39. for i in range(z):
  40. return i + z
  41. i = 0
  42. while i < z:
  43. return i + z
  44. def g(u):
  45. return x + u
  46. # here method 'construct' misses a return statement
  47. g(y)
  48. else:
  49. return 7
  50. else:
  51. return 5
  52. net = NetMissReturn()
  53. x = Tensor(0, mstype.int32)
  54. y = Tensor(5, mstype.int32)
  55. z = Tensor(2, mstype.int32)
  56. with pytest.raises(TypeError) as er:
  57. net(x, y, z)
  58. assert "Function must has 'return' statement, but missing in bound method 'construct'" in str(er.value)
  59. def test_nest_function_missing_return():
  60. class NetNestFuncMissReturn(nn.Cell):
  61. def __init__(self):
  62. super(NetNestFuncMissReturn, self).__init__()
  63. def construct(self, x, y, z):
  64. if x == 1:
  65. return 10
  66. elif x == 20:
  67. if y == 1:
  68. return 3
  69. elif y == 2:
  70. for i in range(z):
  71. return i + z
  72. i = 0
  73. while i < z:
  74. return i + z
  75. def g(u):
  76. x += u
  77. # nested function 'g' misses a return a statement
  78. return g(y)
  79. else:
  80. return 7
  81. else:
  82. return 5
  83. net = NetNestFuncMissReturn()
  84. x = Tensor(0, mstype.int32)
  85. y = Tensor(5, mstype.int32)
  86. z = Tensor(2, mstype.int32)
  87. with pytest.raises(TypeError) as er:
  88. net(x, y, z)
  89. assert "Function must has 'return' statement, but missing in function 'g'" in str(er.value)
  90. def test_raise_in_method():
  91. class NetRaiseInMethod(nn.Cell):
  92. def __init__(self):
  93. super(NetRaiseInMethod, self).__init__()
  94. def construct(self, x, y, z):
  95. if x == 1:
  96. return 10
  97. elif x == 20:
  98. # add not support grammar 'raise' here
  99. raise ValueError('Illegal case')
  100. else:
  101. return y + z
  102. net = NetRaiseInMethod()
  103. x = Tensor(0, mstype.int32)
  104. y = Tensor(5, mstype.int32)
  105. z = Tensor(2, mstype.int32)
  106. with pytest.raises(RuntimeError) as er:
  107. net(x, y, z)
  108. assert "Unsupported statement 'Raise'." in str(er.value)
  109. def test_raise_in_nested_function():
  110. class NetNestRaise(nn.Cell):
  111. def __init__(self):
  112. super(NetNestRaise, self).__init__()
  113. def construct(self, x, y, z):
  114. if x == 1:
  115. return 10
  116. elif x == 20:
  117. def nest_fn(u):
  118. if u > 0:
  119. # add not support grammar 'raise' here
  120. raise ValueError('Illegal case')
  121. return u + z + 1
  122. return nest_fn(y)
  123. else:
  124. return y + z
  125. net = NetNestRaise()
  126. x = Tensor(0, mstype.int32)
  127. y = Tensor(5, mstype.int32)
  128. z = Tensor(2, mstype.int32)
  129. with pytest.raises(RuntimeError) as er:
  130. net(x, y, z)
  131. assert "Unsupported statement 'Raise'." in str(er.value)
  132. def test_nest_branch_with_return():
  133. class NetBranchWithReturn(nn.Cell):
  134. def __init__(self):
  135. super(NetBranchWithReturn, self).__init__()
  136. def construct(self, x, y, z):
  137. if x == 1:
  138. return 10
  139. else:
  140. return 5
  141. context.set_context(save_graphs=True)
  142. net = NetBranchWithReturn()
  143. x = Tensor(0, mstype.int32)
  144. y = Tensor(5, mstype.int32)
  145. z = Tensor(2, mstype.int32)
  146. net(x, y, z)
  147. def test_any_with_no_return():
  148. class NetAnyNoReturn(nn.Cell):
  149. def __init__(self):
  150. super(NetAnyNoReturn, self).__init__()
  151. def construct(self, inp):
  152. result = inp.any()
  153. if result:
  154. return 6
  155. np_input = np.arange(2 * 3 * 4).reshape((2, 3, 4)).astype(np.bool_)
  156. tensor = Tensor(np_input)
  157. net = NetAnyNoReturn()
  158. with pytest.raises(TypeError) as er:
  159. net(tensor)
  160. assert "Function must has 'return' statement, but missing in bound method 'construct'" in str(er.value)
  161. def test_missing_construct():
  162. class NetMissConstruct(nn.Cell):
  163. def __init__(self):
  164. super(NetMissConstruct, self).__init__()
  165. def construct1(self, inp):
  166. return 5
  167. np_input = np.arange(2 * 3 * 4).reshape((2, 3, 4)).astype(np.bool_)
  168. tensor = Tensor(np_input)
  169. net = NetMissConstruct()
  170. assert net(tensor) is None