Skip to content

Latest commit

History

371 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Features

提供3大功能:

  • LLM模型预训练:支持常见模型的预训练,包括:decoder结构(LLaMA、GPT)、encoder结构(GLM)
  • LLM模型评测:参考GPT类模型,基于ZeroShot和FewShot实现
  • ChatGPT模型训练pipeline:根据Learning to Summarize from human feedback ,实现3大流程: SFT、Reward Model和RLHF
    • 支持RLHF阶段 (1) 联合优化reward和policy (2) 单独优化policy,冻结reward
    • 支持DPO作为Reward+RLHF的替代方案,可显著降低显存占用,同时实现RL的效果

Setup

1. Install deepspeed

git clone https://github.com/microsoft/DeepSpeed.git
cd deepspeed
rm -rf build
TORCH_CUDA_ARCH_LIST="7.0" DS_BUILD_OPS=1 pip install -e . --global-option="build_ext" --global-option="-j8" --no-cache -v --disable-pip-version-check 2>&1| tee build.log

如果想创建binary wheel,方便在其他机器上安装,可使用如下命令,会在dist目录生成类似可安装文件deepspeed-0.3.13+8cd046f-cp38-cp38-linux_x86_64.whl

git clone https://github.com/microsoft/DeepSpeed.git
cd deepspeed
rm -rf build
TORCH_CUDA_ARCH_LIST="7.0" DS_BUILD_OPS=1 python setup.py build_ext -j8 bdist_wheel 2>&1| tee build.log

PS:需要根据下图,调整TORCH_CUDA_ARCH_LIST="7.0"为自己对应的NVIDIA GPU架构 image info

或运行torch.cuda.get_device_capability()获取自己GPU的架构

2. Install jieba

在使用Pangu类模型的时候,其special_token格式为<sep><pad>等,而tokenization_gptpangu.pytokenize()函数会使用jieba进行分词。但直接pip install jieba,默认会将<>直接切分开,使用jieba.add_word("<sep>")也没有作用,因为jieba直接hardcode了会自动切分的token,其中就包括了<>

因此需要执行:

git clone https://github.com/fxsjy/jieba.git
cd jieba

将代码clone到本地,修改jieba/__init__.pyre_han_default的取值,具体改动如下:

  • 改动前:
re_han_default=re.compile("([\u4E00-\u9FD5a-zA-Z0-9+#&\._%\-]+)", re.U)
  • 改动后:
re_han_default=re.compile("([\u4E00-\u9FD5a-zA-Z0-9+#&\._%\-<>]+)", re.U)

修改完成后使用pip install .进行本地编译安装,替换原有jieba。安装完成后,在代码中加入jieba.add_word("<sep>")(该代码已加入tokenization_gptpangu.py),即可解决将<sep>一类的special token切分为多个id的情况

3. Install apex (Optional)

git clone https://github.com/NVIDIA/apex
cd apex
pip install --global-option="--cpp_ext" --global-option="--cuda_ext" --no-cache -v --disable-pip-version-check .2>&1| tee build.log

如果想创建binary wheel,方便在其他机器上安装,可使用如下命令,会在dist目录生成类似可安装文件apex-0.0.1+7150e20-cp38-cp38-linux_x86_64.whl

git clone https://github.com/NVIDIA/apex
cd apex
python setup.py --cpp_ext --cuda_ext bdist_wheel 2>&1| tee build.log

Data & Model Download

1. 预训练模型下载

模型sizehuggingface地址百度网盘地址提取码
Pangu-350M659MBsunzeyeah/pangu-350MPangu-350Mc5jj
Pangu-2.6B9.8GBsunzeyeah/pangu-2_6BPangu-2.6B2rad
Pangu-13B23.6GBsunzeyeah/pangu-13BPangu-13Bu3dx
GLM-350M-chinese679MBsunzeyeah/glm-350M-chineseGLM-350M-chineseii8e
GLM-10B-chinese18.4Gsunzeyeah/glm-10B-chineseGLM-10B-chinesefynj
ChatGLM-6B25.6Gsunzeyeah/chatglm-6BChatGLM-6Buq1k

PS: 本repo提供的预训练模型下载中,

  • 对于pytorch_model*.bin
    • 如果源文件已包括,则不做改动
    • 如果源文件不包括,则根据其提供的checkpoint转换为pytorch_model*.bin
  • 其余文件可能相对原文件有改动,包括:modeling_*.py、tokenization_*.py、configuration_*.py、config.json和tokenizer.config

2. 数据下载

