Contents

Foundation Model Engineering

Manuscript updated (UTC) · Chapter source on GitHub · Cite this chapter (RIS)

On this page

10.3 Direct Preference Optimization (DPO)

Direct Preference Optimization (DPO) trains a policy from pairs of responses without fitting a separate scalar reward model or running online rollouts [1]. Its loss compares how a policy changes the odds of one response against another, relative to a frozen reference. Pair construction, templates, response length, and reference identity therefore affect the objective—not just its implementation.


Objective

For the same prompt xx, let ywy_w be preferred and yly_l rejected. For policy πθ\pi_\theta, frozen reference πref\pi_{\text{ref}}, and coefficient β>0\beta>0, the loss for one pair is

LDPO=−log⁡σ(β[log⁡πθ(yw∣x)πref(yw∣x)−log⁡πθ(yl∣x)πref(yl∣x)]).\mathcal{L}_{\text{DPO}} = -\log\sigma\left( \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] \right).

The bracketed quantity is the unscaled policy-reference margin mm. The implicit reward margin, and the logit passed to the sigmoid σ\sigma, is βm\beta m. In the original derivation, β\beta is the coefficient of the KL penalty relative to the reference; in this loss it also scales the preference logit [1]. Its practical effect depends on data, optimizer, and log-probability convention, so run a β\beta sweep. The standard batch objective averages these per-pair losses.


Worked Example: Compare the Change in Odds

Use two authored preference pairs. In A, the question is “What is 2 + 2?” and the chosen/rejected answers are “4”/“5”. In B, the request is “Return only the integer: 3 × 3”, and the answers are “9”/“The answer is 9.” B expresses a format preference. These invented scores are complete response log probabilities, including termination, not outputs measured from a language model.

Write ℓθ,w\ell_{\theta,w} and ℓθ,l\ell_{\theta,l} for the policy’s chosen/rejected log probabilities, and use the subscript ref\mathrm{ref} for the reference. Define

Δθ=ℓθ,w−ℓθ,l,Δref=ℓref,w−ℓref,l,m=Δθ−Δref.\begin{aligned} \Delta_\theta&=\ell_{\theta,w}-\ell_{\theta,l},\\ \Delta_{\mathrm{ref}}&=\ell_{\mathrm{ref},w}-\ell_{\mathrm{ref},l},\\ m&=\Delta_\theta-\Delta_{\mathrm{ref}}. \end{aligned}
QuantityPair APair B
Policy log probabilities (chosen, rejected)(−4, −6)(−8, −7)
Reference log probabilities (chosen, rejected)(−5, −9)(−10, −6)
Policy gap Δθ\Delta_\theta2−1
Reference gap Δref\Delta_{\mathrm{ref}}4−4
Relative margin mm−23

In A, the policy makes the chosen answer more likely than the rejected one, but the reference favors it even more. The relative margin is negative. In B, the chosen answer is still less likely in the policy, yet its odds improved relative to the reference. The relative margin is positive. A positive margin is therefore neither a guarantee that sampling produces the chosen answer nor evidence of factual correctness.

Runnable arithmetic example: Python 3 standard library only; two fixed preference pairs, summed natural-log probabilities, and β=0.5\beta=0.5. It computes the original per-pair sigmoid DPO loss and its derivative with respect to the policy gap. It omits tokenization, model forwards, automatic differentiation, optimization, and checkpoints; it is not a trainer.

from math import exp, isclose, log1p


def dpo_pair(policy, reference, beta=0.5):
    # Each tuple: summed completion log p (chosen, rejected), including EOS.
    policy_gap = policy[0] - policy[1]
    reference_gap = reference[0] - reference[1]
    margin = policy_gap - reference_gap
    logit = beta * margin
    loss = max(0.0, -logit) + log1p(exp(-abs(logit)))
    q = exp(-abs(logit))
    sigmoid_minus_logit = q / (1 + q) if logit >= 0 else 1 / (1 + q)
    gap_gradient = -beta * sigmoid_minus_logit
    return margin, loss, gap_gradient


