4.4.4Alignment, Prompting & RAG

Direct Preference Optimization (DPO)

2,125 words10 min readdifficulty · medium5 backlinks

WHY does DPO exist?


The setup (WHAT are the pieces)


DERIVATION from first principles

We derive DPO in three moves. Follow every "Why this step?".

Step 1 — The RLHF objective

RLHF maximizes expected reward while staying close to the reference model:

maxπθ Ex,yπθ[r(x,y)]    βKL ⁣(πθ(yx)πref(yx))\max_{\pi_\theta}\ \mathbb{E}_{x,\,y\sim\pi_\theta}\big[r(x,y)\big]\;-\;\beta\,\mathrm{KL}\!\big(\pi_\theta(y|x)\,\|\,\pi_{\text{ref}}(y|x)\big)

Why this step? The reward term pushes toward high-quality answers; the KL term is a leash preventing the model from producing gibberish that games the reward (reward hacking).

Step 2 — Solve for the OPTIMAL policy

This objective has a known closed-form optimum (a standard result in KL-regularized reward maximization):

π(yx)  =  1Z(x)πref(yx)exp ⁣(1βr(x,y))\pi^*(y|x) \;=\; \frac{1}{Z(x)}\,\pi_{\text{ref}}(y|x)\,\exp\!\Big(\tfrac{1}{\beta}\,r(x,y)\Big)

where Z(x)=yπref(yx)exp(1βr(x,y))Z(x)=\sum_y \pi_{\text{ref}}(y|x)\exp(\tfrac1\beta r(x,y)) is the partition function (normalizer).

Why this step? To turn "given reward, best policy" into "given policy, implied reward." Z(x)Z(x) is intractable (sums over all sequences) — that's the villain we must eliminate.

Step 3 — Invert to express the reward via the policy

Take logs of Step 2 and solve for rr:

r(x,y)  =  βlogπ(yx)πref(yx)  +  βlogZ(x)r(x,y) \;=\; \beta\,\log\frac{\pi^*(y|x)}{\pi_{\text{ref}}(y|x)} \;+\; \beta\,\log Z(x)

Why this step? Now the reward is written in terms of the policy. The messy Z(x)Z(x) term depends only on xx, not on yy — remember that.

Step 4 — Plug into the Bradley–Terry preference model

Human preferences are modeled with Bradley–Terry: the probability that ywy_w beats yly_l is

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

where σ\sigma is the sigmoid. Substitute the reward from Step 3:

r(x,yw)r(x,yl)=βlogπ(ywx)πref(ywx)βlogπ(ylx)πref(ylx)+βlogZ(x)βlogZ(x)=0r(x,y_w)-r(x,y_l) = \beta\log\frac{\pi^*(y_w|x)}{\pi_{\text{ref}}(y_w|x)} - \beta\log\frac{\pi^*(y_l|x)}{\pi_{\text{ref}}(y_l|x)} + \underbrace{\beta\log Z(x) - \beta\log Z(x)}_{=\,0}

Why this step? This is the magic. Because we take a difference of rewards for the same prompt xx, the intractable logZ(x)\log Z(x) cancels. No reward model, no partition function.

Step 5 — Write the loss

Replace π\pi^* with our trainable πθ\pi_\theta and maximize the log-likelihood of observed preferences (equivalently minimize):

Figure — Direct Preference Optimization (DPO)

HOW does the gradient behave? (intuition check)

The gradient is:

θL=βE[σ(r^lr^w)weight(θlogπθ(ywx)θlogπθ(ylx))]\nabla_\theta\mathcal{L} = -\beta\,\mathbb{E}\Big[\underbrace{\sigma(\hat r_l-\hat r_w)}_{\text{weight}}\big(\nabla_\theta\log\pi_\theta(y_w|x) - \nabla_\theta\log\pi_\theta(y_l|x)\big)\Big]


Worked examples


Common mistakes (Steel-manned)


Active recall

Recall Test yourself (reveal after answering)
  1. Why does the partition function Z(x)Z(x) disappear?
  2. What is the "implicit reward" in DPO?
  3. What does increasing β\beta do to model deviation?
  4. What loss value occurs at initialization (πθ=πref\pi_\theta=\pi_{\text{ref}})?
  5. Which four models does PPO-RLHF need that DPO replaces with two?

Answers: 1) It only depends on xx; taking a difference of rewards for the same xx cancels it. 2) r^θ=βlog(πθ/πref)\hat r_\theta=\beta\log(\pi_\theta/\pi_{\text{ref}}). 3) Reduces deviation (tighter KL leash). 4) log20.693\log 2\approx0.693. 5) PPO needs policy+reference+reward+value; DPO needs only policy+reference.

Recall Feynman: explain to a 12-year-old