数据集sizehuggingface地址百度网盘地址提取码
CLUE Benchmark500MBCLUE Benchmarkm6gt
SFT & Reward Data5GBsunzeyeah/chinese_chatgpt_corpusSFT & Reward Dataecyc
百科652MBbaike_qa_20197jad
知道问答847MBzhidaoneds
对联221MBcouplets54ey
古文125MBClassical & Moderna4cr
古诗词87MBchinese poetry5zzj
微博新闻评论522MBweibo summary commentsw0g1

PS: SFT & Reward Data基于百科、知道问答、对联、古文、古诗词、微博新闻评论数据构造,可直接用于SFT和Reward阶段训练。详见data_prepare.py

Usage

1. LLM模型预训练

对开源LLM进行增量预训练,基于deepspeed实现。目前支持2类模型架构:

  • decoder结构:LLaMA、Baichuan、Pangu
  • encoder结构:GLM、ChatGLM
cd examples
bash pretrain.sh

2. LLM模型评测

对开源中文LLM进行ZeroShot、OneShot或FewShot的评测。详见eval_pretrain.pydata.py

目前支持的评测任务:

目前支持的开源模型:

  • LLaMA及相关衍生模型
  • ChatGLM(1和2)
  • Baichuan
  • Qwen
  • Pangu
  • GLM
cd examples
bash eval_pretrain.sh

3. SFT

使用开源LLM + SFT&Reward数据进行SFT训练

cd examples
bash train_sft.sh

4. Reward Model

使用SFT模型 + SFT&Reward数据进行Reward模型训练

cd examples
bash train_reward.sh

5. RLHF

利用PPO算法和Reward Model,进一步更新SFT模型。基于开源框架DeepSpeedChat 实现

cd examples
bash train_rlhf.sh

6. DPO

利用DPO算法替代Reward+RLHF的pipeline,免去训练Reward模型,同时达到RL训练的效果,该方法可显著降低显存占用。基于开源框架trl 实现

cd examples
bash train_dpo.sh

Results

1. LLM模型评测

C-Eval 5-shot测试集(test)结果
ModelAvgAvg(Hard)STEMSocial ScienceHumanitiesOther
Baichuan2-13B-Chat56.3034.2048.2070.0060.5054.20
xverse-13B55.3032.5045.9066.7059.5057.60
Qwen-7B-Chat54.7035.4047.9068.3058.7050.00
Baichuan-13B-Base53.7035.6046.8065.8058.0050.80
Baichuan2-7B-Chat52.5033.8045.7064.2056.6050.20
ChatGLM2-6B51.2033.4046.9063.0051.6047.70
Baichuan-13B-Chat47.9031.5041.4056.8053.0046.50
Baichuan-7B44.2031.7039.2053.3047.3041.90
Ziya-LLaMA-13B-v1.140.1030.3035.8047.3042.8038.50
ChatGLM1.1-6B38.1028.6033.6046.7040.9035.70
AtomGPT-13B-56k37.6025.3032.0044.7042.8036.10
LLaMA2-13B-chat37.1029.3034.6043.6035.9037.00
ChatGLM-6B36.3027.2032.9042.8038.1034.90
LLaMA-30B35.9029.9034.4042.4033.3035.60
LLaMA2-7B-chat33.5027.3031.6038.1033.8032.70
Ziya-LLaMA-13B-Pretrain-v131.1022.2027.4036.5033.8030.40
LLaMA-13B29.824.2028.4033.7029.6029.00
LLaMA-7B26.8026.7026.2027.6025.7028.10
MMLU 5-shot测试集(test)结果
ModelAvgSTEMSocial ScienceHumanitiesOther
Baichuan2-13B-Chat56.9047.2866.2352.9063.50
LLaMA-30B56.3344.6865.6454.6061.57
xverse-13B55.2445.6064.5150.3263.27
Qwen-7B-Chat54.1341.7663.4350.8162.50
LLaMA2-13B-chat53.9844.5263.4049.3761.21
Baichuan-13B-Base53.4643.8663.1449.7359.28
Baichuan2-7B-Chat53.1143.5162.2649.5859.12
Baichuan-13B-Chat51.1241.6159.1147.5258.31
Ziya-LLaMA-13B-v1.151.0641.8957.7149.2256.54
LLaMA2-7B-chat48.1039.6456.2843.6155.39
LLaMA-13B46.5137.2352.7144.3553.04
ChatGLM2-6B45.8338.7552.0643.2050.82
AtomGPT-13B-56k42.7536.0249.0438.8049.30
Baichuan-7B41.9636.6347.7737.5548.31
Ziya-LLaMA-13B-Pretrain-v141.6133.6146.0139.8548.05
ChatGLM1.1-6B40.0732.9544.5539.2344.12
ChatGLM-6B37.8732.4143.8035.6041.00
LLaMA-7B28.5326.1028.7628.5224.81
CLUEBenchmark 验证集(dev.json)结果
DatasetMethodMetricsTask TypeZero-shotFew-shot
GLM-350M-chinesePangu-350MPangu-2.6BGLM-10B-chinesePangu-13BGLM-350M-chinesePangu-350MPangu-2.6BGLM-10B-chinesePangu-13B
OCNLIPPLaccNLI0.30740.33690.30610.32880.33010.32980.33520.3216
CMNLIPPLaccNLI0.32790.33020.33100.33380.33580.33560.33280.3300
CHIDPPLaccCloze(multi-choices)0.07340.09160.06700.10160.10180.09790.10070.0996
CMRC2018generationf1MRC0.0930.09790.10070.13920.0210.093450.0970.1007
CLUEWSC2020PPLaccWSC0.49340.53280.55920.51310.46710.55260.44730.4671
C3PPLaccCommon sense reasoning0.23600.24260.24180.25730.25670.24760.25590.2515
AFQMCPPLaccText classification0.63060.45820.49140.49600.50000.48720.49930.5018
CSLPPLaccText classification0.49430.49130.46660.51260.49960.51400.50360.4973
IFLYTEKPPLaccText classification0.12920.30580.2650.26200.24080.25390.25350.2524
TNEWSPPLaccText classification0.15820.20220.24490.24890.25270.25550.24660.2494

