Direct preference optimization (DPO) loss
Compute mean DPO loss from paired sequence log probabilities.
What’s going wrong
Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.
Each pair supplies policy_chosen, policy_rejected, reference_chosen, reference_rejected. Compute margin = beta × ((policy_chosen − policy_rejected) − (reference_chosen − reference_rejected)). Return margins, loss (mean stable negative log-sigmoid of margins) and grad_policy as rows [∂loss/∂policy_chosen, ∂loss/∂policy_rejected]. beta is nonnegative. An empty batch returns zero loss and empty lists.
Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.
Expected behavior
Compute mean DPO loss from paired sequence log probabilities.
Reproduction
{"beta":1,"pairs":[{"policy_chosen":-2,"policy_rejected":-4,"reference_chosen":-1,"reference_rejected":-5}]}{"margins":[-2],"loss":2.126928,"grad_policy":[[-0.880797,0.880797]]}{"beta":0,"pairs":[{"policy_chosen":-2,"policy_rejected":-4,"reference_chosen":-1,"reference_rejected":-5}]}{"margins":[0],"loss":0.693147,"grad_policy":[[0,0]]}{"beta":0.5,"pairs":[{"policy_chosen":-1,"policy_rejected":-2,"reference_chosen":-2,"reference_rejected":-3},{"policy_chosen":-1,"policy_rejected":-3,"reference_chosen":-2,"reference_rejected":-3}]}{"margins":[0,0.5],"loss":0.583612,"grad_policy":[[-0.125,0.125],[-0.094385,0.094385]]}Before you begin
- Python functions, lists and dictionaries
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.