MCPcopy
hub / github.com/tinygrad/tinygrad / PatternMatcher

Class PatternMatcher

tinygrad/uop/ops.py:1299–1323  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1297 return lazy_compile
1298
1299class PatternMatcher:
1300 def __init__(self, patterns:Sequence[tuple[UPat, Callable|tuple]], compiled=bool(getenv("UPAT_COMPILE", 1))):
1301 # if this comes from a pickle, we reconstruct the lambda functions here
1302 self.patterns:list[tuple[UPat, Callable]] = [(p,types.FunctionType(*fxn) if isinstance(fxn, tuple) else fxn) for p,fxn in patterns]
1303 # NOTE: use of DefaultDict here is very dangerous! all keys will live for the lifetime of the PatternMatcher!
1304 self.pdict: dict[Ops, list[list]] = {}
1305 # uop is required, arg is optional
1306 for p,fxn in self.patterns:
1307 assert p.op is not None
1308 entry: list = [p, None, p.early_reject]
1309 entry[1] = upat_deferred_compile(p, fxn, entry) if compiled else upat_interpret(p, fxn)
1310 for uop in p.op: self.pdict.setdefault(uop, []).append(entry)
1311
1312 def __reduce__(self): return PatternMatcher, ([(x,deconstruct_function(fxn) if fxn.__name__ == "<lambda>" else fxn) for x,fxn in self.patterns],)
1313
1314 @functools.cache # pylint: disable=method-cache-max-size-none
1315 def __add__(self, more:PatternMatcher) -> PatternMatcher: return PatternMatcher(self.patterns+more.patterns)
1316
1317 def rewrite(self, uop:UOp, ctx=None):
1318 if len(pats:=self.pdict.get(uop.op, [])):
1319 if (ler:=uop.__dict__.get('_src_ops')) is None: uop.__dict__['_src_ops'] = ler = {u.op for u in uop.src}
1320 for _,match,early_reject in pats:
1321 if not early_reject.issubset(ler): continue
1322 if (ret:=match(uop, ctx)) is not None and ret is not uop: return ret
1323 return None
1324
1325# *** tracking pattern matcher ***
1326

Callers 15

callify.pyFile · 0.90
transform_to_callFunction · 0.90
function.pyFile · 0.90
gradient.pyFile · 0.90
cstyle.pyFile · 0.90
ClangRendererClass · 0.90
OpenCLRendererClass · 0.90
IntelRendererClass · 0.90
MetalRendererClass · 0.90
CUDARendererClass · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by 15

test_stepsMethod · 0.72
innerMethod · 0.72
test_exceptionsMethod · 0.72
name_defaultMethod · 0.72
name_from_fxnMethod · 0.72
testMethod · 0.72
outerMethod · 0.72
nested_functionMethod · 0.72
main_rewriteMethod · 0.72
invalid_fxnMethod · 0.72
test_inf_loopMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…