Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
6e0aa776
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看板
未验证
提交
6e0aa776
编写于
6月 28, 2022
作者:
A
Aurelius84
提交者:
GitHub
6月 28, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2Stat]Enhance Python if-else by pruning usless no_return variable (#43880)
上级
6cb24967
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
75 addition
and
29 deletion
+75
-29
python/paddle/fluid/dygraph/dygraph_to_static/convert_operators.py
...ddle/fluid/dygraph/dygraph_to_static/convert_operators.py
+47
-29
python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py
...le/fluid/tests/unittests/dygraph_to_static/test_return.py
+28
-0
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/convert_operators.py
浏览文件 @
6e0aa776
...
...
@@ -209,9 +209,44 @@ def convert_ifelse(pred, true_fn, false_fn, get_args, set_args,
out
=
_run_paddle_cond
(
pred
,
true_fn
,
false_fn
,
get_args
,
set_args
,
return_name_ids
)
else
:
out
=
_run_py_ifelse
(
pred
,
true_fn
,
false_fn
)
out
=
_run_py_ifelse
(
pred
,
true_fn
,
false_fn
,
get_args
,
set_args
,
return_name_ids
)
return
_remove_no_value_return_var
(
out
)
return
out
def
_run_paddle_cond
(
pred
,
true_fn
,
false_fn
,
get_args
,
set_args
,
return_name_ids
):
"""
Paddle cond API will evaluate both ture_fn and false_fn codes.
"""
pred
=
cast_bool_if_necessary
(
pred
)
init_args
=
get_args
()
def
new_true_fn
():
set_args
(
init_args
)
outs
=
true_fn
()
_check_no_undefined_var
(
outs
,
return_name_ids
,
'if_body'
)
return
outs
def
new_false_fn
():
set_args
(
init_args
)
outs
=
false_fn
()
_check_no_undefined_var
(
outs
,
return_name_ids
,
'else_body'
)
return
outs
cond_outs
=
control_flow
.
cond
(
pred
,
new_true_fn
,
new_false_fn
)
return
_recover_args_state
(
cond_outs
,
get_args
,
set_args
,
return_name_ids
)
def
_run_py_ifelse
(
pred
,
true_fn
,
false_fn
,
get_args
,
set_args
,
return_name_ids
):
"""
Evaluate python original branch function if-else.
"""
py_outs
=
true_fn
()
if
pred
else
false_fn
()
py_outs
=
_remove_no_value_return_var
(
py_outs
)
return
_recover_args_state
(
py_outs
,
get_args
,
set_args
,
return_name_ids
)
def
_remove_no_value_return_var
(
out
):
...
...
@@ -258,50 +293,33 @@ def _check_no_undefined_var(outs, names, branch_name):
.
format
(
name
,
branch_name
))
def
_run_paddle_cond
(
pred
,
true_fn
,
false_fn
,
get_args
,
set_args
,
return_name_ids
):
def
_recover_args_state
(
outs
,
get_args
,
set_args
,
return_name_ids
):
"""
Paddle cond API will evaluate both ture_fn and false_fn codes.
"""
pred
=
cast_bool_if_necessary
(
pred
)
init_args
=
get_args
()
def
new_true_fn
():
set_args
(
init_args
)
outs
=
true_fn
()
_check_no_undefined_var
(
outs
,
return_name_ids
,
'if_body'
)
return
outs
def
new_false_fn
():
set_args
(
init_args
)
outs
=
false_fn
()
_check_no_undefined_var
(
outs
,
return_name_ids
,
'else_body'
)
return
outs
Currently we support variant length of early return statement by padding
_no_return_value.
cond_outs
=
control_flow
.
cond
(
pred
,
new_true_fn
,
new_false_fn
)
# TODO(dev): We shall consider to evaluate whether should support this for Python if-else?
"""
# IfExpr's return_name_ids maybe None
if
return_name_ids
is
None
:
return
cond_
outs
return
outs
init_args
=
get_args
()
# recover args state
num_outs
=
len
(
return_name_ids
)
num_args
=
1
if
not
isinstance
(
init_args
,
tuple
)
else
len
(
init_args
)
assert
num_outs
<=
num_args
if
num_args
==
1
:
final_outs
=
cond_
outs
final_outs
=
outs
else
:
cond_outs
=
(
cond_outs
,
)
if
num_outs
==
1
else
cond_
outs
final_outs
=
cond_
outs
+
init_args
[
num_outs
:]
outs
=
(
outs
,
)
if
num_outs
==
1
else
outs
final_outs
=
outs
+
init_args
[
num_outs
:]
set_args
(
final_outs
)
return
final_outs
def
_run_py_ifelse
(
pred
,
true_fn
,
false_fn
):
return
true_fn
()
if
pred
else
false_fn
()
def
convert_len
(
var
):
"""
Returns variable(length) from shape ops based on var.type
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py
浏览文件 @
6e0aa776
...
...
@@ -201,6 +201,28 @@ def test_return_without_paddle_cond(x):
return
y
def
two_value
(
x
):
return
x
*
2
,
x
+
1
def
diff_return_hepler
(
x
):
if
False
:
y
=
x
+
1
z
=
x
-
1
return
y
,
z
else
:
return
two_value
(
x
)
@
to_static
def
test_diff_return
(
x
):
x
=
paddle
.
to_tensor
(
x
)
y
,
z
=
diff_return_hepler
(
x
)
if
y
.
shape
[
0
]
>
1
:
y
=
y
+
1
return
y
,
z
class
TestReturnBase
(
unittest
.
TestCase
):
def
setUp
(
self
):
...
...
@@ -255,6 +277,12 @@ class TestReturnIf(TestReturnBase):
self
.
dygraph_func
=
test_return_if
class
TestReturnIfDiff
(
TestReturnBase
):
def
init_dygraph_func
(
self
):
self
.
dygraph_func
=
test_diff_return
class
TestReturnIfElse
(
TestReturnBase
):
def
init_dygraph_func
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录