From 2f70fb893e012f8b2218063c4bd69cdeac75f1be Mon Sep 17 00:00:00 2001 From: chenyu Date: Sun, 3 Nov 2024 12:36:50 -0500 Subject: [PATCH] move transcendental fuzzer test to test_transcendental (#7511) --- test/test_dtype_alu.py | 40 ------------------------------ test/test_transcendental.py | 49 ++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/test/test_dtype_alu.py b/test/test_dtype_alu.py index d00e37c639..d4e407d5c6 100644 --- a/test/test_dtype_alu.py +++ b/test/test_dtype_alu.py @@ -173,45 +173,5 @@ class TestDTypeALU(unittest.TestCase): @given(ht.int32, strat.sampled_from(dtypes_float+dtypes_int+dtypes_bool)) def test_int32_cast(self, a, dtype): universal_test_cast(a, dtypes.int32, dtype) -class TestFromFuzzer(unittest.TestCase): - @given(strat.sampled_from(dtypes_float)) - def test_sin(self, dtype): - if not is_dtype_supported(dtype): return - if dtype == dtypes.float64: - # crashes in CI CUDA - if getenv("MOCKGPU") and Device.DEFAULT == "NV": return - def _test_value(n: float, unit: float=1.0): - next_float = np.nextafter(1.0, 2.0, dtype=_to_np_dtype(dtype)) - ulp = next_float - 1.0 - ulp = unit * ulp - np.testing.assert_allclose(Tensor([n], dtype=dtype).sin().numpy(), np.sin(np.array([n], dtype=_to_np_dtype(dtype))), atol=ulp, rtol=1e-5) - _test_value(-35.0) - _test_value(-25.0) - _test_value(25.0) - _test_value(30.0) # 30.0 == switch_over - _test_value(35.0) - _test_value(0.0) - _test_value(np.pi / 2) - # worst case of ulp 1.5 - _test_value(np.pi * 2, unit=1.5) - - @given(strat.sampled_from(dtypes_float)) - def test_log2(self, dtype): - if not is_dtype_supported(dtype): return - if dtype == dtypes.float64: - # crashes in CI CUDA - if getenv("MOCKGPU") and Device.DEFAULT == "NV": return - def _test_value(n: float, unit: float=1.0): - next_float = np.nextafter(1.0, 2.0, dtype=_to_np_dtype(dtype)) - ulp = next_float - 1.0 - ulp = unit * ulp - np.testing.assert_allclose(Tensor([n], dtype=dtype).log2().numpy(), np.log2(np.array([n], dtype=_to_np_dtype(dtype))), atol=ulp, rtol=1e-5) - fmin = np.finfo(_to_np_dtype(dtype)).tiny - for scale in [1.0, 1e10, 1e20, 1e30]: - _test_value(fmin * scale) - _test_value(-fmin * scale) - _test_value(0) - _test_value(0.0000009) - if __name__ == '__main__': unittest.main() diff --git a/test/test_transcendental.py b/test/test_transcendental.py index 4d76029d03..8a1417db34 100644 --- a/test/test_transcendental.py +++ b/test/test_transcendental.py @@ -3,7 +3,7 @@ from tinygrad import Tensor, Device, dtypes from tinygrad.tensor import _to_np_dtype from tinygrad.helpers import Context, getenv from test.test_schedule import check_schedule -from test.test_dtype_alu import ht +from test.test_dtype_alu import ht, dtypes_float from test.helpers import is_dtype_supported import numpy as np from hypothesis import given, settings, strategies as strat @@ -40,10 +40,10 @@ class TestTranscendentalMath(unittest.TestCase): op[1](np.array([x], dtype=_to_np_dtype(dtypes.float16))), atol=1e-2, rtol=5e-3) # exp can have bigger rtol - @unittest.skipIf(Device.DEFAULT=="LLVM", "FIXME: LLVM might change computer") + @unittest.skipIf(Device.DEFAULT=="LLVM", "FIXME: LLVM might change compute") @given(strat.sampled_from([(dtypes.float64, 709.5), (dtypes.float32, 88.7), (dtypes.float16, 11)])) def test_exp_near_inf(self, dtype_x): - # reordering compute might give inf result + # reordering compute might return inf dtype, x = dtype_x if not is_dtype_supported(dtype): return with Context(TRANSCENDENTAL=2): @@ -51,8 +51,49 @@ class TestTranscendentalMath(unittest.TestCase): expected = np.exp(np.array([x], dtype=_to_np_dtype(dtype))) np.testing.assert_allclose(y, expected, rtol=5e-3) +class TestFromFuzzer(unittest.TestCase): + @given(strat.sampled_from(dtypes_float)) + def test_sin(self, dtype): + if not is_dtype_supported(dtype): return + if dtype == dtypes.float64: + # crashes in CI CUDA + if getenv("MOCKGPU") and Device.DEFAULT == "NV": return + def _test_value(n: float, unit: float=1.0): + next_float = np.nextafter(1.0, 2.0, dtype=_to_np_dtype(dtype)) + ulp = next_float - 1.0 + ulp = unit * ulp + with Context(TRANSCENDENTAL=2): + np.testing.assert_allclose(Tensor([n], dtype=dtype).sin().numpy(), np.sin(np.array([n], dtype=_to_np_dtype(dtype))), atol=ulp, rtol=1e-5) + _test_value(-35.0) + _test_value(-25.0) + _test_value(25.0) + _test_value(30.0) # 30.0 == switch_over + _test_value(35.0) + _test_value(0.0) + _test_value(np.pi / 2) + # worst case of ulp 1.5 + _test_value(np.pi * 2, unit=1.5) + + @given(strat.sampled_from(dtypes_float)) + def test_log2(self, dtype): + if not is_dtype_supported(dtype): return + if dtype == dtypes.float64: + # crashes in CI CUDA + if getenv("MOCKGPU") and Device.DEFAULT == "NV": return + def _test_value(n: float, unit: float=1.0): + next_float = np.nextafter(1.0, 2.0, dtype=_to_np_dtype(dtype)) + ulp = next_float - 1.0 + ulp = unit * ulp + with Context(TRANSCENDENTAL=2): + np.testing.assert_allclose(Tensor([n], dtype=dtype).log2().numpy(), np.log2(np.array([n], dtype=_to_np_dtype(dtype))), atol=ulp, rtol=1e-5) + fmin = np.finfo(_to_np_dtype(dtype)).tiny + for scale in [1.0, 1e10, 1e20, 1e30]: + _test_value(fmin * scale) + _test_value(-fmin * scale) + _test_value(0) + _test_value(0.0000009) + class TestTranscendentalSchedule(unittest.TestCase): - # w/ payne_hanek_reduction (fp32) def test_transcendental_sin_fusion(self): with Context(TRANSCENDENTAL=2): a = Tensor.empty(10)