【大模型】更强的开源可商用的中英文大语言模型baichuan2来了,从零开始搭建

baichuan-7B 可以查看这篇文章:
【AI实战】开源可商用的中英文大语言模型baichuan-7B,从零开始搭建

Baichuan 2 介绍

Baichuan 2 是百川智能推出的第二代开源大语言模型,采用 2.6 万亿 Tokens 的高质量语料训练。
Baichuan 2 在多个权威的中文、英文和多语言的通用、领域 benchmark 上取得同尺寸最佳的效果。
本次发布包含有 7B、13B 的 Base 和 Chat 版本,并提供了 Chat 版本的 4bits 量化。

技术报告

Baichuan 2: Open Large-scale Language Models

github 地址

https://github.com/baichuan-inc/Baichuan2

模型下载

  • huggingface

本次发布版本和下载链接见下表:

基座模型对齐模型对齐模型 4bits 量化
7B🤗 Baichuan2-7B-Base🤗 Baichuan2-7B-Chat🤗 Baichuan2-7B-Chat-4bits
13B🤗 Baichuan2-13B-Base🤗 Baichuan2-13B-Chat🤗 Baichuan2-13B-Chat-4bits

开放协议

所有版本对学术研究完全开放。同时,开发者通过邮件申请并获得官方商用许可后,即可免费商用

协议

对本仓库源码的使用遵循开源许可协议 Apache 2.0。对 Baichuan 2 模型的社区使用需遵循《Baichuan 2 模型社区许可协议》。Baichuan 2 支持商用。如果将 Baichuan 2 模型或其衍生品用作商业用途,请您通过邮箱 opensource@baichuan-inc.com 联系许可方,申请书面授权。

测试评估

通用领域测试

7B 模型结果

C-EvalMMLUCMMLUGaokaoAGIEvalBBH
5-shot5-shot5-shot5-shot5-shot3-shot
GPT-468.4083.9370.3366.1563.2775.12
GPT-3.5 Turbo51.1068.5454.0647.0746.1361.59
LLaMA-7B27.1035.1026.7527.8128.1732.38
LLaMA2-7B28.9045.7331.3825.9726.5339.16
MPT-7B27.1527.9326.0026.5424.8335.20
Falcon-7B24.2326.0325.6624.2424.1028.77
ChatGLM2-6B50.2045.9049.0049.4445.2831.65
Baichuan-7B42.8042.3044.0236.3434.4432.48
Baichuan2-7B-Base54.0054.1657.0747.4742.7341.56

13B 模型结果

C-EvalMMLUCMMLUGaokaoAGIEvalBBH
5-shot5-shot5-shot5-shot5-shot3-shot
GPT-468.4083.9370.3366.1563.2775.12
GPT-3.5 Turbo51.1068.5454.0647.0746.1361.59
LLaMA-13B28.5046.3031.1528.2328.2237.89
LLaMA2-13B35.8055.0937.9930.8332.2946.98
Vicuna-13B32.8052.0036.2830.1131.5543.04
Chinese-Alpaca-Plus-13B38.8043.9033.4334.7835.4628.94
XVERSE-13B53.7055.2158.4444.6942.5438.06
Baichuan-13B-Base52.4051.6055.3049.6943.2043.01
Baichuan2-13B-Base58.1059.1761.9754.3348.1748.78

法律、医疗

7B 模型结果

JEC-QACEval-MMLU-CMMLUMedQA-USMLEMedQA-MCMLEMedMCQA
5-shot5-shot5-shot5-shot5-shot
GPT-459.3277.1680.2874.5872.51
GPT-3.5 Turbo42.3161.1753.8152.9256.25
LLaMA-7B27.4533.3424.1221.7227.45
LLaMA2-7B29.2036.7527.4924.7837.93
MPT-7B27.4526.6716.9719.7931.96
Falcon-7B23.6625.3321.2918.0733.88
ChatGLM2-6B40.7644.5426.2445.5330.22
Baichuan-7B34.6442.3727.4239.4631.39
Baichuan2-7B-Base44.4656.3932.6854.9341.73

13B 模型结果

JEC-QACEval-MMLU-CMMLUMedQA-USMLEMedQA-MCMLEMedMCQA
5-shot5-shot5-shot5-shot5-shot
GPT-459.3277.1680.2874.5872.51
GPT-3.5 Turbo42.3161.1753.8152.9256.25
LLaMA-13B27.5435.1428.8323.3839.52
LLaMA2-13B34.0847.4235.0429.7442.12
Vicuna-13B28.3840.9934.8027.6740.66
Chinese-Alpaca-Plus-13B35.3246.3127.4932.6635.87
XVERSE-13B46.4258.0832.9958.7641.34
Baichuan-13B-Base41.3451.7729.0743.6739.60
Baichuan2-13B-Base47.4059.3340.3861.6242.86

