4.4.4 · D4Alignment, Prompting & RAG

Exercises — Direct Preference Optimization (DPO)

3,270 words15 min readBack to topic

Before we start, a few tools we use constantly.

The sigmoid function:

The conditioning notation : every probability is conditioned on the prompt . So means "the probability the policy assigns to producing the winning response given the prompt ." We always keep the same when comparing and . In some exercises we write as shorthand, but is always implicitly there and always the same for both responses in a pair.

The partition function : when we solve the RLHF objective for the best policy, the answer must be a valid probability distribution — its values over all possible responses must sum to . The quantity that rescales it to do so is

And the quantity we score each pair with:

Figure — Direct Preference Optimization (DPO)

Level 1 — Recognition

Recall Solution
  • = the prompt.
  • = the winning (chosen / preferred) response.
  • = the losing (rejected) response.
  • is the policy being trained — it receives gradients.
  • is the frozen reference (the Supervised Fine-Tuning (SFT) checkpoint) — never updated.
Recall Solution

Both log-ratios are , so , margin , and . This is the coin-flip baseline: before any training the model has no preference, so it scores exactly per example.

Recall Solution

The margin is negative, meaning the model gives the loser a higher implicit reward than the winner — the model is wrong on this pair. Since is decreasing in (see figure), any gives loss above .


Level 2 — Application

Recall Solution
  • .
  • .
  • margin .
  • .
  • .

Positive margin ⇒ loss below : the model already leans the right way.

Recall Solution

The reward margin is .

  • : , , .
  • : , , .

Larger turns the same policy shift into a bigger reward margin, saturating the sigmoid → smaller loss → the model is "satisfied" and deviates less. Bigger = tighter leash.

Recall Solution

happens only when , i.e. . Since and , we need : the winner and loser have identical policy shifts. This is exactly the initialization / indifference case.


Level 3 — Analysis

Recall Solution

The two terms are identical because depends only on , and both responses share the same . They subtract to : Condition: both responses must be conditioned on the same prompt . That is why DPO needs pairs per prompt — Bradley-Terry Model uses a difference of scores.

Recall Solution

Deriving the weight. We need . Two facts:

  • (standard sigmoid derivative).
  • .

Therefore using the symmetry . By the chain rule the parameter gradient is , and . So the multiplying factor (the "weight") is exactly . Intuition: is "how much probability the model is missing" on getting the winner right — a bigger miss means a bigger push.

Evaluating:

  • (a) badly wrong: , weight large.
  • (b) confidently right: , weight tiny.

The weight is large exactly when the model ranks the loser above the winner (a mistake), and near zero when the model is already correct. DPO automatically focuses on hard/wrong examples — a built-in hard-example weighting, no explicit Reward Modeling needed.

Recall Solution
  • Raw version: , a large-ish absolute value driven mostly by the token already being common — the model gets "credit" for probability it didn't create.
  • Ratio version: — a small reward, because the model barely changed anything.

The ratio rewards only the change the policy made over the reference. This is what prevents collapse onto high-frequency generic text (degeneration) — the failure the raw version invites.


Level 4 — Synthesis

Recall Solution
  • .
  • .
  • .
  • Mean .

Notice pair 2 (negative margin, a mistake) dominates the loss — consistent with the L3.2 hard-example weighting.

Recall Solution

We need with , so . If instead (ten times larger), then for the same , , deep in the saturated region (). The gradient nearly vanishes, so the model stops moving early → it deviates much less from . Larger = stronger leash, as in PPO - Proximal Policy Optimization's KL penalty analogue.

Recall Solution

Take the natural log of both sides: Isolate : Multiply through by : This is the bridge: the reward is now written in terms of the policy, which is what lets us delete the separate reward model.


Level 5 — Mastery

Recall Solution

Using , so :

  • : , so . The model confidently prefers the winner → no loss, no gradient. (Fully learned pair.)
  • : . The indifference / initialization baseline.
  • : dominates, , growing linearly in . Confidently wrong pairs incur unbounded, linearly-growing loss — DPO punishes confident mistakes hard but not explosively (linear, not exponential), keeping training stable.
Recall Solution

If then and likewise for , so exactly. Margin , loss , for every and every parameter setting. Why the gradient is zero. From L3.2 the parameter gradient of a pair is Here two things both hit zero. First, the weight is (finite). Second, and decisively, since the two log-prob terms are the same function, so their gradients are identical and . The product is . So the pair contributes no learning signal but still counts toward the batch loss, dragging the reported average toward and wasting compute. Such pairs should be filtered out.

Recall Solution

Both margins equal , so both give the same loss and the same gradient weight . Pair A gives and Pair B gives — literally the same input, hence identical. Why DPO treats them the same. The loss depends only on the difference , never on the absolute reward levels and versus and . This is by design: absolute values carry the arbitrary offset that cancels in the difference (L3.1 / L4.3). So only the relative preference strength matters — exactly what the Bradley–Terry model encodes. Pair A's larger absolute rewards are completely invisible to the objective; a pair is "learned" based on how much the winner beats the loser, not on how large either raw score is.

Recall Solution

is a strictly decreasing function of (since is strictly increasing and is decreasing). At , . Therefore:

  • (model prefers winner ⇔ loss below baseline).
  • (model prefers loser ⇔ loss above baseline).

The equivalence is exact because is strictly monotone, so it crosses exactly once, at .


Active recall

Recall Quick self-test (reveal after answering)
  1. What margin gives loss exactly ?
  2. As , how fast does the loss grow?
  3. Why is the gradient weight large on wrong examples?
  4. Does DPO loss depend on absolute reward or only the difference?
  5. What happens to deviation when increases?

Answers: 1) . 2) Linearly, as . 3) Weight , near 1 when the loser is ranked above the winner. 4) Only the difference . 5) Deviation decreases (tighter leash).

Return to the parent: Direct Preference Optimization (DPO).