Chapter 18: Post-training
A pretrained language model continues text; it does not yet answer questions in a reliable format, prefer good answers to merely plausible ones, or fit in a small deployment. This optional chapter follows the TinyStories capstone and adapts its small causal language model with four kinds of signal: supervised demonstrations, pairwise preferences, verifiable rewards, and a teacher’s distribution. Together they are the recipe of the post-trained capstone.
Concepts from other sections (13)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- verifiable reward: a reward computed by a deterministic checker rather than a learned model. Read more: 18.3 Group relative policy optimization with verifiable rewards
- DPO: minimizing over preference pairs, equivalent to fitting the KL-regularized reward objective. Read more: 18.2 Direct preference optimization and IPO
- PPO: a policy gradient method that clips the new-to-old probability ratio to keep updates small. Read more: 4.4 World models, multiple agents, and RLHF
- GRPO: a PPO-style RL method that replaces the critic with the group-normalized reward of several samples of the same prompt. Read more: 19.7 The training step and validation
- IPO: regressing the margin to the target with a squared loss. Read more: 18.2 Direct preference optimization and IPO
- clipped surrogate: the minimum of and , which removes the incentive to move a sample’s probability beyond the clip range. Read more: 18.3 Group relative policy optimization with verifiable rewards
- reverse KL: , weighted by the student; mode-seeking. Read more: 18.4 Distillation with forward and reverse KL
- GAE: an exponentially weighted combination of temporal-difference residuals. Read more: 18.6 PPO with a critic and generalized advantage estimation
- Policy lag: the gap between the learner and the policy that produced a rollout. Read more: 18.5 On-policy, off-policy, offline, and online learning
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- KL divergence: , the excess cost of predicting with ; nonnegative and asymmetric. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- Temperature scaling: dividing logits by one fitted scalar to fix over- or underconfidence without changing the argmax. Read more: 13.8 Logistic regression (IRLS), ROC-AUC, calibration (ECE) in Mathematics
- Reward exploitation: improving a proxy score by behavior that misses the intended outcome. Read more: 18.7 Rewards, preference models, and independent evaluation
The methods are choices matched to the available learning signal. SFT learns from demonstrations; DPO learns from fixed preferences; PPO and GRPO learn from sampled outcomes; distillation transfers teacher behavior. For a conceptual reading order, start with policy and data distributions, then PPO and GAE and reward design, before the GRPO implementation. Follow the rollout and distillation engineering parts when moving beyond the scalar examples.
Learning objectives
- Render chat data with the serving template, mask the loss to assistant tokens, and pack examples with document boundaries.
- Derive DPO from the KL-regularized reward objective and implement DPO and IPO stably.
- Implement GRPO’s group-normalized advantages and clipped surrogate objective.
- Implement forward and reverse KL distillation with temperature, and choose a direction.
- Derive PPO’s critic targets and GAE, distinguish behavior from reference policies, and diagnose rollout/learner mismatch.
- Audit rewards, normalization, policy lag, and distillation data before scaling the system.
Before you begin
Section titled “Before you begin”You should have a trained model from the TinyStories capstone, and be comfortable with the cross-entropy of Losses with a fused backward, the adapters of LoRA: low-rank adaptation with PiSSA initialization and merging, and the sampling of Sampling and logit processors. From Mathematics, the chapter uses the KL divergence of Entropy, cross-entropy, KL, JS, and the k3 estimator.
In this chapter
Section titled “In this chapter”-
Supervised fine-tuning: chat rendering, assistant-only loss, packing: rendering conversations with the serving tokenizer’s template, training only on assistant tokens, and packing short examples into rows without letting them attend to each other.
-
Direct preference optimization and IPO: deriving a classification-style preference loss from the KL-regularized RLHF objective, and IPO’s bounded alternative.
-
Group relative policy optimization with verifiable rewards: turning checker scores on sampled rollouts into a clipped policy-gradient update with a per-prompt baseline.
-
Distillation with forward and reverse KL: matching a teacher’s distribution in log space, the mass-covering and mode-seeking directions, and temperature scaling.
-
On-policy, off-policy, offline, and online learning: behavior versus target distributions, importance sampling, support, and replay limits.
-
PPO with a critic and GAE: token rewards, terminal/truncation masks, actor/critic losses, and drift controls.
-
Rewards and preference models: comparison losses, verifiable outcomes, reward exploitation, and independent evaluation.
-
Rollout systems and GRPO variants: actor/learner layouts, revision lag, token normalization, and useful learning throughput.
-
Distillation data and deployment: sequence/logit/feature targets, student prefixes, vocabulary constraints, and compression validation.
-
SFT recipes and data strategies: completion/assistant/final-turn masking, mixtures, synthetic and rejection-sampled demonstrations, weighting, and parameter updates.
-
Preference optimization variants: IPO, label-smoothed/robust DPO, KTO, ORPO, SimPO, online data, and the wider weighting/reference/loss families.
Further reading
Section titled “Further reading”- The Training and Post-Training and Reinforcement Learning chapters of AI Engineering.