Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
b290420f
P
Paddle
项目概览
Crayon鑫
/
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看板
未验证
提交
b290420f
编写于
3月 07, 2020
作者:
L
liym27
提交者:
GitHub
3月 07, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bug in the transformation from to_variable to assign. test=develop (#22885)
上级
ca9c8b41
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
49 addition
and
20 deletion
+49
-20
python/paddle/fluid/dygraph/dygraph_to_static/ast_transformer.py
...paddle/fluid/dygraph/dygraph_to_static/ast_transformer.py
+11
-6
python/paddle/fluid/dygraph/dygraph_to_static/utils.py
python/paddle/fluid/dygraph/dygraph_to_static/utils.py
+20
-4
python/paddle/fluid/tests/unittests/test_dygraph_to_static_basic_api_transformation.py
...ttests/test_dygraph_to_static_basic_api_transformation.py
+18
-10
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/ast_transformer.py
浏览文件 @
b290420f
...
...
@@ -359,15 +359,20 @@ class BasicApiTransformer(gast.NodeTransformer):
def
_update_feed_dict
(
self
,
node
):
assert
isinstance
(
node
,
gast
.
Call
)
va
r_nam
e
=
None
va
lue_nod
e
=
None
for
kw
in
node
.
keywords
:
if
kw
.
arg
==
'value'
:
va
r_name
=
kw
.
value
.
id
# eg: 'a'
for "value=a "
if
not
va
r_nam
e
:
va
r_name
=
node
.
args
[
0
].
id
va
lue_node
=
kw
.
value
# eg: `a`
for "value=a "
if
not
va
lue_nod
e
:
va
lue_node
=
node
.
args
[
0
]
if
not
isinstance
(
value_node
,
gast
.
Name
):
return
else
:
var_name
=
value_node
.
id
feed_var_name
=
unique_name
.
generate
(
var_name
)
# eg: "a_0"
self
.
feed_name_to_arg_id
[
feed_var_name
]
=
var_name
# eg: "a_0" : "a"
self
.
feed_name_to_arg_id
[
feed_var_name
]
=
var_name
# eg: "a_0" : "a"
def
get_feed_name_to_arg_id
(
self
):
return
self
.
feed_name_to_arg_id
...
...
python/paddle/fluid/dygraph/dygraph_to_static/utils.py
浏览文件 @
b290420f
...
...
@@ -150,11 +150,27 @@ def to_static_ast(node, class_node):
return
node
def
to_assign_node
(
ori_node
):
assert
isinstance
(
ori_node
,
gast
.
Call
)
def
to_assign_node
(
node
):
# Transform dygraph api `fluid.dygraph.to_variable` to static api `fluid.layers.assign`.
# NOTE:
# 1. Api `to_variable` supports data type {float16, float32, float64, int16, int32, int64, uint8, uint16},
# but api `assign` only supports {float32, float64, int32, int64, bool};
# 2. If the input of api `assign` is numpy.ndarray, its size cannot be greater than 1024 * 1024.
assert
isinstance
(
node
,
gast
.
Call
)
assign_api
=
gast
.
parse
(
'fluid.layers.assign'
).
body
[
0
].
value
ori_node
.
func
=
assign_api
return
ori_node
node
.
func
=
assign_api
if
node
.
args
:
node
.
args
=
[
node
.
args
[
0
]]
node
.
keywords
=
[]
else
:
for
idx
,
kw
in
enumerate
(
node
.
keywords
):
if
kw
.
arg
==
'value'
:
node
.
keywords
[
idx
].
arg
=
'input'
node
.
keywords
=
[
node
.
keywords
[
idx
]]
node
.
args
=
[]
break
return
node
def
update_args_of_func
(
node
,
dygraph_node
,
method_name
):
...
...
python/paddle/fluid/tests/unittests/test_dygraph_to_static_basic_api_transformation.py
浏览文件 @
b290420f
...
...
@@ -28,19 +28,25 @@ np.random.seed(SEED)
def
dyfunc_to_variable
(
x
):
res
=
fluid
.
dygraph
.
to_variable
(
x
)
res
=
fluid
.
dygraph
.
to_variable
(
x
,
name
=
None
,
zero_copy
=
None
)
return
res
def
dyfunc_to_variable_2
(
x
):
res
=
fluid
.
dygraph
.
to_variable
(
value
=
np
.
zeros
(
shape
=
(
1
),
dtype
=
np
.
int32
))
return
res
class
TestDygraphBasicApi_ToVariable
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
input
=
np
.
ones
(
5
).
astype
(
"int32"
)
self
.
dygraph_func
=
dyfunc_to_variable
self
.
test_funcs
=
[
dyfunc_to_variable
,
dyfunc_to_variable_2
]
self
.
place
=
fluid
.
CUDAPlace
(
0
)
if
fluid
.
is_compiled_with_cuda
(
)
else
fluid
.
CPUPlace
()
def
get_dygraph_output
(
self
):
with
fluid
.
dygraph
.
guard
():
res
=
self
.
dygraph_func
(
self
.
input
).
numpy
()
return
res
def
get_static_output
(
self
):
...
...
@@ -49,12 +55,14 @@ class TestDygraphBasicApi_ToVariable(unittest.TestCase):
with
fluid
.
program_guard
(
main_program
):
static_out
=
dygraph_to_static_graph
(
self
.
dygraph_func
)(
self
.
input
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
()
)
exe
=
fluid
.
Executor
(
self
.
place
)
static_res
=
exe
.
run
(
main_program
,
fetch_list
=
static_out
)
return
static_res
[
0
]
def
test_transformed_static_result
(
self
):
for
func
in
self
.
test_funcs
:
self
.
dygraph_func
=
func
dygraph_res
=
self
.
get_dygraph_output
()
static_res
=
self
.
get_static_output
()
self
.
assertTrue
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录