Chapter 16
Post-Training and Alignment
On this page
Post-training changes a model's behavior using demonstrations, preferences, and interaction. Its gains depend on the starting model, exploration, data, and objective; it is not limited to formatting, nor does a higher reward establish general reasoning improvement.
Start with a behavior specification¶
Post-training should begin with observable behavior, not an algorithm acronym. Define how the model should follow instructions, use tools, express uncertainty, handle conflicting authority, refuse unsafe requests, and preserve capability across languages and domains. Convert those expectations into demonstrations, comparisons, executable checks, and protected evaluations.
Separate three kinds of change:
- capability acquisition, where the model learns a task or tool it could not perform reliably;
- policy selection, where training shifts probability among behaviors already available;
- interface conditioning, where output format, tone, or interaction protocol changes.
SFT, preference optimization, and reinforcement learning can affect all three, but they provide different evidence and failure modes. Naming the intended change makes regressions easier to interpret.
Supervised fine-tuning¶
SFT is appropriate when experts can demonstrate the desired behavior. It teaches format, policy, tool use, domain tone, and solution trajectories. Compare a small, adjudicated demonstration set with a larger, weakly labeled set at matched training budgets; dataset size alone does not establish the better supervision signal.
Mixing must protect broad capability. Include replay from general instruction data when specializing. Mask user or tool tokens according to the learning objective. For multi-turn data, test whether the loss incorrectly rewards copying system content or private tool outputs.
Preference data¶
Pairwise preferences can be collected from humans, verifiers, judges, or product behavior. Each source has bias. Human labels reflect rubric clarity and annotator context. Model judges may favor verbosity, familiar style, or their own outputs. Engagement data is confounded by ranking, position, and user intent.
Build calibration sets with expert adjudication. Measure agreement by slice. Retain ties and uncertainty rather than forcing every comparison into a binary label.
A preference record should preserve both candidates, generation policies, presentation order, rubric, annotator or judge version, confidence, and adjudication status. Randomize order to detect position bias. Control length or explicitly model it so that verbosity does not become an accidental reward dimension.
RLHF and direct preference objectives¶
Classic RLHF trains a reward model and optimizes a policy while constraining divergence from a reference. It supports online sampling and sequence-level reward, but it is operationally complex and vulnerable to reward hacking.
Direct Preference Optimization converts pairwise preferences into a supervised-style objective relative to a reference policy. It removes the explicit reward-model-and-RL loop, simplifying training. Other objectives alter the link function, treatment of a reference, margin, or unpaired feedback.
Do not choose by acronym. Ask:
- Is reward available only at the sequence level?
- Must the policy explore new outputs?
- How reliable and stationary is the preference signal?
- Is an explicit reward model useful for analysis or reuse?
- How much divergence from the base model is safe?
The KL constraint or reference policy is not merely a mathematical convenience. It discourages large changes into regions where the reward model has little evidence, without guaranteeing safety there. Monitor divergence by task slice and response length; a single aggregate can hide a policy that changes radically on rare but important prompts.
Derive the DPO training signal¶
For one prompt, let y+ be the preferred answer and y- the rejected answer. Compute each answer's log probability as the sum over its scored response tokens, conditioned on the same prompt. Let pi be the policy being trained and pi_ref the frozen reference. Define:
The margin compares the policy's preference with the reference's preference for the same pair.
Beta sets the scale of the relative log-probability margin; sigma is the logistic function.
This is the core objective from Direct Preference Optimization. The prompt is omitted from the notation but remains part of every conditional probability. The reference contributes fixed log probabilities and receives no gradient. The loss is a binary preference objective, not next-token imitation of the preferred answer alone.
Suppose the current model's chosen/rejected log probabilities are -2 and -4, while the reference's are -3 and -4. The relative margin is one. With beta=0.5, the loss is about 0.474; if the policy matches the reference's relative preference, the loss is log(2), about 0.693. Moving probability toward the chosen answer relative to the rejected one lowers the loss. The scalar reference in examples/post_training.py tests this direction and numerical stability.
The reference is not the policy that generated the pair unless the experiment chose it that way. Keep those identities separate. Standard sequence log probabilities sum over tokens; replacing the sum with a length average changes the objective. A model can also improve the pairwise margin while lowering both candidates' absolute probabilities. Monitor held-out generation behavior and likelihood, not only training pair accuracy.
Preference labels may be wrong or weakly ordered. Ties, contradictory rubrics, long-versus-short confounds, and judge familiarity can all drive a clean mathematical objective toward undesirable behavior. DPO removes an explicit online rollout stage during each training update; it does not remove the need for representative candidate generation, evaluation, or a reliable preference dataset.
Verifiable reward and reasoning¶
Code execution, theorem checks, database answers, and simulated environments provide strong outcome signals. They still permit shortcut exploitation. Split generators and verifiers, randomize hidden tests, audit suspiciously short or repetitive traces, and evaluate on tasks unavailable to the training loop.
Process supervision labels intermediate steps and can improve diagnosis, but step correctness may be ambiguous and expensive to annotate. Outcome supervision is cheaper when final answers are verifiable but provides sparse credit. Hybrid designs use outcome reward plus auxiliary signals for format, tool validity, or progress.
Online learning and rollback¶
Online post-training creates feedback loops. New policy outputs influence the data later used to judge or train it. Protect a stable holdout, log exposure probabilities, version policies and reward models, and preserve the ability to reconstruct which policy generated each sample.
Use canaries and conservative trust regions. A rollback should restore model, tokenizer, tool policy, prompt templates, safety configuration, and serving parameters as one versioned release.
Build a post-training evaluation ladder¶
Extend the recipe's experiment ladder with checks specific to post-training: label consistency and judge bias, fixed-pair loss sanity checks, SFT-only and base-model controls, and changes in response length, refusal, and tool behavior. Retain broad-capability and adversarial evaluations; a blended preference win rate can conceal regressions.
Design Exercises¶
- When does DPO simplify a problem that does not require online RL?
- Design defenses against reward hacking for a code reasoning model.
- How do you audit a model judge for verbosity bias?
- What belongs in an atomic rollback for an aligned assistant?
- Explain why preference optimization can reduce capability even as reward rises.
Worked answer criteria¶
- DPO is attractive when fixed, trustworthy comparisons cover the desired change and online exploration is not essential. Compare it with SFT and an unchanged-policy baseline.
- Run generated code in an isolated environment with protected tests and bounded resources; distinguish wrong answers from infrastructure errors and test for attempts to tamper with the checker.
- Randomize candidate order, stratify by length, use expert-adjudicated ties, and compare judges against a held-out rubric. Do not silently equate verbosity with correctness.
- Roll back the compatibility set: weights, tokenizer, template, tool policy, serving/sampling configuration, and relevant reward/evaluation versions.
- Reward is a proxy. Distribution shift, biased labels, overoptimization, and loss of rare capabilities can all coexist with a rising proxy score.