From 07f45be2a7de721bc84e0bb86478fdf2929418c7 Mon Sep 17 00:00:00 2001 From: George Hotz Date: Tue, 5 Aug 2025 20:14:07 -0700 Subject: [PATCH] simpler replace buffers --- tinygrad/schedule/kernelize.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tinygrad/schedule/kernelize.py b/tinygrad/schedule/kernelize.py index 7c2468ae31..f54dffcc38 100644 --- a/tinygrad/schedule/kernelize.py +++ b/tinygrad/schedule/kernelize.py @@ -149,20 +149,17 @@ create_kernels = PatternMatcher([ # **** fix kernel AST -early_buffer_ops = PatternMatcher([ +replace_buffers = PatternMatcher([ + # replace ASSIGN with the target BUFFER + (UPat(Ops.ASSIGN, src=(UPat((Ops.BUFFER, Ops.LOAD)), UPat(Ops.KERNEL)), name="assign", allow_any_len=True), lambda assign: assign.src[0]), + # HACK: select the 0 branch of MSTACK (the device is wrong after this, is that okay?) + (UPat(Ops.MSTACK, name="x"), lambda x: x.src[0]), # LOAD (UPat(Ops.BUFFER, name="x"), lambda ctx,x: UOp(Ops.DEFINE_GLOBAL, x.dtype.ptr(x.size), (), ctx.index(x)).load()), # no SINK for meta ops (UPat(Ops.SINK, src=(UPat(Ops.CONTIGUOUS, src=(UPat(GroupOp.Meta, name="x"),),))), lambda x:x), ]) -replace_globals = PatternMatcher([ - # replace ASSIGN with the target BUFFER - (UPat(Ops.ASSIGN, src=(UPat(Ops.BUFFER), UPat(Ops.KERNEL)), name="assign", allow_any_len=True), lambda assign: assign.src[0]), - # HACK: select the 0 branch of MSTACK (the device is wrong after this, is that okay?) - (UPat(Ops.MSTACK, name="x"), lambda x: x.src[0]), -]) - def fix_kernel_ast(k:UOp) -> UOp|None: if k.arg.ast.op in GroupOp.Meta or all(s.op is Ops.STORE for s in k.arg.ast.src): return None # replace buffer with define_global + add load/store last @@ -173,8 +170,7 @@ def fix_kernel_ast(k:UOp) -> UOp|None: while s.op in {Ops.MSELECT, Ops.MSTACK}: s = s.src[0] bufs.append(s) # replace global memory ops with the BUFFER they write to - ast = graph_rewrite(k.arg.ast, replace_globals, bottom_up=True, name="replace globals") - ast = graph_rewrite(ast, early_buffer_ops, bufs, bottom_up=True, name="replace buffer early") + ast = graph_rewrite(k.arg.ast, replace_buffers, bufs, bottom_up=True, name="replace buffers") if ast.op is Ops.SINK and not all_same([x.device for x in k.src]): raise RuntimeError(f"all buffers must be on the same device: {tuple(b.buf_uop.buffer for b in k.src)}") # TODO: move these to codegen