MCPcopy
hub / github.com/tinygrad/tinygrad / transform_precompiled_call

Function transform_precompiled_call

tinygrad/callify.py:96–130  ·  view source on GitHub ↗
(c:UOp)

Source from the content-addressed store, hash-verified

94 return None
95
96def transform_precompiled_call(c:UOp) -> UOp|None:
97 if not c.arg.precompile: return None
98 assert c.src[0].op is Ops.TUPLE, f"expected TUPLE body for precompiled FUNCTION, got {c.src[0].op}"
99 input_buffers = tuple(x.contiguous() if x.op not in {Ops.AFTER, Ops.BIND} else x for x in c.src[1:])
100
101 # add the outputs to the call
102 srcs = c.src[0].src
103 resolved = [c.gettuple(i) for i in range(len(srcs))]
104 outs = tuple(r.empty_like() for r in resolved)
105 targets = [o.param_like(len(c.src)-1+i).shrink_to(s.shape) for i,(o,s) in enumerate(zip(outs, srcs))]
106
107 subs:dict[UOp, UOp] = {}
108 items:list[UOp] = []
109 for s, t in zip(srcs, targets):
110 after_deps:list[UOp] = []
111 while s.op is Ops.AFTER:
112 after_deps.extend(s.src[1:])
113 s = s.src[0]
114 base = s.base
115 if base.op in {Ops.CONTIGUOUS, Ops.BUFFER} and base.shape == t.shape and base not in subs:
116 subs[base] = t.after(t.store(base.src[0])) if base.op is Ops.CONTIGUOUS else t
117 items.append(s.after(*after_deps) if after_deps else s)
118 else:
119 items.append(t.after(t.store(s), *after_deps))
120 fxn = UOp.sink(*(x.substitute(subs) for x in items))
121
122 # body switches from TUPLE to SINK, so the node becomes an opaque CALL (not FUNCTION)
123 new_call = UOp(Ops.CALL, c.dtype, (fxn, *input_buffers, *outs), c.arg)
124 rets = tuple(o.after(new_call) for o in outs)
125
126 # if the CALL has symbolic shapes, shrink the max-sized output to the actual symbolic shape
127 # NOTE: must use resolved shapes from the FUNCTION (which substitutes PARAMs with external args), not raw body shapes
128 rets = tuple(r.shrink_to(rs.shape) for r,rs in zip(rets, resolved))
129
130 return UOp.maketuple(*rets)
131
132# NOTE: adding rules to here is bad. these all need to run before the schedule cache
133pm_early_transform_tensor_graph = PatternMatcher([

Callers

nothing calls this directly

Calls 12

UOpClass · 0.90
gettupleMethod · 0.80
shrink_toMethod · 0.80
param_likeMethod · 0.80
appendMethod · 0.80
substituteMethod · 0.80
maketupleMethod · 0.80
contiguousMethod · 0.45
empty_likeMethod · 0.45
afterMethod · 0.45
storeMethod · 0.45
sinkMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…