Skip to content

Commit

Permalink
torch._numpy: remove noops and half-implemented nan-functions (pytorc…
Browse files Browse the repository at this point in the history
…h#107596)

As discussed in the review of pytorch#106211, remove several noops (pytorch#106211 (review) and pytorch#106211 (review)).

Pull Request resolved: pytorch#107596
Approved by: https://github.com/lezcano
  • Loading branch information
ev-br authored and pytorchmergebot committed Aug 21, 2023
1 parent f5d1df3 commit da67b41
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 1,663 deletions.
6 changes: 2 additions & 4 deletions test/test_binary_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,7 @@ def test_div_rounding_nonfinite(self, device, dtype):
an, bn = a.float().cpu().numpy(), b.float().cpu().numpy()

for mode, np_ref in ((None, np.true_divide), ("floor", np.floor_divide)):
with np.errstate(all="ignore"):
expect = np_ref(an, bn)
expect = np_ref(an, bn)
kwargs = dict(rounding_mode=mode) if mode is not None else {}
with set_default_dtype(torch.double):
actual = torch.divide(a, b, **kwargs)
Expand Down Expand Up @@ -1063,8 +1062,7 @@ def test_div_rounding_numpy(self, device, dtype):
("floor", np.floor_divide),
("trunc", lambda a, b: np.trunc(np.true_divide(a, b)).astype(a.dtype)),
):
with np.errstate(all="ignore"):
expect = torch.from_numpy(np_ref(an, bn))
expect = torch.from_numpy(np_ref(an, bn))

kwargs = dict(rounding_mode=mode) if mode is not None else {}
# Contiguous (likely vectorized)
Expand Down
9 changes: 3 additions & 6 deletions test/torch_np/numpy_tests/core/test_getlimits.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def test_basic(self):
def test_unsigned_max(self):
types = np.sctypes["uint"]
for T in types:
with np.errstate(over="ignore"):
max_calculated = T(0) - T(1)
max_calculated = T(0) - T(1)
assert_equal(iinfo(T).max, max_calculated)


Expand Down Expand Up @@ -154,8 +153,7 @@ def test_known_types():
):
assert_ma_equal(_discovered_machar(ftype), ma_like)
# Suppress warning for broken discovery of double double on PPC
with np.errstate(all="ignore"):
ld_ma = _discovered_machar(np.longdouble)
ld_ma = _discovered_machar(np.longdouble)
bytes = np.dtype(np.longdouble).itemsize
if (ld_ma.it, ld_ma.maxexp) == (63, 16384) and bytes in (12, 16):
# 80-bit extended precision
Expand All @@ -168,8 +166,7 @@ def test_known_types():
@pytest.mark.skip(reason="MachAr no implemented (does it need to be)?")
def test_subnormal_warning():
"""Test that the subnormal is zero warning is not being raised."""
with np.errstate(all="ignore"):
ld_ma = _discovered_machar(np.longdouble)
ld_ma = _discovered_machar(np.longdouble)
bytes = np.dtype(np.longdouble).itemsize
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
Expand Down
166 changes: 77 additions & 89 deletions test/torch_np/numpy_tests/core/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,54 +552,49 @@ def test_default(self):
)

def test_set(self):
with np.errstate():
err = np.seterr()
old = np.seterr(divide="print")
assert_(err == old)
new = np.seterr()
assert_(new["divide"] == "print")
np.seterr(over="raise")
assert_(np.geterr()["over"] == "raise")
assert_(new["divide"] == "print")
np.seterr(**old)
assert_(np.geterr() == old)
err = np.seterr()
old = np.seterr(divide="print")
assert_(err == old)
new = np.seterr()
assert_(new["divide"] == "print")
np.seterr(over="raise")
assert_(np.geterr()["over"] == "raise")
assert_(new["divide"] == "print")
np.seterr(**old)
assert_(np.geterr() == old)

@pytest.mark.skipif(IS_WASM, reason="no wasm fp exception support")
@pytest.mark.skipif(platform.machine() == "armv5tel", reason="See gh-413.")
def test_divide_err(self):
with np.errstate(divide="raise"):
with assert_raises(FloatingPointError):
np.array([1.0]) / np.array([0.0])

