Files
Deadcode 3b13fd3266 radiance 2.3.0
New models. Thinking Machines' Inkling 2 Mini by gostar (#584-#586): its text backbone and loop, the
release's NVFP4 and MXFP8 checkpoint read by the converter, an int8 trunk, the vision tower, tensor
parallelism, tool calls (with Warzuponus) and an image processor that matches transformers bit for
bit (millaguie). Inkling's routed experts are served in IQR-V4's four-bit trellis (moe_gemm_iqr),
fitted by GPTQ against Hessians the plugin now records (`--calib-dir`), its token embedding in int8
(read in place by embed_lookup), and incoai's DFlash2 drafter in four bits in place of the release's
DSpark. That container, 14.4 GiB with the vision tower, decodes 190 / 427 / 658 tokens a second at 1
/ 4 / 8 streams at tp 2 on two R9700 with `draft-window: adaptive` (code 240 / 550 / 790,
step-by-step arithmetic 358 / 811 / 1116): 19-43% ahead of the same target with the release's DSpark
drafter and 45-83% ahead of the all-int8 container (26.1 GiB, 124 / 263 / 386), at a mean KL of
0.0066 against the release's weights (int8: 0.0042). DeepSeek-V4.1-Flash on 2.x by baKon (#583),
with its vision tower, its IQ1_S and IQ2_XXS containers and linears and experts served in
llama.cpp's own blocks; parts ported from mikoskinen/r9700-exact-serving by Dad_Vibes' fork.
GLM-5.3-Flash on 2.x with its IQR / IQ8R container, an int8 latent cache and four-card serving by
AnimusNull (#566). Gemma 4 dense on EXL3-V3 by keptn42 (#537), on the EXL3 dense rows and
qwen35_exl3 tensor parallelism by Hero422. NVIDIA's 2-bit Nemotron-3.5-Bolt-74B-A9B on one card by
keptn42 (#477). Wan 2.2 TI2V-5B text-to-video and first-frame video by rkcth (#535). LFM3-1.7B on
RDNA3 iGPUs by DokterSmol (#578). Qwen3.8-27B in bf16 with the DFlash2 drafter, and bf16x12, bf16
weights packed losslessly in 12.25 bits, by eddie168 (#577). Int8 experts for drama-3 by morevgalex
(#598).

Speculative decoding. Draft trees for Qwen3.8-27B's DFlash2 drafts by Kezi (#450, opt-in): one card,
+17% at one stream and +14% at two. --draft-max-lanes and --draft-min-accept, two gates on a block
drafter, and --draft-window-ckpt, its window KV kept in the checkpoint slots, by rkcth (#600-#602).
`draft-window: adaptive` verifies each sequence's drafts up to the first one unlikely to be kept. A
verify's rows share each K and V tile in Inkling's attention by gostar (#608): 93 to 118 tokens a
second at a 70k-token prompt on one card.

Generation. Krea 2 instruction edits with one or two references and registered outfill, LoRA on the
image generators with bias deltas and row-mixer adapters applied per request, and MiniMax-H3's
model-declared sparse attention profiles, by rkcth (#534, #542, #543, #605-#609). MiniMax-H3's audio
reference guide pinned from a frame by 1beb (#595). LTX-2's chunk ceiling raised to 40 by Itoash
(#570).

Fleets and memory. memory.queue-reserve, the hardware queues a card keeps ready, by kevinjd (#589).
memory.pinned-numa-node by Foundry (#580). memory.card-headroom-mib on single-model tensor-parallel
budgets by zihaomu (#624). Parked weights rebuilt on the CPU, memory ownership kept across a failed
handoff and quiescent callbacks serialised, by rkcth (#557-#561). A warm load may unload a parked
model (#592); the first card listed chooses the architecture (#458).

Kernels. libr3d's packed dense IQR GEMM and routed IQR expert rows on gfx1100, and libhalo's GEMV
decode for Qwen3.8-Flash-Next's MoE down, by zihaomu (#562, #620, #622). GGML IQ2_XXS and IQ3_XXS
packed on the card by baKon.

Dashboard. KV cache in tokens (in use, capacity now and with all flexible space), live figures
through long prefill steps, step / decode / prefill / acceptance lines without holes, and expert hit
rates only on tiered models. Seven tile and history defects fixed by Noblistic (#597), vision
encoder and per-card hardware figures by davetha (#574), per-LoRA attribution by dowjames (#552).

Fixed. return_tokens / return_token_ids on the OpenAI endpoints (#571). reasoning_effort "none" and
"minimal" reach a template that takes them as levels, by millaguie (#608). Misaligned loads in the
windowed paged attention over an int8 cache (#540). A retired 27B head refused by name (#541).
rad-convert's GPU trellis jobs kept fed, and --reuse-only, by gostar (#575). jinja's tojson takes
sort_keys by Warzuponus (#582). hfget measures file sizes with stat, by kepler_62f (#623). The
tested C++ compiler is stated in the build guide by lowell80 (#551). The codebase was cleaned up:
shared helpers instead of repeated blocks, and the int8 linear, activation and GEMM code no longer
named after the retired fp8 formats.

Inkling 2 Mini is served in one form: IQR-V4 experts with the DFlash2 drafter
(StillDeadcode/inkling-2-mini-iq4r-v4 on Hugging Face, or
data/recipes/inkling2-mini-iq4r-v4.recipe). A container with the release's DSpark drafter, or with
NVFP4, int8 or 4-bit experts, is refused by name and needs converting again.

Plugin ABI 44: out-of-tree plugins need to be rebuilt.

Thanks to @gostar, @baKon, @Dad_Vibes, @AnimusNull, @keptn42, @Hero422, @rkcth, @DokterSmol,
@eddie168, @morevgalex, @Kezi, @1beb, @Itoash, @kevinjd, @Foundry, @zihaomu, @Noblistic, @davetha,
@dowjames, @Warzuponus, @millaguie, @kepler_62f, @lowell80 and @Code000.
2026-11-26 15:28:37 +01:00

1101 lines
55 KiB
C++

/* ref_gdn.cpp -- the gated delta net: the conv preamble, the chunk preamble, the chunked scan, the
* recurrent update and the gated norm.
*
* libr4d's algorithm without the fusion: the state lives in a per-thread workspace instead of WMMA
* accumulators, every intermediate is written out, and the gram is recomputed per v head instead
* of once per k head. The arithmetic is the same.
*
* ============================== The algorithm ==============================
*
* Per token, per v head, with q and k l2-normalised and the state S shaped [head_v, head_k]:
*
* g = -exp(A_log) . softplus(a + dt_bias) beta = sigmoid(b)
* S *= e^g
* u = beta . (v - S k) S += u k^T o = S q
*
* Decode runs this recurrent form. Prefill runs the chunked form: the same recurrence blocked over
* `chunk` tokens, with intra-chunk dependencies solved by one triangular inverse (`gdn_kkt_solve`):
*
* ge[j] = e^{g_j} gs[j] = e^{g_last - g_j} (g is the intra-chunk cumsum of the gate)
* U = K S^T W = beta . (V - ge . U) V' = A W
* P = Q K^T sA = scale . e^{g_i - g_j} . tril(P)
* O = scale . ge . (Q S^T) + sA V'
* S = e^{g_last} S + V'^T (gs . K)
*
* Invariant: every decay factor formed is <= 1. Intra-chunk gate spans on real prefill inputs reach
* several hundred nats, so splitting the exponent about any reference c (e^{g_i - c}, e^{c - g_j})
* overflows fp32's e^88 on one side and underflows on the other. The pairs e^{g_i - g_j} (j <= i)
* and e^{g_last - g_j} stay in (0, 1].
*
* ============================== The layouts ==============================
*
* q, k [T, head_k_count, head_k] v, o [T, head_v_count, head_v]
* g, beta [T, head_v_count] A [T, head_v_count, chunk]
* h0, ht [N, head_v_count, head_v, head_k] cu [N+1]
* conv w [conv_dim, conv_width] conv b [conv_dim]
* conv_state [n_slots, conv_dim, state_len]
* state [n_slots, head_v_count, head_v, head_k]
*
* with conv_dim = 2 * head_k_count * head_k + head_v_count * head_v, and `x` the fused projection
* row [q | k | v | a | b]: conv_dim convolved channels, then one `a` and one `b` per v head.
* gdn_conv_prep's `a` and `b_gate` operands are optional: present, they are separate gate and beta
* projections; absent, they are the last 2 * head_v_count columns of `x`. gdn_recurrent_update
* always takes them separately (strided views of a fused row are fine).
*
* The speculative rollback contract for the two state caches is in ref_ops.h.
*/
#include "ref_common.h"
#include "ref_ops.h"
#include <algorithm>
using namespace ref;
namespace {
/* The deepest convolution history held in a stack frame. libr4d's width is 4; a wider request
* returns RAD_E_UNSUPPORTED rather than convolving a truncated window. */
enum { REF_CONV_HIST_MAX = 16 };
struct V3 { const RadTensor* t; int64_t s0, s1, s2; };
struct V2 { const RadTensor* t; int64_t s0, s1; };
static inline V3 v3(const RadTensor* t, int64_t d1, int64_t d2) {
V3 v{ t, d1 * d2, d2, 1 };
if (t->rank >= 3) {
v.s0 = t->stride[t->rank - 3];
v.s1 = t->stride[t->rank - 2];
v.s2 = t->stride[t->rank - 1];
}
return v;
}
/* v3() for an operand that may be a column view of a wider row (the fp8 delta net's output gate is
* the tail of a fused [q|k|v|z] projection). A rank-2 view carries its token stride in stride[0],
* where v3() would assume d1*d2. */
static inline V3 v3_rows(const RadTensor* t, int64_t d1, int64_t d2) {
if (t->rank != 2) return v3(t, d1, d2);
return V3{ t, t->stride[0], d2 * t->stride[1], t->stride[1] };
}
static inline V2 v2(const RadTensor* t, int64_t d1) {
V2 v{ t, d1, 1 };
if (t->rank >= 2) { v.s0 = t->stride[t->rank - 2]; v.s1 = t->stride[t->rank - 1]; }
return v;
}
static inline int64_t at3(const V3& v, int64_t i, int64_t j, int64_t k) {
return i * v.s0 + j * v.s1 + k * v.s2;
}
static inline int64_t at2(const V2& v, int64_t i, int64_t j) { return i * v.s0 + j * v.s1; }
/* [N, heads, head_v, head_k], the shape of the chunked scan's h0/ht and the paged recurrent state.
* Strides are read off the tensor, not derived: a paged state cache is padded to match the
* attention page, so a derived stride would misread every slot after the first. */
struct V4 { const RadTensor* t; int64_t s0, s1, s2, s3; };
static inline V4 v4(const RadTensor* t, int64_t d1, int64_t d2, int64_t d3) {
V4 v{ t, d1 * d2 * d3, d2 * d3, d3, 1 };
if (t->rank >= 4) {
v.s0 = t->stride[t->rank - 4];
v.s1 = t->stride[t->rank - 3];
v.s2 = t->stride[t->rank - 2];
v.s3 = t->stride[t->rank - 1];
}
return v;
}
static inline int64_t at4(const V4& v, int64_t i, int64_t j, int64_t k, int64_t l) {
return i * v.s0 + j * v.s1 + k * v.s2 + l * v.s3;
}
/* The conv state is [n_slots, conv_dim, state_len]; a flattened one is read as the tight layout. */
static inline V3 conv_state_view(const RadTensor* cs, int64_t conv_dim, int64_t width,
int64_t* state_len) {
*state_len = cs->rank >= 3 ? cs->shape[cs->rank - 1] : (width > 1 ? width - 1 : 1);
return v3(cs, conv_dim, *state_len);
}
/* What gdn_conv_prep and gdn_conv_update share: the fused row `x`, the conv weight and bias, and
* the q, k and v outputs the channels split into. */
struct ConvIO {
const RadTensor *x, *w, *bias, *q, *k, *v;
int64_t xrow, xcol, Hg, H, K, V, width;
/* A tree step's parents (the `_tree` ops' `spec_parent`), null for a chain. */
const RadTensor* par = nullptr;
};
/* The row a tree step's row `t` continues from, within its sequence's rows from `bos`
* (RadBatch::spec_parent, docs/OPS.md): -1 for the committed history, which the sequence's first
* row always continues. An ordinary causal row (-2), like every row without the operand, continues
* the row before it. */
static inline int64_t tree_parent(const RadTensor* par, int64_t bos, int64_t t) {
if (t <= 0) return -1;
if (!par) return t - 1;
const int64_t p = ld_int(par->dtype, par->data, offlin(par, bos + t));
return (p < -1 || p >= t) ? t - 1 : p;
}
/* Channel `c` of the depthwise causal convolution with silu over one sequence's `slen` tokens
* from row `bos`, `hist` holding its width - 1 entries of history. Channels are grouped in whole
* heads, so a group never straddles the q/k/v boundaries at any tensor-parallel width. */
static void conv_channel(const ConvIO& io, int64_t c, int64_t bos, int64_t slen,
const float* hist) {
const int64_t Hg = io.Hg, K = io.K, V = io.V, width = io.width;
const RadTensor* out;
V3 ov;
int64_t oh, od;
if (c < Hg * K) {
out = io.q; ov = v3(out, Hg, K); oh = c / K; od = c % K;
} else if (c < 2 * Hg * K) {
out = io.k; ov = v3(out, Hg, K); oh = (c - Hg * K) / K; od = (c - Hg * K) % K;
} else {
out = io.v; ov = v3(out, io.H, V); oh = (c - 2 * Hg * K) / V; od = (c - 2 * Hg * K) % V;
}
const V2 wv = v2(io.w, width);
const float bs = io.bias ? ldt(io.bias, c * laststride(io.bias)) : 0.0f;
for (int64_t t = 0; t < slen; ++t) {
float acc = bs;
if (io.par) {
/* A tree: the taps behind row t are its ancestors' inputs, newest first, and past
* the block's first row the committed history from its newest entry back. */
float tap[REF_CONV_HIST_MAX + 1];
tap[width - 1] = ldt(io.x, (bos + t) * io.xrow + c * io.xcol);
int64_t anc = tree_parent(io.par, bos, t), used = 0;
for (int64_t i = 1; i < width; ++i) {
if (anc >= 0) {
tap[width - 1 - i] = ldt(io.x, (bos + anc) * io.xrow + c * io.xcol);
anc = tree_parent(io.par, bos, anc);
} else {
tap[width - 1 - i] = hist[width - 2 - used++];
}
}
for (int64_t j = 0; j < width; ++j) acc += tap[j] * ldt(io.w, at2(wv, c, j));
} else {
for (int64_t j = 0; j < width; ++j) {
const int64_t src = t - (width - 1) + j;
const float val = src >= 0 ? ldt(io.x, (bos + src) * io.xrow + c * io.xcol)
: hist[src + width - 1];
acc += val * ldt(io.w, at2(wv, c, j));
}
}
stt(out, at3(ov, bos + t, oh, od), act_silu(acc));
}
}
} /* namespace */
/* ------------------------------------------------------------------ gdn_conv_prep */
/* Everything between the qkv projection and the chunked scan: the width-`conv_width` depthwise
* causal convolution with silu and its state cache, the q/k/v split, the l2 norm of each q and k
* head, the gate with its per-chunk cumsum, and beta.
*
* The conv output is rounded before the norm, as the unfused pair does (the conv writes bf16, the
* prep reads it back); the reference writes it to its output tensor and reads it back.
*
* The l2 norm is rsqrt(sum(x^2) + l2_eps) with no division by head width (a norm, not an RMS);
* l2_eps defaults to 1e-6, FLA's and libr4d's L2NORM_EPS. */
int ref_gdn_conv_prep(const RadArgs* a, RadStream) {
if (a->n_t < 16) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *w = &a->t[1], *cs = &a->t[5], *cu = &a->t[6];
const RadTensor *q = &a->t[11], *k = &a->t[12], *v = &a->t[13];
const RadTensor *g = &a->t[14], *beta = &a->t[15];
const RadTensor* bias = t_in(a, 2);
const RadTensor* A_log = t_in(a, 3);
const RadTensor* dt_bias = t_in(a, 4);
/* Separate gate and beta projections; absent, they are the last 2*H columns of `x`. */
const RadTensor* ta = t_in(a, 7);
const RadTensor* tb = t_in(a, 8);
const RadTensor* sidx = t_in(a, 9);
const RadTensor* hini = t_in(a, 10);
if (!x->data || !w->data || !cu->data || !q->data || !k->data || !v->data ||
!g->data || !beta->data) return RAD_E_INVAL;
/* Both or neither, or one read would fall back into a fused row that may not exist. */
if ((ta != nullptr) != (tb != nullptr)) return RAD_E_INVAL;
const int64_t K = p_int(a, "head_k", 0), V = p_int(a, "head_v", 0);
/* `chunk` is RAD_DERIVED and no operand's shape records it, yet the gate cumsum is cut at the
* chunk boundary. A call that omits it gets RAD_E_SHAPE rather than a cumsum over the wrong
* span; the architecture plugins pin it. */
const int64_t chunk = p_int(a, "chunk", 0), width = p_int(a, "conv_width", 0);
const float l2eps = p_f32(a, "l2_eps", 1e-6f);
const float spthr = p_f32(a, "softplus_thr", 20.0f);
if (K <= 0 || V <= 0 || chunk <= 0 || width <= 0) return RAD_E_SHAPE;
if (width - 1 > REF_CONV_HIST_MAX) return RAD_E_UNSUPPORTED;
const int64_t N = numel(cu) - 1;
if (N <= 0) return RAD_E_SHAPE;
const int64_t T = cu_at(cu, N);
if (T <= 0) return RAD_E_SHAPE; /* zero rows (docs/OPS.md, Conventions) */
const int64_t Hg = numel(q) / (T * K);
const int64_t H = numel(v) / (T * V);
if (Hg <= 0 || H <= 0 || H % Hg != 0) return RAD_E_SHAPE;
const int64_t conv_dim = 2 * Hg * K + H * V;
if (numel(w) < conv_dim * width) return RAD_E_SHAPE;
if (numel(g) < T * H || numel(beta) < T * H) return RAD_E_SHAPE;
const int64_t xrow = x->rank >= 2 ? x->stride[x->rank - 2] : (conv_dim + (ta ? 0 : 2 * H));
const int64_t xcol = laststride(x);
/* Bound by the view, not the buffer: `x` may be a column slice of a wider row (the fp8 block
* passes the left `conv_dim` of a fused [q|k|v|z] projection), whose numel undercounts the
* span. Rank >= 2 is checked by shape; only a flattened operand is bounded by numel. */
const int64_t xneed = conv_dim + (ta ? 0 : 2 * H);
if (x->rank >= 2) {
if (x->shape[x->rank - 1] < xneed || x->shape[x->rank - 2] < T) return RAD_E_SHAPE;
} else if (numel(x) < (T - 1) * xrow + xneed) {
return RAD_E_SHAPE;
}
if (ta && (numel(ta) < T * H || numel(tb) < T * H)) return RAD_E_SHAPE;
int64_t state_len = 0;
const V3 cvs = conv_state_view(cs, conv_dim, width, &state_len);
if (cs->data && state_len < width - 1) return RAD_E_SHAPE;
const V3 qv = v3(q, Hg, K), kv = v3(k, Hg, K);
const V2 gv = v2(g, H), bv = v2(beta, H);
const V2 av = ta ? v2(ta, H) : V2{ nullptr, 0, 0 };
const V2 bgv = tb ? v2(tb, H) : V2{ nullptr, 0, 0 };
const ConvIO io{ x, w, bias, q, k, v, xrow, xcol, Hg, H, K, V, width };
/* ---- phase 1: the convolution, straight into q / k / v ---------------------------------- */
REF_PAR_FOR(T * conv_dim * width)
for (int64_t nc = 0; nc < N * conv_dim; ++nc) {
const int64_t n = nc / conv_dim, c = nc % conv_dim;
const int64_t bos = cu_at(cu, n), slen = cu_at(cu, n + 1) - bos;
if (slen <= 0) continue;
const int64_t slot = sidx ? ld_int(sidx->dtype, sidx->data, offlin(sidx, n)) : n;
/* A zero `has_init` means the slot holds no history: the window before the first token is
* zeros, not a recycled slot's previous contents. */
const bool warm = !hini || ld_int(hini->dtype, hini->data, offlin(hini, n)) != 0;
float hist[REF_CONV_HIST_MAX];
for (int64_t i = 0; i < width - 1; ++i)
hist[i] = warm && cs->data && slot >= 0 ? ldt(cs, at3(cvs, slot, c, i)) : 0.0f;
conv_channel(io, c, bos, slen, hist);
}
/* ---- phase 2: the l2 norm over each q and k head, reading the rounded conv output -------- */
REF_PAR_FOR(T * Hg * K * 2)
for (int64_t th = 0; th < T * Hg * 2; ++th) {
const int64_t which = th / (T * Hg);
const int64_t rest = th % (T * Hg);
const int64_t t = rest / Hg, h = rest % Hg;
const RadTensor* z = which ? k : q;
const V3& zv = which ? kv : qv;
float ss = 0.0f;
for (int64_t d = 0; d < K; ++d) { float u = ldt(z, at3(zv, t, h, d)); ss += u * u; }
const float inv = 1.0f / std::sqrt(ss + l2eps);
for (int64_t d = 0; d < K; ++d)
stt(z, at3(zv, t, h, d), ldt(z, at3(zv, t, h, d)) * inv);
}
/* ---- phase 3: the gate, its per-chunk cumsum, and beta ----------------------------------- */
/* The cumsum restarts per chunk and per sequence, as the scan consumes it: g[j] is the decay
* from the start of j's chunk through j. */
REF_PAR_FOR(T * H * 4)
for (int64_t nh = 0; nh < N * H; ++nh) {
const int64_t n = nh / H, h = nh % H;
const int64_t bos = cu_at(cu, n), slen = cu_at(cu, n + 1) - bos;
const float alog = A_log ? ldt(A_log, h * laststride(A_log)) : 0.0f;
const float dtb = dt_bias ? ldt(dt_bias, h * laststride(dt_bias)) : 0.0f;
float run = 0.0f;
for (int64_t t = 0; t < slen; ++t) {
if (t % chunk == 0) run = 0.0f;
const float av_ = ta ? ldt(ta, at2(av, bos + t, h))
: ldt(x, (bos + t) * xrow + (conv_dim + h) * xcol);
const float bvl = tb ? ldt(tb, at2(bgv, bos + t, h))
: ldt(x, (bos + t) * xrow + (conv_dim + H + h) * xcol);
run += -ref_exp(alog) * act_softplus(av_ + dtb, spthr);
stt(g, at2(gv, bos + t, h), run);
stt(beta, at2(bv, bos + t, h), act_sigmoid(bvl));
}
}
/* ---- phase 4: rewrite the conv state from the sequence's last width-1 tokens ---- */
/* After phase 1, which has read the history this overwrites. A sequence shorter than width-1
* keeps the old state in front of its tokens, so a later prefill chunk continues from the
* earlier one. */
if (cs->data && width > 1) {
REF_PAR_FOR(N * conv_dim * width)
for (int64_t nc = 0; nc < N * conv_dim; ++nc) {
const int64_t n = nc / conv_dim, c = nc % conv_dim;
const int64_t bos = cu_at(cu, n), slen = cu_at(cu, n + 1) - bos;
const int64_t slot = sidx ? ld_int(sidx->dtype, sidx->data, offlin(sidx, n)) : n;
if (slen <= 0 || slot < 0) continue;
const bool warm = !hini || ld_int(hini->dtype, hini->data, offlin(hini, n)) != 0;
float keep[REF_CONV_HIST_MAX];
const int64_t st = width - 1;
for (int64_t i = 0; i < st; ++i) {
const int64_t src = slen - (width - 1) + i;
keep[i] = src >= 0 ? ldt(x, (bos + src) * xrow + c * xcol)
: (warm ? ldt(cs, at3(cvs, slot, c, src + width - 1)) : 0.0f);
}
for (int64_t i = 0; i < st; ++i) stt(cs, at3(cvs, slot, c, i), keep[i]);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ gdn_conv_update */
/* The same convolution for a decode step, against the rolling window (contract in ref_ops.h): the
* read starts at num_accepted-1, and the rewrite shifts the buffer down by one. A rejection changes
* the read offset instead of recomputing (spec §10).
*
* No l2 norm: on the decode path the recurrent update does it, as in libr4d. */
static int conv_update_impl(const RadArgs* a, const RadTensor* par) {
if (a->n_t < 10) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *w = &a->t[1], *cs = &a->t[3], *cu = &a->t[6];
const RadTensor *q = &a->t[7], *k = &a->t[8], *v = &a->t[9];
const RadTensor* bias = t_in(a, 2);
const RadTensor* sidx = t_in(a, 4);
const RadTensor* nacc = t_in(a, 5);
if (!x->data || !w->data || !cs->data || !cu->data || !q->data || !k->data || !v->data)
return RAD_E_INVAL;
const int64_t K = p_int(a, "head_k", 0), V = p_int(a, "head_v", 0);
const int64_t width = p_int(a, "conv_width", 0);
if (K <= 0 || V <= 0 || width <= 0) return RAD_E_SHAPE;
if (width - 1 > REF_CONV_HIST_MAX) return RAD_E_UNSUPPORTED;
const int64_t N = numel(cu) - 1;
if (N <= 0) return RAD_E_SHAPE;
const int64_t T = cu_at(cu, N);
if (T <= 0) return RAD_E_SHAPE;
const int64_t Hg = numel(q) / (T * K);
const int64_t H = numel(v) / (T * V);
if (Hg <= 0 || H <= 0 || H % Hg != 0) return RAD_E_SHAPE;
const int64_t conv_dim = 2 * Hg * K + H * V;
if (numel(w) < conv_dim * width) return RAD_E_SHAPE;
const int64_t xrow = x->rank >= 2 ? x->stride[x->rank - 2] : conv_dim;
const int64_t xcol = laststride(x);
int64_t state_len = 0;
const V3 cvs = conv_state_view(cs, conv_dim, width, &state_len);
/* Check (not clamp) the read window and the last write slot against the state depth; a too
* shallow cache is a caller bug that would otherwise silently truncate history. */
for (int64_t n = 0; n < N; ++n) {
const int64_t slen = cu_at(cu, n + 1) - cu_at(cu, n);
const int64_t off = nacc ? ld_int(nacc->dtype, nacc->data, offlin(nacc, n)) - 1 : 0;
if (off < 0) return RAD_E_INVAL;
if (off + width - 1 > state_len) return RAD_E_SHAPE;
if (width > 1 && (width - 2) + slen > state_len) return RAD_E_SHAPE;
}
if (par && numel(par) < T) return RAD_E_SHAPE;
const ConvIO io{ x, w, bias, q, k, v, xrow, xcol, Hg, H, K, V, width, par };
REF_PAR_FOR(T * conv_dim * width)
for (int64_t nc = 0; nc < N * conv_dim; ++nc) {
const int64_t n = nc / conv_dim, c = nc % conv_dim;
const int64_t bos = cu_at(cu, n), slen = cu_at(cu, n + 1) - bos;
if (slen <= 0) continue;
const int64_t slot = sidx ? ld_int(sidx->dtype, sidx->data, offlin(sidx, n)) : n;
if (slot < 0) continue;
const int64_t off = nacc ? ld_int(nacc->dtype, nacc->data, offlin(nacc, n)) - 1 : 0;
float hist[REF_CONV_HIST_MAX];
for (int64_t i = 0; i < width - 1; ++i) hist[i] = ldt(cs, at3(cvs, slot, c, off + i));
conv_channel(io, c, bos, slen, hist);
/* Shift down by one: the tail of the old history, then this step's tokens. Safe in place
* because the history was read into `hist` first. */
if (width > 1) {
for (int64_t i = 0; i + 1 < width - 1; ++i)
stt(cs, at3(cvs, slot, c, i), hist[i + 1]);
for (int64_t t = 0; t < slen; ++t)
stt(cs, at3(cvs, slot, c, t + width - 2),
ldt(x, (bos + t) * xrow + c * xcol));
}
}
return RAD_OK;
}
int ref_gdn_conv_update(const RadArgs* a, RadStream) { return conv_update_impl(a, nullptr); }
/* gdn_conv_update over a tree verify (rad_drafter_tree): operand 10, `spec_parent`, names each
* row's parent within its sequence's rows, and a row's taps are its ancestors' inputs with the
* committed history above the block's first row. The window is rewritten by row, as the chain's
* is: the core moves the accepted path's entries to the front afterwards (ADV_COMPACT). */
int ref_gdn_conv_update_tree(const RadArgs* a, RadStream) {
const RadTensor* par = a->n_t > 10 ? t_in(a, 10) : nullptr;
if (!par || !par->data) return RAD_E_INVAL;
return conv_update_impl(a, par);
}
/* ------------------------------------------------------------------ gdn_kkt_solve */
/* A = (I + strict_lower(diag(beta) . K K^T . e^{g_i - g_j}))^{-1}, per chunk, per v head.
*
* The decay is applied per element as e^{g_i - g_j}, not split about a per-chunk reference: only
* i > j is kept and g decreases along the chunk, so the exponent is <= 0 and cannot overflow (see
* the file header and libr4d's r4d_gdn_kkt_solve).
*
* The inverse is a forward substitution instead of libr4d's blocked 16 -> 32 -> 64 WMMA merge.
* I + M is unit lower triangular, so its inverse is unique; both compute it in fp32. */
int ref_gdn_kkt_solve(const RadArgs* a, RadStream) {
if (!have(a, 5)) return RAD_E_INVAL;
const RadTensor *k = &a->t[0], *beta = &a->t[1], *g = &a->t[2], *cu = &a->t[3], *A = &a->t[4];
const int64_t K = p_int(a, "head_k", 0);
/* `chunk` is RAD_DERIVED and may be absent; here A's shape [T, heads, chunk] carries it. */
const int64_t chunk = p_int(a, "chunk", A->rank >= 3 ? A->shape[A->rank - 1] : 0);
if (K <= 0 || chunk <= 0) return RAD_E_SHAPE;
const int64_t N = numel(cu) - 1;
if (N <= 0) return RAD_E_SHAPE;
const int64_t T = cu_at(cu, N);
if (T <= 0) return RAD_E_SHAPE;
const int64_t H = numel(beta) / T;
const int64_t Hg = numel(k) / (T * K);
if (H <= 0 || Hg <= 0 || H % Hg != 0) return RAD_E_SHAPE;
if (numel(A) < T * H * chunk) return RAD_E_SHAPE;
const int64_t grp = H / Hg;
const V3 kv = v3(k, Hg, K), Av = v3(A, H, chunk);
const V2 gv = v2(g, H), bv = v2(beta, H);
/* The chunk's keys are widened and transposed first, so the dot products and the
* substitution run across j in vector lanes; each element's chain keeps its order. */
REF_PAR_FOR(T * H * chunk * K)
for (int64_t nh = 0; nh < N * H; ++nh) {
const int64_t n = nh / H, h = nh % H, hk = h / grp;
const int64_t bos = cu_at(cu, n), slen = cu_at(cu, n + 1) - bos;
float* wsp = ws_get((size_t)(chunk * chunk + 3 * chunk + 2 * chunk * K));
float* Am = wsp; /* [chunk, chunk] the inverse, in fp32 */
float* row = Am + chunk * chunk; /* [chunk] */
float* s = row + chunk; /* [chunk] */
float* gc = s + chunk; /* [chunk] */
float* kc = gc + chunk; /* [chunk, K] */
float* kT = kc + chunk * K; /* [K, chunk] */
for (int64_t c0 = 0; c0 < slen; c0 += chunk) {
const int64_t L = (c0 + chunk < slen) ? chunk : slen - c0;
for (int64_t j = 0; j < L; ++j) {
const int64_t tj = bos + c0 + j;
gc[j] = ldt(g, at2(gv, tj, h));
for (int64_t d = 0; d < K; ++d) {
kc[j * K + d] = ldt(k, at3(kv, tj, hk, d));
kT[d * chunk + j] = kc[j * K + d];
}
}
for (int64_t i = 0; i < L; ++i) {
const int64_t ti = bos + c0 + i;
const float bi = ldt(beta, at2(bv, ti, h));
const float gi = gc[i];
for (int64_t j = 0; j < i; ++j) row[j] = 0.0f;
for (int64_t d = 0; d < K; ++d) {
const float kd = kc[i * K + d];
const float* kr = kT + d * chunk;
for (int64_t j = 0; j < i; ++j) row[j] += kd * kr[j];
}
for (int64_t j = 0; j < i; ++j) row[j] = bi * row[j] * ref_exp(gi - gc[j]);
/* (I + M) A = I, row by row: A[i][j] = delta - sum_{j<=l<i} M[i][l] A[l][j]. */
for (int64_t j = 0; j <= i; ++j) s[j] = (i == j) ? 1.0f : 0.0f;
for (int64_t l = 0; l < i; ++l) {
const float rl = row[l];
const float* al = Am + l * chunk;
for (int64_t j = 0; j <= l; ++j) s[j] -= rl * al[j];
}
for (int64_t j = 0; j <= i; ++j) Am[i * chunk + j] = s[j];
for (int64_t j = i + 1; j < chunk; ++j) Am[i * chunk + j] = 0.0f;
for (int64_t j = 0; j < chunk; ++j)
stt(A, at3(Av, ti, h, j), Am[i * chunk + j]);
}
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ gdn_chunk_scan */
int ref_gdn_chunk_scan(const RadArgs* a, RadStream) {
if (a->n_t < 10) return RAD_E_INVAL;
const RadTensor *q = &a->t[0], *k = &a->t[1], *v = &a->t[2], *A = &a->t[3];
const RadTensor *g = &a->t[4], *beta = &a->t[5], *cu = &a->t[7];
const RadTensor *o = &a->t[8], *ht = &a->t[9];
const RadTensor* h0 = t_in(a, 6);
/* The state slot, for a state pool not indexed in batch order. Absent, row n owns slot n. */
const RadTensor* sidx = t_in(a, 10);
if (!q->data || !k->data || !v->data || !A->data || !g->data || !beta->data ||
!cu->data || !o->data) return RAD_E_INVAL;
const int64_t K = p_int(a, "head_k", 0), V = p_int(a, "head_v", 0);
/* RAD_DERIVED; A is [T, heads, chunk], as gdn_kkt_solve writes it. */
const int64_t chunk = p_int(a, "chunk", A->rank >= 3 ? A->shape[A->rank - 1] : 0);
const float scale = p_f32(a, "scale", 1.0f);
if (K <= 0 || V <= 0 || chunk <= 0) return RAD_E_SHAPE;
const int64_t N = numel(cu) - 1;
if (N <= 0) return RAD_E_SHAPE;
const int64_t T = cu_at(cu, N);
if (T <= 0) return RAD_E_SHAPE;
const int64_t H = numel(beta) / T;
const int64_t Hg = numel(q) / (T * K);
if (H <= 0 || Hg <= 0 || H % Hg != 0) return RAD_E_SHAPE;
const int64_t grp = H / Hg;
if (numel(o) < T * H * V || numel(A) < T * H * chunk) return RAD_E_SHAPE;
if (!sidx) {
if (ht->data && numel(ht) < N * H * V * K) return RAD_E_SHAPE;
if (h0 && numel(h0) < N * H * V * K) return RAD_E_SHAPE;
} else if (sidx->rank < 1 || sidx->shape[0] < N) {
return RAD_E_SHAPE;
}
const V3 qv = v3(q, Hg, K), kv = v3(k, Hg, K), vv = v3(v, H, V), ov = v3(o, H, V);
const V3 Av = v3(A, H, chunk);
const V2 gv = v2(g, H), bv = v2(beta, H);
const V4 h0v = h0 ? v4(h0, H, V, K) : V4{ nullptr, 0, 0, 0, 0 };
const V4 htv = ht->data ? v4(ht, H, V, K) : V4{ nullptr, 0, 0, 0, 0 };
/* The state is held transposed, St[d][e], and the chunk's operands widened to f32 first, so
* every chain below runs across the V value columns in vector lanes. Each output element is
* still the same chain in the same order: the u, Vp, o and S sums over d or j ascending, and
* the products parenthesised as the formulas above write them. */
REF_PAR_FOR(T * H * (K + chunk) * V)
for (int64_t nh = 0; nh < N * H; ++nh) {
const int64_t n = nh / H, h = nh % H, hk = h / grp;
const int64_t bos = cu_at(cu, n), slen = cu_at(cu, n + 1) - bos;
/* Column 0 of a state_idx row is the state slot. Rows may be wider (speculative decode
* keeps extra slots beside it); the scan writes only column 0's. */
const int64_t sw = sidx && sidx->rank >= 1 ? sidx->stride[0] : 1;
const int64_t sn = sidx ? ld_int(sidx->dtype, sidx->data, n * sw) : n;
if (sn < 0) continue; /* a padded row owns no state */
/* One workspace request, carved up by hand (see ws_get in ref_common.h). */
const size_t need = (size_t)(K * V + 3 * chunk * V + 3 * chunk * K + chunk * chunk +
3 * chunk + 2 * V);
float* wsp = ws_get(need);
float* St = wsp; /* [K, V] the recurrent state, transposed */
float* Wm = St + K * V; /* [chunk, V] */
float* Vp = Wm + chunk * V; /* [chunk, V] */
float* vc = Vp + chunk * V; /* [chunk, V] v, widened */
float* kc = vc + chunk * V; /* [chunk, K] k, widened */
float* qc = kc + chunk * K; /* [chunk, K] q, widened */
float* kT = qc + chunk * K; /* [K, chunk] k, transposed */
float* Ac = kT + chunk * K; /* [chunk, chunk] A, widened */
float* gc = Ac + chunk * chunk; /* [chunk] g */
float* bc = gc + chunk; /* [chunk] beta */
float* row = bc + chunk; /* [chunk] one row of sA */
float* acc = row + chunk; /* [V] */
float* inr = acc + V; /* [V] */
for (int64_t e = 0; e < V; ++e)
for (int64_t d = 0; d < K; ++d)
St[d * V + e] = h0 ? ldt(h0, at4(h0v, sn, h, e, d)) : 0.0f;
for (int64_t c0 = 0; c0 < slen; c0 += chunk) {
const int64_t L = (c0 + chunk < slen) ? chunk : slen - c0;
for (int64_t j = 0; j < L; ++j) {
const int64_t tj = bos + c0 + j;
gc[j] = ldt(g, at2(gv, tj, h));
bc[j] = ldt(beta, at2(bv, tj, h));
for (int64_t d = 0; d < K; ++d) {
kc[j * K + d] = ldt(k, at3(kv, tj, hk, d));
qc[j * K + d] = ldt(q, at3(qv, tj, hk, d));
kT[d * chunk + j] = kc[j * K + d];
}
for (int64_t e = 0; e < V; ++e) vc[j * V + e] = ldt(v, at3(vv, tj, h, e));
for (int64_t i = 0; i <= j; ++i) Ac[j * chunk + i] = ldt(A, at3(Av, tj, h, i));
}
const float glast = gc[L - 1];
/* W = beta . (V - e^{g_j} . (K S^T)) */
for (int64_t j = 0; j < L; ++j) {
const float ge = ref_exp(gc[j]);
for (int64_t e = 0; e < V; ++e) acc[e] = 0.0f;
for (int64_t d = 0; d < K; ++d) {
const float kd = kc[j * K + d];
const float* sr = St + d * V;
for (int64_t e = 0; e < V; ++e) acc[e] += kd * sr[e];
}
for (int64_t e = 0; e < V; ++e)
Wm[j * V + e] = bc[j] * (vc[j * V + e] - ge * acc[e]);
}
/* V' = A W, with A unit lower triangular so only j <= i contributes. */
for (int64_t i = 0; i < L; ++i) {
float* vp = Vp + i * V;
for (int64_t e = 0; e < V; ++e) vp[e] = 0.0f;
for (int64_t j = 0; j <= i; ++j) {
const float aij = Ac[i * chunk + j];
const float* wr = Wm + j * V;
for (int64_t e = 0; e < V; ++e) vp[e] += aij * wr[e];
}
}
/* O = scale . e^{g_i} . (Q S^T) + sA V' */
for (int64_t i = 0; i < L; ++i) {
const int64_t ti = bos + c0 + i;
const float gi = gc[i];
const float ge = ref_exp(gi);
for (int64_t j = 0; j <= i; ++j) row[j] = 0.0f;
for (int64_t d = 0; d < K; ++d) {
const float qd = qc[i * K + d];
const float* kr = kT + d * chunk;
for (int64_t j = 0; j <= i; ++j) row[j] += qd * kr[j];
}
for (int64_t j = 0; j <= i; ++j) row[j] = scale * ref_exp(gi - gc[j]) * row[j];
for (int64_t e = 0; e < V; ++e) inr[e] = 0.0f;
for (int64_t d = 0; d < K; ++d) {
const float qd = qc[i * K + d];
const float* sr = St + d * V;
for (int64_t e = 0; e < V; ++e) inr[e] += qd * sr[e];
}
const float sg = scale * ge;
for (int64_t e = 0; e < V; ++e) acc[e] = sg * inr[e];
for (int64_t j = 0; j <= i; ++j) {
const float rj = row[j];
const float* vp = Vp + j * V;
for (int64_t e = 0; e < V; ++e) acc[e] += rj * vp[e];
}
for (int64_t e = 0; e < V; ++e) stt(o, at3(ov, ti, h, e), acc[e]);
}
/* S = e^{g_last} S + V'^T (e^{g_last - g_j} . K) */
const float el = ref_exp(glast);
for (int64_t j = 0; j < L; ++j) {
const float gs = ref_exp(glast - gc[j]);
for (int64_t d = 0; d < K; ++d) kc[j * K + d] = gs * kc[j * K + d];
}
for (int64_t d = 0; d < K; ++d) {
float* sr = St + d * V;
for (int64_t e = 0; e < V; ++e) sr[e] = el * sr[e];
for (int64_t j = 0; j < L; ++j) {
const float gk = kc[j * K + d];
const float* vp = Vp + j * V;
for (int64_t e = 0; e < V; ++e) sr[e] += vp[e] * gk;
}
}
}
if (ht->data)
for (int64_t e = 0; e < V; ++e)
for (int64_t d = 0; d < K; ++d)
stt(ht, at4(htv, sn, h, e, d), St[d * V + e]);
}
return RAD_OK;
}
/* ------------------------------------------------------------------ the anchor state form
*
* docs/OPS.md `state_form`: under `anchor` a sequence keeps one committed state, and each step
* records its tokens' rank-1 update factors beside it; the next step replays the first
* `num_accepted` of them onto the state before running its own tokens. The cache layout is
* libr4d's (r4d_gdn_recurrent_update_k128_v128_bf16_fp32state.hip), so either library can continue
* a cache the other wrote and an oracle can compare them:
*
* state_idx six columns: 0..2 the anchor, 3..5 the factor block. A paged caller's window can
* shift by -1, 0 or +1 pages a step; the anchor is published at column 2 and each
* factor copy is stamped with its column (1, 2, 3), so the next step finds the shift
* m - 1 from the stamp m at column 4 and reads the anchor at column 3 - m. A zeroed
* block reads as no shift. A non-paged caller names one slot in columns 0..2 and
* another in 3..5.
* factors per token, from the head's [V, K] block of the factor slot: the normalised k once per
* 32-row group of V, then v, then (g, beta) once per group, each group's rows
* replaying from their own copy; room for `tmax` tokens, the stamp just past them.
*
* libr4d's kernel is K = V = 128, where the block holds kTmax = 16 tokens and the stamp sits at
* 16 * fstr. Square heads from 64 up hold 16 too, so the layout is libr4d's wherever both serve
* it. A smaller head's block holds fewer tokens' factors (a 16 x 16 head: 7), so `tmax` is as many
* as fit before the stamp; a step, or an accepted prefix, longer than that is refused. */
struct AnchorLayout {
static constexpr int64_t kTmax = 16;
int64_t groups, fstr, vb, gb, tmax, mark;
AnchorLayout(int64_t K, int64_t V)
: groups((V + 31) / 32), fstr(groups * K + V + 2 * groups), vb(groups * K),
gb(groups * K + V), tmax(std::min<int64_t>(kTmax, (V * K - 1) / fstr)),
mark(tmax * fstr) {}
/* The anchor's column, from the stamp at the factor block's `mark`. */
int anchor_col(const RadTensor* st, int64_t fb) const {
const int m = (int)(ldt(st, fb + mark) + 0.5f) - 1;
return 3 - ((m >= 0 && m <= 2) ? m : 1);
}
};
/* ------------------------------------------------------------------ gdn_recurrent_update */
/* Decode's counterpart of the chunked scan, against the paged state cache: the initial state comes
* from the slot the last accepted token wrote, and every candidate token writes its own slot since
* survivors are unknown until verification (ref_ops.h). The gated RMS norm is folded into the
* epilogue, as in libr4d, whose workgroup owns the whole row. */
static int recurrent_update_impl(const RadArgs* a, const RadTensor* par) {
if (a->n_t < 14) return RAD_E_INVAL;
const RadTensor *q = &a->t[0], *k = &a->t[1], *v = &a->t[2], *av = &a->t[3], *bvt = &a->t[4];
const RadTensor *state = &a->t[7], *o = &a->t[13];
const RadTensor* A_log = t_in(a, 5);
const RadTensor* dt_bias = t_in(a, 6);
const RadTensor* sidx = t_in(a, 8);
const RadTensor* nacc = t_in(a, 9);
const RadTensor* cu = t_in(a, 10);
const RadTensor* z = t_in(a, 11);
const RadTensor* nw = t_in(a, 12);
/* The out projection's quantiser, folded in. Optional, all-or-nothing; see the operand list in
* r4d_rows.cpp for why the group is the row. */
const RadTensor* oq = (a->n_t > 14 && a->t[14].data) ? &a->t[14] : nullptr;
const RadTensor* osc = (a->n_t > 15 && a->t[15].data) ? &a->t[15] : nullptr;
if (!q->data || !k->data || !v->data || !av->data || !bvt->data || !state->data || !o->data ||
!cu) return RAD_E_INVAL;
/* `cu` bounds the sequences, as on gdn_conv_update, which reads the same tensor on the same
* step. A sequence count carried in the state index only works for rectangular batches, and the
* fast kernels read `cu[n]` before any null test. */
const char* sf = p_str(a, "state_form", "per_candidate");
const bool anchor = sf && std::strcmp(sf, "anchor") == 0;
if (!anchor && sf && std::strcmp(sf, "per_candidate") != 0) return RAD_E_UNSUPPORTED;
const int64_t K = p_int(a, "head_k", 0), V = p_int(a, "head_v", 0);
const float scale = p_f32(a, "scale", 1.0f);
const float l2eps = p_f32(a, "l2_eps", 1e-6f);
const float neps = p_f32(a, "eps", 1e-6f);
const float spthr = p_f32(a, "softplus_thr", 20.0f);
static const char* kActs[] = { "silu", "sigmoid" };
const int nact = p_enum(a, "act", kActs, 2, 0);
if (nact != 0 && nact != 1) return RAD_E_INVAL;
if (K <= 0 || V <= 0) return RAD_E_SHAPE;
/* Head and token counts come from v, [T, head_v_count, head_v]; `n_head_v` overrides the head
* count for a flattened view. */
int64_t Hv = p_int(a, "n_head_v", 0);
if (Hv <= 0) Hv = v->rank >= 3 ? v->shape[v->rank - 2] : 0;
if (Hv <= 0) return RAD_E_SHAPE;
const int64_t Tk = numel(v) / (Hv * V);
if (Tk <= 0) return RAD_E_SHAPE;
const int64_t Hg = numel(q) / (Tk * K);
if (Hg <= 0 || Hv % Hg != 0) return RAD_E_SHAPE;
const int64_t grp = Hv / Hg;
/* N comes only from cu ([N+1]); state_idx may have several columns per sequence. */
const int64_t n_seq = numel(cu) - 1;
if (n_seq <= 0) return RAD_E_SHAPE;
if (cu_at(cu, n_seq) != Tk) return RAD_E_SHAPE;
if (par && numel(par) < Tk) return RAD_E_SHAPE;
const int64_t sw = sidx ? (sidx->rank >= 2 ? sidx->shape[sidx->rank - 1]
: (numel(sidx) / n_seq > 0 ? numel(sidx) / n_seq
: 1))
: 1;
const V3 qv = v3(q, Hg, K), kv = v3(k, Hg, K), vv = v3(v, Hv, V), ov = v3(o, Hv, V);
const V3 zv = z ? v3_rows(z, Hv, V) : vv;
const V2 a2 = v2(av, Hv), b2 = v2(bvt, Hv);
const V2 si = sidx ? v2(sidx, sw) : V2{ nullptr, 0, 0 };
const V4 stv = v4(state, Hv, V, K);
/* The anchor form keeps its replay factors inside a head's [V, K] block of a state slot, at
* libr4d's offsets (AnchorLayout), so the block must be dense and hold every token's
* factors. */
const AnchorLayout al(K, V);
if (anchor) {
if (!sidx || sw < 6) return RAD_E_SHAPE;
if (stv.s3 != 1 || stv.s2 != K) return RAD_E_STRIDE;
if (al.tmax < 1) return RAD_E_SHAPE;
for (int64_t n = 0; n < n_seq; ++n) {
const int64_t na = nacc ? ld_int(nacc->dtype, nacc->data, offlin(nacc, n)) : 1;
if (cu_at(cu, n + 1) - cu_at(cu, n) > al.tmax || na > al.tmax) return RAD_E_SHAPE;
}
}
/* The state is held transposed, St[d][e], so the two state passes run across the V value
* columns in vector lanes; every chain over d keeps its order. */
REF_PAR_FOR(Tk * Hv * K * V)
for (int64_t nh = 0; nh < n_seq * Hv; ++nh) {
const int64_t n = nh / Hv, h = nh % Hv, hk = h / grp;
const int64_t bos = cu_at(cu, n), q_len = cu_at(cu, n + 1) - bos;
/* Under `anchor` a sequence with no rows this step still folds its accepted prefix in. */
if (q_len <= 0 && !anchor) continue;
const int64_t na = nacc ? ld_int(nacc->dtype, nacc->data, offlin(nacc, n)) : 1;
const int64_t rd = na > 0 ? na - 1 : 0;
int64_t col[6] = { 0, 0, 0, 0, 0, 0 };
int64_t slot_in = 0;
if (anchor) {
for (int c = 0; c < 6; ++c) col[c] = ld_int(sidx->dtype, sidx->data, at2(si, n, c));
bool pad = false;
for (int c = 0; c < 6; ++c) pad = pad || col[c] < 0;
if (pad) continue;
slot_in = col[al.anchor_col(state, at4(stv, col[4], h, 0, 0))];
} else {
slot_in = sidx ? ld_int(sidx->dtype, sidx->data, at2(si, n, rd < sw ? rd : sw - 1)) : n;
if (slot_in < 0) continue;
}
/* The factor blocks: column 4's is read, and 3, 4 and 5 are written (each a page the
* window can shift onto). */
const int64_t fb = anchor ? at4(stv, col[4], h, 0, 0) : 0;
const int64_t fbs[3] = { anchor ? at4(stv, col[3], h, 0, 0) : 0, fb,
anchor ? at4(stv, col[5], h, 0, 0) : 0 };
const bool dup_f = anchor && (col[3] != col[4] || col[5] != col[4]);
const size_t need = (size_t)(V * K + 2 * K + 3 * V);
float* wsp = ws_get(need);
float* St = wsp; /* [K, V] */
float* qn = St + V * K;
float* kn = qn + K;
float* uu = kn + K;
float* orow = uu + V;
float* sk2 = orow + V;
for (int64_t e = 0; e < V; ++e)
for (int64_t d = 0; d < K; ++d) St[d * V + e] = ldt(state, at4(stv, slot_in, h, e, d));
const float alog = A_log ? ldt(A_log, h * laststride(A_log)) : 0.0f;
const float dtb = dt_bias ? ldt(dt_bias, h * laststride(dt_bias)) : 0.0f;
/* The delta rule's state update for one token over the state rows [e0, e1): decay by
* exp(g), then S += beta (v - S k) k^T, `vrow(e)` reading v. A row's update reads only
* that row, k, g and beta. Shared by the token loop and the replay, so a replayed token
* updates the state exactly as the step that ran it did. */
auto update = [&](float g, float bt, auto vrow, int64_t e0, int64_t e1) {
const float dec = ref_exp(g);
for (int64_t e = e0; e < e1; ++e) sk2[e] = 0.0f;
for (int64_t d = 0; d < K; ++d) {
float* sr = St + d * V;
const float kd = kn[d];
for (int64_t e = e0; e < e1; ++e) {
sr[e] *= dec;
sk2[e] += sr[e] * kd;
}
}
for (int64_t e = e0; e < e1; ++e) uu[e] = bt * (vrow(e) - sk2[e]);
for (int64_t d = 0; d < K; ++d) {
float* sr = St + d * V;
const float kd = kn[d];
for (int64_t e = e0; e < e1; ++e) sr[e] += uu[e] * kd;
}
};
/* One token's update from its stored factors at `f`. Each 32-row group of V replays
* from its own copy of k and its own (g, beta), as libr4d's kernel reads them. Every
* writer stores equal copies, so on a block a step wrote this is the same as reading one
* copy for all rows. */
auto replay = [&](int64_t f) {
for (int64_t gi = 0; gi < al.groups; ++gi) {
for (int64_t d = 0; d < K; ++d) kn[d] = ldt(state, f + gi * K + d);
update(ldt(state, f + al.gb + 2 * gi), ldt(state, f + al.gb + 2 * gi + 1),
[&](int64_t e) { return ldt(state, f + al.vb + e); },
32 * gi, std::min<int64_t>(V, 32 * gi + 32));
}
};
if (anchor) {
/* Replay the previous step's accepted tokens from their factors, publish the advanced
* anchor at column 2, and stamp each factor copy with the column it sits in. */
for (int64_t t = 0; t < na; ++t) replay(fb + t * al.fstr);
for (int64_t e = 0; e < V; ++e)
for (int64_t d = 0; d < K; ++d)
stt(state, at4(stv, col[2], h, e, d), St[d * V + e]);
for (int c = 0; c < 3; ++c) stt(state, fbs[c] + al.mark, (float)(c + 1));
}
/* A tree step (`par`): a row continues from its parent's state, not the row before's, so
* each row's state is kept: snap[0] the state the block starts from, snap[t + 1] the one
* row t left. Under `anchor` the start is the published anchor as the cache holds it and
* a row is applied from its stored factors, so a row's state is the same whichever rows
* ran between it and its parent, at any state dtype (libr4d rebuilds a branch point from
* exactly those). */
std::vector<float> snap;
const bool tree = par != nullptr && q_len > 0;
if (tree) {
if (anchor)
for (int64_t e = 0; e < V; ++e)
for (int64_t d = 0; d < K; ++d)
St[d * V + e] = ldt(state, at4(stv, col[2], h, e, d));
snap.resize((size_t)((q_len + 1) * V * K));
std::copy(St, St + V * K, snap.begin());
}
for (int64_t t = 0; t < q_len; ++t) {
const int64_t tok = bos + t;
const float gv = -ref_exp(alog) *
act_softplus(ldt(av, at2(a2, tok, h)) + dtb, spthr);
const float bt = act_sigmoid(ldt(bvt, at2(b2, tok, h)));
float sq = 0.0f, sk = 0.0f;
for (int64_t d = 0; d < K; ++d) {
const float u = ldt(q, at3(qv, tok, hk, d)), w = ldt(k, at3(kv, tok, hk, d));
sq += u * u;
sk += w * w;
}
const float qs = 1.0f / std::sqrt(sq + l2eps) * scale;
const float ks = 1.0f / std::sqrt(sk + l2eps);
for (int64_t d = 0; d < K; ++d) {
qn[d] = ldt(q, at3(qv, tok, hk, d)) * qs;
kn[d] = ldt(k, at3(kv, tok, hk, d)) * ks;
}
/* This token's factors, for the next step to replay: the normalised k once per
* 32-row group, v, then the decay exponent and beta once per group. */
if (anchor)
for (int c = 0; c < 3; ++c) {
if (c != 1 && !dup_f) continue;
const int64_t f = fbs[c] + t * al.fstr;
for (int64_t gi = 0; gi < al.groups; ++gi) {
for (int64_t d = 0; d < K; ++d) stt(state, f + gi * K + d, kn[d]);
stt(state, f + al.gb + 2 * gi, gv);
stt(state, f + al.gb + 2 * gi + 1, bt);
}
for (int64_t e = 0; e < V; ++e)
stt(state, f + al.vb + e, ldt(v, at3(vv, tok, h, e)));
}
if (tree) {
const int64_t p = tree_parent(par, bos, t);
std::copy(snap.begin() + (p + 1) * V * K, snap.begin() + (p + 2) * V * K, St);
}
if (tree && anchor) replay(fb + t * al.fstr);
else update(gv, bt, [&](int64_t e) { return ldt(v, at3(vv, tok, h, e)); }, 0, V);
if (tree) std::copy(St, St + V * K, snap.begin() + (t + 1) * V * K);
for (int64_t e = 0; e < V; ++e) orow[e] = 0.0f;
for (int64_t d = 0; d < K; ++d) {
const float* sr = St + d * V;
const float qd = qn[d];
for (int64_t e = 0; e < V; ++e) orow[e] += sr[e] * qd;
}
/* The gated RMS norm, over the head's V channels. */
float sq2 = 0.0f;
for (int64_t e = 0; e < V; ++e) sq2 += orow[e] * orow[e];
const float sc = 1.0f / std::sqrt(sq2 / (float)V + neps);
for (int64_t e = 0; e < V; ++e) {
float r = orow[e] * sc;
if (nw) r *= ldt(nw, e * laststride(nw));
if (z) {
const float zz = ldt(z, at3(zv, tok, h, e));
r *= (nact == 1 ? act_sigmoid(zz) : act_silu(zz));
}
stt(o, at3(ov, tok, h, e), r);
}
/* Quantise the stored `o`, not the f32 value, as the standalone quant_act_fp8 read it
* back from memory. One group per (token, head), since head_v is the fp8 block
* width. */
if (oq && osc) {
const int64_t qb = rowoff(oq, tok, Hv * V), qs = laststride(oq);
const float qsc = q8_group([&](int64_t e) { return ldt(o, at3(ov, tok, h, e)); },
V, 448.0f, oq, qb + h * V * qs, qs);
stt(osc, rowoff(osc, tok, Hv) + h * laststride(osc), qsc);
}
if (anchor) continue;
const int64_t so = sidx ? ld_int(sidx->dtype, sidx->data,
at2(si, n, t < sw ? t : sw - 1))
: n;
if (so >= 0)
for (int64_t e = 0; e < V; ++e)
for (int64_t d = 0; d < K; ++d)
stt(state, at4(stv, so, h, e, d), St[d * V + e]);
}
}
return RAD_OK;
}
int ref_gdn_recurrent_update(const RadArgs* a, RadStream) {
return recurrent_update_impl(a, nullptr);
}
/* gdn_recurrent_update over a tree verify (rad_drafter_tree): operand 16, `spec_parent`, names
* each row's parent within its sequence's rows, and a row's state is its parent's with the row
* applied (the committed state's for the block's first row). The factor records are written by
* row; the core moves the accepted path's to the front afterwards (ADV_COMPACT), so the next
* step's `num_accepted` names a prefix as it does after a chain. */
int ref_gdn_recurrent_update_tree(const RadArgs* a, RadStream) {
const RadTensor* par = a->n_t > 16 ? t_in(a, 16) : nullptr;
if (!par || !par->data) return RAD_E_INVAL;
return recurrent_update_impl(a, par);
}
/* ------------------------------------------------------------------ gdn_gated_rmsnorm */
/* out = x * rsqrt(mean_channels(x^2) + eps) * w * act(z), one row per (token, head): the prefill
* form of the epilogue the recurrent update folds in. */
/* Row offset of the gate. rowoff() handles [tokens, heads, ch] and flat [rows, ch], but not a
* column slice of a wider row (the fp8 delta net fuses [q|k|v|z] into one GEMM), whose last axis is
* heads*ch; rowoff would read it as a dense row. */
static inline int64_t gate_rowoff(const RadTensor* t, int64_t r, int64_t ch) {
if (t->rank == 2 && ch > 0 && t->shape[1] != ch && t->shape[1] % ch == 0) {
const int64_t heads = t->shape[1] / ch;
return (r / heads) * t->stride[0] + (r % heads) * ch * t->stride[1];
}
return rowoff(t, r, ch);
}
int ref_gdn_gated_rmsnorm(const RadArgs* a, RadStream) {
if (!have(a, 4)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *z = &a->t[1], *w = &a->t[2], *o = &a->t[3];
const int64_t ch = p_int(a, "channels", w->rank ? w->shape[w->rank - 1] : 0);
const float eps = p_f32(a, "eps", 1e-6f);
static const char* kActs[] = { "silu", "sigmoid" };
const int act = p_enum(a, "act", kActs, 2, 0);
if (act != 0 && act != 1) return RAD_E_INVAL;
if (ch <= 0) return RAD_E_SHAPE;
const int64_t rows = numel(x) / ch;
if (rows <= 0 || numel(o) < rows * ch || numel(z) < rows * ch || numel(w) < ch)
return RAD_E_SHAPE;
REF_PAR_FOR(rows * ch)
for (int64_t r = 0; r < rows; ++r) {
const int64_t xb = rowoff(x, r, ch), zb = gate_rowoff(z, r, ch), ob = rowoff(o, r, ch);
const int64_t xs = laststride(x), zs = laststride(z), os = laststride(o);
const int64_t ws = laststride(w);
float ss = 0.0f;
for (int64_t i = 0; i < ch; ++i) { float u = ldt(x, xb + i * xs); ss += u * u; }
const float sc = 1.0f / std::sqrt(ss / (float)ch + eps);
for (int64_t i = 0; i < ch; ++i) {
const float zz = ldt(z, zb + i * zs);
stt(o, ob + i * os, ldt(x, xb + i * xs) * sc * ldt(w, i * ws) *
(act == 1 ? act_sigmoid(zz) : act_silu(zz)));
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ gdn_conv_recurrent_update */
/* gdn_conv_update into staging q, k and v, then gdn_recurrent_update over them; the fused op is
* defined as the pair. Head geometry comes from `o` ([T, Hv, head_v]) and `x`
* (2 Hg head_k + Hv head_v columns). */
static int conv_recurrent_impl(const RadArgs* a, const RadTensor* par) {
if (a->n_t < 16) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *o = &a->t[15];
if (!x->data || !o->data || o->rank != 3 || x->rank < 2) return RAD_E_SHAPE;
const int64_t K = p_int(a, "head_k", 0), V = p_int(a, "head_v", 0);
const int64_t T = o->shape[0], Hv = o->shape[1];
if (K <= 0 || V <= 0 || T <= 0 || Hv <= 0 || o->shape[2] != V) return RAD_E_SHAPE;
const int64_t conv_dim = x->shape[x->rank - 1];
if (conv_dim <= Hv * V || (conv_dim - Hv * V) % (2 * K)) return RAD_E_SHAPE;
const int64_t Hg = (conv_dim - Hv * V) / (2 * K);
/* The staging takes x's dtype, so q, k and v round as the unfused pair's stores do. */
const auto bytes = [&](int64_t n) { return (size_t)rad_dtype_bytes(x->dtype, n); };
std::vector<uint8_t> qb(bytes(T * Hg * K)), kb(bytes(T * Hg * K)), vb(bytes(T * Hv * V));
auto stage = [&](uint8_t* d, int64_t h, int64_t w) {
RadTensor t{};
t.data = d; t.dtype = x->dtype; t.rank = 3;
t.shape[0] = T; t.shape[1] = h; t.shape[2] = w;
t.stride[0] = h * w; t.stride[1] = w; t.stride[2] = 1;
return t;
};
const RadTensor tq = stage(qb.data(), Hg, K), tk = stage(kb.data(), Hg, K),
tv = stage(vb.data(), Hv, V);
RadParam cp[4] = {};
const char* ck[4] = { "q_len", "head_k", "head_v", "conv_width" };
for (int i = 0; i < 4; ++i) {
const RadParam* f = rad_param_find(a->p, a->n_p, ck[i]);
if (!f) return RAD_E_INVAL;
cp[i] = *f;
}
/* x, w, b, conv_state, state_idx, num_accepted, cu, q, k, v */
RadTensor ct[10] = { a->t[0], a->t[1], a->t[2], a->t[3], a->t[4], a->t[11], a->t[12], tq, tk,
tv };
RadArgs ca = *a;
ca.t = ct; ca.n_t = 10; ca.p = cp; ca.n_p = 4;
int rc = conv_update_impl(&ca, par);
if (rc != RAD_OK) return rc;
/* q, k, v, a, b, A_log, dt_bias, state, state_idx, num_accepted, cu, z, norm_w, o, o_q,
* o_scale. The parameters pass as they are: the recurrence reads its own by name. */
RadTensor rt[16] = { tq, tk, tv, a->t[5], a->t[6], a->t[7], a->t[8], a->t[9], a->t[10],
a->t[11], a->t[12], a->t[13], a->t[14], a->t[15],
a->n_t > 16 ? a->t[16] : RadTensor{}, a->n_t > 17 ? a->t[17]
: RadTensor{} };
RadArgs ra = *a;
ra.t = rt; ra.n_t = 16;
return recurrent_update_impl(&ra, par);
}
int ref_gdn_conv_recurrent_update(const RadArgs* a, RadStream) {
return conv_recurrent_impl(a, nullptr);
}
/* gdn_conv_update_tree then gdn_recurrent_update_tree: operand 18 is `spec_parent`. */
int ref_gdn_conv_recurrent_update_tree(const RadArgs* a, RadStream) {
const RadTensor* par = a->n_t > 18 ? t_in(a, 18) : nullptr;
if (!par || !par->data) return RAD_E_INVAL;
return conv_recurrent_impl(a, par);
}