Özer Cem KELAHMETKELAHMET
Articles · Concepts

How DPO works: deriving the loss, reading the implicit reward and choosing a variant

Direct Preference Optimization trains a model on pairs of better and worse answers with no reward model and no reinforcement learning loop. This article derives the loss from first principles, works through its gradients with numbers, and explains the variants trainers now offer.

Download Markdown
At a glance
Reading time
14 min
Sections
8
Code examples
2
Tables
3
Sources
9
Published
23 Nov 2026
In 30 seconds
What changed
TRL's DPO trainer reaches 14 loss formulations
Why it matters
DPO remains the simplest preference-tuning method to run.
What to do
Start from a good SFT model

Direct Preference Optimization (DPO) is one of the most widely used ways to tune a language model on human or AI preferences. You give it pairs of answers to the same prompt, one marked better than the other, and it adjusts the model so the better answer becomes more likely relative to the worse one. Unlike classic reinforcement learning from human feedback (RLHF), it needs no separate reward model and no sampling loop during training.

DPO was introduced in a 2023 paper with the subtitle "Your Language Model is Secretly a Reward Model". That subtitle is the key to understanding it. This article derives the DPO loss step by step, shows what its gradient does with worked numbers, explains how to read the metrics a trainer logs, and compares the main variants that libraries now ship.

What happened

TRL's DPO trainer reaches 14 loss formulations (13 November 2026)

Hugging Face's TRL library published version 1.18.0 on PyPI on 13 November 2026, with patches on 17 and 19 November. Its current DPO documentation lists 14 loss formulations, from the original sigmoid loss to IPO, robust DPO, SimPO-style length normalisation (sigmoid_norm) and UDPO, a 2026 proposal for noisy preference labels. The trainer can also combine several losses with weights, as in Mixed Preference Optimization, and supports preference pairs that contain tool calls and tool responses. The defaults remain the original sigmoid loss with β = 0.1.

The documentation adds a practical observation worth keeping in mind: in practice, DPO typically widens the gap between preferred and dispreferred answers by pushing down the likelihood of the dispreferred ones, rather than by raising the likelihood of the preferred ones.

From RLHF to DPO in four steps

Step 1: the RLHF objective

Classic RLHF trains a policy π_θ to maximise a reward r(x, y) while staying close to a reference model π_ref, usually the supervised fine-tuned model:

maximise E[r(x, y)] − β · KL(π_θ ‖ π_ref)

The KL term stops the model from drifting into strange text that happens to fool the reward model. β sets how tight the leash is.

Step 2: the optimal policy has a closed form

This objective has a known solution. The best policy is the reference policy reweighted by the exponentiated reward:

π*(y | x) = π_ref(y | x) · exp(r(x, y) / β) / Z(x)

where Z(x) is a normalising constant that sums over all possible answers. Z(x) is impossible to compute directly, which is why RLHF normally uses PPO instead of this formula.

Step 3: turn it around

Take logs and solve for the reward:

r(x, y) = β · log(π*(y | x) / π_ref(y | x)) + β · log Z(x)

This says any reward function corresponds to a policy, and the reward can be read off as β times the log-ratio between that policy and the reference, plus a term that depends only on the prompt.

Step 4: plug into a preference model

Human preferences are usually modelled with the Bradley-Terry model, which says the probability that answer y⁺ beats y⁻ is the sigmoid of the reward difference:

P(y⁺ ≻ y⁻ | x) = σ(r(x, y⁺) − r(x, y⁻))

Substitute the reward from step 3. The awkward β log Z(x) term appears in both rewards and cancels. What remains depends only on the policy and the reference. Maximising the likelihood of the observed preferences gives the DPO loss:

L_DPO = −E[ log σ( β · ( log π_θ(y⁺|x)/π_ref(y⁺|x) − log π_θ(y⁻|x)/π_ref(y⁻|x) ) ) ]

That is the whole method. The language model itself plays the role of the reward model, and training is ordinary supervised learning on pairs. The same Bradley-Terry idea underlies chatbot arena leaderboards, which the Bradley-Terry ratings simulation shows in action.

An analogy helps. Instead of hiring a judge to score every essay and then coaching the student towards high scores, DPO shows the student pairs of essays and says "more like this one, less like that one", measured relative to how the student wrote before training started.

What the loss does, with numbers

Define the implicit reward of an answer as β times its log-ratio against the reference, and the margin h as the chosen reward minus the rejected reward. The loss is −log σ(h).

Take β = 0.1. Suppose after some training the model's log-probability of the chosen answer has risen from −42 under the reference to −40, a log-ratio of +2, and the rejected answer's log-probability has fallen from −43 to −45, a log-ratio of −2. Then:

  • implicit reward of chosen: 0.1 × 2 = 0.2
  • implicit reward of rejected: 0.1 × (−2) = −0.2
  • margin h = 0.4
  • loss = −log σ(0.4) = 0.513

At the start of training, the policy equals the reference, so both log-ratios are zero, h = 0 and the loss is log 2 = 0.693 for every pair.

The gradient focuses on mistakes