np.seterr(divide="ignore")
with assert_raises(FloatingPointError):
np.array([1.0]) / np.array([0.0])

np.seterr(divide="ignore")
np.array([1.0]) / np.array([0.0])

@pytest.mark.skipif(IS_WASM, reason="no wasm fp exception support")
def test_errobj(self):
olderrobj = np.geterrobj()
self.called = 0
try:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
with np.errstate(divide="warn"):
np.seterrobj([20000, 1, None])
np.array([1.0]) / np.array([0.0])
assert_equal(len(w), 1)
np.seterrobj([20000, 1, None])
np.array([1.0]) / np.array([0.0])
assert_equal(len(w), 1)

def log_err(*args):
self.called += 1
extobj_err = args
assert_(len(extobj_err) == 2)
assert_("divide" in extobj_err[0])

with np.errstate(divide="ignore"):
np.seterrobj([20000, 3, log_err])
np.array([1.0]) / np.array([0.0])
np.seterrobj([20000, 3, log_err])
np.array([1.0]) / np.array([0.0])
assert_equal(self.called, 1)

np.seterrobj(olderrobj)
with np.errstate(divide="ignore"):
np.divide(1.0, 0.0, extobj=[20000, 3, log_err])
np.divide(1.0, 0.0, extobj=[20000, 3, log_err])
assert_equal(self.called, 2)
finally:
np.seterrobj(olderrobj)
Expand Down Expand Up @@ -652,74 +647,68 @@ def assert_op_raises_fpe(self, fpeerr, flop, sc1, sc2):
@pytest.mark.parametrize("typecode", np.typecodes["AllFloat"])
def test_floating_exceptions(self, typecode):
# Test basic arithmetic function errors
with np.errstate(all="raise"):
ftype = np.obj2sctype(typecode)
if np.dtype(ftype).kind == "f":
# Get some extreme values for the type
fi = np.finfo(ftype)
ft_tiny = fi._machar.tiny
ft_max = fi.max
ft_eps = fi.eps
underflow = "underflow"
divbyzero = "divide by zero"
else:
# 'c', complex, corresponding real dtype
rtype = type(ftype(0).real)
fi = np.finfo(rtype)
ft_tiny = ftype(fi._machar.tiny)
ft_max = ftype(fi.max)
ft_eps = ftype(fi.eps)
# The complex types raise different exceptions
underflow = ""
divbyzero = ""
overflow = "overflow"
invalid = "invalid"

# The value of tiny for double double is NaN, so we need to
# pass the assert
if not np.isnan(ft_tiny):
self.assert_raises_fpe(underflow, lambda a, b: a / b, ft_tiny, ft_max)
self.assert_raises_fpe(underflow, lambda a, b: a * b, ft_tiny, ft_tiny)
self.assert_raises_fpe(overflow, lambda a, b: a * b, ft_max, ftype(2))
self.assert_raises_fpe(overflow, lambda a, b: a / b, ft_max, ftype(0.5))
self.assert_raises_fpe(
overflow, lambda a, b: a + b, ft_max, ft_max * ft_eps
)
self.assert_raises_fpe(
overflow, lambda a, b: a - b, -ft_max, ft_max * ft_eps
)
self.assert_raises_fpe(overflow, np.power, ftype(2), ftype(2**fi.nexp))
self.assert_raises_fpe(divbyzero, lambda a, b: a / b, ftype(1), ftype(0))
self.assert_raises_fpe(
invalid, lambda a, b: a / b, ftype(np.inf), ftype(np.inf)
)
self.assert_raises_fpe(invalid, lambda a, b: a / b, ftype(0), ftype(0))
self.assert_raises_fpe(
invalid, lambda a, b: a - b, ftype(np.inf), ftype(np.inf)
)
self.assert_raises_fpe(
invalid, lambda a, b: a + b, ftype(np.inf), ftype(-np.inf)
)
self.assert_raises_fpe(invalid, lambda a, b: a * b, ftype(0), ftype(np.inf))
ftype = np.obj2sctype(typecode)
if np.dtype(ftype).kind == "f":
# Get some extreme values for the type
fi = np.finfo(ftype)
ft_tiny = fi._machar.tiny
ft_max = fi.max
ft_eps = fi.eps
underflow = "underflow"
divbyzero = "divide by zero"
else:
# 'c', complex, corresponding real dtype
rtype = type(ftype(0).real)
fi = np.finfo(rtype)
ft_tiny = ftype(fi._machar.tiny)
ft_max = ftype(fi.max)
ft_eps = ftype(fi.eps)
# The complex types raise different exceptions
underflow = ""
divbyzero = ""
overflow = "overflow"
invalid = "invalid"

# The value of tiny for double double is NaN, so we need to
# pass the assert
if not np.isnan(ft_tiny):
self.assert_raises_fpe(underflow, lambda a, b: a / b, ft_tiny, ft_max)
self.assert_raises_fpe(underflow, lambda a, b: a * b, ft_tiny, ft_tiny)
self.assert_raises_fpe(overflow, lambda a, b: a * b, ft_max, ftype(2))
self.assert_raises_fpe(overflow, lambda a, b: a / b, ft_max, ftype(0.5))
self.assert_raises_fpe(overflow, lambda a, b: a + b, ft_max, ft_max * ft_eps)
self.assert_raises_fpe(overflow, lambda a, b: a - b, -ft_max, ft_max * ft_eps)
self.assert_raises_fpe(overflow, np.power, ftype(2), ftype(2**fi.nexp))
self.assert_raises_fpe(divbyzero, lambda a, b: a / b, ftype(1), ftype(0))
self.assert_raises_fpe(
invalid, lambda a, b: a / b, ftype(np.inf), ftype(np.inf)
)
self.assert_raises_fpe(invalid, lambda a, b: a / b, ftype(0), ftype(0))
self.assert_raises_fpe(
invalid, lambda a, b: a - b, ftype(np.inf), ftype(np.inf)
)
self.assert_raises_fpe(
invalid, lambda a, b: a + b, ftype(np.inf), ftype(-np.inf)
)
self.assert_raises_fpe(invalid, lambda a, b: a * b, ftype(0), ftype(np.inf))

@pytest.mark.skipif(IS_WASM, reason="no wasm fp exception support")
def test_warnings(self):
# test warning code path
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
with np.errstate(all="warn"):
np.divide(1, 0.0)
assert_equal(len(w), 1)
assert_("divide by zero" in str(w[0].message))
np.array(1e300) * np.array(1e300)
assert_equal(len(w), 2)
assert_("overflow" in str(w[-1].message))
np.array(np.inf) - np.array(np.inf)
assert_equal(len(w), 3)
assert_("invalid value" in str(w[-1].message))
np.array(1e-300) * np.array(1e-300)
assert_equal(len(w), 4)
assert_("underflow" in str(w[-1].message))
np.divide(1, 0.0)
assert_equal(len(w), 1)
assert_("divide by zero" in str(w[0].message))
np.array(1e300) * np.array(1e300)
assert_equal(len(w), 2)
assert_("overflow" in str(w[-1].message))
np.array(np.inf) - np.array(np.inf)
assert_equal(len(w), 3)
assert_("invalid value" in str(w[-1].message))
np.array(1e-300) * np.array(1e-300)
assert_equal(len(w), 4)
assert_("underflow" in str(w[-1].message))


class TestTypes:
Expand Down Expand Up @@ -2315,8 +2304,7 @@ def test_filled_like(self):
self.check_like_function(np.full_like, 1000, True)
self.check_like_function(np.full_like, 123.456, True)
# Inf to integer casts cause invalid-value errors: ignore them.
with np.errstate(invalid="ignore"):
self.check_like_function(np.full_like, np.inf, True)
self.check_like_function(np.full_like, np.inf, True)

@pytest.mark.parametrize(
"likefunc", [np.empty_like, np.full_like, np.zeros_like, np.ones_like]
Expand Down
Loading

0 comments on commit da67b41

Please sign in to comment.