In DPO, an implicit reward replaces a separately trained reward model. For a completion y given prompt x, it is β times the policy’s log-probability relative to the reference model: completions the policy raises in probability relative to the reference receive higher implicit reward. section 8.1.1
Equation 8.3
\[\label{eq:dpo_reward} r(x, y) = \beta \log \frac{\pi_r(y \mid x)}{\pi_{\text{ref}}(y \mid x)},\]The reward is defined by the policy-to-reference log-probability ratio, scaled by β. section 8.1.1
DPO compares this relative shift for a chosen completion with the shift for a rejected one. Its loss favors a larger shift for the chosen response. In implementation, the policy and frozen reference model provide log-probabilities for both completions; the resulting implicit rewards can be detached from the gradient graph for logging. section 8.1.1 section 8.3
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()
)
This implementation computes chosen and rejected rewards from policy and reference log-probabilities, then detaches them for logging. section 8.3
