E577 torch._numpy: remove noops and half-implemented nan-functions (#107596) · pytorch/pytorch@da67b41 · GitHub
[go: up one dir, main page]

Skip to content

Commit da67b41

Browse files
ev-brpytorchmergebot
authored andcommitted
torch._numpy: remove noops and half-implemented nan-functions (#107596)
As discussed in the review of #106211, remove several noops (#106211 (review) and #106211 (review)). Pull Request resolved: #107596 Approved by: https://github.com/lezcano
1 parent f5d1df3 commit da67b41

File tree

12 files changed

+183
-1663
lines changed

12 files changed

+183
-1663
lines changed

test/test_binary_ufuncs.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,7 @@ def test_div_rounding_nonfinite(self, device, dtype):
985985
an, bn = a.float().cpu().numpy(), b.float().cpu().numpy()
986986

987987
for mode, np_ref in ((None, np.true_divide), ("floor", np.floor_divide)):
988-
with np.errstate(all="ignore"):
989-
expect = np_ref(an, bn)
988+
expect = np_ref(an, bn)
990989
kwargs = dict(rounding_mode=mode) if mode is not None else {}
991990
with set_default_dtype(torch.double):
992991
actual = torch.divide(a, b, **kwargs)
@@ -1063,8 +1062,7 @@ def test_div_rounding_numpy(self, device, dtype):
10631062
("floor", np.floor_divide),
10641063
("trunc", lambda a, b: np.trunc(np.true_divide(a, b)).astype(a.dtype)),
10651064
):
1066-
with np.errstate(all="ignore"):
1067-
expect = torch.from_numpy(np_ref(an, bn))
1065+
expect = torch.from_numpy(np_ref(an, bn))
10681066

10691067
kwargs = dict(rounding_mode=mode) if mode is not None else {}
10701068
# Contiguous (likely vectorized)

test/torch_np/numpy_tests/core/test_getlimits.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ def test_basic(self):
112112
def test_unsigned_max(self):
113113
types = np.sctypes["uint"]
114114
for T in types:
115-
with np.errstate(over="ignore"):
116-
max_calculated = T(0) - T(1)
115+
max_calculated = T(0) - T(1)
117116
assert_equal(iinfo(T).max, max_calculated)
118117

119118

@@ -154,8 +153,7 @@ def test_known_types():
154153
):
155154
assert_ma_equal(_discovered_machar(ftype), ma_like)
156155
# Suppress warning for broken discovery of double double on PPC
157-
with np.errstate(all="ignore"):
158-
ld_ma = _discovered_machar(np.longdouble)
156+
ld_ma = _discovered_machar(np.longdouble)
159157
bytes = np.dtype(np.longdouble).itemsize
160158
if (ld_ma.it, ld_ma.maxexp) == (63, 16384) and bytes in (12, 16):
161159
# 80-bit extended precision
@@ -168,8 +166,7 @@ def test_known_types():
168166
@pytest.mark.skip(reason="MachAr no implemented (does it need to be)?")
169167
def test_subnormal_warning():
170168
"""Test that the subnormal is zero warning is not being raised."""
171-
with np.errstate(all="ignore"):
172-
ld_ma = _discovered_machar(np.longdouble)
169+
ld_ma = _discovered_machar(np.longdouble)
173170
bytes = np.dtype(np.longdouble).itemsize
174171
with warnings.catch_warnings(record=True) as w:
175172
warnings.simplefilter("always")

test/torch_np/numpy_tests/core/test_numeric.py

Lines changed: 77 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -552,54 +552,49 @@ def test_default(self):
552552
)
553553

554554
def test_set(self):
555-
with np.errstate():
556-
err = np.seterr()
557-
old = np.seterr(divide="print")
558-
assert_(err == old)
559-
new = np.seterr()
560-
assert_(new["divide"] == "print")
561-
np.seterr(over="raise")
562-
assert_(np.geterr()["over"] == "raise")
563-
assert_(new["divide"] == "print")
564-
np.seterr(**old)
565-
assert_(np.geterr() == old)
555+
err = np.seterr()
556+
old = np.seterr(divide="print")
557+
assert_(err == old)
558+
new = np.seterr()
559+
assert_(new["divide"] == "print")
560+
np.seterr(over="raise")
561+
assert_(np.geterr()["over"] == "raise")
562+
assert_(new["divide"] == "print")
563+
np.seterr(**old)
564+
assert_(np.geterr() == old)
566565

567566
@pytest.mark.skipif(IS_WASM, reason="no wasm fp exception support")
568567
@pytest.mark.skipif(platform.machine() == "armv5tel", reason="See gh-413.")
569568
def test_divide_err(self):
570-
with np.errstate(divide="raise"):
571-
with assert_raises(FloatingPointError):
572-
np.array([1.0]) / np.array([0.0])
573-
574-
np.seterr(divide="ignore")
569+
with assert_raises(FloatingPointError):
575570
np.array([1.0]) / np.array([0.0])
576571

572+
np.seterr(divide="ignore")
573+
np.array([1.0]) / np.array([0.0])
574+
577575
@pytest.mark.skipif(IS_WASM, reason="no wasm fp exception support")
578576
def test_errobj(self):
579577
olderrobj = np.geterrobj()
580578
self.called = 0
581579
try:
582580
with warnings.catch_warnings(record=True) as w:
583581
warnings.simplefilter("always")
584-
with np.errstate(divide="warn"):
585-
np.seterrobj([20000, 1, None])
586-
np.array([1.0]) / np.array([0.0])
587-
assert_equal(len(w), 1)
582+
np.seterrobj([20000, 1, None])
583+
np.array([1.0]) / np.array([0.0])
584+
assert_equal(len(w), 1)
588585

589586
def log_err(*args):
590587
self.called += 1
591588
extobj_err = args
592589
assert_(len(extobj_err) == 2)
593590
assert_("divide" in extobj_err[0])
594591

595-
with np.errstate(divide="ignore"):
596-
np.seterrobj([20000, 3, log_err])
597-
np.array([1.0]) / np.array([0.0])
592+
np.seterrobj([20000, 3, log_err])
593+
np.array([1.0]) / np.array([0.0])
598594
assert_equal(self.called, 1)
599595

600596
np.seterrobj(olderrobj)
601-
with np.errstate(divide="ignore"):
602-
np.divide(1.0, 0.0, extobj=[20000, 3, log_err])
597+
np.divide(1.0, 0.0, extobj=[20000, 3, log_err])
603598
assert_equal(self.called, 2)
604599
finally:
605600
np.seterrobj(olderrobj)
@@ -652,74 +647,68 @@ def assert_op_raises_fpe(self, fpeerr, flop, sc1, sc2):
652647
@pytest.mark.parametrize("typecode", np.typecodes["AllFloat"])
653648
def test_floating_exceptions(self, typecode):
654649
# Test basic arithmetic function errors
655-
with np.errstate(all="raise"):
656-
ftype = np.obj2sctype(typecode)
657-
if np.dtype(ftype).kind == "f":
658-
# Get some extreme values for the type
659-
fi = np.finfo(ftype)
660-
ft_tiny = fi._machar.tiny
661-
ft_max = fi.max
662-
ft_eps = fi.eps
663-
underflow = "underflow"
664-
divbyzero = "divide by zero"
665-
else:
666-
# 'c', complex, corresponding real dtype
667-
rtype = type(ftype(0).real)
668-
fi = np.finfo(rtype)
669-
ft_tiny = ftype(fi._machar.tiny)
670-
ft_max = ftype(fi.max)
671-
ft_eps = ftype(fi.eps)
672-
# The complex types raise different exceptions
673-
underflow = ""
674-
divbyzero = ""
675-
overflow = "overflow"
676-
invalid = "invalid"
677-
678-
# The value of tiny for double double is NaN, so we need to
679-
# pass the assert
680-
if not np.isnan(ft_tiny):
681-
self.assert_raises_fpe(underflow, lambda a, b: a / b, ft_tiny, ft_max)
682-
self.assert_raises_fpe(underflow, lambda a, b: a * b, ft_tiny, ft_tiny)
683-
self.assert_raises_fpe(overflow, lambda a, b: a * b, ft_max, ftype(2))
684-
self.assert_raises_fpe(overflow, lambda a, b: a / b, ft_max, ftype(0.5))
685-
self.assert_raises_fpe(
686-
overflow, lambda a, b: a + b, ft_max, ft_max * ft_eps
687-
)
688-
self.assert_raises_fpe(
689-
overflow, lambda a, b: a - b, -ft_max, ft_max * ft_eps
690-
)
691-
self.assert_raises_fpe(overflow, np.power, ftype(2), ftype(2**fi.nexp))
692-
self.assert_raises_fpe(divbyzero, lambda a, b: a / b, ftype(1), ftype(0))
693-
self.assert_raises_fpe(
694-
invalid, lambda a, b: a / b, ftype(np.inf), ftype(np.inf)
695-
)
696-
self.assert_raises_fpe(invalid, lambda a, b: a / b, ftype(0), ftype(0))
697-
self.assert_raises_fpe(
698-
invalid, lambda a, b: a - b, ftype(np.inf), ftype(np.inf)
699-
)
700-
self.assert_raises_fpe(
701-
invalid, lambda a, b: a + b, ftype(np.inf), ftype(-np.inf)
702-
)
703-
self.assert_raises_fpe(invalid, lambda a, b: a * b, ftype(0), ftype(np.inf))
650+
ftype = np.obj2sctype(typecode)
651+
if np.dtype(ftype).kind == "f":
652+
# Get some extreme values for the type
653+
fi = np.finfo(ftype)
654+
ft_tiny = fi._machar.tiny
655+
ft_max = fi.max
656+
ft_eps = fi.eps
657+
underflow = "underflow"
658+
divbyzero = "divide by zero"
659+
else:
660+
# 'c', complex, corresponding real dtype
661+
rtype = type(ftype(0).real)
662+
fi = np.finfo(rtype)
663+
ft_tiny = ftype(fi._machar.tiny)
664+
ft_max = ftype(fi.max)
665+
ft_eps = ftype(fi.eps)
666+
# The complex types raise different exceptions
667+
underflow = ""
668+
divbyzero = ""
669+
overflow = "overflow"
670+
invalid = "invalid"
671+
672+
# The value of tiny for double double is NaN, so we need to
673+
# pass the assert
674+
if not np.isnan(ft_tiny):
675+
self.assert_raises_fpe(underflow, lambda a, b: a / b, ft_tiny, ft_max)
676+
self.assert_raises_fpe(underflow, lambda a, b: a * b, ft_tiny, ft_tiny)
677+
self.assert_raises_fpe(overflow, lambda a, b: a * b, ft_max, ftype(2))
678+
self.assert_raises_fpe(overflow, lambda a, b: a / b, ft_max, ftype(0.5))
679+
self.assert_raises_fpe(overflow, lambda a, b: a + b, ft_max, ft_max * ft_eps)
680+
self.assert_raises_fpe(overflow, lambda a, b: a - b, -ft_max, ft_max * ft_eps)
681+
self.assert_raises_fpe(overflow, np.power, ftype(2), ftype(2**fi.nexp))
682+
self.assert_raises_fpe(divbyzero, lambda a, b: a / b, ftype(1), ftype(0))
683+
self.assert_raises_fpe(
684+
invalid, lambda a, b: a / b, ftype(np.inf), ftype(np.inf)
685+
)
686+
self.assert_raises_fpe(invalid, lambda a, b: a / b, ftype(0), ftype(0))
687+
self.assert_raises_fpe(
688+
invalid, lambda a, b: a - b, ftype(np.inf), ftype(np.inf)
689+
)
690+
self.assert_raises_fpe(
691+
invalid, lambda a, b: a + b, ftype(np.inf), ftype(-np.inf)
692+
)
693+
self.assert_raises_fpe(invalid, lambda a, b: a * b, ftype(0), ftype(np.inf))
704694

705695
@pytest.mark.skipif(IS_WASM, reason="no wasm fp exception support")
706696
def test_warnings(self):
707697
# test warning code path
708698
with warnings.catch_warnings(record=True) as w:
709699
warnings.simplefilter("always")
710-
with np.errstate(all="warn"):
711-
np.divide(1, 0.0)
712-
assert_equal(len(w), 1)
713-
assert_("divide by zero" in str(w[0].message))
714-
np.array(1e300) * np.array(1e300)
715-
assert_equal(len(w), 2)
716-
assert_("overflow" in str(w[-1].message))
717-
np.array(np.inf) - np.array(np.inf)
718-
assert_equal(len(w), 3)
719-
assert_("invalid value" in str(w[-1].message))
720-
np.array(1e-300) * np.array(1e-300)
721-
assert_equal(len(w), 4)
722-
assert_("underflow" in str(w[-1].message))
700+
np.divide(1, 0.0)
701+
assert_equal(len(w), 1)
702+
assert_("divide by zero" in str(w[0].message))
703+
np.array(1e300) * np.array(1e300)
704+
assert_equal(len(w), 2)
705+
assert_("overflow" in str(w[-1].message))
706+
np.array(np.inf) - np.array(np.inf)
707+
assert_equal(len(w), 3)
708+
assert_("invalid value" in str(w[-1].message))
709+
np.array(1e-300) * np.array(1e-300)
710+
assert_equal(len(w), 4)
711+
assert_("underflow" in str(w[-1].message))
723712

724713

725714
class TestTypes:
@@ -2315,8 +2304,7 @@ def test_filled_like(self):
23152304
self.check_like_function(np.full_like, 1000, True)
23162305
self.check_like_function(np.full_like, 123.456, True)
23172306
# Inf to integer casts cause invalid-value errors: ignore them.
2318-
with np.errstate(invalid="ignore"):
2319-
self.check_like_function(np.full_like, np.inf, True)
2307+
self.check_like_function(np.full_like, np.inf, True)
23202308

23212309
@pytest.mark.parametrize(
23222310
"likefunc", [np.empty_like, np.full_like, np.zeros_like, np.ones_like]

0 commit comments

Comments
 (0)
0