Repository navigation
fix(sapo): differentiate the soft gate through the ratio - #636
Open
flyingdoubleG wants to merge 1 commit into
Open
flyingdoubleG wants to merge 1 commit into
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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