Skip to content

Commit

Permalink
[test/torch_np] Fix usages of deprecated NumPy 2.0 APIs in numpy_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kiukchung committed Jul 26, 2024
1 parent 2988d33 commit c133e8f
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion test/torch_np/numpy_tests/core/test_getlimits.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def test_subnormal_warning(self):
@xpassIfTorchDynamo # (reason="None of nmant, minexp, maxexp is implemented.")
def test_plausible_finfo(self):
# Assert that finfo returns reasonable results for all types
for ftype in np.sctypes["float"] + np.sctypes["complex"]:
for ftype in [np.float16, np.float32, np.float64, np.longdouble] + [np.complex64, np.complex128, np.clongdouble]:
info = np.finfo(ftype)
assert_(info.nmant > 1)
assert_(info.minexp < -1)
Expand Down
4 changes: 2 additions & 2 deletions test/torch_np/numpy_tests/core/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ def _get_multi_index(self, arr, indices):
if np.any(_indx >= _size) or np.any(_indx < -_size):
raise IndexError
if len(indx[1:]) == len(orig_slice):
if np.product(orig_slice) == 0:
if np.prod(orig_slice) == 0:
# Work around for a crash or IndexError with 'wrap'
# in some 0-sized cases.
try:
Expand Down Expand Up @@ -1094,7 +1094,7 @@ def test_non_integer_sequence_multiplication(self):
def mult(a, b):
return a * b

assert_raises(TypeError, mult, [1], np.float_(3))
assert_raises(TypeError, mult, [1], np.float64(3))
# following should be OK
mult([1], np.int_(3))

Expand Down
18 changes: 12 additions & 6 deletions test/torch_np/numpy_tests/core/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_attributes_2(self):

def test_dtypeattr(self):
assert_equal(self.one.dtype, np.dtype(np.int_))
assert_equal(self.three.dtype, np.dtype(np.float_))
assert_equal(self.three.dtype, np.dtype(np.float64))
assert_equal(self.one.dtype.char, "l")
assert_equal(self.three.dtype.char, "d")
assert_(self.three.dtype.str[0] in "<>")
Expand Down Expand Up @@ -697,7 +697,7 @@ def test_longdouble_assignment(self):
# only relevant if longdouble is larger than float
# we're looking for loss of precision

for dtype in (np.longdouble, np.longcomplex):
for dtype in (np.longdouble, np.clongdouble):
# gh-8902
tinyb = np.nextafter(np.longdouble(0), 1).astype(dtype)
tinya = np.nextafter(np.longdouble(0), -1).astype(dtype)
Expand Down Expand Up @@ -1398,7 +1398,7 @@ def test_cast_from_void(self):

@xfail # (reason="See gh-9847")
def test_cast_from_unicode(self):
self._test_cast_from_flexible(np.unicode_)
self._test_cast_from_flexible(np.str_)

@xfail # (reason="See gh-9847")
def test_cast_from_bytes(self):
Expand Down Expand Up @@ -1829,7 +1829,7 @@ def test_argsort_axis(self):
a = np.array(["aaaaaaaaa" for i in range(100)])
assert_equal(a.argsort(kind="m"), r)
# unicode
a = np.array(["aaaaaaaaa" for i in range(100)], dtype=np.unicode_)
a = np.array(["aaaaaaaaa" for i in range(100)], dtype=np.str_)
assert_equal(a.argsort(kind="m"), r)

@xpassIfTorchDynamo # (reason="TODO: searchsorted with nans differs in pytorch")
Expand Down Expand Up @@ -3487,6 +3487,12 @@ def test_basic(self):
res = 250 * sk[:, np.newaxis]
assert_almost_equal(res.ravel(), 250 * sk)

_sctypes = {
'int': [np.int8, np.int16, np.int32, np.int64],
'uint': [np.uint8, np.uint16, np.uint32, np.uint64],
'float': [np.float32, np.float64],
'complex': [np.complex64, np.complex128, np.clongdouble],
}

