Grimoire
Sheet
pathtraining/objectives
difficultyadvanced
timeO(d)
described2023
revised1w ago

Direct Preference Optimisation

The KL-regularised RL objective has a closed-form optimum. Substitute it back into the Bradley–Terry likelihood and the reward model disappears, leaving a classification loss on preference pairs.

Theory

RLHF maximises expected reward under a KL penalty toward a reference policy. That problem is solved analytically by a Boltzmann tilt of the reference, which means any reward function can be written in terms of the optimal policy it induces.

π(yx)=1Z(x)πref(yx)exp ⁣(r(x,y)β)\pi^{*}(y \mid x) = \frac{1}{Z(x)}\, \pi_{\text{ref}}(y \mid x) \exp\!\left( \frac{r(x, y)}{\beta} \right)
eq. 1 — optimum of the KL-regularised objective

Inverting eq. 1 gives r(x,y)=βlogπ(yx)πref(yx)+βlogZ(x)r(x,y) = \beta \log \frac{\pi(y \mid x)}{\pi_{\text{ref}}(y \mid x)} + \beta \log Z(x). Under Bradley–Terry only reward differences within a prompt matter, so the intractable partition function cancels and the preference likelihood becomes a logistic loss on the implicit reward margin.

LDPO=Elogσ ⁣(βlogπ(ywx)πref(ywx)βlogπ(ylx)πref(ylx))L_{\text{DPO}} = -\,\E \log \sigma\!\left( \beta \log \frac{\pi(y_w \mid x)}{\pi_{\text{ref}}(y_w \mid x)} - \beta \log \frac{\pi(y_l \mid x)}{\pi_{\text{ref}}(y_l \mid x)} \right)
eq. 2Rafailov et al. §4

β\beta is not a learning rate. It sets how far the policy is allowed to move from the reference, and it is the only handle on the failure mode where both chosen and rejected log-probabilities fall together while the margin technically improves.

Implementation
python · torch ≥ 2.1
import torch.nn.functional as F
from torch import Tensor


def dpo_loss(
    pi_chosen: Tensor, pi_rejected: Tensor,      # policy logprobs, summed
    ref_chosen: Tensor, ref_rejected: Tensor,    # reference logprobs, frozen
    beta: float = 0.1,
) -> Tensor:
    chosen_logratio = pi_chosen - ref_chosen
    rejected_logratio = pi_rejected - ref_rejected
    margin = beta * (chosen_logratio - rejected_logratio)
    return -F.logsigmoid(margin).mean()

The log-probabilities must be summed over completion tokens only, with the prompt masked out — averaging instead silently reweights the objective by completion length and biases the policy toward short answers. Track chosen and rejected log-ratios separately during training: a healthy run raises the margin, an unhealthy one lowers both terms and widens the gap by falling more slowly on the chosen side.

Related
References
[1]Rafailov et al. — Direct Preference Optimization (2023)arXiv:2305.18290
[2]Ziegler et al. — Fine-Tuning Language Models from Human Preferences (2019)arXiv:1909.08593
[3]Azar et al. — A General Theoretical Paradigm to Understand Learning from Preferences (2023)arXiv:2310.12036