pairs = {
    "A": ((-4.0, -6.0), (-5.0, -9.0)),
    "B": ((-8.0, -7.0), (-10.0, -6.0)),
}
for name, (policy, reference) in pairs.items():
    margin, loss, gradient = dpo_pair(policy, reference)
    print(f"{name}: margin={margin:.1f}, loss={loss:.6f}, dL/dgap={gradient:.6f}")

policy_a, reference_a = pairs["A"]
a = dpo_pair(policy_a, reference_a)
b = dpo_pair(*pairs["B"])
changed_reference = dpo_pair(policy_a, (-7.0, -8.0))
print(f"A, changed reference: margin={changed_reference[0]:.1f}, "
      f"loss={changed_reference[1]:.6f}, dL/dgap={changed_reference[2]:.6f}")
assert a[0] == -2 and b[0] == 3 and changed_reference[0] == 1
assert isclose(a[1], 1.3132616875182228)
assert isclose(b[1], 0.2014132779827524)
assert isclose(changed_reference[1], 0.4740769841801067)
assert all(result[2] < 0 for result in [a, b, changed_reference])

# Both completion likelihoods can fall while this pair's loss improves.
lower_both = dpo_pair((-6.0, -10.0), reference_a)
assert lower_both[1] < a[1]
print(f"A, both log p lower: margin={lower_both[0]:.1f}, loss={lower_both[1]:.6f}")

The first two losses are 1.313262 and 0.201413; a uniform two-pair batch averages them. Replacing A’s reference log probabilities with (−7, −8) changes its margin from −2 to +1 and its loss to 0.474077, without changing the policy or preference label. This is why a reference checkpoint is part of the objective, not just reproducibility metadata.

The gradient direction does not reverse. For a fixed chosen label and β>0\beta>0,

∂L∂Δθ=−β σ(−βm)<0.\frac{\partial\mathcal L}{\partial\Delta_\theta}=-\beta\,\sigma(-\beta m)<0.

The reference changes the weighting of the update; this derivative still favors a larger chosen-versus-rejected policy gap. Shared model parameters couple many responses, so it does not guarantee that every chosen response becomes more likely after an optimizer step. In the last test, both policy log probabilities fall, but the rejected one falls further: the gap grows and the loss drops to 0.693147. Loss alone cannot establish response quality. Likewise, σ(βm)\sigma(\beta m) is the fitted preference score under the assumed preference model, not a calibrated probability that an answer is correct [1].


Pair and Split Contract

Each record contains one prompt, chosen response, rejected response, preference strength or tie/skip state, annotator/rubric provenance, and stable IDs. Chosen and rejected must share byte- and token-identical prompt context after rendering. Do not construct pairs by joining unrelated high- and low-score answers.

Split by prompt/conversation/entity cluster before training. Keep both completions, all annotations, paraphrases, and synthetic siblings in the same split. Randomize display position during annotation; track agreement, ties, abstentions, and length/verbosity differences. Quarantine public/private evaluations, rubrics, semantic neighbors, and teacher-generated variants.


Tokenization and Log-Probability Contract

Render both sides with the exact tokenizer artifact and apply_chat_template. Use the same system messages, role order, BOS/EOS behavior, prompt truncation, and add_generation_prompt policy. Include the intended EOS in each completion and compute log probabilities only on completion tokens.

Template sketch: Requires a tokenizer with a suitable native chat template. This function renders full conversations; it does not produce completion masks or a working DPO batch. Verify those boundaries with the pinned tokenizer and collator before training [2].

def render_pair(tokenizer, prompt_messages, chosen, rejected):
    def render(answer):
        messages = [*prompt_messages, {"role": "assistant", "content": answer}]
        return tokenizer.apply_chat_template(
            messages,
            tokenize=True,
            add_generation_prompt=False,
        )
    return render(chosen), render(rejected)

# The collator must produce, for each side:
# input_ids, attention_mask, completion_mask, and labels (-100 outside completion).

