Skip to content

Commit 43ce071

Browse files
authored
[PRETRAIN] Add ernie-1.0 dygraph training script. (#1744)
* add ernie-1.0 dygraph training. * modify readme. * fix as reviews. * fix script * typo fix.
1 parent d6d77ec commit 43ce071

File tree

7 files changed

+577
-59
lines changed

7 files changed

+577
-59
lines changed

examples/language_model/data_tools/dataset_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525

2626
import numpy as np
2727
import paddle
28-
import paddle.distributed.fleet as fleet
28+
29+
30+
def get_local_rank():
31+
return int(os.getenv("PADDLE_RANK_IN_NODE", 0))
32+
2933

3034
print_rank_0 = print
3135

@@ -706,9 +710,9 @@ def get_samples_mapping(indexed_dataset, data_prefix, num_epochs,
706710
indexmap_filename += '_{}s'.format(seed)
707711
indexmap_filename += '.npy'
708712

709-
local_rank = 0 if fleet.local_rank() is None else int(fleet.local_rank())
713+
local_rank = get_local_rank()
710714
if share_folder:
711-
local_rank = fleet.worker_index()
715+
local_rank = paddle.distributed.get_rank()
712716
# Build the indexed mapping if not exist.
713717

714718
if local_rank == 0 and \

examples/language_model/ernie-1.0/README.md

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,14 @@ ERNIE在情感分析、文本匹配、自然语言推理、词法分析、阅读
2525
python -u -m paddle.distributed.launch \
2626
--gpus "0,1,2,3,4,5,6,7" \
2727
--log_dir "output/ernie-1.0-dp8-gb512/log" \
28-
run_pretrain_static.py \
28+
run_pretrain.py \
2929
--model_type "ernie" \
3030
--model_name_or_path "ernie-1.0" \
3131
--input_dir "./data" \
3232
--output_dir "output/ernie-1.0-dp8-gb512" \
3333
--max_seq_len 512 \
3434
--micro_batch_size 64 \
35-
--global_batch_size 512 \
36-
--sharding_degree 1\
37-
--dp_degree 8 \
38-
--use_sharding false \
3935
--use_amp true \
40-
--use_recompute false \
4136
--max_lr 0.0001 \
4237
--min_lr 0.00001 \
4338
--max_steps 1000000 \
@@ -58,29 +53,23 @@ python -u -m paddle.distributed.launch \
5853
- `input_dir` 指定输入文件,可以使用目录,指定目录时将包括目录中的所有文件。
5954
- `output_dir` 指定输出文件。
6055
- `max_seq_len` 输入文本序列的长度。
61-
- `micro_batch_size` 单卡单次的 batch size大小。即单张卡运行一次前向网络的 batch size大小。
62-
- `global_batch_size` 全局的batch size大小,即一次参数更新等效的batch size。
63-
- `sharding_degree` 切参数切分的分组大小(如 sharding_degree=4 表示参数分为4组,分别到4个设备)。
64-
- `dp_degree` 数据并行参数。
65-
- `use_sharding` 开启sharding策略,sharding_degree > 1时,请设置为True。
56+
- `micro_batch_size` 单卡batch size大小,比如此处单卡bs=64, 采用8卡训练`global_batch_size=64*8=512`
6657
- `use_amp` 开启混合精度策略。
67-
- `use_recompute` 开启重计算策略。暂时未支持,后续将支持。
6858
- `max_lr` 训练学习率。
6959
- `min_lr` 学习率衰减的最小值。
7060
- `max_steps` 最大训练步数。
71-
- `save_steps` 保存模型间隔。
72-
- `checkpoint_steps` 模型checkpoint间隔,用于模型断点重启训练。
61+
- `save_steps` 保存模型间隔。默认保存地址格式为`output_dir/model_50000`(5w 步时的权重)。
62+
- `checkpoint_steps` 模型checkpoint间隔,用于模型断点重启训练。默认地址为`output_dir/model_last`.
7363
- `weight_decay` 权重衰减参数。
7464
- `warmup_rate` 学习率warmup参数。
7565
- `grad_clip` 梯度裁剪范围。
7666
- `logging_freq` 日志输出间隔。
67+
- `num_workers` DataLoader采样进程,当数据输入为瓶颈时,可尝试提高采样进程数目。
7768
- `eval_freq` 模型评估间隔。
7869
- `device` 训练设备。
7970

8071
注:
81-
- 一般而言,需要设置 `mp_degree * sharding_degree` = 训练机器的总卡数。
82-
- 一般而言, `global_batch_size = micro_batch_size * sharding_degree * dp_degree`。可以使用梯度累积的方式增大`global_batch_size`。设置`global_batch_size`为理论值的整数倍是,默认启用梯度累积。
83-
- 训练断点重启,直接启动即可,程序会找到最新的checkpoint,开始重启训练。
72+
- 训练支持断点重启,直接启动即可,程序会找到最新的checkpoint,开始重启训练。请确保重启的训练配置与之前相同。
8473

8574

8675
### CLUECorpusSmall 数据集训练结果
@@ -109,15 +98,6 @@ CMRC2018 | 72.05/85.67 | - |
10998

11099

111100
### 其他
112-
#### 模型参数转换
113-
本示例提供了静态图训练脚本,但Paddle目前主要的使用方式是动态图。因此,本示例提供了静态图参数到动态图参数的转换脚本:
114-
115-
```python
116-
python converter/params_static_to_dygraph.py --model ernie-1.0 --path ./output/task_name/model_100000/static_vars
117-
# or
118-
python converter/params_static_to_dygraph.py --model ernie-1.0 --path ./output/task_name/model_last/static_vars.pdparams
119-
```
120-
在当前目录下,可以看到转换后的参数`ernie-1.0_converted.pdparams`, 也可以设置脚本中`--output_path`参数,指定输出路径。
121101

122102
#### 为PaddleNLP贡献预训练参数
123103
PaddleNLP为开发者支持了[community](https://github.com/PaddlePaddle/PaddleNLP/tree/develop/community)模块,用户可以上传自己训练的模型,开源给其他用户使用。

examples/language_model/ernie-1.0/args.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def parse_args(MODEL_CLASSES):
7777
# AMP config
7878
parser.add_argument("--use_amp", type=str2bool, nargs='?', const=False, help="Enable mixed precision training.")
7979
parser.add_argument("--enable_addto", type=str2bool, nargs='?', const=True, default=True, help="Whether to enable the addto strategy for gradient accumulation or not. This is only used for AMP training.")
80-
parser.add_argument("--scale_loss", type=float, default=128, help="The value of scale_loss for fp16. This is only used for AMP training.")
80+
parser.add_argument("--scale_loss", type=float, default=32768, help="The value of scale_loss for fp16. This is only used for AMP training.")
8181
parser.add_argument("--hidden_dropout_prob", type=float, default=0.1, help="The hidden dropout prob.")
8282
parser.add_argument("--attention_probs_dropout_prob", type=float, default=0.1, help="The attention probs dropout prob.")
8383

@@ -108,8 +108,4 @@ def parse_args(MODEL_CLASSES):
108108
"The attention_probs_dropout_prob should set to 0 for accuracy checking."
109109
)
110110

111-
logger.info('{:20}:{}'.format("paddle commit id", paddle.version.commit))
112-
for arg in vars(args):
113-
logger.info('{:20}:{}'.format(arg, getattr(args, arg)))
114-
115111
return args
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
set -x
22
export PADDLE_WITH_GLOO=0
3-
export FLAGS_call_stack_level=2
43
unset CUDA_VISIBLE_DEVICES
54

6-
rm -rf *.prototxt
75
rm -rf core.*
8-
rm -rf start_sharding*
9-
rm -rf main_sharding*
106

117
task_name="ernie-1.0-dp8-gb512"
128
rm -rf output/$task_name/log
139

14-
1510
PYTHONPATH=../../../ python -u -m paddle.distributed.launch \
1611
--gpus "0,1,2,3,4,5,6,7" \
1712
--log_dir "output/$task_name/log" \
18-
run_pretrain_static.py \
13+
run_pretrain.py \
1914
--model_type "ernie" \
2015
--model_name_or_path "ernie-1.0" \
2116
--input_dir "./data" \
2217
--output_dir "output/$task_name" \
2318
--max_seq_len 512 \
2419
--micro_batch_size 64 \
25-
--global_batch_size 512 \
26-
--sharding_degree 1\
27-
--dp_degree 8 \
28-
--use_sharding false \
2920
--use_amp true \
3021
--use_recompute false \
3122
--max_lr 0.0001 \
3223
--min_lr 0.00001 \
33-
--max_steps 2000000 \
24+
--max_steps 1000000 \
3425
--save_steps 50000 \
3526
--checkpoint_steps 5000 \
36-
--decay_steps 1980000 \
27+
--decay_steps 990000 \
3728
--weight_decay 0.01 \
38-
--warmup_rate 0.005 \
29+
--warmup_rate 0.01 \
3930
--grad_clip 1.0 \
4031
--logging_freq 20\
4132
--num_workers 2 \
4233
--eval_freq 1000 \
4334
--device "gpu"\
4435

45-
# --check_accuracy true\
46-
47-
# NOTE: please set use_sharding=True for sharding_degree > 1

examples/language_model/ernie-1.0/run_static.sh renamed to examples/language_model/ernie-1.0/run_gb512_s100w_static.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ rm -rf core.*
88
rm -rf start_sharding*
99
rm -rf main_sharding*
1010

11-
task_name="ernie-1.0-dp8-gb1024"
11+
task_name="ernie-1.0-dp8-gb512"
1212
rm -rf output/$task_name/log
1313

1414
PYTHONPATH=../../../ python -u -m paddle.distributed.launch \
@@ -21,18 +21,18 @@ PYTHONPATH=../../../ python -u -m paddle.distributed.launch \
2121
--output_dir "output/$task_name" \
2222
--max_seq_len 512 \
2323
--micro_batch_size 64 \
24-
--global_batch_size 1024 \
24+
--global_batch_size 512 \
2525
--sharding_degree 1\
2626
--dp_degree 8 \
2727
--use_sharding false \
2828
--use_amp true \
2929
--use_recompute false \
3030
--max_lr 0.0001 \
3131
--min_lr 0.00001 \
32-
--max_steps 2000000 \
33-
--save_steps 100000 \
32+
--max_steps 1000000 \
33+
--save_steps 50000 \
3434
--checkpoint_steps 5000 \
35-
--decay_steps 1980000 \
35+
--decay_steps 990000 \
3636
--weight_decay 0.01\
3737
--warmup_rate 0.01 \
3838
--grad_clip 1.0 \

0 commit comments

Comments
 (0)