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 3.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. import pytest
  18. from mindspore import Tensor, Model, context
  19. from mindspore.nn import Cell
  20. def run_test(netclass, count, dev):
  21. context.set_context(mode=context.GRAPH_MODE, device_target=dev)
  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. np.testing.assert_array_almost_equal(output_np, output_ms.asnumpy(), decimal=3)
  30. class ForLoopWithBreak(Cell):
  31. def construct(self, x):
  32. for i in range(8):
  33. if i > 5:
  34. x *= 3
  35. break
  36. x = x * 2
  37. return x
  38. class ForLoopWithContinue(Cell):
  39. def construct(self, x):
  40. for i in range(8):
  41. if i > 5:
  42. x *= 3
  43. continue
  44. x = x * 2
  45. return x
  46. class ForLoopWithContBreak(Cell):
  47. def construct(self, x):
  48. for i in range(8):
  49. if i < 3:
  50. i *= 2
  51. continue
  52. if i > 5:
  53. x *= 3
  54. break
  55. x = x * 2
  56. return x
  57. class ForNestedLoopWithBreak(Cell):
  58. def construct(self, x):
  59. for _ in range(3):
  60. for j in range(5):
  61. if j > 3:
  62. x *= 2
  63. break
  64. x = x * 1.5
  65. return x
  66. class WhileWithBreak(Cell):
  67. def construct(self, x):
  68. i = 0
  69. while i < 5:
  70. if i > 3:
  71. x *= 2
  72. break
  73. x = x * 1.5
  74. i += 1
  75. return x
  76. class WhileWithContinue(Cell):
  77. def construct(self, x):
  78. i = 0
  79. while i < 5:
  80. if i > 3:
  81. x *= 2
  82. i += 1
  83. continue
  84. x = x * 1.5
  85. i += 1
  86. return x
  87. class WhileForNested(Cell):
  88. def construct(self, x):
  89. i = 0
  90. while i < 5:
  91. if i > 3:
  92. for j in range(3):
  93. if j > 1:
  94. break
  95. x *= 2
  96. i += 1
  97. continue
  98. x = x * 1.5
  99. i += 1
  100. return x
  101. class PassBranch(Cell):
  102. def construct(self, x):
  103. i = 0
  104. while i < 5:
  105. if i > 3:
  106. pass
  107. else:
  108. x = x * 1.5
  109. i += 1
  110. return x
  111. @pytest.mark.level0
  112. @pytest.mark.platform_x86_cpu
  113. @pytest.mark.env_onecard
  114. def test_cont_break():
  115. count = 20
  116. dev = 'CPU'
  117. run_test(ForLoopWithBreak, count, dev)
  118. run_test(ForLoopWithContinue, count, dev)
  119. run_test(ForLoopWithContBreak, count, dev)
  120. run_test(ForNestedLoopWithBreak, count, dev)
  121. run_test(WhileWithBreak, count, dev)
  122. run_test(WhileWithContinue, count, dev)
  123. run_test(WhileForNested, count, dev)
  124. run_test(PassBranch, count, dev)