RLHF 怎么做?
DPO、GRPO 和 PPO 有什么区别?
先弄清楚:每个概念是什么
- Reward model
- 读 prompt 和回答,输出一个分数。用人类"两个里选一个"的偏好训练,Bradley-Terry 假设分差决定被选中的概率。例:\(r_a = 2\)、\(r_b = 0\),\(P(a \succ b) = \sigma(2) = 0.88\)。
- Policy 与 reference model
- policy 是正在训练的 LLM;reference(通常是 SFT model)是冻住的起点。
- KL penalty 与 reward hacking
- KL 衡量 policy 离 reference 有多远,远了就扣分。没有它,模型会钻 reward model 的空子刷高分,这叫 reward hacking。
- Advantage 与 critic
- advantage 是这个回答比平常水平好多少。PPO 用和 policy 差不多大的 critic 估平常水平,GRPO 直接用同一题多个回答的平均分。例:4 个回答 reward 为 (1, 0, 0, 1),均值 0.5、std 0.5 → advantage (+1, −1, −1, +1)。
30 秒回答
RLHF has three stages: SFT on demonstrations; a reward model trained on human preference pairs with a Bradley–Terry loss; then PPO to maximize reward minus a KL penalty to the SFT model, which prevents reward hacking. DPO uses the closed-form optimum of that objective to write the reward as β times the policy-to-reference log-ratio, so preference learning becomes a logistic loss on pairs: no reward model, no sampling. GRPO keeps PPO's clipped objective but drops the critic: it samples a group of answers per prompt and uses each reward, normalized within the group, as the advantage. It suits verifiable rewards like math and code.
关键公式
RLHF:reward model → PPO
\[ \mathcal{L}_{\text{RM}} = -\log\sigma\big(r(x, y_w) - r(x, y_l)\big) \] \[ \max_{\pi}\ \mathbb{E}\Big[\, r(x, y) - \beta \log\frac{\pi(y\mid x)}{\pi_{\text{SFT}}(y\mid x)} \Big] \]\(y_w\) 是人选中的回答,\(y_l\) 是没选中的。第二行用 PPO 优化。
DPO:把 reward 换成 policy
\[ \pi^*(y\mid x) \propto \pi_{\text{ref}}(y\mid x)\, e^{r(x,y)/\beta} \] \[ \Rightarrow\ r(x, y) = \beta\log\frac{\pi^*(y\mid x)}{\pi_{\text{ref}}(y\mid x)} + \beta\log Z(x) \]代回 \(\mathcal{L}_{\text{RM}}\),\(Z(x)\) 在 \(r_w - r_l\) 里抵消:
\[ \mathcal{L}_{\text{DPO}} = -\log\sigma\big(\beta\,(\Delta_w - \Delta_l)\big),\quad \Delta = \log\frac{\pi_\theta(y\mid x)}{\pi_{\text{ref}}(y\mid x)} \]例:一开始 \(\pi_\theta = \pi_{\text{ref}}\),loss \(= \log 2 = 0.693\);训到 \(\Delta_w = 2\)、\(\Delta_l = -1\)、\(\beta = 0.1\) 时 loss \(= -\log\sigma(0.3) = 0.554\)。\(\beta\) 越小越允许偏离 reference。
GRPO:group 内 normalize 当 advantage
\[ \hat A_i = \frac{r_i - \operatorname{mean}(r_1, \dots, r_G)}{\operatorname{std}(r_1, \dots, r_G)} \]同一个 prompt 采 \(G\) 个回答,回答里每个 token 都用这个 \(\hat A_i\),代入 PPO 的 clipped objective。KL 直接加在 loss 里,不放进 reward。
对比
| PPO(RLHF) | DPO | GRPO | |
|---|---|---|---|
| 模型 | policy、reference、reward model、critic | policy、reference | policy、reference、reward(无 critic) |
| 数据 | online sampling | offline preference pairs | online,每个 prompt 采 \(G\) 个 |
| 优点 | 最通用 | 简单、稳定、便宜 | 省掉 critic 的 GPU memory;适合 verifiable reward |
| 缺点 | 4 个模型,调参难 | 受数据分布限制,没有 exploration | 要生成多个回答 |
必答点
- RLHF 三步:SFT → reward model(Bradley-Terry)→ PPO
- KL penalty 防 reward hacking、防偏离 SFT model 太远
- DPO:closed-form 最优 policy 代回,reward = \(\beta\) × log-ratio,无 reward model、无 sampling
- GRPO:一组回答的 normalized reward 当 advantage,去掉 critic
- PPO 用 clipped ratio 限制每步更新幅度
追问
PPO 的 clipped objective 是什么?
\(\rho = \pi_\theta/\pi_{\text{old}}\),优化 \(\min\big(\rho A,\ \operatorname{clip}(\rho, 1-\epsilon, 1+\epsilon)A\big)\),\(\epsilon = 0.2\)。例:\(A = 1\)、\(\rho = 1.5\) 时只算 1.2,再往同方向推没有收益。
GRPO 里一组回答全对或全错会怎样?
reward 全相同,减去均值后 advantage 全是 0,这个 prompt 不提供 gradient。太简单或太难的题等于白采样。
为什么 GRPO 适合数学、代码?
答案能用规则判对错,可以直接当 reward,不用另训 reward model。注意 DeepSeekMath 原文用的还是训练出来的 reward model。
来源Ouyang et al. 2022, InstructGPT abstract(p.1)、§3.1(p.6,三步)、§3.5(pp.8–9,eq. 1–2)· Schulman et al. 2017, PPO §3(p.3,eq. 7)· Rafailov et al. 2023, DPO §3(p.3)、§4(pp.4–5,eq. 4–7)· Shao et al. 2024, DeepSeekMath §4.1.1(pp.11–13,eq. 3、Fig. 4)、§4.1.2(p.14)