use nanmean for aggregating loss#44257
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. |
| @@ -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() | |||
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)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.
| @@ -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() | |||
There was a problem hiding this comment.
can you add a check self.args.logging_nan_inf_filter if we are using nanmean ?
| 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() |
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.