Skip to content

Commit

Permalink
Fixed failed test due to uint8 overflow
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
brendan-m-murphy committed Feb 6, 2025
1 parent 625dd67 commit b694013
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
8 changes: 5 additions & 3 deletions tests/compile/function/test_pfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,18 @@ def test_allow_input_downcast_int(self):
with pytest.raises(TypeError):
g([3], np.array([6], dtype="int16"), 0)

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

h = pfunc([a, b, c], (a + b + c)) # Default: allow_input_downcast=None
# Everything here should behave like with False
assert np.all(h([3], [6], 0) == 9)
with pytest.raises(TypeError):
h([3], np.array([6], dtype="int16"), 0)
with pytest.raises(TypeError):

# Value too big for b, raises OverflowError in numpy >= 2.0, TypeError in numpy <2.0
with pytest.raises(uint_overflow_error):
h([3], [312], 0)

def test_allow_downcast_floatX(self):
Expand Down
22 changes: 21 additions & 1 deletion tests/tensor/test_elemwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,27 @@
)
from tests import unittest_tools
from tests.link.test_link import make_function
from tests.tensor.test_math import reduce_bitwise_and


def reduce_bitwise_and(x, axis=-1, dtype="int8"):
"""Helper function for TestCAReduce"""
if dtype == "uint8":
# in numpy version >= 2.0, out of bounds uint8 values are not converted
identity = np.array((255,), dtype=dtype)[0]
else:
identity = np.array((-1,), dtype=dtype)[0]

shape_without_axis = tuple(s for i, s in enumerate(x.shape) if i != axis)
if 0 in shape_without_axis:
return np.empty(shape=shape_without_axis, dtype=x.dtype)

def custom_reduce(a):
out = identity
for i in range(a.size):
out = np.bitwise_and(a[i], out)
return out

return np.apply_along_axis(custom_reduce, axis, x)


class TestDimShuffle(unittest_tools.InferShapeTester):
Expand Down
16 changes: 0 additions & 16 deletions tests/tensor/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -3409,22 +3409,6 @@ def test_var_axes(self):
x.var(a)


def reduce_bitwise_and(x, axis=-1, dtype="int8"):
identity = np.array((-1,), dtype=dtype)[0]

shape_without_axis = tuple(s for i, s in enumerate(x.shape) if i != axis)
if 0 in shape_without_axis:
return np.empty(shape=shape_without_axis, dtype=x.dtype)

def custom_reduce(a):
out = identity
for i in range(a.size):
out = np.bitwise_and(a[i], out)
return out

return np.apply_along_axis(custom_reduce, axis, x)


def test_clip_grad():
# test the gradient of clip
def func(x, y, z):
Expand Down

0 comments on commit b694013

Please sign in to comment.