libr3d: the RDNA3 (gfx11) wave-matrix kernel plugin

A self-contained HIP kernel plugin for gfx11 (RDNA3) that exposes the
libref-declared ops as device rows the engine can tune and dispatch:

  * GEMMs (bf16, f16, int8-activation, vocab) retiled to 16x128 on the
    wave-matrix (WMMA) builtins. The retile removes the 8x wave
    redundancy of the naive tile and stages the activation once per
    16x128 block; the 16-row and 64-row forms share the stage. The k-walk
    double-buffers the 128-deep tile (the next tile stages while the
    current one multiplies), on the m64 and tiled forms.
  * Quantised GEMMs: fp8-e4m3 x e4m3 (tiled and ALU), mxfp4 x a8, w2a8,
    w4a16, w5a16, w8a8 (the prefill heights 17..127 on the wave matrix),
    and the gated (swiglu) form. E4M3 decodes exactly to f16 through a
    256-entry LDS table; the WMMA fragments bit-cast the staged halfs
    (a uint16-to-_Float16 assignment is a value conversion, not a
    reinterpret).
  * Half-precision-accumulate arms (acc = 1) on the m64 wave matrix:
    the f16/bf16 16x16x16 builtins with the compile-time opsel half
    select.
  * Grouped MoE GEMMs (bf16, e4m3 with F32 block scales, mxfp4a8, w4a8),
    retiled the same way, over the run-walk / gather structure.
  * Normalisation (layernorm / rmsnorm) and the quantisation ops.
  * GDN linear-attention, the QSA/KV state ops, and the MoE
    router/scatter/gather family (router_topk_scatter, router_sigmoid_topk
    with the correction bias, moe_gather, moe_gather_inv; the top-k
    router runs to n_expert 512).

Every row implements an op libref declares; the selector (tune) picks
the implementation and the NOTUNE rows carry the untuned default.
Selftest-clean (121 rows, schema + finiteness); values verified against
CPU references per op.
This commit is contained in:
Asim Siddiqui authored and StillDeadcode committed 2026-11-01 20:56:25 +01:00
1 parent 203064b487
commit 34c52ebe96
54 files changed
+12621 -862

No files matched your search

+6 -3
View File
@@ -537,8 +537,9 @@ __device__ __forceinline__ long long slot_k_off(const Args& A, int blk, int kh,
* the table row by the columns the walk can name, ceil(jcap / block_size) with
* jcap = min(ctx, bt_cols * block_size), which the shim proved the table
* covers; the row is `M.s * bt_rs` with `M.s` a clamped sequence index.
* the cache by block ids the table names; the shim requires every index the walk can
* reach to be inside the cache plane (`0 <= id < num_blocks`, dense strides).
* the cache by block ids the table names; the shim proves the table's strides and that
* its rows cover the walk, but the ids themselves are device data -- 0 <= id <
* num_blocks is the caller's contract, shared with the reference.
* The fold reads kv-head 0's slot-0 K row at the `stage()` offset, kHeadDim
* elements (256 bytes bf16, 128 fp8), inside a slot's 2*kHeadDim. A negative
* id (an unmapped block, printed -1) folds to 0 and is not dereferenced. */
@@ -1499,7 +1500,9 @@ int attn_shim(const RadArgs* a, RadStream s, bool decode)
const int block_size = (int)r3d_geti_or(a, "block_size", r3d_dim(KV, 2));
if (block_size != kBlock) return R3D_NO(RAD_E_SHAPE);
if (r3d_dim(KV, 1) != kv_heads) return R3D_NO(RAD_E_SHAPE);
if (r3d_dim(KV, 3) < 2 * kHeadDim) return R3D_NO(RAD_E_SHAPE);
/* The slot stride is hardcoded to 2 * kHeadDim in fill() (K then V in one slot), so a
* wider-last-dim cache would be read at the wrong stride: exact, not a lower bound. */
if (r3d_dim(KV, 3) != 2 * kHeadDim) return R3D_NO(RAD_E_SHAPE);
const long long nb = r3d_dim(KV, 0);
const int max_ctx = (int)(nb * block_size);
const int max_blocks = (int)((max_ctx + block_size - 1) / block_size);