Module 8. RLHF and direct preference optimization
Material version: 2026.20
Factual snapshot: 2026-08-05
Language: EN
Primary practice: level A — a reproducible CPU workflow; level B — an optional open-model run with Low-Rank Adaptation (LoRA) on a suitable accelerator
Structure: 5 lessons, 25 steps
Estimated time: 9–11 hours excluding optional model training
Module 7 “Reward models and preference data” (rl_llm.module_07_reward_models) took us from pairwise labels to reward models. The next problem is operational: how do we change a policy so that preferred responses become more likely without discarding useful behavior from the starting model or learning to exploit defects in the feedback signal?
The classical answer is reinforcement learning from human feedback (RLHF): train a reward model, sample fresh responses, and optimize expected reward while limiting deviation from a reference policy. A more direct answer is direct preference optimization (DPO): convert fixed preference pairs into a policy loss without fitting an explicit reward model or collecting new rollouts inside that stage.
The practical question of the module is:
How should we choose between RLHF and direct preference optimization once data, compute, hidden assumptions, and observable failure modes are made explicit?
After completing the module, you will be able to:
- write the RLHF objective regularized by Kullback–Leibler (KL) divergence and explain the roles of policy, reference, reward model, and critic;
- separate four logical roles from four mandatory full LLM copies;
- derive the exact exponentially tilted optimum;
- derive DPO from that optimum and the Bradley–Terry model;
- implement the DPO loss, gradient weight, and label smoothing;
- explain why a growing relative margin does not guarantee a growing chosen-response probability;
- audit lengths, masks, chat templates, stop reasons, and cached reference log-probabilities;
- distinguish DPO, IPO, SimPO, KTO, and ORPO by their data contracts;
- choose among fixed pairs, iterative preference collection, and online RL;
- run a synthetic CPU experiment and prepare a DPO smoke test on an open 0.6B-class model.
The English practical route is the executable practice; dated claims, primary sources, and inference limits are recorded in Module_8_Sources.md.
Practice route. Graded tasks, worked solutions revealed after an attempt, and executable practice follow the lecture. The explanation remains self-contained.
Lesson 8.1. From preferences to a KL-regularized policy
Lesson 8.1. From preferences to a KL-regularized policy
- What classical RLHF optimizes
- Four logical roles, not four mandatory copies
- Turning sequence KL into token rewards
- Exact optimum: exponential tilting of the reference
- A numerical reward–deviation frontier
then 5 assessment steps
Step 8.1.1 — What classical RLHF optimizes
Let be a prompt, a full completion, the trainable policy, a reward model, and a frozen reference policy. A standard objective is
with pricing departure from the reference.
The reward term moves mass toward responses favored by the reward model. The KL term charges for redistribution relative to the reference. With a fixed reward, a larger makes movement more expensive; a smaller value allows the reward to dominate more strongly.
A related constrained form is
In an ideal convex problem, the penalty is connected to the constraint through a Lagrange multiplier. In finite neural training, the selected does not predetermine the realized KL because parameterization, optimization noise, and training duration matter.
Boundary of the result. KL controls departure in one geometry. It does not validate the reward model, prevent every harmful change, or preserve every capability.
Step 8.1.2 — Four logical roles, not four mandatory copies
A Proximal Policy Optimization (PPO)-like RLHF loop usually separates:
- a trainable policy ;
- a frozen reference policy ;
- a reward model ;
- a critic used to estimate advantages.

These are mathematical and computational roles. An implementation need not retain four independent full-size parameter sets. It may share a policy/value backbone, use adapters, quantize frozen components, cache reference log-probabilities, offload models to CPU, or separate generation and optimization across processes.
The precise statement is:
RLHF contains several logical models and computational roles; physical placement and parameter sharing are implementation choices.
Fresh generation, reward and reference forward passes, value estimation, rollout storage, and policy backpropagation remain costly even after memory engineering. DPO shortens this loop, but it does not eliminate the cost of obtaining reliable preference data.
Step 8.1.3 — Turning sequence KL into token rewards
Autoregressive factorization gives
A token-level RL implementation can therefore add
to every active response token and attach the terminal score to the final active token. The sum is exactly
A sampled log-ratio can be negative. It is not a negative KL divergence: its expectation under the current policy equals forward KL. Module 6 “The LLM as a policy: tokens, log-probabilities, and KL” (rl_llm.module_06_llm_policy) develops this distinction in detail.
The response mask and stop reason are part of the objective. Prompt tokens, padding, and environment observations must not accidentally receive policy reward.
Step 8.1.4 — Exact optimum: exponential tilting of the reference
Fix one prompt and a finite response space. Optimize the full distribution:
Introducing a multiplier for normalization and differentiating gives
where

