Simple Algorithm
Artificial Intelligence · Deep Layer · 30 min

RLHF & Preference Optimization

Reward models, PPO and direct preference optimization.

Every method in this family, from PPO-based RLHF to DPO and its variants, approximates one object: the reference policy tilted by a learned reward, π∗∝πrefer/β\pi^*\propto\pi_{\text{ref}}e^{r/\beta}. Knowing that object exactly tells you what each method can and cannot do, and why all of them inherit the flaws of the reward.

What you’ll learn

  • State the Bradley–Terry and Plackett–Luce preference models, what they identify, and how fast a reward model learns from comparisons.
  • Prove the Gibbs variational principle for KL-regularized reward maximization and the concavity of the reward–KL frontier.
  • Write the token-level PPO objective used for language models, and derive DPO's reparametrization, gradient and failure modes.
  • Compute the KL cost of best-of-nn sampling, and state the empirical laws of reward-model overoptimization.

Preference models and what they identify

Statistically, rewards are learned at a parametric rate. For a linear reward rθ(x,y)=θ⊤ψ(x,y)r_\theta(x,y)=\theta^\top\psi(x,y) in dd dimensions, the negative log-likelihood is convex, and the maximum-likelihood estimate's error, measured in the norm induced by the covariance of the feature differences ψ(x,yw)−ψ(x,yl)\psi(x,y_w)-\psi(x,y_l), is O(d/n)O\big(\sqrt{d/n}\big) (Zhu, Jordan & Jiao, 2023). The constant grows exponentially with the range of rewards, because confident comparisons, where σ′(Δ)\sigma'(\Delta) is small, carry little information.

A bound in that norm says nothing about directions the data never covered, which is precisely where an optimized policy goes. Zhu et al. therefore optimize a pessimistic reward, a lower confidence bound, to obtain guarantees for the policy itself.

Think first Three responses have rewards 22, 11 and 00. Under Plackett–Luce, what is the probability that a labeler ranks them in exactly that order?

e2e2+e+1⋅ee+1=0.665×0.731≈0.49\frac{e^2}{e^2+e+1}\cdot\frac{e}{e+1}=0.665\times0.731\approx0.49. The last factor is 1. Even with clear reward gaps, the "correct" full ranking is less likely than a coin flip, so rankings carry far more noise than their length suggests.

The KL-regularized optimum

DerivationProof, and the shape of the frontier
  1. DKL(π ∥ π∗)=DKL(π ∥ πref)−1β Eπ[r]+log⁡Z\KL(\pi\,\|\,\pi^*)=\KL(\pi\,\|\,\pi_{\text{ref}})-\tfrac1\beta\,\E_\pi[r]+\log Z

    Policies with DKL(π∥πref)=∞\KL(\pi\|\pi_{\text{ref}})=\infty cannot be optimal, so take π≪πref\pi\ll\pi_{\text{ref}} and expand DKL(π∥π∗)\KL(\pi\|\pi^*) using log⁡π∗=log⁡πref+r/β−log⁡Z\log\pi^*=\log\pi_{\text{ref}}+r/\beta-\log Z:

  2. Eπ[r]−β DKL(π ∥ πref)=βlog⁡Z−β DKL(π ∥ π∗) ≤ βlog⁡Z\E_\pi[r]-\beta\,\KL(\pi\,\|\,\pi_{\text{ref}})=\beta\log Z-\beta\,\KL(\pi\,\|\,\pi^*)\ \le\ \beta\log Z

    Rearrange. Gibbs' inequality, DKL≥0\KL\ge0 with equality iff the distributions coincide, finishes the proof:

  3. R′(K)=β,K(β)=DKL(πβ∗ ∥ πref)=1β Eπβ∗[r]−log⁡Z(β)R'(K)=\beta,\qquad K(\beta)=\KL(\pi^*_\beta\,\|\,\pi_{\text{ref}})=\tfrac1\beta\,\E_{\pi^*_\beta}[r]-\log Z(\beta)

    Dually, let R(K)=sup⁡{Eπr:DKL(π∥πref)≤K}R(K)=\sup\{\E_\pi r:\KL(\pi\|\pi_{\text{ref}})\le K\}. KL is convex in π\pi and Eπr\E_\pi r is linear, so a mixture of two feasible policies is feasible for the mixed budget and earns the mixed reward: RR is concave and nondecreasing. The tilted family πβ∗\pi^*_\beta traces it, with β\beta as the Lagrange multiplier, that is, the slope:

  4. Diminishing returns are built in: each extra nat of divergence buys less reward, even of the proxy reward being optimized. Jensen's inequality places the optimal value βlog⁡Z\beta\log Z, a soft maximum of rr, between Eπref[r]\E_{\pi_{\text{ref}}}[r] and max⁡yr(y)\max_yr(y).

