Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
0f8dc611
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
0f8dc611
编写于
7月 16, 2020
作者:
L
liym27
提交者:
GitHub
7月 16, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2Static] Convert assert stmt with new function `convert_assert`. (#25551)
上级
cf3c51a6
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
23 addition
and
17 deletion
+23
-17
python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py
...dle/fluid/dygraph/dygraph_to_static/assert_transformer.py
+10
-16
python/paddle/fluid/dygraph/dygraph_to_static/convert_operators.py
...ddle/fluid/dygraph/dygraph_to_static/convert_operators.py
+13
-1
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py
浏览文件 @
0f8dc611
...
...
@@ -17,12 +17,12 @@ from __future__ import print_function
import
gast
from
paddle.fluid.dygraph.dygraph_to_static.static_analysis
import
AstNodeWrapper
from
paddle.fluid.dygraph.dygraph_to_static.
static_analysis
import
StaticAnalysisVisitor
from
paddle.fluid.dygraph.dygraph_to_static.
utils
import
ast_to_source_code
class
AssertTransformer
(
gast
.
NodeTransformer
):
"""
A class transforms python assert to
fluid.layers.A
ssert.
A class transforms python assert to
convert_a
ssert.
"""
def
__init__
(
self
,
wrapper_root
):
...
...
@@ -32,21 +32,15 @@ class AssertTransformer(gast.NodeTransformer):
self
.
wrapper_root
=
wrapper_root
self
.
root
=
wrapper_root
.
node
self
.
static_analysis_visitor
=
StaticAnalysisVisitor
(
self
.
root
)
def
transform
(
self
):
self
.
visit
(
self
.
root
)
def
visit_Assert
(
self
,
node
):
if
not
self
.
static_analysis_visitor
.
is_tensor_node
(
node
.
test
):
return
node
cast_node
=
gast
.
Call
(
func
=
gast
.
parse
(
"fluid.layers.cast"
).
body
[
0
].
value
,
args
=
[
node
.
test
,
gast
.
Constant
(
value
=
"bool"
,
kind
=
None
)],
keywords
=
[])
assert_node
=
gast
.
Call
(
func
=
gast
.
parse
(
"fluid.layers.Assert"
).
body
[
0
].
value
,
args
=
[
cast_node
],
keywords
=
[])
return
gast
.
Expr
(
value
=
assert_node
)
convert_assert_node
=
gast
.
parse
(
'fluid.dygraph.dygraph_to_static.convert_operators.convert_assert({test}, {msg})'
.
format
(
test
=
ast_to_source_code
(
node
.
test
),
msg
=
ast_to_source_code
(
node
.
msg
)
if
node
.
msg
else
""
)).
body
[
0
].
value
return
gast
.
Expr
(
value
=
convert_assert_node
)
python/paddle/fluid/dygraph/dygraph_to_static/convert_operators.py
浏览文件 @
0f8dc611
...
...
@@ -15,7 +15,7 @@
from
paddle.fluid.data_feeder
import
convert_dtype
from
paddle.fluid.dygraph.dygraph_to_static.variable_trans_func
import
to_static_variable
from
paddle.fluid.framework
import
Variable
,
core
from
paddle.fluid.layers
import
cast
,
control_flow
,
logical_and
,
logical_not
,
logical_or
,
nn
from
paddle.fluid.layers
import
Assert
,
cast
,
control_flow
,
logical_and
,
logical_not
,
logical_or
,
nn
def
convert_while_loop
(
cond
,
body
,
loop_vars
):
...
...
@@ -259,3 +259,15 @@ def convert_var_dtype(var, dtype):
return
cast
(
var
,
dtype
=
cast_map
[
dtype
])
else
:
return
eval
(
'{}(var)'
.
format
(
dtype
))
def
convert_assert
(
cond
,
message
=
""
):
"""
A function representation of a Python ``assert`` statement.
"""
if
isinstance
(
cond
,
Variable
):
cond
=
cast
(
cond
,
"bool"
)
# NOTE: message is not used because Paddle Assert has no corresponding parameter to use.
return
Assert
(
cond
)
else
:
assert
cond
,
message
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录