Reward reweights mass already available under the reference. If , then for finite reward. Adding a constant to every reward leaves the optimum unchanged because the common factor cancels in .
As , the optimum approaches the reference. As , it concentrates on reward maxima inside reference support.
Mathematical extension: Information Theory for ML, Module 8 “Maximum entropy, exponential models, and KL regularization” (
it_ml.module_08_maxent_kl_policy) develops variational forms, partition functions, and maximum-entropy connections.
Step 8.1.5 — A numerical reward–deviation frontier
Take
The exact solution is:
| 4.0 | 0.299 | 0.009 | |
| 2.0 | 0.376 | 0.038 | |
| 1.0 | 0.529 | 0.153 | |
| 0.5 | 0.776 | 0.514 | |
| 0.25 | 0.966 | 1.033 |

The values are reproduced by reward_kl_frontier in module_8_reference.py. Lower raises reward and KL for this exact optimum. It does not imply monotonic behavior for every neural training step.
A useful report therefore presents a frontier, not one proxy-reward number. Reward without deviation and deviation without independent quality are both incomplete.
Constraints: utility and cost are different signals
KL keeps a policy near its reference, but it does not itself encode a safety requirement. Let denote response utility and let denote separate costs, such as the probability of a prohibited action, an access-policy violation, or a dangerous side effect. A natural constrained objective is
Its Lagrangian is
The policy maximizes under the current penalties, while a dual variable rises when its constraint is violated:
A reward model and a cost model are therefore different statistical components. High utility should not automatically compensate for a critical violation. A fixed sum is a scalarization with a preselected trade-off; an explicit constraint states an admissible cost budget and allows the multiplier to adapt to the observed violation.
Consider a two-action example. The safe action has reward and cost , while the risky action has reward and cost . If the policy selects the risky action with probability , then
Under the budget , the optimum is
The functions two_action_constrained_optimum and dual_ascent_step reproduce the calculation.
Where the guarantee ends. An average constraint does not guarantee safety on every prompt. Critical actions require additional mechanisms such as hard filters, safe action sets, refusal or abort paths, and scenario-level checks. A learned cost model can also be wrong and can itself be optimized against. Training-time constraints and the release controls in Module 13 “Evaluating RL systems for LLMs: statistics, reward hacking, and safety” (rl_llm.module_13_evaluation) therefore complement rather than replace one another.
Constrained Policy Optimization (CPO) is a classical policy-optimization line for expected-cost constraints. Safe RLHF adapts the reward-versus-cost separation to LLM alignment. These sources provide mathematical and empirical grounding; neither turns an average constraint into an absolute safety guarantee.
Lesson 8.2. DPO: derivation, loss, and gradient
Lesson 8.2. DPO: derivation, loss, and gradient
- Preference pairs as probabilistic observations
- Implicit reward and cancellation of the normalizer
- The DPO loss
- Gradient weight and the role of β
- DPO assumptions and the boundary with classical offline RL
then 3 assessment steps
Step 8.2.1 — Preference pairs as probabilistic observations
For one prompt, let be chosen and rejected. The Bradley–Terry (BT) model writes
Module 7 “Reward models and preference data” (rl_llm.module_07_reward_models) used this likelihood to fit an explicit reward model. DPO instead expresses reward through a policy and substitutes that expression directly into the preference likelihood.
The label remains conditional on a prompt, two candidates, a rubric, and a protocol. It does not turn the chosen response into globally correct ground truth or identify an absolute reward scale across prompts.
A sound dataset preserves prompt identity, both full completions, candidate provenance, rubric version, tie/abstention policy, symmetric preprocessing, and prompt-level train/evaluation splits.
Step 8.2.2 — Implicit reward and cancellation of the normalizer
The exact optimum implies
For two responses to one prompt, the prompt-dependent cancels:
Replacing the unknown optimum with a trainable policy produces a preference logit. No explicit reward-model training is required during this stage: the policy-to-reference log-ratio acts as an implicit reward.

The cancellation is exact inside the model. It does not prove that human decisions are generated by one scalar reward and logistic noise.
Step 8.2.3 — The DPO loss
Define the sequence margin
Then and
The policy is asked to improve the chosen response relative to the reference more than it improves the rejected response. DPO does not independently maximize raw chosen log-probability.
Sequence log-probabilities sum over response tokens. Prompt and padding tokens are masked. Both completions must use the same tokenizer, chat template, and truncation rule.
Step 8.2.4 — Gradient weight and the role of β
The derivative with respect to the margin is
so the positive pair-weight magnitude is

