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

+108
View File
@@ -346,6 +346,114 @@ int r3d_shape_quant_act_i8(const RadParam* p, int n_p, int operand, RadOpdDesc*
}
/* The per-group int8 the i8a8 GEMM reads: the fp8 quantiser's extents with int8 codes. */
/* The Hadamard-rotated int8: the same three planes as quant_act_i8 (x, q, scale), no row sums. */
int r3d_shape_had_quant_act_i8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const RadOpdDesc list[] = { opd(D, M, n), opd((uint32_t)RAD_I8, M, n),
opd((uint32_t)RAD_F32, M, 1) };
R3D_PICK(list);
}
/* The GELU-gated product quantiser: gate_up [M, 2n], q [M, n] i8, scale [M], out_bf16 [M, n]. */
int r3d_shape_gelu_mul_quant_i8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const RadOpdDesc list[] = { opd(D, M, 2 * n), opd((uint32_t)RAD_I8, M, n),
opd((uint32_t)RAD_F32, M, 1), opd(D, M, n) };
R3D_PICK(list);
}
/* The prefill-overlap length trim: seqused [max_seqs], tail [trim] (extent carries the count),
* out [max_seqs]. */
int r3d_shape_kv_len_trim(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t n = gi(p, n_p, "max_seqs", 0), trim = gi(p, n_p, "trim", 0);
const RadOpdDesc list[] = { opd((uint32_t)RAD_I32, n, 1), opd((uint32_t)RAD_I32, trim, 1),
opd((uint32_t)RAD_I32, n, 1) };
R3D_PICK(list);
}
/* The QSA tail: k [M, n], pos [M], cu [seqs+1], state [seqs, 1, ring, n] (inout), sidx [seqs]. */
int r3d_shape_qsa_tail_store(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const int64_t ring = gi(p, n_p, "ring", 0), seqs = gi(p, n_p, "seqs", 0);
const RadOpdDesc list[] = { opd((uint32_t)RAD_BF16, M, n), opd((uint32_t)RAD_I32, M, 1),
opd((uint32_t)RAD_I32, seqs + 1, 1),
opd((uint32_t)RAD_BF16, seqs, 1, ring, n),
opd((uint32_t)RAD_I32, seqs, 1) };
R3D_PICK(list);
}
/* The gated Hadamard int8: a [M, n], b [M, n] (optional, fused gate_up when absent), q [M, n] i8,
* scale [M]. */
int r3d_shape_gated_had_quant_i8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const RadOpdDesc list[] = { opd(D, M, n), opd(D, M, n), opd((uint32_t)RAD_I8, M, n),
opd((uint32_t)RAD_F32, M, 1) };
R3D_PICK(list);
}
/* The rotated fp8: x [M, n], q [M, n] E4M3, scale [M, n/group]. */
int r3d_shape_had_quant_act_fp8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
const RadOpdDesc list[] = { opd(D, M, n), opd((uint32_t)RAD_F8E4M3, M, n),
opd((uint32_t)RAD_F32, M, ng) };
R3D_PICK(list);
}
/* The gated rotated fp8: gate_up [M, 2n], q [M, n] E4M3, scale [M, n/group], out_bf16 [M, n]. */
int r3d_shape_gated_had_quant_fp8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
const RadOpdDesc list[] = { opd(D, M, 2 * n), opd((uint32_t)RAD_F8E4M3, M, n),
opd((uint32_t)RAD_F32, M, ng), opd(D, M, n) };
R3D_PICK(list);
}
/* The column fp8: x [M, n], y [M, n] E4M3, scale [n] (one per column). */
int r3d_shape_quant_cols_fp8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const RadOpdDesc list[] = { opd(D, M, n), opd((uint32_t)RAD_F8E4M3, M, n),
opd((uint32_t)RAD_F32, n, 1) };
R3D_PICK(list);
}
/* The rotated grouped int8: x [M, n], q [M, n] i8 (row-major), scale [M, n/group]. */
int r3d_shape_had_quant_act_i8g(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
const RadOpdDesc list[] = { opd(D, M, n), opd((uint32_t)RAD_I8, M, n),
opd((uint32_t)RAD_F32, M, ng) };
R3D_PICK(list);
}
/* The gated rotated grouped int8: gate_up [M, 2n], q [M, n] i8 (row-major), scale [M, n/group]. */
int r3d_shape_gated_had_quant_i8g(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
const RadOpdDesc list[] = { opd(D, M, 2 * n), opd((uint32_t)RAD_I8, M, n),
opd((uint32_t)RAD_F32, M, ng), opd(D, M, n) };
R3D_PICK(list);
}
/* The RMS norm + per-row int8: x [M, n], residual [M, n] (optional), w [n], q [M, n] i8,
* scale [M], out_bf16 [M, n] (optional), w_post [n] (optional), scalar [1] (optional). */
int r3d_shape_rmsnorm_quant_i8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
const RadOpdDesc list[] = { opd(D, M, n), opd(D, M, n), opd(D, n, 1), opd((uint32_t)RAD_I8, M, n),
opd((uint32_t)RAD_F32, M, 1), opd(D, M, n), opd(D, n, 1),
opd(D, 1, 1) };
R3D_PICK(list);
}
int r3d_shape_quant_act_i8g(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);