What DPO is
RLHF trains a reward model on preference pairs, then runs reinforcement learning against it, sampling answers and fitting a value model as it goes (see the RLHF tab). Direct preference optimisation (DPO) gets the same kind of result from the pairs alone, with an ordinary loss and no sampling. Each training example is a prompt , a chosen answer and a rejected answer . The loss raises the policy's probability of and lowers that of , both measured relative to a frozen copy of the starting model, the reference .
This page runs ten real updates of TRL's DPOTrainer on SmolLM2-135M-Instruct, with and a learning rate of , on eight preference pairs from UltraFeedback's test split. These are single-turn rows with prompts of at most 40 tokens, answers of at most 70 and a GPT-4 score gap of at least 2. All eight pairs go into every step.
Four log-probabilities
The loss needs four numbers per pair, and the Four log-probabilities → loss panel shows them: and of each answer, each the sum of its tokens' log-probabilities given the prompt and the tokens before. The chat template wraps the prompt in a system line, the user turn and the assistant header; those tokens are context only. The implicit reward of an answer is times its log-ratio, the margin is , is the modelled probability that the chosen answer wins, and the pair's loss is . At step 0 the policy is the reference, so every log-ratio is 0 and every pair's loss is .
The Preference pair panel shades each answer's tokens by their own log-ratio: red where the policy now gives the token more probability than the reference did, blue where it gives less. One token, before and after opens a single position and compares the reference's five most likely next tokens with the trained policy's.
How hard each pair pushes
Write and for the two implicit rewards. The gradient of the loss is
It raises the chosen answer and lowers the rejected one, weighted by : a pair the policy already ranks well pushes less, and at step 0 every pair has weight ½. The How hard this pair pushes line shows that weight (divided by the 8 pairs of the batch mean). Within a pair, the loss asks for the same rise in every chosen token's log-probability and the same fall in every rejected one's. Through the shared weights the tokens actually move by different amounts, and a few move the wrong way. Without the per-pair weight, the paper notes, the update can degenerate.
Ten updates
The strip follows the run. The batch loss falls from 0.693 to 0.103 and the mean margin rises from 0 to 3.21, with all eight pairs ranked the right way round. The log π_θ − log π_ref chart shows how: the chosen answers go up and the rejected ones go down. By the tenth step, as TRL logged it, the chosen answers' summed log-probability had risen from −58.9 to −48.5 on average, and the rejected ones' had fallen from −66.6 to −85.6. Pushing the rejected answers down accounts for more of the margin than pulling the chosen ones up. Every pair's greedy answer changed.
Where 10 updates landed maps the relative size of each weight matrix's change. Every matrix moved by about the same root-mean-square amount, close to ten times the learning rate, because AdamW's first steps move each weight by about lr whatever its gradient. The map therefore mostly shows where the weights themselves are small. Recomputed one sequence at a time, the page's loss matches what TRL logged to .
Where DPO came from
Bradley and Terry (1952) modelled paired comparisons with a score per item, the model behind both RLHF's reward model and DPO. Rafailov et al. (2023) derived DPO and found it matched or beat PPO-based RLHF on sentiment control, summarisation and single-turn dialogue while being much simpler to train. Zephyr (Tunstall et al., 2023) binarised UltraFeedback, GPT-4's ratings of answers from many models, into the chosen and rejected pairs this page uses, and showed DPO on them could align a 7 B model. Azar et al. later analysed DPO as one case of a general objective and showed it can overfit when preferences are nearly deterministic. Variants change the reward: SimPO (the SimPO tab) drops the reference model and normalises by length, and ORPO (the ORPO tab) merges preference tuning into SFT.
Papers
- Direct Preference Optimization: Your Language Model is Secretly a Reward Model Introduces DPO: the KL-constrained RLHF objective solved in closed form, giving a classification loss on preference pairs.
- Rank Analysis of Incomplete Block Designs: I. The Method of Paired Comparisons The Bradley–Terry model, P(i beats j) from a score per item, that DPO’s loss maximises.
- UltraFeedback: Boosting Language Models with Scaled AI Feedback GPT-4 ratings of answers from many models, the source of the eight preference pairs on this page.
- Zephyr: Direct Distillation of LM Alignment Binarises UltraFeedback into chosen and rejected pairs and aligns a 7 B model with DPO on them; TRL’s copy of those pairs is used here.
- Training language models to follow instructions with human feedback InstructGPT’s reward model and PPO, the RLHF pipeline DPO replaces (see the RLHF tab).
- SmolLM2: When Smol Goes Big -- Data-Centric Training of a Small Language Model SmolLM2-135M-Instruct, the policy and reference on this page.
- A General Theoretical Paradigm to Understand Learning from Human Preferences Places RLHF and DPO in one family of objectives and shows DPO can overfit when preferences are deterministic; proposes IPO.