From 277433259eb71b5fc3d6d5cc33c5a1be1458e9fa Mon Sep 17 00:00:00 2001 From: chenyu Date: Fri, 31 Jul 2026 14:38:38 -0400 Subject: [PATCH] fix sym_infer for CAST (#17338) --- test/null/test_uop_symbolic.py | 4 ++++ tinygrad/uop/render.py | 2 ++ 2 files changed, 6 insertions(+) diff --git a/test/null/test_uop_symbolic.py b/test/null/test_uop_symbolic.py index 8678d762a0..183175ef09 100644 --- a/test/null/test_uop_symbolic.py +++ b/test/null/test_uop_symbolic.py @@ -1202,6 +1202,10 @@ class TestSymInfer(unittest.TestCase): # floor: 1 % -1000 = -999, 1 // -1000 = -1 assert sym_infer(a%b, var_vals) == -999 assert sym_infer(a//b, var_vals) == -1 + def test_sym_infer_with_cast(self): + a = Variable("a", 0, 100, dtypes.int) + assert sym_infer(a.cast(dtypes.long) + 1, {a.expr: 5}) == 6 + assert sym_infer(a.cast(dtypes.float) * 0.5, {a.expr: 5}) == 2.5 def test_sym_infer_with_bitcast(self): a = Variable("a", 1, 10, dtypes.int) expr = ((a.bitcast(dtypes.uint) << UOp.const(1)).bitcast(dtypes.int) + 2) diff --git a/tinygrad/uop/render.py b/tinygrad/uop/render.py index b88c97f13b..97ebe79db4 100644 --- a/tinygrad/uop/render.py +++ b/tinygrad/uop/render.py @@ -58,6 +58,8 @@ renderer_infer = PatternMatcher([ (UPat(Ops.CDIV, name="x"), lambda ctx,x: f"cdiv({ctx[x.src[0]]}, {ctx[x.src[1]]})"), (UPat(Ops.FLOORMOD, name="x"), lambda ctx,x: f"floormod({ctx[x.src[0]]}, {ctx[x.src[1]]})"), (UPat(Ops.FLOORDIV, name="x"), lambda ctx,x: f"floordiv({ctx[x.src[0]]}, {ctx[x.src[1]]})"), + (UPat(Ops.CAST, name="x"), + lambda ctx,x: f"{'float' if dtypes.is_float(x.dtype) else 'bool' if x.dtype is dtypes.bool else 'int'}({ctx[x.src[0]]})"), (UPat(Ops.BITCAST, name="x"), lambda ctx,x: f"bitcast({ctx[x.src[0]]}, {x.src[0].dtype!r}, {x.dtype!r})"), ]) + renderer