Preference Optimisation

Preference optimisation trains a language model from comparisons, teaching it to choose responses people prefer rather than merely imitate demonstrations.

The Big Question

Fine-tuning taught the model to imitate good examples. If the dataset contains a helpful burger answer, supervised fine-tuning makes that kind of answer more likely.

How do we train a language model when there is no single correct answer?

Many real prompts have multiple valid answers. One answer may be shorter, another more useful, another safer, another better formatted. Preference optimisation asks a new question: among possible answers, which one would people prefer?

Fine-tuning asks what a good answer looks like. Preference optimisation asks which answer is better. That contrast is the heart of this chapter.

Core Intuition

Imagine Jessie is training at a burger restaurant. At first, she watches good employees answer customers. That is supervised fine-tuning.

Later, her manager shows two possible replies and asks which one should be used:

Response A

Make a burger.

Response B

Make a cheeseburger. Shape the beef into a patty, cook it in a pan, melt the cheese on top, toast the bun, and add fried onion.

The manager chooses Response B. Jessie learns something more subtle than imitation: useful answers tend to be specific, constraint-aware, practical, safe, and neither too short nor too long.

Preference optimisation tries to teach that judgement. It is useful because comparison is often easier than creation. A person may struggle to write the perfect answer, but can still recognize which of two answers is better.

Interactive Demo

Preference optimisation lab

Preference pair

User: I only have beef, buns, onion, and cheese. What can I make?

Reward model gap

Reward gap
3.00
Preference probability
0.953
Loss
0.049
DPO probability race

Shift toward the winner

DPO compares how much the policy has moved toward chosen and rejected responses relative to a frozen reference model.

DPO logit
1.040
Probability chosen wins
0.739
DPO loss
0.303

Preference optimisation learns from contrasts. The chosen answer and rejected answer together define the training signal.

Preference Data

A preference dataset usually contains triples:

(x,  yw,  yl)(x,\;y_w,\;y_l)

Here xx is the prompt, ywy_w is the preferred response, and yly_l is the rejected response.

{
  "prompt": "I only have beef, buns, onion, and cheese. What can I make?",
  "chosen": "Make a cheeseburger. Shape the beef into a patty, cook it in a pan, melt the cheese on top, toast the bun, and add fried onion.",
  "rejected": "Make food with the ingredients."
}

The label does not say the chosen response is the only possible correct answer. It says that, for this prompt, this response is better than that one.

Close comparisons are especially valuable. If the rejected answer is nonsense, the model learns little. If both answers are plausible but one is safer, clearer, or more constraint-aware, the model learns judgement.

Reward Models

One way to learn from preferences is to train a reward model. A reward model takes a prompt and response and outputs a scalar score:

rϕ(x,y)r_\phi(x,y)

A higher reward should mean the response is more likely to be preferred. For a preferred response ywy_w and rejected response yly_l, we want:

rϕ(x,yw)>rϕ(x,yl)r_\phi(x,y_w)>r_\phi(x,y_l)

Bradley-Terry Loss

A common differentiable model says the probability that humans prefer the winner is:

P(ywylx)=σ(rϕ(x,yw)rϕ(x,yl))P(y_w\succ y_l\mid x)=\sigma(r_\phi(x,y_w)-r_\phi(x,y_l))

The difference matters because preferences are relative. The loss for one pair is:

Lreward(ϕ)=logσ(rϕ(x,yw)rϕ(x,yl))\mathcal{L}_{\text{reward}}(\phi)=-\log\sigma(r_\phi(x,y_w)-r_\phi(x,y_l))

If the reward model gives the winner score 55 and the loser score 22, the gap is 33, σ(3)0.95\sigma(3)\approx0.95, and the loss is small. If the scores are reversed, the loss is large.

The reward model does not need perfect absolute scores. It needs to rank preferred responses above rejected ones.

RLHF

RLHF stands for reinforcement learning from human feedback. The classic pipeline has three broad stages: pretrain a language model, fine-tune it on demonstrations, then use preference data to train a reward model and optimise the language model against that reward.

