-
Notifications
You must be signed in to change notification settings - Fork 414
[Fix] fix SP for InternS1 VL RL #1656
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
Open
tina-wen
wants to merge
1
commit into
InternLM:main
Choose a base branch
from
tina-wen:rl_sp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+36
−7
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,9 @@ | |
| import torch | ||
| import torch.distributed as dist | ||
| import torch.nn.functional as F | ||
| from torch.distributed.device_mesh import DeviceMesh | ||
|
|
||
| from xtuner.v1.loss.utils import sp_gather | ||
| from xtuner.v1.utils import get_logger | ||
|
|
||
| from ..base import ( | ||
|
|
@@ -75,15 +77,32 @@ def __init__(self, loss_cfg: GRPOLossConfig, loss_kwargs: GRPOLossKwargs): | |
| self.policy_loss_fn = get_policy_loss_fn(self.loss_cfg.policy_loss_cfg.get("loss_type", "vanilla")) | ||
|
|
||
| @staticmethod | ||
| def build_batches(loss_ctx_list: list["GRPOLossContext"]) -> list["GRPOLossContext"]: # type: ignore[override] | ||
| def build_batches( # type: ignore[override] | ||
| loss_ctx_list: list["GRPOLossContext"], | ||
| *args: Any, | ||
| **kwargs: Any, | ||
| ) -> list["GRPOLossContext"]: | ||
| sp_mesh = cast(DeviceMesh | None, kwargs.get("sp_mesh")) | ||
| if sp_mesh is None and len(args) >= 2: | ||
| sp_mesh = cast(DeviceMesh | None, args[1]) | ||
|
|
||
| assert len(loss_ctx_list) > 0, "loss_ctx_list can not be empty" | ||
|
|
||
| loss_cfg = loss_ctx_list[0].loss_cfg | ||
|
|
||
| shifted_labels_list = [loss_ctx.loss_kwargs.shifted_labels for loss_ctx in loss_ctx_list] | ||
| rank_grad_tokens: torch.Tensor | None = None | ||
| for shifted_labels in shifted_labels_list: | ||
| if sp_mesh is not None: | ||
| # gather shifted_labels from different sp ranks to compute the correct loss weight | ||
| shifted_labels = sp_gather(shifted_labels, sp_mesh=sp_mesh, dim=1) | ||
| rank_grad_tokens = ( | ||
| (shifted_labels != loss_cfg.ignore_idx).sum() | ||
| if rank_grad_tokens is None | ||
| else rank_grad_tokens + (shifted_labels != loss_cfg.ignore_idx).sum() | ||
| ) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All sp ranks in one sp group get the same rank_grad_tokens. Same problem above. |
||
|
|
||
| # Compute the denominator used in the global calibration of the loss | ||
| rank_grad_tokens = sum((labels != loss_cfg.ignore_idx).sum() for labels in shifted_labels_list) | ||
| rank_grad_tokens = cast(torch.Tensor, rank_grad_tokens) | ||
| global_grad_tokens = rank_grad_tokens | ||
| if dist.is_initialized(): | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
each sp rank should calculate its own rank loss, this change makes all ranks in a sp_group caculate the same loss, and it would be all_reduced sum later, thus the
sum_entropywould timessp_size