tinygrad 0.10.0__py3-none-any.whl → 0.10.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tinygrad/codegen/kernel.py +114 -172
- tinygrad/codegen/linearize.py +211 -81
- tinygrad/codegen/lowerer.py +30 -35
- tinygrad/codegen/{uopgraph.py → rewriter.py} +69 -59
- tinygrad/codegen/transcendental.py +12 -13
- tinygrad/device.py +170 -47
- tinygrad/dtype.py +28 -26
- tinygrad/engine/jit.py +80 -63
- tinygrad/engine/memory.py +4 -5
- tinygrad/engine/multi.py +162 -0
- tinygrad/engine/realize.py +58 -107
- tinygrad/engine/schedule.py +381 -314
- tinygrad/engine/search.py +40 -44
- tinygrad/gradient.py +70 -0
- tinygrad/helpers.py +77 -58
- tinygrad/nn/__init__.py +30 -32
- tinygrad/nn/datasets.py +1 -2
- tinygrad/nn/optim.py +22 -26
- tinygrad/nn/state.py +89 -64
- tinygrad/ops.py +562 -446
- tinygrad/renderer/__init__.py +79 -36
- tinygrad/renderer/cstyle.py +70 -84
- tinygrad/renderer/llvmir.py +32 -20
- tinygrad/renderer/ptx.py +79 -99
- tinygrad/renderer/wgsl.py +87 -0
- tinygrad/runtime/autogen/amd_gpu.py +39507 -12
- tinygrad/runtime/autogen/comgr.py +2 -0
- tinygrad/runtime/autogen/kfd.py +4 -3
- tinygrad/runtime/autogen/kgsl.py +1 -1
- tinygrad/runtime/autogen/libpciaccess.py +2023 -0
- tinygrad/runtime/autogen/llvm.py +11379 -0
- tinygrad/runtime/autogen/vfio.py +891 -0
- tinygrad/runtime/graph/cuda.py +8 -9
- tinygrad/runtime/graph/hcq.py +84 -79
- tinygrad/runtime/graph/metal.py +19 -21
- tinygrad/runtime/ops_amd.py +488 -327
- tinygrad/runtime/ops_clang.py +15 -28
- tinygrad/runtime/ops_cloud.py +34 -34
- tinygrad/runtime/ops_cuda.py +30 -27
- tinygrad/runtime/ops_disk.py +62 -63
- tinygrad/runtime/ops_dsp.py +129 -38
- tinygrad/runtime/ops_gpu.py +30 -30
- tinygrad/runtime/ops_hip.py +29 -31
- tinygrad/runtime/ops_llvm.py +45 -40
- tinygrad/runtime/ops_metal.py +93 -73
- tinygrad/runtime/ops_npy.py +2 -2
- tinygrad/runtime/ops_nv.py +232 -270
- tinygrad/runtime/ops_python.py +51 -46
- tinygrad/runtime/ops_qcom.py +129 -157
- tinygrad/runtime/ops_webgpu.py +63 -0
- tinygrad/runtime/support/allocator.py +94 -0
- tinygrad/runtime/support/am/__init__.py +0 -0
- tinygrad/runtime/support/am/amdev.py +384 -0
- tinygrad/runtime/support/am/ip.py +463 -0
- tinygrad/runtime/support/compiler_cuda.py +4 -2
- tinygrad/runtime/support/elf.py +26 -4
- tinygrad/runtime/support/hcq.py +254 -324
- tinygrad/runtime/support/llvm.py +32 -0
- tinygrad/shape/shapetracker.py +84 -53
- tinygrad/shape/view.py +103 -138
- tinygrad/spec.py +154 -0
- tinygrad/tensor.py +744 -496
- {tinygrad-0.10.0.dist-info → tinygrad-0.10.1.dist-info}/METADATA +32 -21
- tinygrad-0.10.1.dist-info/RECORD +86 -0
- {tinygrad-0.10.0.dist-info → tinygrad-0.10.1.dist-info}/WHEEL +1 -1
- tinygrad/engine/lazy.py +0 -228
- tinygrad/function.py +0 -212
- tinygrad/multi.py +0 -177
- tinygrad/runtime/graph/clang.py +0 -39
- tinygrad-0.10.0.dist-info/RECORD +0 -77
- {tinygrad-0.10.0.dist-info → tinygrad-0.10.1.dist-info}/LICENSE +0 -0
- {tinygrad-0.10.0.dist-info → tinygrad-0.10.1.dist-info}/top_level.txt +0 -0
tinygrad/renderer/ptx.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
from typing import
|
1
|
+
from typing import cast, Callable
|
2
2
|
import struct
|
3
3
|
from collections import defaultdict
|
4
4
|
from tinygrad.ops import Ops, UOp, PatternMatcher, UPat, GroupOp
|
5
5
|
from tinygrad.dtype import dtypes, DType, PtrDType
|
6
6
|
from tinygrad.renderer import Renderer
|
7
7
|
from tinygrad.renderer.cstyle import CUDARenderer
|
8
|
-
from tinygrad.helpers import
|
8
|
+
from tinygrad.helpers import flatten, get_single_element
|
9
9
|
|
10
10
|
def render_val(x, dtype):
|
11
11
|
if dtypes.is_float(dtype):
|
@@ -14,30 +14,30 @@ def render_val(x, dtype):
|
|
14
14
|
return "0f%02X%02X%02X%02X" % tuple(struct.pack("f",x)[::-1])
|
15
15
|
return str(int(x)) + ("U" if dtypes.is_unsigned(dtype) else "")
|
16
16
|
|
17
|
-
asm_for_op:
|
17
|
+
asm_for_op: dict[Ops, Callable] = {
|
18
18
|
Ops.RECIP: lambda d,a,dt,name: f"rcp{'.approx' if dtypes.is_float(dt) else ''}.{name} {d}, {a};",
|
19
19
|
Ops.EXP2: lambda d,a,dt,name: f"ex2.approx.{name} {d}, {a};", Ops.LOG2: lambda d,a,dt,name: f"lg2.approx.{name} {d}, {a};",
|
20
20
|
Ops.SIN: lambda d,a,dt,name: f"sin.approx.{name} {d}, {a};", Ops.SQRT: lambda d,a,dt,name: f"sqrt.approx.{name} {d}, {a};",
|
21
21
|
Ops.SHR: lambda d,a,b,dt,name: f"shr.{name} {d}, {a}, {b};", Ops.SHL: lambda d,a,b,dt,name: f"shl.b{name[1:]} {d}, {a}, {b};",
|
22
|
-
Ops.ADD: lambda d,a,b,dt,name: f"{'or' if
|
23
|
-
Ops.MUL: lambda d,a,b,dt,name:
|
24
|
-
Ops.XOR: lambda d,a,b,dt,name: f"xor.pred {d}, {a}, {b};" if
|
25
|
-
Ops.AND: lambda d,a,b,dt, name: f"and.pred {d}, {a}, {b};" if
|
26
|
-
Ops.OR: lambda d,a,b,dt, name: f"or.pred {d}, {a}, {b};" if
|
27
|
-
Ops.IDIV: lambda d,a,b,dt,name: f"div.{name} {d}, {a}, {b};",
|
28
|
-
Ops.MAX: lambda d,a,b,dt,name: f"max.{name} {d}, {a}, {b};",
|
22
|
+
Ops.ADD: lambda d,a,b,dt,name: f"{'or' if dt == dtypes.bool else 'add'}.{name} {d}, {a}, {b};",
|
23
|
+
Ops.MUL: lambda d,a,b,dt,name: f"{'and' if dt == dtypes.bool else 'mul'}{'.lo' if dtypes.is_int(dt) else ''}.{name} {d}, {a}, {b};",
|
24
|
+
Ops.XOR: lambda d,a,b,dt,name: f"xor.pred {d}, {a}, {b};" if dt == dtypes.bool else f"xor.b{name[1:]} {d}, {a}, {b};",
|
25
|
+
Ops.AND: lambda d,a,b,dt, name: f"and.pred {d}, {a}, {b};" if dt == dtypes.bool else f"and.b{name[1:]} {d}, {a}, {b};",
|
26
|
+
Ops.OR: lambda d,a,b,dt, name: f"or.pred {d}, {a}, {b};" if dt == dtypes.bool else f"or.b{name[1:]} {d}, {a}, {b};",
|
27
|
+
Ops.IDIV: lambda d,a,b,dt,name: f"div.{name} {d}, {a}, {b};", Ops.MOD: lambda d,a,b,dt,name: f"rem.{name} {d}, {a}, {b};",
|
28
|
+
Ops.MAX: lambda d,a,b,dt,name: f"max.{name} {d}, {a}, {b};",
|
29
29
|
Ops.CMPLT: lambda d,a,b,dt,name: f"setp.lt.{name} {d}, {a}, {b};", Ops.CMPNE: lambda d,a,b,dt,name: f"setp.ne.{name} {d}, {a}, {b};",
|
30
30
|
Ops.MULACC: lambda d,a,b,c,dt,name: f"{'fma.rn' if dtypes.is_float(dt) else 'mad.lo'}.{name} {d}, {a}, {b}, {c};",
|
31
|
-
Ops.WHERE: lambda d,a,b,c,dt,name:
|
32
|
-
f"
|
31
|
+
Ops.WHERE: lambda d,a,b,c,dt,name: [f"@{a} mov.{name} {d}, {b};", f"@!{a} mov.{name} {d}, {c};"] if dt == dtypes.bool else \
|
32
|
+
f"selp.{'b16' if name == 'f16' else name} {d}, {b}, {c}, {a};"
|
33
33
|
}
|
34
34
|
|
35
|
-
supports_half
|
36
|
-
doesnt_support_half:
|
35
|
+
supports_half = (Ops.EXP2, Ops.ADD, Ops.MUL, Ops.MAX, Ops.CMPLT, Ops.WHERE)
|
36
|
+
doesnt_support_half: tuple[Ops, ...] = tuple(op for op in asm_for_op.keys() if op not in supports_half)
|
37
37
|
ptx_matcher = PatternMatcher([
|
38
38
|
# bool CMPNE is XOR, bool CMPLT is XOR+AND (universal makes this slow, this is for renderer only)
|
39
39
|
(UPat.var('x', dtype=dtypes.bool).ne(UPat.var('y')), lambda x,y: x^y),
|
40
|
-
(UPat.var('x', dtype=dtypes.bool)
|
40
|
+
(UPat.var('x', dtype=dtypes.bool)<UPat.var('y'), lambda x,y: (x^True)&y),
|
41
41
|
# upcast to float32 all the ops that don't support half
|
42
42
|
(UPat(doesnt_support_half, dtype=dtypes.half, name="x"),
|
43
43
|
lambda x: (UOp(x.op, dtypes.float32, tuple(vv.cast(dtypes.float32) for vv in x.src), x.arg).cast(dtypes.half))),
|
@@ -54,46 +54,46 @@ ptx_matcher = PatternMatcher([
|
|
54
54
|
(UPat.var("x") >> UPat.var("y"), lambda x,y: UOp(Ops.SHR, x.dtype, (x,y.cast(dtypes.uint))) if y.dtype != dtypes.uint else None),
|
55
55
|
])
|
56
56
|
|
57
|
-
def mem_type(x: UOp): return 'shared' if
|
57
|
+
def mem_type(x: UOp): return 'shared' if any(_x.op is Ops.DEFINE_LOCAL for _x in x.src[0].toposort) else 'global'
|
58
58
|
|
59
|
-
def
|
60
|
-
gate = f"@{ctx.r[pred]} " if pred is not None and pred.op is not Ops.IF else ""
|
61
|
-
return [f"{gate}st.{mem_type(bidx)}.v{var.dtype.count}.{ctx.mem_types[var.dtype.scalar()]} [{ctx.r[bidx]}+0], {{{', '.join(ctx.r[var])}}};"] \
|
62
|
-
if var.dtype.count > 1 else [f"{gate}st.{mem_type(bidx)}.{ctx.mem_types[var.dtype]} [{ctx.r[bidx]}+0], {ctx.r[var]};"]
|
63
|
-
|
64
|
-
def render_wmma(ctx: "PTXRenderer", x: UOp):
|
59
|
+
def render_wmma(ctx: "PTXRenderer", wmma: UOp):
|
65
60
|
assert ctx.wmma_r, "registry values for wmma must be populated"
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
yield f'mma.sync.aligned.m{M}n{N}k{K}.row.col.
|
75
|
-
|
76
|
-
|
61
|
+
(N, M, K), dtype_in, dtype_out = wmma.arg[1], wmma.arg[2], wmma.arg[3]
|
62
|
+
|
63
|
+
for src, regs in zip(wmma.src, ctx.wmma_r):
|
64
|
+
for i, reg in enumerate(regs): # pack input and acc registers
|
65
|
+
if (elems_per_reg := 4 // src.dtype.scalar().itemsize) == 1: yield f"mov.b32 {reg}, {ctx.r[src][i]};"
|
66
|
+
else: yield f"mov.b32 {reg}, {{{', '.join(ctx.r[src][i * elems_per_reg : (i+1) * elems_per_reg])}}};"
|
67
|
+
|
68
|
+
dt_map_in, dt_map_out = {dtypes.float: "tf32", dtypes.half: "f16"}, {dtypes.float: "f32"}
|
69
|
+
yield f'mma.sync.aligned.m{M}n{N}k{K}.row.col.{dt_map_out[dtype_out]}.{dt_map_in[dtype_in]}.{dt_map_in[dtype_in]}.{dt_map_out[dtype_out]}{" "*12}'+\
|
70
|
+
f'{{{", ".join(ctx.wmma_r[2])}}}, {{{", ".join(ctx.wmma_r[0])}}}, {{{", ".join(ctx.wmma_r[1])}}}, {{{", ".join(ctx.wmma_r[2])}}};'
|
71
|
+
|
72
|
+
for i, reg in enumerate(ctx.wmma_r[2]): # unpack acc registers
|
73
|
+
if (elems_per_reg := 4 // dtype_out.itemsize) == 1: yield f"mov.b32 {ctx.r[wmma][i]}, {reg};"
|
74
|
+
else: yield f"mov.b32 {{{', '.join(ctx.r[wmma][i * elems_per_reg : (i+1) * elems_per_reg])}}}, {reg};"
|
77
75
|
|
78
76
|
def modifier(a: DType, b: DType): return '.rzi' if dtypes.is_int(a) and dtypes.is_float(b) else '.rn' if dtypes.is_float(a) and \
|
79
77
|
(a.itemsize < b.itemsize or dtypes.is_int(b) or b == dtypes.bool) else ''
|
80
78
|
|
81
79
|
string_rewrite = PatternMatcher([
|
82
|
-
(UPat(
|
83
|
-
(UPat(
|
84
|
-
(UPat(Ops.STORE, name="x", src=(UPat.var('bidx'), UPat.var("var")
|
80
|
+
(UPat.cvar("x", dtypes.bool), lambda ctx, x: f"setp.ne.s16 {ctx.r[x]}, {render_val(x.arg, x.dtype)}, 0;"),
|
81
|
+
(UPat.cvar("x"), lambda ctx, x: f"mov.b{ctx.types[x.dtype][1:]} {ctx.r[x]}, {render_val(x.arg, x.dtype)};"),
|
82
|
+
(UPat(Ops.STORE, name="x", src=(UPat.var('bidx'), UPat.var("var")), allow_any_len=True), lambda ctx, x, bidx, var: f"st.{mem_type(bidx)}" + \
|
83
|
+
f"{f'.v{cnt}' if ((cnt:=var.dtype.count)>1) else ''}.{ctx.mem_types[var.dtype.scalar()]} " + \
|
84
|
+
f"[{ctx.r[bidx]}+0], {('{' + ', '.join(ctx.r[var]) + '}') if var.dtype.count > 1 else ctx.r[var]};"),
|
85
85
|
(UPat(Ops.SPECIAL, name="x"), lambda ctx,x: f"mov.u32 %{x.arg[0]}, %{'ctaid' if x.arg[0][0] == 'g' else 'tid'}.{chr(120+int(x.arg[0][-1]))};"),
|
86
86
|
(UPat(Ops.DEFINE_GLOBAL, name="x"), lambda ctx, x: f"ld.param.{ctx.types[dtypes.ulong]} {ctx.r[x]}, [data{x.arg}+0];"),
|
87
|
-
(UPat((Ops.CMPLT, Ops.CMPNE), name="x"),
|
88
|
-
|
87
|
+
(UPat((Ops.CMPLT, Ops.CMPNE), name="x", allow_any_len=True, src=(UPat.var("src0"),)),
|
88
|
+
lambda ctx, x, src0: ctx.code_for_op[x.op](ctx.r[x], *[ctx.r[v] for v in x.src], src0.dtype, ctx.types[src0.dtype])),
|
89
89
|
(UPat(GroupOp.ALU, name="x"), lambda ctx, x: ctx.code_for_op[x.op](ctx.r[x], *[ctx.r[v] for v in x.src], x.dtype, ctx.types[x.dtype])),
|
90
|
-
(UPat(Ops.BITCAST, name="x", src=(UPat.var("a")), allow_any_len=True), lambda ctx, x, a: f"mov.b{ctx.types[x.dtype][1:]} {ctx.r[x]}, {ctx.r[a]};"),
|
91
|
-
(UPat(Ops.CAST, name="x", src=(UPat(dtype=dtypes.bool, name="a"))),
|
90
|
+
(UPat(Ops.BITCAST, name="x", src=(UPat.var("a"),), allow_any_len=True), lambda ctx, x, a: f"mov.b{ctx.types[x.dtype][1:]} {ctx.r[x]}, {ctx.r[a]};"),
|
91
|
+
(UPat(Ops.CAST, name="x", src=(UPat(dtype=dtypes.bool, name="a"),)),
|
92
92
|
lambda ctx, x, a: f"selp.b{ctx.types[x.dtype][1:]} {ctx.r[x]}, {render_val(1, x.dtype)}, {render_val(0, x.dtype)}, {ctx.r[a]};"),
|
93
|
-
(UPat(Ops.CAST, name="x", dtype=dtypes.bool),
|
94
|
-
lambda ctx, x: f"setp.ne.b{ctx.types[
|
95
|
-
(UPat(Ops.CAST, name="x", src=(UPat.var("a"))),
|
96
|
-
lambda ctx, x, a: f"cvt{modifier(x.dtype, a.dtype)}.{ctx.types[x.dtype]}.{ctx.types[
|
93
|
+
(UPat(Ops.CAST, name="x", dtype=dtypes.bool, src=(UPat.var("a"),)),
|
94
|
+
lambda ctx, x, a: f"setp.ne.b{ctx.types[a.dtype][1:]} {ctx.r[x]}, {ctx.r[a]}, {render_val(0, a.dtype)};"),
|
95
|
+
(UPat(Ops.CAST, name="x", src=(UPat.var("a"),)),
|
96
|
+
lambda ctx, x, a: f"cvt{modifier(x.dtype, a.dtype)}.{ctx.types[x.dtype]}.{ctx.types[a.dtype]} {ctx.r[x]}, {ctx.r[a]};"),
|
97
97
|
(UPat(Ops.LOAD, name="x", src=(UPat.var('loc'), UPat(name='alt'), UPat(name="gate", op=GroupOp.ALU))), lambda ctx, x, loc, alt, gate: flatten([
|
98
98
|
[f"mov.{ctx.mem_types[x.dtype.scalar()]} {v}, {render_val(0, x.dtype.scalar())};" for v in ctx.r[x]],
|
99
99
|
[f"@{ctx.r[gate]} ld.{mem_type(x)}.v{x.dtype.count}.{ctx.mem_types[x.dtype.scalar()]} {{{', '.join(ctx.r[x])}}}, [{ctx.r[loc]}+0];"]
|
@@ -101,20 +101,11 @@ string_rewrite = PatternMatcher([
|
|
101
101
|
f"@{ctx.r[gate]} ld.{mem_type(x)}.{ctx.mem_types[x.dtype.scalar()]} {ctx.r[x]}, [{ctx.r[loc]}+0];",
|
102
102
|
f"@!{ctx.r[gate]} mov.b{ctx.types[x.dtype.scalar()][1:]} {ctx.r[x]}, {ctx.r[alt]};"]),
|
103
103
|
(UPat(Ops.LOAD, name="x", src=(UPat.var('loc'),), allow_any_len=True),
|
104
|
-
lambda ctx, x, loc: f"
|
104
|
+
lambda ctx, x, loc: f"ld.{mem_type(x)}.v{x.dtype.count}.{ctx.mem_types[x.dtype.scalar()]} {{{', '.join(ctx.r[x])}}}, [{ctx.r[loc]}+0];" \
|
105
105
|
if x.dtype.count > 1 else f"ld.{mem_type(x)}.{ctx.mem_types[x.dtype]} {ctx.r[x]}, [{ctx.r[loc]}+0];"),
|
106
|
-
(UPat(Ops.DEFINE_ACC, name="x", src=(UPat(
|
107
|
-
lambda ctx, x, pred: flatten([
|
108
|
-
[f"setp.ne.s16 {ctx.r[pred][i]}, {render_val(pred.src[0].arg, x.dtype.scalar())}, 0;",
|
109
|
-
f"mov.b{ctx.types[x.dtype.scalar()][1:]} {uu}, {ctx.r[pred][i]};"] for i, uu in enumerate(ctx.r[x])])),
|
110
|
-
(UPat(Ops.DEFINE_ACC, name="x", src=(UPat(name="pred", op=Ops.VECTORIZE, dtype=dtypes.half),), allow_any_len=True),
|
111
|
-
lambda ctx, x, pred: flatten([[f"mov.b{ctx.types[x.dtype.scalar()][1:]} {ctx.r[pred][i]}, {render_val(pred.src[0].arg, x.dtype.scalar())};",
|
112
|
-
f"mov.b{ctx.types[x.dtype.scalar()][1:]} {uu}, {ctx.r[pred][i]};"] for i, uu in enumerate(ctx.r[x])])),
|
113
|
-
(UPat(Ops.DEFINE_ACC, name="x", src=(UPat(name="pred", op=Ops.VECTORIZE),), allow_any_len=True), lambda ctx, x, pred: [
|
114
|
-
f"mov.b{ctx.types[x.dtype.scalar()][1:]} {uu}, {render_val(pred.src[0].arg, x.dtype.scalar())};" for i, uu in enumerate(ctx.r[x])]),
|
115
|
-
(UPat(Ops.DEFINE_ACC, name="x", src=(UPat(name="pred", op=Ops.CONST, dtype=dtypes.bool), ), allow_any_len=True), lambda ctx, x, pred: [
|
106
|
+
(UPat(Ops.DEFINE_ACC, name="x", src=(UPat.cvar("pred", dtype=dtypes.bool),), allow_any_len=True), lambda ctx, x, pred: [
|
116
107
|
f"setp.ne.s16 {ctx.r[pred]}, {render_val(pred.arg, pred.dtype)}, 0;", f"mov.pred {ctx.r[x]}, {ctx.r[pred]};"]),
|
117
|
-
(UPat(Ops.DEFINE_ACC, name="x", src=(UPat(
|
108
|
+
(UPat(Ops.DEFINE_ACC, name="x", src=(UPat.cvar("pred"),), allow_any_len=True),
|
118
109
|
lambda ctx, x, pred: f"mov.b{ctx.types[x.dtype][1:]} {ctx.r[x]}, {render_val(pred.arg, x.dtype)};"),
|
119
110
|
(UPat(Ops.RANGE, name="x"), lambda ctx, x: [f"mov.u32 {ctx.r[x]}, {ctx.r[x.src[0]]};", "LOOP_" + f"{ctx.r[x][1:]}:"]),
|
120
111
|
(UPat(Ops.ASSIGN, name="x", dtype=dtypes.bool), lambda ctx, x: [f"mov.pred {ctx.r[x.src[0]]}, {ctx.r[x.src[1]]};"]),
|
@@ -124,7 +115,7 @@ string_rewrite = PatternMatcher([
|
|
124
115
|
ctx.code_for_op[Ops.CMPLT](ctx.r[x], ctx.r[x.src[0]], ctx.r[src0.src[1]], dtypes.int, ctx.types[dtypes.int]),
|
125
116
|
f"@{ctx.r[x]} bra LOOP_{ctx.r[src0][1:]};"]),
|
126
117
|
(UPat(Ops.DEFINE_LOCAL, name="x"),
|
127
|
-
lambda ctx, x: [f".shared .align 4 .b8 {x.arg
|
118
|
+
lambda ctx, x: [f".shared .align 4 .b8 {x.arg}[{x.dtype.size*x.dtype.itemsize}];", f"mov.u64 {ctx.r[x]}, {x.arg}[0];"]),
|
128
119
|
(UPat(Ops.IF, name="x"), lambda ctx, x: f"@!{ctx.r[x.src[0]]} bra IF_{ctx.r[x.src[0]][1:]}_{ctx.uops.index(x)};"),
|
129
120
|
(UPat(Ops.ENDIF, name="x"), lambda ctx, x: f"IF_{ctx.r[x.src[0].src[0]][1:]}_{ctx.uops.index(x.src[0])}:"),
|
130
121
|
(UPat(Ops.WMMA, name="x"), lambda ctx, x: list(render_wmma(ctx, x))),
|
@@ -136,11 +127,12 @@ class PTXRenderer(Renderer):
|
|
136
127
|
device = "CUDA"
|
137
128
|
suffix = "PTX"
|
138
129
|
global_max, local_max, shared_max = CUDARenderer.global_max, CUDARenderer.local_max, CUDARenderer.shared_max
|
139
|
-
|
130
|
+
tc_sm80 = [tc for tc in CUDARenderer.tc_sm80 if tc.dtype_in in [dtypes.half, dtypes.float]]
|
140
131
|
code_for_op = asm_for_op
|
141
132
|
extra_matcher = ptx_matcher
|
142
133
|
def __init__(self, arch:str, device="CUDA"):
|
143
|
-
self.device, self.
|
134
|
+
self.device, self.arch = device, arch
|
135
|
+
self.tensor_cores = PTXRenderer.tc_sm80 if int(arch[3:]) >= 80 else CUDARenderer.tc_sm75 if int(arch[3:]) >= 75 else []
|
144
136
|
def __reduce__(self): return self.__class__, (self.arch, self.device)
|
145
137
|
|
146
138
|
# language options
|
@@ -149,33 +141,29 @@ class PTXRenderer(Renderer):
|
|
149
141
|
.address_size 64
|
150
142
|
.visible .entry"""
|
151
143
|
barrier = "bar.sync\t0;"
|
152
|
-
supports_half = supports_half
|
153
144
|
# HACK: Use s16 and u16 for int8 and uint8 buffers. This can be wrong in cast.
|
154
|
-
types:
|
145
|
+
types: dict[DType, str] = { dtypes.int8: "s16", dtypes.int16: "s16", dtypes.int32: "s32", dtypes.int64: "s64",
|
155
146
|
dtypes.uint8: "u16", dtypes.uint16: "u16", dtypes.uint32: "u32", dtypes.uint64: "u64",
|
156
147
|
dtypes.float16: "f16", dtypes.float32: "f32", dtypes.float64: "f64", dtypes.bool: "pred" }
|
157
148
|
|
158
|
-
mem_types:
|
159
|
-
mem_types.update({dtypes.int8: "s8", dtypes.uint8: "u8", dtypes.bool: "u8", dtypes.float16: "b16"})
|
149
|
+
mem_types: dict[DType, str] = {**types, dtypes.int8: "s8", dtypes.uint8: "u8", dtypes.bool: "u8", dtypes.float16: "b16"}
|
160
150
|
|
161
151
|
def render_kernel(self, kernel, function_name, bufs, regs) -> str:
|
162
|
-
kernel = [f".reg .{reg.split('_')[-2]} %{reg}<{cnt}>;" for reg,cnt in regs] + kernel + ["ret;"]
|
163
152
|
def fmt(line): return line if line[0]=="$" else "\t" + line.replace(" ", "\t" if len(line.split(" ")[0]) > 7 else "\t\t", 1)
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
"\n}")
|
153
|
+
kernel = '\n'.join(map(fmt, [f".reg .{reg.split('_')[-2]} %{reg}<{cnt}>;" for reg,cnt in regs] + kernel + ["ret;"]))
|
154
|
+
params = ',\n\t'.join([f".param .{'u64' if dtype.__class__ == PtrDType else self.types[dtype]} {name}" for name,dtype in bufs])
|
155
|
+
return f"{self.kernel_prefix} {function_name}(\n\t{params}\n)\n{{\n{kernel}\n}}"
|
168
156
|
|
169
|
-
def render(self, name:str, uops:
|
170
|
-
kernel:
|
157
|
+
def render(self, name:str, uops:list[UOp]) -> str:
|
158
|
+
kernel:list[str] = []
|
171
159
|
bufs = []
|
172
160
|
|
173
|
-
c:
|
174
|
-
r:
|
161
|
+
c: defaultdict[str, int] = defaultdict(int)
|
162
|
+
r: dict[UOp, list[str]|str] = {}
|
175
163
|
self.r = r
|
176
164
|
self.uops = uops
|
177
165
|
|
178
|
-
def ssa(prefix:str, u:
|
166
|
+
def ssa(prefix:str, u:UOp|None=None, dtype:str|None=None) -> str:
|
179
167
|
nonlocal c, r
|
180
168
|
prefix += f"_{dtype if dtype is not None else self.types[cast(UOp, u).dtype]}_"
|
181
169
|
c[prefix] += 1
|
@@ -186,38 +174,30 @@ class PTXRenderer(Renderer):
|
|
186
174
|
r[u] = [cast(str,r[x]) for x in u.src]
|
187
175
|
continue
|
188
176
|
if u.op is Ops.GEP:
|
189
|
-
|
190
|
-
|
177
|
+
r[u] = r[u.src[0]][get_single_element(u.arg)]
|
178
|
+
continue
|
179
|
+
if u.op in {Ops.CAST, Ops.BITCAST} and (u.src[0].dtype == u.dtype or isinstance(u.src[0].dtype, PtrDType)):
|
180
|
+
r[u] = r[u.src[0]]
|
191
181
|
continue
|
192
|
-
if u.op
|
193
|
-
|
194
|
-
r[u] = r[u.src[0]]
|
195
|
-
continue
|
196
|
-
r[u] = ssa('cast', u, self.types[u.dtype])
|
197
|
-
elif u.op is Ops.ENDRANGE: r[u] = ssa("pred", u, dtype="pred")
|
198
|
-
elif u.op is Ops.RANGE: r[u] = ssa("ridx", u)
|
199
|
-
elif u.op in GroupOp.ALU: r[u] = ssa("alu", u)
|
200
|
-
elif u.op is Ops.DEFINE_ACC:
|
201
|
-
if u.dtype.scalar() in [dtypes.half, dtypes.bool]:
|
202
|
-
r[u.src[0]] = [ssa("const", u.src[0].src[0]) for _ in range(u.dtype.count)] if u.dtype.count > 1 else ssa("const", u.src[0])
|
203
|
-
r[u] = [ssa('acc', u, dtype=self.types[u.dtype.scalar()]) for _ in range(u.dtype.count)] if u.dtype.count > 1 else ssa("acc", u)
|
204
|
-
elif u.op is Ops.SPECIAL: r[u] = "%" + u.arg[0]
|
205
|
-
elif u.op is Ops.DEFINE_VAR:
|
206
|
-
bufs.append((u.arg[0], u.dtype))
|
207
|
-
r[u] = ssa("dat", u, self.types[u.dtype])
|
208
|
-
elif u.op is Ops.CONST: r[u] = ssa("const", u, dtype=self.types[u.dtype])
|
182
|
+
if u.op is Ops.SPECIAL: r[u] = "%" + u.arg[0]
|
183
|
+
elif u.op is Ops.DEFINE_VAR: bufs.append((u.arg[0], u.dtype))
|
209
184
|
elif u.op is Ops.LOAD:
|
210
185
|
assert u.src[0].dtype == dtypes.int64, "load isn't int64"
|
211
186
|
r[u] = [ssa('val', dtype=self.types[u.dtype.scalar()]) for _ in range(u.dtype.count)] if u.dtype.count > 1 else ssa('val', u)
|
212
|
-
elif u.op is Ops.
|
213
|
-
elif u.op is Ops.DEFINE_GLOBAL:
|
214
|
-
bufs.append((f"data{u.arg}", u.dtype))
|
215
|
-
r[u] = ssa('dat', u, self.types[dtypes.ulong if u.dtype.__class__ == PtrDType else u.dtype])
|
187
|
+
elif u.op is Ops.DEFINE_GLOBAL: bufs.append((f"data{u.arg}", u.dtype))
|
216
188
|
elif u.op is Ops.WMMA:
|
217
|
-
|
189
|
+
# registers for packing/unpacking input and acc
|
190
|
+
self.wmma_r = [[ssa("wmma_in", dtype="b32") for _ in range(0, len(r[u.src[0]]), 4 // u.arg[2].itemsize)],
|
191
|
+
[ssa("wmma_in", dtype="b32") for _ in range(0, len(r[u.src[1]]), 4 // u.arg[2].itemsize)],
|
192
|
+
[ssa("wmma_acc", dtype="b32") for _ in range(0, len(r[u.src[2]]), 4 // u.arg[3].itemsize)]]
|
218
193
|
r[u] = [ssa("wmma", dtype=self.types[u.dtype.scalar()]) for _ in range(u.dtype.count)]
|
219
|
-
|
220
|
-
|
194
|
+
prefix, dtype = {Ops.CAST: ("cast", None), Ops.BITCAST: ("cast", None), Ops.ENDRANGE: ("pred", "pred"), Ops.RANGE: ("ridx", None),
|
195
|
+
Ops.DEFINE_ACC: ("acc", None), Ops.DEFINE_VAR: ("dat", None), Ops.CONST: ("const", None), Ops.DEFINE_LOCAL:("local",self.types[dtypes.ulong]),
|
196
|
+
Ops.DEFINE_GLOBAL: ("dat", self.types[dtypes.ulong]), **{op: ("alu", None) for op in GroupOp.ALU}}.get(u.op, (None, None))
|
197
|
+
if prefix: r[u] = ssa(prefix, u, dtype)
|
198
|
+
|
199
|
+
if (l:=cast(str|list[str], string_rewrite.rewrite(u, ctx=self))) is None:
|
200
|
+
raise RuntimeError(f"failed to render {u.op} with {u.dtype} srcs {[x.dtype for x in u.src]}")
|
221
201
|
kernel.extend([l] if isinstance(l, str) else l)
|
222
202
|
|
223
203
|
if u.op is Ops.ASSIGN: r[u] = r[u.src[0]]
|
@@ -0,0 +1,87 @@
|
|
1
|
+
from tinygrad.dtype import DType, PtrDType, dtypes
|
2
|
+
from tinygrad.ops import UOp, Ops, PatternMatcher, UPat
|
3
|
+
from tinygrad.renderer.cstyle import CStyleLanguage, base_rewrite, extra_pm
|
4
|
+
from tinygrad.helpers import strip_parens
|
5
|
+
import math
|
6
|
+
|
7
|
+
def sign_extend(val:UOp, sext_am:int):
|
8
|
+
return (UOp.where((val >> (sext_am - 1)) > 0, UOp.const(dtypes.uint32, 0xffffffff) << sext_am, UOp.const(dtypes.uint32, 0)) \
|
9
|
+
| val.bitcast(dtypes.uint32)).bitcast(dtypes.int)
|
10
|
+
|
11
|
+
# store for char: buf[idx/4] <- (var << (idx%4)*8))
|
12
|
+
def packed_store(bidx:UOp, var:UOp):
|
13
|
+
shift_am = (bidx.src[1].cast(dtypes.uint32)%UOp.const(dtypes.uint32, 4//var.dtype.itemsize))*UOp.const(dtypes.uint32, 8*var.dtype.itemsize)
|
14
|
+
new_v = (var & (0xFF if var.dtype.itemsize == 1 else 0xFFFF)).cast(dtypes.uint32) << shift_am
|
15
|
+
mask = (((0xFF if var.dtype.itemsize == 1 else 0xFFFF) << shift_am) ^ 0xFFFFFFFF).cast(dtypes.uint32)
|
16
|
+
buf = UOp.load(UOp(Ops.INDEX, bidx.dtype, (bidx.src[0], bidx.src[1]//(4//var.dtype.itemsize))), dtype=dtypes.uint32)
|
17
|
+
return UOp.store(UOp(Ops.INDEX, bidx.dtype, (bidx.src[0], bidx.src[1]//(4//var.dtype.itemsize))), ((buf & mask) | new_v.cast(dtypes.uint32)))
|
18
|
+
|
19
|
+
# load for char: sign_extend(buf[idx/4] >> ((idx%4)*8))
|
20
|
+
def packed_load(root:UOp, bidx:UOp, dtype:DType, var:UOp|None=None):
|
21
|
+
div_idx = bidx.src[1]//(4//dtype.itemsize)
|
22
|
+
shift_am = (bidx.src[1].cast(dtypes.uint32)%UOp.const(dtypes.uint32, 4//dtype.itemsize))*UOp.const(dtypes.uint32, 8*dtype.itemsize)
|
23
|
+
if var is not None: load = UOp.load(UOp(Ops.INDEX, bidx.dtype, (bidx.src[0], div_idx)), var, root.src[2], dtype=dtypes.uint32, arg=root.arg)
|
24
|
+
else: load = UOp.load(UOp(Ops.INDEX, bidx.dtype, (bidx.src[0], div_idx)), *root.src[1:], dtype=dtypes.uint32, arg=root.arg)
|
25
|
+
val = (load.cast(dtypes.uint32) >> shift_am) & (0xFF if dtype.itemsize == 1 else 0xFFFF)
|
26
|
+
return sign_extend(val, 8*dtype.itemsize).cast(dtype) if dtype in [dtypes.char, dtypes.short] else val.cast(dtype)
|
27
|
+
|
28
|
+
wgsl_matcher = PatternMatcher([
|
29
|
+
(UPat((Ops.CMPLT, Ops.XOR), src=(UPat(name="a", dtype=dtypes.bool), UPat.var("b")), name="c"),
|
30
|
+
lambda a,b,c: a.cast(dtypes.int).alu(c.op, b.cast(dtypes.int)).cast(dtypes.bool)),
|
31
|
+
(UPat(Ops.LOAD, name="l", src=(UPat.var('b'),)), lambda l,b: packed_load(l,b,l.dtype) if l.dtype.itemsize < 4 else None),
|
32
|
+
(UPat(Ops.LOAD, name="l", src=(UPat.var('b'), UPat.var('c'), UPat())),
|
33
|
+
lambda l,b,c: packed_load(l,b,l.dtype,c.cast(dtypes.uint32)) if l.dtype.itemsize < 4 else None),
|
34
|
+
(UPat.store(UPat.var("bidx"), UPat.var("var"), allow_any_len=True), lambda bidx,var: packed_store(bidx,var) if var.dtype.itemsize < 4 else None),
|
35
|
+
# TODO: why is this needed, and only for this MUL order
|
36
|
+
(UPat(Ops.MUL, src=(UPat.var("a"), UPat.var("g").where(UPat.cvar("c1"), UPat.cvar("c2")))),
|
37
|
+
lambda a,g,c1,c2: g.where(c1, a) if math.isnan(c1.arg) and c2.arg == 1.0 else None),
|
38
|
+
]) + extra_pm
|
39
|
+
|
40
|
+
class WGSLRenderer(CStyleLanguage):
|
41
|
+
device = "WEBGPU"
|
42
|
+
global_max = (65535, 65535, 65535)
|
43
|
+
local_max = (256, 256, 64)
|
44
|
+
code_for_workitem = {"g": lambda x: f"i32(gindex.{'xyz'[int(x)]})", "l": lambda x: f"i32(lindex.{'xyz'[int(x)]})"}
|
45
|
+
extra_matcher = wgsl_matcher
|
46
|
+
supports_float4 = False
|
47
|
+
barrier = "workgroupBarrier();"
|
48
|
+
code_for_op = {**CStyleLanguage.code_for_op, Ops.WHERE: lambda a,b,c,dtype: f"select({c},{b},{a})"}
|
49
|
+
nan = "nan()"
|
50
|
+
type_map = { dtypes.float: "f32", dtypes.uchar: "u32", dtypes.ushort: "u32", dtypes.short: "i32",
|
51
|
+
dtypes.char: "i32", dtypes.int32: "i32", dtypes.uint32: "u32", dtypes.bool: "bool" }
|
52
|
+
|
53
|
+
string_rewrite = PatternMatcher([
|
54
|
+
(UPat(Ops.CONST, dtype=dtypes.bool, name="x"), lambda ctx,x: "true" if x.arg else "false"),
|
55
|
+
(UPat(Ops.CONST, dtype=(dtypes.uchar, dtypes.ushort, dtypes.uint32), name="x"), lambda ctx,x: f"bitcast<u32>({x.arg})" \
|
56
|
+
if x.arg < 0 else f"{x.arg&0xFFFFFFFF}u"),
|
57
|
+
(UPat(Ops.DEFINE_LOCAL, name="x"), lambda ctx,x: f"var<workgroup> {ctx[x]}: array<{ctx.buf_map(x.dtype.base)}, {x.dtype.size}>;"),
|
58
|
+
(UPat(Ops.BITCAST, name="x"), lambda ctx,x: f"bitcast<{ctx.type_map[x.dtype]}>({ctx[x.src[0]]}{['&0xFF','&0xFFFF','',''][x.dtype.itemsize-1]})"),
|
59
|
+
(UPat.load(UPat.var("b"),UPat.var("v"),UPat.var("g")),lambda ctx,b,v,g:f"select({ctx[v]}, {ctx.render_load(ctx[b],b.src[0].dtype)}, {ctx[g]})"),
|
60
|
+
(UPat.load(UPat.var("b"), allow_any_len=True), lambda ctx, b: ctx.render_load(ctx[b], b.src[0].dtype)),
|
61
|
+
(UPat.index(UPat.var("b"), UPat.var("idx")), lambda ctx,b,idx: f"{ctx[b]}[{strip_parens(ctx[idx]) if idx.arg == Ops.ADD else ctx[idx]}]"),
|
62
|
+
(UPat.store(UPat.var('b'), UPat.var("v"), allow_any_len=True),lambda ctx,b,v:\
|
63
|
+
# (load & mask) | var -> mask = v.src[0].src[1], var = v.src[1]
|
64
|
+
f"atomicAnd(&{ctx[b]},{ctx[v.src[0].src[1]]});\n atomicAdd(&{ctx[b]},{ctx[v.src[1]]});" if b.src[0].dtype.itemsize < 4 \
|
65
|
+
else f"{ctx[b]} = {ctx[v]};"),
|
66
|
+
# fix nan check: 'a != a -> is_nan()'
|
67
|
+
(UPat.var("a") != UPat.var("a"), lambda ctx,a: f"is_nan({ctx[a]})"),
|
68
|
+
]) + base_rewrite
|
69
|
+
|
70
|
+
def render_cast(self, dt:DType, val: str) -> str: return f"{self.type_map[dt]}({val})"
|
71
|
+
def render_dtype(self, dt:DType, mutable=True) -> str: return "var"
|
72
|
+
def render_load(self, x:str, dt:DType) -> str: return f"atomicLoad(&{x})" if dt.itemsize < 4 else x
|
73
|
+
def buf_map(self, dt:DType) -> str: return "atomic<u32>" if dt.itemsize < 4 else self.type_map[dt.base]
|
74
|
+
def render_kernel(self, function_name:str, kernel:list[str], bufs:list[tuple[str,tuple[DType,bool]]], uops:list[UOp], prefix=None) -> str:
|
75
|
+
local_size = [num for _, num in sorted([u.arg for u in uops if u.op is Ops.SPECIAL and u.arg[0][0] == 'l'], key=lambda x: x[0])]
|
76
|
+
if not local_size: local_size = [1]
|
77
|
+
bind_it = iter(range(len(bufs)))
|
78
|
+
external_local_bufs = [line.lstrip() for line in kernel if "var<workgroup>" in line]
|
79
|
+
kernel[:] = [line for line in kernel if "var<workgroup>" not in line]
|
80
|
+
prg = "fn nan() -> f32 { let bits = 0xffffffffu; return bitcast<f32>(bits); }\n"
|
81
|
+
# trick to obfuscate compiler so that nan is detected properly
|
82
|
+
prg += "fn is_nan(v:f32) -> bool { return min(v, 1.0) == 1.0 && max(v, -1.0) == -1.0; }\n@group(0) @binding(0)\nvar<uniform> INFINITY : f32;\n"
|
83
|
+
prg += "\n".join((external_local_bufs or [])+[f"@group(0) @binding({next(bind_it)+1})" +
|
84
|
+
f"{'var<storage,read_write>' if isinstance(dtype, PtrDType) else 'var<uniform>'}" +
|
85
|
+
f"{name}:{f'array<{self.buf_map(dtype.base)}>' if isinstance(dtype,PtrDType) else self.buf_map(dtype)};" for name,(dtype,_) in bufs])
|
86
|
+
prg += f"\n@compute @workgroup_size({','.join([str(x) for x in local_size])}) fn {function_name}(@builtin(workgroup_id) gindex: vec3<u32>,"
|
87
|
+
return prg + "@builtin(local_invocation_id) lindex: vec3<u32>) {\n" + "\n".join(kernel) + "\n}"
|