Add GSPOLoss (Group Sequence Policy Optimization) - #2
Draft
smellslikeml wants to merge 1 commit into
Draft
smellslikeml wants to merge 1 commit into
smellslikeml wants to merge 1 commit into
Conversation
Port (Apache, with attribution) of GSPO's sequence-level importance ratio (arXiv:2507.18071) into torchrl/objectives/llm/grpo.py as GSPOLoss(GRPOLoss), overriding _log_weight for a sequence length-normalized ratio; clipping is inherited. Mirrors the CISPOLoss variant-per-subclass pattern. CPU synthetic-TensorDict test; GPU convergence deferred. Co-Authored-By: remyx-ai[bot] <289541483+remyx-ai[bot]@users.noreply.github.com> Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
| Prefix | Label Applied | Example |
|---|---|---|
[Algorithm] |
new algo | [Algorithm] Add new RL objective |
[BE] |
BE | [BE] Improve error messages |
[Benchmark] or [Benchmarks] |
Benchmarks | [Benchmark] Add collector benchmark |
[BugFix] |
BugFix | [BugFix] Fix memory leak in collector |
[Example] or [Examples] |
Examples | [Example] Add training script |
[Feature] |
Feature | [Feature] Add new optimizer |
[Doc] or [Docs] |
Documentation | [Doc] Update installation guide |
[Refactor] |
Refactoring | [Refactor] Clean up module imports |
[CI] |
CI | [CI] Fix workflow permissions |
[Test] or [Tests] |
Tests | [Tests] Add unit tests for buffer |
[Trainer] or [Trainers] |
Trainers | [Trainer] Add trainer config |
[Environment] or [Environments] |
Environments | [Environments] Add Gymnasium support |
[Data] |
Data | [Data] Fix replay buffer sampling |
[LLM] |
llm/ | [LLM] Add reward model integration |
[Minor] |
small change | [Minor] Fix typo in error message |
[Performance] or [Perf] |
Performance | [Performance] Optimize tensor ops |
[BC-Breaking] |
bc breaking | [BC-Breaking] Remove deprecated API |
[Deprecation] |
Deprecation | [Deprecation] Mark old function |
[Algorithm] or [Algorithms] |
new algo | [Algorithm] Add new objective |
[Quality] |
Quality | [Quality] Fix typos and add codespell |
[Versioning] |
versioning | [Versioning] Bump release version |
[WIP] |
WIP | [WIP] Draft implementation |
Note: Common variations like singular/plural are supported (e.g., [Doc] or [Docs]).
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.
Add GSPOLoss — Group Sequence Policy Optimization
What. Adds
GSPOLosstotorchrl/objectives/llm/grpo.py— the sequence-level policy-optimization objective from GSPO (Group Sequence Policy Optimization, Qwen — arXiv:2507.18071). GSPO defines the importance ratio at the sequence likelihood level (a length-normalized / geometric-mean log-ratio over the response tokens) and clips at the sequence level, vs GRPO's token-level ratio. Theobjectives/llm/surface has GRPO/DAPO/CISPO (all token-level) but no sequence-level mode.Provenance / license. The sequence-ratio math is ported with attribution from the
importance_sampling_level == "sequence"branch of huggingface/trl (Apache-2.0 → compatible with TorchRL's MIT). Credited to the trl source + the paper in the class docstring. No trl trainer/Liger machinery copied.Design (house style — variant-per-subclass, mirrors
CISPOLoss).class GSPOLoss(GRPOLoss)+class GSPOLossOutput(LLMLossOutput)added ingrpo.py(not a new file, not a string mode);output_typeset accordingly._log_weight: takes the inherited token-level log-ratio + response mask fromsuper()._log_weight(...), reduces to a per-sequence length-normalized log-ratio(tok_log_weight · mask).sum(-1) / mask.sum(-1).clamp(1), and broadcasts it back over the sequence — so sequence-level clipping is inherited for free fromGRPOLoss._compute_policy_objective(not re-implemented)._AcceptedKeys/set_keys()); GRPO/DAPO/CISPO behavior untouched; pure PyTorch/TensorDict, no new deps.Validation.
test/llm/test_llm_objectives.py, mirroringtest_cispo_clips_importance_weight(with an inline batch-2 ragged-mask TensorDict, since_policy_loss_datahardcodes a single all-ones sequence), using the trl formula as the in-test oracle._log_weightreduction is bit-identical to the trl reference formula on a ragged-mask batch, padded tokens are correctly excluded, and the cancellation property holds (token ratios 1.2 and 1/1.2 → sequence ratio exactly 1.0).VALIDATION.md.Scope. 6 files: the loss + output class in
grpo.py, itsobjectives/llm/__init__.pyexport, the test, two docs entries (llms_objectives.rst+ thellms.rstindex), andVALIDATION.md. No changes to the trainer, collectors, envs, or existing objectives.Attribution. Drafted with Outrider (
remyx-ai[bot], GLM-5.2) + Claude Code; human-reviewed and the sequence-ratio math independently validated. Co-authored on the commit.