2. SFT

模型训练参数:

模型可训练参数量数据量batch sizesequence length硬件显存占用speedHours per epoch
GLM-350M-chinese355M5.4M4512V100 16G13G3.7 s/iter88h
Pangu-350M345M5.4M48512A100 80G78G1.91 s/iter27.5h
Pangu-2.6B2.6B5.4M8512A100 80G79.4G9.61 s/iter116h

SFT模型下载:

预训练模型SFT数据集sizehuggingface地址百度网盘地址提取码
Pangu-350MSFT & Reward Data1.3GBsunzeyeah/pangu-350M-sftPangu-350M-SFT0f8j
Pangu-2.6BSFT & Reward Data9.8GBsunzeyeah/pangu-2_6B-sftPangu-2.6B-SFTr13w
Pangu-2.6B-SFT生成示例
# 生成参数
max_length = 512
max_length_generation = 100
top_k = 10
temperature = 0.8
do_sample=True
[Input] 我不能确定对方是不是喜欢我,我却想分分秒秒跟他在一起,有谁能告诉我如何能想他少一点<sep>回答:
[Generated] 你爱他就多关心他,少在他面前表现出你的脆弱。这样他才更会爱你。
[Input] 一次次买彩,一次次倾听那比分,一次次兴奋,一次次失落,一次次无归而返!最终还是任8~~~~<sep>回答:
[Generated] 我也是这样,买彩的时间也不长,但是每次都是这样。。。。。。。
[Input] 现代文:太祖很高兴地说:凭着这样的部下去作战,还担心不能取胜么?<sep>古文:
[Generated] 太祖悦曰:此自是以去,何忧不胜?
[Input] 现代文:十一年秋,丞相伯颜与阿术会师襄阳,遣阿剌罕率诸翼军攻郢、复诸州,十月夺取郢州之南门堡。<sep>古文:
[Generated] 十一年秋,丞相伯颜与阿术会师襄阳,遣阿剌罕率诸翼军攻郢、复诸州,十月夺郢州之南门堡。
[Input] 以孔⁇的风格,写一篇经书,题为中庸<sep>
[Generated] 我闻昔者夫子之言也,曰:吾道一以贯之。其或行之或不行,吾未知之。是故君子慎其独也。吾未见好德如好色者也。好德如好色者,未之有也。好德如好色者,既已之有也。君子不以言举人,不以人废言。好德如好色者,其如命何!吾
[Input] 以陆游的风格,写一首七言诗,题为晨起<sep>
[Generated] 秋光满纸清⁇明,睡醒一江万马奔。自笑身如枯木鹤,何妨食饱作闲人。
[Input] 上联:霜风几笔雕残画<sep>下联:
[Generated] 云海千山叠翠峰
[Input] 新闻内容:据悉,这名14岁的女生在Facebook上发布了戏谑考试的博文,其中使用了胡志明1946年反对法国殖民者的讲话,随后被越南当局停课一年。越南官员称,该学生歪曲了历史事件,侮辱了老师的情感。多数网友虽对越南当局做法不满,但都表示,至少他们还能上Facebook.<sep>摘要:越南女生Facebook上模仿胡志明讲话被停课<sep>评论:
[Generated] 这女的,还真有勇气,我喜欢你!
[Input] 新闻内容:习近平天津考察时勉励大学生志存高远、脚踏实地,转变择业观念,坚持从实际出发,勇于到基层一线和艰苦地方去,把人生的路一步步走稳走实,善于在平凡岗位上创造不平凡的业绩<sep>摘要:习近平勉励大学生转变择业观念勇于到基层和艰苦地方去<sep>评论:
[Generated] 现在的社会,很多年轻人都是在父母的安排下,去了不同的单位,在不同的地方工作,在不同的城市生活,这就导致了年轻人的思维方式,价值观都不一样,这也导致了年轻人的成长道路,也有很多的不安全感,这也是很正常的事情,年轻人需要正确的引导,正确的引导,也需要正确的引导!
[Input] 问题:劳动仲裁申请法院强制执行,法院强制执行需要多长时间<sep>回答:
[Generated] 一般申请法院强制执行需要3-6个月时间,具体需要看法院的执行措施。
[Input] 问题:银行贷款50万,30年!年利率是4.41怎么算一个月还多少钱<sep>回答:
[Generated] 若申请的是招行贷款,要计算贷款利息或者每月还款金额,需要知道贷款本金,贷款期限,还款方式,贷款年利率。若上述信息都确认到,可以通过我行贷款计算器尝试计算,登录招行官网右下方找到“理财计算器”--“个人贷款计算器”可以计算。

