Skip to content

Commit b818ea1

Browse files
author
小小的香辛料
authored
[Model Zoo] Refactor Bert usage in Model Zoo (PaddlePaddle#4851)
* [Model Zoo] Refactor Bert usage in Model Zoo
1 parent 91b6d44 commit b818ea1

File tree

10 files changed

+715
-403
lines changed

10 files changed

+715
-403
lines changed

model_zoo/bert/README.md

+102-16
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,54 @@ python create_pretraining_data.py \
5353
GLUE评测任务所含数据集已在paddlenlp中以API形式提供,无需预先准备,使用`run_glue.py`执行微调时将会自动下载。
5454

5555
### 执行Pre-training
56+
<details>
57+
<summary>GPU训练</summary>
58+
<pre><code>unset CUDA_VISIBLE_DEVICES
59+
python -m paddle.distributed.launch --gpus "0" run_pretrain.py \
60+
--model_type bert \
61+
--model_name_or_path bert-base-uncased \
62+
--max_predictions_per_seq 20 \
63+
--batch_size 32 \
64+
--learning_rate 1e-4 \
65+
--weight_decay 1e-2 \
66+
--adam_epsilon 1e-6 \
67+
--warmup_steps 10000 \
68+
--num_train_epochs 3 \
69+
--input_dir data/ \
70+
--output_dir pretrained_models/ \
71+
--logging_steps 1 \
72+
--save_steps 20000 \
73+
--max_steps 1000000 \
74+
--device gpu \
75+
--use_amp False</code></pre>
5676

57-
#### GPU训练
77+
其中参数释义如下:
78+
- `model_type` 指示了模型类型,使用BERT模型时设置为bert即可。
79+
- `model_name_or_path` 指示了某种特定配置的模型,对应有其预训练模型和预训练时使用的 tokenizer。若模型相关内容保存在本地,这里也可以提供相应目录地址。
80+
- `max_predictions_per_seq` 表示每个句子中会被mask的token的最大数目,与创建预训练数据时的设置一致。
81+
- `batch_size` 表示每次迭代**每张卡**上的样本数目。
82+
- `learning_rate` 表示基础学习率大小,将于learning rate scheduler产生的值相乘作为当前学习率。
83+
- `weight_decay` 表示AdamW优化器中使用的weight_decay的系数。
84+
- `adam_epsilon` 表示AdamW优化器中使用的epsilon值。
85+
- `warmup_steps` 表示动态学习率热启的step数。
86+
- `num_train_epochs` 表示训练轮数。
87+
- `input_dir` 表示输入数据的目录,该目录下所有文件名中包含training的文件将被作为训练数据。
88+
- `output_dir` 表示模型的保存目录。
89+
- `logging_steps` 表示日志打印间隔。
90+
- `save_steps` 表示模型保存及评估间隔。
91+
- `max_steps` 表示最大训练步数,达到`max_steps`后就提前结束。注意,我们必须设置 `max_steps`
92+
- `device` 表示训练使用的设备, 'gpu'表示使用GPU, 'xpu'表示使用百度昆仑卡, 'cpu'表示使用CPU。
93+
- `use_amp` 指示是否启用自动混合精度训练。
94+
</details>
95+
96+
#### GPU训练(Trainer版本)
5897
```shell
5998
unset CUDA_VISIBLE_DEVICES
60-
python -m paddle.distributed.launch --gpus "0" run_pretrain.py \
99+
python -m paddle.distributed.launch --gpus "0" run_pretrain_trainer.py \
61100
--model_type bert \
62101
--model_name_or_path bert-base-uncased \
63102
--max_predictions_per_seq 20 \
64-
--batch_size 32 \
103+
--per_device_train_batch_size 32 \
65104
--learning_rate 1e-4 \
66105
--weight_decay 1e-2 \
67106
--adam_epsilon 1e-6 \
@@ -73,12 +112,13 @@ python -m paddle.distributed.launch --gpus "0" run_pretrain.py \
73112
--save_steps 20000 \
74113
--max_steps 1000000 \
75114
--device gpu \
76-
--use_amp False
115+
--fp16 False \
116+
--do_train
77117
```
78118

79-
#### XPU训练
80-
```shell
81-
unset FLAGS_selected_xpus
119+
<details>
120+
<summary>XPU训练</summary>
121+
<pre><code>unset FLAGS_selected_xpus
82122
python -m paddle.distributed.launch --xpus "0" run_pretrain.py \
83123
--model_type bert \
84124
--model_name_or_path bert-base-uncased \
@@ -95,8 +135,8 @@ python -m paddle.distributed.launch --xpus "0" run_pretrain.py \
95135
--save_steps 20000 \
96136
--max_steps 1000000 \
97137
--device xpu \
98-
--use_amp False
99-
```
138+
--use_amp False</code></pre>
139+
100140
其中参数释义如下:
101141
- `model_type` 指示了模型类型,使用BERT模型时设置为bert即可。
102142
- `model_name_or_path` 指示了某种特定配置的模型,对应有其预训练模型和预训练时使用的 tokenizer。若模型相关内容保存在本地,这里也可以提供相应目录地址。
@@ -114,6 +154,48 @@ python -m paddle.distributed.launch --xpus "0" run_pretrain.py \
114154
- `max_steps` 表示最大训练步数,达到`max_steps`后就提前结束。注意,我们必须设置 `max_steps`
115155
- `device` 表示训练使用的设备, 'gpu'表示使用GPU, 'xpu'表示使用百度昆仑卡, 'cpu'表示使用CPU。
116156
- `use_amp` 指示是否启用自动混合精度训练。
157+
</details>
158+
159+
#### XPU训练(Trainer版本)
160+
```shell
161+
unset FLAGS_selected_xpus
162+
python -m paddle.distributed.launch --xpus "0" run_pretrain_trainer.py \
163+
--model_type bert \
164+
--model_name_or_path bert-base-uncased \
165+
--max_predictions_per_seq 20 \
166+
--per_device_train_batch_size 32 \
167+
--learning_rate 1e-4 \
168+
--weight_decay 1e-2 \
169+
--adam_epsilon 1e-6 \
170+
--warmup_steps 10000 \
171+
--num_train_epochs 3 \
172+
--input_dir data/ \
173+
--output_dir pretrained_models/ \
174+
--logging_steps 1 \
175+
--save_steps 20000 \
176+
--max_steps 1000000 \
177+
--device xpu \
178+
--fp16 False \
179+
--do_train
180+
```
181+
其中参数释义如下:
182+
- `model_type` 指示了模型类型,使用BERT模型时设置为bert即可。
183+
- `model_name_or_path` 指示了某种特定配置的模型,对应有其预训练模型和预训练时使用的 tokenizer。若模型相关内容保存在本地,这里也可以提供相应目录地址。
184+
- `max_predictions_per_seq` 表示每个句子中会被mask的token的最大数目,与创建预训练数据时的设置一致。
185+
- `per_device_train_batch_size` 表示用于训练的每个 GPU 核心/CPU 的batch大小.(`int`,可选,默认为 8)
186+
- `learning_rate` 表示基础学习率大小,将于learning rate scheduler产生的值相乘作为当前学习率。
187+
- `weight_decay` 表示AdamW优化器中使用的weight_decay的系数。
188+
- `adam_epsilon` 表示AdamW优化器中使用的epsilon值。
189+
- `warmup_steps` 表示动态学习率热启的step数。
190+
- `num_train_epochs` 表示训练轮数。
191+
- `input_dir` 表示输入数据的目录,该目录下所有文件名中包含training的文件将被作为训练数据。
192+
- `output_dir` 表示模型的保存目录。
193+
- `logging_steps` 表示日志打印间隔。
194+
- `save_steps` 表示模型保存及评估间隔。
195+
- `max_steps` 表示最大训练步数,达到`max_steps`后就提前结束。注意,我们必须设置 `max_steps`
196+
- `device` 表示训练使用的设备, 'gpu'表示使用GPU, 'xpu'表示使用百度昆仑卡, 'cpu'表示使用CPU。
197+
- `fp16` 是否使用 fp16 混合精度训练而不是 fp32 训练。(`bool`, 可选, 默认为 `False`)
198+
- `do_train` 是否进行训练任务。(`bool`, 可选, 默认为 `False`)
117199

118200
**NOTICE**: 预训练时data目录存放的是经过 `create_pretraining_data.py` 处理后的数据,因此需要通过该数据处理脚本预先处理,否则预训练将会出现报错。
119201

@@ -123,34 +205,38 @@ python -m paddle.distributed.launch --xpus "0" run_pretrain.py \
123205

124206
```shell
125207
unset CUDA_VISIBLE_DEVICES
126-
python -m paddle.distributed.launch --gpus "0" run_glue.py \
127-
--model_type bert \
208+
python -m paddle.distributed.launch --gpus "0" run_glue_trainer.py \
128209
--model_name_or_path bert-base-uncased \
129210
--task_name SST2 \
130211
--max_seq_length 128 \
131-
--batch_size 32 \
212+
--per_device_train_batch_size 32 \
213+
--per_device_eval_batch_size 32 \
132214
--learning_rate 2e-5 \
133215
--num_train_epochs 3 \
134216
--logging_steps 1 \
135217
--save_steps 500 \
136218
--output_dir ./tmp/ \
137219
--device gpu \
138-
--use_amp False
220+
--fp16 False\
221+
--do_train \
222+
--do_eval
139223
```
140224

141225
其中参数释义如下:
142-
- `model_type` 指示了模型类型,使用BERT模型时设置为bert即可。
143226
- `model_name_or_path` 指示了某种特定配置的模型,对应有其预训练模型和预训练时使用的 tokenizer。若模型相关内容保存在本地,这里也可以提供相应目录地址。注:`bert-base-uncased`等对应使用的预训练模型转自[huggingface/transformers](https://github.com/huggingface/transformers),具体可参考当前目录下converter中的内容。
144227
- `task_name` 表示Fine-tuning的任务。
145228
- `max_seq_length` 表示最大句子长度,超过该长度将被截断。
146-
- `batch_size` 表示每次迭代**每张卡**上的样本数目。
229+
- `per_device_train_batch_size` 表示用于训练的每个 GPU 核心/CPU 的batch大小.(`int`,可选,默认为 8)
230+
- `per_device_eval_batch_size` 表示用于评估的每个 GPU 核心/CPU 的batch大小.(`int`,可选,默认为 8)
147231
- `learning_rate` 表示基础学习率大小,将于learning rate scheduler产生的值相乘作为当前学习率。
148232
- `num_train_epochs` 表示训练轮数。
149233
- `logging_steps` 表示日志打印间隔。
150234
- `save_steps` 表示模型保存及评估间隔。
151235
- `output_dir` 表示模型保存路径。
152236
- `device` 表示训练使用的设备, 'gpu'表示使用GPU, 'xpu'表示使用百度昆仑卡, 'cpu'表示使用CPU, 'npu'表示使用华为昇腾卡。
153-
- `use_amp` 指示是否启用自动混合精度训练。
237+
- `fp16` 是否使用 fp16 混合精度训练而不是 fp32 训练。(`bool`, 可选, 默认为 `False`)
238+
- `do_train` 是否进行训练任务。(`bool`, 可选, 默认为 `False`)
239+
- `do_eval` 是否进行评估任务。同上。(`bool`, 可选, 默认为 `False`)
154240

155241
基于`bert-base-uncased`在GLUE各评测任务上Fine-tuning后,在验证集上有如下结果:
156242

model_zoo/bert/create_pretraining_data.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,18 @@
1515
# limitations under the License.
1616
"""Create masked LM/next sentence masked_lm examples for BERT."""
1717
import argparse
18-
import logging
18+
import collections
1919
import os
2020
import random
2121
from io import open
22+
2223
import h5py
2324
import numpy as np
2425
from tqdm import tqdm
2526

2627
from paddlenlp.transformers import BertTokenizer
2728
from paddlenlp.transformers.tokenizer_utils import convert_to_unicode
2829

29-
import random
30-
import collections
31-
3230

3331
class TrainingInstance(object):
3432
"""A single training instance (sentence pair)."""
@@ -243,7 +241,7 @@ def create_instances_from_document(
243241
is_random_next = False
244242
for j in range(a_end, len(current_chunk)):
245243
tokens_b.extend(current_chunk[j])
246-
truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng)
244+
truncate_seq_pair(tokens_a, tokens_b, target_seq_length, rng)
247245

248246
assert len(tokens_a) >= 1
249247
assert len(tokens_b) >= 1
@@ -400,7 +398,7 @@ def main():
400398
"and ignore vocab_file and do_lower_case.",
401399
)
402400

403-
## Other parameters
401+
# Other parameters
404402
# int
405403
parser.add_argument(
406404
"--max_seq_length",

model_zoo/bert/export_model.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import os
1717

1818
import paddle
19-
20-
from run_glue import MODEL_CLASSES
19+
from run_glue_trainer import MODEL_CLASSES
2120

2221

2322
def parse_args():

model_zoo/bert/predict_glue.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import paddle
1919
from datasets import load_dataset
20-
from run_glue import METRIC_CLASSES, MODEL_CLASSES, task_to_keys
20+
from run_glue_trainer import METRIC_CLASSES, MODEL_CLASSES, task_to_keys
2121

2222
from paddlenlp.data import Dict, Pad
2323

0 commit comments

Comments
 (0)