Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
135b62a4
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看板
未验证
提交
135b62a4
编写于
10月 20, 2020
作者:
A
Aurelius84
提交者:
GitHub
10月 20, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2stat] Refine code of DygraphToStaticAst (#28103)
* refine code of DygraphToStaticAst * add __init__ function
上级
6dd64b0a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
29 addition
and
60 deletion
+29
-60
python/paddle/fluid/dygraph/dygraph_to_static/ast_transformer.py
...paddle/fluid/dygraph/dygraph_to_static/ast_transformer.py
+29
-60
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/ast_transformer.py
浏览文件 @
135b62a4
...
...
@@ -47,6 +47,9 @@ class DygraphToStaticAst(gast.NodeTransformer):
Main class to transform Dygraph to Static Graph
"""
def
__init__
(
self
):
self
.
translator_logger
=
logging_utils
.
TranslatorLogger
()
def
get_static_ast
(
self
,
root
):
# save root for some analysis may need global AST
self
.
root
=
root
...
...
@@ -57,71 +60,37 @@ class DygraphToStaticAst(gast.NodeTransformer):
self
.
transfer_from_node_type
(
self
.
static_analysis_root
)
return
self
.
static_analysis_root
def
_apply
(
self
,
transformer
,
node_wrapper
,
log_level
):
transformer
(
node_wrapper
).
transform
()
self
.
translator_logger
.
log_transformed_code
(
log_level
,
self
.
root
,
transformer
.
__name__
)
def
transfer_from_node_type
(
self
,
node_wrapper
):
translator_logger
=
logging_utils
.
TranslatorLogger
()
translator_logger
.
log
(
self
.
translator_logger
.
log
(
1
,
"Source code:
\n
{}"
.
format
(
ast_to_source_code
(
self
.
root
)))
# Generic transformation
self
.
visit
(
node_wrapper
.
node
)
# Transform basic api of dygraph to static graph and get feed_name_to_arg_name
BasicApiTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
1
,
self
.
root
,
"BasicApiTransformer"
)
# Transform Tensor.shape into fluid.layers.shape(Tensor)
TensorShapeTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
2
,
self
.
root
,
"TensorShapeTransformer"
)
# Transform list used in control flow
ListTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
3
,
self
.
root
,
"ListTransformer"
)
# Transform break/continue in loops
BreakContinueTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
4
,
self
.
root
,
"BreakContinueTransformer"
)
# Transform return in functions
ReturnTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
5
,
self
.
root
,
"ReturnTransformer"
)
# Transform logical and/or/not
LogicalTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
6
,
self
.
root
,
"LogicalTransformer"
)
# Transform for loop and while loop
LoopTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
7
,
self
.
root
,
"LoopTransformer"
)
# Transform all if/else statement of Dygraph into Static Graph.
IfElseTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
8
,
self
.
root
,
"IfElseTransformer"
)
# Transform python assert statement
AssertTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
9
,
self
.
root
,
"AssertTransformer"
)
# Transform all python print statement
PrintTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
10
,
self
.
root
,
"PrintTransformer"
)
# Transform call recursively
CallTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
11
,
self
.
root
,
"CallTransformer"
)
# Transform python type casting statement
CastTransformer
(
node_wrapper
).
transform
()
translator_logger
.
log_transformed_code
(
12
,
self
.
root
,
"CastTransformer"
)
translator_logger
.
log_transformed_code
(
logging_utils
.
LOG_AllTransformer
,
self
.
root
,
"All Transformers"
)
transformers
=
[
BasicApiTransformer
,
# Basic Api
TensorShapeTransformer
,
# Tensor.shape -> layers.shape(Tensor)
ListTransformer
,
# List used in control flow
BreakContinueTransformer
,
# break/continue in loops
ReturnTransformer
,
# return in functions
LogicalTransformer
,
# logical and/or/not
LoopTransformer
,
# for/while -> while_op
IfElseTransformer
,
# if/else -> cond_op
AssertTransformer
,
# assert statement
PrintTransformer
,
# print statement
CallTransformer
,
# transform call recursively
CastTransformer
,
# type casting statement
]
for
index
,
transformer
in
enumerate
(
transformers
):
self
.
_apply
(
transformer
,
node_wrapper
,
log_level
=
index
+
1
)
self
.
translator_logger
.
log_transformed_code
(
logging_utils
.
LOG_AllTransformer
,
self
.
root
,
"All Transformers"
)
def
visit_FunctionDef
(
self
,
node
):
if
self
.
decorate_func_name
is
None
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录