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

+22
View File
@@ -46,6 +46,11 @@
#include "r3d_launch.h"
#include "r3d_args.h"
/* The wave-matrix arm's launcher, in r3d_gemm_inta8_wmma.hip: the row's `impl` point 1. */
extern "C" int r3d_gemm_nt_q_w8a8_m16_wmma(long a, long a_scale, long b, long b_scale, long y,
int M, int N, int K, int a_ld, int y_ld,
long long b_rb, long long sb_rb, int nt, long stream);
namespace {
constexpr int kWave = 32;
@@ -412,6 +417,23 @@ extern "C" int r3d_rad_gemm_nt_q_w8a8_m16(const RadArgs* a, RadStream s)
if (yp < N) return R3D_NO(RAD_E_STRIDE);
if (!r3d_aligned(r3d_p(A), 4) || !r3d_aligned(r3d_p(B), 16)) return R3D_NO(RAD_E_ALIGN);
R3dGemmCfg c;
r3d_cfg_gemm(a, &c);
if (c.impl == 1) {
/* The wave-matrix arm: same contract, the i8 tile's, its own launcher. */
const int rc = r3d_gemm_nt_q_w8a8_m16_wmma(r3d_addr(A), r3d_addr(AS), r3d_addr(B),
r3d_addr(BS), r3d_addr(Y), (int)M, (int)N,
(int)K, (int)ap, (int)yp,
(long long)r3d_row_pitch(B),
(long long)r3d_row_pitch(BS), c.nt, (long)s);
if (rc == -1) return R3D_NO(RAD_E_INVAL);
if (rc == -2) return R3D_NO(RAD_E_SHAPE);
if (rc == -3) return R3D_NO(RAD_E_STRIDE);
if (rc == -5) return R3D_NO(RAD_E_ALIGN);
if (rc == -4) return R3D_NO(RAD_E_DEVICE);
return RAD_OK;
}
const int rc = r3d_gemm_nt_q_w8a8_m16(r3d_addr(A), r3d_addr(AS), r3d_addr(B), r3d_addr(BS),
r3d_addr(Y), (int)M, (int)N, (int)K, (int)ap, (int)yp,
(long long)r3d_row_pitch(B), (long long)r3d_row_pitch(BS),