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
Deadcode 4a61d7296b 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).
2026-10-29 12:04:24 +01:00

292 lines
15 KiB
Plaintext

/* r3d_dflash_f32.hip -- DFlash2's candidate walk, `dflash_select`.
*
* A block-diffusion drafter proposes every position of a block in one backbone pass, so position
* l+1 is not conditioned on the drafter's choice at l. This op restores that dependence: it walks
* the `steps` positions in order and at each picks the candidate that maximises
*
* score[l, c] = unary[l, c] + sum_r pred[pid(l)][r] * hp[l][r] * succ[cand[l, c]][r]
*
* where pid(0) is the anchor (the token the target just committed, at `anchor[m * anchor_stride]`)
* and pid(l) is the walk's previous pick, `cand[l-1, chosen]`. With `hp` absent it is a plane of
* ones and the form reduces to a low-rank bigram (DSpark's Markov head). This file transcribes the
* arithmetic and order of libref's `ref_dflash_select`.
*
* Parallelism. The walk is serial in l, because step l's predecessor is the pick of step l-1;
* scoring the whole [steps, K, K] edge tensor up front would compute K times the edges read. The
* sequences and, within a step, the K candidates are parallel: a block takes `256 / top_k`
* sequences, each lane scores one (sequence, candidate) edge, and one lane per sequence takes the
* argmax. Each step costs two LDS round trips (scores out, pick back).
*
* Cost. At the band (steps=7, top_k=16, rank=256, M=256) a walk is 7 * 16 * 256 = 28672 terms, and
* the op is a gather over `succ` [n_vocab, rank]: 66.8 MiB of codebook, of which the walk reads
* M * steps * K = 28672 rows (about 14.7 MiB) plus one predecessor row per step. There is no
* arithmetic to hide the gather behind, so a tree reduction gains nothing and would break the
* agreement below.
*
* Agreement with the reference is bit-exact:
*
* - the rank sum is `sc = unary; for (i = 0; i < rank; ++i) sc += ax[i] * succ[i];`, ascending in
* r, one accumulator, with `-ffp-contract=off` so no FMA changes a term's rounding;
* - `ax[i] = pred[pid][i] * hp[l][i]` is rounded to f32 before it meets `succ`, as the reference
* stores it in a float array first (with `hp` absent, `pred * 1.0f` is the same value);
* - the argmax is a strict `>` over c ascending, so the earliest candidate wins a tie and a NaN
* never displaces the incumbent;
* - a predecessor outside the vocabulary contributes zero and a candidate outside it is scored by
* `unary` alone (the reference's `pid >= 0 && pid < nv`, `tok >= 0 && tok < nv`), so padding
* rows (a shard's unused rows, a block shorter than `steps`) are legal inputs;
* - the pick of step l is written to `tokens[m, l]` and becomes pid(l+1) unchanged, even when it
* is out of range for the codebooks.
*
* The shim refuses, none of it reachable from a row whose constraints hold: a codebook (`pred`,
* `succ`, `hp`) or band `dtype` other than bf16, the one instantiation (RAD_E_DTYPE); `unary` not
* f32 or `cand`/`anchor`/`tokens` not i32, as the schema types them (RAD_E_DTYPE); a codebook whose
* rows are not contiguous, since the gather is `token * rank + r` (RAD_E_STRIDE); `top_k` above
* 256, one lane per candidate (RAD_E_UNSUPPORTED); a `cand` plane describing more sequences than
* the band's `M` (RAD_E_SHAPE, not a truncation).
*/
#include "r3d.h"
#include "r3d_args.h"
#include "r3d_codecs.h"
#include "r3d_common.h"
#include "r3d_launch.h"
#include <rad_types.h>
namespace {
constexpr uint32_t kThr = 256; /* 8 waves: the block's (sequence, candidate) lanes */
constexpr uint32_t kKMax = kThr; /* one lane a candidate, so top_k <= 256 */
/* Whole-register loads of the planes the walk gathers through. bf16 is a 16-bit load decoded with
* the library's codec, the same decode as libref's `ldt`. */
__device__ __forceinline__ int32_t ldg_i32(const int32_t* p) {
return (int32_t)ldg_u32((const uint32_t*)p);
}
__device__ __forceinline__ float ldg_f32(const float* p) { return *(const float*)p; }
__device__ __forceinline__ uint16_t ldg_u16(const uint16_t* p) { return *(const uint16_t*)p; }
/* ------------------------------------------------------------------ the walk
*
* `per` sequences per block, `K` candidates per sequence; lane (s, c) owns the edge
* (sequence s, candidate c) of the current step. `hp` may be null, and the factor is then 1.0f, as
* in the reference's `(hp ? ldt(hp, ...) : 1.0f)`. */
__global__ __launch_bounds__(kThr) void dflash_select_kernel(
const int32_t* __restrict__ cand, const float* __restrict__ un,
const uint16_t* __restrict__ hp, const int32_t* __restrict__ anc,
const uint16_t* __restrict__ pred, const uint16_t* __restrict__ succ,
int32_t* __restrict__ out, uint32_t M, uint32_t steps, uint32_t K, uint32_t R, uint32_t nv,
uint32_t astr, long long cand_ld, long long un_ld, long long hp_ld, long long pred_ld,
long long succ_ld, long long tok_ld, uint32_t per) {
__shared__ int32_t s_pid[kThr]; /* the walk's predecessor, one per sequence */
__shared__ float s_sc[kThr]; /* this step's K scores, one per lane */
const uint32_t s = threadIdx.x / K; /* the sequence this lane walks for */
const uint32_t c = threadIdx.x - s * K; /* the candidate this lane scores */
const uint32_t seq0 = blockIdx.x * per + s;
/* A block of 256 lanes holds ceil(256 / K) sequences and the last group can be partial. A lane
* with no sequence replays the last real one, so it reaches every __syncthreads() and its loads
* stay in range; its stores are suppressed. */
const uint32_t seq = seq0 < M ? seq0 : M - 1;
const bool mine = seq0 < M;
/* pid(0): the anchor, at `m * anchor_stride` in a flat plane. As in the reference (`ld_int`),
* a negative id means "no predecessor", not an error. */
int32_t pid = ldg_i32(anc + (size_t)seq * (size_t)astr);
const bool has_hp = (hp != nullptr);
for (uint32_t l = 0; l < steps; ++l) {
const uint32_t r = seq * steps + l; /* cand/unary/hp are one row per (m, l) */
const size_t cb = (size_t)r * (size_t)cand_ld;
const int32_t tok = ldg_i32(cand + cb + c);
float sc = ldg_f32(un + (size_t)r * (size_t)un_ld + c);
/* Out-of-vocabulary predecessor: no contribution. Out-of-vocabulary candidate: `unary`
* alone. See the header. */
const bool have_p = pid >= 0 && (uint32_t)pid < nv;
const bool have_s = tok >= 0 && (uint32_t)tok < nv;
if (have_s) {
const uint16_t* pp = pred + (size_t)(have_p ? pid : 0) * (size_t)pred_ld;
const uint16_t* sp = succ + (size_t)tok * (size_t)succ_ld;
const uint16_t* hpb = hp + (size_t)r * (size_t)hp_ld;
for (uint32_t i = 0; i < R; ++i) {
/* ax[i] is rounded to f32 before the multiply, as in the reference. */
const float ax = have_p
? bf16_to_f32(ldg_u16(pp + i)) * (has_hp ? bf16_to_f32(ldg_u16(hpb + i)) : 1.0f)
: 0.0f;
sc += ax * bf16_to_f32(ldg_u16(sp + i));
}
}
s_sc[threadIdx.x] = sc; /* == s_sc[s * K + c] */
__syncthreads();
if (c == 0) {
/* Strict `>`, c ascending: the earliest candidate wins a tie, a NaN never leads. */
uint32_t best_c = 0;
float best_s = -INFINITY;
for (uint32_t j = 0; j < K; ++j) {
const float v = s_sc[s * K + j];
if (v > best_s) { best_s = v; best_c = j; }
}
const int32_t pick = ldg_i32(cand + cb + best_c);
if (mine) out[(size_t)seq * (size_t)tok_ld + l] = pick;
s_pid[s] = pick; /* pid(l+1), out-of-range included */
}
__syncthreads();
pid = s_pid[s];
}
}
/* ------------------------------------------------------------------ shim helpers */
/* The plane's row count (`rows`) and row pitch (`ld`) for rows of K elements. A rank-1 plane is a
* run of rows K apart, as the reference's rowoff treats it. */
static inline int rows_pitch(const RadTensor* t, long long K, long long* rows, long long* ld) {
const long long p = r3d_rows_pitch(t, K, rows);
if (p < 0) return RAD_E_STRIDE;
if (K <= 0 || *rows <= 0 || *rows > 0x7fffffffLL) return RAD_E_SHAPE;
if (p < K) return RAD_E_SHAPE;
*ld = p;
return RAD_OK;
}
/* A codebook: [n, rank] bf16 with contiguous rows, because the gather is `token * rank + r`. */
static inline int codebook_ok(const RadTensor* t, long long rank) {
if (!t || !t->data) return RAD_E_INVAL;
if (!r3d_is_bf16(t)) return RAD_E_DTYPE;
if (!r3d_aligned(r3d_p(t), 2)) return RAD_E_ALIGN;
if (rank > 0 && t->rank >= 1 && r3d_stride(t, (int)t->rank - 1) != 1) return RAD_E_STRIDE;
return RAD_OK;
}
static inline int r3d_launch_status(int rc) {
if (rc == 0) return RAD_OK;
switch (rc) {
case -1: return RAD_E_INVAL;
case -2: return RAD_E_SHAPE;
case -3: return RAD_E_STRIDE;
default: return RAD_E_DEVICE;
}
}
} /* namespace */
#define R3D_NEED(x) \
do { const int _s = (x); if (_s != RAD_OK) return R3D_NO(_s); } while (0)
/* ================================================================== launcher
*
* cand [M*steps][top_k] i32, candidate token ids, -1 for padding
* unary [M*steps][top_k] f32, the candidate-independent half of the score
* hp [M*steps][rank] bf16, the step's factor; 0 (absent) is a plane of ones
* anchor [M*anchor_stride] i32, the token each walk starts from, at m*anchor_stride
* pred [n_vocab][rank] bf16, the predecessor codebook
* succ [n_vocab][rank] bf16, the successor codebook the walk gathers with its picks
* tokens [M][steps] i32, out: the accepted prefix, one pick per step
*
* 0, -1 null, -2 bad extent, -3 pitch, -4 the HIP launch failed. No allocation, no synchronisation.
*/
extern "C" int r3d_dflash_select(long cand, long unary, long hp, long anchor, long pred, long succ,
long tokens, int M, int steps, int top_k, int rank, int n_vocab,
int anchor_stride, long long cand_ld, long long un_ld,
long long hp_ld, long long pred_ld, long long succ_ld,
long long tok_ld, long stream) {
if (!cand || !unary || !anchor || !pred || !succ || !tokens) return -1;
if (M <= 0 || steps <= 0 || top_k <= 0 || rank <= 0 || n_vocab <= 0 || anchor_stride <= 0)
return -2;
if ((unsigned)top_k > kKMax) return -2; /* one lane a candidate */
if (!cand_ld) cand_ld = top_k;
if (!un_ld) un_ld = top_k;
if (!hp_ld) hp_ld = rank;
if (!pred_ld) pred_ld = rank;
if (!succ_ld) succ_ld = rank;
if (!tok_ld) tok_ld = steps;
if (cand_ld < top_k || un_ld < top_k) return -3;
if (hp_ld < rank || pred_ld < rank || succ_ld < rank) return -3;
if (tok_ld < steps) return -3;
const uint32_t per = (kThr + (uint32_t)top_k - 1) / (uint32_t)top_k;
R3D_KLAUNCH(R3D_KNAME(dflash_select_kernel), dim3((M + per - 1) / per), dim3(kThr),
(hipStream_t)(uintptr_t)stream, (const int32_t*)(uintptr_t)cand,
(const float*)(uintptr_t)unary, (const uint16_t*)(uintptr_t)hp,
(const int32_t*)(uintptr_t)anchor, (const uint16_t*)(uintptr_t)pred,
(const uint16_t*)(uintptr_t)succ, (int32_t*)(uintptr_t)tokens, (uint32_t)M,
(uint32_t)steps, (uint32_t)top_k, (uint32_t)rank, (uint32_t)n_vocab,
(uint32_t)anchor_stride, cand_ld, un_ld, hp_ld, pred_ld, succ_ld, tok_ld, per);
return hipGetLastError() == hipSuccess ? 0 : -4;
}
/* ================================================================== shim
*
* libref's operand list is {cand, unary, hp?, anchor, pred, succ, tokens} and its parameter list is
* {M, steps, top_k, rank, n_vocab, anchor_stride?, dtype}. The sequence count comes from `cand`
* (M * steps * top_k ids), with the band's M as a ceiling: a plane describing more sequences is
* refused, not truncated. `rank` and `n_vocab` default to the predecessor codebook's shape, as in
* the reference, and `anchor_stride` to 1.
*/
extern "C" int r3d_rad_dflash_select(const RadArgs* a, RadStream stream) {
const RadTensor* cand = r3d_opt(a, 0);
const RadTensor* un = r3d_opt(a, 1);
const RadTensor* hp = r3d_opt(a, 2); /* OPD_O: absent is a plane of ones */
const RadTensor* anc = r3d_opt(a, 3);
const RadTensor* pred = r3d_opt(a, 4);
const RadTensor* succ = r3d_opt(a, 5);
const RadTensor* tok = r3d_opt(a, 6);
if (!cand || !cand->data || !un || !un->data || !anc || !anc->data || !pred || !pred->data ||
!succ || !succ->data || !tok || !tok->data)
return R3D_NO(RAD_E_INVAL);
/* The band names the codebooks' width; this is the bf16 instantiation. */
const char* dt = r3d_gets(a, "dtype");
if (dt && !r3d_dt_named(RAD_BF16, dt)) return R3D_NO(RAD_E_DTYPE);
const long long steps = r3d_geti_or(a, "steps", 0);
const long long K = r3d_geti_or(a, "top_k", 0);
const long long astr = r3d_geti_or(a, "anchor_stride", 1);
long long R = r3d_geti_or(a, "rank", pred->rank >= 2 ? r3d_dim(pred, (int)pred->rank - 1) : 0);
long long nv = r3d_geti_or(a, "n_vocab",
pred->rank >= 2 ? r3d_dim(pred, (int)pred->rank - 2) : 0);
const long long band_M = r3d_geti_or(a, "M", 0);
if (steps <= 0 || K <= 0 || astr <= 0 || R <= 0 || nv <= 0) return R3D_NO(RAD_E_SHAPE);
if (K > (long long)kKMax) return R3D_NO(RAD_E_UNSUPPORTED);
if (steps > 0x7fffffffLL / (K * 256) ) return R3D_NO(RAD_E_SHAPE);
/* Sequences from the plane, the band as a ceiling. */
const long long all = steps * K;
if (all <= 0 || r3d_numel(cand) % all) return R3D_NO(RAD_E_SHAPE);
const long long M = r3d_numel(cand) / all;
if (M <= 0 || (band_M > 0 && M > band_M)) return R3D_NO(RAD_E_SHAPE);
/* The reference's extent tests, in its order. */
if (r3d_numel(un) < M * steps * K) return R3D_NO(RAD_E_SHAPE);
if (r3d_numel(anc) < (M - 1) * astr + 1) return R3D_NO(RAD_E_SHAPE);
if (r3d_numel(tok) < M * steps) return R3D_NO(RAD_E_SHAPE);
if (r3d_numel(pred) < nv * R || r3d_numel(succ) < nv * R) return R3D_NO(RAD_E_SHAPE);
if (hp && hp->data && r3d_numel(hp) < M * steps * R) return R3D_NO(RAD_E_SHAPE);
if (!r3d_is_i32(cand)) return R3D_NO(RAD_E_DTYPE);
if (!r3d_is_f32(un)) return R3D_NO(RAD_E_DTYPE);
if (!r3d_is_i32(anc)) return R3D_NO(RAD_E_DTYPE);
if (!r3d_is_i32(tok)) return R3D_NO(RAD_E_DTYPE);
R3D_NEED(codebook_ok(pred, R));
R3D_NEED(codebook_ok(succ, R));
if (hp && hp->data) R3D_NEED(codebook_ok(hp, R));
if (!r3d_aligned(r3d_p(cand), 4) || !r3d_aligned(r3d_p(un), 4) || !r3d_aligned(r3d_p(anc), 4) ||
!r3d_aligned(r3d_p(tok), 4))
return R3D_NO(RAD_E_ALIGN);
long long cand_ld = 0, un_ld = 0, tok_ld = 0, rows = 0;
R3D_NEED(rows_pitch(cand, K, &rows, &cand_ld));
R3D_NEED(rows_pitch(un, K, &rows, &un_ld));
R3D_NEED(rows_pitch(tok, steps, &rows, &tok_ld));
long long hp_ld = R, pred_ld = R, succ_ld = R;
if (hp && hp->data) R3D_NEED(rows_pitch(hp, R, &rows, &hp_ld));
R3D_NEED(rows_pitch(pred, R, &rows, &pred_ld));
R3D_NEED(rows_pitch(succ, R, &rows, &succ_ld));
return r3d_launch_status(r3d_dflash_select(
r3d_addr(cand), r3d_addr(un), (hp && hp->data) ? r3d_addr(hp) : 0L, r3d_addr(anc),
r3d_addr(pred), r3d_addr(succ), r3d_addr(tok), (int)M, (int)steps, (int)K, (int)R, (int)nv,
(int)astr, cand_ld, un_ld, hp_ld, pred_ld, succ_ld, tok_ld, (long)(uintptr_t)stream));
}