-
Notifications
You must be signed in to change notification settings - Fork 432
[FSDP][Loss] Switch gradient reduction to SUM #1963
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
Closed
Closed
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -676,6 +676,10 @@ def fully_shard( | |
| reshard_after_forward=fsdp_config.reshard_after_forward, | ||
| offload_policy=CPUOffloadPolicy() if self.fsdp_config.cpu_offload else None, | ||
| ) | ||
| # Reduce-scatter gradients with pure SUM (no divide). Combined with the loss forwards no | ||
| # longer injecting x world_size, each param's gradient is the sum of per-rank local-component | ||
| # gradients, i.e. the global loss gradient. Covers nested/child FSDP modules via self.modules(). | ||
| self.set_gradient_reduce_sum() | ||
| return self | ||
|
|
||
| def _fully_shard( | ||
|
|
@@ -1388,6 +1392,21 @@ def post_micro_batch_forward(self, batch_outputs: Sequence[ModelOutputs]) -> Bat | |
| if "reduced_base_loss" in reduced_other_losses: | ||
| reduced_other_losses["reduced_llm_loss"] = reduced_other_losses.pop("reduced_base_loss") | ||
|
|
||
| # Safety net: every `*loss` tensor field an output exposes must have produced a display curve | ||
| # from a registered local component. Otherwise a newly added loss term would silently vanish | ||
| # from the logged curves while still driving backward, hiding the regression. | ||
| for output in batch_outputs: | ||
|
Collaborator
Author
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. Encapsulate it into an internal function; subclasses can override it if needed |
||
| for name in output.model_fields: | ||
| field_value = getattr(output, name, None) | ||
| if "loss" in name and isinstance(field_value, torch.Tensor): | ||
| expected = "reduced_llm_loss" if name == "loss" else f"reduced_{name}" | ||
| if expected not in reduced_other_losses: | ||
| raise RuntimeError( | ||
| f"Loss field '{name}' has no display curve ('{expected}' missing from reduced " | ||
| f"logs): register its detached local component on extra_info (see " | ||
| f"`_store_local_display_losses` / CE's `local_base_loss`)." | ||
| ) | ||
|
|
||
| ret = BatchForwardInfo( | ||
| logs_info=reduced_other_losses, | ||
| extra_info=train_engine_extra_info, | ||
|
|
||
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
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
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.
If the changes in PR 1962 are necessary, it is recommended to merge PR 1962 and PR 1963 into a single one, as it makes no sense for a function defined in PR 1962 to be called in PR 1963 — it feels fragmented