1PretrainingLearn broad language ability from next-token prediction
2Supervised fine-tuningLearn what assistant-style answers look like
3Generate candidatesSample several responses to the same prompt
4Collect preferencesHumans or AI judges rank responses
5Optimise preferencesUpdate the model toward preferred responses
6Evaluate behaviorCheck helpfulness, honesty, harmlessness, and failure cases

Let πθ(yx)\pi_\theta(y\mid x) be the language model policy and rϕ(x,y)r_\phi(x,y) be the reward model. A naive objective would be:

maxθ  Eyπθ(x)[rϕ(x,y)]\max_\theta\;\mathbb{E}_{y\sim\pi_\theta(\cdot\mid x)}[r_\phi(x,y)]

But optimizing only reward is dangerous. The model can exploit weaknesses in the reward model and drift away from useful behavior. So RLHF usually keeps the model close to a reference model, often the supervised fine-tuned model:

maxθ  Eyπθ(x)[rϕ(x,y)βlogπθ(yx)πref(yx)]\max_\theta\;\mathbb{E}_{y\sim\pi_\theta(\cdot\mid x)}\left[r_\phi(x,y)-\beta\log\frac{\pi_\theta(y\mid x)}{\pi_{\text{ref}}(y\mid x)}\right]

The reward term pulls toward preferred behavior. The reference penalty keeps the model from moving too far from a model we already trust. The coefficient β\beta controls that trade-off.

Reward Hacking

Reward hacking happens when the model gets high reward without satisfying the true goal. If a reward model overvalues confident wording, the assistant may become overconfident. If it overvalues long answers, the assistant may become bloated.

The measured objective is not always the real objective. This is why models need human evaluation after preference optimisation, not only reward-model scores.

Direct Preference Optimisation

DPO, or direct preference optimisation, learns from preference pairs without training a separate reward model and without running a full reinforcement learning loop.

It asks: can we directly update the language model so it assigns higher probability to preferred responses than rejected responses?

Let πθ\pi_\theta be the policy we are training and πref\pi_{\text{ref}} be the frozen reference model. For one preference pair, DPO defines:

z=β[logπθ(ywx)πref(ywx)logπθ(ylx)πref(ylx)]z=\beta\left[\log\frac{\pi_\theta(y_w\mid x)}{\pi_{\text{ref}}(y_w\mid x)}-\log\frac{\pi_\theta(y_l\mid x)}{\pi_{\text{ref}}(y_l\mid x)}\right]
LDPO(θ)=logσ(z)\mathcal{L}_{\text{DPO}}(\theta)=-\log\sigma(z)

The first ratio asks how much more the new model favors the winner compared with the reference. The second ratio asks how much more it favors the loser. The difference asks whether the policy shifted more toward the winner than toward the loser.

DPO folds reward learning and policy optimization into a supervised-style objective. It is simpler than classic RLHF, but it still depends heavily on high-quality preference data.

Constitutional AI And RLAIF

Human preference labels are expensive, and some tasks can expose labelers to harmful content. Constitutional AI uses a written set of principles, a constitution, to guide critique, revision, and AI-generated feedback.

A simple burger constitution

  • Give practical cooking advice.
  • Respect the user's constraints.
  • Do not recommend unsafe food handling.
  • Admit uncertainty when needed.
  • Prefer concise answers unless the user asks for detail.

RLAIF means reinforcement learning from AI feedback. Another AI system compares or critiques responses according to principles or rubrics. This can scale feedback, but it does not remove the need for human judgement. Human judgement moves upstream into designing principles, audits, and evaluations.

What Preferences Can Teach

Preference optimisation can teach many qualities, but it only optimises what the feedback process rewards.

HelpfulnessSatisfy the user's actual request with useful, specific information
HonestyAvoid inventing facts and admit uncertainty when appropriate
HarmlessnessAvoid harmful assistance while still giving safe alternatives
Instruction-followingRespect format, constraints, tone, and scope
StyleBe clear, concise, calm, and appropriately detailed

These goals can conflict. A request for harmful instructions may require refusing the harmful part while still offering safe alternatives. A bad safety model refuses too much. A good one balances helpfulness, harmlessness, and honesty.

How The Methods Differ

MethodData signalMain idea
Supervised fine-tuningDemonstrationsImitate this good response
Reward-model RLHFPreference rankings plus a learned reward modelGenerate responses the reward model scores highly
DPOChosen/rejected response pairsMake chosen responses more likely than rejected ones relative to a reference
Constitutional AIPrinciples, critique, revision, and AI feedbackImprove behavior according to a written constitution

Preference optimisation changes model behavior. Retrieval gives the model external information. If the problem is missing current facts, use retrieval. If the problem is poor judgement or constraint-following, use fine-tuning or preference optimisation.

Implementation Sketches

Reward Model

for batch in dataloader:
    prompt = batch["prompt"]
    chosen = batch["chosen"]
    rejected = batch["rejected"]

    reward_chosen = reward_model(prompt, chosen)
    reward_rejected = reward_model(prompt, rejected)

    difference = reward_chosen - reward_rejected
    loss = -log_sigmoid(difference)

    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

DPO

for batch in dataloader:
    prompt = batch["prompt"]
    chosen = batch["chosen"]
    rejected = batch["rejected"]

    chosen_logp = policy.log_prob(prompt, chosen)
    rejected_logp = policy.log_prob(prompt, rejected)

    with torch.no_grad():
        chosen_ref_logp = reference.log_prob(prompt, chosen)
        rejected_ref_logp = reference.log_prob(prompt, rejected)

    chosen_shift = chosen_logp - chosen_ref_logp
    rejected_shift = rejected_logp - rejected_ref_logp

    z = beta * (chosen_shift - rejected_shift)
    loss = -log_sigmoid(z)

    loss.backward()
    optimizer.step()
    optimizer.zero_grad()

The policy model is trained. The reference model stays frozen. The objective rewards the policy for shifting probability toward the chosen response more than toward the rejected response.

Interview Discussion

Why is supervised fine-tuning not enough?

Many prompts have multiple valid answers. Preference optimisation teaches the model which answers are better according to comparisons.

What does a reward model learn?

It learns to score prompt-response pairs so preferred responses receive higher reward than rejected responses.

Why does RLHF use a reference model or KL penalty?

To prevent the policy from drifting too far from a trusted model or exploiting reward-model weaknesses.

How does DPO differ from RLHF?

DPO directly trains the language model on preference pairs, avoiding a separate reward model and reinforcement learning loop.

What makes a good preference pair?

Both responses are plausible, but one is meaningfully better on usefulness, safety, honesty, format, or constraint-following.

Active Recall

1. What problem does preference optimisation solve?

2. What do x, y_w, and y_l represent?

3. Why does the reward loss use a difference of rewards?

4. What is reward hacking?

5. What does the KL or reference penalty protect?

6. What does DPO remove from the classic RLHF pipeline?

7. Why does DPO still need a reference model?

8. Why does preference optimisation not guarantee truth?

Common Mistakes

  • Thinking preference optimisation finds truth. It optimises behavior preferred by the feedback process.
  • Thinking the reward model is the assistant. The reward model judges responses; the language model generates them.
  • Ignoring the reference model. Without a reference constraint, optimisation can drift into strange behavior.
  • Treating DPO as magic. DPO simplifies the method, but data quality still decides what the model learns.
  • Confusing harmlessness with refusal. A good model refuses harmful help while still offering useful safe alternatives.

Connection To Inference And Decoding

We have now trained the model in stages. Pretraining gave broad language ability. Fine-tuning taught assistant behavior. Preference optimisation taught the model to prefer better answers over worse ones.

But training is over. The weights are fixed. When a user asks a new question, the model must generate an answer one token at a time. Inference and decoding decide how that probability distribution becomes actual text.