Implement the ORPO odds-ratio preference loss
Why it matters ORPO folds preference alignment into SFT with no reference model, which halves the memory of DPO-style training.
ORPO aligns a model in one stage with no reference model: the usual SFT loss on the chosen answer plus a penalty that pushes the odds of the chosen answer above the odds of the rejected one.
Write orpo_loss(chosen_logps, rejected_logps, lam=0.1). Inputs are length-B arrays of length-normalised log-probabilities (mean token log-prob, always < 0). With p = exp(logp), log odds(y) = logp - log(1 - p), computed stably as logp - log1p(-exp(logp)).
- sft = mean(-chosen_logps)
- or_term = mean(-log sigmoid(log odds(chosen) - log odds(rejected))), using a stable log-sigmoid
- loss = sft + lam * or_term
Return (loss, sft, or_term) as floats.
def orpo_loss(chosen_logps, rejected_logps, lam=0.1)Example
orpo_loss([-0.5], [-1.5]) is about (0.5171, 0.5, 0.1709).
Understand the idea
Odds compare a chance of happening to a chance of not happening: a probability of 0.6 is odds 0.6 / 0.4 = 1.5. ORPO says the model's odds of producing the chosen answer should be higher than its odds of producing the rejected one, and adds that as a penalty on top of normal fine-tuning.
Example: the chosen answer has average log-prob -0.5, so p = 0.607 and log odds = -0.5 - ln(0.393) = 0.433. The rejected answer has -1.5, so p = 0.223 and log odds = -1.5 - ln(0.777) = -1.248. The difference is 1.680, and -log sigmoid(1.680) = 0.171. SFT loss = 0.5. Total with lam 0.1: 0.5 + 0.0171 = 0.5171.
Rule: L = -mean(logp_w) + lam * mean(-log sigmoid(logodds_w - logodds_l)), logodds = logp - log(1 - e^logp).
There is no reference model, unlike DPO, so only one copy of the network sits in memory during training.