Axolotl 详解:YAML 配方与多卡微调

Axolotl 是 YAML 驱动的 LLM 微调框架,在 多卡、DeepSpeed、社区训练配方 方面积累深厚,适合「拿一份成熟 config 改数据就上」的场景。与 LLaMA-Factory、ms-swift 同属配置化工厂;对比见 03-00

段末注释:DeepSpeed 是微软开源的深度学习优化库,提供 ZeRO 分片等能力;Axolotl 常通过 DeepSpeed 配置实现大模型多卡微调。

系列索引:微调技术路线导读


一、定位

适合 不太适合
多 GPU + DeepSpeed ZeRO 需要 WebUI 的非技术用户
复用 GitHub 上大量示例 yaml 轻度单次实验(Factory/swift 更轻)
细调 flash attention、sample packing 快速 GRPO 实验(优先 TRL)

二、安装

1
2
3
git clone https://github.com/axolotl-ai-cloud/axolotl
cd axolotl
pip install -e '.[flash-attn,deepspeed]'

对 CUDA、PyTorch 版本敏感,建议用官方 Docker 或 environment.yaml


三、配置结构概览

单文件 config.yml 驱动全流程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
base_model: meta-llama/Llama-3.1-8B-Instruct
model_type: LlamaForCausalLM
tokenizer_type: AutoTokenizer

load_in_8bit: false
load_in_4bit: true # QLoRA 场景

datasets:
- path: data/emotion.json
type: chat_template # 或 alpaca, sharegpt 等
field_messages: messages

dataset_prepared_path: last_run_prepared
val_set_size: 0.05
output_dir: ./outputs/llama-emotion

adapter: lora
lora_r: 16
lora_alpha: 32
lora_target_modules:
- q_proj
- v_proj
- k_proj
- o_proj
- gate_proj
- up_proj
- down_proj

sequence_len: 2048
sample_packing: true # 提升吞吐

micro_batch_size: 2
gradient_accumulation_steps: 4
num_epochs: 1
learning_rate: 0.0002
optimizer: adamw_bnb_8bit
lr_scheduler: cosine
warmup_steps: 100

bf16: auto
gradient_checkpointing: true

deepspeed: deepspeed_configs/zero2.json

启动:

1
accelerate launch -m axolotl.cli.train config.yml

四、数据集类型

type 说明
alpaca instruction/input/output
sharegpt conversations
chat_template messages 列表,贴近 01-01
completion prompt + completion

预处理结果可缓存到 dataset_prepared_path,重复实验更快。


五、LoRA / QLoRA

  • adapter: lora + lora_r / lora_alpha — 见 02-01
  • load_in_4bit: true — QLoRA,见 02-02
  • merge_lora 选项 — 训练结束合并权重便于部署 02-04

六、多卡与 DeepSpeed

deepspeed: deepspeed_configs/zero2.jsonzero3.json

ZeRO 场景
zero2 7B–13B 全参/QLoRA 常见
zero3 更大模型参数分片

详见 07-02 分布式。LoRA 多卡也可用 DDP 配方(社区 yaml 有标注)。


七、sample packing

sample_packing: true 将短样本拼进同一序列,提高 GPU 利用率——与 TRL SFTConfig(packing=True) 同类思路(03-01 §十一)。


八、与 SFTTrainer / Factory 对照

Axolotl LLaMA-Factory SFTTrainer
配置 单 yaml,偏极客 yaml + WebUI Python
DeepSpeed 配方 丰富 需自配
上手 需读示例 yaml WebUI 最易 需写代码
对齐 DPO 社区示例 内置 stage TRL 最全

九、常见踩坑

现象 对策
model_type 与权重不符 对照官方支持列表
packing + mask 错 检查 type 与字段映射
DeepSpeed 报错 GPU 数与 json 配置一致
预处理极慢 dataset_prepared_path 缓存

十、小结

Axolotl = YAML + DeepSpeed + packing 的多卡微调利器。快速演示选 Factory/swift;复杂多卡配方或复用社区 config 选 Axolotl。

横向对比:03-00 | 编程栈:03-01 SFTTrainer

-------------本文结束感谢您的阅读-------------