Imagine teaching a robot to answer nicely. The old way: hire a judge robot to score every answer, then train the answerer to chase high scores — clumsy and the answerer learns to trick the judge. DPO says: skip the judge! Just show the robot pairs — "this answer is good, this one is bad" — and nudge it to make the good one more likely and the bad one less likely, but only compared to how it used to talk, so it doesn't go crazy. The robot ends up being its own judge. Simple, and no cheating.


Connections

DPO stands for
Direct Preference Optimization
Two models DPO trains/uses
The trainable policy πθ\pi_\theta and the frozen reference πref\pi_{\text{ref}} (the SFT checkpoint)
DPO loss formula
E[logσ(βlogπθ(ywx)πref(ywx)βlogπθ(ylx)πref(ylx))]-\mathbb{E}[\log\sigma(\beta\log\frac{\pi_\theta(y_w|x)}{\pi_{ref}(y_w|x)} - \beta\log\frac{\pi_\theta(y_l|x)}{\pi_{ref}(y_l|x)})]
Why does the partition function Z(x) cancel in DPO
Because the Bradley-Terry model uses a difference of rewards for the same prompt x, and βlogZ(x)\beta\log Z(x) depends only on x
DPO implicit reward
r^θ(x,y)=βlogπθ(yx)πref(yx)\hat r_\theta(x,y)=\beta\log\frac{\pi_\theta(y|x)}{\pi_{ref}(y|x)}
Effect of increasing β in DPO
Tightens the KL leash → the policy deviates LESS from the reference model
DPO loss at initialization (π_θ = π_ref)
log20.693\log 2 \approx 0.693 per example (coin-flip baseline)
Closed-form optimal RLHF policy
π(yx)=1Z(x)πref(yx)exp(1βr(x,y))\pi^*(y|x)=\frac{1}{Z(x)}\pi_{ref}(y|x)\exp(\frac1\beta r(x,y))
Preference model DPO assumes
Bradley-Terry: P(ywyl)=σ(r(x,yw)r(x,yl))P(y_w\succ y_l)=\sigma(r(x,y_w)-r(x,y_l))
What data does DPO need
Triples (x,yw,yl)(x, y_w, y_l): prompt, chosen (winning) response, rejected (losing) response
Why keep the reference model
Its ratio prevents likelihood collapse/degeneration and enables Z(x) cancellation; dropping it rewards raw high-frequency text
Which RLHF component does DPO eliminate
The separate reward-model training stage and the PPO/RL optimization loop

Concept Map

is unstable and costly

motivates

has closed-form optimum

invert and take logs

Z of x depends only on x

plug into

enables

becomes

trains

frozen leash via beta

supervises

policy IS the reward model

RLHF 3-stage pipeline

PPO problems

Direct Preference Optimization

KL-regularized reward objective

Optimal policy pi*

Reward via policy ratio

Partition function cancels

Bradley-Terry model

Simple classification loss

Policy pi-theta

Reference model pi-ref

Preference data x, y_w, y_l

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, RLHF ka purana tareeka thoda painful hai: pehle ek reward model train karo jo har answer ko score de, phir PPO (ek RL algorithm) se policy ko us score ke peeche bhagao, aur saath mein KL penalty lagao taaki model gibberish na banane lage. Iske liye ek saath 4 models memory mein chahiye aur PPO tuning karna sar-dard hai. DPO bolta hai — yeh sab drama chhodo!

DPO ka core insight yeh hai: KL-regularized reward maximization ka ek closed-form optimal solution hota hai. Us formula ko ulta karke reward ko policy ke terms mein likh lo: r^=βlog(πθ/πref)\hat r = \beta\log(\pi_\theta/\pi_{ref}). Ab jab hum Bradley-Terry preference model mein winner aur loser ke reward ka difference lete hain, toh woh dushman jaisa partition function Z(x)Z(x) automatically cancel ho jaata hai (kyunki dono same prompt xx ke hain). Result: ek simple logistic (sigmoid) loss — bas winner ywy_w ki likelihood badhao aur loser yly_l ki ghatao, reference model ke comparison mein.

β\beta ko galat mat samjho — yeh brake hai, accelerator nahi. Bada β\beta matlab model reference se kam hatega (tight leash); chhota β\beta matlab zyada freedom. Aur ek cheez yaad rakhna: reference model ko mat hatao — uska ratio hi model ko degenerate hone se rokta hai aur Z(x)Z(x) ko cancel karne mein help karta hai.

Kyun important hai? Kyunki DPO ne alignment ko itna simple aur stable bana diya — bas preference pairs ka data lo aur ek supervised loss chala do, koi reward model nahi, koi RL nahi. Isiliye aaj-kal open-source models (jaise Zephyr, Llama variants) DPO se align hote hain. 80/20 rule: yeh derivation (Z cancel + implicit reward) samajh gaye toh poora DPO samajh gaye.

Go deeper — visual, from zero

Test yourself — Alignment, Prompting & RAG

Connections