Skip to content

Commit c84309c

Browse files
Fixed failed test due to uint8 overflow
In numpy 2.0, -1 as uint8 is out of bounds, whereas previously it would be converted to 255. This affected the test helper function `reduced_bitwise_and`. The helper function was changed to use 255 instead of -1 if the dtype was uint8, since this is what is needed to match the behavior of the "bitwise and" op. `reduced_bitwise_and` was only used by `TestCAReduce` in `tests/tensor/test_elemwise.py`, so it was moved there from `tests/tensor/test_math.py`
1 parent 634d13e commit c84309c

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

tests/compile/function/test_pfunc.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,18 @@ def test_allow_input_downcast_int(self):
327327
with pytest.raises(TypeError):
328328
g([3], np.array([6], dtype="int16"), 0)
329329

330-
# Value too big for b, raises TypeError
331-
with pytest.raises(TypeError):
330+
# Value too big for b, raises OverflowError in numpy >= 2.0, TypeError in numpy <2.0
331+
with pytest.raises(uint_overflow_error):
332332
g([3], [312], 0)
333333

334334
h = pfunc([a, b, c], (a + b + c)) # Default: allow_input_downcast=None
335335
# Everything here should behave like with False
336336
assert np.all(h([3], [6], 0) == 9)
337337
with pytest.raises(TypeError):
338338
h([3], np.array([6], dtype="int16"), 0)
339-
with pytest.raises(TypeError):
339+
340+
# Value too big for b, raises OverflowError in numpy >= 2.0, TypeError in numpy <2.0
341+
with pytest.raises(uint_overflow_error):
340342
h([3], [312], 0)
341343

342344
def test_allow_downcast_floatX(self):

tests/tensor/test_elemwise.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,27 @@
3939
)
4040
from tests import unittest_tools
4141
from tests.link.test_link import make_function
42-
from tests.tensor.test_math import reduce_bitwise_and
42+
43+
44+
def reduce_bitwise_and(x, axis=-1, dtype="int8"):
45+
"""Helper function for TestCAReduce"""
46+
if dtype == "uint8":
47+
# in numpy version >= 2.0, out of bounds uint8 values are not converted
48+
identity = np.array((255,), dtype=dtype)[0]
49+
else:
50+
identity = np.array((-1,), dtype=dtype)[0]
51+
52+
shape_without_axis = tuple(s for i, s in enumerate(x.shape) if i != axis)
53+
if 0 in shape_without_axis:
54+
return np.empty(shape=shape_without_axis, dtype=x.dtype)
55+
56+
def custom_reduce(a):
57+
out = identity
58+
for i in range(a.size):
59+
out = np.bitwise_and(a[i], out)
60+
return out
61+
62+
return np.apply_along_axis(custom_reduce, axis, x)
4363

4464

4565
class TestDimShuffle(unittest_tools.InferShapeTester):

tests/tensor/test_math.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,22 +3409,6 @@ def test_var_axes(self):
34093409
x.var(a)
34103410

34113411

3412-
def reduce_bitwise_and(x, axis=-1, dtype="int8"):
3413-
identity = np.array((-1,), dtype=dtype)[0]
3414-
3415-
shape_without_axis = tuple(s for i, s in enumerate(x.shape) if i != axis)
3416-
if 0 in shape_without_axis:
3417-
return np.empty(shape=shape_without_axis, dtype=x.dtype)
3418-
3419-
def custom_reduce(a):
3420-
out = identity
3421-
for i in range(a.size):
3422-
out = np.bitwise_and(a[i], out)
3423-
return out
3424-
3425-
return np.apply_along_axis(custom_reduce, axis, x)
3426-
3427-
34283412
def test_clip_grad():
34293413
# test the gradient of clip
34303414
def func(x, y, z):

0 commit comments

Comments
 (0)