The language¶
A kernel is a Python function, traced once per set of constexprs into the hk dialect, translated to HipKittens C++ and compiled by hipcc. This page is the language a kernel body is written in. For what happens to it after tracing, see the IR and the runtime; for complete programs, see the examples.
Before writing a kernel, see whether a template does the job:
hk.templates.elementwise / rowwise for
the math of an element or a row, hk.templates.gemm for
a GEMM with a fused epilogue. They reach the tuned kernels' speed in a few
lines. This page is for the kernels they do not cover. Every name it uses is
in the API reference.
- Kernels and parameters
- Types
- Values and operators
- Memory
- Rows by index, atomics, random numbers
- Control flow
- Between workgroups, XCDs and GPUs:
hk.comm(gfx950) - Which XCD runs a workgroup: placement
- Namespaces
@hk.func- Errors and locations
- Limits
Kernels and parameters¶
A kernel is a Python function. Positional parameters are tensors, 4-D
(batch, depth, rows, cols): hk.Tensor[hk.T] takes its dtype from the call,
so one definition serves bf16 and fp16 (below),
and hk.Tensor[hk.fp32] fixes one. Constexprs are the
keyword-only parameters, or any parameter annotated hk.Constexpr[int] with a
default. They are baked into the source and are ordinary Python values in the
body, so range(D // 16) unrolls and if KIND == "softmax" picks a branch at
trace time.
A plain tensor's outer axes are compact over its rows: the launcher takes a
contiguous tensor or a slice of its last axis (one row stride). An engine's
layout often is neither, and hk.Tensor[dtype, "strided"] takes it in place:
every stride the torch tensor has (batch, depth, row) and a granule stride
along the columns, the distance between consecutive 16-byte granules of a row.
Pass a 4-D view whose last axis is contiguous -- vLLM's flash-layout KV cache
(blocks, block, heads, d) as .permute(0, 2, 1, 3) -- or a 5-D view
(b, d, r, granules, G) whose last axis is one 16-byte granule -- ROCM_ATTN's
x-split K cache (blocks, heads, d / 8, block, 8) as .permute(0, 1, 3, 2, 4),
whose rows are then 8 elements apart and their granules block * 8. RDNA only,
and read by g.stage so far; translating a kernel that does anything else with
one is an error that names the op.
TILE = hk.RT(hk.fp32, 16, 64)
@hk.kernel(grid=lambda p: (1, hk.cdiv(p.x.rows, 16), p.x.batch))
def row_sumsq(x: hk.Tensor[hk.T], o: hk.Tensor[hk.fp32], *, warps=4):
row = hk.arch.block_idx.y * 16
acc = hk.zeros(hk.col_vec(TILE))
for cb in range(hk.arch.warp_id(), hk.cdiv(x.cols, 64), warps):
v = x.at[row, cb * 64].load(TILE) # global -> registers, converted to fp32
acc += (v * v).sum(axis=1) # one row_sum, accumulated in place
o.at[hk.arch.warp_id(), row] = acc # a vector is stored along a row
Each warp sums every warps-th 64-column chunk of 16 rows, and stores its 16
partial sums along a row of o: with o of shape (warps, rows), o.sum(0)
is each row's sum of squares. row_sumsq(x, o) takes x's dtype, bf16 or
fp16, at 4 warps; row_sumsq(x, o8, warps=8) with an 8-row o8 at 8.
@hk.kernel takes grid, warps (unless the kernel has a warps parameter, as
above), and optionally arch, name,
max_vgprs, min_occupancy, max_occupancy, defines, resident and placement
(python/hk/dsl/kernel.py; the last two are
below).
defines is a dict emitted as #define KEY value lines ahead of the
generated C++, for the knobs of the headers it includes. The one a kernel
most often needs is defines={"HK_BF16_ROUND": 2} on gfx1100: there a
conversion to bf16 in registers (t.to(hk.bf16), copy()) rounds ties
away from zero, HipKittens' cheaper default, while every store that narrows
rounds to nearest even as torch does, and gfx950 rounds to even
throughout. A kernel that converts in registers and must match torch bit
for bit -- a residual x + r of two bf16 values, which is a tie about one
time in ten -- sets it. The grid is a
function of the parameters: p.<tensor>.<batch|depth|rows|cols> gives an
extent, p.<CONSTEXPR> gives a constexpr value and p.warps the warp count,
and it must return a 3-tuple. A kernel that names no arch is built for hk.target.current_arch()
(see runtime). max_vgprs and min_occupancy are
extra build gates on top of the unconditional no-spill rule
(resource gates). max_occupancy=N is not a
gate but a cap: the kernel holds enough VGPRs that at most N waves fit a SIMD
(an empty asm clobbers the register the target's model puts at that level). A
schedule tuned at an occupancy keeps it when a compile frees registers -- fewer
concurrent waves can read memory faster, as hk.ops.decode_gemm's two- and
four-tile schedules do (6 and 5 waves).
A constexpr does not have to be keyword-only. A positional one is a constexpr when it is annotated so, and it still needs a default, because every constexpr is baked into the source:
@hk.kernel(grid=lambda p: (hk.cdiv(p.o.cols, 64), hk.cdiv(p.o.rows, 16), p.o.batch))
def scale_pow2(x: hk.Tensor[hk.T], o: hk.Tensor[hk.T], N: hk.Constexpr[int] = 1):
idx = hk.tile_coord(hk.arch.block_idx.z, 0, hk.arch.block_idx.y,
hk.arch.block_idx.x)
v = x[idx].load(hk.RT(hk.fp32, 16, 64))
for _ in range(N): # N is a Python int: unrolled
v = v + v
o[idx] = v.to(o.dtype)
Constexpr values are ints or floats. Specialising on a constexpr produces a
different kernel, and a different compilation. k.specialize(D=64) is
explicit about that; calling with a different value does the same thing
implicitly — k(x, o, EPS=eps) passes the constexpr to the call itself, uses
the caches directly and makes no object at all. specialize() is for handing
a configured kernel to something that will call it many times.
A tensor with fewer than four dimensions fills the trailing ones, so a 2-D
tensor is (rows, cols) (include/pyutils/hk_bind.cuh).
One kernel for every dtype and warp count¶
A tensor annotated with a dtype parameter, hk.Tensor[hk.T], takes its dtype
from the call, and a keyword-only parameter named warps makes the warp count
an argument. One definition then covers every dtype and warp count:
@hk.kernel(grid=lambda p: (hk.cdiv(p.o.cols, 64 * p.warps), hk.cdiv(p.o.rows, 16), 1))
def gated_silu(a: hk.Tensor[hk.T], b: hk.Tensor[hk.T], o: hk.Tensor[hk.T], *, warps=4):
col = (hk.arch.block_idx.x * warps + hk.arch.warp_id()) * 64
idx = hk.elem_coord(0, 0, hk.arch.block_idx.y * 16, col)
t = hk.RT(hk.fp32, 16, 64) # the load converts; silu is fp32
g = a[idx].load(t)
g.silu(into=g)
o[idx] = (g * b[idx].load(t)).to(o.dtype)
gated_silu(a, b, o) # T read from `a`: bf16 tensors, a bf16 kernel; 4 warps
gated_silu(a16, b16, o16, warps=8) # fp16, 8 warps
gated_silu(a, b, o, T=hk.bf16) # T given: nothing is read
- T is inferred from the first tensor annotated with it, and every tensor
sharing it must agree (a call with a bf16
aand an fp16bis refused, naming both).T=hk.fp16in the call sets it instead, and the call then reads no dtype. A second parameter isU = hk.TypeVar("U"):hk.Tensor[U]. - In the body the dtype is a tensor's
.dtype(o.dtypeabove), or the parameter's own name:Tis the boundhk.DTypein a kernel that imports it (from hk import T;hk.RT(T, 16, 64),x.to(T)). A module that also defines some other globalT-- a tile, say -- is told so when the kernel is made. warpsis the workgroup's warp count, per call (warps=8), with the parameter's default otherwise; in the body it is a plain int, in the gridp.warps.hk.kernel(warps=...)is then not also given. (p.warpsreads any kernel's warp count.)- Each (dtypes, warps) is its own kernel, traced, built and cached as if
written for that dtype -- the generated C++ is the same as a factory's, but
for the name:
gated_silu__Tbf16_warps4.gated_silu.variant(T=hk.fp16, warps=8)is that kernel, for whatever takes ahk.Kernel;trace,source,buildandspecializetake the same keywords (T=is required there: there are no tensors to read it from). - A launch costs what a plain kernel's does: the call reads the
.dtypeof each tensor annotated with an inferred parameter and probes one dict. On a W7900D,k(a, b, o)is within 0.1 us of calling the specialization itself, and a call withT=orwarps=about 0.2 us more than a plain kernel's call with a constexpr keyword (tools/hk-bench/generic_launch.py).
hk.ops builds its dtype- and warps-varying kernels this way:
fused.silu_mul_kernel, fused.strip_silu_mul_kernel, the rotary kernels
(fused.ROTARY, ROTARY16, ROTARY_HALVES: one kernel each without and with
positions -- a different signature), quant.quantize_kernel,
quant.dequantize_kernel, quant.strip_quantize_kernel and norm's row kernels
(norm.ROW_REDUCE, norm.RMS_WEIGHTED, norm.STRIP, by kind). Their tables
(fused.KERNELS[...], norm.KERNELS[...]) hold the specializations. What stays
a factory argument is what changes the signature or is code: a positions
tensor, a residual, a norm's kind, an elementwise op.
Kernels made by a factory¶
What neither a constexpr nor a dtype parameter carries -- the arch, a Python
function the body calls -- is a factory's argument: a function that
defines the kernel and returns it. @hk.specialize makes the factory build each
variant once and names the kernel after its arguments. Before 0.9.17 a factory
was also how one kernel took several dtypes or warp counts, as in this example;
that still works, but hk.T and warps (above)
replace it:
@hk.specialize
def silu_mul(*, dtype=hk.bf16, warps=4):
@hk.kernel(warps=warps, grid=lambda p: (hk.cdiv(p.o.cols, 64), hk.cdiv(p.o.rows, 16), 1))
def body(a: hk.Tensor[dtype], b: hk.Tensor[dtype], o: hk.Tensor[dtype]):
idx = hk.tile_coord(0, 0, hk.arch.block_idx.y, hk.arch.block_idx.x)
t = hk.RT(hk.fp32, 16, 64) # the load converts; silu is fp32
g = a[idx].load(t)
g.silu(into=g)
o[idx] = (g * b[idx].load(t)).to(dtype)
return body
k = silu_mul(dtype=hk.fp16) # built once; silu_mul(dtype=hk.fp16, warps=4) is the same kernel
k.name # 'silu_mul__dtypefp16_warps4'
The arguments, defaults included, are the key, so there is no
functools.lru_cache, no name string spelled from the arguments and no
body.__name__ = name. Without @hk.specialize, a kernel defined inside a
function and given no name= is named <function>__<values>
(<factory>_body__<values> for a def body) from the factory values its body reads (and its annotations' dtypes and its
warps), the same in every process. A top-level kernel keeps its function's
name, and name= always wins. A kernel's __name__ is its name, which is
what hk.profile, hk.check and tracebacks show. Renaming a kernel changes
its generated source, so it is compiled once more; nothing else is keyed on
the name.
Types¶
- Types are values you build:
hk.RT(dtype, rows, cols, layout, frag=None)a register tile,hk.RS(dtype, rows, cols, run=8)a register strip,hk.STan LDS tile,hk.col_vec(t)/hk.row_vec(t)the vectors a reduction oftgives,hk.SVan LDS vector. A tile'sfragis the matrix-core fragment it is laid out in, where that is not 16x16; left asNone, thehk-fragmentspass sets it on the operand tiles of a target that needs one (gfx950's bf16 operand is 16x32) (IR). On gfx950 an accumulator may befrag=(32, 32)(the 32x32x16 MFMA), and an operand may name its lane run,frag=(16, 32, 4): a lane holding two runs of 4 rows rather than one of 8, which is what a 32x32 accumulator's lane holds (see below).
Register strips¶
A tile's lane layout is the one WMMA gives it: a 16-bit row tile holds each
row twice, and an fp32 tile gives a lane alternate columns. A kernel that only
streams rows wants each lane to own contiguous runs of a row instead: on
16x64 tiles such a kernel leaves bandwidth unused
(tiles and strips). Elementwise and row-wise kernels
belong on strips, and the templates put
them there for you. That is
a strip (!hk.rt<f32, 1x1024, strip<8>>, kittens::rs):
- Layout. Lane l owns, of every row, the runs of run columns starting
at j*wave*run + l*run: a chunk is the wave's lanes, 32 on gfx11 and 64 on
gfx950. A bf16 run of 8 is one 16-byte access, and nothing is held twice.
cols is a whole number of wave*run-column chunks.
- Memory. A strip loads and stores in elements (x.at[r, c]), and
converts its element type on the way. bounded=True reads zero past the
tensor's last row and column, and writes nothing there.
- Operations. It maps, reduces along rows (a butterfly that leaves the
answer in every lane; on gfx950 in an order you can state, below) and
along columns, broadcasts its col_vec and
row_vec, and fills by column (right_fill_, left_fill_). On gfx950,
swap_halves() exchanges each row's two halves (column c takes column
(c + cols/2) % cols, RoPE's rotate_half without the sign): a register
swap for an even number of chunks, one v_permlane32_swap a dword for a
row of one chunk.
- Vectors. Its row_vec has the strip's own column layout, so a weight
vector loads with the accesses of the rows it scales. Its col_vec is
what hk.collective.cross_warp combines.
- Limits. It is never an MMA operand or an LDS tile. It exists where
Target.strips says: gfx1100 (include/rdna3), gfx1201 (include/rdna4)
and gfx950 (hk's wave64 port in include/hk/cdna.cuh); gfx942 refuses
it while tracing. Its elements are bf16,
fp16, fp32 or i32
(below).
# WARPS and EPS are constexprs; row and warp come from hk.arch
S = hk.RS(hk.fp32, 1, 512) # one row, 512 columns a warp
v = x.at[row, warp * 512].load(S) # 16 bytes a lane of bf16, twice on gfx11
sq = hk.collective.cross_warp((v * v).sum(axis=1), "add", WARPS)
v *= (sq / x.cols + EPS).rsqrt()
o.at[row, warp * 512].store(v)
A stated row-reduction order (gfx950). A result that must match another
kernel's bit for bit -- a norm's sum of squares against vLLM's or aiter's --
needs the order of the additions, not only their set.
t.sum(axis=1, lanes="seq" | "tree", xor=(32, 16, 8, 4, 2, 1)), and the same
for max, min and prod, states it:
- a lane's own values are combined left to right ("seq") or as a balanced
tree ("tree"), then the lanes over the given xor offsets in that order,
each of 1, 2, 4, 8, 16 and 32 once;
- the strip's values are summed as they are: (x * y).sum(...) adds the
rounded products, and the compiler does not fuse them into the sum;
- x.sum(axis=1, lanes="seq", xor=..., times=y) is the fused form:
s = fma(x0, y0, r(x1 * y1)), then s = fma(xi, yi, s), then the xor
sums. That is what a compiled s += a * b loop computes, and what
dsv41_mono's and aiter's norms do for their sum of squares (times=x).
Without the arguments nothing changes (a tree, then 1 .. 32). They are refused on wave32 targets, on columns and on WMMA tiles.
sq = v.sum(axis=1, lanes="seq", xor=(32, 16, 8, 4, 2, 1), times=v) # aiter's order
fp32 max and min: reductions and elementwise ops (gfx950). hk has two
kinds of max and min, with different rules for NaN and signed zeros.
- Reductions are IEEE 754-2019 maximum and minimum on gfx950
(llvm.maximum, one v_maximum3_f32 a pair of values): max / min along
a register tile's rows or columns and a strip's rows or columns,
group_max / group_min, the combine of cross_warp(v, "max" | "min")
and fold_rows, and m.max_(...) / m.min_(...) -- of a reduction or of
another vector, the online fold. A NaN anywhere in what is reduced gives
NaN, however the reduction is split across lanes, warps or blocks (as
torch.amax does), and -0 orders below +0: the max of -0 and +0 is +0, the
min -0.
- Elementwise hk.max / hk.min (a.maximum(b), a.minimum(b), and with
a number, a clamp) are fmaxf / fminf everywhere: a NaN operand gives the
other operand, like torch.fmax.
- gfx942 has no v_maximum3_f32, where llvm.maximum would become compares
and selects: its reductions keep fmaxf / fminf, as do RDNA's.
hk.ops.norm's RMSNorm, fused add + RMSNorm, LayerNorm and softmax run on
strips wherever a row fits one. On
gfx950 so do quantize, the elementwise ops, silu_mul (which then takes
the two halves of a gate/up output as strided views) and hk.ops.rotary,
vLLM's rotary_embedding in NeoX style, token-major, from a cos_sin_cache
of [cos | sin] rows and optional int32 positions.
Integer, mask and bit expressions on strips: hk.map¶
A strip may hold i32, hk.RS(hk.i32, rows, cols, run): index
arithmetic, exponent bits, scale bytes. It loads from and stores to i32
tensors like any strip, bounded=True included. .to(hk.fp32) and
.to(hk.i32) convert in registers, and an fp32 strip stored into an i32
tensor converts on the way; fp32 to i32 truncates toward zero (2.5 to 2, -1.7
to -1). There are no other integer strips (an i8 strip is refused).
- Operators on i32 strips are C's:
+ - * // % << >> & | ^ ~and unary-;//and%truncate toward zero (-7 // 2is -3,-7 % 2is -1, ashk.Int's),>>is arithmetic, and+,-,*must not overflow (C++'sint)./is refused on an i32 strip. The other operand is a strip of the same shape and run, anhk.Intor a Python int. - Comparisons of i32 or fp32 strips (
< <= == != > >=) make a mask, ab1strip;&,|,^and~combine masks, andhk.where(m, a, b)picksawheremholds, elseb(C'sm ? a : b;a,bstrips of one dtype or numbers). Float comparisons are C's: a NaN compares unequal to everything, sox != xfinds NaNs. A mask has no load or store, and using one as a Pythonboolis aTypeError.hk.min/hk.maxof i32 strips are signed. hk.math.fma/rcp/popcount/ctz/ceil/floorhave no tile op: of a strip each is one map.fmais one rounding,rcpisv_rcp_f32(within an ulp, not the division),ctz(0)is 32.hk.map(fn, *args)tracesfnonce on the elements -- element (r, c) of each strip argument, anhk.Intor a Python number as itself -- into one map: the operators above,.to(i32 <-> fp32, fp32 <-> bf16, a mask to i32),.bitcast,hk.where,hk.min/hk.maxand thehk.mathfunctions (exp2,log2,sqrtandabsof an element are C'sexp2f, ...). The result's dtype is whatfnreturns. Floating-point contraction is off in a map, soa * b + cis two roundings andhk.math.fma(a, b, c)one. Elsewhere HIP compiles with contraction on, and whether a product and a sum fused changed with the code around them;__fmul_rn/__fadd_rnare no barrier on ROCm.- One expression. Each operator builds a one-op map; the
hk-map-fusepass merges a chain into one, so((c >> 4) & 63) * w + ccompiles to the C++ expression an author would write. A value used twice is computed once, and nothing moves across a load, a store, a lane exchange or a barrier. - Where. On every target with strips: gfx950, gfx1100 and gfx1201. A map
prints as
kittens::map_cpp(dst, [&](const auto x0, ...) { ... }, args...), from include/hk/cdna on gfx950 and include/hk/rdna/map.cuh on RDNA. RDNA's strips hold no bool, so there a mask lives only inside a map: one used where it is made (hk.where(a < b, x, y),m1 & m2) or read once by another map is fine; one read twice, or stored, is a verifier error -- compute it inside onehk.map(fn)instead.
S = hk.RS(hk.fp32, 1, 512) # one row: 64 lanes x a run of 8
I = hk.RS(hk.i32, 1, 512) # the same layout, i32
@hk.kernel(warps=1, grid=lambda p: (p.x.rows, 1, 1))
def exp_ldexp(x: hk.Tensor[hk.fp32], n: hk.Tensor[hk.i32],
e: hk.Tensor[hk.i32], o: hk.Tensor[hk.fp32]):
row = hk.arch.block_idx.x
v = x.at[row, 0].load(S)
k = n.at[row, 0].load(I) # an i32 strip
# the biased exponent of v
e.at[row, 0].store(hk.map(lambda x: (x.bitcast(hk.i32) >> 23) & 0xFF, v))
# v * 2^k where k is in range, else v
o.at[row, 0].store(hk.where((k >= -8) & (k <= 8),
hk.map(lambda x, k: x * hk.math.exp2(k.to(hk.fp32)), v, k), v))
tests/hk/gpu/test_map.py checks the semantics against torch.
The escape hatch: hk.map_cpp(expr, *args, dtype=None) evaluates one
C++ expression for each element, for what hk.map cannot say: an intrinsic
or instruction hk has no op for (__builtin_amdgcn_*), or another kernel's
exact instruction sequence. Triton's inline_asm_elementwise is the same
escape; it is hk.map with a string body.
- Arguments. x0, x1, ... in expr are args in order. A strip binds
its element (r, c); an hk.Int or a Python int binds its value. Each has its
C++ type: float for an fp32 strip, int for an i32 strip, an hk.Int or a
Python int. A Python float is refused: write it in the expression (0.5f).
At least one argument is a strip, and every strip has one shape and run.
- Types. Strip arguments and the result are fp32 or i32. The result is the
expression's value cast to dtype, which defaults to the strips' one dtype;
strips of fp32 and i32 together must say dtype=.
- The expression is one C++ expression, with no ; and no braces
(ValueError), compiled as written with contraction off: hk does not parse
it, so a mistake in it is a hipcc error. It is never fused with a map.
- Where. Where hk.map is; its use in hk.ops is on a reviewed allowlist
(scripts/escape_hatches_allow.txt).
Strip ops for a megakernel's roles (gfx950)¶
What a persistent kernel's roles do between its GEMMs -- index arithmetic,
table lookups, small reductions inside a lane group, hand-offs element by
element -- written on strips. All gfx950, and written for a kernel that must
match a hand-written one bit for bit.
- Indices. hk.lane_coords(ty) gives two i32 strips of a strip type's
shape, element (r, c) holding r and c; hk.iota(ty) is the second. Of a
register tile, hk.lane_coords(tile, run) gives each element of
tile.lane_strip(run) its row and column in the tile. With the i32 strip
operators they compute flat indices.
- Elements at computed indices. x.load_elems(idx) /
x.store_elems(val, idx, runs=False) gather and scatter element by element
(flat indices, an i32 strip); an index below zero or past the tensor reads 0
and writes nothing, so -1 masks an element. runs=True stores a lane's run
as one store from its first element's index (eight fp8 values in one 8-byte
store). On an fp32 LDS tile, st.load_elems / st.store_elems are ds
operations; another warp's writes need hk.arch.barrier(drain=True) first.
- Lane groups. t.group_sum(cols) / group_max / group_min reduce over
groups of cols columns (an MX group is 32 columns of a run-8 strip), the
result in every element, in a stated order (lanes=, xor=, as
sum(axis=1, ...)). t.lane_gather(lanes) takes each element from another
lane (ds_bpermute).
- One MFMA as written. hk.mfma_strip("f32_16x16x4", a, b, acc) is
v_mfma_f32_16x16x4_f32 on strip registers, nothing reordered, for an
MFMA another kernel's result depends on.
- Mailbox elements. On a scoped int32 tensor, x.post_elems(val, idx,
tag) / x.take_elems(idx, tag) are hk.comm.post / take element by
element: value and tag in one 8-byte scoped store, and a poll until every
element's tag arrived (a timeout traps). runs=True and bf16=True pack a
lane's run.
Every name is in the API reference; tests/hk/gpu/test_strip.py
and test_elems.py exercise them.
Dtypes, and two deliberate spellings¶
The dtypes are hk.bf16, hk.fp16, hk.fp32, hk.i8 and hk.i32, plus
hk.fp8e4m3 and hk.fp8e5m2, which are accepted only where the target has an
fp8 WMMA — gfx1201, not gfx1100 (supported hardware).
Two spellings are deliberate, both kept on purpose when the language moved to
MLIR:
- a vector type is named by the tile it reduces, hk.col_vec(T), not
hk.ColVec[fp32, 16], because the dialect ties a vector's layout to its
tile;
- a load names the register tile it makes, x.at[r, c].load(T), not
x.tile(16, 64)[r, c].load(fp32), because x.tile[r, c] already means a
tile-unit coordinate.
Values and operators¶
- Device values are objects with operators.
a + b,t * v(a vector broadcasts along the axis it came off;v[:, None]/v[None, :]spell that axis out and check it),t.exp(),t.to(hk.bf16),t.sum(axis=1),acc += a @ b.T(one WMMA intoacc; the variant follows from the transposes),-t,hk.min(a, b); scalars areInt/Boolwith Python arithmetic and comparisons. - Tiles are buffers. An operation either makes a new tile or writes an
existing one —
+=,t.exp_(),into=— which is how include/rdna3 spells everything and what keeps a loop's registers the same registers.
Memory¶
- Memory is indexed.
x[coord],x.at[r, c](element coordinate) andx.tile[r, c]are views;.load(type),.store(value),x[idx] = value,.load_scalar(). LDS tiles come fromhk.lds.alloc, ands[i],s.subtile(...),s.load(type, wait=False),s.granules()follow.load_scalar(invariant=True)says the kernel never writes the element (a page table, a length): on gfx950 it is a scalar-caches_load, on lgkmcnt, where a global load's wait would sit behind every copy in vmcnt's queue.
A global carries one runtime row stride, so a slice of the last axis — the
two halves of a fused (M, 2I) output, say — is accepted without a copy:
contiguous along it, rows evenly strided, outer axes compact over the rows.
Anything else is refused with a message saying why. Register loads and stores
can be bounded by rows, x[idx].load(ty, bounded=True) and
o[idx].store(t, bounded=True), which makes a tile that runs past the last
row read zeros and drop the stores past it, with no branch; a strip
is bounded by rows and columns alike (x.at[r, c].load(S, bounded=True)).
A workgroup's synchronous copy, g.load(tile, view) / g.store(view, tile),
is bounded by rows without asking on gfx1100 and on gfx950: rows past
the tensor's last read zero and are not written. gfx942's copies are still
unbounded.
LDS offsets and every lgkmcnt are derived by the passes, which also prove
that every asynchronously loaded tile is waited for before it is read: see
hk-lds-alloc and hk-lds-pipeline. An hk.lds.alloc
inside a runtime if counts toward the launch's LDS (the two arms start at
the same offset, the larger continues); one inside a runtime for or while
is refused, so allocate it before the loop.
examples/04_gemm.py stages through LDS with a
derived wait.
Copies left in flight (gfx950). g.load(tile, view, wait=False) is
gfx950's DMA, buffer_load_dwordx4 ... lds: sixteen bytes a lane straight
into LDS, counted on vmcnt and returned in flight. g.wait_for(*tiles) is the
s_waitcnt vmcnt(n) that retires this warp's share of the copies into those
tiles, with n derived from the order the copies were issued, as lgkmcnt is
for LDS reads. The other warps' shares are visible after a barrier:
g.load(As[1], a.at[m0, k1], wait=False) # next tile, in flight
... # math on As[0]
g.wait_for(As[1]) # vmcnt, derived
hk.arch.barrier() # every warp's share has landed
x = As[1].subtile(64, 64, wr, 0).load(T, wait=False)
g.load(..., nontemporal=True) copies with sc0 nt, for data read once, and
hk.lds.alloc(..., shape="16x64" | "32x32") lays an allocation out as that
include/cdna4 st_shape rather than the one its reads' fragment picks (16x32
row operands, and 32x16 stride-4 col operands, read out of those too): a copy
then reads each row in 128- or 64-byte runs instead of 64 or 32, and its
lines once. Use the two together: non-temporal copies in the shorter runs
fetch each line twice.
The tile is a whole allocation or a buffer of a stack picked by a Python int,
so hk-lds-pipeline knows which buffer each copy writes and checks the two
hazards a copy has across warps: a read of the buffer before every warp's
share has landed, and a copy into it before every warp's reads of it have.
Both are a count of barriers between the two events. In exchange, a read of
such a buffer may stay in flight across a barrier, which the LDS rules refuse
for a buffer picked at run time.
Two halves a barrier apart. with hk.arch.pingpong(behind): makes the
warps where behind holds wait at one extra s_barrier on entry, and the
others at one on exit. Between the two, every barrier pairs a program point in
one half with the next in the other: a schedule that alternates memory and
math between barriers runs the halves out of phase, so with eight warps the
two waves on a SIMD take turns at the matrix core. behind comes from
warp_id(), both ends are in the kernel's own body, and inside, the pass
counts two barriers where one would do outside. gfx950's GEMM
(hk.ops.gemm_cdna) is built from these.
An accumulator read as an operand (gfx950)¶
Attention's P comes out of an MFMA as an accumulator and P·V wants it as an operand. On gfx950 that needs no data movement if the scores are kept transposed (Sᵀ = K·Qᵀ, Oᵀ += Vᵀ·Pᵀ) and P is read in place:
T_S = hk.RT(hk.fp32, 64, 32, "col", frag=(32, 32)) # Sᵀ, an accumulator
T_P = hk.RT(hk.bf16, 64, 32, "col", frag=(16, 32, 4)) # Pᵀ, an operand
S = hk.zeros(T_S)
S += k_tile @ q_tile.T
S.subtile(32, 1).exp2_() # rows 32-63: the same registers
grow = m_new.any_above(m, 8.0) # one int a wave: did any lane's max jump
S.as_operand(hk.bf16, into=P) # converts, no register moves
hk.arch.pin(P, m) # P and m stay in this phase
O += v_tile.T @ P # V read with P's lane run
Tile.as_operand(dtype, into=)reads a col-layout accumulator (32x32 or 16x16) as the stride-4 operand fragment of the same rows, converting fp32 to bf16 or fp16 on the way. The reduction index then runs down the rows in the accumulator's order;hk-fragmentsgives the MMA's other operand the same stride, so the product is unchanged.Tile.as_operand(hk.fp16, k_order="even_odd")is RDNA3's form (gfx1100): a 32x16 fp32 col-layout pair of 16x16 WMMA accumulators (Sᵀ's kv rows 0-15 and 16-31) as two fp16 operands,subtile(16, 0)the one whose K axis is the pair's even rows andsubtile(16, 1)the odd ones. V's rows are laid out in that order (hk.ops.attn_k3's Vᵀ kernel does), and each packed dword costs one conversion, onepermlanex16and two selects.Tile.subtile(rows, index)is rows[index*rows, (index+1)*rows)of a register tile as a tile: an alias, so a write into it writes the tile.indexis a Python int.Vec.any_above(other, threshold)is 1 in every lane if any lane's entry exceedsother's by more thanthreshold: a wave vote, usable as a branch condition or a factor.hk.arch.pin(*values)holds register values to the program point: their definitions stay above it and their uses below. LLVM sinks a value to its use; in a schedule of phases a barrier apart that moves work into the other phase, whichsched_barrier(a fence for the scheduler only) does not stop.
hk.ops.attn_cdna is built from these.
Rows by index, atomics, random numbers¶
What a mixture-of-experts GEMM, an embedding, a KV cache write, a split-K reduction and a dropout need, and a tile load cannot say:
t = table.gather(T, ids, at, col=c) # tile row r <- table row ids[at + r]
cache.scatter(t, slots, at, col=c) # tile row r -> cache row slots[at + r]
out.at[r, c].atomic_add(acc) # an fp32 tile, element by element
old = cnt.at[0, e].atomic_add_scalar(1) # one int32 add per warp (also _max_)
u = hk.rand(T32, seed, offset, row_stride) # what triton.language.rand draws
keep = (u - p).step() # 1 where u > p, else 0
ids is an int32 global. Gather and scatter move row-layout tiles, a lane's
row being its own; an index outside the tensor -- -1 for a CUDA graph's
padding, numel for moe_align_block_size's -- or a position at or past
count= reads zeros or writes nothing, with no branch, because the row is a
buffer offset and an out-of-range one points past the resource. Two tile rows
scattered to one row race.
atomic_add takes fp32 tiles only: a 16-bit row tile is replicated across the
wave halves, and adding it would count every element twice. The scalar
atomics are issued by lane 0 and return the old value to every lane; a
workgroup of W warps that calls one performs W of them.
hk.rand(ty, seed, offset, row_stride) fills an fp32 tile: element (r, c) is
Philox4x32-10 at offset + r * row_stride + c, bit for bit Triton's draw for
that seed and offset, so a kernel can be checked against its Triton original
exactly. The seed may be a device scalar (x.at[0].load_scalar()): a
non-tensor argument is a constexpr, built in, and a seed that changes every
step would compile every step. step() turns a comparison into a factor;
examples/07_dropout.py is a dropout with it.
Control flow¶
- Control flow is Python. The body is rewritten at the AST level, as CuTe
DSL does: a
forover a device bound, anifon a device condition and awhileon a device test becomescf.for/scf.if/scf.while. A scalar the loop or branch rebinds is carried through it (iter_args, branch results); rebinding a tile inside a runtime loop is refused, with the in-place spelling in the message.hk.range(n, unroll=4)asks for a runtime loop with a pragma,hk.range_constexprinsists on unrolling, andhk.scope()/hk.if_(c, opaque=True)keep their register-allocator meaning.
A branch that holds a workgroup operation (a barrier, a group copy) must be
taken by every warp or none. if hk.uniform(cond): (or
hk.if_(cond, uniform=True)) declares cond the same in every warp -- made of
the block index, scalar loads every warp makes and constexprs, never of
warp_id or a lane's value -- and such a branch may hold them: a persistent
kernel's role dispatch (gfx950). The declaration is the author's: a
condition that differs between warps deadlocks at the first barrier inside. A
divergent branch around it is still refused.
A for over a Python bound — a constexpr, range(D // 16) — is unrolled at
trace time, as in scale_pow2 above. examples/06_control_flow.py
shows for, if and while on device values, carried scalars, and the
scf and C++ they become. What control flow cannot do is under
limits.
Between workgroups, XCDs and GPUs: hk.comm (gfx950)¶
A persistent kernel, a task queue, a hand-off of a tile from one XCD or GPU
to another: on gfx950 a plain store stays in the writer's XCD's L2, and a
plain re-read on the reader's side can be stale even after the flag it
waited for arrived. hk.comm lowers each hand-off to what a memory-model
litmus suite found correct (the design and the measurements):
- The scope belongs to the tensor. hk.Tensor[dtype, "device"] (between
the XCDs of one GPU) or hk.Tensor[dtype, "system"] (between GPUs): every
load, store and scalar atomic through it carries the scope's cache-policy
bits, since both sides of a hand-off need them.
- Signals. hk.comm.signal(view, value) drains, barriers and stores one
scoped flag; hk.comm.wait(view, value, timeout_s=, error=) polls it, and
a timeout sets the error element (or traps) instead of hanging.
hk.comm.arrive / hk.comm.barrier are an epoch'd grid barrier,
hk.comm.xcd_barrier one with one global atomic per XCD a round.
- Work queues. hk.comm.claim(view) takes the next task (one scoped
atomic add, no drain); every scalar fetch-op (atomic_min_scalar,
_or_, _and_, _xor_, _exch_ beside _add_ and _max_) works
plain or scoped.
- Mailboxes. hk.comm.post(view, value, tag) / hk.comm.take(view, tag)
send one int32 and its tag in one 8-byte scoped store. A mailbox orders
nothing else.
- Residency. A grid that waits on its own workgroups must all be on the
GPU at once: hk.kernel(..., resident=True) refuses a launch that cannot
be, with ResourceError, instead of deadlocking
(Kernel.resident_capacity()).
- Between GPUs. hk.runtime.SymmetricBuffer is one buffer per rank
with every peer's mapped; while one is open, a launch refuses it bound to
an unscoped parameter.
@hk.kernel(warps=1, grid=lambda p: (64, 1, 1), resident=True)
def drain(q: hk.Tensor[hk.i32, "device"], done: hk.Tensor[hk.i32], *, N: int = 1):
t = hk.comm.claim(q.at[0]) # the next task, once each
while t < N:
done.at[t].atomic_add_scalar(1)
t = hk.comm.claim(q.at[0])
Every name is in the API reference;
examples/14_task_graph.py is a persistent
task graph with dependencies, and hk.arch.xcd_id() says which XCD ran each
task. What hk.comm still lacks against the NVIDIA megakernel designs is its
roadmap.
Which XCD runs a workgroup: placement¶
A gfx950 deals workgroup w to the XCD of w % 8, and each XCD has its own
4 MB L2. Workgroups that read the same bytes -- attention's K and V of one KV
head, a GEMM's panels -- fetch them once per XCD that reads them, and
numbered the wrong way each L2 holds many unrelated streams at once.
hk.kernel(placement=...) renumbers the grid at kernel entry; what each
workgroup computes is unchanged, so outputs are bitwise the same:
- placement=hk.Placement.cluster(): one sharing cluster an XCD at a time,
the key found by the analysis (hk.placement.analyze), or named with
key=(hk.Digit.of("z"), hk.Digit.of("y")) where it is read from memory;
- hk.Placement.chunked(chunk, tail): block_idx.x alone, in rounds of
chunk consecutive ids an XCD (a GEMM's split-K y untouched);
- placement="auto": the cluster placement where the policy
(hk.placement.choose) takes it -- not for order-dependent or resident
kernels, from five KV streams an XCD, not for non-uniform work under twelve
rounds an XCD. k.placement_choice(*args) says what it decided and why.
On a part with one L2 (gfx1100, hk.arch.xcd_count() == 1) every placement
is the identity. python -m hk.check FILE.py:NAME --placement --inputs ...
checks that a launch covers its grid exactly once
(check). The design and
its measurements are in workgroup placement;
examples/15_placement.py shows it on a kernel
of your own.
Namespaces¶
- The rest has namespaces:
hk.arch: block, warp and lane ids,xcd_id()andxcd_count()(gfx950's XCDs),target()(the target being traced for, for a body whose tiling depends on it),barrier(drain=False),pingpong(behind),sched_barrier,sched_group_barrierandSCHED_CLASSES,setprio,pin,vm_wait,wg_arrive;hk.lds:alloc,shared_vecandalloc_vec,reserve,wait_for;hk.math: elementwise functions --exp,exp2,log,log2,abs,relu,gelu(the tanh form),dgelu,sqrt,rsqrt,silu, andadd/sub/mul/div/maximum/minimumwithinto=. The transcendental ones are fp32 only, and there is notanh,sigmoidorerf: compose them. Each one's signature and dtypes are in the catalogue;hk.group(n): workgroup loads (wait=Falseon gfx950) and theirwait_for, stores, and the staging pipeline;hk.collective:cross_warp,fold_rows;hk.comm(gfx950): scoped hand-offs between workgroups, XCDs and GPUs (above).
Scheduling hints. hk.arch.sched_group_barrier(classes, size,
sync_id=0) asks the compiler's scheduler for size instructions of
classes ("mma", "vmem_read", "ds_read", "valu", ... or a mask; the
names are hk.arch.SCHED_CLASSES) at this point, after the earlier groups
with the same sync_id. A run of them is an interleave: "one staging load,
then two WMMAs". It moves only what the scheduler can see, and an inline-asm
instruction is in no class. include/rdna3's LDS fragment reads and writes are
asm, so in hk's GEMM it can place the staging loads and the WMMAs, not the
LDS traffic. s_setprio ends a scheduling region, so a group does not reach
across one. hk.arch.sched_barrier(allow) is a fence that lets the classes
in allow cross.
Occupancy caps. hk.lds.reserve(nbytes) allocates LDS the kernel never
uses, to cap how many workgroups share a WGP: on gfx1100 a WGP holds
128 KB ÷ (the kernel's LDS + nbytes) workgroups.
hk.arch.wg_arrive(counter, expected) counts this workgroup's arrival at
counter (an element view of an int32 global) and returns how many arrived
before it; the last to arrive resets the counter, and it contains barriers, so
every warp must reach it (python/hk/dsl/arch.py). It is a counter rather
than fp32 atomics so the combine is deterministic: the partial sums are added
in one fixed order.
@hk.func¶
@hk.func marks a helper that is traced inline, with its control flow
rewritten too. Calling one outside a kernel that is being traced raises.
Errors and locations¶
A device value used where Python needs a number — a tile extent, a list index,
range() outside a traced function — raises at that line, saying which of
those it was.
The passes' errors come out of k.trace() as hk.VerifyError, ending
at file:line in function — the kernel author's line
(passes).
An op that the library does not implement for the requested dtype is the same
kind of late failure, so it is caught the same way: gelu on a bf16 tile
raises while tracing rather than producing undefined hidden symbol at the end
of a 30-second compile.
Limits¶
What the language does not do. Each of these raises, with the spelling that works:
- A loop or branch body that
returns,breaks orcontinues is left as Python, so its condition has to be known at trace time.while ... elseis not rewritten. - A workgroup operation (
barrier, group loads and stores, staging) cannot sit inside a runtime branch or a runtimewhile: a warp that leaves early leaves the rest waiting. Aforover a bound every warp agrees on is fine. - A tile rebound inside a runtime loop is refused; it is updated in place
(
+=,into=), so that every iteration writes the same registers. - A kernel body must be source
inspectcan read. Code built withexec, typed intopython -c, written as a lambda, or loaded from a stale__pycache__compiled at another path traces, but itsfor,ifandwhileare not rewritten, so its loops cannot be runtime loops. An error about control flow on a device value then says that the source could not be read, and why. - Pass errors name values by their base name (
lds), not the C++'s deduplicated one (lds1), so "add lds to that lds_wait_for" can be ambiguous with several LDS tiles in flight. The old front end had the same limit. - Nothing is eliminated before the passes, so an unused allocation still
counts in
lds_bytes. The old front end behaved the same way. - MLIR is built from source (23.1.3). Wheels are Linux x86_64 only, one CPython each.
- gfx942 is compile-only, and has no register strips (so no templates).
gfx1201 runs the GPU tier on an R9700 with gfx1100's plans and schedules,
none of them tuned there yet. On gfx950 strips build, and its GEMM and attention are
kernels of hk's own (
hk.ops.gemm_cdna,hk.ops.attn_cdna), with HipKittens' CDNA4 kernels also wrapped as hk ops (CDNA3/4).