3. Reward Model

模型训练参数:

模型可训练参数量数据量batch sizesequence length硬件显存占用speedHours per epoch
Pangu-350M131M12M32512A100 80G72.6G1.91 s/iter105h
Pangu-2.6B815M12M8512A100 80G80.7G423h

Reward模型下载:

预训练模型SFT数据集sizehuggingface地址百度网盘地址提取码
Pangu-350MSFT & Reward Data1.3GBsunzeyeah/pangu-350M-rewardPangu-350M-Reward4gju

4. RLHF

To be updated

5. DeepSpeed实验

为验证不同预训练模型使用deepspeed的训练效率是否能达到官方宣称的效果(加速、节省GPU等),进行了benchmarking

  • 实验场景:SFT阶段训练
  • 实验参数:max_sequence_length=512
DeepSpeed实验结果
模型数据整体耗时/epoch单条样本耗时内存使用量显存使用量GPU型号和数量fp16bf16deepspeed stageoffload optimizerpin memoryoffloard paramoverlap commallgather bucket sizestage3 max live parametersbatch sizegradient accumulation stepsgradient checkpointingmodel half
T5-largewmt16-en-ro, 共计61万条样本43h0.5s/it7.1G1*14529MB1*V100 16Gtrue--------28falsefalse
152h1.78s/it38.26G1*11663MB1*V100 16Gtrue-2truetrue-false2e8-28falsefalse
250h2.95s/it38.74G1*7255MB1*V100 16Gtrue-2truetrue-false1e5-28falsefalse
62h5.8s/it86.81G8*7811MB8*V100 16Gtrue-2truetrue-false1e5-28falsefalse
---OOM1*V100 16Gtrue-2truetrue-false2e8-168falsefalse
---OOM1*V100 16Gtrue-2truetrue-false1e5-168falsefalse
290h3.48s/it46.53G1*6655MB1*V100 16Gtrue-3truetruetruefalse2e82e828falsefalse
380h4.5s/it43.48G1*5263MB1*V100 16Gtrue-3truetruetruefalse1e51e528falsefalse
215h4.9s/it47.31G2*5019MB2*V100 16Gtrue-3truetruetruefalse1e51e528falsefalse
1370h64s/it57.55G4*4701MB4*V100 16Gtrue-3truetruetruefalse1e51e528falsefalse
948h90s/it72.54G8*4585MB8*V100 16Gtrue-3truetruetruefalse1e51e528falsefalse
Pangu-2.6BSFT & Reward Data的验证集,共1万条样本2h5.76s/it67.86G1*15631MB1*V100 16Gtrue-2truetrue-false2e8-28falsefalse
2.1h6.15s/it67.88G1*15705MB1*V100 16Gtrue-2truetrue-false1e5-28falsefalse
4.5h13.3s/it81.02G1*15449MB1*V100 16Gtrue-3truetruetruefalse2e82e828falsefalse
11.5h8.2s/it75.89G1*15299MB1*V100 16Gtrue-3truetruetruefalse1e51e528falsefalse
5.5h7.8s/it81.16G2*14851MB2*V100 16Gtrue-3truetruetruefalse1e51e528falsefalse
6.2h18.3s/it97.31G4*14389MB4*V100 16Gtrue-3truetruetruefalse1e51e528falsefalse
6.6h38s/it118.82G8*14335MB8*V100 16Gtrue-3truetruetruefalse1e51e528falsefalse
ChatGLM-6BSFT & Reward Data的验证集,共1万条样本--120.45GOOM1*V100 16Gtrue-2truetrue-false1e5-18truetrue
--120.48GOOM1*V100 16Gtrue-2truetrue-false1e3-18falsetrue
--153.02GOOM1*V100 16Gtrue-3truetruetruefalse1e21e218falsetrue
--154GOOM1*V100 16Gtrue-3truetruetruefalse2e82e818truetrue
21.2h60s/it154G1*10443MB1*V100 16Gtrue-3truetruetruefalse2e8auto18truetrue
21.5h60s/it152.81G1*10409MB1*V100 16Gtrue-3truetruetruefalse1e51e518truetrue
23.5h65s/it153.36G1*9229MB1*V100 16Gtrue-3truetruetruefalse1e31e318truetrue
14h80s/it158.21G2*8631MB2*V100 16Gtrue-3truetruetruefalse1e31e318truetrue
7.8h90s/it168.38G4*6743MB4*V100 16Gtrue-3truetruetruefalse1e31e318truetrue
4h90s/it189.34G8*6729MB8*V100 16Gtrue-3truetruetruefalse1e31e318truetrue
1h100s/it189.38G8*10047MB8*V100 16Gtrue-3truetruetruefalse1e31e348truetrue
50min40s/it189.39G8*14763MB8*V100 16Gtrue-3truetruetruefalse1e31e382truetrue
35min113s/it189.39G8*14763MB8*V100 16Gtrue-3truetruetruefalse1e31e388truetrue
--189.34GOOM8*V100 16Gtrue-3truetruetruefalse1e31e3108truetrue
GLM-10B-ChineseSFT & Reward Data的验证集,共1万条样本---OOM1*V100 16Gtrue-3truetruetruefalse2e82e818truefalse
---OOM1*V100 16Gtrue-3truetruetruefalse2e8auto18truefalse
---OOM1*V100 16Gtrue-3truetruetruefalse1e51e518truefalse
---OOM1*V100 16Gtrue-3truetruetruefalse1e31e318truefalse
---OOM1*V100 16Gtrue-3truetruetruefalse1e21e218truefalse
---OOM2*V100 16Gtrue-3truetruetruefalse1e21e218truefalse
---OOM4*V100 16Gtrue-3truetruetruefalse1e21e218truefalse
--OOM-8*V100 16Gtrue-3truetruetruefalse1e21e218truefalse
---OOM4*V100 16Gtrue-3truetruetruefalse1e21e218truetrue
---OOM6*V100 16Gtrue-3truetruetruefalse1e21e218truetrue
--OOM-8*V100 16Gtrue-3truetruetruefalse1e21e218truetrue

