DPO Implementation › Worked Example: Manual DPO Loss Computation
Language AI HandbookAll chapters DPO Implementation › Worked Example: Manual DPO Loss ComputationOne section · Sections in this chapter4 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Before looking at the full training loop, let us trace through a single DPO loss computation by hand to make every step concrete. Understanding the numerical path from raw model outputs to a loss value is invaluable when debugging.
Suppose we have a batch of one preference pair with the following hypothetical log probabilities after tokenization and masking:
- Policy model log probability of chosen response:
- Policy model log probability of rejected response:
- Reference model log probability of chosen response:
- Reference model log probability of rejected response:
These numbers represent plausible values for short responses from a GPT-2 scale model. The policy has made the chosen response somewhat more likely than the reference did, while the reference considered the rejected response slightly more likely than the policy does.
Step 1: Compute the log ratios (implicit rewards before scaling)
The policy has increased its probability for the chosen response (positive ratio) and decreased its probability for the rejected response (negative ratio) relative to the reference. This is the desired behavior: the model has started learning the preference.
Step 2: Scale by to get implicit rewards
With :
Step 3: Compute the reward margin
A positive reward margin means the model correctly prefers the chosen response. The magnitude of 0.11 is modest, showing the model has made some progress but has not strongly internalized the preference yet.
Step 4: Apply the sigmoid and compute the loss
A loss of 0.640 makes sense: we are closer to the maximum loss at (which gives loss ) than to near-zero loss, because the reward margin is still small. As training progresses and the margin grows, say to 2.0, the loss would drop to approximately .
Step 5: Interpret the gradient direction
The gradient of the loss with respect to the reward margin is . This negative gradient means the optimizer will increase the reward margin, which happens by either increasing the chosen log ratio, decreasing the rejected log ratio, or both. In terms of model weights, the update nudges the model toward assigning higher probability to the chosen response and lower probability to the rejected response relative to the reference.
This worked example reveals that DPO training is gradual and incremental. Each update makes a small change to the model's implicit preferences, and over many batches these changes accumulate into a policy that reflects the preference data. The key insight is that no single preference pair causes a large jump in model behavior: the regularization effect of keeps each update modest, and alignment emerges from the accumulation of thousands of such modest updates.