Repository navigation
Crash on invalid loss (i.e. infinite or nan) - #3863
Conversation
|
|
tianyu-l
left a comment
There was a problem hiding this comment.
makes sense to me, thank you!
|
lint error should be fixed |
|
mmmh, it does not seem to be related to my modification: Am I reading this correctly? |
|
seems CI is broken. I'll merge. |
| global_valid_tokens=global_valid_tokens, | ||
| ) | ||
|
|
||
| if not loss.isfinite(): |
There was a problem hiding this comment.
I think this will cause a D2H sync every microbatch, which is not a good idea. cc., @tianyu-l
There was a problem hiding this comment.
is it? Could you share a code pointer or documentation for the fact? @fegin
There was a problem hiding this comment.
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.
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.
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.
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.
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:
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.