Negative-margin pairs receive strong pressure. At a large positive margin the weight decays. Because both scales the logit and multiplies the derivative, “larger always means a stronger update” is not a valid statement without the current margin, learning rate, and exact implementation.
In the variational objective, prices KL. In the DPO code path it scales a classifier logit. The derivation connects those roles, but realized KL still depends on data, optimization, and training duration.
Step 8.2.5 — DPO assumptions and the boundary with classical offline RL
DPO is easiest to interpret when:
- both completions share one prompt;
- BT is a reasonable preference model;
- the reference is fixed and evaluated with the same template;
- both completions lie in reference support;
- labels represent the intended criterion;
- the fixed data cover future usage reasonably well.
DPO uses no explicit Q-function, Bellman target, or bootstrapping. Calling it “exactly classical offline RL” is therefore too strong. A better statement is:
DPO is direct policy optimization on fixed preference pairs, derived from a KL-regularized problem; it is related to offline learning but not identical to value-based offline RL.
If BT is misspecified, DPO remains a well-defined pairwise loss. Its interpretation changes from exact implicit-reward recovery to a discriminative approximation.
Lesson 8.3. Where the simple picture breaks
Lesson 8.3. Where the simple picture breaks
- Relative margin is not absolute probability
- An exact likelihood-displacement toy
- Length, masks, and chat templates
- Noisy pairs and conservative DPO (cDPO)
- Fixed pairs grow stale
then 4 assessment steps
Step 8.3.1 — Relative margin is not absolute probability
A growing does not imply that grows. The chosen probability may rise while the rejected falls, both may rise at different rates, or both may fall while the rejected falls faster.
The third regime is invisible if a dashboard contains only DPO loss and pair accuracy. Softmax normalization couples the observed pair to all other sequences; an unlabelled completion may absorb the probability mass.
At minimum, log chosen and rejected sequence probabilities, their separate reference ratios, margin, gradient weight, response lengths, KL, and independent generation quality.
Step 8.3.2 — An exact likelihood-displacement toy
Consider three completions with logits
The first is chosen, the second rejected, and the third absent from the pair. Their pairwise log-margin is
which grows strictly, while probabilities are:
| pair margin | ||||
|---|---|---|---|---|
| 0 | 0.3333 | 0.3333 | 0.3333 | 0.0 |
| 0.25 | 0.1962 | 0.1190 | 0.6848 | 0.5 |
| 0.5 | 0.0738 | 0.0271 | 0.8991 | 1.0 |
| 1.0 | 0.0067 | 0.0009 | 0.9924 | 2.0 |

The pair is increasingly well separated, yet the chosen probability falls. The toy does not claim that every DPO run deteriorates. It proves the narrower point that pair margin does not determine absolute chosen probability.
Step 8.3.3 — Length, masks, and chat templates
Sequence log-probability is a sum of negative token contributions, so length affects scale directly. If chosen responses are longer, length may become a label-correlated shortcut. Dividing by length is not a universal repair: it changes the objective and can introduce a short-response bias.
Audit chosen/rejected length distributions, symmetric truncation, truncation rate, EOS and stop reasons, response-only masks, identical chat templates, tokenizer/reference versions, and hidden winner metadata.
Cached reference log-probabilities become stale after changing tokenization, template, truncation, or checkpoint. Visually identical text is not necessarily the same action sequence.