Two readings of the same formula. As inference, π∗\pi^* is a Bayesian posterior with prior πref\pi_{\text{ref}} and likelihood er/βe^{r/\beta} for an "optimality" event, so RLHF is variational inference, and the KL term is what stops it from collapsing onto the reward's maximum (Korbak, Perez & Buckley, 2022).

As control, the sequence-level problem is a token-level MDP with KL regularization at every step, and its optimum factorizes as π∗(yt∣st)∝πref(yt∣st)exp⁡(Q∗(st,yt)/β)\pi^*(y_t\mid s_t)\propto\pi_{\text{ref}}(y_t\mid s_t)\exp\big(Q^*(s_t,y_t)/\beta\big) with a soft Q-function over prefixes st=(x,y<t)s_t=(x,y_{<t}). A language model trained toward π∗\pi^* implicitly represents a value function (Rafailov et al., 2024).

Think first Sampling at temperature T<1T<1 computes q∝p1/Tq\propto p^{1/T} for each next token. For which reward is that a KL-regularized optimum, and is per-token temperature the same as tilting whole sequences?

With r(y)=log⁡πref(y)r(y)=\log\pi_{\text{ref}}(y), π∗∝πref1+1/β\pi^*\propto\pi_{\text{ref}}^{1+1/\beta}, so T=β/(1+β)T=\beta/(1+\beta), exactly, for a single next-token distribution. Over sequences the tilt gives the "power distribution" πref(y)1/T/Z\pi_{\text{ref}}(y)^{1/T}/Z, but per-token sampling produces ∏tπref(yt∣y<t)1/T/Zt(y<t)\prod_t\pi_{\text{ref}}(y_t\mid y_{<t})^{1/T}/Z_t(y_{<t}), and the normalizers ZtZ_t depend on the prefix. The two differ: sharpening each step is not sharpening the sequence.

PPO for language models

Treat generation as an MDP: the state is st=(x,y<t)s_t=(x,y_{<t}), the action is the next token, transitions are deterministic, and the episode ends at an end-of-sequence token. The reward is sparse, plus a per-token KL term (Ziegler et al., 2019; Stiennon et al., 2020):

Rt=−βlog⁡πθ(yt∣st)πref(yt∣st)+1[t=T]  rϕ(x,y),∑t=1TRt=rϕ(x,y)−βlog⁡πθ(y∣x)πref(y∣x)R_t=-\beta\log\frac{\pi_\theta(y_t\mid s_t)}{\pi_{\text{ref}}(y_t\mid s_t)}+\mathbf 1[t=T]\;r_\phi(x,y),\qquad\sum_{t=1}^{T}R_t=r_\phi(x,y)-\beta\log\frac{\pi_\theta(y\mid x)}{\pi_{\text{ref}}(y\mid x)}
The returns telescope to the sequence-level objective, whose expectation under πθ\pi_\theta is E[rϕ]−β DKL(πθ∥πref)\E[r_\phi]-\beta\,\KL(\pi_\theta\|\pi_{\text{ref}}). A single sample's log-ratio can be negative; only its expectation is a divergence.

PPO (Schulman et al., 2017) maximizes a clipped surrogate on minibatches of tokens generated by the previous policy πold\pi_{\text{old}}, with advantages A^t\hat A_t from generalized advantage estimation (Schulman et al., 2016) and a learned value head; InstructGPT initialized the value function from the reward model:

LCLIP(θ)=−Et[min⁡(ρtA^t, clip⁡(ρt,1−ϵ,1+ϵ) A^t)],ρt=πθ(yt∣st)πold(yt∣st)\loss^{\text{CLIP}}(\theta)=-\E_t\Big[\min\big(\rho_t\hat A_t,\ \operatorname{clip}(\rho_t,1-\epsilon,1+\epsilon)\,\hat A_t\big)\Big],\qquad\rho_t=\frac{\pi_\theta(y_t\mid s_t)}{\pi_{\text{old}}(y_t\mid s_t)}
Clipping removes the incentive to move ρt\rho_t outside [1−ϵ,1+ϵ][1-\epsilon,1+\epsilon], a cheap stand-in for a trust region (Trust Regions: TRPO & PPO). InstructGPT added a pretraining log-likelihood term ("PPO-ptx") to limit regressions on public NLP benchmarks.
MethodSamples while trainingRewardValue networkNotes
PPO (Ouyang et al., 2022)yes, on-policylearned reward modelyesfour networks in memory; many hyperparameters
RLOO (Ahmadian et al., 2024); GRPO (Shao et al., 2024)yes, kk responses per promptreward model or verifiernobaseline from the other samples: A^i=ri−1k−1∑j≠irj\hat A_i=r_i-\frac1{k-1}\sum_{j\ne i}r_j in RLOO, group-normalized rewards in GRPO
Best-of-nn, rejection samplingyesreward modelnokeep the best of nn; use at inference, or fine-tune on it and repeat (expert iteration)
DPO (Rafailov et al., 2023)no, offline pairsimplicit, βlog⁡(πθ/πref)\beta\log(\pi_\theta/\pi_{\text{ref}})noa supervised loss; next step
Verifiable rewards and GRPO's use in training reasoning models are covered in Reasoning in Language Models.
Think first RLOO's baseline for sample ii averages the other k−1k-1 rewards. Why exclude rir_i?

A baseline may depend on anything except the action it multiplies, because Eyi[∇log⁡πθ(yi∣x)]=∑y∇πθ(y∣x)=∇1=0\E_{y_i}\big[\nabla\log\pi_\theta(y_i\mid x)\big]=\sum_y\nabla\pi_\theta(y\mid x)=\nabla1=0. The leave-one-out mean is independent of yiy_i, so the estimator stays unbiased. Including rir_i correlates baseline and action and scales the expected gradient by (k−1)/k(k-1)/k: harmless in direction, but a bias all the same.

Direct preference optimization, precisely

So minimizing LDPO\loss_{\text{DPO}} is maximum-likelihood estimation of a Bradley–Terry reward within the class {βlog⁡(πθ/πref)}\{\beta\log(\pi_\theta/\pi_{\text{ref}})\}, and when that class is rich enough, the population minimizer is the KL-optimal policy for the true reward. The gradient (derived in Fine-Tuning, RLHF & Prompting) raises the chosen response's log-probability and lowers the rejected one's, weighted by σ(r^θ(x,yl)−r^θ(x,yw))\sigma\big(\hat r_\theta(x,y_l)-\hat r_\theta(x,y_w)\big), the implicit reward model's probability of getting the pair wrong.

Predict before you look

DPO with β=0.1\beta=0.1: training has raised log⁡πθ(yw∣x)\log\pi_\theta(y_w\mid x) by 2 nats relative to the reference and lowered log⁡πθ(yl∣x)\log\pi_\theta(y_l\mid x) by 3 nats. What probability does the implicit reward model assign to yw≻yly_w\succ y_l, in percent?

Show the answer

62 %

The implicit reward margin is β(2−(−3))=0.5\beta\big(2-(-3)\big)=0.5, and σ(0.5)≈0.622\sigma(0.5)\approx0.622; this pair's loss is −log⁡0.622≈0.47-\log0.622\approx0.47. Shifts of several nats in log-probability buy only a modest preference probability at small β\beta, which is why DPO moves log-probabilities a long way.

Three documented failure modes follow from the derivation:

  • Deterministic preferences. When the data decide every pair the same way, the Bradley–Terry likelihood keeps improving as the margin grows, which drives πθ(yl∣x)/πref(yl∣x)→0\pi_\theta(y_l\mid x)/\pi_{\text{ref}}(y_l\mid x)\to0 whatever β\beta is: on finite data the KL regularization stops binding. IPO (Azar et al., 2024) replaces −log⁡σ(⋅)-\log\sigma(\cdot) by a squared loss that regresses the log-ratio margin to a finite target 1/(2τ)1/(2\tau), with τ\tau playing the role of β\beta.
  • Offline coverage. The loss sees only the dataset's responses. It can lower the probability of both ywy_w and yly_l while mass moves to responses never shown, a widely reported effect (e.g., Pal et al., 2024), and it cannot correct the current policy's errors on responses the data lack. Iterated or online DPO, which relabels fresh samples, addresses this.
  • Length. Sequence log-ratios scale with length, and preference data often favor longer answers; length-normalized, reference-free variants such as SimPO (Meng et al., 2024) respond to this.

Is offline preference optimization as good as on-policy RL? Careful comparisons found well-tuned PPO ahead on harder tasks such as code generation, and on-policy samples helpful in general (Xu et al., 2024; Tajwar et al., 2024), while several openly documented pipelines, such as Llama 3's (2024), ran DPO in rounds on freshly sampled, reward-ranked responses. As of the mid-2020s the question is contested and probably task-dependent.

Overoptimization, sycophancy and AI feedback

DerivationProof
  1. πbon(y)πref(y)=n F(r(y))n−1\frac{\pi_{\text{bon}}(y)}{\pi_{\text{ref}}(y)}=n\,F\big(r(y)\big)^{n-1}

    Let FF be the CDF of r(Y)r(Y) under πref\pi_{\text{ref}}, so that U=F(r(Y))U=F\big(r(Y)\big) is uniform on [0,1][0,1]. The best of nn has the largest UU, whose density is nun−1nu^{n-1}. With no ties, y↦Uy\mapsto U is one-to-one, so the likelihood ratio of responses is that of UU:

  2. DKL(πbon ∥ πref)=log⁡n+(n−1) E[log⁡Umax⁡]=log⁡n−n−1n\KL(\pi_{\text{bon}}\,\|\,\pi_{\text{ref}})=\log n+(n-1)\,\E[\log U_{\max}]=\log n-\frac{n-1}{n}

    Take the expectation of its logarithm under πbon\pi_{\text{bon}}, where Umax⁡∼Beta(n,1)U_{\max}\sim\mathrm{Beta}(n,1) and E[log⁡Umax⁡]=∫01nun−1log⁡u du=−1n\E[\log U_{\max}]=\int_0^1nu^{n-1}\log u\,du=-\frac1n:

  3. The divergence grows only logarithmically: best-of-1,000 costs about 5.9 nats. Best-of-nn is a gentle optimizer, which makes it a useful yardstick for how far RL has moved a policy.

Predict before you look

Using the theorem, what is DKL(πbon∥πref)\KL(\pi_{\text{bon}}\|\pi_{\text{ref}}) for best-of-16, in nats?

Show the answer

1.84 nats

log⁡16−1516=2.773−0.938=1.835\log16-\frac{15}{16}=2.773-0.938=1.835 nats, so d=DKL≈1.35d=\sqrt{\KL}\approx1.35 on the axis Gao et al. used. Doubling nn adds only about log⁡2≈0.69\log2\approx0.69 nats.

Gao, Schulman & Hilton (2023) measured overoptimization in a synthetic setup: a large "gold" reward model stands in for human raters, and proxy reward models are trained on its labels. With d=DKL(π∥πref)d=\sqrt{\KL(\pi\|\pi_{\text{ref}})}, the gold reward of the optimized policy fit

