Skip to content

Comments

GSPO-token policy loss function#154

Merged
yanxi-chen merged 6 commits intoagentscope-ai:mainfrom
nkkarpov:nk/add-gspo
Aug 3, 2025
Merged

GSPO-token policy loss function#154
yanxi-chen merged 6 commits intoagentscope-ai:mainfrom
nkkarpov:nk/add-gspo

Conversation

@nkkarpov
Copy link
Contributor

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.

  • Code has passed all tests
  • Docstrings have been added/updated in Google Style
  • Documentation has been updated
  • Code is ready for review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GSPOLossFn has been integrated into the existing POLICY_LOSS_FN registry, making it discoverable and usable within the trinity algorithm framework. This involves importing the new class and adding it to the __all__ list in trinity/algorithm/policy_loss_fn/__init__.py.
  • Unit Test Coverage: A dedicated unit test, test_gspo_policy_loss, has been added to tests/algorithm/policy_loss_test.py to verify the correctness and expected behavior of the GSPOLossFn. 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

  1. 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@hiyuchang
Copy link
Collaborator

/unittest-module-algorithm

@github-actions
Copy link

Summary

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Other ❓ Flaky 🍂 Duration ⏱️
6 6 0 0 0 0 3ms

Tests

Test Name Status Flaky Duration
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_dpo_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_gspo_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_mix_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_opmd_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_ppo_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_sft_policy_loss 1ms

Github Test Reporter by CTRF 💚

@hiyuchang
Copy link
Collaborator

hiyuchang commented Jul 31, 2025

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?

@nkkarpov
Copy link
Contributor Author

/unittest-module-algorithm

@nkkarpov
Copy link
Contributor Author

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.

@nkkarpov nkkarpov requested a review from hiyuchang July 31, 2025 21:07
@pan-x-c
Copy link
Collaborator

pan-x-c commented Aug 1, 2025

/unittest-module-algorithm

@github-actions
Copy link

github-actions bot commented Aug 1, 2025

Summary

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Other ❓ Flaky 🍂 Duration ⏱️
6 6 0 0 0 0 8ms

Tests

Test Name Status Flaky Duration
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_dpo_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_gspo_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_mix_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_opmd_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_ppo_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_sft_policy_loss 1ms

Github Test Reporter by CTRF 💚

Copy link
Collaborator

@yanxi-chen yanxi-chen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@nkkarpov
Copy link
Contributor Author

nkkarpov commented Aug 1, 2025

@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 vanilla GSPO loss function, and it probably lead you to the confusion.
Screenshot 2025-08-01 at 12 29 19

@yanxi-chen
Copy link
Collaborator

@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 vanilla GSPO loss function, and it probably lead you to the confusion. Screenshot 2025-08-01 at 12 29 19

@nkkarpov Thanks for the update.

I understand that you intend to implement the GSPO-token variant. Note that Eq. (14) still involves $s_i(\theta)$ which, as defined in Eq. (7), involves sequence-wise prob ratio (which equals product of token-wise prob ratio).
image

You can also see this in the gradient of the GSPO-token loss, i.e., Eq. (16) and (17) of the paper:
image

This explains my proposed modification: negative_approx_kl_seq = torch.sum(negative_approx_kl * action_mask, dim=-1) / seq_lengths. Please feel free to correct me if I'm wrong :)

@nkkarpov
Copy link
Contributor Author

nkkarpov commented Aug 2, 2025

Yes, you are right, now it should be correct :)

@yanxi-chen
Copy link
Collaborator

Thanks @nkkarpov . Could you fix the pre-commit errors and then commit again? Otherwise I think it's ready to merge :)

@yanxi-chen
Copy link
Collaborator

/unittest-module-algorithm

@github-actions
Copy link

github-actions bot commented Aug 3, 2025

Summary

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Other ❓ Flaky 🍂 Duration ⏱️
6 6 0 0 0 0 5ms

Tests

Test Name Status Flaky Duration
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_dpo_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_gspo_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_mix_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_opmd_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_ppo_policy_loss 1ms
tests/algorithm/policy_loss_test.py::VerlPolicyLossTest::test_sft_policy_loss 1ms

Github Test Reporter by CTRF 💚

Copy link
Collaborator

@yanxi-chen yanxi-chen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks for contributing!

@yanxi-chen yanxi-chen merged commit 04cf8c8 into agentscope-ai:main Aug 3, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants