Skip to content

Commit 2a5ac15

Browse files
committed
DOC: a bit more on RuntimeWarnings
1 parent 61322fd commit 2a5ac15

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

torch_np/_dtypes_impl.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ def category(dtyp):
8484

8585

8686
def nep50_to_tensors(x1, x2, handle_weaks):
87-
"""If either of inputs is a python scalar, type-promote with NEP 50.
88-
89-
NB: NEP 50 mandates RuntimeWarnings on some overflows. We do not emit them:
90-
we either raise an OverflowError or silently do the computation.
91-
"""
87+
"""If either of inputs is a python scalar, type-promote with NEP 50."""
9288
x1_type, x2_type = type(x1), type(x2)
9389
if x1_type == torch.Tensor and x2_type == torch.Tensor:
9490
# two tensors: nothing to do here
@@ -120,6 +116,11 @@ def nep50_to_tensors(x1, x2, handle_weaks):
120116

121117
# detect overflows: in PyTorch, uint8(-1) wraps around to 255,
122118
# while NEP50 mandates an exception.
119+
#
120+
# Note that we only check if each element of the binop overflows,
121+
# not the result. Consider, e.g. `uint8(100) + 200`. Operands are OK
122+
# in uint8, but the result overflows and wrap around 255.
123+
# Numpy emits a RuntimeWarning, PyTorch does not, and we do not either.
123124
if cat_weak == 1 and cat_not_weak == 1:
124125
# integers
125126
iinfo = torch.iinfo(not_weak.dtype)

0 commit comments

Comments
 (0)