Tokenizer revision, chat template, truncation rule, reference checkpoint, and completion mask are part of the pair's mathematical contract.
Engineering extension: Modern LLMs, Module 13 “Supervised Fine-Tuning and Data Work” (
modern_llms.module_11_sft) covers dialogue-data and chat-template engineering in more depth.
Step 8.3.4 — Noisy pairs and conservative DPO (cDPO)
Conservative DPO (cDPO) smooths the binary target. With a possible label-flip rate, for ,
At this is ordinary DPO. Positive smoothing avoids treating every observed label as perfectly certain. In this form, cDPO is a practical label-smoothing heuristic; it should not be conflated with Robust DPO, whose correction is derived under a specified symmetric label-flip model and retains that model's assumptions.
Neither variant repairs a systematically wrong rubric, position leakage, generator bias, or arbitrary distribution shift. Monitor held-out pair accuracy, chosen/rejected log-probabilities, margin distribution, KL, lengths, domain slices, and independent fresh-generation evaluations. Training loss can keep improving after useful quality plateaus.
Step 8.3.5 — Fixed pairs grow stale
DPO commonly trains on responses produced by an earlier policy. After updating the model, new responses may occupy a different region. The original pairs then cover the current policy less well.
Three regimes are useful to distinguish:
- one fixed dataset — easiest to reproduce, but never refreshed;
- iterative preference optimization — regenerate candidates and recollect pairs;
- online RL — score current-policy rollouts and update directly.
Iterative collection reduces policy–data mismatch, but requires versioning of generators, rubrics, judges, and rounds. It cannot repair a systematically wrong feedback source.
Store policy_version, reference_version, decoding settings, date, label source, and round identifier for every collection cycle.
Lesson 8.4. Variants of direct preference optimization
Lesson 8.4. Variants of direct preference optimization
- IPO: a finite target margin
- SimPO: mean log-probability and a target gap
- KTO: unpaired binary feedback
- ORPO: supervised fine-tuning and odds ratio in one objective
- Method map: data first, name second
then 3 assessment steps
Step 8.4.1 — IPO: a finite target margin
Identity Preference Optimization (IPO) retains a pairwise reference-corrected margin but gives it a finite target. In the convention used here,
Ordinary DPO keeps decreasing as a positive margin grows. IPO penalizes both undershooting and overshooting its target. This is useful when unbounded margin growth is not the desired behavior.
A finite target does not repair bad pairs, stale templates, or weak coverage. Its is not numerically comparable to DPO's without mapping the exact objectives.
Step 8.4.2 — SimPO: mean log-probability and a target gap
Simple Preference Optimization (SimPO) uses a reference-free response score
and forms a pair logit with a target margin :
It avoids a separate reference forward pass and explicitly normalizes by length. The trade-off is a different mathematical contract: no reference anchor remains, and mean log-probability has its own biases.
SimPO and DPO should be compared under matched data, compute, and independent evaluation. Length normalization is not a proof that length effects are solved.
Step 8.4.3 — KTO: unpaired binary feedback
Kahneman–Tversky Optimization (KTO) targets datasets where an individual completion is marked desirable or undesirable, without an explicit paired completion for the same prompt.
Its appeal is a different data contract: collecting isolated binary judgments may be cheaper than forming balanced pairs. The key question is whether those labels have a consistent semantic meaning.
Do not manufacture DPO pairs by joining unrelated prompts. Audit class balance, label provenance, domain balance, and the baseline against which desirability is interpreted.
Step 8.4.4 — ORPO: supervised fine-tuning and odds ratio in one objective
Odds Ratio Preference Optimization (ORPO) combines a supervised fine-tuning (SFT) term on the chosen completion with a pairwise odds-ratio term:
No separate reference model is required during training. The SFT component supports chosen likelihood, while the pair component separates chosen from rejected.
ORPO is not literally DPO with one model removed; the loss, scale, and anchoring mechanism differ. Report the chosen-response negative log-likelihood (NLL) and the pairwise preference loss separately so one cannot hide degradation in the other.
Step 8.4.5 — Method map: data first, name second

