Skip to content

Commit ea4e81e

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 c4b6577 commit ea4e81e

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

tests/compile/function/test_function.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pytensor.compile.function import function, function_dump
1212
from pytensor.compile.io import In
1313
from pytensor.configdefaults import config
14+
from pytensor.npy_2_compat import UintOverflowError
1415
from pytensor.tensor.type import (
1516
bscalar,
1617
bvector,
@@ -166,12 +167,12 @@ def test_in_allow_downcast_int(self):
166167
# Value too big for a, silently ignored
167168
assert np.array_equal(f([2**20], np.ones(1, dtype="int8"), 1), [2])
168169

169-
# Value too big for b, raises TypeError
170-
with pytest.raises(TypeError):
170+
# Value too big for b, raises OverflowError (in numpy >= 2.0... TypeError in numpy < 2.0)
171+
with pytest.raises(UintOverflowError):
171172
f([3], [312], 1)
172173

173-
# Value too big for c, raises TypeError
174-
with pytest.raises(TypeError):
174+
# Value too big for c, raises OverflowError
175+
with pytest.raises(UintOverflowError):
175176
f([3], [6], 806)
176177

177178
def test_in_allow_downcast_floatX(self):

tests/compile/function/test_pfunc.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pytensor.compile.sharedvalue import shared
1010
from pytensor.configdefaults import config
1111
from pytensor.graph.utils import MissingInputError
12+
from pytensor.npy_2_compat import UintOverflowError
1213
from pytensor.tensor.math import sum as pt_sum
1314
from pytensor.tensor.type import (
1415
bscalar,
@@ -237,12 +238,12 @@ def test_param_allow_downcast_int(self):
237238
# Value too big for a, silently ignored
238239
assert np.all(f([2**20], np.ones(1, dtype="int8"), 1) == 2)
239240

240-
# Value too big for b, raises TypeError
241-
with pytest.raises(TypeError):
241+
# Value too big for b, raises OverflowError in numpy >= 2.0, TypeError in numpy <2.0
242+
with pytest.raises(UintOverflowError):
242243
f([3], [312], 1)
243244

244-
# Value too big for c, raises TypeError
245-
with pytest.raises(TypeError):
245+
# Value too big for c, raises OverflowError in numpy >= 2.0, TypeError in numpy <2.0
246+
with pytest.raises(UintOverflowError):
246247
f([3], [6], 806)
247248

248249
def test_param_allow_downcast_floatX(self):
@@ -327,16 +328,18 @@ def test_allow_input_downcast_int(self):
327328
with pytest.raises(TypeError):
328329
g([3], np.array([6], dtype="int16"), 0)
329330

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

334335
h = pfunc([a, b, c], (a + b + c)) # Default: allow_input_downcast=None
335336
# Everything here should behave like with False
336337
assert np.all(h([3], [6], 0) == 9)
337338
with pytest.raises(TypeError):
338339
h([3], np.array([6], dtype="int16"), 0)
339-
with pytest.raises(TypeError):
340+
341+
# Value too big for b, raises OverflowError in numpy >= 2.0, TypeError in numpy <2.0
342+
with pytest.raises(UintOverflowError):
340343
h([3], [312], 0)
341344

342345
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
@@ -40,7 +40,27 @@
4040
)
4141
from tests import unittest_tools
4242
from tests.link.test_link import make_function
43-
from tests.tensor.test_math import reduce_bitwise_and
43+
44+
45+
def reduce_bitwise_and(x, axis=-1, dtype="int8"):
46+
"""Helper function for TestCAReduce"""
47+
if dtype == "uint8":
48+
# in numpy version >= 2.0, out of bounds uint8 values are not converted
49+
identity = np.array((255,), dtype=dtype)[0]
50+
else:
51+
identity = np.array((-1,), dtype=dtype)[0]
52+
53+
shape_without_axis = tuple(s for i, s in enumerate(x.shape) if i != axis)
54+
if 0 in shape_without_axis:
55+
return np.empty(shape=shape_without_axis, dtype=x.dtype)
56+
57+
def custom_reduce(a):
58+
out = identity
59+
for i in range(a.size):
60+
out = np.bitwise_and(a[i], out)
61+
return out
62+
63+
return np.apply_along_axis(custom_reduce, axis, x)
4464

4565

4666
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)