radiance 1.7.0

- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves
  every shipped container
- Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime
- /v1/messages works with Claude Code as it ships (gearwave00001)
- a long agent conversation keeps its cached history when the client rewrites its last turn
  (manueloverride)
- a stream's first event goes out with the first token, so llama-benchy measures prefill right
- opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42)
- four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b)
- NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA
  adapters (baKon)
- many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool
  arguments and the CPU's vision and audio encoders
- plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
Deadcode committed 2026-10-16 18:25:18 +02:00
1 parent b068190326
commit 1e968f6e1d
739 files changed
+128447 -84632

No files matched your search

+73 -98
View File
@@ -1,57 +1,45 @@
// r4d_gemm_mxfp4a8_decode.hip - r4d_gemm_mxfp4a8_decode: the OCP-MXFP4 x fp8 GEMM for the DECODE
// band, M <= 128, gfx1201. C[M,N] = A[M,K] @ dequant(Wq)[N,K]^T over the same operands, weight
// layout and fold as r4d_gemm_mxfp4a8_nt_m64 and r4d_gemm_mxfp4a8_tiled.
// r4d_gemm_mxfp4a8_decode.hip - r4d_gemm_mxfp4a8_decode: the OCP-MXFP4 x fp8 GEMM for the decode
// band, M <= 128, gfx1201. C[M,N] = A[M,K] @ dequant(Wq)[N,K]^T with the operands, weight layout and
// fold of r4d_gemm_mxfp4a8_nt_m64 and r4d_gemm_mxfp4a8_tiled.
//
// WHY A THIRD MXFP4 KERNEL. At decode the GEMM is a weight stream: a speculative verify step is
// a handful of rows against the whole 4-bit trunk, and the only number that matters is how close
// the stream runs to the card's DRAM bandwidth. The skinny kernel splits K inside one workgroup
// and reads the weight straight into registers, which leaves the narrow projections well short of
// it. This one splits K ACROSS workgroups with a fused last-arriver reduction, unpacks the weight
// through LDS, gives each block one 16-row A fragment per M tile, and streams the weight with
// non-temporal loads.
// At decode the GEMM is a weight stream and the goal is DRAM bandwidth. The skinny kernel (split-K
// within a workgroup, weight straight to registers) falls short on narrow projections. This one
// splits K across workgroups with a fused last-arriver reduction, unpacks the weight through LDS,
// and streams it with non-temporal loads.
//
// THE TILE. A block is DWN = 8 waves over BND = 128 output columns, one 16-column fragment a wave,
// and DTM 16-row M fragments -- the SMALLEST that covers M, because a wider tile computes rows
// nobody asked for. A K slab of DBK bytes is staged: A (16*DTM rows) and the weight's 128 rows,
// unpacked to e4m3 with the block exponent folded in through r4d_mxfp4.h, as the other two MXFP4
// kernels fold it.
// Tile: DWN = 8 waves over BND = 128 output columns (one 16-column fragment per wave) and DTM
// 16-row M fragments, the fewest that cover M. Each K slab of DBK bytes stages A (16*DTM rows) and
// 128 weight rows, unpacked to e4m3 through r4d_mxfp4.h.
//
// SPLIT-K IS NOT OPTIONAL. With one 16-row M fragment the independent N fragments number N/16, so
// N=5120 is 320 waves -- 2.5 a SIMD however the N tile is chosen. DKS blocks split the K slabs;
// each writes an fp32 partial [DKS][M][N] and the LAST to arrive (an agent-scope counter per column
// block) sums them in place, so there is no second launch.
// Split-K is required: N/16 fragments alone (320 waves at N=5120) do not fill the part. DKS blocks
// split the K slabs; each writes an fp32 partial [DKS][M][N], and the last to arrive (an
// agent-scope counter per column block) sums them, with no second launch.
//
// THE SPLIT, SLAB AND LOAD HINT are a rule measured cell by cell on this kernel -- every split x
// slab x hint against the 27B's per-rank shapes at M = 8..128, DRAM-fed -- because no single
// fill target fits: the split is the smallest power of two whose grid reaches 48 blocks at M <= 16,
// 56 at M <= 96 and 40 above (an unsplit 40-block grid already streams a short-K weight at the
// card's rate there, and a split's partial traffic grows with M); a deep K (8192 or more, down at
// K=8704) keeps doubling while a split still has 24 or more 128-wide slabs, since 40-odd blocks
// do not keep its long slab loop fed; and no split goes past ks*M = 512, where the partials
// outweigh what the split buys. The slab is 128 except where LDS per block, (16*DTM + 128) *
// (DBK+8) bytes, would cost a wide or deeply split grid its resident blocks (see mxd_plan); a K
// that is not a multiple of 128 takes 64 (and a 2-way split widens to 4). Weight loads are non-temporal for weights of 4 MiB or more,
// plain below, where the weight is too small to evict anything the split partials need. All three
// are tunables (`ks`, `bk`, `nt`) whose 0 is this rule.
// Split, slab and load hint follow a rule tuned per cell on the 27B's per-rank shapes at
// M = 8..128 (tunables `ks`, `bk`, `nt`; 0 selects the rule):
// - split: the smallest power of two whose grid reaches 48 blocks at M <= 16, 56 at M <= 96 and
// 40 above; for K >= 8192 keep doubling while a split has >= 24 128-wide slabs; never past
// ks*M = 512, where partial traffic outweighs the gain.
// - slab: 128, or 64 where LDS per block, (16*DTM + 128) * (DBK+8) bytes, would cost a wide or
// deeply split grid its resident blocks (see mxd_plan). A K not a multiple of 128 takes 64 (and
// a 2-way split widens to 4).
// - non-temporal weight loads for weights of 4 MiB or more.
//
// NO STATE IS KEYED ON THE STREAM. The split partial is the op's own scratch -- the core sizes it
// from the scratch hook and treats it as read+write, so two ops that could overlap get disjoint
// slices -- and the arrival counters are a small block keyed on the OUTPUT buffer (mxd_out_ctr),
// zero at entry and left zero by the reducer. A recorded pass is replayed with its recorded
// arguments, possibly on another stream than it was recorded on, so a pointer that depended on the
// stream would make the replay differ from its recording. Two launches writing the same output are
// ordered by that dependency; two that can overlap write different outputs. The row is therefore
// concurrent (rad_kernel_concurrent), as gemm_fp8a8_nt_m16 is. A scratch shorter than the split
// needs is refused: the hook sizes it for every M of the band, so a short one is a caller's error.
// No state is keyed on the stream, since a recorded pass may replay on another stream. The split
// partial is the op's scratch (sized by the scratch hook, read+write, so overlapping ops get
// disjoint slices); the arrival counters are keyed on the output buffer (mxd_out_ctr), zero at
// entry and reset by the reducer. Launches writing the same output are ordered; overlapping ones
// write different outputs. The row is therefore concurrent (rad_kernel_concurrent), like
// gemm_fp8a8_nt_m16. A scratch shorter than the split needs is refused.
//
// Every thread fences its partial stores before the counter: a fence on thread 0 alone orders
// only thread 0's stores, and the reducer would then read whatever happened to drain.
// Every thread fences its partial stores before the counter; a fence on thread 0 alone would order
// only its own stores.
//
// TWO ACTIVATION SCALE GRIDS, as in the tiled kernel: one f32 a row, or one a (row, 128 of K). The
// per-block form carries each row's accumulator in the current block's scale and rescales it by
// s_prev / s_next at each 128 boundary, the ratio computed once a row into LDS from scales loaded a
// boundary early; with the accumulator transposed a lane reads one ratio for each of its rows. A split's partial is brought back
// to absolute units before it is written, because the splits end in different blocks.
// Two activation scale grids as in the tiled kernel: one f32 per row, or one per (row, 128 of K).
// The block form carries each row's accumulator in the current block's scale, rescaling by
// s_prev / s_next at each boundary (ratio computed once per row into LDS from scales loaded a
// boundary early). Each split's partial is converted to absolute units before it is written,
// since splits end in different blocks.
#include "r4d.h"
#include "r4d_gemm_args.h"
#include "r4d_common.h"
@@ -81,22 +69,17 @@ static __device__ __forceinline__ T mxd_ld(const T* p) {
else return *p;
}
// ---- what both kernels share: the scale grid at a 128 boundary, and the epilogue.
// ---- shared by both kernels: the scale grid at a 128 boundary, and the epilogue.
//
// THE ACCUMULATOR IS C TRANSPOSED. The WMMA takes the weight fragment as its A operand and the
// activation fragment as its B operand -- the two fragments have the same register layout, so
// this is only an argument order -- and computes C^T: a lane's accumulator for M fragment i holds
// ONE row, m = 16i + col, and the eight consecutive columns n0 + 16*wave + kb8 + e. So a row's
// activation scale is one value a lane, not eight: the (row, 128) grid's boundary rescale reads
// DTM scales instead of 8*DTM, and the epilogue writes each lane's eight outputs as one 16-byte
// store (one 32-byte store of partials) instead of eight 2-byte ones.
// The accumulator is C transposed: the WMMA takes the weight fragment as A and the activation as B
// (same register layout), so a lane's accumulator for M fragment i holds one row, m = 16i + col,
// and eight consecutive columns n0 + 16*wave + kb8 + e. A lane needs one activation scale per row,
// and writes its eight outputs as one 16-byte store (one 32-byte store of partials).
// THE (ROW, 128) SCALE GRID, computed once a row: thread t < ROWS owns row t. It loads the next
// 128 boundary's two scales -- the block that starts there and the one before it -- a boundary
// early, writes the row's ratio s_prev / s_next to LDS at the boundary (published by the staging
// barrier), and a lane then reads ONE ratio for each of its rows. A zero block keeps the carried
// scale; a zero block before this one sends the carried scale back to the grid, which only an
// all-zero activation block ever does.
// The (row, 128) scale grid: thread t < ROWS owns row t, loads the next boundary's two scales a
// boundary early and writes s_prev / s_next to LDS at the boundary (published by the staging
// barrier). A zero next block keeps the carried scale; after a zero block the carried scale is
// looked up from the grid.
static __device__ __forceinline__ float mxd_ratio(float ns, float np, const float* __restrict__ row,
int kb) {
return ns > 0.f ? (np > 0.f ? np : r4d_mxfp4_carried(row, kb - 2)) / ns : 1.f;
@@ -112,9 +95,9 @@ static __device__ __forceinline__ void mxd_rescale(v8f (&acc)[DTM], const float*
}
}
// The split's result: written straight to C when the accumulator holds the whole K range, else as
// an fp32 partial that the last block to arrive on this column block sums. `kb_end` is the block
// the split's last slab ends in (ABLK: the scale the accumulators are carried in).
// The split's result: written to C when the accumulator covers all of K, else as an fp32 partial
// that the last block to arrive on this column block sums. `kb_end` is the block the split's last
// slab ends in (ABLK: the scale the accumulators are carried in).
template <int DTM, int DKS, bool ABLK>
static __device__ __forceinline__ void mxd_epilogue(
const v8f (&acc)[DTM], const float* __restrict__ As, int as_rs,
@@ -125,8 +108,8 @@ static __device__ __forceinline__ void mxd_epilogue(
constexpr int ROWS = R4D_MXD_MTILE * DTM;
const int tid = threadIdx.x;
const int nb = n0 + wave * 16 + kb8; // this lane's eight columns; N % 16 == 0 keeps all or none
// Each row's final scale, once a row: the scale the split's accumulators end in (ABLK), or the
// row's own. The K loop's closing barrier retired the last ratio reads.
// Each row's final scale: the one the split's accumulators end in (ABLK), or the row's own. The
// K loop's closing barrier retired the last ratio reads.
if (tid < ROWS) {
const int m = tid < M ? tid : M - 1;
sR[tid] = ABLK ? r4d_mxfp4_carried(As + (size_t)m * as_rs, kb_end) : As[(size_t)m * as_rs];
@@ -178,7 +161,7 @@ static __device__ __forceinline__ void mxd_epilogue(
}
}
}
// EVERY thread's partial stores are ordered before the counter, then the last arriver reduces.
// Every thread's partial stores are ordered before the counter; then the last arriver reduces.
__threadfence();
__syncthreads();
if (tid == 0)
@@ -186,7 +169,7 @@ static __device__ __forceinline__ void mxd_epilogue(
__HIP_MEMORY_SCOPE_AGENT);
__syncthreads();
if (*s_last != (unsigned)(DKS - 1)) return;
if (tid == 0) cnt[blockIdx.x] = 0u; // left clean for the next launch on this stream
if (tid == 0) cnt[blockIdx.x] = 0u; // reset for the next launch
__threadfence(); // acquire, before reading the other partials
const int nhi = min(n0 + BND, N);
for (int nn = n0 + tid; nn < nhi; nn += NTHR) {
@@ -200,12 +183,10 @@ static __device__ __forceinline__ void mxd_epilogue(
}
}
/* THE WAVES A SIMD EACH TILE SHAPE RUNS AT (R4D_WAVES): the register budget the decode tile was
* tuned at, by K block, M fragments and activation-scale form, so a compiler release cannot move
* it -- LLVM 24 adds two VGPRs to the DBK 64, three-fragment form and drops it from sixteen waves to
* twelve. Where LLVM 22 itself ran out of registers (DBK 128 at seven and eight fragments without
* block activation scales, 256 VGPRs and a spill) the budget is the one the tile reaches without
* spilling. */
/* Waves per SIMD for each tile shape (R4D_WAVES), by K block, M fragments and scale form: pins
* the register budget the tile was tuned at so a compiler release cannot change occupancy (LLVM 24
* would drop the DBK 64, three-fragment form from 16 waves to 12). Where a shape spilled at 256
* VGPRs, the budget is the one it reaches without spilling. */
constexpr int mxd_waves(int dbk, int dtm, bool ablk) {
if (dbk == 64)
return dtm <= 3 ? 16 : dtm == 4 ? 12 : dtm == 5 ? 10 : dtm == 6 ? (ablk ? 9 : 10)
@@ -281,8 +262,8 @@ void r4d_gemm_mxfp4a8_decode_kernel(
const int kst = rest % KSTEPS_T, ntl = rest / KSTEPS_T;
const int r = ntl * 16 + (lane_ & 15), kloc = kst * 16 + (lane_ >> 4) * 8;
const int gn = n0 + r;
// gn is even and N % 16 == 0: clamping to the last even row keeps the 8 B read aligned and
// gc + 1 in bounds; the slot address is recomputed from the clamped row.
// gn is even and N % 16 == 0, so clamping to N - 2 keeps the 8 B read aligned and gc + 1 in
// bounds; the slot address is recomputed from the clamped row.
const int gc = gn < N - 2 ? gn : N - 2;
const int lanec = (lane_ & 16) | (gc & 15);
const mxd_u2 wv =
@@ -328,13 +309,10 @@ void r4d_gemm_mxfp4a8_decode_kernel(
kb_end, &s_last, sR);
}
// THE ONE-FRAGMENT FORM (M <= 16), which is every single-sequence verify step. The same tile, fold
// and split as above, with two changes that pay only when one M fragment leaves the block waiting
// on memory: the NEXT slab's A, codes and exponents are loaded into registers while this slab
// computes, so a block keeps its loads in flight across the WMMA phase instead of draining at every
// barrier; and a thread takes four adjacent slots, one 16 B load instead of two 8 B ones. Each
// row's reference exponent is read once, not once a slab. Bit-identical to the kernel above at
// every split: the K order, the fold and the reduction are unchanged.
// The one-fragment form (M <= 16), every single-sequence verify step. Same tile, fold and split as
// above, plus: the next slab's A, codes and exponents are loaded into registers while this slab
// computes, keeping loads in flight across barriers; a thread takes four adjacent slots (one 16 B
// load); and each row's reference exponent is read once. Bit-identical to the kernel above.
template <int DBK, int DKS, bool NT, bool ABLK>
__global__ __launch_bounds__(R4D_MXD_DWN * 32) R4D_WAVES(DBK == 64 ? 16 : 12)
void r4d_gemm_mxfp4a8_decode1_kernel(
@@ -479,10 +457,10 @@ void r4d_gemm_mxfp4a8_decode1_kernel(
}
namespace {
// THE ARRIVAL COUNTERS, one block an output buffer -- see the header. The counters cannot live in
// the scratch: other ops write the arena between two of ours, and the counters must be zero at
// entry. So they are a small device allocation of their own, taken once per (device, output) and
// kept zero by the reducer, as gemm_fp8a8_nt_m16 keys its split_ctr.
// The arrival counters, one block per output buffer (see the header). They cannot live in the
// scratch, which other ops overwrite, because they must be zero at entry. One allocation per
// (device, output), kept zero by the reducer and never freed (a recorded pass replays the
// pointer), as gemm_fp8a8_nt_m16's split_ctr.
unsigned* mxd_out_ctr(const void* out) {
static std::mutex mu;
static std::map<std::pair<int, const void*>, unsigned*> blocks;
@@ -499,9 +477,8 @@ unsigned* mxd_out_ctr(const void* out) {
return p;
}
// THE SPLIT, SLAB AND LOAD HINT A LAUNCH RUNS, from M, the shape and the tunables (0 = the rule).
// One function for the launch and the scratch hook, so the partial the hook sizes is the one
// written. See the header for the rule and what it was measured on.
// The split, slab and load hint for a launch, from M, the shape and the tunables (0 = the rule; see
// the header). Shared by the launch and the scratch hook so the sized partial is the one written.
struct MxdPlan { int ks, bk; bool nt; };
MxdPlan mxd_plan(int M, int K, int N, int KS, int BK, int NT) {
const int nblk = (N + R4D_MXD_DWN * 16 - 1) / (R4D_MXD_DWN * 16);
@@ -513,9 +490,8 @@ MxdPlan mxd_plan(int M, int K, int N, int KS, int BK, int NT) {
if (K >= 8192) // a deep K: a split of 24 or more slabs still waits on them
while (ks < R4D_MXD_MAX_KS && (K / 128) / ks >= 24 && 2 * ks * M <= 512) ks <<= 1;
p.ks = KS > 0 ? KS : ks;
// 64-wide slabs where LDS per block, (16*DTM + 128)*(DBK+8) bytes, costs a wide or deeply split
// grid its resident blocks: DTM 4 unsplit on 110+ column blocks, and DTM 8 on those or at a 4-way
// split.
// 64-wide slabs where LDS per block would cost resident blocks: DTM 4 unsplit on 110+ column
// blocks, and DTM 8 on those or at a split of 4 or more.
const bool slab64 = (tm == 4 && p.ks == 1 && nblk >= 110) || (tm == 8 && (nblk >= 110 || p.ks >= 4));
p.bk = BK > 0 ? BK : (slab64 ? 64 : 128);
p.nt = NT == 1 || (NT == 0 && (long)N * K / 2 >= (4l << 20));
@@ -632,11 +608,10 @@ extern "C" int r4d_rad_gemm_mxfp4a8_decode(const RadArgs* a, RadStream s) {
return RAD_OK;
}
/* The core sizes the op's scratch from this. `M` is the band's widest, and a narrower M can split
* wider (the fill rule asks for more blocks at M <= 24), so the size is the widest split over the
* band times its widest M -- an upper bound on every launch the band can make. A partitioned
* weight launches each part at ITS rows, which splits wider than the whole would, so the bound is
* the largest of the parts' (they run one after another and share the scratch). */
/* The op's scratch size. A narrower M can split wider than the band's widest M, so the size is the
* widest split over the band times the widest M, an upper bound for every launch. Parts of a
* partitioned weight split at their own N and run in turn sharing the scratch, so the bound is the
* largest part's. */
extern "C" int64_t r4d_rad_gemm_mxfp4a8_decode_scratch(const RadArgs* a) {
const long long M = r4d_geti_or(a, "M", 0), N = r4d_geti_or(a, "N", 0);
const long long K = r4d_geti_or(a, "K", 0);