Under the pinned smoke protocol, both DPO regimes reach pair-ranking accuracy 1.0. Moderate training retains exact greedy-generation matches on all eight templates, whereas the high-pressure regime retains only 0.125. Mean chosen-response log-probability changes from roughly under moderate training to under high pressure. This is a synthetic course result, not an external benchmark; its purpose is to show why pair accuracy and DPO loss are insufficient without absolute likelihoods and free generation.
python -B modules/module-08/test_module_8_reference.py
python -B modules/module-08/test_module_8_training.py
python -B modules/module-08/module_8_training.py
The graded practice provides tasks and worked solutions revealed after an attempt; the executable practice provides the connected computational route.
Step 8.5.3 — Practice B: DPO on Qwen3-0.6B
- a synthetic CPU path without model downloads;
- a real path using
Qwen/Qwen3-0.6B,trl-lib/ultrafeedback_binarized, TRLDPOTrainer, and LoRA.
RUN_REAL_MODEL=False by default, so the release route downloads nothing and needs no accelerator. The real branch is a current engineering template for a suitable GPU environment; memory, runtime, and stability depend on the runtime image, sequence length, batch size, numerical precision, and library versions. The course does not promise that every free T4 profile will fit the selected configuration.
The optional branch snapshot is pinned to TRL 1.9.2, Transformers 5.14.1, PEFT 0.20.0, Accelerate 1.14.0, and Datasets 5.0.1 because APIs are part of the experiment.
Release boundary: the Qwen3/TRL branch was not executed for this edition. The course therefore makes no measured claim about Qwen3 runtime, GPU memory, convergence, or trained-model quality. The NumPy mathematics and the complete autoregressive CPU route with a small GRU policy were executed.
Step 8.5.4 — What to measure after training
A minimal report includes DPO loss, pair accuracy, chosen and rejected log-probabilities, margin and gradient weight, KL under a consistent protocol, lengths and stop reasons, domain/generator slices, fresh-generation evaluation, and checks for retained base capabilities.
Use at least:
- held-out pairs from the same source;
- new prompts from the same domain;
- shifted prompts or responses from the updated policy.
High pair accuracy does not prove strong generation. The model may learn style shortcuts or improve relative ordering while chosen absolute likelihood falls.
Step 8.5.5 — Decision rule: DPO, RLHF, or online RL
A useful sequence is:
- Is the feedback reliable? If not, repair the rubric and data first.
- Are there fixed same-prompt pairs? DPO and its variants provide a strong baseline.
- Must data follow the current policy? Use iterative collection or online RL.
- Is reward programmatically verifiable? This naturally leads to reinforcement learning with verifiable rewards (RLVR), covered in Module 9 “RLVR and GRPO: verifiable rewards, group-relative estimates, and stability” (
rl_llm.module_09_rlvr_grpo). - Is open-ended reward and token-level credit assignment required? PPO or REINFORCE Leave-One-Out (RLOO) loops remain relevant despite their cost.
- Is a simple one-stage path required? Consider ORPO or SimPO with their own assumptions.
The final map is deliberately less dramatic than “DPO replaced RLHF”:
DPO replaces a particular expensive loop when fixed pairs and its assumptions fit the task. Online RL remains necessary when data must track the current policy or interaction and verifiable reward are part of the objective.
Module summary
- RLHF maximizes reward under a KL price for departing from a reference.
- Four RLHF roles do not imply four mandatory full model copies.
- The exact KL-regularized optimum is an exponential tilt of the reference.
- DPO follows by inserting implicit reward into Bradley–Terry and cancelling prompt normalization.
- DPO controls a relative margin; absolute likelihoods must be monitored separately.
- Length, masks, templates, truncation, and reference version belong to the mathematical contract.
- IPO, SimPO, KTO, and ORPO solve different problems under different data assumptions.
- Fixed pairs grow stale as the policy changes; iterative collection and online RL remain distinct tools.
Compact glossary
- RLHF — policy training from human feedback through a reward and RL loop.
- DPO — a direct pairwise policy loss relative to a reference.
- KL anchor — a penalty or constraint on policy departure from the reference.
- Implicit reward — up to a prompt-dependent offset.
- cDPO — DPO with label smoothing.
- IPO — a pairwise objective with a finite desired margin.
- SimPO — a reference-free pairwise objective based on mean sequence log-probability.
- KTO — an objective for unpaired desirable and undesirable examples.
- ORPO — an SFT term combined with a pairwise odds-ratio term.
- Likelihood displacement — a regime where pair margin grows while chosen probability may fall.
Sources and further-study route
- Ouyang et al. (2022), Training language models to follow instructions with human feedback — the canonical LLM sequence SFT → reward model → PPO; it does not prescribe one mandatory memory layout.
- Rafailov et al. (2023), Direct Preference Optimization — the core DPO derivation from a KL-regularized objective and a pairwise preference model.
- Azar et al. (2023), A General Theoretical Paradigm to Understand Learning from Human Preferences — analysis of pairwise objectives and the motivation for IPO.
- Meng, Xia, and Chen (2024), SimPO, Ethayarajh et al. (2024), KTO, and Hong, Lee, and Thorne (2024), ORPO — primary sources for alternative direct-optimization contracts.
- Chowdhury et al. (2024), Provably Robust DPO — a theoretical correction under a specified noise model, not a synonym for ordinary cDPO label smoothing.
- Razin et al. (2024), Unintentional Unalignment: Likelihood Displacement in DPO — motivation for tracking chosen and rejected absolute likelihoods separately.
- Current TRL DPOTrainer documentation — a dated implementation interface, not a mathematical guarantee.
The line-by-line registry of claims, limitations, and reproduced values is in Module_8_Sources.md. For the variational derivation, use Information Theory for ML, Module 8 “Maximum entropy, exponential models, and KL regularization” (it_ml.module_08_maxent_kl_policy); for SFT data engineering, use Modern LLMs, Module 13 “Supervised Fine-Tuning and Data Work” (modern_llms.module_11_sft); for the broader post-training map, use Modern LLMs, Module 14 “Learning from Feedback: RLHF, DPO, RLOO, and GRPO” (modern_llms.module_12_preference_optimization).