Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
40a77319
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看板
未验证
提交
40a77319
编写于
6月 27, 2022
作者:
A
Aurelius84
提交者:
GitHub
6月 27, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2Stat]Enhance nonlocal machanism while nonlocal vars is empty (#43848)
上级
e6e1c5e7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
45 addition
and
1 deletion
+45
-1
python/paddle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py
...dle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py
+26
-1
python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py
...d/tests/unittests/dygraph_to_static/ifelse_simple_func.py
+12
-0
python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py
...le/fluid/tests/unittests/dygraph_to_static/test_ifelse.py
+7
-0
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/ifelse_transformer.py
浏览文件 @
40a77319
...
@@ -524,7 +524,8 @@ def transform_if_else(node, root):
...
@@ -524,7 +524,8 @@ def transform_if_else(node, root):
if
ARGS_NAME
in
nonlocal_names
:
if
ARGS_NAME
in
nonlocal_names
:
nonlocal_names
.
remove
(
ARGS_NAME
)
nonlocal_names
.
remove
(
ARGS_NAME
)
nonlocal_stmt_node
=
[
create_nonlocal_stmt_node
(
nonlocal_names
)]
nonlocal_stmt_node
=
[
create_nonlocal_stmt_node
(
nonlocal_names
)
]
if
nonlocal_names
else
[]
empty_arg_node
=
gast
.
arguments
(
args
=
[],
empty_arg_node
=
gast
.
arguments
(
args
=
[],
posonlyargs
=
[],
posonlyargs
=
[],
...
@@ -557,8 +558,20 @@ def create_get_args_node(names):
...
@@ -557,8 +558,20 @@ def create_get_args_node(names):
def get_args_0():
def get_args_0():
nonlocal x, y
nonlocal x, y
return x, y
"""
"""
def
empty_node
():
func_def
=
"""
def {func_name}():
return
"""
.
format
(
func_name
=
unique_name
.
generate
(
GET_ARGS_FUNC_PREFIX
))
return
gast
.
parse
(
textwrap
.
dedent
(
func_def
)).
body
[
0
]
assert
isinstance
(
names
,
(
list
,
tuple
))
assert
isinstance
(
names
,
(
list
,
tuple
))
if
not
names
:
return
empty_node
()
template
=
"""
template
=
"""
def {func_name}():
def {func_name}():
nonlocal {vars}
nonlocal {vars}
...
@@ -578,7 +591,19 @@ def create_set_args_node(names):
...
@@ -578,7 +591,19 @@ def create_set_args_node(names):
nonlocal x, y
nonlocal x, y
x, y = __args
x, y = __args
"""
"""
def
empty_node
():
func_def
=
"""
def {func_name}({args}):
pass
"""
.
format
(
func_name
=
unique_name
.
generate
(
SET_ARGS_FUNC_PREFIX
),
args
=
ARGS_NAME
)
return
gast
.
parse
(
textwrap
.
dedent
(
func_def
)).
body
[
0
]
assert
isinstance
(
names
,
(
list
,
tuple
))
assert
isinstance
(
names
,
(
list
,
tuple
))
if
not
names
:
return
empty_node
()
template
=
"""
template
=
"""
def {func_name}({args}):
def {func_name}({args}):
nonlocal {vars}
nonlocal {vars}
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/ifelse_simple_func.py
浏览文件 @
40a77319
...
@@ -28,6 +28,18 @@ def loss_fn(x, lable):
...
@@ -28,6 +28,18 @@ def loss_fn(x, lable):
return
loss
return
loss
def
dyfunc_empty_nonlocal
(
x
):
flag
=
True
if
flag
:
print
(
"It's a test for empty nonlocal stmt"
)
if
paddle
.
mean
(
x
)
<
0
:
x
+
1
out
=
x
*
2
return
out
def
dyfunc_with_if_else
(
x_v
,
label
=
None
):
def
dyfunc_with_if_else
(
x_v
,
label
=
None
):
if
fluid
.
layers
.
mean
(
x_v
).
numpy
()[
0
]
>
5
:
if
fluid
.
layers
.
mean
(
x_v
).
numpy
()[
0
]
>
5
:
x_v
=
x_v
-
1
x_v
=
x_v
-
1
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py
浏览文件 @
40a77319
...
@@ -73,6 +73,13 @@ class TestDygraphIfElse3(TestDygraphIfElse):
...
@@ -73,6 +73,13 @@ class TestDygraphIfElse3(TestDygraphIfElse):
self
.
dyfunc
=
dyfunc_with_if_else3
self
.
dyfunc
=
dyfunc_with_if_else3
class
TestDygraphIfElse4
(
TestDygraphIfElse
):
def
setUp
(
self
):
self
.
x
=
np
.
random
.
random
([
10
,
16
]).
astype
(
'float32'
)
self
.
dyfunc
=
dyfunc_empty_nonlocal
class
TestDygraphIfElseWithListGenerator
(
TestDygraphIfElse
):
class
TestDygraphIfElseWithListGenerator
(
TestDygraphIfElse
):
def
setUp
(
self
):
def
setUp
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录