The gradient of the DPO loss with respect to the margin is weighted by σ(−h). The table shows how that weight shrinks as the unscaled log-ratio gap grows, with β = 0.1:

Table 1. The gradient focuses on mistakes
Log-ratio gap (chosen minus rejected) Margin h = β × gap Loss −log σ(h) Gradient weight σ(−h)
0 0.0 0.693 0.500
4 0.4 0.513 0.401
10 1.0 0.313 0.269
20 2.0 0.127 0.119
40 4.0 0.018 0.018

Pairs the model already orders correctly, with a wide margin, contribute almost nothing. Pairs it gets wrong, where h is negative, get a weight above 0.5. The DPO paper highlights this weighting as important: without it, the model degenerates.

What β controls

β plays two roles. In the RLHF objective it is the KL leash, so a larger β keeps the model closer to the reference. In the loss it scales the log-ratio gap, so with a larger β the same gap produces a larger margin and the loss saturates sooner. In the table, β = 0.1 needs a log-ratio gap of 40 to push the loss to 0.018; with β = 0.5, a gap of 8 would do it. TRL's help text puts it simply: higher β means less deviation from the reference model.

The code below reproduces the table:

import math
sig = lambda z: 1 / (1 + math.exp(-z))
beta = 0.1
for gap in (0, 4, 10, 20, 40):
    h = beta * gap
    print(gap, round(-math.log(sig(h)), 3), round(sig(-h), 3))

Reading the training metrics

DPO trainers log the implicit rewards directly, and they tell you more than the loss. TRL records rewards/chosen and rewards/rejected as the average β log-ratios, rewards/margins as their difference, and rewards/accuracies as the fraction of pairs where the chosen reward exceeds the rejected one. It also logs logps/chosen and logps/rejected, the raw average log-probabilities.

Three patterns are common:

Table 2. Reading the training metrics
Pattern in the logs What it means What to check
Margin rises, rewards/chosen falls slowly, rewards/rejected falls fast The usual DPO behaviour: the model mostly suppresses rejected answers Fine if quality holds; watch that chosen log-probs do not fall too far
Both chosen and rejected log-probs fall sharply The model is moving probability mass to answers in neither set Lower the learning rate, raise β, or add an SFT term on chosen answers
Accuracy near 1.0 early, margins very large The pairs are too easy or the model is overfitting Check data quality and evaluate on held-out pairs

The second pattern is a known weakness. The loss only cares about the gap, so it can be satisfied by lowering both log-probabilities as long as the rejected one falls faster. One practical remedy is to add a supervised term on the chosen answers. TRL supports this by combining losses, for example loss_type=["sigmoid", "sft"] with weights; its documentation shows the Mixed Preference Optimization recipe, which combines sigmoid, bco_pair and sft losses with weights 0.8, 0.2 and 1.0.

Variants and what they fix

Many variants change one piece of the loss. Here are the ones that address the most common problems.

Table 3. Variants and what they fix
Variant TRL loss_type Change Problem addressed
DPO sigmoid the original logistic loss baseline
IPO ipo squared loss towards a fixed target margin overfitting when preferences are near-deterministic
SimPO-style sigmoid_norm log-probabilities averaged per token length bias
Robust DPO robust loss corrected for a label-flip probability ε noisy preference labels
UDPO udpo another noise-corrected loss stays unbiased if ε is overestimated
SLiC / RSO hinge hinge hinge loss with margin 1/β different loss shape

IPO: stop pushing at a target

The IPO authors argued that the logistic loss never stops pushing: if one answer is always preferred, the optimal margin is infinite, and the KL leash becomes ineffective. IPO replaces the loss with a squared error towards a fixed target. In TRL, the loss is (Δ − 1/(2β))², where Δ is the difference of the average per-token log-ratios. The code comments note that TRL averages per token so that β is comparable across lengths, and that this matches the results reported in the IPO paper. With β = 0.1 the target gap is 1/(2 × 0.1) = 5.

Length normalisation

The DPO loss uses log-probabilities summed over the whole answer. A 300-token answer can therefore build a much larger log-ratio than a 30-token answer from the same per-token change: a shift of 0.1 per token adds up to 30 over 300 tokens but only 3 over 30. That gives longer answers more influence and can teach the model that longer is better. The SimPO authors normalise by length, and TRL exposes their normalisation as sigmoid_norm, which divides each answer's score by its number of tokens before taking the difference.

Noisy labels

Real preference data contains mistakes: annotators disagree, and AI judges have biases. Robust DPO assumes each label is flipped with probability ε and corrects the loss so that its expectation matches the clean-label loss:

L_robust = [ (1 − ε) · (−log σ(h)) − ε · (−log σ(−h)) ] / (1 − 2ε)

TRL implements exactly this form and requires ε, set through label_smoothing, to lie in [0, 0.5). The documentation describes UDPO, published in 2026, as another unbiased loss under noisy preferences that, unlike robust DPO, stays unbiased when the flip probability is overestimated.

What it means in practice

