Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
33ff833a
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看板
未验证
提交
33ff833a
编写于
9月 11, 2020
作者:
C
Chen Weihang
提交者:
GitHub
9月 11, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix loaded no params layer run error (#27241)
上级
f1ab2882
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
78 addition
and
15 deletion
+78
-15
paddle/fluid/operators/run_program_op.cc
paddle/fluid/operators/run_program_op.cc
+2
-8
paddle/fluid/operators/run_program_op.h
paddle/fluid/operators/run_program_op.h
+6
-1
python/paddle/fluid/dygraph/io.py
python/paddle/fluid/dygraph/io.py
+9
-5
python/paddle/fluid/tests/unittests/test_jit_save_load.py
python/paddle/fluid/tests/unittests/test_jit_save_load.py
+61
-1
未找到文件。
paddle/fluid/operators/run_program_op.cc
浏览文件 @
33ff833a
...
...
@@ -27,9 +27,6 @@ class RunProgramOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
ctx
->
HasInputs
(
"X"
),
true
,
platform
::
errors
::
NotFound
(
"Input(X) of RunProgramOp should not be null."
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasInputs
(
"Params"
),
true
,
platform
::
errors
::
NotFound
(
"Input(Params) of RunProgramOp should not be null."
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasOutputs
(
"Out"
),
true
,
platform
::
errors
::
NotFound
(
"Output(Out) of RunProgramOp should not be null."
));
...
...
@@ -73,7 +70,8 @@ class RunProgramOpMaker : public framework::OpProtoAndCheckerMaker {
"(vector<LoDTensor or SelecetedRows>)"
"The input parameter of RunProgram operator, also the parameters "
"of the loaded program."
)
.
AsDuplicable
();
.
AsDuplicable
()
.
AsDispensable
();
AddOutput
(
"Out"
,
"(vector<LoDTensor>)"
"The output tensors of RunProgram operator, also the fetch "
...
...
@@ -121,10 +119,6 @@ class RunProgramGradOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE_EQ
(
ctx
->
HasInputs
(
"X"
),
true
,
platform
::
errors
::
NotFound
(
"Input(X) of RunProgramGradOp should not be null."
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasInputs
(
"Params"
),
true
,
platform
::
errors
::
NotFound
(
"Input(Params) of RunProgramGradOp should not be null."
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasInputs
(
framework
::
GradVarName
(
"Out"
)),
true
,
platform
::
errors
::
NotFound
(
...
...
paddle/fluid/operators/run_program_op.h
浏览文件 @
33ff833a
...
...
@@ -209,9 +209,14 @@ class RunProgramOpKernel : public framework::OpKernel<T> {
auto
output_vars
=
ctx
.
MultiOutputVar
(
"Out"
);
auto
input_var_names
=
ctx
.
InputNames
(
"X"
);
auto
param_names
=
ctx
.
InputNames
(
"Params"
);
auto
output_var_names
=
ctx
.
OutputNames
(
"Out"
);
// current program may not hold parameters
std
::
vector
<
std
::
string
>
param_names
;
if
(
!
param_vars
.
empty
())
{
param_names
=
ctx
.
InputNames
(
"Params"
);
}
auto
*
block
=
ctx
.
Attr
<
BlockDesc
*>
(
"global_block"
);
auto
*
program
=
block
->
Program
();
auto
start_op_index
=
ctx
.
Attr
<
int64_t
>
(
"start_op_index"
);
...
...
python/paddle/fluid/dygraph/io.py
浏览文件 @
33ff833a
...
...
@@ -479,11 +479,15 @@ def _load_persistable_vars(model_path,
var_file_path
=
os
.
path
.
join
(
model_path
,
params_filename
)
else
:
var_file_path
=
os
.
path
.
join
(
model_path
,
VARIABLE_FILENAME
)
framework
.
_dygraph_tracer
().
trace_op
(
type
=
'load_combine'
,
inputs
=
{},
outputs
=
{
'Out'
:
load_var_list
},
attrs
=
{
'file_path'
:
var_file_path
})
if
not
os
.
path
.
exists
(
var_file_path
):
if
len
(
extra_var_info
)
!=
0
:
raise
ValueError
(
"The model to be loaded is incomplete."
)
else
:
framework
.
_dygraph_tracer
().
trace_op
(
type
=
'load_combine'
,
inputs
=
{},
outputs
=
{
'Out'
:
load_var_list
},
attrs
=
{
'file_path'
:
var_file_path
})
return
load_var_dict
...
...
python/paddle/fluid/tests/unittests/test_jit_save_load.py
浏览文件 @
33ff833a
...
...
@@ -23,7 +23,7 @@ from paddle.static import InputSpec
import
paddle.fluid
as
fluid
from
paddle.fluid.dygraph
import
Linear
from
paddle.fluid.dygraph
import
declarative
,
ProgramTranslator
from
paddle.fluid.dygraph.io
import
EXTRA_VAR_INFO_FILENAME
from
paddle.fluid.dygraph.io
import
EXTRA_VAR_INFO_FILENAME
,
VARIABLE_FILENAME
BATCH_SIZE
=
32
BATCH_NUM
=
10
...
...
@@ -153,6 +153,24 @@ class LinearNetReturnHidden(fluid.dygraph.Layer):
return
y
,
loss
class
EmptyLayer
(
paddle
.
nn
.
Layer
):
def
__init__
(
self
):
super
(
EmptyLayer
,
self
).
__init__
()
@
paddle
.
jit
.
to_static
def
forward
(
self
,
x
):
return
x
class
NoParamLayer
(
paddle
.
nn
.
Layer
):
def
__init__
(
self
):
super
(
NoParamLayer
,
self
).
__init__
()
@
paddle
.
jit
.
to_static
def
forward
(
self
,
x
,
y
):
return
x
+
y
def
train
(
layer
,
input_size
=
784
,
label_size
=
1
):
# create optimizer
sgd
=
fluid
.
optimizer
.
SGDOptimizer
(
...
...
@@ -273,6 +291,15 @@ class TestJitSaveLoad(unittest.TestCase):
with
self
.
assertRaises
(
ValueError
):
model_dict
,
_
=
fluid
.
dygraph
.
load_dygraph
(
model_path
)
def
test_jit_load_model_incomplete
(
self
):
model_path
=
"model.test_jit_save_load.remove_variables"
self
.
train_and_save_model
(
model_path
=
model_path
)
# remove `__variables__`
var_path
=
os
.
path
.
join
(
model_path
,
VARIABLE_FILENAME
)
os
.
remove
(
var_path
)
with
self
.
assertRaises
(
ValueError
):
paddle
.
jit
.
load
(
model_path
)
class
TestSaveLoadWithInputSpec
(
unittest
.
TestCase
):
def
setUp
(
self
):
...
...
@@ -695,5 +722,38 @@ class TestJitSaveMultiCases(unittest.TestCase):
configs
=
configs
)
class
TestJitSaveLoadEmptyLayer
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
model_path
=
"model.jit_save_load_empty_layer"
# enable dygraph mode
paddle
.
disable_static
()
def
test_save_load_empty_layer
(
self
):
layer
=
EmptyLayer
()
x
=
paddle
.
to_variable
(
np
.
random
.
random
((
10
)).
astype
(
'float32'
))
out
=
layer
(
x
)
paddle
.
jit
.
save
(
layer
,
self
.
model_path
)
load_layer
=
paddle
.
jit
.
load
(
self
.
model_path
)
load_out
=
load_layer
(
x
)
self
.
assertTrue
(
np
.
array_equal
(
out
,
load_out
))
class
TestJitSaveLoadNoParamLayer
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
model_path
=
"model.jit_save_load_no_param_layer"
# enable dygraph mode
paddle
.
disable_static
()
def
test_save_load_no_param_layer
(
self
):
layer
=
NoParamLayer
()
x
=
paddle
.
to_variable
(
np
.
random
.
random
((
5
)).
astype
(
'float32'
))
y
=
paddle
.
to_variable
(
np
.
random
.
random
((
5
)).
astype
(
'float32'
))
out
=
layer
(
x
,
y
)
paddle
.
jit
.
save
(
layer
,
self
.
model_path
)
load_layer
=
paddle
.
jit
.
load
(
self
.
model_path
)
load_out
=
load_layer
(
x
,
y
)
self
.
assertTrue
(
np
.
array_equal
(
out
,
load_out
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录