Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
cf8d42bb
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
cf8d42bb
编写于
7月 01, 2022
作者:
A
Aurelius84
提交者:
GitHub
7月 01, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2Stat]Polish break/continue statement transformer logic (#43489)
* [Dy2Stat]Polish break/continue statement transformer logic
上级
76156d12
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
18 addition
and
5 deletion
+18
-5
python/paddle/fluid/dygraph/dygraph_to_static/break_continue_transformer.py
...d/dygraph/dygraph_to_static/break_continue_transformer.py
+4
-4
python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py
...le/fluid/dygraph/dygraph_to_static/variable_trans_func.py
+9
-0
python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py
.../tests/unittests/dygraph_to_static/test_break_continue.py
+5
-1
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/break_continue_transformer.py
浏览文件 @
cf8d42bb
...
...
@@ -20,7 +20,7 @@ from paddle.fluid import unique_name
from
paddle.fluid.dygraph.dygraph_to_static.utils
import
index_in_list
from
paddle.fluid.dygraph.dygraph_to_static.utils
import
ForNodeVisitor
from
paddle.fluid.dygraph.dygraph_to_static.utils
import
BaseNodeVisitor
from
paddle.fluid.dygraph.dygraph_to_static.variable_trans_func
import
create_
fill_constant
_node
from
paddle.fluid.dygraph.dygraph_to_static.variable_trans_func
import
create_
bool
_node
__all__
=
[
'BreakContinueTransformer'
]
...
...
@@ -140,7 +140,7 @@ class BreakContinueTransformer(BaseNodeVisitor):
self
.
_replace_if_stmt
(
loop_node_index
,
first_block_index
,
variable_name
)
# 4. For 'break' add break into condition of the loop.
assign_false_node
=
create_
fill_constant
_node
(
variable_name
,
False
)
assign_false_node
=
create_
bool
_node
(
variable_name
,
False
)
self
.
_add_stmt_before_cur_node
(
loop_node_index
,
assign_false_node
)
cond_var_node
=
gast
.
UnaryOp
(
op
=
gast
.
Not
(),
...
...
@@ -177,7 +177,7 @@ class BreakContinueTransformer(BaseNodeVisitor):
self
.
_replace_if_stmt
(
loop_node_index
,
first_block_index
,
variable_name
)
# 4. For 'continue', set continue to False at the beginning of each loop
assign_false_node
=
create_
fill_constant
_node
(
variable_name
,
False
)
assign_false_node
=
create_
bool
_node
(
variable_name
,
False
)
loop_node
.
body
.
insert
(
0
,
assign_false_node
)
def
_remove_stmts_after_break_continue
(
self
,
break_continue_node
,
...
...
@@ -221,7 +221,7 @@ class BreakContinueTransformer(BaseNodeVisitor):
i
=
index_in_list
(
stmt_list
,
break_continue_node
)
if
i
==
-
1
:
return
False
assign_true_node
=
create_
fill_constant
_node
(
break_continue_name
,
True
)
assign_true_node
=
create_
bool
_node
(
break_continue_name
,
True
)
stmt_list
[
i
:]
=
[
assign_true_node
]
return
True
...
...
python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py
浏览文件 @
cf8d42bb
...
...
@@ -76,3 +76,12 @@ def create_bool_as_type(x, value=True):
return
paddle
.
full
(
shape
=
[
1
],
fill_value
=
value
,
dtype
=
"bool"
)
else
:
return
value
def
create_bool_node
(
name
,
value
):
'''
Create a assign stmt for name = value .
'''
assert
isinstance
(
value
,
bool
)
node
=
"{} = {}"
.
format
(
name
,
value
)
return
gast
.
parse
(
node
).
body
[
0
]
python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py
浏览文件 @
cf8d42bb
...
...
@@ -101,7 +101,11 @@ def test_break_continue_in_for(x):
x
+=
10086
a
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int32'
,
value
=
0
)
for
i
in
range
(
1
,
10
,
1
):
b
=
fluid
.
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'int32'
,
value
=
3
)
# b = 10
# TODO: add Raise Error and suggestion for usage:
# Py for contains break/continue depends on control-flow.
for
i
in
range
(
b
):
if
a
<=
4
:
x
+=
1
a
+=
1
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录