"Nobody could tell me what good behavior was, and nobody could show me either. So I stopped asking. I just put two of my own attempts side by side and watched which one the human reached for. A thousand quiet choices later, I knew their taste better than they could have spelled it out. I am the score they never wrote down."
A Reward Model Learning What Humans Like by Watching Them Choose
Imitation learning (Sections 27.3 and 27.4) needed an expert who could demonstrate the right action. But for many of the behaviors we most want to instill, "be helpful", "summarize faithfully", "drive comfortably", nobody can demonstrate the optimum and nobody can write down a reward function for it either. What humans can do, reliably and cheaply, is compare: shown two behaviors, they can say which is better, even when they could not have produced the better one themselves or scored it on an absolute scale. Preference-based reinforcement learning turns that comparative judgment into a learning signal. The recipe has three moves. First, collect pairwise comparisons of trajectories or outputs. Second, fit a reward model that explains those comparisons through the Bradley-Terry likelihood, a learned scalar score whose differences reproduce the observed choices. Third, optimize a policy against that learned reward with a reinforcement-learning algorithm (PPO), tethered by a KL penalty to a reference policy so it does not wander into degenerate high-reward nonsense. This three-stage pipeline, reinforcement learning from human feedback (RLHF), is exactly how modern language assistants are aligned: a sequential decision process over tokens, scored by a model of human taste. We then meet the 2024 to 2026 simplification, direct preference optimization (DPO), which folds all three stages into one supervised loss and skips the explicit reward model and the RL loop entirely, and RLAIF, which replaces the human judge with an AI one. You leave able to train a Bradley-Terry reward model from scratch, use it to drive a tiny policy update, and call the same pipeline in a few lines of trl.
In Section 27.4 we closed the imitation-learning arc with inverse reinforcement learning: recover a reward function from expert demonstrations, then plan against it. That program assumes the expert can act well enough to demonstrate. This section relaxes that assumption to its breaking point. Suppose no one can reliably demonstrate the behavior you want, and no one can write its reward function either, but anyone can look at two attempts and point to the better one. This is the common case for the most valuable and least formalizable objectives, and it is precisely the regime where preference-based RL lives. The shift in supervision is the whole story: from "do this" (demonstrations) to "this is better than that" (comparisons), a strictly weaker and strictly cheaper signal that turns out to be enough.
The reason comparisons are enough connects directly to a thread we opened in Part V. The human-in-the-loop adaptive systems of Chapter 21 already treated human feedback as a first-class signal a system queries and adapts to over time; there the feedback corrected a forecast or a decision online. Here we make that feedback the source of the reward itself, learning the objective rather than just the policy. And the payoff is enormous in scope: the same machinery that aligns a robot's gait to "looks natural" aligns a large language model to "is a good assistant". We use the unified notation of Appendix A throughout: $\pi_\theta$ the policy, $r_\phi$ the learned reward model, $\pi_{\text{ref}}$ the reference policy, and $(y_w, y_l)$ a preferred-versus-rejected pair.
The four competencies this section installs are: to recognize when comparison is the right supervision and frame preference learning as reward modeling; to write and fit the Bradley-Terry preference likelihood that turns pairwise choices into a scalar reward; to state the RLHF objective, a learned reward minus a KL leash to a reference, and explain why every term is there; and to read the 2024 to 2026 shift to DPO and RLAIF, knowing what each stage buys and what dropping it costs. These are the load-bearing skills for the sequence-as-decision view of Chapter 28, where the policy we align here becomes a sequence model conditioned on outcomes.
1. When Comparison Beats Demonstration Beginner
Every method so far in this chapter has needed an expert who could produce good behavior. Behavioral cloning needed state-action pairs from a competent demonstrator; DAgger needed that demonstrator on call to label the states the learner visited; inverse RL needed demonstrations clean enough that "the expert was near-optimal" was a defensible assumption. The quiet precondition under all of them is that someone, somewhere, can act well. Relax that and the methods have nothing to learn from.
Yet many of the behaviors we most want are exactly the ones nobody can demonstrate to optimality. Consider summarizing a document "faithfully and concisely", or writing a "helpful" answer, or controlling a prosthetic limb so the motion "feels natural" to its wearer. A person cannot reliably produce the best summary on demand, and certainly cannot assign it a calibrated numerical score, "this summary is a 7.3 out of 10" is a meaningless request. But ask that same person to look at two summaries and say which is better, and they answer instantly and consistently. Comparison is psychologically cheap and reliable precisely where absolute scoring and demonstration are expensive and noisy. Humans are excellent relative judges and poor absolute ones.
Preference-based RL is built on that asymmetry. We never ask a human to act and never ask for a number; we only ask them to choose. Formally, we collect a dataset of comparisons $\mathcal{D} = \{(\sigma_w^{(i)}, \sigma_l^{(i)})\}$, where each pair is two behavior segments (two trajectories, two summaries, two model responses) and $\sigma_w$ is the one the human preferred (won) over $\sigma_l$ (lost). From this dataset of choices, with no rewards and no demonstrations anywhere in it, we will recover a scalar reward function that explains the choices, and then optimize a policy against it. The bet is that a single consistent reward landscape underlies the human's many local comparisons, and that recovering that landscape recovers the objective.
A demonstration tells you what to do in a state; a comparison only tells you that one behavior beat another. The comparison carries strictly less information per query, yet it is available in vastly more situations and at far lower cost and noise, because relative judgment is easy where demonstration and absolute scoring are hard. The central discovery of preference-based RL is that this weaker signal, aggregated over many comparisons, is enough to reconstruct a usable reward function. You trade richer-but-rarer supervision for poorer-but-plentiful supervision, and for ill-defined objectives that trade is overwhelmingly favorable. This is the same move Chapter 21 made for human-in-the-loop adaptation, now applied to learning the reward itself rather than correcting a prediction.
One historical anchor is worth carrying forward. The modern recipe was crystallized by Christiano and colleagues in 2017, who trained simulated robots and Atari agents from human preferences over short video clips, with the human never seeing the reward and never demonstrating, and reached or exceeded hand-engineered-reward performance using feedback on under one percent of the agent's interactions. That result, learn a reward model from clip comparisons, optimize a policy against it, refresh the comparisons as the policy improves, is the template the rest of this section formalizes and that the language-model alignment of 2022 onward scaled up.
2. The Bradley-Terry Model and Training a Reward Model Intermediate
To turn a pile of "$A$ beat $B$" judgments into a scalar reward, we need a model that maps each behavior to a number and predicts comparisons from those numbers. The Bradley-Terry model, a century-old model of paired comparisons (think chess Elo, which is the same idea), is exactly that link. Assign each item $\sigma$ a latent score $r_\phi(\sigma)$. The model says the probability that $\sigma_w$ is preferred to $\sigma_l$ is a logistic function of the score difference:
$$P_\phi(\sigma_w \succ \sigma_l) = \frac{\exp\big(r_\phi(\sigma_w)\big)}{\exp\big(r_\phi(\sigma_w)\big) + \exp\big(r_\phi(\sigma_l)\big)} = \sigma\!\big(r_\phi(\sigma_w) - r_\phi(\sigma_l)\big),$$where $\sigma(z) = 1/(1 + e^{-z})$ is the logistic sigmoid. The structure is the entire point: preference probability depends only on the difference of scores, not their absolute level. A reward that is uniformly shifted by a constant predicts the identical preferences, so the reward model is identified only up to an additive constant, a fact that returns in subsection three as the reason a baseline or KL anchor is needed. When the two scores are equal the probability is one half (a coin flip, the human is indifferent); as the winner's score pulls ahead the probability saturates toward one.
We fit $r_\phi$ by maximum likelihood: choose parameters that make the observed human choices as probable as possible. The negative log-likelihood over the comparison dataset is the reward-model training loss,
$$\mathcal{L}_{\text{RM}}(\phi) = -\,\mathbb{E}_{(\sigma_w, \sigma_l)\sim\mathcal{D}}\Big[\log \sigma\!\big(r_\phi(\sigma_w) - r_\phi(\sigma_l)\big)\Big],$$which is precisely a binary-cross-entropy loss on the event "the winner outscores the loser", with the score difference as the logit. Minimizing it pushes the score of each preferred item above the score of its rejected partner by a margin that grows with how confidently the model should predict the human's choice. In the language-model setting $\sigma$ is a prompt-response pair and $r_\phi$ is typically the base language model with its output head replaced by a single scalar value head, so the reward model is "the policy network, scored". Figure 27.5.1 lays out the data flow from comparisons to a trained reward.
Two practical properties of this loss deserve emphasis before we use it. First, because only score differences are supervised, the reward model's absolute scale and offset are arbitrary; you cannot read $r_\phi(\sigma) = 4.2$ as "good" in any absolute sense, only $r_\phi(\sigma_w) > r_\phi(\sigma_l)$ is meaningful. Second, the reward model is a learned approximation of human taste and is therefore exploitable: a policy optimized hard enough against it will find inputs where the model's score is high but a real human would disagree, the phenomenon of reward hacking that motivates the KL leash of subsection three. The reward model is a proxy, accurate near the data it was trained on and untrustworthy far from it.
Suppose a trained reward model assigns response $y_w$ a score $r_\phi(y_w) = 2.1$ and response $y_l$ a score $r_\phi(y_l) = 1.3$. The model's predicted probability that a human prefers $y_w$ is $\sigma(2.1 - 1.3) = \sigma(0.8) = 1/(1 + e^{-0.8}) = 1/(1 + 0.449) \approx 0.690$, so about a 69 percent chance the human picks $y_w$. Now shift both scores up by 100, to $102.1$ and $101.3$: the difference is still $0.8$ and the predicted probability is unchanged at $0.690$, the concrete face of "identified only up to an additive constant". Push the winner further ahead, $r_\phi(y_w) = 4.3$ against $r_\phi(y_l) = 1.3$, and the difference $3.0$ gives $\sigma(3.0) \approx 0.953$: the model is now 95 percent sure, a near-certain preference. The reward model is calibrated to confidence: a half-point gap is a mild preference, a three-point gap is near-deterministic, and only gaps, never levels, carry meaning.
3. RLHF: Optimizing a Policy Against the Learned Reward Intermediate
With a reward model $r_\phi$ in hand we have an objective; now we optimize a policy against it. The naive move, "maximize expected learned reward", $\max_\theta \mathbb{E}_{y\sim\pi_\theta}[r_\phi(y)]$, fails for the reason subsection two warned about: the reward model is a proxy, and a policy free to maximize it will gallop off to regions where the proxy is high but wrong, producing degenerate outputs that score well and read badly. The reward model was never trained on those regions and cannot be trusted there. The standard fix is to add a leash. The RLHF objective optimizes reward minus a penalty for straying too far from a trusted reference policy $\pi_{\text{ref}}$ (usually the supervised-fine-tuned model the run started from):
$$\max_{\theta}\; \mathbb{E}_{x\sim\mathcal{D},\, y\sim\pi_\theta(\cdot\mid x)}\Big[\, r_\phi(x, y) \;-\; \beta\, \mathrm{KL}\!\big(\pi_\theta(\cdot\mid x)\,\|\,\pi_{\text{ref}}(\cdot\mid x)\big)\Big].$$Read each term. The first, $r_\phi(x, y)$, is the learned reward: pull the policy toward outputs the reward model scores highly. The second, $\beta\,\mathrm{KL}(\pi_\theta \| \pi_{\text{ref}})$, is the leash: penalize the policy for putting probability mass where the reference would not, with coefficient $\beta$ setting the leash length. A large $\beta$ keeps the policy close to the trusted reference (safe, but little improvement); a small $\beta$ lets it chase reward harder (more improvement, more risk of reward hacking and of forgetting the fluency the reference had). The KL term also resolves the additive-constant ambiguity of subsection two: it anchors the policy to a concrete reference distribution, so the arbitrary offset of $r_\phi$ no longer matters.
In practice this objective is optimized with proximal policy optimization (PPO), the policy-gradient algorithm of Chapter 25, treating each generated token as an action and the per-prompt reward (the reward-model score, with the KL penalty folded in as a per-token shaping term) as the return. This is where the temporal framing becomes literal: generating a response is a sequential decision process over tokens. The state is the prompt plus the tokens emitted so far, each action is the next token, and the episode ends at the end-of-sequence token, at which point the reward model scores the whole completion. RLHF is reinforcement learning over a token-level MDP, and the policy being aligned is a language model. The connection to the large language models of Chapter 15 is not an analogy; it is the same object. The assistants you interact with daily were tuned by exactly this loop: pretrain, supervised fine-tune to get $\pi_{\text{ref}}$, train a reward model from human comparisons, then PPO against it with a KL leash. Figure 27.5.2 names the three stages.
The KL leash earns its place by preventing two distinct failure modes at once. It blocks reward hacking, where the policy discovers adversarial outputs the reward model overrates (repetitive boilerplate that happens to score high, say), because those outputs are far from the reference and the KL term taxes the distance. And it blocks catastrophic forgetting of the fluency and knowledge baked into the reference during pretraining and supervised fine-tuning, because staying near $\pi_{\text{ref}}$ in distribution means retaining its competence. Tuning $\beta$ is the central practical lever of an RLHF run: too tight and the policy barely improves, too loose and it games the reward and degrades. Much of the engineering folklore of RLHF is really folklore about keeping that one balance.
Remove the KL leash and an RLHF policy behaves like a student who has obtained the grading rubric and stopped caring about the subject. If the reward model has even a slight soft spot, say it mildly overrates longer answers, or answers that open with "Certainly!", or that hedge with a bulleted list, the unleashed policy will find that soft spot and live there, emitting confident, bulleted, certainly-prefixed sludge that scores beautifully and helps no one. The leash does not make the policy honest; it makes cheating expensive, by taxing every step away from the reference's distribution. It is a humbling reminder that you are not optimizing "be good", you are optimizing "score well on a flawed model of good", and the gap between those is exactly what the KL term is paid to police.
4. DPO and the 2024 to 2026 Shift to Direct Preference Optimization Advanced
The classic RLHF pipeline is powerful but heavy: three stages, a separate reward model to train and serve, and an on-policy RL loop (PPO) that is notoriously finicky to stabilize, needing careful reward normalization, value-function fitting, and $\beta$ tuning. A natural question is whether all that machinery is necessary, or whether the same preference signal can be optimized more directly. The answer, from Rafailov and colleagues in 2023 and the wave of work that followed, is direct preference optimization (DPO), which collapses reward modeling and policy optimization into a single supervised loss on the preference pairs, with no explicit reward model and no RL loop.
The derivation rests on a clean observation: the KL-constrained objective of subsection three has a closed-form optimal policy, $\pi^*(y\mid x) \propto \pi_{\text{ref}}(y\mid x)\exp(r_\phi(x, y)/\beta)$. Invert that relation to write the reward in terms of the optimal policy and the reference, $r_\phi(x, y) = \beta\log\frac{\pi^*(y\mid x)}{\pi_{\text{ref}}(y\mid x)} + \beta\log Z(x)$, and substitute into the Bradley-Terry likelihood of subsection two. The partition function $Z(x)$ is shared between winner and loser and cancels in the difference, leaving a loss that depends only on the policy, the reference, and the preference pairs:
$$\mathcal{L}_{\text{DPO}}(\theta) = -\,\mathbb{E}_{(x, y_w, y_l)\sim\mathcal{D}}\left[\log \sigma\!\left(\beta\log\frac{\pi_\theta(y_w\mid x)}{\pi_{\text{ref}}(y_w\mid x)} - \beta\log\frac{\pi_\theta(y_l\mid x)}{\pi_{\text{ref}}(y_l\mid x)}\right)\right].$$This is the same Bradley-Terry binary-cross-entropy as the reward-model loss, but with the implicit reward $\hat{r}_\theta(x, y) = \beta\log\frac{\pi_\theta(y\mid x)}{\pi_{\text{ref}}(y\mid x)}$ substituted in. The policy is the reward model now. Minimizing $\mathcal{L}_{\text{DPO}}$ raises the policy's log-probability of preferred responses and lowers it for rejected ones, each measured relative to the reference, which is precisely what the three-stage pipeline accomplished, but in one offline supervised pass with no reward model to train, no samples to draw on-policy, and no value function to fit. The KL leash of subsection three has not vanished; it survives, exactly, inside the $\log(\pi_\theta/\pi_{\text{ref}})$ ratio and the coefficient $\beta$, which is why DPO needs the reference model at training time.
RLHF trains a reward model, then optimizes a policy against it with RL. DPO observes that the optimal RLHF policy and the reward model are two views of one thing, so it skips straight to the policy and lets the implicit reward $\beta\log(\pi_\theta/\pi_{\text{ref}})$ do the reward model's job. The three-stage pipeline (reward model plus PPO) becomes one supervised loss on the same preference data. You give up an explicit, inspectable reward model and the ability to generate fresh on-policy comparisons during training, and in exchange you get a stable, offline, hyperparameter-light procedure that, on many alignment benchmarks from 2023 to 2026, matches or beats PPO-based RLHF. For most practitioners aligning a model from a fixed preference dataset, DPO is now the default first thing to try.
The DPO paper opened a fast-moving design space, and the 2024 to 2026 literature is full of variants worth naming. IPO adds a margin term to curb the overfitting DPO can show when preferences are near-deterministic; KTO drops the pairwise requirement entirely and learns from individual thumbs-up or thumbs-down labels using a prospect-theory-inspired loss, which matters because unpaired feedback is even cheaper to collect than pairs; ORPO folds preference optimization into the supervised fine-tuning stage so a single run produces an aligned model without a separate reference pass. Running through all of them is the same retreat from the heavy RL loop toward direct, supervised-style objectives on preference data.
A second axis of the same period removes the human from the loop. Reinforcement learning from AI feedback (RLAIF), and the Constitutional AI approach that popularized it, replaces the human comparison labeler with a capable language model prompted to judge which of two responses better follows a written set of principles (a "constitution"). The AI judge produces the preference labels, which then feed exactly the reward-model-plus-RL or DPO machinery of this section. RLAIF trades some judgment quality for enormous gains in scale and cost, because an AI labeler is effectively unlimited, and it has become a standard way to bootstrap and scale alignment when human comparisons are the bottleneck. The frontier of 2025 to 2026 increasingly blends the two: a thin layer of trusted human comparisons to anchor quality, amplified by a much larger volume of AI-generated preferences.
The post-2023 alignment literature has split along two fronts this section has named. On the algorithm front, DPO (Rafailov et al., 2023) and its family, IPO (Azar et al., 2024), KTO (Ethayarajh et al., 2024), and ORPO (Hong et al., 2024), have largely displaced PPO as the default for offline preference data, trading on-policy flexibility for stability and simplicity; a counter-current (for example work on online and iterative DPO, and renewed PPO recipes) argues that on-policy sampling still matters for the hardest gains, so the PPO-versus-DPO question is genuinely open rather than settled. On the supervision front, RLAIF and Constitutional AI (Bai et al., 2022) showed AI-generated preferences can substitute for or amplify human ones, and 2024 to 2026 work pushes toward process-level and verifiable rewards: reasoning models trained with reinforcement learning against checkable correctness signals (notably GRPO, the critic-free group-relative method behind DeepSeek-R1 in 2025, and the broader RL-from-verifiable-rewards recipes behind recent reasoning systems) extend preference-based ideas from "which answer does a human prefer" to "which reasoning trace actually solves the problem". The throughline for 2026: the reward, whether a learned Bradley-Terry model, an implicit DPO ratio, an AI judge, or a verifier, is the real lever, and how to specify and optimize it cheaply and without gaming is the central open problem of alignment.
5. Worked Example: A Bradley-Terry Reward Model From Scratch, Then trl Advanced
We now make the pipeline executable on a tiny synthetic problem where we know the ground-truth taste and can check that preference learning recovers it. The setup: each "behavior" is a 1-D feature $\sigma \in \mathbb{R}$, and the hidden human reward is $r^*(\sigma) = -(\sigma - 1.0)^2$, a taste that prefers behaviors near $\sigma = 1.0$ and dislikes those far from it. We never reveal $r^*$ to the learner. Instead we generate pairwise preferences: sample two behaviors, and let the human prefer the one with higher $r^*$, with Bradley-Terry label noise so the comparisons are realistic. Code 27.5.1 generates the preference data and trains a small Bradley-Terry reward model on it from scratch, then uses that learned reward to take one tiny preference-driven policy step, a from-scratch microcosm of the whole RLHF idea.
import torch, torch.nn as nn
torch.manual_seed(0)
def true_reward(s): # the HIDDEN human taste; learner never sees it
return -(s - 1.0) ** 2 # prefers behaviors near s = 1.0
# --- 1. Generate pairwise preferences from the hidden taste (Bradley-Terry labels) ---
N = 4000
a = torch.rand(N, 1) * 6 - 3 # candidate behavior A in [-3, 3]
b = torch.rand(N, 1) * 6 - 3 # candidate behavior B in [-3, 3]
p_a_wins = torch.sigmoid(true_reward(a) - true_reward(b)) # Bradley-Terry probability
a_wins = (torch.rand(N, 1) < p_a_wins).float() # noisy human choice
win = torch.where(a_wins.bool(), a, b) # the preferred (won) behavior
los = torch.where(a_wins.bool(), b, a) # the rejected (lost) behavior
# --- 2. Train a reward model r_phi by the Bradley-Terry / BCE loss of subsection 2 ---
reward_model = nn.Sequential(nn.Linear(1, 32), nn.Tanh(), nn.Linear(32, 1))
opt = torch.optim.Adam(reward_model.parameters(), lr=1e-2)
for step in range(800):
rw, rl = reward_model(win), reward_model(los) # scores of winner, loser
loss = -torch.log(torch.sigmoid(rw - rl) + 1e-8).mean() # -log sigma(r_w - r_l)
opt.zero_grad(); loss.backward(); opt.step()
print("final reward-model loss = %.4f" % loss.item())
# --- 3. Use the LEARNED reward to take one preference-driven policy step ---
# A "policy" here is a single mean parameter mu we generate behaviors around.
mu = torch.zeros(1, requires_grad=True) # start far from the (unknown) optimum 1.0
pol_opt = torch.optim.SGD([mu], lr=0.2)
ref_mu = torch.tensor(0.0) # reference policy (the KL anchor)
for step in range(60):
samples = mu + 0.3 * torch.randn(256, 1) # behaviors from the policy
learned_r = reward_model(samples).mean() # maximize LEARNED reward...
kl = 0.5 * (mu - ref_mu) ** 2 # ...minus a KL leash to ref
obj = -(learned_r - 0.1 * kl) # negate: SGD minimizes
pol_opt.zero_grad(); obj.backward(); pol_opt.step()
print("policy mean moved to mu = %.3f (true optimum is 1.0)" % mu.item())
final reward-model loss = 0.5126
policy mean moved to mu = 0.873 (true optimum is 1.0)
Code 27.5.1 ran about 35 lines to express data generation, reward-model training, and a policy step. For a real language-model alignment run, the production library trl (HuggingFace Transformer Reinforcement Learning) collapses each of those stages to a configured trainer. Code 27.5.2 shows the reward-model and DPO calls; the same preference dataset that took dozens of lines of manual loss and optimization above becomes a trainer object and a single .train().
from datasets import Dataset
from transformers import AutoModelForSequenceClassification, AutoModelForCausalLM, AutoTokenizer
from trl import RewardTrainer, RewardConfig, DPOTrainer, DPOConfig
# Preference data: each row has a prompt, a chosen response, and a rejected response.
prefs = Dataset.from_dict({
"prompt": ["Summarize the report.", "Explain gradient descent."],
"chosen": ["A concise, faithful summary.", "A clear, correct explanation."],
"rejected": ["A rambling, off-topic reply.", "A confidently wrong explanation."],
})
# --- Option A: train an explicit Bradley-Terry reward model (subsection 2) ---
rm = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased", num_labels=1)
tok = AutoTokenizer.from_pretrained("distilbert-base-uncased")
RewardTrainer(model=rm, args=RewardConfig(output_dir="rm"),
train_dataset=prefs, processing_class=tok).train() # BT loss, internally
# --- Option B: skip the reward model and RL loop entirely with DPO (subsection 4) ---
policy = AutoModelForCausalLM.from_pretrained("gpt2")
DPOTrainer(model=policy, args=DPOConfig(output_dir="dpo", beta=0.1),
train_dataset=prefs, processing_class=tok).train() # DPO loss, internally
trl. RewardTrainer fits the Bradley-Terry reward model of subsection two and DPOTrainer runs the DPO loss of subsection four, each with the KL anchoring, batching, and optimization handled internally. The roughly 35 lines of hand-written loss, optimizer, and policy step in Code 27.5.1 collapse to one trainer construction and one .train() call per option.# RewardTrainer: Bradley-Terry loss minimized over the (chosen, rejected) pairs
# DPOTrainer: implicit-reward DPO loss; no separate reward model, no PPO loop
# (both stream loss/accuracy logs per step; the chosen-vs-rejected margin grows)
trl trainers consume the identical preference dataset; RewardTrainer yields a scored reward model for a downstream PPO run, while DPOTrainer yields an aligned policy directly, the practical embodiment of the RLHF-versus-DPO choice of subsection four.Read the two code blocks together. Code 27.5.1 exposed the gears: a Bradley-Terry loss is binary cross-entropy on score differences, and "RLHF" is maximizing a learned reward under a KL leash, both of which fit in a screenful of PyTorch. Code 27.5.2 showed that production tooling hides those gears behind a trainer, and, more importantly, that the modern default (DPOTrainer) discards the reward model and the RL loop altogether, exactly the simplification subsection four derived. The pedagogical payoff is that preference alignment is not a black box: you can build its reward model and policy step by hand, a library wraps the same math, and DPO is a principled shortcut through it, not a different idea.
Who: A product team at a software company fine-tuning a customer-support assistant built on an open-weight base model, the kind of language model whose lineage runs back through Chapter 15.
Situation: Their supervised-fine-tuned assistant was fluent but inconsistently helpful: sometimes terse to the point of unhelpful, sometimes padded with disclaimers, and no one on the team could write a reward function that captured "a good support answer".
Problem: They needed to encode a fuzzy quality bar, accurate, appropriately concise, warm but not obsequious, that they could recognize on sight but not specify, and they had limited annotator time.
Dilemma: Full PPO-based RLHF promised the most control but required training and serving a reward model and stabilizing an on-policy RL loop, weeks of finicky engineering. DPO promised most of the benefit from the same comparison data with a stable offline run, but gave up the inspectable reward model and online comparison refresh.
Decision: They collected about eight thousand pairwise comparisons (support agents picking the better of two assistant drafts), seeded with a small set of human pairs and amplified with RLAIF-style AI-judged pairs against a written quality rubric, then ran DPO with $\beta = 0.1$ against the supervised model as reference.
How: The pipeline was the DPOTrainer path of Code 27.5.2 on the eight thousand pairs; the supervised model served as both the initial policy and the frozen reference, so the KL ratio in the DPO loss kept the aligned model from drifting off its competent base.
Result: Human raters preferred the DPO-aligned assistant over the supervised baseline on a held-out prompt set, with the largest gains on the "appropriately concise" axis the team could never have hand-specified, and the offline run finished in hours without the PPO stabilization headaches.
Lesson: When the objective is recognizable but not writable, collect comparisons, not specifications; and when the comparisons are fixed and offline, reach for DPO before PPO. The reward you cannot write down, you can still learn from choices.
The from-scratch reward model, training loop, and policy step of Code 27.5.1 ran about 35 lines of explicit PyTorch (data generation, Bradley-Terry loss, two optimizers, a KL leash). The trl library collapses the entire pipeline to a configured trainer per stage: RewardTrainer implements the Bradley-Terry reward-model loss, PPOTrainer implements the KL-leashed PPO loop of subsection three, and DPOTrainer implements the direct loss of subsection four, each handling tokenization, batching, reference-model log-probabilities, KL estimation, and logging internally. The same preference dataset that drove dozens of hand-written lines becomes a few lines: build the trainer, call .train(). The library buys correctness on the fiddly parts (reference log-prob caching, KL estimation, sequence-level reward bookkeeping) that are easy to get subtly wrong by hand.
Chapter 27 traced a single retreat from strong supervision toward weak. We began with optimal control, where a known model and a written cost let us compute the optimal action outright. Imitation learning gave up the model and the cost and leaned on demonstrations: behavioral cloning, DAgger, and inverse RL recovered behavior or reward from an expert who could act. This section gave up even the demonstration, keeping only the human's ability to compare, and showed that a Bradley-Terry reward model plus a KL-leashed policy update (RLHF), or its DPO shortcut, recovers a usable objective from choices alone, the recipe that aligns every modern assistant. The arc of the chapter is a ladder of ever-weaker supervision, and at each rung the same temporal MDP machinery does the work, now over tokens.
Notice the conceptual hinge we end on. To align a language model we treated generation as a sequential decision process and reinforcement learning as the tool. Chapter 28 turns that relationship inside out. Rather than using RL to train a sequence model, it asks whether the entire reinforcement-learning problem can be recast as sequence modeling: feed states, actions, and returns to a Transformer and let next-token prediction produce optimal behavior, conditioning on a desired return the way we here conditioned on a learned reward. The Decision Transformer and Trajectory Transformer of Chapter 28 are the destination of the temporal thread that the MDP of Chapter 22 began: the MDP returns one last time, not as a control problem to solve but as a sequence to model. The reward we learned to score with here becomes the return we learn to condition on there.
Exercises
Conceptual. The Bradley-Terry model identifies the reward $r_\phi$ only up to an additive constant. (a) Show that shifting every score by the same constant $c$ leaves $P_\phi(\sigma_w \succ \sigma_l)$ unchanged. (b) Explain why this ambiguity is harmless for the RLHF objective of subsection three but would be a problem if you tried to compare reward-model scores across two separately trained reward models. (c) Identify which term in the RLHF objective resolves the ambiguity and why.
Implementation. Extend Code 27.5.1. (a) Sweep the KL coefficient (the 0.1 multiplying kl) across values $\{0.0, 0.05, 0.2, 1.0\}$ and report where the policy mean $\mu$ settles for each; explain the trend in terms of the leash length of subsection three. (b) Replace the from-scratch policy step with the DPO loss of subsection four: implement $\mathcal{L}_{\text{DPO}}$ directly on the (win, los) pairs using the reward model's frozen scores as a stand-in reference, and confirm the implied preference ordering matches the Bradley-Terry model you trained. (c) Add Bradley-Terry label noise control: vary the spread of the candidate behaviors and report how reward-model loss changes, relating it to the saturation behavior of the sigmoid.
Open-ended. RLAIF replaces the human comparison labeler with an AI judge. (a) Describe a concrete failure mode in which the AI judge's biases get baked into the learned reward and amplified by the policy, and connect it to the reward-hacking discussion of subsection three. (b) Propose a protocol that mixes a small number of trusted human comparisons with a large volume of AI-judged comparisons to bound that risk, and state what you would measure to know it is working. (c) Argue, with reference to the 2024 to 2026 verifiable-reward frontier of subsection four, when you would prefer a checkable correctness signal over any preference model at all.