Skip to content

Commit 84257cd

Browse files
authored
GPT, spilt modeling to gpt-3 for training (PaddlePaddle#710)
* GPT. refine args print. * GPT split to gpt-3.
1 parent d66300b commit 84257cd

File tree

13 files changed

+1495
-149
lines changed

13 files changed

+1495
-149
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# GPT-3 千亿参数模型训练
2+
3+
## 模型介绍
4+
GPT-[3](https://arxiv.org/pdf/2005.14165.pdf) 是以[Transformer](https://arxiv.org/abs/1706.03762) 为基础的语言生成模型。GPT-3模型的最大参数量可以达到170B,如此大规模参数的模型对于训练使用的深度学习框架是一个巨大的挑战。
5+
6+
本示例主要提供了GPT-3的训练过程,数据准备、预测部署等内容请参见[GPT](../gpt) 目录。
7+
本示例包含了GPT-3的[静态图](./static)[动态图](./dygraph)的多级并行训练流程。
8+
用户可以根据自己的需求,训练GPT-3模型,或者参考本示例,使用模型并行、流水线并行等策略,开发训练其他大模型。
9+
10+
11+
## 使用方法
12+
13+
```shell
14+
cd static # 或者 cd dygraph
15+
# 下载样例数据
16+
mkdir data && cd data
17+
wget https://paddlenlp.bj.bcebos.com/models/transformers/gpt/train.data.json_ids.npz
18+
cd ..
19+
# 运行pretrian 脚本
20+
sh run.sh
21+
```
22+
下面以静态图的运行脚本为例,说明训练参数的具体作用:
23+
```shell
24+
python -u -m paddle.distributed.fleet.launch \
25+
--gpus "0,1,2,3,4,5,6,7" \
26+
--log_dir "output/$task_name/log" run_pretrain_static.py \
27+
--model_type "gpt" \
28+
--model_name_or_path "gpt2-en" \
29+
--input_dir "./data" \
30+
--output_dir "output/$task_name" \
31+
--max_seq_len 1024 \
32+
--micro_batch_size 8 \
33+
--global_batch_size 32 \
34+
--sharding_degree 4\
35+
--mp_degree 2 \
36+
--dp_degree 1 \
37+
--pp_degree 1 \
38+
--use_sharding true \
39+
--use_amp true \
40+
--use_recompute true \
41+
--max_lr 0.00015 \
42+
--min_lr 0.00001 \
43+
--max_steps 500000 \
44+
--save_steps 100000 \
45+
--decay_steps 320000 \
46+
--weight_decay 0.01\
47+
--warmup_rate 0.01 \
48+
--grad_clip 1.0 \
49+
--logging_freq 1\
50+
--eval_freq 10000 \
51+
--device "gpu"
52+
```
53+
其中参数释义如下:
54+
- `model_name_or_path` 要训练的模型或者之前训练的checkpoint。
55+
- `input_dir` 指定输入文件,可以使用目录,指定目录时将包括目录中的所有文件。
56+
- `output_dir` 指定输出文件。
57+
- `max_seq_len` 输入文本序列的长度。
58+
- `micro_batch_size` 单卡单次的 batch size大小。即单张卡运行一次前向网络的 batch size大小。
59+
- `global_batch_size` 全局的batch size大小,即一次参数更新等效的batch size。
60+
- `mp_degree` 模型并行划分的数(如 mp_degree=2 表示将计算的Tensor划分到两个设备)。
61+
- `sharding_degree` 切参数切分的分组大小(如 sharding_degree=4 表示参数分为4组,分别到4个设备)。
62+
- `pp_degree` 流水线并行参数,表示将网络划分成多少段。
63+
- `dp_degree` 数据并行参数。
64+
- `use_sharding` 开启sharding策略
65+
- `use_amp` 开启混合精度策略。
66+
- `use_recompute` 开启重计算策略。
67+
- `max_lr` 训练学习率。
68+
- `min_lr` 学习率衰减的最小值。
69+
- `max_steps` 最大训练步数。
70+
- `save_steps` 保存模型间隔。
71+
- `weight_decay` 权重衰减参数。
72+
- `warmup_rate` 学习率warmup参数。
73+
- `grad_clip` 梯度裁剪范围。
74+
- `logging_freq` 日志输出间隔。
75+
- `eval_freq 模型评估间隔。
76+
- `device` 训练设备。
77+
78+
79+
- 一般而言,需要设置 `mp_degree * sharding_degree * pp_degree * dp_degree` = 训练机器的总卡数。
80+
- 一般而言, `global_batch_size = micro_batch_size * sharding_degree * dp_degree`。用户也可以使用梯度累积的方式增大global_batch_size。
81+
82+
### 飞桨 4D 并行简介
83+
84+
飞桨的4D混合并行包括一下4个维度:
85+
86+
- 模型并行(Model Parallelism,通过将乘法张量切片)
87+
- 参数分组切片的数据并行(Sharding)
88+
- 流水线并行(Pipeline Parallelism)
89+
- 纯数据并行(Data Parallelism)
90+
91+
具体内容可以参考稿件:[飞桨分布式训练又推新品,4D混合并行可训千亿级AI模型](https://baijiahao.baidu.com/s?id=1697085717806202673)
92+
93+
### 参考文献
94+
- [Language Models are Few-Shot Learners](https://arxiv.org/pdf/2005.14165.pdf)
95+
除了上述混合并行策略外,飞桨还支持重计算、offload、混合精度等策略,来减少显存占用、加速训练。
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../gpt/args.py
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../gpt/dataset.py
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../gpt/lr.py

0 commit comments

Comments
 (0)