数学、代码

7B 模型结果

GSM8KMATHHumanEvalMBPP
4-shot4-shot0-shot3-shot
GPT-489.9940.2069.5163.60
GPT-3.5 Turbo57.7713.9652.4461.40
LLaMA-7B9.783.0211.5914.00
LLaMA2-7B16.223.2412.8014.80
MPT-7B8.642.9014.0223.40
Falcon-7B5.461.68-10.20
ChatGLM2-6B28.896.409.159.00
Baichuan-7B9.172.549.206.60
Baichuan2-7B-Base24.495.5818.2924.20

13B 模型结果

GSM8KMATHHumanEvalMBPP
4-shot4-shot0-shot3-shot
GPT-489.9940.2069.5163.60
GPT-3.5 Turbo57.7713.9652.4461.40
LLaMA-13B20.553.6815.2421.40
LLaMA2-13B28.894.9615.2427.00
Vicuna-13B28.134.3616.4615.00
Chinese-Alpaca-Plus-13B11.982.5016.4620.00
XVERSE-13B18.202.1815.8516.80
Baichuan-13B-Base26.764.8411.5922.80
Baichuan2-13B-Base52.7710.0817.0730.20

多语言翻译

7B 模型结果

CN-ENCN-FRCN-ESCN-ARCN-RUCN-JPCN-DEAverage
GPT-429.9429.5620.0110.7618.6213.2620.8320.43
GPT-3.5 Turbo27.6726.1519.5810.7317.451.8219.7017.59
LLaMA-7B17.2712.029.540.004.471.418.737.63
LLaMA2-7B25.7615.1411.920.794.992.2010.1510.14
MPT-7B20.779.538.960.103.542.916.547.48
Falcon-7B22.1315.679.280.111.350.416.417.91
ChatGLM2-6B22.289.427.770.641.780.264.616.68
Baichuan-7B25.0716.5112.720.416.662.249.8610.50
Baichuan2-7B-Base27.2720.8716.171.3911.213.1112.7613.25

13B 模型结果

CN-ENCN-FRCN-ESCN-ARCN-RUCN-JPCN-DEAverage
GPT-429.9429.5620.0110.7618.6213.2620.8320.43
GPT-3.5 Turbo27.6726.1519.5810.7317.451.8219.7017.59
LLaMA-13B21.7516.1613.290.587.610.4110.6610.07
LLaMA2-13B25.4419.2517.491.3810.340.1311.1312.17
Vicuna-13B22.6318.0414.670.709.273.5910.2511.31
Chinese-Alpaca-Plus-13B22.5313.8211.290.281.520.318.138.27
XVERSE-13B29.2624.0316.672.7811.613.0814.2614.53
Baichuan-13B-Base30.2420.9015.920.989.652.6412.0013.19
Baichuan2-13B-Base30.6122.1117.272.3914.1711.5814.5316.09

推理和部署

推理所需的模型权重、源码、配置已发布在 Hugging Face,下载链接见本文档最开始的表格。我们在此示范多种推理方式。程序会自动从 Hugging Face 下载所需资源。

安装依赖

pip install -r requirements.txt

Python 代码方式

Chat 模型推理方法示范

>>> import torch
>>> from transformers import AutoModelForCausalLM, AutoTokenizer
>>> from transformers.generation.utils import GenerationConfig
>>> tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan2-13B-Chat", use_fast=False, trust_remote_code=True)
>>> model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Chat", device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
>>> model.generation_config = GenerationConfig.from_pretrained("baichuan-inc/Baichuan2-13B-Chat")
>>> messages = []
>>> messages.append({"role": "user", "content": "解释一下“温故而知新”"})
>>> response = model.chat(tokenizer, messages)
>>> print(response)
"温故而知新"是一句中国古代的成语,出自《论语·为政》篇。这句话的意思是:通过回顾过去,我们可以发现新的知识和理解。换句话说,学习历史和经验可以让我们更好地理解现在和未来。

这句话鼓励我们在学习和生活中不断地回顾和反思过去的经验,从而获得新的启示和成长。通过重温旧的知识和经历,我们可以发现新的观点和理解,从而更好地应对不断变化的世界和挑战。

Base 模型推理方法示范

