Unit 3 of 11 · about 4 hours · derive

Value functions, GAE and PPO clipping for language models

PPO adds a learned credit model and reuses trajectories. This unit audits those extra dependencies before deciding whether a critic is worth its cost for the small study.

You'll be able to

  • Derive prefix-value targets and GAE for a completed language trajectory with explicit terminal masks.
  • Check both signs of the clipped surrogate and explain why clipping does not enforce a hard policy-distance bound.
  • Separate actor advantages, critic targets and old-policy scores in a fixed-rollout audit.

Covers

Value functions and generalized advantage estimation vocabularyImportance ratios, PPO clipping and failure boundaries
1

Read

In this order, only the parts listed

0 of 2 done

  1. RLHF Book Chapter 6: Reinforcement LearningRLHF Book - Policy Gradients, Reasoning and Rejection Sampling

    Proximal Policy Optimization (PPO), Understanding the PPO Objective, Value Functions and PPO; Auxiliary Topics, Generalized Advantage Estimation (GAE)

    Read the token and prefix interpretation, not another general RL first pass. The prose calls clipping a bound on drift; it is only a clipped surrogate, not a hard bound on ratios or KL.

  2. CS336 Spring 2026 Assignment 5: token clippingCS336 Assignment 5 - Alignment and Reasoning RL

    Section 6.2.2, Clipping, and compute_policy_gradient_loss_off_policy

2

Focus on

  • The Reinforcement Learning unit on n-step advantages and Generalized Advantage Estimation derived the estimator; here it runs over token prefixes. A prefix value predicts future reward before the next sampled token, not correctness after reading the answer.
  • Use and . Set terminal bootstrap to zero; decide separately whether a length-limit stop is terminal or censored.
  • Freeze rollout-time values and advantages during reuse. Actor advantage normalization must not replace the unnormalized critic return target.
  • The surrogate plateaus for positive advantages above the upper ratio limit and negative advantages below the lower limit. Other samples and shared parameters can still move those probabilities.
Common mistakes · 3
  • Clipping the ratio and multiplying without the pessimistic minimum. That loses the sign-dependent branches.
  • Using final-answer information as the prefix baseline. It can depend on the action whose score it weights.
  • Assuming GAE is needed for GRPO. Group estimators remove the learned critic; TRL 1.15.0 has no PPOTrainer.
3

Try it yourself

Closed-book, before checking anything

On CPU in PyTorch 2.14.1 float64, use a completed two-action response with rewards [0., 1.], prefix values [0.2, 0.6], zero terminal bootstrap, and . Recover TD residuals [0.4, 0.4] and GAE [0.6, 0.4]. Separately use ratios [0.7, 1.3, 0.7, 1.3], advantages [1., 1., -1., -1.] and clip width 0.2. The negative mean clipped surrogate is 0.05; its current-log-probability gradients are [-0.175, 0., 0., 0.325], within floating-point tolerance.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.What happens to GAE at the final terminal action?Answer

    No future value or advantage is bootstrapped. The advantage is the terminal reward minus the prefix value.

  2. 2.A negative-advantage token has ratio 1.3 and clip width 0.2. Is its surrogate gradient zero?Answer

    No. The unclipped negative term is smaller, so it remains active and pushes the probability down.

  3. 3.Can every sampled token be on a flat branch while KL continues to grow?Answer

    Yes. The clipped objective does not impose a hard trust region. Other losses, shared parameters and optimizer state can still change the policy.

5

Practice

  • CS336 Assignment 5: compute_policy_gradient_loss_off_policy

    Section 6.2.2; test_compute_policy_gradient_loss_off_policy compares unclipped and clipped per-token loss snapshots

    Add your own GAE boundary fixture and finite-difference checks away from clipping kinks. No critic training run is required; the mini-project can use the simpler group estimator. Do not publish solutions.

You're done when

The public clipping snapshot and your GAE boundary and sign-branch gradient tests pass, and you can explain a policy change beyond the clipping interval without calling it a contradiction.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 18 more in the section
  • CS336 Assignment 5 - Alignment and Reasoning RLStanford University · primary

    Do Sections 3 to 5: prompting and grading baselines, baseline_calcs, then the Section 4.2 functions (tokenization, response log probabilities, rollout rewards, group-normalized rewards, loss aggregation, the on-policy GRPO step) until the GRPO and metrics tests pass on CPU. The handout runs OLMo-2 1B with a two-GPU vLLM loop; run a small GRPO on a model and task that fit your GPU instead, with one normalization ablation and one preserved failure.

  • RLHF Book - Policy Gradients, Reasoning and Rejection SamplingNathan Lambert · required

    Read Chapter 6 (policy gradients: REINFORCE, RLOO, PPO, GRPO and loss aggregation), Chapter 7 (reasoning), Chapter 9 (rejection sampling), Chapter 12 (distillation) and Chapter 15 (regularization). Do the Suggested Experiments in Chapters 6 and 9. Derive only the estimator used in your experiment, then use Chapters 14 and 15 to design the reward-hacking tests.

Also in this section