radiance 1.14.0

New models and formats:
- libr3d: a kernel library for RDNA3 (gfx11) cards
- nomic-embed-text-v2-moe (the nomicbert plugin)
- Gemma 4 E2B with its per-layer MLP width, and Google's unmodified E2B/E4B directories in bf16
- DeepSeek-V4.1-Flash converted straight from its release: the Engram tables, the fused imatrix and
  rad-convert --chat-template for a release that ships no template
- GGUF Q8_0, Q4_K and Q6_K tensors import as their planes; rad-convert writes them without a recipe
- Qwen3.8-Flash-Next-FP8 converts and serves through qwen4exp_fp8

Speed:
- hc_read's decode kernels as one grid (34.7 -> 25.2 us at 4 rows) and its tiled up-mix pipelined
- gemm_fp8a8: the narrow form's scales staged through LDS (Flash-Next int8 at 32 rows 83.6 -> 68.3
  us), the gated three-tile form at 8 waves (gate_up at 48 rows 272.9 -> 180.6 us)
- Qwen3.8-27B: the delta net's out-projection read beside its decode recurrent update (x1 19.40 ->
  19.10 ms a step), and the verify's split-KV attention sized by context depth (x3 at 180K 42.4 ->
  40.3 ms a step)
- Hy-Memory (HYV3) decode: fused QK norm, RoPE and KV store, residual adds folded into the norms,
  coalesced one-token expert GEMMs
- spechub: the coupled verify walks the live candidates only (151 -> 31 us a launch)

Speculation:
- DFlash2: lossless coupled acceptance at T > 0 (--draft-coupling on)
- A block drafter's proposals verified as a tree, on by default for the Gemma 4 31B's DFlash2, with
  a block-16 tree that runs its short block under load

Features:
- Decode-hot residency: --draft-expert-placement host, --expert-profile, --expert-pin-top, and
  --expert-stats FILE for the routed experts each layer selects
- --ngram-placement ram=DIR: one n-gram table shared by engines on separate cards
- Prefix cache: --prefix-resume exact (default) resumes a hit on the cold run's own chunk grid, so it
  answers bit-identically; snapshots at a head other prompts share, from 528-token prompts, and for
  tip-only one drafter window below the tip; hits on models whose KV groups have different block
  sizes; the snapshot store keeps a floor of 8 ahead of --host-headroom-mib
- Typed tool-call argument tags; rad-chatparse --request renders and reads back one request as the
  server does
- MiniMax-H3: keyframes ride a latent upscale
- Gemma 3's fused all-reduce junction on the lossy wire

