-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import unittest.mock as mock | ||
|
||
import pytest | ||
import torch | ||
from transformer_nuggets.utils.tracing import NanInfDetect | ||
|
||
|
||
def test_nan(): | ||
a = torch.tensor( | ||
[ | ||
0.0, | ||
] | ||
) | ||
with pytest.raises(RuntimeError, match="returned a NaN"): | ||
with NanInfDetect(): | ||
print(torch.div(a, a)) | ||
|
||
|
||
def test_inf(): | ||
a = torch.tensor( | ||
[ | ||
1.0, | ||
], | ||
dtype=torch.float16, | ||
) | ||
with pytest.raises(RuntimeError, match="returned an Inf"): | ||
with NanInfDetect(): | ||
print(torch.mul(a, 65537)) | ||
|
||
|
||
def test_breakpoint(): | ||
a = torch.tensor( | ||
[ | ||
0.0, | ||
] | ||
) | ||
with pytest.raises(RuntimeError, match="returned a NaN"): | ||
with mock.patch("builtins.breakpoint") as mock_breakpoint: | ||
with NanInfDetect(do_breakpoint=True): | ||
print(torch.div(a, a)) | ||
mock_breakpoint.assert_called_once() | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
This file contains 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