Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
26bc953b
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看板
未验证
提交
26bc953b
编写于
3月 19, 2020
作者:
A
Aurelius84
提交者:
GitHub
3月 19, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix returned arguments in IfElse_fn test=develop (#23102)
上级
0d8f40d2
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
93 addition
and
42 deletion
+93
-42
python/paddle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py
...dle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py
+46
-22
python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py
.../tests/unittests/dygraph_to_static/test_break_continue.py
+12
-13
python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse_basic.py
...id/tests/unittests/dygraph_to_static/test_ifelse_basic.py
+35
-7
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py
浏览文件 @
26bc953b
...
...
@@ -388,20 +388,20 @@ class IfConditionVisitor(object):
class
NameVisitor
(
gast
.
NodeVisitor
):
def
__init__
(
self
,
node_black_set
=
None
):
#
Set of nodes that will not be visited
.
self
.
node_black_set
=
node_black_set
or
set
()
def
__init__
(
self
,
end_node
=
None
):
#
The terminate node of the visitor
.
self
.
end_node
=
end_node
# Dict to store the names and ctxs of vars.
self
.
name_ids
=
defaultdict
(
list
)
# List of current visited nodes
self
.
ancestor_nodes
=
[]
# Available only when
node_black_set
is set.
# Available only when
end_node
is set.
self
.
_is_finished
=
False
self
.
_candidate_ctxs
=
(
gast
.
Store
,
gast
.
Load
,
gast
.
Param
)
def
visit
(
self
,
node
):
"""Visit a node."""
if
node
in
self
.
node_black_set
or
self
.
_is_finished
:
if
node
==
self
.
end_node
or
self
.
_is_finished
:
self
.
_is_finished
=
True
return
...
...
@@ -433,21 +433,28 @@ class NameVisitor(gast.NodeVisitor):
In above two cases, we should consider to manage the scope of vars to parsing
the arguments and returned vars correctly.
"""
before_if_name_ids
=
copy
.
deepcopy
(
self
.
name_ids
)
body_name_ids
=
self
.
_visit_child
(
node
.
body
)
# If the traversal process stops early, just return the name_ids that have been seen.
if
self
.
_is_finished
:
for
name_id
,
ctxs
in
before_if_name_ids
.
items
():
self
.
name_ids
[
name_id
]
=
ctxs
+
self
.
name_ids
[
name_id
]
# Blocks the vars in `if.body` and only inserts the vars both created in 'if/else' branch
# into name_ids.
if
not
self
.
end_node
:
self
.
generic_visit
(
node
)
else
:
else_name_ids
=
self
.
_visit_child
(
node
.
orelse
)
new_name_ids
=
self
.
_find_new_name_ids
(
body_name_ids
,
else_name_ids
)
for
new_name_id
in
new_name_ids
:
before_if_name_ids
[
new_name_id
].
append
(
gast
.
Store
())
self
.
name_ids
=
before_if_name_ids
before_if_name_ids
=
copy
.
deepcopy
(
self
.
name_ids
)
body_name_ids
=
self
.
_visit_child
(
node
.
body
)
# If traversal process stops early in `if.body`, return the currently seen name_ids.
if
self
.
_is_finished
:
self
.
_update_name_ids
(
before_if_name_ids
)
else
:
else_name_ids
=
self
.
_visit_child
(
node
.
orelse
)
# If traversal process stops early in `if.orelse`, return the currently seen name_ids.
if
self
.
_is_finished
:
self
.
_update_name_ids
(
before_if_name_ids
)
else
:
# Blocks the vars in `if.body` and only inserts the vars both created in 'if/else' branch
# into name_ids.
new_name_ids
=
self
.
_find_new_name_ids
(
body_name_ids
,
else_name_ids
)
for
new_name_id
in
new_name_ids
:
before_if_name_ids
[
new_name_id
].
append
(
gast
.
Store
())
self
.
name_ids
=
before_if_name_ids
def
visit_Attribute
(
self
,
node
):
if
not
self
.
_is_call_func_name_node
(
node
):
...
...
@@ -463,6 +470,19 @@ class NameVisitor(gast.NodeVisitor):
node
.
_fields
=
(
'value'
,
'targets'
)
self
.
generic_visit
(
node
)
def
visit_FunctionDef
(
self
,
node
):
if
not
self
.
end_node
:
self
.
generic_visit
(
node
)
else
:
before_name_ids
=
copy
.
deepcopy
(
self
.
name_ids
)
self
.
name_ids
=
defaultdict
(
list
)
self
.
generic_visit
(
node
)
if
self
.
_is_finished
:
self
.
_update_name_ids
(
before_name_ids
)
else
:
self
.
name_ids
=
before_name_ids
def
visit_Return
(
self
,
node
):
# Ignore the vars in return
return
...
...
@@ -505,12 +525,16 @@ class NameVisitor(gast.NodeVisitor):
return
True
return
False
def
_update_name_ids
(
self
,
new_name_ids
):
for
name_id
,
ctxs
in
new_name_ids
.
items
():
self
.
name_ids
[
name_id
]
=
ctxs
+
self
.
name_ids
[
name_id
]
def
get_name_ids
(
nodes
,
node_black_set
=
None
):
def
get_name_ids
(
nodes
,
end_node
=
None
):
"""
Return all ast.Name.id of python variable in nodes.
"""
name_visitor
=
NameVisitor
(
node_black_set
)
name_visitor
=
NameVisitor
(
end_node
)
for
node
in
nodes
:
name_visitor
.
visit
(
node
)
return
name_visitor
.
name_ids
...
...
@@ -611,7 +635,7 @@ def transform_if_else(node, root):
"""
Transform ast.If into control flow statement of Paddle static graph.
"""
parent_name_ids
=
get_name_ids
([
root
],
node_black_set
=
[
node
]
)
parent_name_ids
=
get_name_ids
([
root
],
end_node
=
node
)
if_name_ids
=
get_name_ids
(
node
.
body
)
else_name_ids
=
get_name_ids
(
node
.
orelse
)
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py
浏览文件 @
26bc953b
...
...
@@ -102,19 +102,18 @@ def test_break_continue_in_for(x):
def
test_for_in_else
(
x
):
x
=
fluid
.
dygraph
.
to_variable
(
x
)
#
# TODO: Huihuang founds that if we put the for range in else body
# the testcase will fail. Enable this test case after fixing it.
#
#if False:
# pass
#else:
# for i in range(0, 10):
# if i > 5:
# x += 1
# break
# x += i
#
# Case 1:
if
False
:
pass
else
:
for
i
in
range
(
0
,
10
):
if
i
>
5
:
x
+=
1
break
x
+=
i
# Case 2:
if
False
:
pass
else
:
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse_basic.py
浏览文件 @
26bc953b
...
...
@@ -65,10 +65,24 @@ class TestGetNameIds2(TestGetNameIds):
return z
"""
self
.
all_name_ids
=
{
'x'
:
[
gast
.
Param
(),
gast
.
Store
()],
'a'
:
[
gast
.
Store
(),
gast
.
Load
()],
'y'
:
[
gast
.
Param
(),
gast
.
Load
()],
'z'
:
[
gast
.
Store
()]
'x'
:
[
gast
.
Param
(),
gast
.
Store
(),
gast
.
Load
(),
gast
.
Load
(),
gast
.
Load
()
],
'a'
:
[
gast
.
Store
(),
gast
.
Load
(),
gast
.
Load
()],
'y'
:
[
gast
.
Param
(),
gast
.
Load
(),
gast
.
Load
(),
gast
.
Load
(),
gast
.
Load
(),
],
'z'
:
[
gast
.
Store
(),
gast
.
Load
(),
gast
.
Store
(),
gast
.
Store
(),
]
}
...
...
@@ -83,9 +97,23 @@ class TestGetNameIds3(TestGetNameIds):
return z
"""
self
.
all_name_ids
=
{
'x'
:
[
gast
.
Param
()],
'y'
:
[
gast
.
Param
()],
'z'
:
[
gast
.
Store
()]
'x'
:
[
gast
.
Param
(),
gast
.
Load
(),
gast
.
Load
(),
gast
.
Load
(),
],
'y'
:
[
gast
.
Param
(),
gast
.
Load
(),
gast
.
Load
(),
],
'z'
:
[
gast
.
Store
(),
gast
.
Store
(),
gast
.
Load
(),
gast
.
Store
(),
]
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录