lowerer is kernel [run_process_replay] (#5437)

This commit is contained in:
George Hotz
2024-07-12 18:50:55 -07:00
committed by GitHub
parent b8342fb085
commit 03c2dc8bd7
33 changed files with 215 additions and 213 deletions
+50 -3
View File
@@ -4,14 +4,19 @@ from dataclasses import replace
from collections import defaultdict
from typing import Optional, List, Tuple, cast, Dict, Union, Final, DefaultDict
from tinygrad.engine.graph import print_tree
from tinygrad.ops import LazyOp, UnaryOps, BinaryOps, ReduceOps, MemBuffer, ConstBuffer, BufferOps, MetaOps, UNSAFE_PAD_OPS, verify_lazyop, KernelInfo
from tinygrad.ops import LazyOp, UnaryOps, BinaryOps, ReduceOps, MemBuffer, ConstBuffer, BufferOps, MetaOps, UNSAFE_PAD_OPS, \
verify_lazyop, KernelInfo, get_lazyop_info
from tinygrad.device import Device
from tinygrad.renderer import Renderer, TensorCore
from tinygrad.renderer import Renderer, TensorCore, Program
from tinygrad.dtype import dtypes, ImageDType
from tinygrad.helpers import all_same, colored, ansilen, dedup, getenv, prod, DEBUG, TC_OPT, USE_TC, round_up, all_int, get_contraction, to_function_name # noqa: E501
from tinygrad.helpers import all_same, colored, ansilen, dedup, getenv, prod, DEBUG, TC_OPT, USE_TC, round_up, all_int, \
get_contraction, to_function_name, diskcache_put, ContextVar
from tinygrad.shape.shapetracker import ShapeTracker
from tinygrad.shape.symbolic import sint
from tinygrad.shape.view import strides_for_shape
from tinygrad.codegen.uops import UOps, flops_mem
from tinygrad.codegen.uopgraph import UOpGraph
from tinygrad.codegen.lowerer import lazyop_to_uop
from dataclasses import dataclass
from enum import Enum, auto
@@ -719,3 +724,45 @@ class Kernel:
arg = op.arg
return LazyOp(op.op, tuple(fixup_ast(x) for x in op.src), arg)
return fixup_ast(self.ast)
# **** this is the lowerer ****
def linearize(self) -> Kernel:
modified_ast = self.get_optimized_ast()
if DEBUG >= 3:
print(self.name)
print_tree(modified_ast)
uop_sink = lazyop_to_uop(modified_ast, self.opts)
# extract global/local sizes
if self.opts.has_local:
self.global_size: Optional[List[int]] = [1,1,1]
self.local_size: Optional[List[int]] = [1,1,1]
for u in uop_sink.parents:
if u.op is UOps.SPECIAL:
if u.arg[1][0] == 'l': self.local_size[u.arg[0]] = u.arg[2]
else: self.global_size[u.arg[0]] = u.arg[2]
else:
self.global_size, self.local_size = None, None
# generate the UOpGraph
self.uops:UOpGraph = UOpGraph(uop_sink, self.opts)
if DEBUG >= 5: self.uops.print()
if getenv("GRAPHUOPS"):
self.uops.graph()
if getenv("GRAPHUOPS") == 2: exit(0)
return self
def to_program(self) -> Program:
self.linearize()
src = self.opts.render(name:=to_function_name(self.name), self.uops)
if getenv("RUN_PROCESS_REPLAY"):
table_name = f"process_replay_{getenv('GITHUB_SHA', 'HEAD')}"
diskcache_put(table_name, id(self), (self.ast, self.opts, self.applied_opts, name, src, {k:v.value for k,v in ContextVar._cache.items()}))
info = get_lazyop_info(self.ast.src[0]) # TODO: this should be removed
ops, mem = flops_mem(self.uops.uops)
run_count = prod((self.global_size or []) + (self.local_size or []))
return Program(self.name, src, self.opts.device, self.global_size, self.local_size,
self.uops, min(info.flops, ops * run_count), min(info.mem_estimate, mem * run_count))