use nanmean for aggregating loss#44257
Open
winglian wants to merge 1 commit intohuggingface:mainfrom
Open
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
SunMarc
reviewed
Feb 24, 2026
| @@ -2054,7 +2101,7 @@ def _maybe_log_save_evaluate( | |||
| logs: dict[str, float] = {} | |||
|
|
|||
| # all_gather + mean() to get average loss over all processes | |||
| tr_loss_scalar = nested_gather(tr_loss, self.args.parallel_mode).mean().item() | |||
| tr_loss_scalar = nested_gather(tr_loss, self.args.parallel_mode).nanmean().item() | |||
Member
There was a problem hiding this comment.
we have the following already. Not sure how we are still getting the nan
if (
self.args.logging_nan_inf_filter
and not is_torch_xla_available()
and (torch.isnan(tr_loss_step) or torch.isinf(tr_loss_step))
):
# if loss is nan or inf simply add the average of previous logged losses
self._tr_loss += self._tr_loss / (1 + self.state.global_step - self._globalstep_last_logged)
Collaborator
Author
There was a problem hiding this comment.
this was a fix we had to make last year when we added CP support, https://github.com/axolotl-ai-cloud/axolotl/pull/3033/changes, even though that code block above has been around for a couple of years. My assumption is that it has something to do with the multigpu gather.
SunMarc
reviewed
Feb 25, 2026
| @@ -2054,7 +2101,7 @@ def _maybe_log_save_evaluate( | |||
| logs: dict[str, float] = {} | |||
|
|
|||
| # all_gather + mean() to get average loss over all processes | |||
| tr_loss_scalar = nested_gather(tr_loss, self.args.parallel_mode).mean().item() | |||
| tr_loss_scalar = nested_gather(tr_loss, self.args.parallel_mode).nanmean().item() | |||
| @@ -2054,7 +2101,7 @@ def _maybe_log_save_evaluate( | |||
| logs: dict[str, float] = {} | |||
|
|
|||
| # all_gather + mean() to get average loss over all processes | |||
| tr_loss_scalar = nested_gather(tr_loss, self.args.parallel_mode).mean().item() | |||
| tr_loss_scalar = nested_gather(tr_loss, self.args.parallel_mode).nanmean().item() | |||
Member
There was a problem hiding this comment.
can you add a check self.args.logging_nan_inf_filter if we are using nanmean ?
Comment on lines
+2797
to
+2799
| metrics[f"{metric_key_prefix}_loss"] = np.nanmean(np.concatenate(all_losses)).item() | ||
| elif isinstance(all_losses, np.ndarray): | ||
| metrics[f"{metric_key_prefix}_loss"] = all_losses.mean().item() | ||
| metrics[f"{metric_key_prefix}_loss"] = np.nanmean(all_losses).item() |
This was referenced Apr 29, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
When post training using context parallelism, some processes may have their chunk of the sample input masked out leading to a NaN loss for that process. Using
nanmeanallows us to keep the real loss that isn'tNaN@SunMarc
Fixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.