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_cont_break.py 4.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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. """ test_cont_break """
  16. import numpy as np
  17. from mindspore import Tensor, Model, context
  18. from mindspore.nn import Cell
  19. from ...ut_filter import non_graph_engine
  20. def run_test(netclass, count):
  21. context.set_context(mode=context.GRAPH_MODE)
  22. net = netclass()
  23. model = Model(net)
  24. for _ in range(count):
  25. input_np = np.random.randn(2, 3).astype(np.float32)
  26. input_ms = Tensor(input_np)
  27. output_np = net.construct(input_np) # run python
  28. output_ms = model.predict(input_ms) # run graph
  29. assert np.shape(output_np) == np.shape(output_ms.asnumpy())
  30. # Disable equal assert because UT in CI use fake backend.
  31. # np.testing.assert_array_almost_equal(output_np, output_ms.asnumpy(), decimal=3)
  32. # pylint: disable=unnecessary-pass
  33. class for_loop_with_break(Cell):
  34. def __init__(self):
  35. super().__init__()
  36. def construct(self, x):
  37. for i in range(8):
  38. if i > 5:
  39. x *= 3
  40. break
  41. x = x * 2
  42. pass
  43. return x
  44. @non_graph_engine
  45. def test_for_loop_with_break():
  46. run_test(for_loop_with_break, 10)
  47. class for_loop_with_continue(Cell):
  48. def __init__(self):
  49. super().__init__()
  50. def construct(self, x):
  51. for i in range(8):
  52. if i > 5:
  53. x *= 3
  54. continue
  55. x = x * 2
  56. return x
  57. @non_graph_engine
  58. def test_for_loop_with_continue():
  59. run_test(for_loop_with_continue, 10)
  60. # pylint: disable=unnecessary-pass
  61. class for_loop_with_cont_break(Cell):
  62. def __init__(self):
  63. super().__init__()
  64. def construct(self, x):
  65. for i in range(8):
  66. if i < 3:
  67. i *= 2
  68. continue
  69. if i > 5:
  70. x *= 3
  71. break
  72. # x *= 2
  73. x = x * 2
  74. pass
  75. return x
  76. @non_graph_engine
  77. def test_for_loop_with_cont_break():
  78. run_test(for_loop_with_cont_break, 10)
  79. class for_nested_loop_with_break(Cell):
  80. def __init__(self):
  81. super().__init__()
  82. def construct(self, x):
  83. for i in range(3):
  84. for j in range(5):
  85. if j > 3:
  86. x *= 2
  87. break
  88. x = x * 1.5
  89. return x
  90. @non_graph_engine
  91. def test_for_nested_loop_with_break():
  92. run_test(for_nested_loop_with_break, 10)
  93. class while_with_break(Cell):
  94. def __init__(self):
  95. super().__init__()
  96. def construct(self, x):
  97. i = 0
  98. while i < 5:
  99. if i > 3:
  100. x *= 2
  101. break
  102. x = x * 1.5
  103. i += 1
  104. return x
  105. @non_graph_engine
  106. def test_while_with_break():
  107. run_test(while_with_break, 10)
  108. class while_with_continue(Cell):
  109. def __init__(self):
  110. super().__init__()
  111. def construct(self, x):
  112. i = 0
  113. while i < 5:
  114. if i > 3:
  115. x *= 2
  116. i += 1
  117. continue
  118. x = x * 1.5
  119. i += 1
  120. return x
  121. @non_graph_engine
  122. def test_while_with_continue():
  123. run_test(while_with_continue, 10)
  124. class while_for_nested(Cell):
  125. def __init__(self):
  126. super().__init__()
  127. def construct(self, x):
  128. i = 0
  129. while i < 5:
  130. if i > 3:
  131. for j in range(3):
  132. if j > 1:
  133. break
  134. x *= 2
  135. i += 1
  136. continue
  137. x = x * 1.5
  138. i += 1
  139. return x
  140. @non_graph_engine
  141. def test_while_for_nested():
  142. run_test(while_for_nested, 10)
  143. class pass_branch(Cell):
  144. def __init__(self):
  145. super().__init__()
  146. def construct(self, x):
  147. i = 0
  148. while i < 5:
  149. if i > 3:
  150. pass
  151. else:
  152. x = x * 1.5
  153. i += 1
  154. return x
  155. @non_graph_engine
  156. def test_pass_branch():
  157. run_test(pass_branch, 10)