Skip to content

Crash on invalid loss (i.e. infinite or nan) - #3863

Merged
tianyu-l merged 1 commit into
pytorch:mainfrom
francesco-bertolotti:f14-crash-on-invalid-loss
Jul 6, 2026
Merged

tianyu-l merged 1 commit into
pytorch:mainfrom
francesco-bertolotti:f14-crash-on-invalid-loss

Conversation

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator

In my experience, the training loss can occasionally become NaN for a variety of reasons. Regardless of the underlying cause, once the loss is NaN, it quickly propagates through the model, causing all subsequent computations to produce NaN as well.

I don't think there is a practical scenario where continuing training after detecting a NaN loss is desirable.

This PR adds a simple check after the loss is computed. If the loss is NaN, the corresponding rank immediately raises a RuntimeException and terminates. The remaining distributed ranks will then fail naturally due to the communication timeout.

I also added the following debug information to the exception message:

f"debugging -- input_dict: {input_dict}, labels: {labels}, "
f"global_valid_tokens: {global_valid_tokens}"

I realize this is quite verbose and may produce large log messages, but I thought it could be useful for debugging. If you would prefer to keep the logs lighter, I'm happy to remove it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 5, 2026
@pytorch-bot

pytorch-bot Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense to me, thank you!

@tianyu-l

tianyu-l commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

lint error should be fixed

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator Author

mmmh, it does not seem to be related to my modification:

ERROR Could not import `OpaqueBase` from `torch._library.opaque_object` [missing-module-attribute]
  --> torchtitan/distributed/deepep/hybridep.py:26:42
   |
26 | from torch._library.opaque_object import OpaqueBase, register_opaque_type
   |                                          ^^^^^^^^^^
   |

Am I reading this correctly?

@tianyu-l

tianyu-l commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

seems CI is broken. I'll merge.

@tianyu-l
tianyu-l merged commit 90a542a into pytorch:main Jul 6, 2026
24 of 29 checks passed
Comment thread torchtitan/trainer.py
global_valid_tokens=global_valid_tokens,
)

if not loss.isfinite():

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will cause a D2H sync every microbatch, which is not a good idea. cc., @tianyu-l

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it? Could you share a code pointer or documentation for the fact? @fegin

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chatted offline, but repeat here for future reference. if torch.some_ops() requires a item() or a similar API to get the Python value (CPU) of some_ops() so that Python can do the condition, hence D2H sync.

fegin added a commit that referenced this pull request Jul 7, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* __->__ #3873

#3863 introduces a good
feature to raise if loss is NaN but also introduces per-micro-batch D2H
sync. This PR fixes the issue with the penalty that the actual batch
that causes the NaN is unkown (within a certain range).

#2728 is a potential solution
to remove the penalty.

Another mitigation from @mlazos: you could also copy N loss values on
GPU to keep a record of which of the N steps had the NaN.
jemitche1 pushed a commit to jemitche1/torchtitan that referenced this pull request Jul 15, 2026
In my experience, the training loss can occasionally become NaN for a
variety of reasons. Regardless of the underlying cause, once the loss is
NaN, it quickly propagates through the model, causing all subsequent
computations to produce NaN as well.

I don't think there is a practical scenario where continuing training
after detecting a NaN loss is desirable.

This PR adds a simple check after the loss is computed. If the loss is
NaN, the corresponding rank immediately raises a RuntimeException and
terminates. The remaining distributed ranks will then fail naturally due
to the communication timeout.

I also added the following debug information to the exception message:

```python
f"debugging -- input_dict: {input_dict}, labels: {labels}, "
f"global_valid_tokens: {global_valid_tokens}"
```

I realize this is quite verbose and may produce large log messages, but
I thought it could be useful for debugging. If you would prefer to keep
the logs lighter, I'm happy to remove it.
jemitche1 pushed a commit to jemitche1/torchtitan that referenced this pull request Jul 15, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* __->__ pytorch#3873

pytorch#3863 introduces a good
feature to raise if loss is NaN but also introduces per-micro-batch D2H
sync. This PR fixes the issue with the penalty that the actual batch
that causes the NaN is unkown (within a certain range).

pytorch#2728 is a potential solution
to remove the penalty.

Another mitigation from @mlazos: you could also copy N loss values on
GPU to keep a record of which of the N steps had the NaN.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants