-
Notifications
You must be signed in to change notification settings - Fork 55
GSPO-token policy loss function #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a9417a8
implement gspo-token policy loss function
nkkarpov 8217109
Apply suggestion from @gemini-code-assist[bot]
nkkarpov f2698dc
adress comments
nkkarpov dce3ecd
Refactor GSPO policy loss function to improve clarity and accuracy of…
nkkarpov 80112a7
apply suggestions
nkkarpov 9d6e596
make pre-commit happy
nkkarpov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """GSPO-token policy loss function. | ||
|
|
||
| Implemented from https://arxiv.org/pdf/2507.18071 | ||
| """ | ||
|
|
||
| from typing import Dict, Optional, Tuple | ||
|
|
||
| import torch | ||
|
|
||
| from trinity.algorithm.policy_loss_fn.policy_loss_fn import POLICY_LOSS_FN, PolicyLossFn | ||
| from trinity.algorithm.utils import masked_mean | ||
|
|
||
|
|
||
| @POLICY_LOSS_FN.register_module("gspo") | ||
| class GSPOLossFn(PolicyLossFn): | ||
| def __init__( | ||
| self, | ||
| backend: str = "verl", | ||
| clip_range: Optional[float] = None, | ||
| clip_range_low: Optional[float] = None, | ||
| clip_range_high: Optional[float] = None, | ||
| ) -> None: | ||
| super().__init__(backend=backend) | ||
| _clip_range_low = clip_range_low if clip_range_low is not None else clip_range | ||
| if _clip_range_low is None: | ||
| raise ValueError("Either clip_range or clip_range_low must be specified.") | ||
| self.clip_range_low = _clip_range_low | ||
|
|
||
| _clip_range_high = clip_range_high if clip_range_high is not None else clip_range | ||
| if _clip_range_high is None: | ||
| raise ValueError("Either clip_range or clip_range_high must be specified.") | ||
| self.clip_range_high = _clip_range_high | ||
|
|
||
| def __call__( # type: ignore | ||
| self, | ||
| logprob: torch.Tensor, # [batch_size, seq_len] | ||
| old_logprob: torch.Tensor, # [batch_size, seq_len] | ||
| action_mask: torch.Tensor, # [batch_size, seq_len] | ||
| advantages: torch.Tensor, # [batch_size, seq_len] | ||
| **kwargs, | ||
| ) -> Tuple[torch.Tensor, Dict]: | ||
| negative_approx_kl = logprob - old_logprob # [batch_size, seq_len] | ||
| negative_approx_kl_seq = masked_mean( | ||
| negative_approx_kl, action_mask, axis=-1 | ||
| ) # [batch_size] | ||
| log_seq_importance_ratio = ( | ||
| logprob - logprob.detach() + negative_approx_kl_seq.detach().unsqueeze(-1) | ||
| ) # [batch_size, seq_len] | ||
| ratio = torch.exp(log_seq_importance_ratio) # [batch_size, seq_len] | ||
| pg_losses = -advantages * ratio # [batch_size, seq_len] | ||
| pg_losses_clipped = -advantages * torch.clamp( | ||
| ratio, 1.0 - self.clip_range_low, 1.0 + self.clip_range_high | ||
| ) # [batch_size, seq_len] | ||
|
|
||
| seq_losses = masked_mean( | ||
| torch.max(pg_losses, pg_losses_clipped), action_mask, axis=-1 | ||
| ) # [batch_size] | ||
| pg_loss = torch.mean(seq_losses) | ||
| pg_clipfrac = masked_mean(torch.gt(pg_losses_clipped, pg_losses).float(), action_mask) | ||
| ppo_kl = masked_mean(-negative_approx_kl, action_mask) | ||
| ppo_kl_seq = torch.mean(-negative_approx_kl_seq) | ||
| metrics = { | ||
| "pg_clipfrac": pg_clipfrac.detach().item(), | ||
| "ppo_kl": ppo_kl.detach().item(), | ||
| "pg_loss": pg_loss.detach().item(), | ||
| "ppo_kl_seq": ppo_kl_seq.detach().item(), | ||
| } | ||
| return pg_loss, metrics | ||
|
|
||
| @classmethod | ||
| def default_args(cls) -> Dict: | ||
| # See discussion in https://github.com/volcengine/verl/pull/2775#issuecomment-3130065984 | ||
| return { | ||
| "clip_range_low": 0.0003, | ||
| "clip_range_high": 0.0004, | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.