跳转至

LLM 微调

LoRA / QLoRA / P-Tuning / 数据准备 / 评估

微调方式对比

方式 可训练参数 显存 适用
全量微调 100% 数据充足
LoRA ~0.1% 通用首选
QLoRA ~0.1% 极低 消费级 GPU
P-Tuning v2 ~0.1% 提示驱动任务
IA3 ~0.01% 极低 极省参数
DoRA ~0.2% LoRA 增强版

LoRA 原理

  • 低秩分解:\(W = W_0 + BA\),其中 \(B \in \mathbb{R}^{d \times r}\), \(A \in \mathbb{R}^{r \times k}\), \(r \ll \min(d,k)\)
  • 仅训练 A 和 B,原权重冻结
  • 合并推理:\(W_{merged} = W_0 + \alpha/r \cdot BA\)

数据准备

  • 指令数据集格式:{"instruction": ..., "input": ..., "output": ...}
  • 数据质量 > 数据数量,少量高质量数据胜过大量噪声数据
  • 数据去重、长度过滤、质量筛选

框架对比

框架 链接 特点
LLaMA-Factory https://github.com/hiyouga/LLaMA-Factory 一站式,WebUI,国产
Unsloth https://github.com/unslothai/unsloth 2x 更快,省显存
HuggingFace PEFT https://github.com/huggingface/peft 官方库,灵活
Axolotl https://github.com/OpenAccess-AI-Collective/axolotl 配置驱动

练习

  • 用 Unsloth 微调 Qwen2.5-7B(单卡 24GB)
  • 用 LLaMA-Factory 微调 Llama3-8B(指令跟随)
  • 对比 LoRA rank=8/16/32 的效果
  • 构建自己的指令数据集

资源

资源 链接
LoRA 论文 https://arxiv.org/abs/2106.09685
QLoRA 论文 https://arxiv.org/abs/2305.14314
LLaMA-Factory https://github.com/hiyouga/LLaMA-Factory
Unsloth https://github.com/unslothai/unsloth