Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
hapi
提交
e103f756
H
hapi
项目概览
PaddlePaddle
/
hapi
通知
11
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
7
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hapi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
7
合并请求
7
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e103f756
编写于
1月 07, 2020
作者:
Y
Yang Zhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Lazy load optimizer state in static graph mode
上级
887474e9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
36 addition
and
25 deletion
+36
-25
model.py
model.py
+36
-25
未找到文件。
model.py
浏览文件 @
e103f756
...
...
@@ -122,6 +122,8 @@ class StaticGraphAdapter(object):
self
.
_progs
=
{}
self
.
_compiled_progs
=
{}
self
.
_lazy_load_optimizer
=
None
# parse shape hints
self
.
_input_desc
=
OrderedDict
([
(
n
,
None
)
for
n
in
extract_args
(
self
.
model
.
forward
)
if
n
!=
'self'
...
...
@@ -188,20 +190,6 @@ class StaticGraphAdapter(object):
with
open
(
path
,
'rb'
)
as
f
:
return
pickle
.
load
(
f
)
def
set_var
(
var
,
ndarray
):
t
=
global_scope
().
find_var
(
var
.
name
).
get_tensor
()
p
=
t
.
_place
()
if
p
.
is_cpu_place
():
place
=
fluid
.
CPUPlace
()
elif
p
.
is_cuda_pinned_place
():
place
=
fluid
.
CUDAPinnedPlace
()
else
:
p
=
fluid
.
core
.
Place
()
p
.
set_place
(
t
.
_place
())
place
=
fluid
.
CUDAPlace
(
p
.
gpu_device_id
())
t
.
set
(
ndarray
,
place
)
param_path
=
path
+
".pdparams"
param_state
=
_load
(
param_path
)
assert
param_state
,
"failed to load parameters, please check path"
...
...
@@ -218,16 +206,11 @@ class StaticGraphAdapter(object):
assert
key
in
param_state
,
\
"parameter [{}] is not found in model file [{}]"
.
format
(
key
,
param_path
)
set_var
(
var
,
param_state
[
key
])
se
lf
.
_se
t_var
(
var
,
param_state
[
key
])
# FIXME what if a different optimizer is used?
if
not
self
.
model
.
_optimizer
:
return
prog
=
self
.
_progs
.
get
(
'train'
,
None
)
optim
=
list
(
filter
(
is_belong_to_optimizer
,
prog
.
list_vars
()))
if
not
optim
:
return
optim_path
=
path
+
".pdopt"
optim_state
=
_load
(
optim_path
)
if
optim_state
is
None
:
...
...
@@ -235,14 +218,38 @@ class StaticGraphAdapter(object):
assert
'__static_graph_only__'
in
optim_state
,
\
"optimizer saved in dygraph mode is not usable in static graph"
if
self
.
_executor
is
not
None
:
self
.
_load_optimizer
(
optim_state
)
else
:
self
.
_lazy_load_optimizer
=
optim_state
def
_load_optimizer
(
self
,
state
):
prog
=
self
.
_progs
.
get
(
'train'
,
None
)
optim
=
list
(
filter
(
is_belong_to_optimizer
,
prog
.
list_vars
()))
if
not
optim
:
return
fluid
.
core
.
_create_loaded_parameter
(
optim
,
global_scope
(),
executor
)
optim
,
global_scope
(),
self
.
_executor
.
_default_
executor
)
for
var
in
optim
:
assert
var
.
name
in
optim_state
,
\
"variable [{}] is not found in model file [{}]"
.
format
(
var
.
name
,
optim_path
)
set_var
(
var
,
optim_state
[
var
.
name
])
assert
var
.
name
in
state
,
\
"variable [{}] is not in optimizer state file"
.
format
(
var
.
name
)
self
.
_set_var
(
var
,
state
[
var
.
name
])
def
_set_var
(
self
,
var
,
ndarray
):
t
=
global_scope
().
find_var
(
var
.
name
).
get_tensor
()
p
=
t
.
_place
()
if
p
.
is_cpu_place
():
place
=
fluid
.
CPUPlace
()
elif
p
.
is_cuda_pinned_place
():
place
=
fluid
.
CUDAPinnedPlace
()
else
:
p
=
fluid
.
core
.
Place
()
p
.
set_place
(
t
.
_place
())
place
=
fluid
.
CUDAPlace
(
p
.
gpu_device_id
())
t
.
set
(
ndarray
,
place
)
def
_run
(
self
,
inputs
,
labels
=
None
,
device
=
'CPU'
,
device_ids
=
None
):
inputs
=
to_list
(
inputs
)
...
...
@@ -349,6 +356,10 @@ class StaticGraphAdapter(object):
startup_prog
=
self
.
_startup_prog
.
_prune
(
uninitialized
)
self
.
_executor
.
run
(
startup_prog
)
if
self
.
mode
==
'train'
and
self
.
_lazy_load_optimizer
:
self
.
_load_optimizer
(
self
.
_lazy_load_optimizer
)
self
.
_lazy_load_optimizer
=
None
compiled_prog
=
fluid
.
CompiledProgram
(
prog
)
if
len
(
device_ids
)
>
1
:
loss_name
=
None
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录