Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
3693394c
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看板
提交
3693394c
编写于
7月 25, 2018
作者:
T
tangwei12
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
hidden slice_vars in distribute transpile, hidden it to users
上级
2c05e37a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
87 addition
and
45 deletion
+87
-45
python/paddle/fluid/trainer.py
python/paddle/fluid/trainer.py
+87
-45
未找到文件。
python/paddle/fluid/trainer.py
浏览文件 @
3693394c
...
@@ -134,8 +134,6 @@ class CheckpointConfig(object):
...
@@ -134,8 +134,6 @@ class CheckpointConfig(object):
self
.
epoch_id
=
0
self
.
epoch_id
=
0
self
.
step_id
=
0
self
.
step_id
=
0
self
.
load_serial
=
None
self
.
load_serial
=
None
self
.
pserver_id
=
None
self
.
lookup_table_name
=
None
def
check_and_get_place
(
place
):
def
check_and_get_place
(
place
):
...
@@ -351,11 +349,9 @@ class Trainer(object):
...
@@ -351,11 +349,9 @@ class Trainer(object):
t
.
transpile
(
t
.
transpile
(
self
.
trainer_id
,
pservers
=
pserver_endpoints
,
trainers
=
trainers
)
self
.
trainer_id
,
pservers
=
pserver_endpoints
,
trainers
=
trainers
)
if
training_role
==
"PSERVER"
:
if
training_role
==
"PSERVER"
:
if
self
.
checkpoint_cfg
:
self
.
pserver_id
=
eplist
.
index
(
current_endpoint
)
pserver_id
=
eplist
.
index
(
current_endpoint
)
self
.
pserver_endpoints
=
pserver_endpoints
self
.
checkpoint_cfg
.
pserver_id
=
pserver_id
self
.
lookup_table_name
=
t
.
table_name
if
t
.
has_distributed_lookup_table
else
None
if
t
.
has_distributed_lookup_table
:
self
.
checkpoint_cfg
.
lookup_table_name
=
t
.
table_name
self
.
train_program
=
t
.
get_pserver_program
(
current_endpoint
)
self
.
train_program
=
t
.
get_pserver_program
(
current_endpoint
)
self
.
startup_program
=
t
.
get_startup_program
(
current_endpoint
,
self
.
startup_program
=
t
.
get_startup_program
(
current_endpoint
,
...
@@ -417,6 +413,11 @@ class Trainer(object):
...
@@ -417,6 +413,11 @@ class Trainer(object):
def
save_params
(
self
,
param_path
):
def
save_params
(
self
,
param_path
):
"""
"""
Save all parameters into :code:`param_path`.
Save all parameters into :code:`param_path`.
Only No.0 trainer will save dense params.
In standalone PaddlePaddle, the only existing trainer will save dense params.
In distributed PaddlePaddle, the No.0 trainer will save dense params,
If there have lookup table need to save, No.0 trainer will broadcast notification
to all Parameter Servers to save it on Parameter Servers independent.
Args:
Args:
param_path(str): The path to save parameters.
param_path(str): The path to save parameters.
...
@@ -424,9 +425,19 @@ class Trainer(object):
...
@@ -424,9 +425,19 @@ class Trainer(object):
Returns:
Returns:
None
None
"""
"""
if
self
.
trainer_id
!=
0
:
return
with
self
.
_prog_and_scope_guard
():
with
self
.
_prog_and_scope_guard
():
# save params on trainer
exe
=
executor
.
Executor
(
self
.
place
)
exe
=
executor
.
Executor
(
self
.
place
)
io
.
save_persistables
(
exe
,
dirname
=
param_path
)
io
.
save_persistables
(
exe
,
dirname
=
param_path
)
# save params on pserver
if
self
.
lookup_table_name
:
_save_pserver_vars_by_notify
(
exe
,
param_path
,
self
.
lookup_table_name
,
self
.
pserver_endpoints
)
@
contextlib
.
contextmanager
@
contextlib
.
contextmanager
def
_prog_and_scope_guard
(
self
):
def
_prog_and_scope_guard
(
self
):
...
@@ -560,15 +571,16 @@ class Trainer(object):
...
@@ -560,15 +571,16 @@ class Trainer(object):
if
epoch_id
%
self
.
checkpoint_cfg
.
epoch_interval
==
0
\
if
epoch_id
%
self
.
checkpoint_cfg
.
epoch_interval
==
0
\
and
step_id
%
self
.
checkpoint_cfg
.
step_interval
==
0
:
and
step_id
%
self
.
checkpoint_cfg
.
step_interval
==
0
:
print
(
"_save_checkpoint ..."
)
exe
=
executor
.
Executor
(
self
.
place
)
exe
=
executor
.
Executor
(
self
.
place
)
save_checkpoint
(
save_checkpoint
(
executor
=
exe
,
executor
=
exe
,
checkpoint_dir
=
self
.
checkpoint_cfg
.
checkpoint_dir
,
checkpoint_dir
=
self
.
checkpoint_cfg
.
checkpoint_dir
,
trainer_id
=
self
.
trainer_id
,
trainer_args
=
self
.
_get_checkpoint_save_args
(
epoch_id
,
step_id
),
main_program
=
self
.
train_program
,
main_program
=
self
.
train_program
,
trainer_id
=
self
.
trainer_id
,
save_trainer_args
=
self
.
_get_checkpoint_save_args
(
epoch_id
,
step_id
),
save_lookup_table
=
self
.
lookup_table_name
,
pserver_endpoints
=
self
.
pserver_endpoints
,
max_num_checkpoints
=
self
.
checkpoint_cfg
.
max_num_checkpoints
)
max_num_checkpoints
=
self
.
checkpoint_cfg
.
max_num_checkpoints
)
def
_load_checkpoint
(
self
):
def
_load_checkpoint
(
self
):
...
@@ -579,7 +591,7 @@ class Trainer(object):
...
@@ -579,7 +591,7 @@ class Trainer(object):
self
.
checkpoint_cfg
.
load_serial
)
self
.
checkpoint_cfg
.
load_serial
)
# Trainer Load
# Trainer Load
if
self
.
checkpoint_cfg
.
pserver_id
is
None
:
if
self
.
pserver_id
is
None
:
# load model
# load model
load_checkpoint
(
load_checkpoint
(
executor
=
exe
,
executor
=
exe
,
...
@@ -608,15 +620,25 @@ class Trainer(object):
...
@@ -608,15 +620,25 @@ class Trainer(object):
# Pserver Load
# Pserver Load
else
:
else
:
# load model
load_checkpoint
(
executor
=
exe
,
checkpoint_dir
=
checkpoint_dir
,
main_program
=
self
.
startup_program
,
role_id
=
self
.
pserver_id
,
is_trainer
=
False
,
load_models
=
True
,
load_lookup_table
=
self
.
lookup_table_name
)
# load lookup table
# load lookup table
if
self
.
checkpoint_cfg
.
lookup_table_name
:
if
self
.
lookup_table_name
:
load_checkpoint
(
load_checkpoint
(
executor
=
exe
,
executor
=
exe
,
checkpoint_dir
=
checkpoint_dir
,
checkpoint_dir
=
checkpoint_dir
,
main_program
=
self
.
startup_program
,
main_program
=
self
.
startup_program
,
role_id
=
self
.
checkpoint_cfg
.
pserver_id
,
role_id
=
self
.
pserver_id
,
is_trainer
=
False
,
is_trainer
=
False
,
load_lookup_table
=
self
.
checkpoint_cfg
.
lookup_table_name
)
load_lookup_table
=
self
.
lookup_table_name
)
def
build_feed_var_list
(
program
,
feed_order
):
def
build_feed_var_list
(
program
,
feed_order
):
...
@@ -813,13 +835,21 @@ def load_checkpoint(executor,
...
@@ -813,13 +835,21 @@ def load_checkpoint(executor,
if
is_trainer
:
if
is_trainer
:
if
load_models
:
if
load_models
:
_load_persistable_vars
(
executor
,
checkpoint_dir
,
main_program
,
True
)
_load_persistable_vars
(
executor
,
checkpoint_dir
,
main_program
,
True
)
return
if
load_trainer_args
:
if
load_trainer_args
:
trainer_args_ret
=
_load_trainer_args
(
checkpoint_dir
,
role_id
,
trainer_args_ret
=
_load_trainer_args
(
checkpoint_dir
,
role_id
,
load_trainer_args
)
load_trainer_args
)
return
trainer_args_ret
return
trainer_args_ret
# pserver load
# pserver load
else
:
else
:
if
load_models
:
if
load_lookup_table
:
_load_persistable_vars
(
executor
,
checkpoint_dir
,
main_program
,
True
,
[
load_lookup_table
])
else
:
_load_persistable_vars
(
executor
,
checkpoint_dir
,
main_program
,
True
)
if
load_lookup_table
:
if
load_lookup_table
:
_load_lookup_table_vars
(
executor
,
checkpoint_dir
,
main_program
,
_load_lookup_table_vars
(
executor
,
checkpoint_dir
,
main_program
,
role_id
,
load_lookup_table
)
role_id
,
load_lookup_table
)
...
@@ -843,7 +873,11 @@ def clean_checkpoint(checkpoint_dir, delete_dir=False):
...
@@ -843,7 +873,11 @@ def clean_checkpoint(checkpoint_dir, delete_dir=False):
os
.
rmdir
(
checkpoint_dir
)
os
.
rmdir
(
checkpoint_dir
)
def
_load_persistable_vars
(
executor
,
dirname
,
program
,
has_model_dir
=
False
):
def
_load_persistable_vars
(
executor
,
dirname
,
program
,
has_model_dir
=
False
,
except_vars
=
None
):
"""
"""
This function filters out all checkpoint variables from the give
This function filters out all checkpoint variables from the give
program and then trys to load these variables from the given directory.
program and then trys to load these variables from the given directory.
...
@@ -888,7 +922,7 @@ def _load_persistable_vars(executor, dirname, program, has_model_dir=False):
...
@@ -888,7 +922,7 @@ def _load_persistable_vars(executor, dirname, program, has_model_dir=False):
executor
,
executor
,
dirname
=
dirname
,
dirname
=
dirname
,
main_program
=
program
,
main_program
=
program
,
predicate
=
_is_checkpoint_var
,
predicate
=
_is_checkpoint_var
(
except_vars
)
,
filename
=
None
)
filename
=
None
)
...
@@ -983,13 +1017,13 @@ def _save_persistable_vars(executor, dirname, program):
...
@@ -983,13 +1017,13 @@ def _save_persistable_vars(executor, dirname, program):
dirname
=
cur_dir
,
dirname
=
cur_dir
,
main_program
=
program
,
main_program
=
program
,
vars
=
None
,
vars
=
None
,
predicate
=
_is_checkpoint_var
,
predicate
=
_is_checkpoint_var
()
,
filename
=
None
)
filename
=
None
)
_write_success
(
cur_dir
)
_write_success
(
cur_dir
)
def
_save_pserver_vars_by_notify
(
executor
,
dirname
,
lookup_table
,
def
_save_pserver_vars_by_notify
(
executor
,
dirname
,
lookup_table
,
ps
_endpoint_list
):
ps
erver_endpoints
):
"""
"""
This function will send checkpoint notify message from Trainer 0
This function will send checkpoint notify message from Trainer 0
to all the pservers.
to all the pservers.
...
@@ -1002,8 +1036,8 @@ def _save_pserver_vars_by_notify(executor, dirname, lookup_table,
...
@@ -1002,8 +1036,8 @@ def _save_pserver_vars_by_notify(executor, dirname, lookup_table,
lookup_table(string): the lookup table name, when use distribute
lookup_table(string): the lookup table name, when use distribute
lookup table, we can get lookup table name by DistributeTranspiler.
lookup table, we can get lookup table name by DistributeTranspiler.
table_name
table_name
ps
_endpoint_list
(list): the parameter server ip:port list.
ps
erver_endpoints
(list): the parameter server ip:port list.
when use distribute lookup table, we can get ps
_endpoint_list
by
when use distribute lookup table, we can get ps
erver_endpoints
by
distribute arguments.
distribute arguments.
Return:
Return:
None
None
...
@@ -1027,7 +1061,7 @@ def _save_pserver_vars_by_notify(executor, dirname, lookup_table,
...
@@ -1027,7 +1061,7 @@ def _save_pserver_vars_by_notify(executor, dirname, lookup_table,
checkpoint_notify_block
=
checkpoint_notify_program
.
global_block
()
checkpoint_notify_block
=
checkpoint_notify_program
.
global_block
()
attrs
=
{}
attrs
=
{}
attrs
[
'epmap'
]
=
ps
_endpoint_list
attrs
[
'epmap'
]
=
ps
erver_endpoints
.
split
(
","
)
attrs
[
'dir'
]
=
cur_dir
attrs
[
'dir'
]
=
cur_dir
attrs
[
'lookup_table'
]
=
lookup_table
attrs
[
'lookup_table'
]
=
lookup_table
...
@@ -1086,29 +1120,37 @@ def _load_trainer_args(checkpoint_dir, trainer_id, trainer_args):
...
@@ -1086,29 +1120,37 @@ def _load_trainer_args(checkpoint_dir, trainer_id, trainer_args):
return
ret_values
return
ret_values
def
_is_checkpoint_var
(
var
):
def
_is_checkpoint_var
(
except_vars
=
None
):
"""
except_vars
=
[]
if
except_vars
is
None
else
except_vars
the checkpoint will not save or load all the variables.
var type is FEED_MINIBATCH/FETCH_LIST/RAW or var name ends with @GRAD are discarded.
: param var(Variable)
def
_except_vars
(
var
):
"""
"""
if
var
.
desc
.
type
()
==
core
.
VarDesc
.
VarType
.
FEED_MINIBATCH
or
\
the checkpoint will not save or load all the variables.
var
.
desc
.
type
()
==
core
.
VarDesc
.
VarType
.
FETCH_LIST
or
\
var type is FEED_MINIBATCH/FETCH_LIST/RAW or var name ends with @GRAD are discarded.
var
.
desc
.
type
()
==
core
.
VarDesc
.
VarType
.
RAW
:
return
False
: param var(Variable)
# @GRAD are named for gradient variables, checkpoint will not save it.
"""
if
"@GRAD"
in
var
.
name
:
if
var
.
desc
.
type
()
==
core
.
VarDesc
.
VarType
.
FEED_MINIBATCH
or
\
return
False
var
.
desc
.
type
()
==
core
.
VarDesc
.
VarType
.
FETCH_LIST
or
\
# .trainer_ are named for distribute train variables, checkpoint will not save it.
var
.
desc
.
type
()
==
core
.
VarDesc
.
VarType
.
RAW
:
if
".trainer_"
in
var
.
name
:
return
False
return
False
# @GRAD are named for gradient variables, checkpoint will not save it.
if
"@GRAD"
in
var
.
name
:
# .block is named for distribute train variables, checkpoint will not save it.
return
False
if
".block"
in
var
.
name
:
# .trainer_ are named for distribute train variables, checkpoint will not save it.
return
False
if
".trainer_"
in
var
.
name
:
return
False
return
var
.
persistable
# .block is named for distribute train variables, checkpoint will not save it.
if
".block"
in
var
.
name
:
return
False
if
var
in
except_vars
:
return
False
return
var
.
persistable
return
_except_vars
def
_make_chekcpoint_dirs
(
dirs
):
def
_make_chekcpoint_dirs
(
dirs
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录