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

+30
View File
@@ -220,6 +220,36 @@ __device__ __forceinline__ float block_sum(float v, float* lds) {
__device__ __forceinline__ void sched_barrier() { __builtin_amdgcn_sched_barrier(0); }
__device__ __forceinline__ void group_barrier() { __syncthreads(); }
/* --- WMMA fragment types and wrappers (shared by every wave-matrix GEMM). ---
*
* The fragment types: v16half (f16 codes), v16short (bf16 codes, the truncated-f32 bit pattern),
* v4int (int8 codes), and the v8float / v8int accumulators. The wrappers are thin around the gfx11
* wave-matrix builtins; w32 is the only width gfx11 exposes. Shared so a kernel never re-defines
* the fragment types or re-wraps the builtin. */
using v16half = _Float16 [[clang::ext_vector_type(16)]];
using v16short = short [[clang::ext_vector_type(16)]];
using v8float = float [[clang::ext_vector_type(8)]];
using v4int = int [[clang::ext_vector_type(4)]];
using v8int = int [[clang::ext_vector_type(8)]];
__device__ __forceinline__ v8float wmma_f16(const v16half& a, const v16half& b, const v8float& c)
{ return __builtin_amdgcn_wmma_f32_16x16x16_f16_w32(a, b, c); }
__device__ __forceinline__ v8float wmma_bf16(const v16short& a, const v16short& b, const v8float& c)
{ return __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32(a, b, c); }
__device__ __forceinline__ v8int wmma_i8(const v4int& a, const v4int& b, const v8int& c)
{ return __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32(true, a, true, b, c, 0); }
/* The half-precision-accumulate builtins. The accumulator is a 16-element vector (8 VGPRs x 2);
* `opsel` (a compile-time constant) selects which half holds the 8 live elements (false = the
* even indices 0,2,..,14, true = the odd). The non-selected half is untouched, so an epilogue
* that reads all 16 sees stale values in the dead half -- read only the selected half. */
template <bool OSEL>
__device__ __forceinline__ v16half wmma_f16acc(const v16half& a, const v16half& b, const v16half& c)
{ return __builtin_amdgcn_wmma_f16_16x16x16_f16_w32(a, b, c, OSEL); }
template <bool OSEL>
__device__ __forceinline__ v16short wmma_bf16acc(const v16short& a, const v16short& b, const v16short& c)
{ return __builtin_amdgcn_wmma_bf16_16x16x16_bf16_w32(a, b, c, OSEL); }
/* ================================================================== the geometry constants
*
* A gfx11 GPU's wave slots: a CU has 4 SIMDs and a SIMD holds 16 wave32s, so 64 slots a CU. HIP's