Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
55538c56
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
55538c56
编写于
7月 01, 2019
作者:
H
hutuxian
提交者:
GitHub
7月 01, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cherry-pick: update api format (#18413) (#18421)
上级
49884564
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
73 addition
and
70 deletion
+73
-70
paddle/fluid/API.spec
paddle/fluid/API.spec
+0
-7
python/paddle/fluid/optimizer.py
python/paddle/fluid/optimizer.py
+73
-63
未找到文件。
paddle/fluid/API.spec
浏览文件 @
55538c56
...
@@ -874,14 +874,7 @@ paddle.fluid.optimizer.ExponentialMovingAverage.apply (ArgSpec(args=['self', 'ex
...
@@ -874,14 +874,7 @@ paddle.fluid.optimizer.ExponentialMovingAverage.apply (ArgSpec(args=['self', 'ex
paddle.fluid.optimizer.ExponentialMovingAverage.restore (ArgSpec(args=['self', 'executor'], varargs=None, keywords=None, defaults=None), ('document', '8c8a1791608b02a1ede53d6dd3a4fcec'))
paddle.fluid.optimizer.ExponentialMovingAverage.restore (ArgSpec(args=['self', 'executor'], varargs=None, keywords=None, defaults=None), ('document', '8c8a1791608b02a1ede53d6dd3a4fcec'))
paddle.fluid.optimizer.ExponentialMovingAverage.update (ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None), ('document', 'ea10f08af6d7aac3b7974aa976e4085f'))
paddle.fluid.optimizer.ExponentialMovingAverage.update (ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None), ('document', 'ea10f08af6d7aac3b7974aa976e4085f'))
paddle.fluid.optimizer.PipelineOptimizer.__init__ (ArgSpec(args=['self', 'optimizer', 'cut_list', 'place_list', 'concurrency_list', 'queue_size', 'sync_steps', 'start_cpu_core_id'], varargs=None, keywords=None, defaults=(None, None, None, 30, 1, 0)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.optimizer.PipelineOptimizer.__init__ (ArgSpec(args=['self', 'optimizer', 'cut_list', 'place_list', 'concurrency_list', 'queue_size', 'sync_steps', 'start_cpu_core_id'], varargs=None, keywords=None, defaults=(None, None, None, 30, 1, 0)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.optimizer.PipelineOptimizer.create_vars (ArgSpec(args=['self', 'block', 'main_program'], varargs=None, keywords=None, defaults=None), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.optimizer.PipelineOptimizer.extract_section_ops (ArgSpec(args=['self', 'ops', 'cut_point_name'], varargs=None, keywords=None, defaults=None), ('document', '4a29be77da04b5c30dd7202f44c79b70'))
paddle.fluid.optimizer.PipelineOptimizer.extract_section_opt_ops (ArgSpec(args=['self', 'ops', 'cut_point_name'], varargs=None, keywords=None, defaults=None), ('document', '99e0f641222c1ce4dd0d7194c3b2c653'))
paddle.fluid.optimizer.PipelineOptimizer.find_input_output (ArgSpec(args=['self', 'ops', 'name', 'is_forward'], varargs=None, keywords=None, defaults=(True,)), ('document', '92d77fb262766b352746f09cca81db93'))
paddle.fluid.optimizer.PipelineOptimizer.find_persistable_vars (ArgSpec(args=['self', 'ops', 'whole_parameters'], varargs=None, keywords=None, defaults=None), ('document', '877b7cc290f0647455e5e4409e825923'))
paddle.fluid.optimizer.PipelineOptimizer.find_section_opt (ArgSpec(args=['self', 'ops', 'params'], varargs=None, keywords=None, defaults=None), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.optimizer.PipelineOptimizer.minimize (ArgSpec(args=['self', 'loss', 'startup_program', 'parameter_list', 'no_grad_set'], varargs=None, keywords=None, defaults=(None, None, None)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.optimizer.PipelineOptimizer.minimize (ArgSpec(args=['self', 'loss', 'startup_program', 'parameter_list', 'no_grad_set'], varargs=None, keywords=None, defaults=(None, None, None)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.optimizer.PipelineOptimizer.split_program (ArgSpec(args=['self', 'main_program', 'cut_list'], varargs=None, keywords=None, defaults=None), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.backward.append_backward (ArgSpec(args=['loss', 'parameter_list', 'no_grad_set', 'callbacks'], varargs=None, keywords=None, defaults=(None, None, None)), ('document', '08a5dd9f6f376ff3d55e0b1d92115cbd'))
paddle.fluid.backward.append_backward (ArgSpec(args=['loss', 'parameter_list', 'no_grad_set', 'callbacks'], varargs=None, keywords=None, defaults=(None, None, None)), ('document', '08a5dd9f6f376ff3d55e0b1d92115cbd'))
paddle.fluid.backward.gradients (ArgSpec(args=['targets', 'inputs', 'target_gradients', 'no_grad_set'], varargs=None, keywords=None, defaults=(None, None)), ('document', 'e2097e1e0ed84ae44951437bfe269a1b'))
paddle.fluid.backward.gradients (ArgSpec(args=['targets', 'inputs', 'target_gradients', 'no_grad_set'], varargs=None, keywords=None, defaults=(None, None)), ('document', 'e2097e1e0ed84ae44951437bfe269a1b'))
paddle.fluid.regularizer.L1DecayRegularizer.__init__ (ArgSpec(args=['self', 'regularization_coeff'], varargs=None, keywords=None, defaults=(0.0,)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
paddle.fluid.regularizer.L1DecayRegularizer.__init__ (ArgSpec(args=['self', 'regularization_coeff'], varargs=None, keywords=None, defaults=(0.0,)), ('document', '6adf97f83acf6453d4a6a4b1070f3754'))
...
...
python/paddle/fluid/optimizer.py
浏览文件 @
55538c56
...
@@ -2650,57 +2650,67 @@ class ExponentialMovingAverage(object):
...
@@ -2650,57 +2650,67 @@ class ExponentialMovingAverage(object):
class
PipelineOptimizer
(
object
):
class
PipelineOptimizer
(
object
):
"""
"""
Pipeline Optimizer
Pipeline Optimizer
Train with pipeline mode. The program will be splited by cut_list.
If the len of cut_list is k, then the whole program (including
Train with pipeline mode. The program will be splited by cut_list.
backward part) will be splited to 2*k-1 sections. So the length of place_list
and concurrency_list must be also 2*k-1.
If the len of cut_list is k, then the whole program (including
\
Note: Though the asynchronous mode is applied in pipeline training to speed up,
backward part) will be splited to 2*k-1 sections.
So the length of place_list and concurrency_list must be also 2*k-1.
Note: Though the asynchronous mode is applied in pipeline training to speed up,
\
the final performance depends on the training progress of each pipeline heavily.
the final performance depends on the training progress of each pipeline heavily.
And we will try the synchronous mode in the future
And we will try the synchronous mode in the future.
Args:
Args:
optimizer (Optimizer): The based optimizer, such as SGD
optimizer (Optimizer): The based optimizer, such as SGD
.
cut_list (list of Variable list): The cut variable of the main_program
cut_list (list of Variable list): The cut variable of the main_program
.
place_list (list of Place): The place where the section will run on
place_list (list of Place): The place where the section will run on
.
concurrency_list (list of int): The concurrency degree
concurrency_list (list of int): The concurrency degree
.
queue_size (int): Each section will consume scopes from its in-scope queue
queue_size (int): Each section will consume scopes from its in-scope queue
and produce scopes to out-scope queue. And this parameter
and produce scopes to out-scope queue. And this parameter
specify the scope queue size. [Optional. Default: 30]
specify the scope queue size. [Optional. Default: 30].
sync_steps (int): The synchronization steps between different cards. [Optional. Default: 1]
sync_steps (int): The synchronization steps between different cards. [Optional. Default: 1].
start_cpu_core_id (int): specify the first cpu core id. [Optional. Default:0]
start_cpu_core_id (int): specify the first cpu core id. [Optional. Default:0].
Examples:
Examples:
.. code-block:: python
.. code-block:: python
x = fluid.layers.data(name='x', shape=[1], dtype='int64', lod_level=0)
y = fluid.layers.data(name='y', shape=[1], dtype='int64', lod_level=0)
import paddle.fluid.layers as layers
emb_x = layers.embedding(input=x, param_attr=fluid.ParamAttr(name="embx"), size=[10,2], is_sparse=False)
emb_y = layers.embedding(input=y, param_attr=fluid.ParamAttr(name="emby",learning_rate=0.9), size=[10,2], is_sparse=False)
x = fluid.layers.data(name='x', shape=[1], dtype='int64', lod_level=0)
concat = layers.concat([emb_x, emb_y], axis=1)
y = fluid.layers.data(name='y', shape=[1], dtype='int64', lod_level=0)
fc = layers.fc(input=concat, name="fc", size=1, num_flatten_dims=1, bias_attr=False)
emb_x = layers.embedding(input=x, param_attr=fluid.ParamAttr(name="embx"), size=[10,2], is_sparse=False)
loss = layers.reduce_mean(fc)
emb_y = layers.embedding(input=y, param_attr=fluid.ParamAttr(name="emby",learning_rate=0.9), size=[10,2], is_sparse=False)
optimizer = fluid.optimizer.SGD(learning_rate=0.5)
concat = layers.concat([emb_x, emb_y], axis=1)
optimizer = fluid.optimizer.PipelineOptimizer(optimizer,
fc = layers.fc(input=concat, name="fc", size=1, num_flatten_dims=1, bias_attr=False)
cut_list=[[emb_x, emb_y], [loss]],
loss = layers.reduce_mean(fc)
place_list=[fluid.CPUPlace(), fluid.CUDAPlace(0), fluid.CPUPlace()],
optimizer = fluid.optimizer.SGD(learning_rate=0.5)
concurrency_list=[1, 1, 4],
optimizer = fluid.optimizer.PipelineOptimizer(optimizer,
queue_size=2,
cut_list=[[emb_x, emb_y], [loss]],
sync_steps=1,
place_list=[fluid.CPUPlace(), fluid.CUDAPlace(0), fluid.CPUPlace()],
)
concurrency_list=[1, 1, 4],
optimizer.minimize(loss)
queue_size=2,
place = fluid.CPUPlace()
sync_steps=1,
exe = fluid.Executor(place)
)
exe.run(fluid.default_startup_program())
optimizer.minimize(loss)
filelist = [] # you should set your own filelist, e.g. filelist = ["dataA.txt"]
place = fluid.CPUPlace()
dataset = fluid.DatasetFactory().create_dataset("FileInstantDataset")
exe = fluid.Executor(place)
dataset.set_use_var([x,y])
exe.run(fluid.default_startup_program())
dataset.set_batch_size(batch_size)
filelist = [] # you should set your own filelist, e.g. filelist = ["dataA.txt"]
dataset.set_filelist(filelist)
dataset = fluid.DatasetFactory().create_dataset("FileInstantDataset")
exe.train_from_dataset(
dataset.set_use_var([x,y])
fluid.default_main_program(),
dataset.set_batch_size(batch_size)
dataset,
dataset.set_filelist(filelist)
thread=2,
exe.train_from_dataset(
debug=False,
fluid.default_main_program(),
fetch_list=[],
dataset,
fetch_info=[],
thread=2,
print_period=1)
debug=False,
fetch_list=[],
fetch_info=[],
print_period=1)
"""
"""
def
__init__
(
self
,
def
__init__
(
self
,
...
@@ -2720,7 +2730,7 @@ class PipelineOptimizer(object):
...
@@ -2720,7 +2730,7 @@ class PipelineOptimizer(object):
self
.
_sync_steps
=
sync_steps
self
.
_sync_steps
=
sync_steps
self
.
_start_cpu_core_id
=
start_cpu_core_id
self
.
_start_cpu_core_id
=
start_cpu_core_id
def
create_vars
(
self
,
block
,
main_program
):
def
_
create_vars
(
self
,
block
,
main_program
):
used_var_set
=
set
()
used_var_set
=
set
()
for
op_idx
in
range
(
block
.
desc
.
op_size
()):
for
op_idx
in
range
(
block
.
desc
.
op_size
()):
op_desc
=
block
.
desc
.
op
(
op_idx
)
op_desc
=
block
.
desc
.
op
(
op_idx
)
...
@@ -2732,7 +2742,7 @@ class PipelineOptimizer(object):
...
@@ -2732,7 +2742,7 @@ class PipelineOptimizer(object):
source_var
=
main_program
.
block
(
0
).
var
(
str
(
var
))
source_var
=
main_program
.
block
(
0
).
var
(
str
(
var
))
block
.
_clone_variable
(
source_var
,
False
)
block
.
_clone_variable
(
source_var
,
False
)
def
extract_section_opt_ops
(
self
,
ops
,
cut_point_name
):
def
_
extract_section_opt_ops
(
self
,
ops
,
cut_point_name
):
"""
"""
Extract opt ops in the given section
Extract opt ops in the given section
"""
"""
...
@@ -2748,7 +2758,7 @@ class PipelineOptimizer(object):
...
@@ -2748,7 +2758,7 @@ class PipelineOptimizer(object):
op_path
=
[
ops
[
i
]
for
i
in
range
(
len
(
ops
))
if
relevant_op_flags
[
i
]]
op_path
=
[
ops
[
i
]
for
i
in
range
(
len
(
ops
))
if
relevant_op_flags
[
i
]]
return
op_path
return
op_path
def
find_input_output
(
self
,
ops
,
name
,
is_forward
=
True
):
def
_
find_input_output
(
self
,
ops
,
name
,
is_forward
=
True
):
"""
"""
Find the inputs or outputs of a section
Find the inputs or outputs of a section
"""
"""
...
@@ -2763,7 +2773,7 @@ class PipelineOptimizer(object):
...
@@ -2763,7 +2773,7 @@ class PipelineOptimizer(object):
all_set
.
update
(
op
.
desc
.
input_arg_names
())
all_set
.
update
(
op
.
desc
.
input_arg_names
())
return
all_set
-
part_set
return
all_set
-
part_set
def
find_persistable_vars
(
self
,
ops
,
whole_parameters
):
def
_
find_persistable_vars
(
self
,
ops
,
whole_parameters
):
"""
"""
find the persistable input vars in current section
find the persistable input vars in current section
"""
"""
...
@@ -2791,7 +2801,7 @@ class PipelineOptimizer(object):
...
@@ -2791,7 +2801,7 @@ class PipelineOptimizer(object):
return
True
return
True
return
False
return
False
def
extract_section_ops
(
self
,
ops
,
cut_point_name
):
def
_
extract_section_ops
(
self
,
ops
,
cut_point_name
):
"""
"""
Extract ops in the given section
Extract ops in the given section
"""
"""
...
@@ -2811,11 +2821,11 @@ class PipelineOptimizer(object):
...
@@ -2811,11 +2821,11 @@ class PipelineOptimizer(object):
op_path
=
[
ops
[
i
]
for
i
in
range
(
len
(
ops
))
if
relevant_op_flags
[
i
]]
op_path
=
[
ops
[
i
]
for
i
in
range
(
len
(
ops
))
if
relevant_op_flags
[
i
]]
return
op_path
return
op_path
def
find_section_opt
(
self
,
ops
,
params
):
def
_
find_section_opt
(
self
,
ops
,
params
):
res
=
self
.
extract_section_opt_ops
(
ops
,
params
)
res
=
self
.
_
extract_section_opt_ops
(
ops
,
params
)
return
res
return
res
def
split_program
(
self
,
main_program
,
cut_list
):
def
_
split_program
(
self
,
main_program
,
cut_list
):
programs
=
[]
programs
=
[]
block
=
main_program
.
block
(
0
)
block
=
main_program
.
block
(
0
)
whole_parameters
=
[
e
.
name
for
e
in
block
.
all_parameters
()]
whole_parameters
=
[
e
.
name
for
e
in
block
.
all_parameters
()]
...
@@ -2836,24 +2846,24 @@ class PipelineOptimizer(object):
...
@@ -2836,24 +2846,24 @@ class PipelineOptimizer(object):
"input_set"
:
set
(),
"input_set"
:
set
(),
"output_set"
:
set
()
"output_set"
:
set
()
}
}
cur_ops
=
self
.
extract_section_ops
(
ops
,
cut_vars
)
cur_ops
=
self
.
_
extract_section_ops
(
ops
,
cut_vars
)
if
i
==
0
:
if
i
==
0
:
for
op
in
ops
:
for
op
in
ops
:
if
self
.
_is_lr_role_op
(
op
):
if
self
.
_is_lr_role_op
(
op
):
cur_ops
.
append
(
op
)
cur_ops
.
append
(
op
)
#prevent inplace in/out
#prevent inplace in/out
program
[
"input_set"
].
update
(
program
[
"input_set"
].
update
(
self
.
find_input_output
(
self
.
_
find_input_output
(
cur_ops
,
[],
is_forward
=
True
))
cur_ops
,
[],
is_forward
=
True
))
for
e
in
cur_ops
:
for
e
in
cur_ops
:
ops
.
remove
(
e
)
ops
.
remove
(
e
)
if
i
<
cut_len
:
if
i
<
cut_len
:
sec_params
.
append
(
sec_params
.
append
(
self
.
find_persistable_vars
(
cur_ops
,
whole_parameters
))
self
.
_
find_persistable_vars
(
cur_ops
,
whole_parameters
))
if
i
>=
cut_len
-
1
:
if
i
>=
cut_len
-
1
:
opt_ops
=
self
.
find_section_opt
(
ops
,
opt_ops
=
self
.
_find_section_opt
(
sec_params
[
2
*
cut_len
-
2
-
i
])
ops
,
sec_params
[
2
*
cut_len
-
2
-
i
])
for
e
in
opt_ops
:
for
e
in
opt_ops
:
ops
.
remove
(
e
)
ops
.
remove
(
e
)
...
@@ -2864,11 +2874,11 @@ class PipelineOptimizer(object):
...
@@ -2864,11 +2874,11 @@ class PipelineOptimizer(object):
ap_op
=
program
[
"program"
].
block
(
0
).
desc
.
append_op
()
ap_op
=
program
[
"program"
].
block
(
0
).
desc
.
append_op
()
ap_op
.
copy_from
(
op_desc
)
ap_op
.
copy_from
(
op_desc
)
program
[
"input_set"
].
update
(
program
[
"input_set"
].
update
(
self
.
find_input_output
(
self
.
_
find_input_output
(
cur_ops
,
cut_vars
,
is_forward
=
True
))
cur_ops
,
cut_vars
,
is_forward
=
True
))
program
[
"input_set"
].
update
(
sec_params
[
min
(
i
,
2
*
cut_len
-
2
-
i
)])
program
[
"input_set"
].
update
(
sec_params
[
min
(
i
,
2
*
cut_len
-
2
-
i
)])
program
[
"output_set"
].
update
(
program
[
"output_set"
].
update
(
self
.
find_input_output
(
self
.
_
find_input_output
(
cur_ops
,
cut_vars
,
is_forward
=
False
))
cur_ops
,
cut_vars
,
is_forward
=
False
))
programs
.
append
(
program
)
programs
.
append
(
program
)
program
=
{
program
=
{
...
@@ -2883,7 +2893,7 @@ class PipelineOptimizer(object):
...
@@ -2883,7 +2893,7 @@ class PipelineOptimizer(object):
program
[
"input_set"
].
update
(
program
[
"input_set"
].
update
(
[
cut_var
.
name
+
"@GRAD"
for
cut_var
in
cut_list
[
0
]])
[
cut_var
.
name
+
"@GRAD"
for
cut_var
in
cut_list
[
0
]])
program
[
"input_set"
].
update
(
program
[
"input_set"
].
update
(
self
.
find_input_output
(
self
.
_
find_input_output
(
ops
,
[],
is_forward
=
True
))
ops
,
[],
is_forward
=
True
))
program
[
"input_set"
].
update
(
sec_params
[
0
])
program
[
"input_set"
].
update
(
sec_params
[
0
])
programs
.
append
(
program
)
programs
.
append
(
program
)
...
@@ -2904,9 +2914,9 @@ class PipelineOptimizer(object):
...
@@ -2904,9 +2914,9 @@ class PipelineOptimizer(object):
self
.
_optimizer
.
minimize
(
loss
,
startup_program
,
parameter_list
,
self
.
_optimizer
.
minimize
(
loss
,
startup_program
,
parameter_list
,
no_grad_set
)
no_grad_set
)
program
=
loss
.
block
.
program
program
=
loss
.
block
.
program
program_list
=
self
.
split_program
(
program
,
self
.
_cut_list
)
program_list
=
self
.
_
split_program
(
program
,
self
.
_cut_list
)
for
p
in
program_list
:
for
p
in
program_list
:
self
.
create_vars
(
p
[
"program"
].
block
(
0
),
program
)
self
.
_
create_vars
(
p
[
"program"
].
block
(
0
),
program
)
whole_parameters
=
[
e
.
name
for
e
in
program
.
block
(
0
).
all_parameters
()]
whole_parameters
=
[
e
.
name
for
e
in
program
.
block
(
0
).
all_parameters
()]
param_need_sync
=
[]
param_need_sync
=
[]
for
i
,
section_p
in
enumerate
(
program_list
):
for
i
,
section_p
in
enumerate
(
program_list
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录