class TestClip(TestCase):
def _check_range(self, x, cmin, cmax):
Expand All @@ -3508,7 +3514,7 @@ def _clip_type(
if expected_max is None:
expected_max = clip_max

for T in np.sctypes[type_group]:
for T in _sctypes[type_group]:
if sys.byteorder == "little":
byte_orders = ["=", ">"]
else:
Expand Down Expand Up @@ -6412,7 +6418,7 @@ def test_to_int_scalar_2(self):
# gh-9972
assert_equal(4, int_func(np.array("4")))
assert_equal(5, int_func(np.bytes_(b"5")))
assert_equal(6, int_func(np.unicode_("6")))
assert_equal(6, int_func(np.str_("6")))

# The delegation of int() to __trunc__ was deprecated in
# Python 3.11.
Expand Down
13 changes: 6 additions & 7 deletions test/torch_np/numpy_tests/core/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_count_nonzero(self):

def test_cumproduct(self):
A = [[1, 2, 3], [4, 5, 6]]
assert_(np.all(np.cumproduct(A) == np.array([1, 2, 6, 24, 120, 720])))
assert_(np.all(np.cumprod(A) == np.array([1, 2, 6, 24, 120, 720])))

def test_diagonal(self):
a = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
Expand Down Expand Up @@ -701,7 +701,7 @@ def assert_op_raises_fpe(self, fpeerr, flop, sc1, sc2):
@parametrize("typecode", np.typecodes["AllFloat"])
def test_floating_exceptions(self, typecode):
# Test basic arithmetic function errors
ftype = np.obj2sctype(typecode)
ftype = np.dtype(typecode).type
if np.dtype(ftype).kind == "f":
# Get some extreme values for the type
fi = np.finfo(ftype)
Expand Down Expand Up @@ -924,14 +924,14 @@ def test_can_cast_2(self):
@xpassIfTorchDynamo # (reason="value-based casting?")
def test_can_cast_values(self):
# gh-5917
for dt in np.sctypes["int"] + np.sctypes["uint"]:
for dt in [np.int8, np.int16, np.int32, np.int64] + [np.uint8, np.uint16, np.uint32, np.uint64]:
ii = np.iinfo(dt)
assert_(np.can_cast(ii.min, dt))
assert_(np.can_cast(ii.max, dt))
assert_(not np.can_cast(ii.min - 1, dt))
assert_(not np.can_cast(ii.max + 1, dt))

for dt in np.sctypes["float"]:
for dt in [np.float16, np.float32, np.float64, np.longdouble]:
fi = np.finfo(dt)
assert_(np.can_cast(fi.min, dt))
assert_(np.can_cast(fi.max, dt))
Expand Down Expand Up @@ -969,8 +969,8 @@ def test_values(self):
expected = np.array(list(self.makegen()))
a = np.fromiter(self.makegen(), int)
a20 = np.fromiter(self.makegen(), int, 20)
assert_(np.alltrue(a == expected, axis=0))
assert_(np.alltrue(a20 == expected[:20], axis=0))
assert_(np.all(a == expected, axis=0))
assert_(np.all(a20 == expected[:20], axis=0))

def load_data(self, n, eindex):
# Utility method for the issue 2592 tests.
Expand Down Expand Up @@ -2159,7 +2159,6 @@ class TestCreationFuncs(TestCase):

def setUp(self):
super().setUp()
# dtypes = {np.dtype(tp) for tp in itertools.chain.from_iterable(np.sctypes.values())}
dtypes = {np.dtype(tp) for tp in "efdFDBbhil?"}
self.dtypes = dtypes
self.orders = {
Expand Down
2 changes: 1 addition & 1 deletion test/torch_np/numpy_tests/core/test_scalar_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_class_getitem_38(self, cls: Type[np.number]) -> None:
class TestBitCount(TestCase):
# derived in part from the cpython test "test_bit_count"

@parametrize("itype", np.sctypes["int"] + np.sctypes["uint"])
@parametrize("itype", [np.int8, np.int16, np.int32, np.int64] + [np.uint8, np.uint16, np.uint32, np.uint64])
def test_small(self, itype):
for a in range(max(np.iinfo(itype).min, 0), 128):
msg = f"Smoke test for {itype}({a}).bit_count()"
Expand Down
14 changes: 7 additions & 7 deletions test/torch_np/numpy_tests/lib/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _make_complex(real, imag):
Like real + 1j * imag, but behaves as expected when imag contains non-finite
values
"""
ret = np.zeros(np.broadcast(real, imag).shape, np.complex_)
ret = np.zeros(np.broadcast(real, imag).shape, np.complex128)
ret.real = real
ret.imag = imag
return ret
Expand Down Expand Up @@ -268,8 +268,8 @@ def test_basic(self):
def test_nd(self):
y1 = [[0, 0, 0], [0, 1, 0], [1, 1, 0]]
assert_(np.any(y1))
assert_array_equal(np.sometrue(y1, axis=0), [1, 1, 0])
assert_array_equal(np.sometrue(y1, axis=1), [0, 1, 1])
assert_array_equal(np.any(y1, axis=0), [1, 1, 0])
assert_array_equal(np.any(y1, axis=1), [0, 1, 1])


class TestAll(TestCase):
Expand All @@ -285,8 +285,8 @@ def test_basic(self):
def test_nd(self):
y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]]
assert_(not np.all(y1))
assert_array_equal(np.alltrue(y1, axis=0), [0, 0, 1])
assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1])
assert_array_equal(np.all(y1, axis=0), [0, 0, 1])
assert_array_equal(np.all(y1, axis=1), [0, 0, 1])


class TestCopy(TestCase):
Expand Down Expand Up @@ -496,7 +496,7 @@ def test_broadcasting(self):
assert_equal(select([True], [0], default=[0]).shape, (1,))

def test_return_dtype(self):
assert_equal(select(self.conditions, self.choices, 1j).dtype, np.complex_)
assert_equal(select(self.conditions, self.choices, 1j).dtype, np.complex128)
# But the conditions need to be stronger then the scalar default
# if it is scalar.
choices = [choice.astype(np.int8) for choice in self.choices]
Expand Down Expand Up @@ -2607,7 +2607,7 @@ def test_error_not_1d(self, vals):
parametrize_interp_sc = parametrize(
"sc",
[
subtest(lambda x: np.float_(x), name="real"),
subtest(lambda x: np.float64(x), name="real"),
subtest(lambda x: _make_complex(x, 0), name="complex-real"),
subtest(lambda x: _make_complex(0, x), name="complex-imag"),
subtest(lambda x: _make_complex(x, np.multiply(x, -2)), name="complex-both"),
Expand Down
14 changes: 7 additions & 7 deletions test/torch_np/numpy_tests/lib/test_type_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class TestIscomplex(TestCase):
def test_fail(self):
z = np.array([-1, 0, 1])
res = iscomplex(z)
assert_(not np.sometrue(res, axis=0))
assert_(not np.any(res, axis=0))

def test_pass(self):
z = np.array([-1j, 1, 0])
Expand Down Expand Up @@ -390,19 +390,19 @@ def test_integer(self):
def test_float(self):
vals = nan_to_num(1.0)
assert_all(vals == 1.0)
assert_equal(type(vals), np.float_)
assert_equal(type(vals), np.float64)
vals = nan_to_num(1.1, nan=10, posinf=20, neginf=30)
assert_all(vals == 1.1)
assert_equal(type(vals), np.float_)
assert_equal(type(vals), np.float64)

@skip(reason="we return OD arrays not scalars")
def test_complex_good(self):
vals = nan_to_num(1 + 1j)
assert_all(vals == 1 + 1j)
assert isinstance(vals, np.complex_)
assert isinstance(vals, np.complex128)
vals = nan_to_num(1 + 1j, nan=10, posinf=20, neginf=30)
assert_all(vals == 1 + 1j)
assert_equal(type(vals), np.complex_)
assert_equal(type(vals), np.complex128)

@skip(reason="we return OD arrays not scalars")
def test_complex_bad(self):
Expand All @@ -411,15 +411,15 @@ def test_complex_bad(self):
vals = nan_to_num(v)
# !! This is actually (unexpectedly) zero
assert_all(np.isfinite(vals))
assert_equal(type(vals), np.complex_)
assert_equal(type(vals), np.complex128)

@skip(reason="we return OD arrays not scalars")
def test_complex_bad2(self):
v = 1 + 1j
v += np.array(-1 + 1.0j) / 0.0
vals = nan_to_num(v)
assert_all(np.isfinite(vals))
assert_equal(type(vals), np.complex_)
assert_equal(type(vals), np.complex128)
# Fixme
# assert_all(vals.imag > 1e10) and assert_all(np.isfinite(vals))
# !! This is actually (unexpectedly) positive
Expand Down
2 changes: 1 addition & 1 deletion test/torch_np/numpy_tests/linalg/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ def test_nan(self):
A[0, 1] = np.nan
for p in ps:
c = linalg.cond(A, p)
assert_(isinstance(c, np.float_))
assert_(isinstance(c, np.float64))
assert_(np.isnan(c))

A = np.ones((3, 2, 2))
Expand Down

0 comments on commit c133e8f

Please sign in to comment.