Rbon(d)=d (αbon−βbon d),RRL(d)=d (αRL−βRLlog⁡d)R_{\text{bon}}(d)=d\,\big(\alpha_{\text{bon}}-\beta_{\text{bon}}\,d\big),\qquad R_{\text{RL}}(d)=d\,\big(\alpha_{\text{RL}}-\beta_{\text{RL}}\log d\big)
Here α\alpha and β\beta are fitted coefficients, not the KL weight. The proxy reward rises monotonically while the gold reward peaks and falls; for best-of-nn the peak is at d∗=αbon/(2βbon)d^*=\alpha_{\text{bon}}/(2\beta_{\text{bon}}). The coefficients varied smoothly with reward-model size, larger reward models overoptimizing less. These are empirical fits in one setup, not laws of nature.

Why does true quality fall? Regressional Goodhart: if proxy == true reward ++ independent noise, selecting high proxy scores also selects noise; in the Gaussian case the expected true reward given a proxy value pp is shrunk toward the mean, E[r∣p]=Var⁡rVar⁡r+Var⁡ϵ p\E[r\mid p]=\frac{\Var r}{\Var r+\Var\epsilon}\,p for centered variables. Extremal Goodhart: far from the training distribution the proxy's errors stop being small noise and become systematic, and an optimizer seeks them out.

Observed symptoms include reward that tracks length itself (Singhal et al., 2023) and sycophancy, telling users what they appear to want: it increased with model size and RLHF in model-written evaluations (Perez et al., 2023), and human preference data, and preference models trained on them, sometimes favor convincing sycophantic answers over correct ones (Sharma et al., 2024). Mitigations include reward-model ensembles and uncertainty penalties (Coste et al., 2024), held-out gold evaluations and early stopping on KL; see Specification Gaming & Reward Hacking.

AI feedback. Constitutional AI (Bai et al., 2022) replaces human harmlessness labels with model judgments guided by a short written list of principles: a supervised stage in which the model critiques and revises its own responses, then RL against a preference model trained on AI-chosen comparisons (RLAIF). Lee et al. (2023) reported RLAIF performing comparably to RLHF on summarization and dialogue. AI feedback scales and is auditable through its principles, but it imports the labeling model's own biases, such as a preference for its own style.

Key takeaways

  • Preference models identify rewards only up to a per-prompt shift, and the shift changes neither the comparisons nor the KL-optimal policy.
  • Gibbs variational principle: max⁡πEπr−β DKL(π∥πref)=βlog⁡Z\max_\pi\E_\pi r-\beta\,\KL(\pi\|\pi_{\text{ref}})=\beta\log Z, attained only by π∗∝πrefer/β\pi^*\propto\pi_{\text{ref}}e^{r/\beta}; the reward–KL frontier is concave with slope β\beta.
  • PPO optimizes the token-level problem with a per-token KL penalty and a value head; RLOO and GRPO drop the value network; DPO reparametrizes the reward by the policy and needs no sampling, at the cost of offline coverage and unbounded margins on deterministic data.
  • Best-of-nn costs exactly log⁡n−n−1n\log n-\frac{n-1}n nats without ties. Under a proxy reward, true reward peaks and then falls as KL grows (Gao et al., 2023); length bias and sycophancy are common symptoms.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What is the maximum over policies of Eπ[r]−β DKL(π∥πref)\E_\pi[r]-\beta\,\KL(\pi\|\pi_{\text{ref}})?

  1. max⁡yr(y)\max_yr(y)
  2. Eπref[r]\E_{\pi_{\text{ref}}}[r]
  3. βlog⁡Eπref[er/β]\beta\log\E_{\pi_{\text{ref}}}\big[e^{r/\beta}\big]
  4. β Eπref[er/β]\beta\,\E_{\pi_{\text{ref}}}\big[e^{r/\beta}\big]
Show the answer

C. βlog⁡Eπref[er/β]\beta\log\E_{\pi_{\text{ref}}}\big[e^{r/\beta}\big]

By the Gibbs variational principle the value is βlog⁡Z\beta\log Z, a soft maximum of rr that lies between Eπref[r]\E_{\pi_{\text{ref}}}[r] (by Jensen) and max⁡yr\max_yr, tending to them as β→∞\beta\to\infty and β→0\beta\to0.

Question 2

A policy returns the highest-reward response among 20 independent samples from πref\pi_{\text{ref}}, with no ties in reward. What is its KL divergence from πref\pi_{\text{ref}}?

  1. log⁡20≈3.00\log20\approx3.00 nats
  2. log⁡20−1920≈2.05\log20-\frac{19}{20}\approx2.05 nats
  3. 1920=0.95\frac{19}{20}=0.95 nats
  4. 1919 nats
Show the answer

B. log⁡20−1920≈2.05\log20-\frac{19}{20}\approx2.05 nats

DKL(πbon∥πref)=log⁡n−n−1n\KL(\pi_{\text{bon}}\|\pi_{\text{ref}})=\log n-\frac{n-1}n. The likelihood ratio nF(r)n−1nF(r)^{n-1} is at most nn, so log⁡n\log n is only an upper bound; the exact value subtracts (n−1)/n(n-1)/n.

Question 3

Every pair in a preference dataset appears exactly once, so each comparison is decided one way in the data. What does the DPO objective do as training continues, whatever β\beta is?

  1. It converges to the KL-optimal policy for the true reward
  2. It stops once the margin reaches a value set by β\beta
  3. It raises the probability of both responses in each pair
  4. It keeps pushing πθ(yl∣x)/πref(yl∣x)\pi_\theta(y_l\mid x)/\pi_{\text{ref}}(y_l\mid x) down, because −log⁡σ(m)-\log\sigma(m) decreases for every finite margin mm
Show the answer

D. It keeps pushing πθ(yl∣x)/πref(yl∣x)\pi_\theta(y_l\mid x)/\pi_{\text{ref}}(y_l\mid x) down, because −log⁡σ(m)-\log\sigma(m) decreases for every finite margin mm

−log⁡σ(m)→0-\log\sigma(m)\to0 only as m→∞m\to\infty, so on such data empirical DPO has no finite minimizer, and the KL regularization, present only implicitly through the parametrization, stops binding (Azar et al., 2024). Early stopping, IPO's squared loss or label smoothing restore a finite target.

Question 4

During PPO-based RLHF, a sampled response's summed KL term −β∑tlog⁡πθ(yt∣st)πref(yt∣st)-\beta\sum_t\log\frac{\pi_\theta(y_t\mid s_t)}{\pi_{\text{ref}}(y_t\mid s_t)} comes out positive. What does that mean?

  1. Nothing is wrong: it is a one-sample estimate of −β DKL-\beta\,\KL, and individual samples can have either sign
  2. The KL divergence has become negative, so there is a bug
  3. The policy has collapsed back onto the reference
  4. The reward model is being overoptimized
Show the answer

A. Nothing is wrong: it is a one-sample estimate of −β DKL-\beta\,\KL, and individual samples can have either sign

For this response πref(y∣x)>πθ(y∣x)\pi_{\text{ref}}(y\mid x)>\pi_\theta(y\mid x), which happens for some responses whenever the two policies differ. Only the expectation DKL(πθ∥πref)≥0\KL(\pi_\theta\|\pi_{\text{ref}})\ge0 is a divergence; some implementations use lower-variance estimators that are non-negative for every sample.

Further reading

  • P. F. Christiano, J. Leike, T. B. Brown, M. Martic, S. Legg & D. Amodei, "Deep reinforcement learning from human preferences," NeurIPS (2017).
  • L. Ouyang et al., "Training language models to follow instructions with human feedback," NeurIPS (2022).
  • R. Rafailov, A. Sharma, E. Mitchell, S. Ermon, C. D. Manning & C. Finn, "Direct preference optimization: Your language model is secretly a reward model," NeurIPS (2023).
  • L. Gao, J. Schulman & J. Hilton, "Scaling laws for reward model overoptimization," ICML (2023).
  • S. Casper et al., "Open problems and fundamental limitations of reinforcement learning from human feedback," Transactions on Machine Learning Research (2023).