Skip to content

[Feature Request] GSPOLoss (sequence-level importance ratio for GRPO) #4652

Done

Description

@arun-elr

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:

  • ESS counts each sequence's ratio once per valid token, and clip_fraction averages over all token slots, padding included.
  • The KL penalties to the reference and inference policies stay token-level.
  • kl_mask_threshold masks tokens after the sequence ratio is computed, which no longer matches the paper. I'd have GSPOLoss raise if it is set.

The PR would add:

  • GSPOLoss and GSPOLossOutput in torchrl/objectives/llm/grpo.py, exported and listed in docs/source/reference/llms.rst next to CISPO.
  • Tests in test/llm/test_llm_objectives.py, using the existing _FixedLogProbPolicy helpers:
    • loss and gradient against the hand-written objective, with padding ignored;
    • a sequence's token gradients all vanish when A_i > 0 and s_i > 1 + eps_high, or A_i < 0 and s_i < 1 - eps_low, and not for the opposite advantage sign.

Alternatives

  • A flag on GRPOLoss (like TRL's importance_sampling_level) instead of a subclass. A subclass follows how DAPO and CISPO were added and keeps GRPOLoss unchanged.
  • 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:

  1. Subclass or flag. Is a GSPOLoss subclass fine, or would you rather have an option on GRPOLoss?
  2. Defaults. The paper's objective corresponds to aggregation="prompt_mean", while GRPOLoss defaults 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. Should GSPOLoss default to the paper's values, or keep the GRPOLoss defaults and document the paper's?
  3. Scripts. Should sota-implementations/grpo get 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

  1. arun-elr commented on Oct 31, 2026

    @arun-elr
    ContributorAuthor

    /assign

  2. arun-elr commented on Oct 31, 2026

    @arun-elr
    ContributorAuthor

    Draft PR: #4653. It follows the issue and keeps the GRPOLoss defaults for now; I'll update it once you've weighed in on the three questions above.

  3. added a commit that references this issue on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions