diff --git a/test/external/fuzz_emulated_long.py b/test/external/fuzz_emulated_long.py new file mode 100644 index 0000000000..e15c479e85 --- /dev/null +++ b/test/external/fuzz_emulated_long.py @@ -0,0 +1,25 @@ +import random +import z3 +from tinygrad import dtypes, Device +from tinygrad.uop.validate import uops_to_z3, z3_cdiv +from tinygrad.uop.ops import UOp +from tinygrad.uop import Ops +from tinygrad.uop.decompositions import l2i +random.seed(42) + +def split(v): return (v & 0xFFFFFFFF).cast(dtypes.uint), (v >> 32).cast(dtypes.uint) +def combine(lo, hi): return lo.cast(dtypes.ulong) | (hi.cast(dtypes.ulong) << 32) + +if __name__ == "__main__": + for i in range(10_000): + if i % 1000 == 0: + print(f"Progress: {i}") + a = UOp.variable('a0', random.randint(dtypes.ulong.min, 0), random.randint(1, dtypes.ulong.max), dtype=dtypes.ulong) + b = UOp.variable('b0', random.randint(dtypes.ulong.min, 0), random.randint(1, dtypes.ulong.max), dtype=dtypes.ulong) + expr = combine(*l2i(Ops.IDIV, dtypes.uint, *split(a), *split(b))) + + solver = z3.Solver() + z3_expr, x = uops_to_z3(solver, expr, a, b) + + if solver.check(z3_expr != z3_cdiv(a, b)) == z3.sat: + assert False, f"Failed: {expr.render()} != x//{d} at x={solver.model()}\nx={u}\nd={d}\n{z3_expr=}\n{x/d=}" diff --git a/test/test_dtype.py b/test/test_dtype.py index 8579850e8c..e1bbe695cc 100644 --- a/test/test_dtype.py +++ b/test/test_dtype.py @@ -372,6 +372,17 @@ class TestBoolDType(TestDType): DTYPE = dtypes.bool class TestBFloat16Type(TestDType): DTYPE = dtypes.bfloat16 +@unittest.skipUnless(Ops.SHL in Device[Device.DEFAULT].renderer.code_for_op, "half decomp requires bitshift") +class TestEmulatedFloat16(TestFloat16Type): + @classmethod + def setUpClass(cls): + cls.stack = contextlib.ExitStack() + cls.stack.enter_context(Context(EMULATED_DTYPES="half")) + cls.DATA = rand_for_dtype(cls.DTYPE, 10) + + @classmethod + def tearDownClass(cls): cls.stack.close() + class TestFp8e4m3(TestDType): DTYPE = dtypes.fp8e4m3 class TestFp8e5m2(TestDType): DTYPE = dtypes.fp8e5m2