GSPO-token policy loss function#154
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @nkkarpov, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a new policy loss function, GSPOLossFn, which implements the GSPO-token policy loss as detailed in a recent research paper. This addition expands the set of available policy optimization algorithms within the framework, providing a new tool for training language models. The implementation includes the core loss calculation logic, proper integration into the system's loss function registry, and corresponding unit tests to ensure its correctness.
Highlights
- New Policy Loss Function: I've implemented the GSPO-token policy loss function,
GSPOLossFn, based on the methodology described in the research paper 'https://arxiv.org/pdf/2507.18071'. This new function is designed to calculate policy loss for token-level operations. - Integration into Framework: The newly implemented
GSPOLossFnhas been integrated into the existingPOLICY_LOSS_FNregistry, making it discoverable and usable within thetrinityalgorithm framework. This involves importing the new class and adding it to the__all__list intrinity/algorithm/policy_loss_fn/__init__.py. - Unit Test Coverage: A dedicated unit test,
test_gspo_policy_loss, has been added totests/algorithm/policy_loss_test.pyto verify the correctness and expected behavior of theGSPOLossFn. This test initializes the loss function with default arguments and asserts the computed loss and metrics against known values.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request introduces the GSPO-token policy loss function, including its implementation and corresponding unit tests. The implementation is generally well-structured. Suggestions have been provided to improve code clarity, maintainability, and test readability, including refactoring initialization logic, adding comments to explain complex calculations, and using named constants in tests.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
/unittest-module-algorithm |
Summary
Tests
Github Test Reporter by CTRF 💚 |
|
Thanks for your contribution! We have suggested some revisions, with a reference to verl's PR about GSPO. Could you verify if the suggested modifications are correct, update the unittest if needed, and then rerun the pre-commit and unittest? |
|
/unittest-module-algorithm |
|
Thank you, @hiyuchang and @yanxi-chen , for the feedback. I've made another pass and improved the naming and verbosity of the code. I also tried to balance creating new variables and keeping lines concise. |
|
/unittest-module-algorithm |
Summary
Tests
Github Test Reporter by CTRF 💚 |
yanxi-chen
left a comment
There was a problem hiding this comment.
Please see whether the inline comments better reflect the algorithm logic as described in the GSPO paper, and update the unittest if necessary. Otherwise lgtm!
|
@yanxi-chen My purpose was to implement GSPO-token loss function. Please, find the attached image from the original paper. Additionallym I have tried to make the interface compatible with the original grpo policy loss function (it accepts the same tensors). I assume you looked into |
@nkkarpov Thanks for the update. I understand that you intend to implement the GSPO-token variant. Note that Eq. (14) still involves You can also see this in the gradient of the GSPO-token loss, i.e., Eq. (16) and (17) of the paper: This explains my proposed modification: |
|
Yes, you are right, now it should be correct :) |
|
Thanks @nkkarpov . Could you fix the pre-commit errors and then commit again? Otherwise I think it's ready to merge :) |
|
/unittest-module-algorithm |
Summary
Tests
Github Test Reporter by CTRF 💚 |
yanxi-chen
left a comment
There was a problem hiding this comment.
lgtm, thanks for contributing!




Description
GSPO-token policy loss function from https://arxiv.org/pdf/2507.18071
Checklist
Please check the following items before code is ready to be reviewed.