DPO remains the simplest preference-tuning method to run. It needs one model in training, a frozen reference model for log-probabilities, and a dataset of pairs. Reference log-probabilities can even be precomputed once, so training costs little more than supervised fine-tuning. Combined with LoRA adapters, it fits on modest hardware.

Its limits come from being offline. DPO learns only from the pairs you give it, so it cannot discover better answers the way online methods such as GRPO can when a checkable reward exists. A common pattern is supervised fine-tuning first, DPO on preference pairs for style and safety, and online reinforcement learning where answers can be verified. The post-training simulation walks through the SFT and DPO stages, and the RLHF page covers the classic pipeline DPO replaces.

What to do now

  1. Start from a good SFT model. DPO measures everything relative to the reference, so the reference should already follow instructions and produce answers in the right format.
  2. Keep β at 0.1 until you have evidence. Tune it only after watching the metrics; raise it if the model drifts too far from the reference, and lower it if margins barely move.
  3. Watch chosen log-probabilities, not just the loss. If logps/chosen falls steadily, add an SFT term on chosen answers or lower the learning rate.
  4. Check length statistics. Compare the average length of chosen and rejected answers in your data; if chosen answers are much longer, try sigmoid_norm or balance the data.
  5. Estimate label noise. Re-label a small sample with a second annotator or judge, measure disagreement, and use it as a starting ε for the robust loss.
  6. Hold out preference pairs. Track rewards/accuracies on held-out pairs to catch overfitting that training accuracy hides.
  7. Use on-policy pairs where possible. Pairs sampled from the current model match its distribution better than pairs from a different model, and many teams regenerate pairs between rounds.
  8. Evaluate on real tasks. Implicit reward margins measure fit to the data, not quality; confirm gains with task benchmarks and side-by-side reviews.

Risks and open questions

Likelihood displacement. Because the loss depends only on the gap, both chosen and rejected answers can lose probability, and the freed mass may go to answers nobody reviewed.

Preference data quality sets the ceiling. DPO copies whatever the labels reward. If annotators or AI judges favour long or confident answers, the model learns that.

Offline data goes stale. Pairs generated by an older model become less informative as the policy changes, which is one reason iterative and online variants exist.

Too many variants, too few comparisons. Fourteen loss types in one trainer is a sign that no single choice dominates. Most comparisons use a handful of benchmarks, so results may not transfer to your domain.

Watch next: whether noise-aware losses such as robust DPO and UDPO become default for AI-labelled data, and how teams combine offline DPO with online reinforcement learning in one pipeline.

Frequently asked questions

Why does DPO not need a reward model?

Because the optimal policy under the RLHF objective implies a reward equal to β times the log-ratio between policy and reference, plus a prompt-only term. In the Bradley-Terry preference model that prompt-only term cancels, so the policy's own log-probabilities act as the reward.

What does β do in DPO?

It sets the strength of the KL constraint to the reference model. A higher β keeps the model closer to the reference and makes the loss saturate after smaller log-ratio gaps; TRL's default is 0.1.

Why does the chosen answer's probability sometimes fall during training?

The loss only rewards a larger gap between chosen and rejected answers. It can be reduced by lowering both, as long as the rejected answer falls faster, and TRL's documentation notes that DPO typically works mostly by suppressing rejected answers.

When should I use IPO instead of DPO?

When preferences are nearly deterministic or your data is small, the logistic loss can push margins without limit and overfit. IPO targets a fixed margin of 1/(2β), which gives a natural stopping point.

Is DPO better than GRPO?

They solve different problems. DPO learns offline from fixed preference pairs and suits style, tone and safety; GRPO learns online from rewards on fresh samples and suits tasks with checkable answers such as maths or code.

Summary

DPO turns preference tuning into supervised learning. Starting from the RLHF objective, the optimal policy has a closed form, which implies that a reward equals β times the log-ratio between policy and reference; inserting that into the Bradley-Terry model cancels the intractable normaliser and leaves a logistic loss on pairs. The gradient focuses on pairs the model orders wrongly, β sets both the KL leash and how fast the loss saturates, and the logged implicit rewards show whether the model is lifting good answers or only pushing bad ones down. Variants fix specific weaknesses: IPO stops at a target margin, length normalisation removes the advantage of long answers, and robust losses correct for noisy labels. For related methods, see the DPO, SimPO and IPO pages, the TRL page and the training and alignment topic.

Sources

Cite this article

Kelahmet, Ö. C. (2026, November 23). How DPO works: deriving the loss, reading the implicit reward and choosing a variant. Özer Cem KELAHMET. https://www.ozercemkelahmet.com/articles/dpo-loss-derivation-implicit-reward

BibTeX
@online{kelahmet2026loss,
  author = {Kelahmet, {\"O}zer Cem},
  title  = {{How DPO works: deriving the loss, reading the implicit reward and choosing a variant}},
  year   = {2026},
  month  = {nov},
  url    = {https://www.ozercemkelahmet.com/articles/dpo-loss-derivation-implicit-reward}
}
Version historyfirst version
  1. Published

Factual corrections are listed here with what changed. Wording and formatting edits are not listed.

Last updated Nov 23, 2026
Type to search.
    ↑↓ navigate↵ open