PS: deepspeed的参数介绍和调优经验,可参见DeepSpeed Configuration

6. LoRA实验

为验证LoRA的训练效率提升,进行了benchmarking

  • 实验场景:SFT阶段训练
  • 实验数据:SFT & Reward Data的验证集,共1万条样本
  • 实验参数:max_sequence_length=512, lora_alpha=1, lora_train_bias='none'
LoRA实验结果
模型LoRA rank可训练参数量deepspeedbatch sizeGPU型号和数量显存使用量单条样本耗时整体耗时/epoch
Pangu-2.6B-2.6B-81*A100 80G1*79421MB9.66s/it12.5min
10001.5B-81*A100 80G1*76129MB11.61s/it15min
500758MB-121*A100 80G1*77179MB16.2s/it14min
100151MB-161*A100 80G1*81103MB18.6s/it12min
5075MB-161*A100 80G1*80809MB17.8s/it11.5min
1015MB-161*A100 80G1*78735MB17.6s/it11.5min
100151MBstage=2, w offloading241*A100 80G1*76933MB25.5s/it11min
100151MBstage=3, w offloading241*A100 80G1*77259MB46.5s/it20min
ChatGLM-6B-6.2B-31*A100 80G1*79206MB6.7s/it23.5min
10001.9B-61*A100 80G1*78840MB12.8s/it22.5min
500994MB-61*A100 80G1*68832MB12.4s/it21.5min

About

Implementation of Chinese ChatGPT

Topics

Resources

Stars

285 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages