9.6 — Proximal Policy Optimization (PPO)
Goal: Derive the PPO clipped surrogate objective from the importance-sampling identity. Understand the role of the clip threshold ε, the value-function critic, the advantage estimator (GAE), and the per-token application in LLM training. Place PPO as the workhorse RL algorithm of RLHF since 2017.
1. Why this session exists
§9.5 ended with REINFORCE: a correct but high-variance, sample-inefficient policy gradient algorithm. The two operational problems were (a) fresh samples are required for each gradient step, so each step is expensive, and (b) a single unlucky large gradient step can push the policy off-distribution and break it.
Proximal Policy Optimization (Schulman et al., 2017, arXiv:1707.06347)[2] addresses both. The core idea: use importance sampling to reuse on-policy samples for multiple gradient steps, and clip the importance-weighted objective so that each step cannot drag the policy too far from its sampling distribution.
PPO became the default RL algorithm in 2017 for control / robotics and was inherited by RLHF in 2020. Every major RLHF deployment from InstructGPT (2022) through Llama 3 (2024) used PPO until DPO and GRPO offered alternatives. Even now, PPO remains the reference algorithm — the one DPO is derived against (§9.10) and the one GRPO is simplified from (§9.7).
This session derives the clipped surrogate objective, names the additional machinery (advantage estimation with GAE, value-function critic, per-token application), and writes the full PPO loop for LLM training.
2. Importance sampling and the surrogate objective
REINFORCE's gradient is . The expectation is under the current policy — we cannot reuse old samples from a previous policy , because the gradient direction depends on , not just on the samples.
Importance sampling lifts this restriction. The identity is
which holds for any function such that the ratio is well-defined. The ratio is the importance-sampling weight that re-weights samples from to give expectations under .
Applying this to the policy gradient:
where we've replaced the raw reward with the advantage (§9.5 §5.1) — a baseline-subtracted reward — and the second equality uses .
So the gradient of — a function of that can be evaluated on samples from — is the policy gradient at the current . We can take multiple gradient steps on this objective using a single batch of samples, as long as we treat as fixed and the importance ratio updates as moves.
This is the surrogate objective:
Maximising with respect to moves the policy in the direction of higher expected reward — but only locally. If moves too far from , the importance ratio can become extreme (very large or very small), and the surrogate becomes a poor approximation to the true objective.
3. The clipping trick
Schulman's contribution: prevent from moving too far in one update by clipping the importance ratio in the surrogate.
Define
where saturates to the interval , and (typically –, with as the de facto default) is the clip threshold.
The structure of the clipping:
- If (the action was better than the baseline) and , the clipped version is , which is smaller than . The picks the smaller, capping the surrogate reward. So the policy update will not chase a more-than--times increase in this action's probability.
- If (the action was worse than the baseline) and , the clipped version is , which is larger than (because , smaller-magnitude gives a less-negative value). The now picks , not clipping. So the policy is allowed to push probability all the way down. The clipping is asymmetric and only triggers when probability is being increased on positive advantage or decreased on negative advantage.
Wait, that last paragraph is mis-stated. Let me redo it carefully.
The is over (a) the unclipped surrogate and (b) the clipped surrogate . The objective is maximised with respect to . So:
- If : We want to grow (to increase the action's probability). If , clipping caps it at . Since , the clipped term . The picks the smaller, so the objective is the clipped value. The gradient with respect to at this point is zero — further increases in do not improve the objective. So the policy stops pushing this action's probability up.
- If : We want to shrink (to decrease the action's probability). If , clipping floors it at . Since , the clipped term (less negative). The picks the smaller, , which is the unclipped value. So the objective continues to decrease as decreases — the policy can push this action's probability further down.
Hmm, this is the opposite behaviour. Let me re-derive. Actually I had it right the first time but got the asymmetry direction confused; here is the careful version:
PPO uses . The two terms are:
- : the unclipped surrogate.
- : the clipped surrogate.
When : clipping at (from above) lowers . picks the lower of the two. So caps the upside of the surrogate when : no incentive to push beyond . When , clipping floors at , making the clipped term larger than ; picks , the unclipped — so the objective is not clipped from below for positive . The policy continues to be rewarded for moving up from a too-low value.
When : clipping at (from below) lowers further (more negative). picks , which is the lower. So the surrogate is not clipped here — the policy keeps being penalised. When , clipping floors at , making the clipped term less negative; picks (the unclipped, more negative one). So again the surrogate is not clipped from below.
Net effect: the clipping caps the upside of moving probability in the "right" direction. Moving an action's probability beyond when it has positive advantage, or below when it has negative advantage, gives no further gradient. The policy is bounded from making any single action's probability move by more than a multiplicative factor of per update.
This is the trust region interpretation: PPO's clipping keeps the new policy within a multiplicative-ratio neighbourhood of the old, preventing destabilising large updates.
4. The advantage function and GAE
PPO uses advantages rather than raw rewards (§9.5 §5.1). The advantage is the expected-reward improvement of taking action at prompt over the prompt's baseline.
For LLM RLHF, the standard advantage estimator is Generalised Advantage Estimation (GAE; Schulman et al., 2015[3], arXiv:1506.02438). For a trajectory with per-step rewards and value-function estimates , GAE defines per-step advantages
where is the temporal-difference error and are hyperparameters. GAE interpolates between high-variance Monte Carlo estimates () and high-bias one-step TD estimates ().
For LLM RLHF the conventions are usually:
- (no per-token discount — we care equally about all tokens of the response).
- (close to Monte Carlo; some bias for variance reduction).
- Per-token reward except at the last token, where — possibly with the KL penalty subtracted at each token (§9.9).
The value function is a separate neural network trained jointly with the policy — the critic in actor-critic terminology.
5. The value-function critic
The critic estimates the expected return from state . It is trained by regression on the observed returns:
where is the empirical return-to-go.
In LLM RLHF, the critic is usually:
- The same architecture as the policy (a transformer).
- Initialised from the policy or the reward model.
- Trained jointly with the policy in PPO's training loop.
This doubles the GPU memory cost of PPO (we now hold the policy, the frozen RM, and the critic — three transformers — plus the frozen reference policy for the KL penalty of §9.9, which is four). PPO RLHF is memory-hungry compared to SFT or DPO, which is one of DPO's selling points.
6. The full PPO loop for LLM RLHF
Putting it all together:
PPO for LLM RLHF:
Initialize π_θ ← π_SFT, V_ψ (critic, e.g. from RM init), π_ref ← π_SFT (frozen).
For iteration k = 1, 2, ...:
# === ROLLOUT ===
1. Sample a batch of prompts {x_i}.
2. For each x_i, sample y_i ~ π_θ_old(· | x_i), and record per-token log-probs.
3. Compute per-token rewards: r_t = (RM reward at last token) - β · KL(π_θ_old || π_ref).
4. Compute per-token advantages Â_t via GAE using V_ψ.
# === MULTIPLE UPDATES per rollout ===
For epoch e = 1, ..., E_ppo (typically 2-4):
For minibatch in batch:
# Compute importance ratios
log_probs_new = π_θ.log_probs(y_i | x_i, per-token)
ratios = exp(log_probs_new - log_probs_old) # ρ_t per token
# PPO clipped policy loss
loss_policy = -mean( min(ratios · Â, clip(ratios, 1-ε, 1+ε) · Â) )
# Critic loss
loss_value = mean( (V_ψ(s_t) - R_t)^2 )
# Optional entropy bonus
loss_entropy = -mean(entropy(π_θ))
loss = loss_policy + c_v · loss_value - c_e · loss_entropy
loss.backward()
optimizer.step()
# End of epoch: copy θ → θ_old, ψ → ψ_old.
A few important details:
- Multiple update epochs per rollout. This is what importance sampling buys us: one expensive rollout, updates per data point. (More epochs sometimes destabilise — the importance ratios drift further from 1.)
- KL penalty applied at every token. This is the standard practical way to handle the KL constraint we'll derive in §9.9. Alternative: KL only as a single term per trajectory.
- Entropy bonus. A small term that rewards keeping the policy's per-token distribution from collapsing to determinism. Helps exploration. Coefficient .
- Value coefficient. scales the critic loss relative to the policy loss.
6.1 Per-token vs per-trajectory
A subtle implementation choice: the importance ratio can be defined per-token or per-trajectory.
- Per-token: , one ratio per token, applied with the per-token advantage .
- Per-trajectory: , one ratio for the whole sequence.
The per-token form is dominant: it gives finer-grained credit assignment and integrates naturally with GAE's per-step advantages. Per-trajectory is occasionally used in simpler implementations (e.g., RLOO).
7. Hyperparameters and recipes
Standard PPO hyperparameters for LLM RLHF (Llama 2, InstructGPT, OpenRLHF default):
| Hyperparameter | Typical value | |---|---| | Clip threshold | | | GAE | | | GAE | – | | Update epochs | – | | Value coefficient | | | Entropy coefficient | – | | Learning rate (policy) | – | | Learning rate (critic) | | | KL penalty coefficient | –, adaptive (§9.9) | | Rollout batch size | – prompts | | Generation max-new-tokens | – |
The two parameters that materially change training are (the KL penalty, §9.9) and the learning rate. Most other hyperparameters are reasonably forgiving.
PPO for LLM RLHF is mature engineering: open-source implementations (DeepSpeed-Chat, OpenRLHF, TRL) provide drop-in PPO trainers that handle the rollout / update split, multi-GPU sharding, and the four-model memory layout (policy, critic, RM, reference).
8. PPO's weaknesses
PPO works well but has several well-known operational pains.
(a) Memory cost. Holding four transformer-sized models (policy, critic, RM, reference) is expensive. For a 70B policy, this is roughly GB GB just for model parameters in fp16, before activations and optimiser state. Multi-node sharding is standard.
(b) Hyperparameter sensitivity. , the KL coefficient, has to be tuned per dataset. Too small and the policy drifts off-distribution (reward hacks); too large and the policy never moves. Llama 2 used adaptive (auto-adjusted based on observed KL); even so, the recipe required substantial tuning per training run.
(c) Training instability. PPO RLHF runs occasionally diverge: loss spikes, output quality crashes, KL explodes. The literature includes many cautionary tales and many proposed mitigations.
(d) Reward hacking. The policy learns to exploit reward-model errors rather than improve substantively (§9.14). PPO has no structural defence beyond the KL penalty.
These pains motivated DPO (§9.10), which skips the critic, the rollouts, the importance sampling, and the clipping entirely — by deriving the policy update directly from preferences. We will see in §9.10 that DPO is, in a precise sense, "the right answer of PPO at a specific limit."
9. What we keep going forward
-
PPO's clipped surrogate objective is the workhorse RL objective for LLM RLHF since 2017. It allows multi-step updates per rollout via importance sampling, with clipping preventing destabilising large updates.
-
GAE advantages with a learned value-function critic. Per-token advantages replace raw rewards; the critic provides the baseline.
-
The full RLHF PPO loop holds four models in memory: policy, critic, RM, reference. Memory-hungry but mature.
-
Standard , , . Most other knobs are forgiving; (the KL coefficient) requires tuning per run.
-
PPO's weaknesses (memory, hyperparameter sensitivity, reward hacking) motivated DPO and GRPO — the algorithms of §9.7 and §9.10 that recapture PPO's benefits with simpler structure.
The next session derives GRPO — DeepSeek-Math / R1's group-relative simplification of PPO that eliminates the critic.
(a) Derive the surrogate objective from the importance-sampling identity. Show that its gradient at equals the policy gradient.
(b) Sketch the PPO clipped objective as a function of for (i) and (ii) . Identify the region(s) where the gradient is zero.
(c) Describe GAE: write the recursion for and identify the high-variance () and high-bias () endpoints.
(d) List the four transformer-sized models held in memory by the PPO LLM RLHF loop, with a one-line description of each. Estimate the total memory for a 70B-policy run in fp16.
(e) A PPO run reports the importance ratios are routinely within an epoch. Diagnose: is too large or too small? What happens to the gradient signal?
(f) Argue why PPO is not "doing pure policy gradient": identify the role of (i) importance sampling (multi-step reuse), (ii) clipping (trust region), (iii) GAE (advantage variance reduction). For each, name what PPO loses if it is omitted.
References
- [1]
Schulman, J., Levine, S., Abbeel, P., Jordan, M., & Moritz, P. (2015). "Trust region policy optimization" (TRPO). arXiv:1502.05477 — Constrained policy update; the precursor PPO simplifies (9.6, 9.9 §4.3).
↩ - [2]
Schulman, J., Wolski, F., Dhariwal, P., Radford, A., & Klimov, O. (2017). "Proximal policy optimization algorithms" (PPO). arXiv:1707.06347 — Clipped surrogate objective; the workhorse of RLHF (9.6).
↩ - [3]
Schulman, J., Moritz, P., Levine, S., Jordan, M., & Abbeel, P. (2015). "High-dimensional continuous control using generalized advantage estimation" (GAE). arXiv:1506.02438 — GAE; used by PPO inside RLHF (9.6 §5).
↩