Test the token-level boundary rather than searching decoded delimiters. Exclude the prompt, conditioning-only role markers, and padding from the completion mask, but retain the intended response-ending EOS/end-of-turn token. Do not mask every special token indiscriminately. If the pad ID equals the EOS ID, use actual padding positions from attention_mask, not token-ID equality, to distinguish them. Align each target and its completion mask with the preceding position’s causal logit. Packed examples must not attend across pair/sample boundaries.

Sequence log probability is usually the sum of completion-token log probabilities, which matches the sequence likelihood in the derivation but can create response-length pressure. Sum valid target log probabilities separately for each completion, compute one DPO loss per pair, then average pairs with the declared weighting. Do not divide each sequence score by its token count merely because the batch contains different lengths. A mean changes the objective and is not a neutral fix. Log chosen/rejected lengths, compare sum and length-normalized diagnostics, control verbosity in the data, and choose the serving-relevant policy explicitly.

Truncation is pair-coupled: apply the same prompt budget to both sides, preserve the assistant boundary, and reject or flag pairs whose decisive span or EOS was cut. Never let one side keep more prompt context than the other.


Reference and PEFT Semantics

Freeze and record the exact reference checkpoint hash, tokenizer hash, template hash, precision, and log-probability implementation. A reference silently reconstructed from “the same model name” is not reproducible.

With PEFT, define whether the reference is the base with the adapter disabled, a separate frozen adapter snapshot, or cached reference log probabilities. Verify equivalence on a fixed batch. Reference-free variants optimize a different objective and must be named and evaluated as such.


Run Contract and Monitoring

Start from an SFT checkpoint that already produces valid responses. Pin the pair manifest, code/container, optimizer, batch in completion tokens, precision, max lengths, gradient accumulation, β\beta, and seeds. Save model/adapter, optimizer, scheduler, scaler, RNG, sampler, and data cursor.

Monitor more than loss:

  • chosen and rejected policy log probabilities;
  • chosen and rejected reference log probabilities;
  • implicit reward margin and preference accuracy;
  • KL proxy to reference, entropy, response length, EOS and truncation rates;
  • held-out win rate with paired uncertainty;
  • domain, base-retention, safety, refusal, format/tool, latency, and cost slices.

Stop when both chosen and rejected log probabilities collapse, KL/length leaves the declared band, held-out preference stalls while retention regresses, nonfinite values persist, or a critical slice fails. Release the complete artifact bundle only after a paired baseline comparison and rollback rehearsal.


Quizzes

Quiz 1: Why must chosen and rejected responses use the same rendered prompt? The objective compares two completions conditional on the same input. Different system text, truncation, or template tokens change that conditioning, so the loss no longer represents the labeled comparison. Identical prompts are necessary; they do not make noisy preference labels correct.

Quiz 2: Why is replacing summed completion log probability with a mean not a harmless length correction? The derivation uses sequence likelihood, whose log is a sum. Averaging changes the objective. Engineers should diagnose length bias, curate/control verbosity, and evaluate both length and quality rather than silently changing semantics.

Quiz 3: What must identify the DPO reference policy? The exact checkpoint and tokenizer/template hashes, precision, adapter-enabled state or cached log-prob artifact, and log-probability implementation must be fixed and verified.

Quiz 4: Why can falling DPO loss accompany a worse model? The policy can exploit length/style confounds, drive both answer likelihoods down, move too far from the reference, or overfit annotator bias. Held-out wins, margins, KL, length, retention, safety, and format slices reveal those failures.

Quiz 5: A reference change flips the relative margin from negative to positive. Does DPO now push toward the rejected answer? No. The chosen label remains fixed. For positive beta, the derivative with respect to the policy’s chosen-minus-rejected log-probability gap remains negative. The reference changes its magnitude. Neither margin sign nor loss alone establishes the quality of sampled responses.

References

  1. Rafailov, R., et al. (2023). Direct Preference Optimization: Your Language Model is Secretly a Reward Model. arXiv:2305.18290.
  2. Hugging Face. DPO Trainer. TRL documentation.