Alignment (RLHF, DPO, RLVR, CAI)
Preference optimization, the algorithmic centerpiece of post-training. Where SFT (Chapter 13) taught the model to respond, this chapter teaches it to respond well. The chapter frames generation as a token-level MDP and walks four method families as answers to one weight-and-leash problem, classical RLHF (reward model + PPO with KL folded into the reward), DPO (the closed-form offline collapse), GRPO (critic-free, with a group-relative Monte Carlo baseline), and RLVR (a deterministic verifier as the reward, powering DeepSeek-R1 and o1 reasoning). It also covers Constitutional AI / RLAIF, replacing the human half of preference collection with an AI critic guided by a written constitution. Two pedagogical centerpieces, the DPO derivation and a synthesis showing all four methods are reweighted next-token prediction with a leash to the reference.
Chapter 13 left you with an SFT’d model. It follows the chat template, knows it is the assistant side of a conversation, and produces grammatical responses to instructions. What it does not know is how to produce good responses. Ask it a hard question and it may confidently invent an answer. Ask it for help on something edgy and it may refuse harmlessly or comply harmfully. The format is there; the quality and the alignment are not.
Preference optimization fills that gap. The premise is operational, not theoretical: humans cannot easily demonstrate the best response to an open-ended question, but they can readily compare two responses and pick the better one. Collect a dataset of preference pairs, instruction plus a chosen response plus a rejected response, and train the model to produce more chosen-like outputs and fewer rejected-like ones. The mechanics of how to do that turn out to be where this chapter lives.
Four families of methods divide the space. Classical RLHF (Christiano et al. 2017; InstructGPT 2022) trains a reward model on preferences and then optimizes the policy with PPO against the reward, constrained by a KL penalty back toward the SFT model. DPO (Rafailov et al. 2023) is the mathematical insight that the KL-regularized RL objective has a closed-form solution that reparameterizes into a supervised loss, no reward model, no PPO loop, no on-policy sampling. GRPO (DeepSeekMath 2024) keeps the on-policy RL setup but drops PPO’s learned value function, using an empirical group mean as the baseline; this halves the model count and pairs naturally with verifiable rewards. RLVR (DeepSeek-R1, o1, late 2024 – early 2025) is the reward-design move: replace the learned reward model with a deterministic verifier, math correctness, code tests passing, theorem-prover validity, and bootstrap long-chain reasoning from the verifier alone. This chapter walks all four and sets up the token-level frame that makes them instances of one idea.
The setup: SFT teaches format, not quality
After SFT the model is recognizable as a chatbot. It begins responses where it should, addresses the user, structures answers, knows to stop. None of this guarantees that the answers are correct, honest, or helpful. The most visible failure modes are not subtle. The model may confidently invent factual answers to questions it does not know. It may refuse a benign request because the surface texture pattern-matches something harmful, or comply with a harmful request because the request used a benign frame. It may be verbose, hedging, or sycophantic in patterns inherited from the SFT data that do not generalize to other queries. Reasoning is shallow; complex multi-step problems often fall apart. Each of these is a quality failure, not a format failure.
The reason SFT cannot address quality is structural. Every example in an SFT dataset is an instance of a valid response. Nothing in the data says one response is better than another. Gradient descent on a single demonstration teaches the model to imitate that demonstration; it gives no signal about which of the model’s own samples is preferable to which of its other samples. A higher-quality SFT dataset shifts the demonstrations the model imitates, but the architecture of the learning signal, copy this, does not change. To teach quality you need a learning signal that compares.
Preference data provides that signal at low cost. Asking a human “which of these two responses is better?” is far easier than asking them to write the ideal response from scratch. Inter-rater agreement is imperfect (typical 65–75% on real preference data), but the signal is real and aggregates well across raters. Once the preference dataset exists, the question is how to turn it into a training signal. The four families of methods correspond to four different answers, and the bridge from Chapter 13 is the SFT model itself, which becomes the reference policy that every preference method stays close to.
Generation as a token-level MDP
The choice this chapter asks you to make, how to turn preference data into model updates, has a useful skeleton underneath. Drawing it once, in the language of reinforcement learning, makes the rest of the chapter (PPO, DPO, GRPO, RLVR) look like four answers to the same two questions rather than four unrelated algorithms.
An autoregressive language model generating a completion for a prompt defines a Markov decision process where the state is the prompt plus the tokens generated so far, the action is the next token, the transition is deterministic (you append the token, and nothing else happens), and the policy is exactly the model’s next-token softmax:
Two features of this MDP are unusual and they drive most of the design choices in the rest of the chapter. The first is that the only randomness is the policy’s own sampling: the environment does not branch, so “exploration” is whatever diversity the policy puts in its own samples (temperature, top-, group size). The second is that the reward is sparse and terminal: a reward model or verifier looks at the finished response and emits a single scalar; nothing scores individual tokens directly. A correct final answer has to credit however many intermediate reasoning tokens produced it, and none of those tokens can be individually judged. This is the credit-assignment problem, and every algorithm in this chapter is partly a story about how it handles it.
The log-probability of a completion factorizes over tokens:
This is why everything in the chapter ultimately reduces to per-token terms. The chapter’s organizing claim, which the synthesis section returns to, is that all of the methods we are about to study are doing the same thing at the token level: maximizing with a per-token weight that depends on how good turned out to be, subject to a leash that prevents the policy from drifting too far from the reference. Supervised fine-tuning (Chapter 13) is the trivial case where the weight is on every gold token and there is no leash. Every other method differs in how it computes the weight and where it attaches the leash.
That framing gives the chapter its two organizing questions. How do you compute the per-token weight? PPO answers with a learned value function plus generalized advantage estimation. DPO answers with the implicit reward inside a preference loss. GRPO answers with an empirical group mean as the baseline, broadcast back to every token of each sampled completion. RLVR answers by replacing the learned reward with a deterministic verifier. How do you keep the model near the reference? All four answer with a KL constraint to a frozen , but they place it differently: PPO folds it into the per-token reward, DPO bakes it into the loss through the implicit-reward parameterization, and GRPO adds it as an explicit term in the loss with the k3 estimator.
Preference data and the Bradley-Terry model
Preference data structure
A preference example is a tuple . is the prompt or conversation context. , winner, also called chosen, is the preferred response. , loser, also called rejected, is the dispreferred one. The labels come from a rater: shown and two candidate responses , the rater picks which is better, and that choice determines which one is and which is . The two candidates typically come from the same model at different temperatures, or from two different models, or from one model with one of the two responses lightly edited. The construction matters less than the contrast: the dataset records that one is preferred, and the magnitude of preference is not recorded.
The data is noisier than it looks. Inter-rater agreement is typically 65–75%: different humans disagree on which response is better, especially on open-ended or edgy queries. Some of that disagreement is genuine value diversity; some is bias. The most-studied biases are length (longer responses are preferred even when they should not be), confidence (assertive phrasing rates higher than equivalent hedged phrasing), and formatting (responses with lists, headers, or markdown structure beat the same content as plain prose). These biases propagate. A reward model trained on this data inherits them; a policy trained against that reward model inherits them again. The chapter returns to the consequences in section 11.
The Bradley-Terry model
To turn discrete preference labels into a continuous training signal, the canonical move is to posit a hidden quality score for every pair and assume that the probability a human prefers over is a sigmoid of the score difference:
This is the Bradley-Terry model of pairwise preferences. The sigmoid choice is not arbitrary: it is the natural distribution mapping a real-valued score difference into a probability of preference. A difference of maps to a coin flip; a difference of maps to about preference; a difference of maps to . The exact shape comes from imagining each response sampled noisily around its hidden score, with the Gumbel noise that yields the softmax/sigmoid form. The only thing the model needs to identify is the reward function .
The reward function is learned. Train a model whose output is a single scalar, on the binary cross-entropy objective for the Bradley-Terry probabilities:
This is just classification: predict which response a human preferred, using a sigmoid log-likelihood on the score difference. Architecturally the reward model is usually the SFT model with the language-modeling head replaced by a scalar output, fine-tuned on the preference data with this loss. After training, scores any response by how much humans should prefer it. Differences of have a probabilistic interpretation; absolute values do not.
With a reward model in hand, the next question is how to use it.
Classical RLHF: reward model + PPO
The three-stage recipe
The Ouyang et al. (2022) InstructGPT paper crystallized the canonical RLHF recipe, the three-stage pipeline that produced ChatGPT and most chat models since. Stage 1 is SFT (Chapter 13), which produces a starting policy that knows it is a chatbot. Stage 2 is reward modeling: train on a fresh preference dataset using the Bradley-Terry loss from section 3. Stage 3 is PPO fine-tuning: optimize a policy , initialized from , to maximize the learned reward , while penalizing divergence from via a KL constraint. Each stage uses a different dataset and a different optimizer state; the SFT model is the bridge between them.
The optimization objective
The objective combines a reward term and a KL term:
In words: the policy wants high reward but is penalized for drifting from the reference policy , typically , the frozen SFT model. The temperature controls the trade-off. Small lets the policy chase reward aggressively, accepting large KL; large keeps the policy close to the SFT model, accepting smaller reward gains. For now, take it as a regularizer.
The starting point: policy gradients
The objective above is an RL one: the policy generates responses, the reward model scores them, the gradients update the policy. Because the policy is generating its own training data as it learns, the optimization is on-policy. The starting point for this kind of optimization is the policy gradient theorem, which expresses the gradient of expected reward as an expectation of per-token score-function gradients weighted by an advantage:
Read this carefully. The gradient on each token is (advantage) times (gradient of token log-prob). That is the cross-entropy gradient from SFT with a per-token weight attached: positive advantage pushes the log-probability of that token up at that state; negative advantage pushes it down. The rest of PPO is machinery to estimate stably and to keep each update from moving the policy too far.
The naive instantiation is REINFORCE: set to the trajectory’s total reward, take one gradient step per batch of rollouts, repeat. Two problems. First, variance is huge: every token in a high-reward trajectory is pushed up, including the irrelevant ones. Second, you can only take one gradient step per rollout, because after one update the samples were drawn from a now-stale policy, biasing the estimator. Generating rollouts from a multi-billion-parameter model is the expensive step in this whole pipeline; one gradient step per rollout is wasteful. PPO fixes both, in that order.
Importance sampling and the clipped surrogate
PPO lets you take multiple gradient steps on each batch of rollouts by reweighting with an importance ratio, and bounds the per-step policy change with a clip. Let denote the policy snapshot at the start of this PPO iteration, the one that generated the current batch of rollouts. The per-token ratio is
and PPO’s clipped surrogate objective is
with . The ratio reweights stale samples so the same batch supports two to four epochs of updates; the clip bounds the per-step move so no single update destabilizes the policy. The of clipped and unclipped looks fiddly but does exactly one job: it makes the bound one-sided. Walking the cases makes this concrete.
PPO is not an independent invention; it is a deliberate simplification of TRPO (Trust Region Policy Optimization, Schulman et al. 2015, arxiv.org/abs/1502.05477), which enforced the same “don’t move too far from the old policy” idea as a hard constraint on KL divergence, solved every step by a second-order (natural-gradient) optimization over the whole parameter space. That machinery is accurate but expensive and fiddly to implement at scale. PPO’s contribution was to replace TRPO’s constrained optimization with the first-order clip above: cheaper, easier to implement, and it plugs into ordinary SGD. PPO is the simpler variant TRPO inspired, not a competing algorithm invented in parallel, a relationship worth keeping straight since the names get used almost interchangeably in casual discussion of “trust-region” RL methods.
Case (good token, push up). When , the clip does nothing and both terms equal . Once , the clipped term saturates at , which is smaller than the unclipped term, so the picks it: the objective flatlines and the gradient is zero. Translation: PPO lets you raise a good token’s probability up to about times its old probability, then it stops, on the grounds that you are extrapolating past the data you actually sampled. Conversely, if an earlier minibatch overshot and drove , the unclipped term becomes the smaller one and the picks it over the clipped, keeping the gradient active so the policy can pull the probability back up. The clip caps reward-seeking; it never blocks error correction.
Case (bad token, push down). Mirror image. Once , the gradient is zero (you have already lowered this token’s probability enough). If an earlier update accidentally raised above , the gradient stays active to push it back down. Same one-sided shape: PPO stops you from over-decreasing, but it always lets you correct.
This piecewise gating is the whole content of “PPO is conservative.” It is not a trust-region constraint imposed externally; it is a piecewise-linear objective whose gradient turns on or off depending on how far has moved from on the current token.
Advantage estimation: the critic and GAE
The advantage still needs a baseline. Without one, every token in a trajectory whose total reward was gets pushed up by , even the mediocre middle tokens. The standard fix is to learn a value function , a second model (the critic), typically the policy with a scalar head, often initialized from the reward model. The critic predicts the expected return from state . Subtracting from the realized return turns “this trajectory was good” into “this token did better than expected from this state,” which is the signal you actually want and which cuts gradient variance by roughly an order of magnitude.
The PPO-standard estimator is Generalized Advantage Estimation (GAE), an exponentially-weighted sum of one-step TD residuals:
trades bias against variance: is single-step TD (low variance, high bias), is full Monte Carlo (the opposite). is the discount, typically close to for LLM episodes since they are short. The critic is trained alongside the policy by regression toward the empirical returns :
The full PPO loss is then three terms,
clipped surrogate (maximized), value loss (minimized), and an entropy bonus on (maximized, to keep the policy from collapsing to determinism mid-training).
Where the KL lives: per-token reward shaping
The KL penalty from the optimization objective is not a separate loss term in PPO; it is applied to the reward signal at every token. Concretely, the reward each token receives during the GAE computation is
For every token before the last, the reward is just the per-token KL penalty (negative when the policy moved away from on this token). At the final token only, the reward model’s scalar score is added on top. This is the part of PPO most worth holding in your head at the token level: the dense signal across tokens is the per-token KL, which keeps generations coherent because every token is gently pulled toward the reference distribution, and the sparse signal at one token is the reward model’s score, deposited at EOS and then smeared back over earlier tokens by GAE’s discounted sum. The credit-assignment problem from section 2 gets resolved jointly by the critic (which learns to predict expected return at each state), GAE (which converts the sparse end-of-sequence reward into per-token advantages), and the clip (which keeps the resulting updates within the trust region). is often controlled adaptively to hit a target KL: raise it when the policy drifts too far, lower it when the policy is barely moving.
The four-model system and the rollout loop
The full PPO-RLHF setup keeps four models resident at training time:
| Model | Role | Trained? |
|---|---|---|
| Policy | the actor being optimized | yes |
| Value | critic / baseline | yes |
| Reward | scores completions | frozen (trained earlier in stage 2) |
| Reference | KL anchor, the SFT checkpoint | frozen |
Two models receive gradients; two are forward-only. Memory and compute scale with that footprint, and that footprint is the main reason most open-source teams chose DPO and frontier teams still pay the price for PPO. The training loop given the pieces is straightforward to describe. Sample a batch of prompts. Generate completions with (sequential decoding, slow). Score with , compute per-token KL against , run GAE through the critic to get advantages. Do two to four epochs of minibatch SGD on , with the importance ratio correcting for drifting from across epochs. Copy . Repeat.
The KL constraint
Why the constraint matters
The reward model is an imperfect proxy for human preference. Section 3 noted that reward-model accuracy on held-out human preferences is typically only –%. The remaining –% is noise: the RM gets a sizable fraction of comparisons wrong, and the wrongs are not random; they cluster around the biases the RM inherited from preference data. Optimizing aggressively against an imperfect proxy is dangerous. Without a constraint, the policy will exploit the errors of the reward model: it will find out-of-distribution responses that score artificially high on the RM but are not actually preferred by humans. This is reward hacking, and it is the central failure mode of unconstrained RLHF.
The KL constraint is the standard defense. By penalizing , the optimization is forced to stay in the vicinity of the SFT model in policy space. Any response with high probability under must also have non-trivial probability under , otherwise the KL penalty explodes. This rules out the most extreme reward-hacking failure modes, where the policy finds bizarre responses far from the SFT distribution that fool the RM. The constraint is, in Goodhart’s-law terms: as soon as you make the RM the target rather than a measure, the RM will degrade; stay close enough to the original distribution that we know the responses are still recognizable.
What happens without it
Empirically the failure is fast and dramatic. Strip the KL term out of the PPO objective and within a few hundred steps the policy converges to degenerate solutions: repetitive sequences, formatting tricks, hallucinated tokens, sometimes specific reward-positive phrases hammered until the RM saturates. The responses become unrecognizable as language; the model has essentially learned to attack the RM as an adversarial example generator rather than to produce useful text. Even before the dramatic failures, removing the constraint causes the base pre-training capabilities to degrade: the model drifts away from the broad linguistic and factual competence that pre-training instilled, because that competence is not what the (narrow) reward model rewards.
Choosing is a calibration exercise. Too small () and reward hacking kicks in. Too large () and the policy barely moves from : the RL signal can’t overcome the regularizer, and you get the SFT model back with extra compute spent. Empirically the sweet spot for PPO-style RLHF is to on most setups, which is why most papers report values in that range without much justification. The number is small because the RM rewards have larger magnitudes than the KL: a typical reward delta of a few units competes with a KL of a fraction of a nat, and the trade-off lands at small .
The DPO derivation shows that the entire pipeline of section 4 (train an RM, run PPO, hold both models in memory, generate rollouts) has a much simpler equivalent when you do the math: a supervised loss on preference pairs. In the token-level frame from section 2, DPO is the offline instance of the same weight-and-leash problem PPO solves online; the leash this section established stays exactly where it is, but baked into the implicit reward inside the loss instead of folded into a per-token signal.
DPO: direct preference optimization
The chapter’s pedagogical centerpiece. The DPO derivation (Rafailov et al. 2023) shows that the KL-regularized RLHF objective from section 4 has a closed-form optimal policy, and that substituting that closed-form solution back into the Bradley-Terry probability of preference produces the loss in (14.dpo) below, expressed in and alone. The result is a supervised loss on preference pairs, same destination as RLHF, none of the operational complexity. The derivation runs in four steps.
The closed-form optimal policy
Start with the RLHF objective from section 4 (reward maximization minus times KL). This is a constrained optimization over the policy for each fixed . A standard variational argument (Lagrange multipliers on the per-context probability constraint ) shows that the constrained optimum has the form
where is a normalizer over all possible responses to context . The intuition is clean: the optimal policy is the reference policy reweighted by the exponential of the reward. High-reward responses are boosted relative to ; low-reward responses are suppressed. The temperature controls how aggressively. This is the canonical form for a KL-regularized RL objective; the same derivation appears in soft Q-learning and maximum-entropy RL.
Solving for the reward
The closed-form expression is the wrong direction for what we want. We do not actually want to use , we cannot, because is intractable (it sums over all sequences). We want to learn a that matches . The trick is to rearrange the closed-form expression for in terms of :
The reward equals times the log-ratio of optimal to reference policy, plus a context-only term ( depends only on ). This says something striking: the reward function and the optimal policy are interchangeable. If you know one, you know the other (up to the term, which depends only on the context).
Substituting into Bradley-Terry
The Bradley-Terry preference model from section 3 says the probability of preferring over is the sigmoid of the reward difference. Substitute the expression for in terms of :
The two terms, one for , one for , cancel, because depends only on the shared context, not on . The intractable normalizer disappears entirely. What is left is a probability of preference expressed as a function of the policy log-ratios alone. No reward; no ; no sum over all possible responses. Just on , on , and the corresponding values under .
The DPO loss
Now run maximum likelihood. The preference data records that was preferred over ; maximize the log-likelihood of that observation under the Bradley-Terry probability above. Replacing with the trainable gives the DPO loss:
Equation (14.dpo) is the DPO loss. To compute it, take a preference pair from the dataset; compute and (two forward passes through the training policy on the chosen and rejected responses); compute and (two forward passes through the frozen reference); plug into the sigmoid; backprop. There is no reward model, no PPO loop, no on-policy sampling. The loss is supervised; the optimizer is Adam; the dataset is offline.
The quantity that appears inside the sigmoid is the implicit reward, the reward function the policy is implicitly optimizing. The training policy is the reward model. The paper’s subtitle, “Your Language Model is Secretly a Reward Model”, is the central observation: in the DPO formulation, the policy parameterizes its own implicit reward, and the two objects you held separately in RLHF (policy and RM) collapse into one.
The DPO formulation has spawned a family of related methods.
DPO variants: IPO, KTO, ORPO, SimPO
Four variants
IPO (Identity Preference Optimization, Azar et al. 2023) replaces DPO’s sigmoid log-likelihood with a squared-error objective on the implicit-reward difference, regularized toward the reference. It behaves better than vanilla DPO when preference labels are nearly deterministic, every rater always picks the same response, a regime where DPO can drift to extreme implicit rewards because the sigmoid loss never saturates. The IPO objective adds a quadratic penalty that does saturate, stabilizing the deterministic-preferences case.
KTO (Kahneman-Tversky Optimization, Ethayarajh et al. 2024) drops the pair format entirely. Instead of triples, KTO works with tuples, a single response labeled as desirable or undesirable. This is much easier to collect in production: thumbs-up/thumbs-down feedback on a deployed model, accept/reject signals from human moderators, anything where you have a binary judgment on individual responses. The loss is built on a prospect-theory-style value function, gains and losses are asymmetric, with the same magnitudes, but the data format is the headline advantage.
ORPO (Odds-Ratio Preference Optimization, Hong et al. 2024) folds SFT and preference optimization into a single training stage. It adds an odds-ratio penalty term to the standard SFT log-likelihood, jointly maximizing the probability of chosen responses and penalizing the probability of rejected ones. No reference model required, the same base model trains directly from raw base to aligned chatbot in one stage. This is appealing when you want to skip a separate SFT phase and when you have the preference data up front.
SimPO (Simple Preference Optimization, Meng et al. 2024) drops the reference model and uses length-normalized log probabilities directly. The implicit reward becomes , sum of token log-probs divided by sequence length, rather than DPO’s log-ratio against a reference. Two consequences: SimPO uses half the memory (no to keep loaded), and the length normalization directly addresses the length bias that DPO inherits from preference data. Empirically competitive with DPO at lower complexity.
The four variants stacked next to each other, with DPO as the baseline for comparison:
| Variant | Data format | Reference model | Loss shape | Length handling |
|---|---|---|---|---|
| DPO | pairs | required | sigmoid log-likelihood on implicit-reward difference | none (inherits length bias) |
| IPO | pairs | required | squared error on implicit-reward difference | none |
| KTO | tuples | required | prospect-theory value function (asymmetric gains/losses) | none |
| ORPO | pairs | not required | SFT log-likelihood + odds-ratio penalty (single stage from base) | none |
| SimPO | pairs | not required | sigmoid on length-normalized log-probs | built in (divide by $ |
Which to use
DPO is the default. It is well-studied, stable across hyperparameters, and supported by every alignment library (TRL, Axolotl, OpenRLHF). Start there. Switch to SimPO if you are seeing length-bias problems and want to drop the reference model, it is simpler and slightly cheaper. Switch to KTO when you only have single-response ratings rather than pairwise comparisons (deployed-model thumbs-up/down feedback is the canonical case). Switch to ORPO when you want a single-stage pipeline directly from a base model. Switch to IPO when your preference labels are highly deterministic (an unusual regime, most real preference data has substantial rater disagreement).
GRPO keeps PPO’s clipped surrogate but drops the value function: sample a group of completions per prompt and use the group’s mean reward as the baseline directly. RLVR then changes the reward source itself, replacing the learned reward model with a deterministic verifier. The combination of those two changes, GRPO plus RLVR, is what DeepSeek used to train R1.
GRPO: critic-free policy optimization
PPO works, but the four-model footprint is expensive: policy, value, reward, reference, two of them trained. The value function is the most fragile of the four. Critics for LLMs are hard to fit; their predictions are noisy; and the GAE math leans on them more than it should. GRPO (Group Relative Policy Optimization, Shao et al. 2024, DeepSeek-Math) removes the value function entirely. The importance ratio and clip from PPO stay; the baseline is computed empirically per prompt instead of being learned.
The group baseline
For each prompt in the batch, sample a group of completions from . Score them all with a reward function (a learned reward model or a verifier, GRPO is agnostic), getting rewards . The per-completion advantage is the reward standardized within the group:
In the outcome-supervised variant (the one R1 uses), this single scalar is then broadcast to every token of completion . Every token of an above-average completion gets positive advantage; every token of a below-average one gets negative. The credit-assignment problem from section 2 gets resolved by ignoring it: at training time, GRPO acts as if every token in the completion shares responsibility for the final reward equally.
This is legitimate because the group mean is a Monte Carlo estimate of , the exact quantity PPO’s critic was trying to approximate. With on the order of to samples per prompt, the empirical mean is already a reasonable baseline, and as grows the variance of the advantage estimate falls. You traded a learned function approximator for empirical samples drawn fresh per prompt: more rollouts per prompt, no critic to train. For LLM-scale models this is usually a good trade because the critic was the wobbly part of the four-model system.
The GRPO objective
GRPO uses the same clipped surrogate as PPO with the group-relative advantage, and places the KL penalty differently: as an explicit term in the loss rather than folded into the per-token reward. Let as before. The objective is
The KL uses Schulman’s k3 estimator, which is always positive and lower variance than the naive log-ratio:
The substantive difference from PPO: PPO folds the KL into the per-token reward (so the KL contributes to advantages via GAE); GRPO puts it directly in the loss as a separate term (so the gradient on KL flows through the explicit derivative of ). Both prevent drift; the location changes how the gradients arrive.
Two edge cases bite in practice and are worth holding alongside the math.
Dead groups. If every completion in the group gets the same reward (all correct or all wrong on a math problem, for example), then and the standardized advantage is undefined; in practice collapses to zero and the group contributes no gradient. This is not a bug, it is the algorithm telling you that the prompt is currently too easy or too hard for the model to learn from. Some training pipelines (notably DAPO) detect this and resample more prompts to keep batches informative.
Length and difficulty bias. The token-mean inside the sum penalizes per-token loss on long wrong completions less than on short wrong ones, which biases training toward verbosity. The standardization scales gradients up on prompts where the group happens to have low variance, biasing training toward “consistent” prompts at the expense of hard ones. Dr. GRPO (Liu et al. 2025) removes both biases: sum the per-token losses without dividing by length, and use the un-standardized as the advantage. The corrected version is what most recent reasoning-model recipes use.
Why GRPO pairs naturally with reasoning
The group baseline does something subtle that fits reasoning training. Consider a hard math problem the model currently solves times out of . The three correct attempts get advantage ; the five wrong attempts get advantage . The standardization automatically calibrates for problem difficulty. An easy prompt where the model nails it produces zero advantage (no learning, this is the dead-group case in the positive direction), and a too-hard prompt where the model misses also produces zero (dead group, negative direction). The signal lives precisely on prompts the model is uncertain about, the same prompts where there is the most room to improve.
That property is what makes GRPO the natural partner for verifiable rewards. When the reward function is a binary correctness check, the group splits cleanly into “the few that worked” and “the many that did not,” and the policy is pushed toward whatever reasoning trace the working completions used.
RLVR: RL with verifiable rewards
When rewards are objective
Classical RLHF uses a learned reward model to approximate human preferences. The signal is noisy by construction, section 3’s –% inter-rater agreement, propagated through a model whose own held-out accuracy is in the same range. RL is famously sensitive to reward noise; much of the operational complexity of RLHF (the KL constraint, the conservative , the iterative RM updates) exists to compensate for that noise.
But not every task has noisy rewards. For some tasks the reward is objectively verifiable: math problems have answers you can check against ground truth; programs have tests they either pass or fail; theorems have proofs a verifier either accepts or rejects; chess games have a win/loss/draw outcome. For tasks like these, the “reward function” can be a verifier, a calculator, a compiler, a theorem prover, a game referee, and there is nothing learned about it. The reward is correct by construction.
RLVR, RL with Verifiable Rewards, uses these verifiable rewards directly in place of a learned reward model. The point worth emphasizing is that RLVR is a reward-design choice, not an optimization algorithm: you can pair it with PPO, with GRPO from section 8, or with anything else that consumes a scalar . The choice that produced R1 was GRPO + RLVR, and that combination is what people usually mean colloquially when they say “RLVR” in 2025: a group of completions per prompt, a verifier scoring each completion, and a clipped GRPO update with the group-relative advantage broadcast across the tokens of each sampled trace. Because the reward signal is not noisy, RL works much better than under a learned reward model. The policy can chase the reward harder; the KL leash can be relaxed; reward hacking is structurally harder because the verifier is the truth about the answer (you cannot fool a calculator into accepting a wrong number, though you can still fool a weak test suite or a brittle answer-extraction regex, which is its own discipline).
Concretely: take a math prompt “what is ?” and sample completions. Say the rewards from the verifier come back as , two correct, two wrong. The group mean is ; the standard deviation is . The advantages are , one per completion, and each one is broadcast across every token of its completion. Two epochs of clipped GRPO updates then push every token of the two correct traces up in and every token of the two wrong traces down, with the explicit KL term holding the policy near . There is no reward model in the loop, and there is no critic. The verifier and the group baseline replaced both.
DeepSeek-R1’s recipe
The headline demonstration is DeepSeek-R1 (DeepSeek-AI, January 2025), the first open-weights frontier reasoning model. The recipe has five stages. Start from a base model (DeepSeek-V3-Base). Apply cold-start SFT on a small high-quality reasoning dataset, a few thousand carefully curated math and code examples with explicit chain-of-thought, to stabilize the model’s output format. Then run large-scale RLVR using GRPO (section 8) on verifiable math and code problems, scoring each rollout by whether the final answer is correct or whether the code passes its tests. Use rejection sampling on the RL-trained model to generate a much larger synthetic dataset of high-quality chain-of-thought traces, keep only the traces that lead to a correct answer. Finally, run a second SFT + RLVR pass on the augmented data.
A parallel experiment in the same paper, DeepSeek-R1-Zero, skipped the cold-start SFT entirely. Pure RLVR on a base model. It worked, the model learned to reason and matched many R1 capabilities, but with stability issues: language mixing between Chinese and English mid-trace, poor readability, occasional incoherent passages. The cold-start SFT in DeepSeek-R1 stabilized these issues without losing the reasoning gains. The lesson: a small SFT pass before RLVR helps; pure RL from base is the harder path but is possible.
OpenAI’s o1 (September 2024) followed an analogous recipe at frontier scale. Public details are limited, but the published behavior, a model that produces long internal chain-of-thought before answering math, code, and reasoning queries, and whose reasoning capability scales with inference-time compute, is consistent with large-scale RL on verifiable tasks.
Reasoning emergence
The most striking finding of the RLVR work is that long chain-of-thought emerges from RL training without being explicitly programmed. As RLVR training progresses, the model’s traces grow longer. It starts trying multiple approaches before committing. It backtracks. It restates the problem. It checks its own work. None of this was hand-engineered, there was no SFT data showing it how to think, no chain-of-thought template prompting it to produce intermediate steps. The verifier only checked the final answer.
The mechanism is plain in hindsight: on hard problems, thinking longer increases the probability of arriving at a correct answer, and correct answers get reward. The model discovers this by gradient descent. Once long traces are reliably correlated with higher reward, the policy learns to produce them. The model writes its own thinking template. This is the most exciting recent development in post-training: capabilities, not just behaviors, emerging from reward maximization on objective tasks.
Where RLVR applies
RLVR works where the answer is checkable. Math (the final numeric answer can be compared to ground truth). Code (the test suite either passes or it does not; the code compiles or it does not). Theorem proving (a proof verifier accepts or rejects). Games (you won or you lost). Structured outputs (the JSON parses and matches the schema, or it does not).
It does not apply to subjective tasks. Creative writing has no objective verifier. Open-ended advice has no ground-truth answer. Conversation quality is judgment-based, and the judgments are exactly the noisy preference data that classical RLHF/DPO were built to handle. For tasks like these, RLVR does not replace preference learning, they serve different purposes. The current state-of-the-art reasoning models use both: classical RLHF or DPO for alignment on open-ended tasks, RLVR layered on top for reasoning capability on verifiable ones.
Constitutional AI: RL from AI feedback
Every method so far assumed the preference labels come from a human rater. That assumption has a specific cost the earlier sections did not count: to label “which of these two responses is more harmful,” a human has to read the harmful one. Scaling harmlessness training to the volume of red-team prompts a production model needs means exposing a large number of human raters to abusive, violent, or otherwise disturbing content, repeatedly, as a job. Constitutional AI (Bai et al. 2022, arxiv.org/abs/2212.08073) removes the human from that specific step. In its place goes an AI model, guided by a short written list of principles the paper calls a constitution, and the resulting labels drop into the reward-model-plus-PPO machinery from section 4 essentially unchanged.
The self-critique-and-revise loop
The first phase (Bai et al. call it SL-CAI) generates a supervised dataset the same way an SFT dataset is generated, except the “demonstrations” are produced by the model itself, not by human writers. Prompt the model with something a red-teamer would ask, sample a response, then show the model its own response alongside a constitutional principle (“choose the response that is least likely to be harmful,” phrased a dozen different ways across a small fixed set of principles) and ask it to critique that response against the principle. Then ask it to revise the response in light of its own critique. Repeat the critique-revise step a few times, then fine-tune a fresh copy of the model, via ordinary SFT (Chapter 13), on the final revised responses. The resulting model, call it , has been taught harmlessness through the model’s own iterated self-correction rather than through a human demonstrating the corrected response.
RLAIF: AI preference labels in place of human ones
The second phase, the one people mean when they say RLAIF (RL from AI Feedback), replaces the human step in section 3’s preference-collection pipeline, not the reward-model or PPO machinery downstream of it. Sample a pair of responses to a prompt from , show both to an AI model conditioned on a constitutional principle, and ask it to state which response better satisfies the principle. That choice becomes the label: whichever response the AI preferred is , the other is . Everything from section 3 onward runs unmodified on top of these labels: the Bradley-Terry loss trains a reward model on the AI-generated pairs exactly as it would on human-generated ones, and PPO optimizes against that reward model with the same KL leash to the reference from section 5. The constitution never touches the RL loop directly; it only shapes which labels the reward model is trained to predict.
Two things make RLAIF more than “let the model grade its own homework.” First, the constitution is written down and inspectable: a fixed, auditable list of principles, rather than whatever implicit standard sits in a given rater’s head. Second, the same base capability that makes a model useful, understanding what a harmful request looks like, also makes it a competent critic of that request, at a fraction of the cost and none of the human exposure to the harmful content itself. The tradeoff is that the AI judge’s reliability is bounded by the underlying model’s own understanding of the constitution; Bai et al.’s original recipe kept human labels for helpfulness preferences (a domain where getting it right does not require a rater to look at anything disturbing) and used AI feedback only for harmlessness, rather than removing humans from the pipeline entirely.
Synthesis: every method is reweighted next-token prediction
Section 2 promised that PPO, DPO, GRPO, and RLVR are four points on one map. With all four laid out, the map is now easy to draw. The fixed point of comparison is supervised fine-tuning from Chapter 13. SFT maximizes on every gold token, with weight on each token and no leash:
Every method this chapter covered is a generalization of that gradient: same per-token score function, but with a per-token weight and a leash to the reference. The differences are entirely in those two slots.
The four methods, side by side
The methods only differ along four axes: where the per-token weight comes from, what baseline anchors that weight, what produces the underlying reward, and where the KL leash to is applied. Stacked:
| Method | Per-token weight | Baseline source | Reward source | KL leash placement | Models held |
|---|---|---|---|---|---|
| PPO | GAE over per-token TD residuals | learned critic | learned reward model | per-token, inside the reward | 4 (policy, critic, RM, ref) |
| DPO | scalar prefactor , broadcast to all tokens (positive on chosen, negative on rejected) | the paired rejected completion (contrast) | preferences (no explicit reward) | implicit, via in the parameterization | 2 (policy, ref) |
| GRPO | group-relative advantage , broadcast to all tokens of | empirical group mean (Monte Carlo) | reward model or verifier | explicit term in the loss (k3 estimator) | 3 with RM, 2 with verifier |
| RLVR | inherited from whatever optimizer it composes with (typically GRPO) | optimizer’s | deterministic verifier | optimizer’s | drops the RM entirely |
Read this table in two passes. First pass: every row computes a per-token weight and a leash, exactly as section 2 set up. Second pass: the columns reveal the design space. The advantage column tells you what makes each method different at the gradient. The baseline column tells you what reference each method standardizes against. The reward column tells you what scalar signal drives learning. The KL column tells you how each method enforces “stay near the reference.”
The gradient at the token level
The unifying formula for the gradient on a single token, with clipping in its active region and the leash term included, is
where is the per-token weight from the relevant column of the table and is the leash. The first term is the reweighted cross-entropy gradient: identical in shape to SFT’s gradient, but with a signed scalar weight that can be positive (push this token up) or negative (push it down). The second term holds the policy near . The clipped surrogate (PPO, GRPO) modifies the first term by gating it on or off based on the importance ratio , but where the gradient is active, it has exactly this form.
Three observations sharpen the comparison.
-
RL fine-tuning can push token probabilities down. SFT cannot: it only sees positive examples (gold tokens) and only ever increases their probability. RL fine-tuning sees the model’s own samples and assigns negative advantage to bad ones, decreasing those token probabilities. This is the structural reason RL teaches quality in a way SFT cannot, the motivation that opened the chapter.
-
DPO is RL in disguise. Its loss does not have a clipped surrogate or an explicit advantage, but the gradient of the DPO loss (derived in section 6) has the same shape: a positive push on tokens of chosen completions, a negative push on tokens of rejected completions, with the implicit reward playing the role plays in PPO. The “RL” label is about how the data is collected (offline pairs versus on-policy rollouts), not about what is happening at the gradient.
-
RLVR’s contribution is in one column only. It changes the reward source from a learned model to a deterministic verifier; it does nothing to the advantage estimator, the baseline, or the KL placement. That is why “RLVR” composes with PPO or GRPO and is best thought of as a reward-design move rather than a new algorithm.
Practical issues
Reward hacking
Goodhart’s law: when a measure becomes a target, it ceases to be a good measure. Reward models are imperfect measures of human preference; the moment you optimize against them as targets, they stop measuring well. The policy finds responses that score high on the reward model but are actually bad. The most common manifestations are length hacking, longer responses scoring higher on the RM, so the policy produces verbose outputs, sycophancy, agreeing with the user’s premise even when wrong, because raters prefer agreement on average, repetition, reward-positive phrases hammered until the RM saturates, and out-of-distribution responses that confuse the RM into high scores. The standard mitigations are the KL constraint (section 5’s main defense), length normalization (SimPO’s contribution), iterative RM training on freshly collected preferences as the policy drifts, and diverse preference data to limit the RM’s blind spots. Verifier-based rewards (RLVR) sidestep most learned-RM hacking but introduce their own variants: gaming the answer-extraction step, exploiting weak test suites, or producing correct final answers from incoherent reasoning the verifier cannot inspect.
Length bias
Length is the single most studied reward-hacking failure mode. The mechanism is straightforward: humans rate longer responses as more thorough or more confident on average, even when the underlying content is identical. The reward model learns this preference; the policy optimized against the RM amplifies it; the deployed model is verbose. Direct measurements show RLHF and DPO models producing responses two to three times longer than their pre-RLHF counterparts on equivalent queries, with much of the extra length being filler. SimPO’s length normalization addresses this by dividing the implicit-reward log-probability by sequence length, removing the length-scaling advantage that long responses get under both DPO and PPO. Other approaches add an explicit length penalty to the reward, train the RM with length-balanced preference pairs, or evaluate on length-controlled benchmarks.
Mode collapse
The dual failure mode of reward hacking is mode collapse: the policy converges to a narrow distribution of high-reward responses and loses diversity. Mode collapse is bad for creative tasks, writing, brainstorming, anything benefiting from variety, and for robustness, a narrow policy is easier to perturb adversarially. Mitigations are similar to the reward-hacking ones: a stronger KL constraint, fewer total training steps, a lower learning rate, occasionally an explicit entropy bonus in the objective. The trade-off with mode collapse is direct: pushing the KL constraint harder gives back diversity at the cost of less preference-following.
GRPO-specific failure modes
GRPO has two failure modes PPO does not, both arising from the group baseline. Dead groups occur when every completion in the group gets the same reward, all correct or all wrong, which forces and collapses the standardized advantage to zero. The group then contributes no gradient, which wastes compute. This is most common on prompts the model already solves reliably (advancing too fast for the current curriculum) and prompts it cannot touch at all (too hard). The standard mitigation is dynamic sampling: detect zero-std groups and resample more prompts to keep batches informative (DAPO’s contribution).
GRPO also has two standardization biases baked into the standard objective. Dividing by completion length penalizes per-token loss on long wrong completions less than on short wrong ones, biasing training toward verbosity. Dividing by the group standard deviation up-weights gradients on prompts where the group happens to have low variance, biasing training away from the prompts where the model is genuinely uncertain. Dr. GRPO removes both: sum per-token losses without the divide, and use the un-standardized as the advantage. The corrected version is what most 2025 reasoning-model recipes use.
Entropy collapse affects every RL method but is sharpest in GRPO trained with verifiable rewards. As the model gets better at producing correct answers, the policy concentrates probability mass on the patterns that work, lowering entropy until the policy is effectively deterministic and learning stalls. Standard mitigations are an explicit entropy bonus in the loss (PPO has this as ; GRPO recipes often add it), a tighter KL constraint, and sampling at higher temperature during rollouts to maintain exploration.
Exercises
Five exercises that build on the chapter’s machinery. Each is a self-contained problem with a starting template; hints are collapsed by default; try the problem first.
Exercise 1 (medium): Bradley-Terry reward model training
Implement reward model training using the Bradley-Terry loss. Train on synthetic preference pairs where the “ground truth” reward is a simple linear function. Verify that, after training, the model assigns higher reward to chosen responses than rejected.
Hint
The Bradley-Terry loss is:
For the synthetic setup:
- Generate tuples. Use a “ground truth” reward (e.g., is the dot product of with ).
- The “true” preference is determined by which response has higher .
- Train your reward model to predict preferences via BT loss.
- After ~100 steps of gradient descent, verify the trained correlates with the true .
Solution
The BT gradient is a logistic-regression gradient on the reward difference: push in the direction of , scaled by how wrong the model currently is (, near when uncertain, near once confident).
learning_rate = 0.1
for step in range(500):
reward_chosen = chosen @ w_phi
reward_rejected = rejected @ w_phi
loss = bt_loss(reward_chosen, reward_rejected)
grad_per_pair = -sigmoid(reward_rejected - reward_chosen)[:, None] * (chosen - rejected)
grad = grad_per_pair.mean(axis=0)
w_phi -= learning_rate * grad
if step % 100 == 0:
print(f"step {step}: loss={loss:.4f}, cos_sim(w_phi, w_true)={np.dot(w_phi, w_true) / (np.linalg.norm(w_phi) * np.linalg.norm(w_true)):.3f}")
r_c = chosen @ w_phi
r_r = rejected @ w_phi
accuracy = np.mean(r_c > r_r)
print(f"\nFinal accuracy: {accuracy*100:.1f}% of pairs have chosen > rejected reward.")
print(f"Final cosine similarity w_phi vs w_true: {np.dot(w_phi, w_true) / (np.linalg.norm(w_phi) * np.linalg.norm(w_true)):.3f}")With this seed, loss drops from to over 500 steps, cosine similarity between and climbs from to , and final pairwise accuracy is . never recovers the exact scale of (BT loss is scale-invariant in the reward; only the direction is identified), which is why cosine similarity, not raw distance, is the right check.
Exercise 2 (medium): DPO loss and gradients
Implement the DPO loss and verify by hand that its gradients push the chosen log-probability up and the rejected log-probability down, exactly what the loss landscape widget showed.
Hint
The DPO loss is:
Define implicit rewards and similarly for . Then:
Taking gradients with respect to the chosen and rejected log-probs (treating as constant):
- , this is negative, so gradient descent increases .
- , this is positive, so gradient descent decreases .
Implement these and verify numerically with finite differences.
Solution
Differentiate with (and similarly for ) using and the chain rule through :
def dpo_grad_analytical(logp_w, logp_l, logp_ref_w, logp_ref_l, beta=0.1):
r_w = beta * (logp_w - logp_ref_w)
r_l = beta * (logp_l - logp_ref_l)
s = sigmoid(r_w - r_l)
grad_w = -beta * (1 - s)
grad_l = beta * (1 - s)
return grad_w, grad_lFor the test point (, , both refs at , ): loss is , analytical gradients are and , and the finite-difference check matches to . The signs confirm the mechanism: ‘s gradient is negative (descent increases the chosen log-prob), ‘s gradient is positive (descent decreases the rejected log-prob): exactly the “push chosen up, rejected down” behavior from the loss landscape widget.
Exercise 3 (easy): RLVR math verifier
Implement a verifier for arithmetic word problems. Extract the final numeric answer from a model’s response and check it against the ground truth. This is the “reward function” used in RLVR for math tasks.
Hint
- Use a regex to extract numbers from the response.
- Take the last number, it’s typically the final answer (e.g., “Let me compute: 5 + 3 = 8. The answer is 8.”).
- Compare to the ground-truth answer with a small tolerance for floating-point.
- Return 1.0 if correct, 0.0 otherwise. This is the RL reward signal.
For bonus credit: handle cases where the response contains no number at all (return 0), cases where the response is correct but expressed differently (“eight” instead of “8”), or cases where the wrong number appears earlier in the response but the final answer is correct.
Solution
Grab every number in the response, take the last one (models state the final answer last), and check it against ground truth within tolerance:
def extract_final_answer(response):
nums = re.findall(r'-?\d+\.?\d*', response)
if not nums:
return None
return float(nums[-1])
def verify_math_answer(response, correct_answer, tolerance=1e-4):
ans = extract_final_answer(response)
if ans is None:
return 0.0
return 1.0 if abs(ans - correct_answer) < tolerance else 0.0Running this over the seven problems gives total reward ( correct): the two deliberately wrong responses (“100 minus 37 is 73” and “maybe 41”) score , and the other five, including the “sqrt(16)” and circle-area cases where earlier numbers appear in the response, score because the last number is the one that matters. This binary correct/incorrect signal, with no learned reward model in sight, is the entire RLVR reward function.
Exercise 4 (hard): Length bias detection
Real reward models often exhibit length bias: they give higher rewards to longer responses, regardless of quality. This is one of the most common failures in RLHF/DPO. Detect length bias in a synthetic preference dataset by measuring the correlation between response length and “chosen” status.
Hint
For each preference pair :
- Compute the length of and (e.g., character count or word count)
- Tally: how often is longer than ?
- If the answer is far from 50%, your data has length bias.
Detailed analysis:
- Compute the average length of chosen vs rejected responses
- Compute the Pearson correlation between and “chosen vs rejected” labels (always +1 if chosen is longer; -1 if rejected is longer)
- Report % of pairs where chosen is longer
Mitigations:
- Length-normalize the reward model
- Use SimPO (length-normalized DPO)
- Curate preference data to balance lengths
- Re-train with length-controlled subsets
Solution
Compare lengths within each pair and aggregate:
def length_bias_analysis(preference_pairs):
lens_w = np.array([len(c) for c, r in preference_pairs])
lens_l = np.array([len(r) for c, r in preference_pairs])
diff = lens_w - lens_l
return {
"pct_chosen_longer": np.mean(lens_w > lens_l) * 100,
"avg_len_chosen": lens_w.mean(),
"avg_len_rejected": lens_l.mean(),
"len_diff_mean": diff.mean(),
"len_diff_std": diff.std(),
}With this seed: no-bias data has chosen-longer at (statistical noise around , as expected when length carries no signal); mild bias () pushes it to ; strong bias () pushes it to , with average chosen length ( chars) far exceeding average rejected length ( chars). The reward model, or a human annotator, is being fooled by length regardless of actual quality: exactly the failure SimPO’s length normalization and Dr. GRPO’s std-divide removal are designed to fix.
Exercise 5 (medium): GRPO group-relative advantage
Implement the GRPO group-relative advantage and a single clipped GRPO step on a toy categorical policy. Verify (a) that the advantage flips sign correctly between above- and below-mean completions, (b) that a dead group (all rewards equal) produces zero advantage and zero gradient, and (c) that after one update the probabilities of “good” tokens have risen and “bad” tokens have fallen.
Hint
- Sample tokens from a toy softmax policy over a small vocab. Treat each sampled token as a single-token “completion” to keep the bookkeeping simple.
- Score each completion: if the sampled token is in a “good” set, otherwise.
- Compute the group-relative advantage . Handle the case explicitly (set every to zero).
- Compute the per-completion gradient: at the importance ratio is and the clip is inactive, so this is just . For a softmax over tokens, at logit position is .
- Sum across the samples, apply a small ascent step, and check the new probabilities moved in the expected directions.
- As a sanity check, construct a dead group where all sampled tokens get the same reward and verify both the advantage and the gradient are zero.
For bonus credit: run two epochs (so on the second pass) and observe the clip kick in on tokens whose probability moved past of the snapshot.
Solution
Standardize rewards within the group (guarding std ), then use the softmax score function per sample, weighted by that sample’s advantage:
std = rewards.std()
if std == 0:
advantages = np.zeros(G)
else:
advantages = (rewards - rewards.mean()) / std
grad_theta = np.zeros(V)
for i in range(G):
one_hot = np.zeros(V); one_hot[samples[i]] = 1.0
grad_theta += advantages[i] * (one_hot - pi_old)
theta_new = theta_old + 0.5 * grad_theta
pi_new = softmax(theta_new)With np.random.seed(1), the sampled group is [2 4 0 1 0 0 1 2] with rewards [0 0 1 1 1 1 1 0]: a genuine mix of good and bad tokens (the exercise’s original seed(0) happens to draw a group with zero good tokens, a dead group that can’t demonstrate the sign flip, so use seed(1) instead). Advantages come out to for the three bad-token samples and for the five good-token samples, correctly signed. After one ascent step, probability mass on {0, 1} rises from to and mass on the bad tokens falls correspondingly. The dead-group check ([5, 5, 5, 5, 5, 5, 5, 5]) gives std = 0.0, so advantages is all zeros and grad_theta is exactly zero: no gradient signal when every completion is equally (un)rewarded.
The modern post-training stack
The current production recipe for a chat model, as of late to early , follows a clean six-stage pipeline. Stage one: a base model (Chapters 7–10), pre-trained on web text and code at scale. Stage two: SFT (Chapter 13) on a curated instruction-response dataset, to teach the model the format of being a helpful assistant. Stage three: preference optimization (this chapter), typically DPO in the open-source world or PPO at frontier labs, occasionally iterative DPO with online preference collection. Stage four (optional): RLVR with GRPO for reasoning-focused models, large-scale RL on math and code with verifiable rewards (group-relative advantage from a sampled group of completions; a deterministic verifier as the reward signal), layered on top of stage three. Stage five (optional): PEFT (Chapter 15) for cheap domain-specific behavior on top of an already-aligned model. Stage six (optional): distillation (Chapter 16) to compress the fully-trained model into a smaller form factor for deployment.
Most production chat models stop at stage three. They have an SFT base and a preference-optimization pass, and they ship. Frontier reasoning models, o1, DeepSeek-R1, Gemini Thinking, add stage four, and the resulting capability jump on math and code benchmarks has been a major story of the last twelve months. Open-source models in the B class through followed the SFT + DPO template that Zephyr-7B established (Tunstall et al. 2023), producing ChatGPT--class capability at a fraction of the cost. Late saw a clear shift toward SFT + DPO + light RLVR in open-source releases, following DeepSeek-R1’s demonstration that RLVR is the path to frontier reasoning.
Preference optimization is the algorithmic centerpiece of post-training. SFT taught the model to respond; the methods in this chapter teach it to respond well. Classical RLHF (reward model plus PPO plus KL constraint) was the founding recipe; DPO simplified both the math and the implementation by deriving a supervised loss equivalent to the RL objective; the DPO variants explored the design space along several axes; RLVR opened a new frontier by extending RL to verifiable tasks where reasoning capability emerges from reward maximization. What remains in Part V: Chapter 15 covers PEFT, LoRA, adapters, and other parameter-efficient methods that make full fine-tuning unnecessary for most teams. Chapter 16 covers distillation, compressing a fully-trained model into a smaller one. Most production teams do not reach Chapters 15 and 16; they are optimizations that build on the foundations from Chapters 13 and 14. Together, Part V gives you the full post-training toolkit, from “I have a pre-trained model” to “I have a useful, aligned, possibly reasoning chatbot.”