Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
711aa19b
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
711aa19b
编写于
12月 18, 2020
作者:
W
WangXi
提交者:
GitHub
12月 18, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bert benchmark fleet compiled (#5117)
上级
d7009805
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
58 addition
and
13 deletion
+58
-13
PaddleNLP/benchmark/bert/run_pretrain.py
PaddleNLP/benchmark/bert/run_pretrain.py
+58
-13
未找到文件。
PaddleNLP/benchmark/bert/run_pretrain.py
浏览文件 @
711aa19b
...
@@ -172,13 +172,27 @@ def reset_program_state_dict(model, state_dict):
...
@@ -172,13 +172,27 @@ def reset_program_state_dict(model, state_dict):
loc
=
0.0
,
scale
=
scale
,
size
=
p
.
shape
).
astype
(
dtype_str
)
loc
=
0.0
,
scale
=
scale
,
size
=
p
.
shape
).
astype
(
dtype_str
)
return
new_state_dict
return
new_state_dict
def
create_strategy
():
def
build_compiled_program
(
main_program
,
loss
):
"""
Create build strategy and exec strategy.
Args:
Returns:
build_strategy: build strategy
exec_strategy: exec strategy
"""
build_strategy
=
paddle
.
static
.
BuildStrategy
()
exec_strategy
=
paddle
.
static
.
ExecutionStrategy
()
exec_strategy
=
paddle
.
static
.
ExecutionStrategy
()
build_strategy
.
enable_addto
=
args
.
enable_addto
exec_strategy
.
num_threads
=
1
exec_strategy
.
num_threads
=
1
exec_strategy
.
num_iteration_per_drop_scope
=
10000
exec_strategy
.
num_iteration_per_drop_scope
=
10000
build_strategy
=
paddle
.
static
.
BuildStrategy
()
return
build_strategy
,
exec_strategy
build_strategy
.
enable_addto
=
args
.
enable_addto
def
build_compiled_program
(
main_program
,
loss
):
build_strategy
,
exec_strategy
=
create_strategy
()
main_program
=
paddle
.
static
.
CompiledProgram
(
main_program
=
paddle
.
static
.
CompiledProgram
(
main_program
).
with_data_parallel
(
main_program
).
with_data_parallel
(
loss_name
=
loss
.
name
,
loss_name
=
loss
.
name
,
...
@@ -187,6 +201,33 @@ def build_compiled_program(main_program, loss):
...
@@ -187,6 +201,33 @@ def build_compiled_program(main_program, loss):
return
main_program
return
main_program
def
dist_optimizer
(
args
,
optimizer
):
"""
Create a distributed optimizer based on a normal optimizer
Args:
args:
optimizer: a normal optimizer
Returns:
optimizer: a distributed optimizer
"""
build_strategy
,
exec_strategy
=
create_strategy
()
dist_strategy
=
fleet
.
DistributedStrategy
()
dist_strategy
.
execution_strategy
=
exec_strategy
dist_strategy
.
build_strategy
=
build_strategy
dist_strategy
.
fuse_grad_size_in_MB
=
16
if
args
.
use_amp
:
dist_strategy
.
amp
=
True
dist_strategy
.
amp_configs
=
{
'custom_white_list'
:
[
'softmax'
,
'layer_norm'
,
'gelu'
],
'init_loss_scaling'
:
args
.
scale_loss
,
}
optimizer
=
fleet
.
distributed_optimizer
(
optimizer
,
strategy
=
dist_strategy
)
return
optimizer
def
set_seed
(
seed
):
def
set_seed
(
seed
):
random
.
seed
(
seed
)
random
.
seed
(
seed
)
np
.
random
.
seed
(
seed
)
np
.
random
.
seed
(
seed
)
...
@@ -208,9 +249,12 @@ def do_train(args):
...
@@ -208,9 +249,12 @@ def do_train(args):
place
=
paddle
.
set_device
(
args
.
select_device
)
place
=
paddle
.
set_device
(
args
.
select_device
)
fleet
.
init
(
is_collective
=
True
)
fleet
.
init
(
is_collective
=
True
)
worker_num
=
fleet
.
worker_num
()
worker_index
=
fleet
.
worker_index
()
# Create the random seed for the worker
# Create the random seed for the worker
set_seed
(
args
.
seed
)
set_seed
(
args
.
seed
)
worker_init
=
WorkerInitObj
(
args
.
seed
+
fleet
.
worker_index
()
)
worker_init
=
WorkerInitObj
(
args
.
seed
+
worker_index
)
# Define the input data in the static mode
# Define the input data in the static mode
main_program
=
paddle
.
static
.
default_main_program
()
main_program
=
paddle
.
static
.
default_main_program
()
...
@@ -260,7 +304,7 @@ def do_train(args):
...
@@ -260,7 +304,7 @@ def do_train(args):
p
.
name
for
n
,
p
in
model
.
named_parameters
()
p
.
name
for
n
,
p
in
model
.
named_parameters
()
if
not
any
(
nd
in
n
for
nd
in
[
"bias"
,
"norm"
])
if
not
any
(
nd
in
n
for
nd
in
[
"bias"
,
"norm"
])
])
])
if
args
.
use_amp
:
if
worker_num
==
1
and
args
.
use_amp
:
amp_list
=
paddle
.
fluid
.
contrib
.
mixed_precision
.
AutoMixedPrecisionLists
(
amp_list
=
paddle
.
fluid
.
contrib
.
mixed_precision
.
AutoMixedPrecisionLists
(
custom_white_list
=
[
'softmax'
,
'layer_norm'
,
'gelu'
])
custom_white_list
=
[
'softmax'
,
'layer_norm'
,
'gelu'
])
optimizer
=
paddle
.
fluid
.
contrib
.
mixed_precision
.
decorate
(
optimizer
=
paddle
.
fluid
.
contrib
.
mixed_precision
.
decorate
(
...
@@ -268,9 +312,10 @@ def do_train(args):
...
@@ -268,9 +312,10 @@ def do_train(args):
amp_list
,
amp_list
,
init_loss_scaling
=
args
.
scale_loss
,
init_loss_scaling
=
args
.
scale_loss
,
use_dynamic_loss_scaling
=
True
)
use_dynamic_loss_scaling
=
True
)
# Use the fleet api to compile the distributed optimizer
strategy
=
fleet
.
DistributedStrategy
()
if
worker_num
>
1
:
optimizer
=
fleet
.
distributed_optimizer
(
optimizer
,
strategy
=
strategy
)
# Use the fleet api to compile the distributed optimizer
optimizer
=
dist_optimizer
(
args
,
optimizer
)
optimizer
.
minimize
(
loss
)
optimizer
.
minimize
(
loss
)
# Define the Executor for running the static model
# Define the Executor for running the static model
...
@@ -281,14 +326,14 @@ def do_train(args):
...
@@ -281,14 +326,14 @@ def do_train(args):
# Use the state dict to update the parameter
# Use the state dict to update the parameter
reset_state_dict
=
reset_program_state_dict
(
model
,
state_dict
)
reset_state_dict
=
reset_program_state_dict
(
model
,
state_dict
)
paddle
.
static
.
set_program_state
(
main_program
,
reset_state_dict
)
paddle
.
static
.
set_program_state
(
main_program
,
reset_state_dict
)
# Construct the compiled program
main_program
=
build_compiled_program
(
main_program
,
loss
)
if
worker_num
==
1
:
# Construct the compiled program
main_program
=
build_compiled_program
(
main_program
,
loss
)
pool
=
ThreadPoolExecutor
(
1
)
pool
=
ThreadPoolExecutor
(
1
)
global_step
=
0
global_step
=
0
tic_train
=
time
.
time
()
tic_train
=
time
.
time
()
worker_num
=
fleet
.
worker_num
()
worker_index
=
fleet
.
worker_index
()
epoch
=
0
epoch
=
0
while
True
:
while
True
:
files
=
[
files
=
[
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录