Chapter 06
训练算法:SFT / DPO / ORPO / KTO / GRPO
Axolotl 用 rl 字段决定走 SFT 还是 RLHF。底层都是 TRL。
6.1rl 字段取值
| rl 值 | 算法 | 数据格式 |
|---|---|---|
| 未设 | SFT | chat_template / alpaca / sharegpt |
| dpo | DPO | prompt + chosen + rejected |
| ipo | IPO | 同 DPO |
| kto | KTO | prompt + completion + label(bool) |
| orpo | ORPO | prompt + chosen + rejected |
| simpo | SimPO | 同 DPO |
| grpo | GRPO | prompt + reward_funcs |
6.2SFT 配方(前一章已讲)
主要靠 chat_template type + sample_packing 跑通。无需 rl 字段。
6.3DPO 配方
base_model: ./sft_out
rl: dpo
rl_beta: 0.1
datasets:
- path: HuggingFaceH4/ultrafeedback_binarized
split: train_prefs
type: chatml.intel
sequence_len: 2048
micro_batch_size: 1
gradient_accumulation_steps: 8
num_epochs: 1
learning_rate: 5e-7 # ★ DPO 用极小 lr
optimizer: adamw_torch
lr_scheduler: cosine
warmup_ratio: 0.03
adapter: lora
lora_r: 16
lora_target_modules: [q_proj, k_proj, v_proj, o_proj]
bf16: auto
gradient_checkpointing: true
6.4KTO 配方
rl: kto
rl_beta: 0.1
kto_desirable_weight: 1.0
kto_undesirable_weight: 1.0
datasets:
- path: argilla/kto-mix-15k
type: kto.argilla
6.5ORPO 配方(单阶段)
ORPO 不要 SFT 前置、不要 reference 模型:
base_model: meta-llama/Meta-Llama-3-8B # 直接从 base 起
rl: orpo
orpo_alpha: 0.1 # SFT loss vs odds ratio 权重
datasets:
- path: argilla/distilabel-capybara-dpo-7k-binarized
type: chat_template.argilla
ORPO 显存最省,效果接近 SFT+DPO 两阶段(但调起来更敏感)。
6.6GRPO 配方
rl: grpo
trl: # GRPO 用 trl 字段
num_generations: 8
max_completion_length: 1024
vllm_server_host: ""
vllm_server_port: 8000
reward_funcs:
- "axolotl.integrations.grpo.rewards.format_reward"
- "axolotl.integrations.grpo.rewards.accuracy_reward"
beta: 0.04
datasets:
- path: openai/gsm8k
name: main
type: grpo.gsm8k
Axolotl 内置几个 reward function,也支持指向自定义 Python 函数。
6.7决策表
| 需求 | 选 |
|---|---|
| 第一次微调 | SFT |
| 有偏好数据,跳过 RM | DPO |
| 偏好数据噪声大 | IPO / cDPO |
| 只有 thumbs up/down | KTO |
| 显存极紧 + 想跳 SFT | ORPO |
| 推理任务 + rule reward | GRPO |
6.8HFRLTrainerBuilder._get_trainer_cls() 真实 dispatch
YAML 一行 rl: dpo 背后做了什么?看 core/builders/rl.py:39–99 的真实 dispatch:
| RLType | 选的 Trainer 类 | 父类 |
|---|---|---|
RLType.DPO | AxolotlDPOTrainer | trl.DPOTrainer |
RLType.IPO | 同上,loss_type 切换 | 同上 |
RLType.KTO | AxolotlKTOTrainer | trl.KTOTrainer |
RLType.ORPO | AxolotlORPOTrainer | trl.ORPOTrainer |
RLType.SIMPO | AxolotlCPOTrainer | trl.CPOTrainer(SimPO = CPO 变体) |
RLType.GRPO | AxolotlGRPOTrainer 或 AxolotlAsyncGRPOTrainer | trl.GRPOTrainer |
RLType.GDPO | 同 GRPO trainer 但 advantage 公式不同 | — |
RLType.EBFT | EBFTStrategy(独立路径) | 不走 TRL |
6.9EBFT:能量基特征训练
2024-12 引入的 EBFT 是 Axolotl 在 TRL 之外的独门 RLHF 方法。位置 prompt_strategies/ebft/(5 个变种)+ core/builders/rl.py 的 EBFT 分支。
| EBFT 变种 | 用途 |
|---|---|
ebft_chat_multiturn | 多轮对话特征对齐 |
ebft_opencode | 代码生成场景 |
ebft_reasoning | 推理任务 |
ebft_strided_chat | 跨步采样 chat |
ebft_strided_structured | 结构化输出 + strided |
原理:不显式对比 chosen/rejected,而是把"模型内部 hidden state 的能量"作为目标。比 DPO 更稳,但配置门槛高。
6.10GDPO:Generalized DPO(2026-02 新)
rl: gdpo 在 2026-02 加入,是 DPO 的泛化版本:
- 不需要严格 chosen/rejected 对,可处理多个 candidate;
- 用 group baseline 类似 GRPO,但仍走"已采样数据"而非在线生成;
- 配置上接近 DPO(同样 dataset schema),但 trainer 是 GRPO trainer 借壳;
- 对偏好质量不高的数据更鲁棒。
示例配置见 examples/llama-3/qlora-1b-gdpo.yaml。新算法慎用,先在小模型跑通对比 DPO。
6.11GRPO + vllm_serve:在线 rollout 路径
GRPO 与 DPO/KTO/ORPO 最大差异:训练时实时采样而非读静态数据。Axolotl 的实现路径:
- 独立 vLLM server 起来:
axolotl vllm_serve config.yml(cli/main.py:321); - 训练 YAML 加:
rl: grpo vllm: enable: true host: localhost port: 8000 tensor_parallel_size: 1 trl: reward_funcs: - axolotl.integrations.grpo.rewards.format_reward - axolotl.integrations.grpo.rewards.accuracy_reward beta: 0.04 num_generations: 8 - 训练 trainer 拉 vLLM 生成 → 算 reward → 算 advantage → 反向;
- 多卡时
AxolotlAsyncGRPOTrainer异步并发,rollout 与 forward 重叠。
6.12这章你需要带走的
- 切算法 = 改 YAML 里的
rl字段; - 底层都是 TRL,参数风格一致;
- ORPO / SimPO 跳 SFT 直接对齐,最省;
- GRPO 用
trl.reward_funcs列表配 reward。