Fixes:
- A speculative step shorter than its band (an MTP step of 4 rows at gqa 8) no longer stops the
  engine with a scratch refusal at issue (#293)
- --p2p off: prefill back to 1.12.0's rate; 1.13.0's push check read a host-memory slot over the link
  twice (#323)
- A chat template that cannot render a conversation is refused at load, not mid-request
- imatrix entries found under llama.cpp's spelling; the W4A8 prefill's tail scale read; the GDN conv
  kernels on an f32 weight; dense integer rows at the group they compute (a Q8_0 weight at group 32)
- --expert-profile refuses a profile recorded on another model
- hc_read's one-grid wait traps past its deadline instead of reading a stale value

Changes: Plugin ABI 31, unchanged. RADIANCE_DRAFT_TREE, DFLASH2_TREE_NODES, DFLASH2_TREE_TEMP and
DFLASH_Q_TEMP are gone (trees run where the probe says they pay; temperatures 1.25 / 0.6).
This commit is contained in:
Deadcode committed 2026-10-29 12:04:24 +01:00
1 parent 481fcd5df0
commit 4a61d7296b
160 files changed
+8790 -6819

No files matched your search

+84 -106
View File
@@ -1,89 +1,78 @@
/* r3d_gemm_bf16_nt_m16.hip -- the skinny bf16 GEMM, M <= 16: the linear a token is made of.
/* r3d_gemm_bf16_nt_m16.hip -- the skinny bf16 GEMM, M <= 16.
*
* y[M,N] = a[M,K] @ b[N,K]^T, f32 accumulate, narrowed once by the store.
*
* The band is decode and speculative verify, where the weight is the cost and the activation is
* free: the kernel is built so the whole weight plane streams once, and so every output's sum is
* the sum libref forms.
* The band is decode and speculative verify, where the weight stream is the cost. The kernel
* streams the whole weight plane once, and every output's sum is the sum libref forms.
*
* ---- the lane map --------------------------------------------------------------------------
*
* One output element a lane, walking k ascending. A wave covers MT rows of the activation by
* 32/MT rows of the weight: lane l owns (m = l % MT, n = n0 + l / MT), MT being the template
* argument -- the request's M rounded up to 1, 2, 4, 8 or 16. Lanes sharing an n read the same
* weight words, so the wave issues 32/MT loads per step and broadcasts them to MT lanes each;
* lanes sharing an m read the same activation words out of LDS. A lane therefore owns its own
* accumulator from 0 to K and never exchanges a value with another lane: its chain is
* One output element per lane, walking k ascending. A wave covers MT activation rows by 32/MT
* weight rows: lane l owns (m = l % MT, n = n0 + l / MT), where MT (the template argument) is M
* rounded up to 1, 2, 4, 8 or 16. Lanes sharing an n read the same weight words; lanes sharing an m
* read the same activation words from LDS. Each lane owns one accumulator from 0 to K and never
* exchanges a value with another lane:
*
* acc = 0; for k ascending: acc = dot2(pair k+1, dot2(pair k, acc))
*
* with pairs taken two k at a time, which is `s += a[k]*b[k]` in ascending k, summand for summand,
* the loop r4d_gemm_bf16_nt_tiled's contract quotes and ref_gemm.h's quant_tile runs. There is no
* wave reduction anywhere in this kernel, so nothing reassociates: at sk = 1 the output is
* bit-identical to libref's, not merely within a tolerance.
* which is `s += a[k]*b[k]` in ascending k, the loop r4d_gemm_bf16_nt_tiled's contract quotes and
* ref_gemm.h's quant_tile runs. No wave reduction, so nothing reassociates: at sk = 1 the output
* is bit-identical to libref's.
*
* Rows past M and columns past N are clamped, not skipped -- a tail lane re-reads the last real row
* and its result is dropped at the store -- so the k loop stays uniform and the loads stay
* straight-line.
* Rows past M and columns past N are clamped, not skipped: a tail lane re-reads the last real row
* and its result is dropped at the store, so the k loop stays uniform and the loads straight-line.
*
* ---- where the bytes come from --------------------------------------------------------------
*
* The activation is staged in LDS and widened on the way in: one row is M <= 16 elements wide per
* k, so a tile is MT * 128 bf16 (4.25 KiB at MT = 16) and one 16-byte load a thread. Staging
* converts bf16 -> f16 once per element with bf16x2_to_f16x2, which is exact, so the dot2 reads
* f16 pairs straight out of LDS and the widening is paid N/MT times less often than it would be
* read. The pitch is 68 dwords: 68 mod 32 = 4, so the eight lanes of a ds_read_b128 phase land on
* banks 4 apart -- conflict-free -- and 68 * 4 = 272 keeps a 16-byte LDS read aligned.
* The activation is staged in LDS: a tile is MT * 128 bf16 (4.25 KiB at MT = 16), one 16-byte load
* per thread. Staging widens bf16 -> f16 once per element with the exact bf16x2_to_f16x2, so the
* dot2 reads f16 pairs straight from LDS and the conversion is not repeated for every weight row.
* The pitch is 68 dwords: 68 mod 32 = 4 puts the eight lanes of a ds_read_b128 phase on banks 4
* apart (conflict-free), and 68 * 4 = 272 bytes keeps a 16-byte LDS read aligned.
*
* The weight is NOT staged. M is tiny, so a staged tile would be read by 32/MT lanes and then
* thrown away; LDS staging buys reuse a skinny shape does not have. Weight bytes go straight from
* global memory into registers, widened there by four v_cvt_pkrtz per eight values (one per dword,
* so one convert per two dot2 products -- the price of the dot2 path being the widening the staged
* row pays once per element), and live for the four products they feed.
* Each wave keeps NT = 32/MT rows resident in L1 (NT lines of 128 B, 4 KiB at MT = 1, the worst
* case), which is what makes 16-byte-per-lane strided reads across NT rows cost what they fetch.
* The weight is not staged: with M this small a staged tile would be read by 32/MT lanes and
* discarded, so LDS buys no reuse. Weight bytes go from global memory into registers, are widened
* there by four v_cvt_pkrtz per eight values (one convert per two dot2 products), and live for the
* four products they feed. Each wave keeps NT = 32/MT rows resident in L1 (NT lines of 128 B,
* 4 KiB at MT = 1, the worst case), so 16-byte-per-lane strided reads across NT rows cost only what
* they fetch.
*
* ---- the register budget --------------------------------------------------------------------
*
* A gfx11 SIMD has 1024 VGPRs for a wave32. The loop is one accumulator, four activation pairs,
* four weight dwords, one 16-byte staging quad and a handful of pointers and counters: about 24
* live VGPRs, so the kernel is nowhere near the 128 that R3D_WAVES(8) -- eight waves a SIMD,
* 1024/8 -- asks for. The target is stated for the reason r3d_common.h gives: unstated, the
* scheduler picks the occupancy itself, and this kernel's throughput is waves in flight, since
* every wave is waiting on a memory load that takes hundreds of cycles. Eight waves a block of
* 256 threads is the default, and `wv` is the axis a tuner walks.
* A gfx11 SIMD has 1024 VGPRs for a wave32. The loop holds one accumulator, four activation pairs,
* four weight dwords, one 16-byte staging quad and some pointers and counters: about 24 live
* VGPRs, far under the 128 that R3D_WAVES(8) (eight waves per SIMD, 1024/8) allows. The target is
* stated for the reason r3d_common.h gives: otherwise the scheduler picks the occupancy, and this
* kernel's throughput is waves in flight, each waiting hundreds of cycles on a memory load. Eight
* waves per 256-thread block is the default; `wv` is the tuner's axis.
*
* ---- split-K --------------------------------------------------------------------------------
*
* At M = 1 a wide N is the only parallelism there is, and N = 48 (a GDN a|b projection) gives three
* waves on a 32-CU card. `sk` splits K into that many contiguous slabs, each owned by one
* blockIdx.y, each writing an f32 partial to the caller's `scratch` arena; a second kernel on the
* same stream adds the partials per output in ascending split order and narrows once.
* At M = 1 the only parallelism is N, and N = 48 (a GDN a|b projection) gives three waves on a
* 32-CU card. `sk` splits K into that many contiguous slabs, one per blockIdx.y, each writing an
* f32 partial to the caller's `scratch` arena; a second kernel on the same stream adds the partials
* per output in ascending split order and narrows once.
*
* The order matters and is stated: summing sk per-slab partials is NOT the ascending sum libref
* forms. Each partial is an ascending sum over its own slab, and the slab sums are then added in
* ascending slab order, which brackets the products differently than a single running chain does.
* sk = 1 is the bit-identical case; sk > 1 agrees to the last bits, which is the same contract
* libr4d's tiled row states about its slices. The row is verified at every sk kbench measures.
* Summing sk per-slab partials is not libref's single ascending chain: the slab sums bracket the
* products differently. sk = 1 is bit-identical; sk > 1 agrees to the last bits, the same contract
* libr4d's tiled row states for its slices. The row is verified at every sk kbench measures.
*
* `sk == 0` means "the largest legal split for this K" off the axis's ladder {16, 8, 4, 2, 1},
* legality being K % (sk * 8) == 0: a slice must end where the inner loop stops, on a group of the
* eight k (four f16 pairs) a lane consumes per step, so a slice boundary never falls inside a
* step. The staged tile is 128 k wide and its last tile is ragged, which is what lets a slice be
* 8 k long, and the cap of 16 is the split the row's scratch hook sizes the arena for.
* `sk == 0` picks the largest legal split from the axis's ladder {16, 8, 4, 2, 1}. A split is legal
* when K % (sk * 8) == 0, so a slice boundary never falls inside a lane's step of eight k (four f16
* pairs). The staged tile is 128 k wide with a ragged last tile, which allows slices as short as
* 8 k. The cap of 16 is the split the row's scratch hook sizes the arena for.
*
* ---- `nt` -----------------------------------------------------------------------------------
*
* RDNA3 has no global_load_tr_b128 (that is RDNA4) and no cache-swizzle field on a global load. So
* `nt` is an axis that measures a knob the ISA does not special-case: nt = 1 issues the weight
* reads through an address-space-1 pointer, which lowers to global_load_b128 with no cache modifier
* and no swizzle -- a plain stream -- and nt = 0 issues the same 16-byte reads through the generic
* pointer, which lowers to flat_load_dwordx4. Nothing is invented here; the axis exists so kbench
* can price the two lowerings and record that on this card they are the same stream.
* RDNA3 has no global_load_tr_b128 (an RDNA4 instruction) and no cache-swizzle field on a global
* load. nt = 1 issues the weight reads through an address-space-1 pointer, which lowers to
* global_load_b128 with no cache modifier and no swizzle; nt = 0 issues the same 16-byte reads
* through the generic pointer, which lowers to flat_load_dwordx4. The axis lets kbench price the
* two lowerings, which on this card stream the same.
*/
/* hip_runtime.h first: it defines __forceinline__, which r3d_codecs.h's R3D_HD spells, and
* r3d_common.h reaches the codecs before it reaches the runtime. */
/* hip_runtime.h first: it defines __forceinline__, which r3d_codecs.h's R3D_HD uses, and
* r3d_common.h includes the codecs before the runtime. */
#include <hip/hip_runtime.h>
#include "r3d.h"
@@ -96,15 +85,14 @@ namespace {
constexpr int kBlockThreads = 256; // eight waves, the default
constexpr int kWaves = kBlockThreads / kWave;
constexpr int kKTile = 128; // k a staged tile of a covers
constexpr int kAPitch = kKTile / 2 + 4; // dwords a staged a row
constexpr int kKTile = 128; // k covered by one staged tile of a
constexpr int kAPitch = kKTile / 2 + 4; // dwords per staged row of a
constexpr int kMTMax = 16; // the band's upper bound
/* The split rule, in one place so the launch, the shim and the row's gemm_tune_valid agree.
* `kUnit` is the k a lane's step consumes -- eight elements, four f16 pairs -- and a slice must be
* whole steps, so a split is legal when K divides by sk * kUnit. The ladder is the row's `sk` axis
* descending, and its top is the split the scratch hook sizes for: a wider split the arena cannot
* hold is not a legal default. */
/* The split rule, shared by the launcher, the shim and the row's gemm_tune_valid. `kUnit` is the
* k one lane step consumes (eight elements, four f16 pairs); a slice must be whole steps, so a
* split is legal when K divides by sk * kUnit. The ladder is the row's `sk` axis descending; its
* top is the split the scratch hook sizes the arena for. */
constexpr int kUnit = 8;
inline int pick_sk(long long K, int maxsk)
{
@@ -114,9 +102,8 @@ inline int pick_sk(long long K, int maxsk)
return 1;
}
/* A 16-byte weight read. NT true is the address-space-1 form: global_load_b128, no cache modifier,
* no swizzle, the plain stream the row's `nt` axis names. NT false goes through the generic
* pointer, which the compiler lowers to flat_load_dwordx4. */
/* A 16-byte weight read. NT true is the address-space-1 form (global_load_b128, no cache modifier,
* no swizzle); NT false goes through the generic pointer (flat_load_dwordx4). See the header. */
template <bool NT>
__device__ __forceinline__ r3d_u32x4 ldw(const uint16_t* p)
{
@@ -129,19 +116,19 @@ __device__ __forceinline__ r3d_u32x4 lds_a(const uint32_t* p)
return *(const r3d_u32x4*)p;
}
/* y[M,N] = a[M,K] @ b[N,K]^T, one output element a lane.
/* y[M,N] = a[M,K] @ b[N,K]^T, one output element per lane.
*
* `kslab` is the k range a blockIdx.y owns; with sk = 1 it is all of K and the store is the
* narrowed bf16 the caller asked for. With sk > 1 every split writes its own f32 partial, dense
* [sk][M][N], and gemm_split_reduce adds them. */
* `kslab` is the k range one blockIdx.y owns. With sk = 1 it is all of K and the store is bf16.
* With sk > 1 each split writes an f32 partial into a dense [sk][M][N] plane, and
* gemm_split_reduce adds them. */
template <int MT, bool NT, bool SPLIT>
__global__ __launch_bounds__(kBlockThreads) R3D_WAVES(8) void gemm_m16_kernel(
const uint16_t* __restrict__ A, const uint16_t* __restrict__ B,
uint16_t* __restrict__ Y, float* __restrict__ Part,
int M, int N, int K, int a_ld, int y_ld, int w_ld, int kslab)
{
/* Staged activation, widened to f16 pairs on the way in. One allocation for the widest MT:
* this kernel is instantiated for MT = 1, 2, 4, 8 and 16 and 4.25 KiB is not worth a template. */
/* Staged activation, widened to f16 pairs. Sized for the widest MT (4.25 KiB) in every
* instantiation. */
__shared__ uint32_t s_a[kMTMax * kAPitch];
const uint32_t tid = threadIdx.x;
@@ -164,13 +151,12 @@ __global__ __launch_bounds__(kBlockThreads) R3D_WAVES(8) void gemm_m16_kernel(
for (int kt = kbeg; kt < kend; kt += kKTile) {
const int kl = std::min(kKTile, kend - kt);
/* Stage: eight elements a unit, MT * (kl/8) units, 256 threads. A row past M re-reads the
* last real row, whose products the store drops. */
/* Stage: eight elements per unit, MT * (kl/8) units. A row past M re-reads the last real
* row, whose products the store drops. */
const int nu = (kl + 7) / 8;
/* blockDim.x, not the 256-thread default: the wv axis launches one, two, four or eight
* waves, and a stage loop that strides the block constant leaves units staged only for
* the waves that exist -- wv = 1 stages 32 of MT*16 units and the k loop reads the rest of
* the tile out of LDS that was never written. */
/* Stride by blockDim.x, not 256: the wv axis launches one, two, four or eight waves, and a
* fixed stride would leave part of the tile unstaged (wv = 1 would stage 32 of MT*16
* units). */
for (int i = (int)tid; i < MT * nu; i += (int)blockDim.x) {
const int r = i / nu, c = (i % nu) * 8;
const int rr = r < M ? r : M - 1;
@@ -198,33 +184,28 @@ __global__ __launch_bounds__(kBlockThreads) R3D_WAVES(8) void gemm_m16_kernel(
for (int k = 0; k < kf; k += 8) {
const r3d_u32x4 aw = lds_a(ap + k / 2);
const r3d_u32x4 b16 = ldw<NT>(w + kt + k);
/* The weight is widened here and it is not optional: dot2's operands are f16, and a
* bf16 bit pattern read as f16 is a different number -- bf16's 1.0 (0x3F80) is 1.875 --
* and any magnitude at or above 2^16 is an infinity. Feeding the bytes straight in is
* the trap this ISA sets for a bf16 GEMM that reaches for v_dot2acc_f32_f16, and it
* arrives as an output uncorrelated with the reference at rel ~ 1, with infinities in
* it, which reads as a numerics problem and is a reading problem. Four
* v_cvt_pk_rtz_f16_f32_e32 per eight values, each with the pair's two VALU of unpack,
* so the widening costs one ALU slot per product the dot2 makes; RDNA3 pairs some of
* the dots as v_dual_dot2acc_f32_f16 with the converts riding alongside. */
/* The weight must be widened: dot2's operands are f16, and a bf16 bit pattern read as
* f16 is a different number (bf16 1.0, 0x3F80, reads as 1.875; magnitudes >= 2^16
* become infinities). Four v_cvt_pk_rtz_f16_f32_e32 per eight values, each with two
* VALU of unpack, so the widening costs one ALU slot per dot2 product; RDNA3 pairs
* some of the dots as v_dual_dot2acc_f32_f16 alongside the converts. */
const r3d_u32x4 bw = r3d_u32x4{ bf16x2_to_f16x2(b16.x), bf16x2_to_f16x2(b16.y),
bf16x2_to_f16x2(b16.z), bf16x2_to_f16x2(b16.w) };
acc = dot4_f16x4(aw.x, aw.y, bw.x, bw.y, acc);
acc = dot4_f16x4(aw.z, aw.w, bw.z, bw.w, acc);
}
/* The ragged end of the last slab, ascending. A split is legal only when its slab is whole
* tiles, so this runs at sk = 1 with a K that is not a multiple of 128. */
/* The elements past the last whole step of eight, ascending. The launcher's
* K % (sk * 8) == 0 rule leaves none, so this loop does not run. */
for (int k = kf; k < kl; ++k)
acc = acc + bf16_to_f32((uint16_t)ldg_u16(A + (size_t)m_real * a_ld + kt + k))
* bf16_to_f32((uint16_t)ldg_u16(w + kt + k));
group_barrier(); // the next tile's stage must not race this one's reads
group_barrier(); // the next tile's stage must not race these reads
}
const bool keep = (n0 < (uint32_t)N) && (m < (uint32_t)M);
if (!keep) return;
if constexpr (SPLIT) {
/* Partial plane [sk][M][N]: split-major, so the reducer reads sk values M*N apart and this
* store keeps the same [M][N] shape at every split. */
/* Partial plane [sk][M][N], split-major: the reducer reads sk values M*N apart. */
Part[((size_t)blockIdx.y * M + m) * N + n0] = acc;
} else {
Y[(size_t)m * y_ld + n0] = f32_to_bf16(acc); // narrowed once, here
@@ -232,10 +213,7 @@ __global__ __launch_bounds__(kBlockThreads) R3D_WAVES(8) void gemm_m16_kernel(
}
/* The split reduction: v = ((p0 + p1) + p2) + ... in ascending split order, then one narrowing.
*
* This is the order libref's ascending sum produces at sk = 1 and not at sk > 1: each partial is
* ascending over its own slab, and the slab sums bracket the products differently than one chain
* does. The difference is in the last bits and is the split's, not a bug in it. */
* At sk > 1 this differs from libref's single chain in the last bits (see the header). */
__global__ __launch_bounds__(kBlockThreads) R3D_WAVES(8) void gemm_split_reduce(
const float* __restrict__ Part, uint16_t* __restrict__ Y, int M, int N, int y_ld, int sk)
{
@@ -254,14 +232,14 @@ __global__ __launch_bounds__(kBlockThreads) R3D_WAVES(8) void gemm_split_reduce(
/* y[M,N] = a[M,K] @ b[N,K]^T, bf16 in and out, f32 accumulate, M <= 16.
*
* a [M][K] bf16, `a_ld` elements a row (0 = K)
* a [M][K] bf16, `a_ld` elements per row (0 = K)
* b [N][K] bf16, row-major, k contiguous
* y [M][N] bf16, `y_ld` elements a row (0 = N)
* y [M][N] bf16, `y_ld` elements per row (0 = N)
*
* `sk` splits K (0 = the largest legal split), `wv` is waves per block (0 = 8), `nt` picks the
* weight load's lowering (see the file header), and `scratch` is the f32 partial arena,
* sk * M * N * 4 bytes, required when the split is on. Returns 0, -1 a null pointer, -2 a shape it
* does not serve, -3 a pitch it cannot read, -4 the HIP launch failed. */
* sk * M * N * 4 bytes, required when sk > 1. Returns 0, -1 a null pointer, -2 an unsupported
* shape, -3 an unreadable pitch, -4 the HIP launch failed. */
extern "C" int r3d_gemm_nt_bf16_m16(long a, long b, long y, int M, int N, int K, int a_ld,
int y_ld, int sk, int wv, int nt, long scratch, long stream)
{
@@ -344,10 +322,10 @@ extern "C" int r3d_rad_gemm_nt_bf16_m16(const RadArgs* a, RadStream s)
const long long y_ld = r3d_rows_pitch(Y, N, &yrows);
if (a_ld < 0 || y_ld < 0) return R3D_NO(RAD_E_STRIDE);
if (arows < M || yrows < M) return R3D_NO(RAD_E_SHAPE);
/* The weight must be dense: every wave reads b[n][k] at n * K + k, and a pitch the launcher
* does not carry cannot be described to it. */
/* The weight must be dense: every wave reads b[n][k] at n * K + k, and the launcher takes no
* weight pitch. */
if (!r3d_contig(B)) return R3D_NO(RAD_E_STRIDE);
/* 16-byte weight loads, at a row base and at a tile base, both multiples of 8 elements. */
/* 16-byte loads at a row base and at a tile base, both multiples of 8 elements. */
if (a_ld % 8) return R3D_NO(RAD_E_STRIDE);
if (!r3d_aligned(A->data, 16) || !r3d_aligned(B->data, 16)) return R3D_NO(RAD_E_ALIGN);