|
|
|
@@ -1,18 +1,197 @@
|
|
|
|
|
from dataclasses import replace
|
|
|
|
|
from tinygrad.uop.ops import PatternMatcher, UPat, Ops, UOp, KernelInfo
|
|
|
|
|
from tinygrad.helpers import colored
|
|
|
|
|
from tinygrad.codegen.opt.kernel import axis_colors
|
|
|
|
|
import math, itertools
|
|
|
|
|
from tinygrad.dtype import AddrSpace
|
|
|
|
|
from tinygrad.uop.ops import UOp, Ops, sint, ssimplify, AxisType, KernelInfo, PatternMatcher, UPat, graph_rewrite
|
|
|
|
|
from tinygrad.helpers import DEBUG, BEAM, getenv
|
|
|
|
|
from tinygrad.codegen.opt.kernel import Kernel, Opt, OptOps, KernelOptError
|
|
|
|
|
from tinygrad.renderer import Renderer
|
|
|
|
|
from tinygrad.dtype import dtypes
|
|
|
|
|
from tinygrad.device import Buffer
|
|
|
|
|
|
|
|
|
|
def rename_sink(s:UOp):
|
|
|
|
|
if s.arg is not None and s.arg.name != "test": return None
|
|
|
|
|
def flatten_range(r:UOp):
|
|
|
|
|
off = 2 if r.op is Ops.STORE else 1
|
|
|
|
|
rngs = r.src[off:]
|
|
|
|
|
if not len(rngs): return None
|
|
|
|
|
new_rngs = [x for x in UOp.sink(*rngs).toposort() if x.op is Ops.RANGE]
|
|
|
|
|
return r.replace(src=r.src[:off]+tuple(new_rngs))
|
|
|
|
|
|
|
|
|
|
# get all ranges (sorted)
|
|
|
|
|
rngs = sorted([u for u in s.parents if u.op is Ops.RANGE], key=lambda x: x.arg[0:-1])
|
|
|
|
|
pm_flatten_range = PatternMatcher([
|
|
|
|
|
# real ranges only
|
|
|
|
|
(UPat((Ops.REDUCE, Ops.STORE), name="r"), flatten_range),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
# add name to kernel
|
|
|
|
|
name = "k" + colored('_', 'BLACK').join(['']+[colored(x.src[0].render(), axis_colors[x.arg[-1]]) for x in rngs])
|
|
|
|
|
return s.replace(arg=KernelInfo(name=name) if s.arg is None else replace(s.arg, name=name))
|
|
|
|
|
class RKernel(Kernel):
|
|
|
|
|
def __init__(self, ast:UOp, opts:Renderer|None=None):
|
|
|
|
|
self.rng = sorted([u for u in ast.toposort() if u.op is Ops.RANGE and u.vmax > 0], key=lambda x: x.arg)
|
|
|
|
|
super().__init__(ast, opts)
|
|
|
|
|
self.sts.clear()
|
|
|
|
|
|
|
|
|
|
pm_postrange_opt = PatternMatcher([
|
|
|
|
|
(UPat(Ops.SINK, name="s"), rename_sink),
|
|
|
|
|
# convert LOOP to GLOBAL
|
|
|
|
|
self.replaces = {}
|
|
|
|
|
if self.opts.has_local:
|
|
|
|
|
store_rngs = self.ast.src[0].src[2:]
|
|
|
|
|
|
|
|
|
|
# filter any not in local stores
|
|
|
|
|
local_store_rngs = [x.ranges for x in self.ast.toposort() if (x.op is Ops.STORE and x.src[0].dtype.addrspace == AddrSpace.LOCAL) \
|
|
|
|
|
or (x.op is Ops.BUFFERIZE and x.arg == AddrSpace.LOCAL)]
|
|
|
|
|
for ls in local_store_rngs: store_rngs = tuple([x for x in store_rngs if x in ls])
|
|
|
|
|
|
|
|
|
|
store_rng = [x for x in UOp.sink(*store_rngs).toposort() if x.op is Ops.RANGE] if store_rngs else []
|
|
|
|
|
rng = [x.replace(arg=(x.arg[0], AxisType.GLOBAL)) if x.arg[1] == AxisType.LOOP and x in store_rng else x for x in self.rng]
|
|
|
|
|
self.replaces.update(dict(zip(self.rng, rng)))
|
|
|
|
|
self.rng = rng
|
|
|
|
|
|
|
|
|
|
# NOTE: needed for tensor cores
|
|
|
|
|
self.substitute()
|
|
|
|
|
|
|
|
|
|
self.maxarg = max([x.arg[0] for x in self.rng]) if len(self.rng) else 0
|
|
|
|
|
|
|
|
|
|
def substitute(self) -> UOp:
|
|
|
|
|
self.ast = graph_rewrite(self.ast.substitute(self.replaces), pm_flatten_range)
|
|
|
|
|
self.replaces = {}
|
|
|
|
|
return self.ast
|
|
|
|
|
|
|
|
|
|
def copy(self):
|
|
|
|
|
self.substitute()
|
|
|
|
|
return RKernel(self.ast, self.opts)
|
|
|
|
|
|
|
|
|
|
# must be done earlier
|
|
|
|
|
def simplify_merge_adjacent(self): return
|
|
|
|
|
|
|
|
|
|
def apply_opt(self, opt:Opt, append_opt:bool=True) -> UOp|None:
|
|
|
|
|
if opt.op == OptOps.PADTO: raise KernelOptError("PAD is not supported yet. needs INVALID")
|
|
|
|
|
if opt.op == OptOps.SWAP: raise KernelOptError("SWAP is not supported yet")
|
|
|
|
|
return super().apply_opt(opt, append_opt)
|
|
|
|
|
|
|
|
|
|
def shift_to(self, axis:int, amount:int, new_type:AxisType, top:bool=False, insert_at:int|None=None):
|
|
|
|
|
old_sz = self.rng[axis].src[0].arg // amount
|
|
|
|
|
assert old_sz > 0, f"bad old_sz on {axis} {amount} {self.rng[axis]}"
|
|
|
|
|
|
|
|
|
|
self.maxarg += 1
|
|
|
|
|
new_rng = UOp.range(amount, self.maxarg, new_type)
|
|
|
|
|
|
|
|
|
|
if old_sz == 1:
|
|
|
|
|
self.replaces[self.rng[axis]] = new_rng
|
|
|
|
|
self.rng.insert(insert_at if insert_at is not None else len(self.rng), new_rng)
|
|
|
|
|
del self.rng[axis]
|
|
|
|
|
else:
|
|
|
|
|
replaced_rng = self.rng[axis].replace(src=(UOp.const(dtypes.int, old_sz),))
|
|
|
|
|
self.replaces[self.rng[axis]] = (new_rng * old_sz + replaced_rng) if top else (replaced_rng * amount + new_rng)
|
|
|
|
|
self.rng[axis] = replaced_rng
|
|
|
|
|
self.rng.insert(insert_at if insert_at is not None else len(self.rng), new_rng)
|
|
|
|
|
return new_rng
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def axis_types(self) -> list[AxisType]: return [x.arg[-1] for x in self.rng]
|
|
|
|
|
@property
|
|
|
|
|
def shape_len(self): return len(self.rng)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def full_shape(self) -> tuple[sint, ...]: return tuple([ssimplify(x.src[0]) for x in self.rng])
|
|
|
|
|
@property
|
|
|
|
|
def output_shape(self) -> tuple[sint, ...]:
|
|
|
|
|
if self.ast.src[0].op is not Ops.STORE: return ()
|
|
|
|
|
return tuple([ssimplify(x.src[0]) for x in self.ast.src[0].src[2:]])
|
|
|
|
|
|
|
|
|
|
def get_optimized_ast(self, name_override:str|None=None) -> UOp:
|
|
|
|
|
ret = self.substitute()
|
|
|
|
|
kernel_name = ret.arg.name if ret.arg is not None and ret.arg.name != "test" else self.name if name_override is None else name_override
|
|
|
|
|
rarg = KernelInfo(kernel_name, tuple(self.axis_types), self.dont_use_locals, tuple(self.applied_opts))
|
|
|
|
|
return ret.replace(arg=rarg)
|
|
|
|
|
|
|
|
|
|
# does nothing
|
|
|
|
|
@axis_types.setter
|
|
|
|
|
def axis_types(self, value): pass
|
|
|
|
|
|
|
|
|
|
def _apply_tc_opt(self, use_tensor_cores:int, axis:int, tc_select:int, opt_level:int) -> bool:
|
|
|
|
|
reduceops = [x for x in self.ast.toposort() if x.op is Ops.REDUCE]
|
|
|
|
|
if not len(reduceops): raise KernelOptError("no reduce ops for TensorCore")
|
|
|
|
|
reduceop = reduceops[0]
|
|
|
|
|
if use_tensor_cores and reduceop is not None and reduceop.arg is Ops.ADD:
|
|
|
|
|
mul = reduceop.src[0] if reduceop.src[0].op is not Ops.CAST else reduceop.src[0].src[0]
|
|
|
|
|
if mul.op is not Ops.MUL: return False
|
|
|
|
|
in0, in1 = mul.src
|
|
|
|
|
tensor_cores = self.opts.tensor_cores if tc_select == -1 else [self.opts.tensor_cores[tc_select]]
|
|
|
|
|
for tc in tensor_cores:
|
|
|
|
|
if tc.dtype_in == in0.dtype.scalar() and tc.dtype_in == in1.dtype.scalar() and tc.dtype_out == reduceop.dtype.scalar():
|
|
|
|
|
# early realize for TC
|
|
|
|
|
self.substitute()
|
|
|
|
|
|
|
|
|
|
# tensor cores have three ranges. X, Y, and REDUCE
|
|
|
|
|
in0_ranges = sorted([u for u in in0.ranges if u not in in1.ranges], key=lambda x: x.arg[0])
|
|
|
|
|
in1_ranges = sorted([u for u in in1.ranges if u not in in0.ranges], key=lambda x: x.arg[0])
|
|
|
|
|
red_ranges = sorted(reduceop.src[1:], key=lambda x: x.arg[0])
|
|
|
|
|
if DEBUG >= 3:
|
|
|
|
|
print(f"TC({axis}): {[(x.arg[0],x.vmax+1) for x in in0_ranges]}",
|
|
|
|
|
f"{[(x.arg[0],x.vmax+1) for x in in1_ranges]} {[(x.arg[0],x.vmax+1) for x in red_ranges]}")
|
|
|
|
|
if not len(in0_ranges) or not len(in1_ranges) or not len(red_ranges): return None
|
|
|
|
|
|
|
|
|
|
# pick ranges
|
|
|
|
|
# NOTE: why are in1 and in0 switched?
|
|
|
|
|
axis_choices = list(itertools.product(in1_ranges, in0_ranges, red_ranges))
|
|
|
|
|
if not (axis < len(axis_choices)): return None
|
|
|
|
|
axes = axis_choices[axis]
|
|
|
|
|
|
|
|
|
|
# do optimizations and save the ranges
|
|
|
|
|
try:
|
|
|
|
|
for i,a in enumerate(axes):
|
|
|
|
|
if a.src[0].divides(tc.dims[i]) is None:
|
|
|
|
|
self.apply_opt(Opt(OptOps.PADTO, self.rng.index(a), tc.dims[i]), append_opt=False) # PADTO might fail
|
|
|
|
|
except KernelOptError: continue
|
|
|
|
|
ne: list[UOp] = []
|
|
|
|
|
for opt in tc.opts:
|
|
|
|
|
ne.append(self.apply_opt(Opt({"u":OptOps.UPCAST, "l":OptOps.LOCAL}[opt[0]], axes[int(opt[1])].arg[0], 2), append_opt=False))
|
|
|
|
|
reduce_axis = [self.rng[i] for i in self.axes_of(AxisType.REDUCE)].index(axes[2])
|
|
|
|
|
for _, amt in tc.get_reduce_axes():
|
|
|
|
|
ne.append(self.apply_opt(Opt(OptOps.UNROLL, reduce_axis, amt), append_opt=False))
|
|
|
|
|
|
|
|
|
|
if use_tensor_cores != 2:
|
|
|
|
|
# fix the srcs
|
|
|
|
|
reduceop = [x for x in self.ast.toposort() if x.op is Ops.REDUCE][0]
|
|
|
|
|
tne = [x.replace(tag=1) for x in ne]
|
|
|
|
|
ret = reduceop.substitute(dict(zip(ne, tne)))
|
|
|
|
|
srcs = list((ret.src[0] if ret.src[0].op is not Ops.CAST else ret.src[0].src[0]).src)
|
|
|
|
|
srcs = [x.substitute(dict(zip(tne, [ne[i] for i in p]))) for x,p in zip(srcs, tc.permutes_for_shape_str(tc.base_shape_str()))]
|
|
|
|
|
|
|
|
|
|
# get reduce/upcast axes for the tensor cores
|
|
|
|
|
tc_reduce_axes = self.shape_str_to_axis([f"r{i}" for i in range(len(tc.get_reduce_axes()))])
|
|
|
|
|
base_upcast_axes = tuple([(s,2) for s in self.shape_str_to_axis(tc.base_upcast_axes())])
|
|
|
|
|
tc_upcast_axes = tuple([base_upcast_axes[:int(math.log2(tc.elements_per_thread[i]))] for i in range(3)])
|
|
|
|
|
|
|
|
|
|
# axes to range number (was done in lowerer)
|
|
|
|
|
tc_upcast_axes = tuple([tuple([(self.rng[a].arg[0], sz) for a,sz in v]) for v in tc_upcast_axes])
|
|
|
|
|
tc_reduce_axes = tuple([self.rng[a].arg[0] for a in tc_reduce_axes])
|
|
|
|
|
|
|
|
|
|
# construct the op
|
|
|
|
|
# TODO: remove tc_upcast_axes from the arg
|
|
|
|
|
wmma_arg = (str(tc), tc.dims, tc.dtype_in, tc.dtype_out, self.opts.device, tc.threads, tc_upcast_axes, tc_reduce_axes)
|
|
|
|
|
wmma = UOp(Ops.WMMA, dtype=tc.dtype_out.vec(tc.elements_per_thread[2]), src=(
|
|
|
|
|
UOp(Ops.CONTRACT, dtype=srcs[0].dtype.vec(tc.elements_per_thread[0]), src=(srcs[0],), arg=tc_upcast_axes[0], tag=1),
|
|
|
|
|
UOp(Ops.CONTRACT, dtype=srcs[1].dtype.vec(tc.elements_per_thread[1]), src=(srcs[1],), arg=tc_upcast_axes[1], tag=1),
|
|
|
|
|
UOp.const(tc.dtype_out.vec(tc.elements_per_thread[2]), 0.0)), arg=wmma_arg, tag=1)
|
|
|
|
|
tc_uop = UOp(Ops.UNROLL, tc.dtype_out, (wmma,), arg=tc_upcast_axes[2], tag=1)
|
|
|
|
|
|
|
|
|
|
# preserve extra reduces
|
|
|
|
|
reduce_ranges = [x for x in UOp.sink(*reduceop.src[1:]).toposort() if x.op is Ops.RANGE and x.arg[0] not in tc_reduce_axes]
|
|
|
|
|
if len(reduce_ranges): tc_uop = UOp(Ops.REDUCE, tc_uop.dtype, (tc_uop,)+tuple(reduce_ranges), Ops.ADD)
|
|
|
|
|
self.ast = self.ast.substitute({reduceop: tc_uop})
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def bufs_from_ast(ast:UOp, dname:str) -> list[Buffer]:
|
|
|
|
|
glbls = sorted([x for x in ast.parents if x.op is Ops.DEFINE_GLOBAL], key=lambda x: x.arg)
|
|
|
|
|
return [Buffer(dname, x.dtype.size, x.dtype.base) for x in glbls]
|
|
|
|
|
|
|
|
|
|
def apply_ropt(ast:UOp, renderer:Renderer):
|
|
|
|
|
k = RKernel(ast, opts=renderer)
|
|
|
|
|
if BEAM >= 1:
|
|
|
|
|
from tinygrad.codegen.opt.search import beam_search
|
|
|
|
|
kb = RKernel(ast, opts=renderer)
|
|
|
|
|
rawbufs = bufs_from_ast(ast, renderer.device)
|
|
|
|
|
k = beam_search(kb, rawbufs, BEAM.value, bool(getenv("BEAM_ESTIMATE", 1)))
|
|
|
|
|
elif ast.arg is not None: k.apply_opts(ast.arg.opts_to_apply)
|
|
|
|
|
return k.get_optimized_ast()
|
|
|
|
|
|
|
|
|
|
pm_postrange_opt = pm_flatten_range+PatternMatcher([
|
|
|
|
|
(UPat(Ops.SINK, name="ast"), lambda ctx,ast: apply_ropt(ast, ctx) if ast.arg is None or \
|
|
|
|
|
(ast.arg is not None and ast.arg.opts_to_apply is not None) else None),
|
|
|
|
|
])
|
|
|
|
|