MCPcopy
hub / github.com/tinygrad/tinygrad / apply_opts

Function apply_opts

tinygrad/codegen/opt/postrange.py:335–352  ·  view source on GitHub ↗
(ast:UOp, ren:Renderer, beam:int=0)

Source from the content-addressed store, hash-verified

333 return [Buffer(dname, x.ptrdtype.size, x.dtype.base) for x in glbls]
334
335def apply_opts(ast:UOp, ren:Renderer, beam:int=0) -> UOp:
336 if ast.tag is not None: return ast
337 k = Scheduler(ast, ren)
338 k.convert_loop_to_global()
339 if ast.arg is not None and ast.arg.opts_to_apply is not None:
340 for opt in ast.arg.opts_to_apply: k.apply_opt(opt)
341 elif beam >= 1:
342 from tinygrad.codegen.opt.search import beam_search
343 rawbufs = bufs_from_ast(ast, ren.target.device)
344 # beam search may open devices
345 with Context(ALLOW_DEVICE_USAGE=1):
346 k = beam_search(k, rawbufs, beam, bool(getenv("BEAM_ESTIMATE", 1)))
347 elif not NOOPT and (ast.arg is None or ast.arg.applied_opts == ()):
348 from tinygrad.codegen.opt.heuristic import hand_coded_optimizations
349 # NOTE: hand_coded_optimizations doesn't support multiblock opts yet
350 if not any(u.op is Ops.STAGE for u in ast.backward_slice):
351 k = hand_coded_optimizations(k)
352 return k.get_optimized_ast(name_override=ast.arg.name if ast.arg is not None and ast.arg.name != "test" else None)

Callers 1

full_rewrite_to_sinkFunction · 0.90

Calls 9

apply_optMethod · 0.95
get_optimized_astMethod · 0.95
ContextClass · 0.90
beam_searchFunction · 0.90
getenvFunction · 0.90
hand_coded_optimizationsFunction · 0.90
SchedulerClass · 0.85
bufs_from_astFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…