Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
f8205ffa
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
f8205ffa
编写于
3月 29, 2020
作者:
A
Aurelius84
提交者:
GitHub
3月 29, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix conflict var bug in loop_transformer test=develop (#23287)
上级
16e74f11
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
51 addition
and
1 deletion
+51
-1
python/paddle/fluid/dygraph/dygraph_to_static/loop_transformer.py
...addle/fluid/dygraph/dygraph_to_static/loop_transformer.py
+29
-1
python/paddle/fluid/tests/unittests/dygraph_to_static/test_loop.py
...ddle/fluid/tests/unittests/dygraph_to_static/test_loop.py
+22
-0
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/loop_transformer.py
浏览文件 @
f8205ffa
...
@@ -125,6 +125,11 @@ class NameVisitor(gast.NodeVisitor):
...
@@ -125,6 +125,11 @@ class NameVisitor(gast.NodeVisitor):
# List of gast.While/gast.For nodes
# List of gast.While/gast.For nodes
self
.
current_loop
=
[]
self
.
current_loop
=
[]
# List of nodes that have scope of variables.
self
.
nodes_with_scope
=
[]
self
.
blacklist_names
=
{
"False"
,
"True"
,
"None"
}
# Mapping from gast.While/gast.For to variable nodes
# Mapping from gast.While/gast.For to variable nodes
self
.
before_loop_body_vars
=
defaultdict
(
set
)
self
.
before_loop_body_vars
=
defaultdict
(
set
)
self
.
in_loop_vars
=
defaultdict
(
set
)
self
.
in_loop_vars
=
defaultdict
(
set
)
...
@@ -169,7 +174,7 @@ class NameVisitor(gast.NodeVisitor):
...
@@ -169,7 +174,7 @@ class NameVisitor(gast.NodeVisitor):
if
self
.
_is_call_func_name_node
(
node
):
if
self
.
_is_call_func_name_node
(
node
):
self
.
generic_visit
(
node
)
self
.
generic_visit
(
node
)
return
return
if
node
.
id
==
"False"
or
node
.
id
==
"True"
or
node
.
id
==
"None"
:
if
node
.
id
in
self
.
blacklist_names
:
self
.
generic_visit
(
node
)
self
.
generic_visit
(
node
)
return
return
...
@@ -178,6 +183,19 @@ class NameVisitor(gast.NodeVisitor):
...
@@ -178,6 +183,19 @@ class NameVisitor(gast.NodeVisitor):
self
.
in_loop_vars
[
loop_node
].
add
(
node
)
self
.
in_loop_vars
[
loop_node
].
add
(
node
)
self
.
generic_visit
(
node
)
self
.
generic_visit
(
node
)
def
visit_FunctionDef
(
self
,
node
):
self
.
nodes_with_scope
.
append
(
node
)
self
.
blacklist_names
.
add
(
node
.
name
)
# The variables in the function are not visible to the outside scope.
before_func_seen_vars
=
copy
.
copy
(
self
.
current_seen_vars
)
self
.
generic_visit
(
node
)
self
.
nodes_with_scope
.
pop
()
# After exiting the scope of the node, variables in this scope
# should be removed from self.current_seen_vars.
if
self
.
nodes_with_scope
:
self
.
current_seen_vars
=
before_func_seen_vars
def
visit
(
self
,
node
):
def
visit
(
self
,
node
):
method
=
'visit_'
+
node
.
__class__
.
__name__
method
=
'visit_'
+
node
.
__class__
.
__name__
visitor
=
getattr
(
self
,
method
,
self
.
generic_visit
)
visitor
=
getattr
(
self
,
method
,
self
.
generic_visit
)
...
@@ -188,6 +206,16 @@ class NameVisitor(gast.NodeVisitor):
...
@@ -188,6 +206,16 @@ class NameVisitor(gast.NodeVisitor):
if
self
.
_is_call_func_name_node
(
node
):
if
self
.
_is_call_func_name_node
(
node
):
return
return
attr_full_name
=
get_attribute_full_name
(
node
)
attr_full_name
=
get_attribute_full_name
(
node
)
# Class variables are not allowed to appear in the arguments list
# of defined function under class methods in Python.
"""
def class_func(self):
def while_loop_body(self.x, y) # `self.x` is illegal.
"""
# TODO: If do change the variable with `self.var`, need a better
# way to deal with this case.
if
attr_full_name
.
startswith
(
"self."
):
return
self
.
current_seen_vars
.
add
(
node
)
self
.
current_seen_vars
.
add
(
node
)
for
loop_node
in
self
.
current_loop
:
for
loop_node
in
self
.
current_loop
:
self
.
in_loop_vars
[
loop_node
].
add
(
node
)
self
.
in_loop_vars
[
loop_node
].
add
(
node
)
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/test_loop.py
浏览文件 @
f8205ffa
...
@@ -35,6 +35,23 @@ def while_loop_dyfunc(x):
...
@@ -35,6 +35,23 @@ def while_loop_dyfunc(x):
return
i
return
i
def
while_loop_dyfun_with_conflict_var
(
x
):
i
=
fluid
.
dygraph
.
to_variable
(
x
)
def
relu
(
y
):
# 'y' is not visible outside the scope.
return
fluid
.
layers
.
relu
(
y
)
while
x
<
10
:
# If a tmp variable is created which has same name
# with a argument in function, it should not be
# included in the loop_vars.
add_fn
=
lambda
x
,
y
:
x
+
y
i
=
add_fn
(
i
,
x
)
x
=
x
+
1
return
i
def
while_loop_dyfunc_with_none
(
x
):
def
while_loop_dyfunc_with_none
(
x
):
i
=
fluid
.
dygraph
.
to_variable
(
x
)
\
i
=
fluid
.
dygraph
.
to_variable
(
x
)
\
if
x
is
not
None
\
if
x
is
not
None
\
...
@@ -131,6 +148,11 @@ class TestTransformWhileLoop(unittest.TestCase):
...
@@ -131,6 +148,11 @@ class TestTransformWhileLoop(unittest.TestCase):
# self.assertTrue(np.allclose(self._run_dygraph(), self._run_static()))
# self.assertTrue(np.allclose(self._run_dygraph(), self._run_static()))
class
TestTransformWhileLoopWithConflicVar
(
TestTransformWhileLoop
):
def
_init_dyfunc
(
self
):
self
.
dyfunc
=
while_loop_dyfun_with_conflict_var
class
TestTransformWhileLoopWithNone
(
TestTransformWhileLoop
):
class
TestTransformWhileLoopWithNone
(
TestTransformWhileLoop
):
def
_init_dyfunc
(
self
):
def
_init_dyfunc
(
self
):
self
.
dyfunc
=
while_loop_dyfunc_with_none
self
.
dyfunc
=
while_loop_dyfunc_with_none
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录