Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
# Copyright 2020 Huawei Technologies Co., Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
# ============================================================================
|
|
|
|
""" test_cont_break """
|
|
|
|
import numpy as np
|
2020-05-18 16:42:35 +08:00
|
|
|
import pytest
|
|
|
|
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
from mindspore import Tensor, Model, context
|
2020-05-18 16:42:35 +08:00
|
|
|
from mindspore.nn import Cell
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
def run_test(netclass, count, dev):
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target=dev)
|
|
|
|
net = netclass()
|
|
|
|
model = Model(net)
|
|
|
|
for _ in range(count):
|
|
|
|
input_np = np.random.randn(2, 3).astype(np.float32)
|
|
|
|
input_ms = Tensor(input_np)
|
2020-05-18 10:31:46 +08:00
|
|
|
output_np = net.construct(input_np) # run python
|
|
|
|
output_ms = model.predict(input_ms) # run graph
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
np.testing.assert_array_almost_equal(output_np, output_ms.asnumpy(), decimal=3)
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2021-04-12 09:41:05 +08:00
|
|
|
class ForLoopWithBreak(Cell):
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
def construct(self, x):
|
|
|
|
for i in range(8):
|
|
|
|
if i > 5:
|
|
|
|
x *= 3
|
|
|
|
break
|
|
|
|
x = x * 2
|
|
|
|
return x
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2021-04-12 09:41:05 +08:00
|
|
|
class ForLoopWithContinue(Cell):
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
def construct(self, x):
|
|
|
|
for i in range(8):
|
|
|
|
if i > 5:
|
|
|
|
x *= 3
|
|
|
|
continue
|
|
|
|
x = x * 2
|
|
|
|
return x
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2021-04-12 09:41:05 +08:00
|
|
|
class ForLoopWithContBreak(Cell):
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
def construct(self, x):
|
|
|
|
for i in range(8):
|
|
|
|
if i < 3:
|
|
|
|
i *= 2
|
|
|
|
continue
|
|
|
|
if i > 5:
|
|
|
|
x *= 3
|
|
|
|
break
|
|
|
|
x = x * 2
|
|
|
|
return x
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2021-04-12 09:41:05 +08:00
|
|
|
class ForNestedLoopWithBreak(Cell):
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
def construct(self, x):
|
2020-05-20 11:12:14 +08:00
|
|
|
for _ in range(3):
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
for j in range(5):
|
|
|
|
if j > 3:
|
|
|
|
x *= 2
|
|
|
|
break
|
|
|
|
x = x * 1.5
|
|
|
|
return x
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2021-04-12 09:41:05 +08:00
|
|
|
class WhileWithBreak(Cell):
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
def construct(self, x):
|
|
|
|
i = 0
|
|
|
|
while i < 5:
|
|
|
|
if i > 3:
|
|
|
|
x *= 2
|
|
|
|
break
|
|
|
|
x = x * 1.5
|
|
|
|
i += 1
|
|
|
|
return x
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2021-04-12 09:41:05 +08:00
|
|
|
class WhileWithContinue(Cell):
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
def construct(self, x):
|
|
|
|
i = 0
|
|
|
|
while i < 5:
|
|
|
|
if i > 3:
|
|
|
|
x *= 2
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
x = x * 1.5
|
|
|
|
i += 1
|
|
|
|
return x
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2021-04-12 09:41:05 +08:00
|
|
|
class WhileForNested(Cell):
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
def construct(self, x):
|
|
|
|
i = 0
|
|
|
|
while i < 5:
|
|
|
|
if i > 3:
|
|
|
|
for j in range(3):
|
|
|
|
if j > 1:
|
|
|
|
break
|
|
|
|
x *= 2
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
x = x * 1.5
|
|
|
|
i += 1
|
|
|
|
return x
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
2021-04-12 09:41:05 +08:00
|
|
|
class PassBranch(Cell):
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
def construct(self, x):
|
|
|
|
i = 0
|
|
|
|
while i < 5:
|
|
|
|
if i > 3:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
x = x * 1.5
|
|
|
|
i += 1
|
|
|
|
return x
|
|
|
|
|
2020-05-18 10:31:46 +08:00
|
|
|
|
Support 'break', 'continue' and 'pass'
To handle 'break' and 'continue' statement, a loop context is pushed
to a stack before we parse the loop body, and pop it after body parsed.
When a 'break', 'continue' statement is encountered, we retrieve current
loop contex from the stack, and let the current block jump to the end
block or header block;
For 'break' statement, we added an extra 'end_block' follow the 'after_block',
because 'after_block' is called from a ContionalJump in 'header_block', it can
not be set as jump target from other place. to support 'break', we let loop
body jump to the 'end_block' at the 'break' point. and 'after_block'
maybe a good place to handle loop 'else' clause in the future.
Handle 'pass' is simple, just bypass it when doing parse.
2020-05-12 08:51:09 +08:00
|
|
|
@pytest.mark.level0
|
|
|
|
@pytest.mark.platform_x86_cpu
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
def test_cont_break():
|
|
|
|
count = 20
|
|
|
|
dev = 'CPU'
|
2021-04-12 09:41:05 +08:00
|
|
|
run_test(ForLoopWithBreak, count, dev)
|
|
|
|
run_test(ForLoopWithContinue, count, dev)
|
|
|
|
run_test(ForLoopWithContBreak, count, dev)
|
|
|
|
run_test(ForNestedLoopWithBreak, count, dev)
|
|
|
|
run_test(WhileWithBreak, count, dev)
|
|
|
|
run_test(WhileWithContinue, count, dev)
|
|
|
|
run_test(WhileForNested, count, dev)
|
|
|
|
run_test(PassBranch, count, dev)
|