Repository navigation
Description
Motivation
torchrl.objectives.llm has GRPOLoss, DAPO and CISPOLoss, all with a per-token importance ratio. GSPO (Group Sequence Policy Optimization, used to train Qwen3) uses one ratio per sequence instead:
s_i = (pi_theta(y_i | x) / pi_old(y_i | x)) ** (1 / |y_i|) = exp(mean_t(log pi_theta(y_i,t) - log pi_old(y_i,t)))
J = mean_i min(s_i * A_i, clip(s_i, 1 - eps_low, 1 + eps_high) * A_i)
Clipping then keeps or drops whole sequences, which the paper reports is more stable than token-level ratios, especially for MoE models. verl (actor_rollout_ref.actor.policy_loss.loss_mode=gspo) and TRL (GRPOConfig(importance_sampling_level="sequence")) both support it.
Solution
A GSPOLoss(GRPOLoss) subclass, in the same shape as CISPOLoss, that overrides only _log_weight. It uses the paper's GSPO-token form: each valid token's log weight is sg[log s_i] + (log pi_theta(y_i,t) - sg[log pi_theta(y_i,t)]). Its value is log s_i and its gradient flows through each token's log-prob, so the existing clipped objective and aggregation are reused as is. It also allows per-token advantages, as in the paper.
A sequence here is one row of the batch. With masking_strategy="rlhf", a row's ratio pools all its assistant tokens that have stored old log-probs, across turns.
I checked this on CPU with a prototype subclass. With aggregation="prompt_mean" (mean over each sequence's tokens, then over sequences), the loss and gradient match the paper's sequence-level objective computed by hand. That held for a wide clip, for the paper's (3e-4, 4e-4) clip, and for ratios just past each bound with both advantage signs. Padded tokens get zero gradient.
Other parts of GRPOLoss.forward keep their current behavior, which I'd document:
ESScounts each sequence's ratio once per valid token, andclip_fractionaverages over all token slots, padding included.- The KL penalties to the reference and inference policies stay token-level.
kl_mask_thresholdmasks tokens after the sequence ratio is computed, which no longer matches the paper. I'd haveGSPOLossraise if it is set.
The PR would add:
GSPOLossandGSPOLossOutputintorchrl/objectives/llm/grpo.py, exported and listed indocs/source/reference/llms.rstnext to CISPO.- Tests in
test/llm/test_llm_objectives.py, using the existing_FixedLogProbPolicyhelpers:- loss and gradient against the hand-written objective, with padding ignored;
- a sequence's token gradients all vanish when
A_i > 0ands_i > 1 + eps_high, orA_i < 0ands_i < 1 - eps_low, and not for the opposite advantage sign.
Alternatives
- A flag on
GRPOLoss(like TRL'simportance_sampling_level) instead of a subclass. A subclass follows how DAPO and CISPO were added and keepsGRPOLossunchanged. - Pure sequence-level GSPO (one ratio and one advantage per sequence). The GSPO-token form gives the same gradient when the advantage is constant over a sequence, and fits the per-token tensors the loss already uses.
Additional context
A few choices I'd like your view on before opening the PR:
- Subclass or flag. Is a
GSPOLosssubclass fine, or would you rather have an option onGRPOLoss? - Defaults. The paper's objective corresponds to
aggregation="prompt_mean", whileGRPOLossdefaults to"token_mean". The paper also clips at (3e-4, 4e-4), much tighter than GRPO's 0.2, because the sequence ratio is a geometric mean over tokens. ShouldGSPOLossdefault to the paper's values, or keep theGRPOLossdefaults and document the paper's? - Scripts. Should
sota-implementations/grpoget a config option to pick the loss (GRPO, DAPO, CISPO, GSPO), with a smoke test, or is that out of scope like it was for CISPO?
I'd like to work on this.
Checklist
- I have checked that there is no similar issue in the repo (required)
Activity
arun-elr commentedon Oct 31, 2026
/assign
arun-elr commentedon Oct 31, 2026
Draft PR: #4653. It follows the issue and keeps the
GRPOLossdefaults for now; I'll update it once you've weighed in on the three questions above.[Feature] Add GSPOLoss