@@ -84,11 +84,7 @@ def category(dtyp):
84
84
85
85
86
86
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."""
92
88
x1_type , x2_type = type (x1 ), type (x2 )
93
89
if x1_type == torch .Tensor and x2_type == torch .Tensor :
94
90
# two tensors: nothing to do here
@@ -120,6 +116,11 @@ def nep50_to_tensors(x1, x2, handle_weaks):
120
116
121
117
# detect overflows: in PyTorch, uint8(-1) wraps around to 255,
122
118
# 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.
123
124
if cat_weak == 1 and cat_not_weak == 1 :
124
125
# integers
125
126
iinfo = torch .iinfo (not_weak .dtype )
0 commit comments