A log-ratio measures how much one positive quantity exceeds another on a logarithmic scale. In preference optimization, the key quantity compares a policy’s probability with a frozen reference model’s probability for the same completion: log(πθ(y|x) / πref(y|x)). Summed across completion tokens, it captures the policy’s relative shift while retaining a reference-based constraint. section 8.1.1 section 15.3.1
Direct Preference Optimization (DPO) subtracts the rejected completion’s log-ratio from the chosen completion’s log-ratio, then applies a sigmoid loss scaled by β. Increasing this difference makes the policy prefer the chosen response relative to the reference, while β controls the strength of the preference pull and KL-distance trade-off. section 8.1.1
A practical DPO implementation computes the quantity in a short pipeline:
- Run policy and frozen-reference forward passes on chosen and rejected completions to obtain sequence log-probabilities. section 8.3
- Form policy and reference log-ratios by subtracting rejected log-probabilities from chosen log-probabilities. section 8.3
- Subtract the reference log-ratio from the policy log-ratio, compute the negative log-sigmoid of β times the result, and optionally detach implicit rewards for logging. section 8.3
- Cache reference log-probabilities when memory is constrained; reuse can reduce peak memory usage by 50%. section 8.3
Chosen and rejected completions
For a prompt x with chosen completion yc and rejected completion yr, DPO compares log(πθ(yc|x)/πref(yc|x)) against log(πθ(yr|x)/πref(yr|x)). A larger chosen-minus-rejected difference lowers the loss; a rejected response receiving the stronger relative shift produces a larger corrective update. section 8.1.1
pi_logratios = policy_chosen_logps - policy_rejected_logps 1
ref_logratios = reference_chosen_logps - reference_rejected_logps2
logits = pi_logratios - ref_logratios3
losses = -F.logsigmoid(beta * logits)4
chosen_rewards = (
beta * (policy_chosen_logps - reference_chosen_logps).detach()
)5
rejected_rewards = (
beta * (policy_rejected_logps - reference_rejected_logps).detach()
)
Inspect the compact computation showing policy ratios, reference ratios, their difference, the negative log-sigmoid loss, and detached completion rewards. section 8.3
