Topic 46 of 62 · Post-training
PPO, advantages, and clipping
HardWhat does PPO clip, and why doesn’t clipping the objective guarantee that KL divergence stays small?
1 · Start with the intuition
When you collect a batch of rollouts from the policy π_old and want to take multiple gradient steps on that same batch to improve sample efficiency, standard policy gradient breaks down: as πθ moves away from π_old, the importance sampling ratio rₜ(θ) = πθ(aₜ|sₜ) / π_old(aₜ|sₜ) can explode and cause a destructive policy update. PPO’s clipped surrogate objective caps the reward incentive whenever rₜ(θ) moves outside [1 − ε, 1 + ε] in the direction that improves the objective—removing the gradient push on that token once it has moved far enough.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Collect rollouts & advantages
Sample completions from π_old and compute token advantages Âₜ (using a critic + GAE, or group normalization in GRPO).
Next, we will look at: Form the probability ratio. Move on when this operation makes sense.
Visualizing the behavior
Where this sits: the RL-for-LLMs progression
Clipped surrogate min(r·A, clip(r, 0.8, 1.2)·A), with ε = 0.2
Derived- A = +1 (good action)
- A = −1 (bad action)
3 · The governing equation
rₜ(θ) = πθ(aₜ | sₜ) / π_old(aₜ | sₜ) L_CLIP(θ) = Eₜ [ min( rₜ(θ) Âₜ, clip(rₜ(θ), 1 − ε, 1 + ε) Âₜ ) ]
Notation: ε is the clipping hyperparameter (typically 0.2). Notice that π_old is the rollout policy from the start of the current RL iteration, whereas π_ref in RLHF is the frozen initial SFT checkpoint.
Follow the two cases of the min(): (1) When the advantage is positive (Âₜ > 0, a better-than-expected action), we want to increase rₜ, but once rₜ > 1 + ε, the clipped term (1 + ε)Âₜ is smaller than rₜÂₜ, so the objective flattens at (1 + ε)Âₜ and its gradient with respect to θ becomes zero for that token. (2) When the advantage is negative (Âₜ < 0, a worse-than-expected action), we want to decrease rₜ, and because Âₜ is negative, multiplying by a smaller number is larger—so min() selects the flat (1 − ε)Âₜ branch once rₜ < 1 − ε. Why can the policy still drift? Because clipping only zeroes the gradient on already-clipped tokens in the current minibatch; unclipped tokens, momentum in Adam, and cumulative drift across dozens of rollout iterations can still move πθ far from π_old and π_ref.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Saying “PPO clips the gradient norm” or confusing π_old (the rollout policy from the current iteration) with π_ref (the frozen SFT model used for the KL penalty against reward hacking).
In practice · implementation & engineering rule of thumb
Compute the importance ratio as exp(logp_new - logp_old) in log-space for numerical stability, and monitor the clipped-token fraction (if >20–30% of tokens clip, your RL step size is too aggressive).
ratio = torch.exp(logp_new - logp_old.detach()) surr1 = ratio * advantages surr2 = torch.clamp(ratio, 1.0 - eps, 1.0 + eps) * advantages policy_loss = -torch.min(surr1, surr2).mean() clip_frac = ((ratio - 1.0).abs() > eps).float().mean()
Go one level deeper
Follow-ups that often come after a correct first answer.
- Where the advantage comes from (GAE)
- Âₜ = Σₗ (γλ)ˡ·δₜ₊ₗ with δₜ = rₜ + γV(sₜ₊₁) − V(sₜ). λ trades bias (small λ trusts the critic) against variance (large λ trusts observed returns). In LLM RLHF, the reward usually arrives at the end, plus a per-token KL penalty.
- GRPO drops the critic
- Sample G responses per prompt and set each advantage to (rᵢ − group mean) / group std. That replaces the learned value function with a group baseline, while keeping a PPO-style clipped objective and a KL term. It was introduced in DeepSeekMath.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWalk through the negative-advantage case: if Âₜ = −2.0, ε = 0.2, and rₜ = 0.5, which branch of min() wins?
Show follow-up reasoning
The unclipped term is rₜ Âₜ = 0.5 × (−2.0) = −1.0. The clipped ratio is clip(0.5, 0.8, 1.2) = 0.8, giving 0.8 × (−2.0) = −1.6. Because min(−1.0, −1.6) = −1.6, the clipped flat branch is selected—removing the incentive to push the probability ratio even lower than 0.8 on this update.