外观
百川大模型系列技术全解
内容整理自学习笔记,仅供面试备考参考;不构成录用、培训或考试承诺。
1. Baichuan-7B 篇
- 项目地址:https://github.com/baichuan-inc/baichuan-7B
- 预训练模型:https://huggingface.co/baichuan-inc/baichuan-7B
- ModelScope:https://modelscope.cn/models/baichuan-inc/baichuan-7B/
1.1 Baichuan-7B 模型架构介绍
Baichuan-7B 基于 Transformer 结构,在大约 1.2 万亿 tokens 上训练的 70 亿参数模型,支持中英双语,上下文窗口长度为 4096。
| 维度 | 参数 |
|---|---|
| 模型结构 | Transformer |
| 参数量 | 7B |
| 训练数据量 | 1.2T tokens |
| 语言支持 | 中英双语 |
| 上下文长度 | 4096 |
1.2 Baichuan-7B 如何收集原始数据并构建训练数据?
| 阶段 | 说明 |
|---|---|
| 原始数据收集 | 开源中英文数据 + 自行抓取的中文互联网数据 + 部分高质量知识性数据 |
| 数据预处理 | 基于启发式规则和质量模型打分,进行篇章和句子粒度过滤;利用局部敏感哈希方法做去重 |
| 数据配比 | 使用基于自动学习的数据权重策略,对不同类别数据进行配比 |
提示:频率和质量是数据处理环节重点考虑的两个维度。
1.3 Baichuan-7B 如何提高训练稳定性和吞吐?
在原本的 LLaMA 框架上进行诸多修改以提升训练吞吐:
| 优化技术 | 说明 |
|---|---|
| 算子优化 | 采用 Flash-Attention、NVIDIA apex 的 RMSNorm 等高效算子 |
| 算子切分 | 将部分计算算子进行切分,减小内存峰值 |
| 混合精度 | 不损失模型精度的情况下加速计算 |
| 训练容灾 | IaaS + PaaS 实现分钟级故障定位和任务恢复 |
| 通信优化 | 拓扑感知集合通信、自适应 bucket size、计算通信重叠 |
通信优化细节:
- 采用拓扑感知的集合通信算法,避免网络拥塞问题
- 根据卡数自适应设置 bucket size,提高带宽利用率
- 根据模型和集群环境,调优通信原语的触发时机,将计算和通信重叠
效果:在千卡 A800 显卡上达到了 7B 模型 182 TFLOPS 的吞吐,GPU 峰值算力利用率高达 58.3%。
2. Baichuan-13B 篇
- 项目地址:https://github.com/Baichuan-inc/Baichuan-13B
- 预训练模型:https://huggingface.co/baichuan-inc/Baichuan-13B-Base
- 对话模型:https://huggingface.co/baichuan-inc/Baichuan-13B-Chat
2.1 相比 Baichuan-7B,Baichuan-13B 的特点
| 特点 | 说明 |
|---|---|
| 更大尺寸、更多数据 | 参数量扩大到 130 亿,训练 1.4 万亿 tokens(超 LLaMA-13B 40%),开源 13B 尺寸训练数据量最多 |
| 同时开源预训练和对齐模型 | 预训练模型(基座)+ 对齐模型(Baichuan-13B-Chat),开箱即用 |
| 更高效的推理 | 同时开源 int8 和 int4 量化版本,几乎无效果损失,可部署在消费级显卡(如 3090) |
位置编码:Baichuan-13B 使用 ALiBi 位置编码,上下文窗口长度为 4096。
2.2 如何对 Baichuan-13B 进行推理和部署?
2.2.1 环境安装
bash
$ pip install -r requirements.txt2.2.2 GPU 直接部署
方法一:Python 代码方式
python
>>> import torch
>>> from transformers import AutoModelForCausalLM, AutoTokenizer
>>> from transformers.generation.utils import GenerationConfig
>>> tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan-13B-Chat",
... use_fast=False, trust_remote_code=True)
>>> model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan-13B-Chat",
... device_map="auto", torch_dtype=torch.float16, trust_remote_code=True)
>>> model.generation_config = GenerationConfig.from_pretrained("baichuan-inc/Baichuan-13B-Chat")
>>> messages = []
>>> messages.append({"role": "user", "content": "世界上第二高的山峰是哪座"})
>>> response = model.chat(tokenizer, messages)
>>> print(response)
乔戈里峰。世界第二高峰———乔戈里峰西方登山者称其为k2峰,海拔高度是8611米,位于喀喇昆仑山脉的中巴边境上注意:模型加载指定
device_map='auto',会使用所有可用显卡。如需指定设备,可使用export CUDA_VISIBLE_DEVICES=0,1。
方法二:命令行方式
bash
$ python cli_demo.py2.2.3 量化部署
Baichuan-13B 支持 int8 和 int4 量化,用户只需在推理代码中简单修改两行即可实现。
注意:如果是为了节省显存而进行量化,应加载原始精度模型到 CPU 后再开始量化;避免在
from_pretrained时添加device_map='auto'等会导致直接加载到 GPU 的参数。
int8 量化:
python
model = AutoModelForCausalLM.from_pretrained(
"baichuan-inc/Baichuan-13B-Chat",
torch_dtype=torch.float16,
trust_remote_code=True
)
model = model.quantize(8).cuda()int4 量化:
python
model = AutoModelForCausalLM.from_pretrained(
"baichuan-inc/Baichuan-13B-Chat",
torch_dtype=torch.float16,
trust_remote_code=True
)
model = model.quantize(4).cuda()2.2.4 CPU 部署
使用 CPU 进行推理大概需要 60GB 内存。
2.3 如何对 Baichuan-13B 进行微调?
注意:团队测试了与 Baichuan-13B 兼容的微调工具 LLaMA Efficient Tuning,支持全量微调和 LoRA 微调。
数据格式:
json
[
{
"instruction": "What are the three primary colors?",
"input": "",
"output": "The three primary colors are red, blue, and yellow."
}
]instruction:用户输入input:可选项,与 instruction 用\n连接output:期望的模型输出
2.3.1 全量微调
环境:8 * Nvidia A100 80 GB + DeepSpeed
bash
deepspeed --num_gpus=8 src/train_bash.py \
--stage sft \
--model_name_or_path baichuan-inc/Baichuan-13B-Base \
--do_train \
--dataset alpaca_gpt4_en,alpaca_gpt4_zh \
--finetuning_type full \
--output_dir path_to_your_sft_checkpoint \
--overwrite_cache \
--per_device_train_batch_size 4 \
--per_device_eval_batch_size 4 \
--gradient_accumulation_steps 8 \
--preprocessing_num_workers 16 \
--lr_scheduler_type cosine \
--logging_steps 10 \
--save_steps 100 \
--eval_steps 100 \
--learning_rate 5e-5 \
--max_grad_norm 0.5 \
--num_train_epochs 2.0 \
--dev_ratio 0.01 \
--evaluation_strategy steps \
--load_best_model_at_end \
--plot_loss \
--fp16 \
--deepspeed deepspeed.jsondeep_speed.json 配置:
json
{
"train_micro_batch_size_per_gpu": "auto",
"zero_allow_untested_optimizer": true,
"fp16": {
"enabled": "auto",
"loss_scale": 0,
"initial_scale_power": 16,
"loss_scale_window": 1000,
"hysteresis": 2,
"min_loss_scale": 1
},
"zero_optimization": {
"stage": 2,
"allgather_partitions": true,
"allgather_bucket_size": 5e8,
"overlap_comm": false,
"reduce_scatter": true,
"reduce_bucket_size": 5e8,
"contiguous_gradients": true
}
}2.3.2 LoRA 微调
环境:1 * Nvidia A100 80 GB
bash
CUDA_VISIBLE_DEVICES=0 python src/train_bash.py \
--stage sft \
--model_name_or_path baichuan-inc/Baichuan-13B-Base \
--do_train \
--dataset alpaca_gpt4_en,alpaca_gpt4_zh \
--finetuning_type lora \
--lora_rank 8 \
--lora_target W_pack \
--output_dir path_to_your_sft_checkpoint \
--overwrite_cache \
--per_device_train_batch_size 4 \
--per_device_eval_batch_size 4 \
--gradient_accumulation_steps 8 \
--preprocessing_num_workers 16 \
--lr_scheduler_type cosine \
--logging_steps 10 \
--save_steps 100 \
--eval_steps 100 \
--learning_rate 5e-5 \
--max_grad_norm 0.5 \
--num_train_epochs 2.0 \
--dev_ratio 0.01 \
--evaluation_strategy steps \
--load_best_model_at_end \
--plot_loss \
--fp163. Baichuan-53B 篇
3.1 Baichuan-53B 的技术优势
Baichuan-53B 的三个技术优势:
| 优势 | 说明 |
|---|---|
| 预训练数据 | 基于百川团队丰富的搜索引擎经验 |
| 搜索增强 | 依赖搜索引擎能力实现实时知识获取 |
| 对齐能力 | 强大的 RLHF 对齐训练 |
3.2 预训练数据处理
- 全面的世界知识体系:覆盖各个领域和学科,整合各类信息源
- 系统的数据质量体系:包括低质、优质、类别等标准,维持高标准数据质量
- 多粒度大规模聚类系统:识别和整合相似或相关数据,为去重、采样提供支撑
- 细粒度自动化匹配算法:自动配比各类任务(如课程学习),实现个性化模型学习
3.3 搜索增强
| 步骤 | 说明 |
|---|---|
| 动态响应策略 | 依赖 Prompt,将指令任务细化为 16 个独立类别 |
| 智能化搜索词生成 | 通过精细化人工标注,捕捉用户多元化需求 |
| 高质量搜索结果筛选 | 构建搜索结果相关性模型,筛选高质量搜索引用内容 |
| 回答结果搜索增强 | RLHF 让模型参照搜索结果生成高价值且具有实时性的回答 |
4. Baichuan 2 篇
- 开源链接:https://github.com/baichuan-inc/Baichuan2
- 技术报告:https://cdn.baichuan-ai.com/paper/Baichuan2-technical-report.pdf
4.1 Baichuan 2 与其他大模型对比
Baichuan2-13B-Base 相比上一代 13B 模型的能力提升:
| 能力维度 | 提升幅度 |
|---|---|
| 数学能力 | +49% |
| 代码能力 | +46% |
| 安全能力 | +37% |
| 逻辑推理能力 | +25% |
| 语义理解能力 | +15% |
5. Baichuan 数据构建篇
5.1 微调时领域数据与通用数据配比
| 场景 | 最优配比(领域:通用) |
|---|---|
| 基于 base 做 fine-tune | 1:10 |
| 基于 base 继续预训练(不用通用数据) | 1:5 |
| 基于 base 继续预训练(领域:通用=1:5) | 1:5 |
| 基于 chat(多轮对话+指令微调) | 1:5 |
| 基于 base 做预训练 + SFT finetune(无通用数据) | 效果最好 |
参考:ChatHome: Development and Evaluation of a Domain-Specific Language Model for Home Renovation https://arxiv.org/abs/2307.15290
5.2 配比结论
- 基于 baichuan-13B base 预训练模型做 fine-tune 时,领域数据:通用数据配比 1:10 时领域指标最好
- 基于 baichuan-13B base 继续做预训练(不用通用领域数据)时,配比 1:5 时领域指标最好
- 基于 baichuan-13B base 继续做预训练(领域:通用=1:5)时,配比 1:5 时领域指标最好
- 基于 baichuan-13B chat(多轮对话和指令微调),配比 1:5 时领域指标最好
- 基于 baichuan-13B base 做预训练和 SFT finetune(无通用数据)时效果最好
6. 总结
百川大模型系列从 7B 到 13B、53B 再到 Baichuan 2,在模型规模、训练数据、推理效率和搜索增强方面持续迭代。核心特点包括:
- 7B:高吞吐训练(182 TFLOPS),中英双语基础能力
- 13B:更大参数量 + 量化部署(int8/int4),同时开源基座和对齐模型
- 53B:搜索引擎增强 + RLHF 对齐,实时知识获取
- Baichuan 2:全面能力提升,数学/代码/安全/逻辑显著增强