Repository navigation
fix(sapo): differentiate the soft gate through the ratio - #636
flyingdoubleG wants to merge 1 commit into
Conversation
SAPO maximizes E[f(r) * A] with f(r) = sigmoid(tau (r - 1)) * 4 / tau; its gradient is f'(r) * r * A * grad(log pi), i.e. the weight w * r with w = 4 p (1 - p), p = sigmoid(tau (r - 1)) (arXiv:2511.20347, Eq. 7-8), which is 1 at r = 1 and decays as r moves away from 1. The loss was written as -A * sg(f(r)) * log pi, whose gradient weight is f(r) itself: 2 / tau at r = 1 (twice the policy gradient for tau = 1) and increasing with r towards 4 / tau, so off-policy tokens were weighted up instead of down. Use -A * f(r) with the gate differentiated through r, as verl and ms-swift do. Update the loss snapshot in the existing test and add a gradient test against Eq. 7-8. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I independently replayed the SAPO loss at base For both The CPU replay executes the loss and reduction definitions at the stated commits. It leaves the choice of default reduction and full training behavior open. |
Description
Background. SAPO (arXiv:2511.20347) maximizes
E[f(r) * A]with the soft gatef(r) = sigmoid(tau (r - 1)) * 4 / tau,r = pi_theta / pi_old. Differentiating gives (paper Eq. 7-8)so the per-token weight
w * ris 1 atr = 1(plain policy gradient) and decays asrmoves away from 1. That decay is the soft trust region.Problem.
SAPOPolicyLossFncomputeswhose gradient weight is
f(r)itself, notf'(r) * r. With the defaulttau_pos = 1:So on-policy tokens get
2 / tautimes the policy gradient, and the weight increases withr(towards4 / tau) instead of decaying: off-policy tokens are weighted up rather than down. (Computed with autograd onsoft_gate_functionfrom this file.)Change.
sapo_loss = -advantages * soft_gate, with the gate differentiated throughratio, which is how verl (compute_policy_loss_sapo) and ms-swift implement it. The logged gate / ratio metrics are unchanged; the logged loss value is now-mean(f(r) A)instead of-mean(f(r) A log pi).Tests.
tests/algorithm/policy_loss_test.py: updated the loss snapshot intest_sapo_policy_loss(other metrics unchanged) and addedtest_sapo_policy_loss_gradient, which checksd loss / d logprobagainst Eq. 7-8 for the default temperatures. Both fail before the change; the file passes after (9 tests, CPU).Note, not changed here: the paper averages over tokens within each sequence and then over sequences (
1/G Σ 1/|y_i| Σ), i.e.seq-mean-token-mean, and verl forces that mode for SAPO; this class defaults totoken-mean. Happy to change the default in a follow-up if you prefer.Checklist
🤖 Generated with Claude Code