This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libr3d/r3d_qsa_tail_store.hip
Asim Siddiqui 34c52ebe96 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.
2026-11-01 20:56:25 +01:00

94 lines
4.1 KiB
Plaintext

/* r3d_qsa_tail_store.hip -- qsa_tail_store: the QSA indexer's tail, raw index keys of tokens that
* have not yet completed a block, into a per-layer linear KV state.
*
* The tail is a per-layer linear KV state owned by the sequence (allocated zeroed, kept for the
* sequence's life), not a plane indexed by the step-local batch row. `state` is one layer's cache,
* [slots, 1, ring, n]; `sidx` names the sequence's slot; position p lands at row p % ring.
*
* The ring is ratio + n_spec, not ratio: the next step restarts at the first rejected position, so
* a step may still owe positions back to p0 - ratio + 1 while writing up to p0 + n_spec. A token's
* sequence comes from cu_seqlens (a linear scan suffices for a handful of sequences). Only the last
* `ring` tokens of a sequence's chunk write: positions p and p + ring share a row, so writing all
* of a prefill chunk would race.
*
* k [T][n] bf16, pos [T] i32, cu [seqs+1] i32, state [slots,1,ring,n] bf16 (inout), sidx [seqs]
*/
#include <hip/hip_runtime.h>
#include <cstdint>
#include "r3d.h"
#include "r3d_args.h"
#include "r3d_launch.h"
namespace {
constexpr int kThreads = 256;
__global__ __launch_bounds__(kThreads) void qsa_tail_store_kernel(
const uint16_t* __restrict__ k, const int32_t* __restrict__ pos,
const int32_t* __restrict__ cu, const int32_t* __restrict__ sidx,
uint16_t* __restrict__ state,
int T, int n, int ring, int seqs, int k_ld, int s_slot, int s_row, int sidx_ld) {
const int t = (int)blockIdx.x;
if (t >= T) return;
// This token's sequence, by a linear scan in every thread (cheaper than LDS plus a barrier).
int s = -1;
for (int i = 0; i < seqs; ++i)
if (t >= cu[i] && t < cu[i + 1]) { s = i; break; }
if (s < 0) return;
// Only the last `ring` tokens of this sequence's chunk.
if (t < cu[s + 1] - ring) return;
// Column 0 is the committed slot; -1 marks a sequence with no state.
const int si = sidx[(size_t)s * sidx_ld];
if (si < 0) return;
const int row = pos[t] % ring;
const size_t src = (size_t)t * k_ld;
const size_t dst = (size_t)si * s_slot + (size_t)row * s_row;
for (int d = (int)threadIdx.x; d < n; d += kThreads) state[dst + d] = k[src + d];
}
} // namespace
extern "C" {
int r3d_rad_qsa_tail_store(const RadArgs* a, RadStream s)
{
if (!a || a->n_t < 5) return R3D_NO(RAD_E_INVAL);
const RadTensor* k = r3d_opt(a, 0), *po = r3d_opt(a, 1), *cu = r3d_opt(a, 2),
*st = r3d_opt(a, 3), *sx = r3d_opt(a, 4);
if (!r3d_p(k) || !r3d_p(po) || !r3d_p(cu) || !r3d_wp(st) || !r3d_p(sx))
return R3D_NO(RAD_E_INVAL);
if (k->rank != 2) return R3D_NO(RAD_E_SHAPE);
// The state arrives as the group declared it: [slots, heads, ring, n], heads 1.
if (st->rank != 4 || st->shape[1] != 1) return R3D_NO(RAD_E_SHAPE);
if (st->stride[3] != 1) return R3D_NO(RAD_E_STRIDE);
if (k->dtype != RAD_BF16 || st->dtype != RAD_BF16) return R3D_NO(RAD_E_DTYPE);
const long long n = r3d_geti_or(a, "n", 0);
const long long ratio = r3d_geti_or(a, "ratio", 0);
const long long ring = r3d_geti_or(a, "ring", 0);
if (n <= 0 || ratio <= 0 || ring < ratio) return R3D_NO(RAD_E_INVAL);
const long long T = k->shape[0];
if (T < 1 || k->shape[1] != n) return R3D_NO(RAD_E_SHAPE);
if (st->shape[2] < ring || st->shape[3] != n) return R3D_NO(RAD_E_SHAPE);
const long long seqs = r3d_numel(cu) - 1;
if (seqs < 1 || r3d_numel(po) < T) return R3D_NO(RAD_E_SHAPE);
const long long sidx_ld = sx->rank >= 2 ? sx->shape[1] : 1;
if (sidx_ld < 1 || (sx->rank >= 1 && sx->shape[0] < seqs)) return R3D_NO(RAD_E_SHAPE);
const long long kp = k->stride[1] ? k->stride[0] / k->stride[1] : k->shape[1];
if (kp < n) return R3D_NO(RAD_E_STRIDE);
R3D_KLAUNCH(qsa_tail_store_kernel, dim3((unsigned)T), dim3(kThreads), (hipStream_t)s,
(const uint16_t*)r3d_p(k), (const int32_t*)r3d_p(po), (const int32_t*)r3d_p(cu),
(const int32_t*)r3d_p(sx), (uint16_t*)r3d_wp(st), (int)T, (int)n, (int)ring,
(int)seqs, (int)kp, (int)st->stride[0], (int)st->stride[2], (int)sidx_ld);
return (int)hipGetLastError();
}
} // extern "C"