>>> from transformers import AutoModelForCausalLM, AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan2-13B-Base", trust_remote_code=True)
>>> model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Base", device_map="auto", trust_remote_code=True)
>>> inputs = tokenizer('登鹳雀楼->王之涣\n夜雨寄北->', return_tensors='pt')
>>> inputs = inputs.to('cuda:0')
>>> pred = model.generate(**inputs, max_new_tokens=64, repetition_penalty=1.1)
>>> print(tokenizer.decode(pred.cpu()[0], skip_special_tokens=True))
登鹳雀楼->王之涣
夜雨寄北->李商隐

在上述两段代码中,模型加载指定 device_map='auto',会使用所有可用显卡。如需指定使用的设备,可以使用类似 export CUDA_VISIBLE_DEVICES=0,1(使用了0、1号显卡)的方式控制。

命令行工具方式

python cli_demo.py

本命令行工具是为 Chat 场景设计,因此我们不支持使用该工具调用 Base 模型。

网页 demo 方式

依靠 streamlit 运行以下命令,会在本地启动一个 web 服务,把控制台给出的地址放入浏览器即可访问。本网页 demo 工具是为 Chat 场景设计,因此我们不支持使用该工具调用 Base 模型。

streamlit run web_demo.py

量化部署

为了让不同的用户以及不同的平台都能运行 Baichuan 2 模型,我们针对 Baichuan 2 模型做了相应地量化工作(包括 Baichuan2-7B-Chat 和 Baichuan2-13B-Chat),方便用户快速高效地在自己的平台部署 Baichuan 2 模型。

量化方法

Baichuan 2 的采用社区主流的量化方法:BitsAndBytes。该方法可以保证量化后的效果基本不掉点,目前已经集成到 transformers 库里,并在社区得到了广泛应用。BitsAndBytes 支持 8bits 和 4bits 两种量化,其中 4bits 支持 FP4 和 NF4 两种格式,Baichuan 2 选用 NF4 作为 4bits 量化的数据类型。

基于该量化方法,Baichuan 2 支持在线量化和离线量化两种模式。

在线量化

对于在线量化,我们支持 8bits 和 4bits 量化,使用方式和 Baichuan-13B 项目中的方式类似,只需要先加载模型到 CPU 的内存里,再调用quantize()接口量化,最后调用 cuda()函数,将量化后的权重拷贝到 GPU 显存中。实现整个模型加载的代码非常简单,我们以 Baichuan2-7B-Chat 为例:

8bits 在线量化:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat", torch_dtype=torch.float16, trust_remote_code=True)
model = model.quantize(8).cuda() 

4bits 在线量化:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat", torch_dtype=torch.float16, trust_remote_code=True)
model = model.quantize(4).cuda() 

需要注意的是,在用 from_pretrained 接口的时候,用户一般会加上 device_map="auto",在使用在线量化时,需要去掉这个参数,否则会报错。

离线量化

为了方便用户的使用,我们提供了离线量化好的 4bits 的版本 Baichuan2-7B-Chat-4bits,供用户下载。
用户加载 Baichuan2-7B-Chat-4bits 模型很简单,只需要执行:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat-4bits", device_map="auto", trust_remote_code=True)

对于 8bits 离线量化,我们没有提供相应的版本,因为 Hugging Face transformers 库提供了相应的 API 接口,可以很方便的实现 8bits 量化模型的保存和加载。用户可以自行按照如下方式实现 8bits 的模型保存和加载:

# Model saving: model_id is the original model directory, and quant8_saved_dir is the directory where the 8bits quantized model is saved.
model = AutoModelForCausalLM.from_pretrained(model_id, load_in_8bit=True, device_map="auto", trust_remote_code=True)
model.save_pretrained(quant8_saved_dir)
model = AutoModelForCausalLM.from_pretrained(quant8_saved_dir, device_map="auto", trust_remote_code=True)

量化效果

量化前后显存占用对比 (GPU Mem in GB):

PrecisionBaichuan2-7BBaichuan2-13B
bf16 / fp1615.327.5
8bits8.016.1
4bits5.18.6

量化后在各个 benchmark 上的结果和原始版本对比如下:

Model 5-shotC-EvalMMLUCMMLU
Baichuan2-13B-Chat56.7457.3259.68
Baichuan2-13B-Chat-4bits56.0556.2458.82
Baichuan2-7B-Chat54.3552.9354.99
Baichuan2-7B-Chat-4bits53.0451.7252.84

C-Eval 是在其 val set 上进行的评测

可以看到,4bits 相对 bfloat16 精度损失在 1 - 2 个百分点左右。

CPU 部署

Baichuan 2 模型支持 CPU 推理,但需要强调的是,CPU 的推理速度相对较慢。需按如下方式修改模型加载的方式:

# Taking Baichuan2-7B-Chat as an example
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat", torch_dtype=torch.float32, trust_remote_code=True)

对 Baichuan 1 的推理优化迁移到 Baichuan 2

由于很多用户在 Baichuan 1 (Baichuan-7B, Baichuan-13B)上做了很多优化的工作,例如编译优化、量化等,为了将这些工作零成本地应用于 Baichuan 2,用户可以对 Baichuan 2 模型做一个离线转换,转换后就可以当做 Baichuan 1 模型来使用。具体来说,用户只需要利用以下脚本离线对 Baichuan 2 模型的最后一层 lm_head 做归一化,并替换掉lm_head.weight即可。替换完后,就可以像对 Baichuan 1 模型一样对转换后的模型做编译优化等工作了。

import torch
import os
ori_model_dir = 'your Baichuan 2 model directory'
# To avoid overwriting the original model, it's best to save the converted model to another directory before replacing it
new_model_dir = 'your normalized lm_head weight Baichuan 2 model directory'
model = torch.load(os.path.join(ori_model_dir, 'pytorch_model.bin'))
lm_head_w = model['lm_head.weight']
lm_head_w = torch.nn.functional.normalize(lm_head_w)
model['lm_head.weight'] = lm_head_w
torch.save(model, os.path.join(new_model_dir, 'pytorch_model.bin'))

模型微调

依赖安装

git clone https://github.com/baichuan-inc/Baichuan2.git
cd Baichuan2/fine-tune
pip install -r requirements.txt
  • 如需使用 LoRA 等轻量级微调方法需额外安装 peft
  • 如需使用 xFormers 进行训练加速需额外安装 xFormers

单机训练

下面我们给一个微调 Baichuan2-7B-Base 的单机训练例子。

训练数据:data/belle_chat_ramdon_10k.json,该样例数据是从 multiturn_chat_0.8M 采样出 1 万条,并且做了格式转换。主要是展示多轮数据怎么训练,不保证效果。

hostfile=""
deepspeed --hostfile=$hostfile fine-tune.py  \
    --report_to "none" \
    --data_path "data/belle_chat_ramdon_10k.json" \
    --model_name_or_path "baichuan-inc/Baichuan2-7B-Base" \
    --output_dir "output" \
    --model_max_length 512 \
    --num_train_epochs 4 \
    --per_device_train_batch_size 16 \
    --gradient_accumulation_steps 1 \
    --save_strategy epoch \
    --learning_rate 2e-5 \
    --lr_scheduler_type constant \
    --adam_beta1 0.9 \
    --adam_beta2 0.98 \
    --adam_epsilon 1e-8 \
    --max_grad_norm 1.0 \
    --weight_decay 1e-4 \
    --warmup_ratio 0.0 \
    --logging_steps 1 \
    --gradient_checkpointing True \
    --deepspeed ds_config.json \
    --bf16 True \
    --tf32 True

多机训练

多机训练只需要给一下 hostfile ,内容类似如下:

ip1 slots=8
ip2 slots=8
ip3 slots=8
ip4 slots=8
....

同时在训练脚本里面指定 hosftfile 的路径:

hostfile="/path/to/hostfile"
deepspeed --hostfile=$hostfile fine-tune.py  \
    --report_to "none" \
    --data_path "data/belle_chat_ramdon_10k.json" \
    --model_name_or_path "baichuan-inc/Baichuan2-7B-Base" \
    --output_dir "output" \
    --model_max_length 512 \
    --num_train_epochs 4 \
    --per_device_train_batch_size 16 \
    --gradient_accumulation_steps 1 \
    --save_strategy epoch \
    --learning_rate 2e-5 \
    --lr_scheduler_type constant \
    --adam_beta1 0.9 \
    --adam_beta2 0.98 \
    --adam_epsilon 1e-8 \
    --max_grad_norm 1.0 \
    --weight_decay 1e-4 \
    --warmup_ratio 0.0 \
    --logging_steps 1 \
    --gradient_checkpointing True \
    --deepspeed ds_config.json \
    --bf16 True \
    --tf32 True

轻量化微调

代码已经支持轻量化微调如 LoRA,如需使用仅需在上面的脚本中加入以下参数:

--use_lora True

LoRA 具体的配置可见 fine-tune.py 脚本。

使用 LoRA 微调后可以使用下面的命令加载模型:

from peft import AutoPeftModelForCausalLM
model = AutoPeftModelForCausalLM.from_pretrained("output", trust_remote_code=True)

参考

1.https://github.com/baichuan-inc/Baichuan2
2.https://modelscope.cn/models/baichuan-inc/Baichuan2-7B-Base/summary
3.【AI实战】开源可商用的中英文大语言模型baichuan-7B,从零开始搭建
4.https://huggingface.co/baichuan-inc/Baichuan2-7B-Base

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值