radiance 1.7.0

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

No files matched your search

+137 -287
View File
@@ -2,43 +2,34 @@
*
* ============================== WHAT IT SERVES ==============================
*
* `model_type: llama` with `LlamaForCausalLM`, which is the most widely shared decoder shape there
* is and is what MiniCPM5-2B reports: 42 layers, hidden 2048, 16 query heads over 2 KV heads at
* head_dim 128, SwiGLU at 6144, RMS norms, full rotary at theta 5e6, an untied 130560-row head,
* and no biases anywhere. Every layer is full attention; there is no gated delta net, no layer
* schedule, no output gate and no per-head q/k norm, which is precisely what separates this file
* from arch/qwen35_fp8.
* `model_type: llama` with `LlamaForCausalLM`, as MiniCPM5-2B reports: 42 layers, hidden 2048, 16
* query heads over 2 KV heads at head_dim 128, SwiGLU at 6144, RMS norms, full rotary at theta
* 5e6, an untied 130560-row head, and no biases. Every layer is full attention; unlike
* arch/qwen35_fp8 there is no gated delta net, layer schedule, output gate or per-head q/k norm.
*
* ============================== TWO CHECKPOINT FORMATS, ONE GRAPH ==============================
*
* The selection key is the PAIR (architecture, quantisation) -- spec §2.4 -- and one .so claims one
* The selection key is the (architecture, quantisation) pair (spec §2.4) and one .so claims one
* pair, so this source builds twice:
*
* llama_fp8 quant "fp8_e4m3" the checkpoint already holds E4M3 codes and a
* `weight_scale_inv` plane, which are the two planes of
* each linear as the engine reads them.
* llama_dense_fp8 quant "" the checkpoint is one dense bf16 tensor per linear, and
* a RECIPE quantises each to those two planes at convert
* time (data/recipes/minicpm5-2b-dspark.recipe). The
* container then holds what the first one's checkpoint
* does.
* llama_fp8 quant "fp8_e4m3" the checkpoint holds E4M3 codes and a `weight_scale_inv`
* plane per linear, as the engine reads them.
* llama_dense_fp8 quant "" the checkpoint is dense bf16, and a recipe quantises each
* linear to those two planes at convert time
* (data/recipes/minicpm5-2b-dspark.recipe: amax per 128x128
* block, scale = amax/448 rounded to bf16).
*
* THE SECOND IS THE ONE MiniCPM5-2B NEEDS, because OpenBMB publishes it in bf16 and nothing else.
* "MiniCPM5-2B FP8A8" is therefore a statement about what this engine RUNS and not about what the
* checkpoint holds, and the conversion is where the two meet: one pass per 128x128 block, amax,
* scale = amax/448 rounded to bf16, divide and encode. Both builds declare the same weights and
* the same ops, so nothing downstream -- the kernels, the container, the placement plan -- can
* tell which one ran; the descriptor is the only reason there are two.
* MiniCPM5-2B needs the second, since it is published only in bf16. Both builds declare the same
* weights and ops, so nothing downstream can tell which one ran.
*
* ============================== WHAT IS AND IS NOT FP8 ==============================
*
* E4M3 + a bf16 [N/128][K/128] plane self_attn.{q,k,v,o}_proj, mlp.{gate,up,down}_proj
* BF16, no plane every norm and the embedding
*
* The lm_head is declared at whatever the model holds it as: bf16 as every llama checkpoint ships
* it, or block fp8 where a recipe quantised it -- the widest GEMM in the model at half the bytes.
* The logits kernel that reads that encoding is the one selected. Both vocabulary edges are
* ROW-SHARDED with the vocabulary.
* The lm_head is declared at whatever the model holds: bf16 as llama checkpoints ship it, or block
* fp8 where a recipe quantised it (the widest GEMM at half the bytes), and the logits kernel for
* that encoding is selected. Both vocabulary edges are row-sharded with the vocabulary.
*/
#ifndef LLAMA_DENSE_SRC
#define LLAMA_DENSE_SRC 0
@@ -84,22 +75,21 @@ struct Model {
rad_buf b_gate_up = 0;
ActFP8 a_ffn{};
/* THE AUXILIARY HIDDEN-STATE TAPS, declared only when the container carries a drafter that
* asks for them. A DSpark or DFlash drafter does not read the trunk's FINAL hidden state --
* it reads the RESIDUAL STREAM at several intermediate layers and projects the concatenation
* down, so its `fc` is [n_embd, n_taps * n_embd] and the input has to be one contiguous row.
* The trunk keeps one residual buffer that every layer overwrites, so the rows are copied out
* as they pass, into the column slice that tap owns. This is arch/qwen35_fp8's mechanism and
* the comments there carry the reasoning; what is here is the same thing over this graph. */
/* Auxiliary hidden-state taps, declared only when the container carries a drafter that wants
* them. A DSpark or DFlash drafter reads the residual stream at several intermediate layers
* and projects the concatenation down (`fc` is [n_embd, n_taps * n_embd]), so the input must
* be one contiguous row. The trunk's one residual buffer is overwritten by every layer, so
* each tap's rows are copied into its column slice as they pass. Same mechanism as
* arch/qwen35_fp8. */
enum { MAX_TAPS = 8 };
int n_taps = 0;
int tap_layer[MAX_TAPS] = {};
rad_buf b_dfsrc = 0; /* [max_tok, n_taps * n_embd] */
rad_op op_tap = 0;
/* THE DRAFTER, declared only when the container carries one and this serve intends to
* speculate. Its layers get absolute layer indices AFTER the trunk's, so the KV group's
* layer_slot lookup finds them and its weights land in layer groups of their own. */
/* The drafter, declared only when the container carries one and this serve speculates. Its
* layers get absolute indices after the trunk's, so the KV group's layer_slot lookup finds
* them and its weights land in layer groups of their own. */
Dspark ds{};
Dspark::Wire dsw{};
rad_kvgroup kv_draft = 0;
@@ -111,44 +101,24 @@ struct Model {
std::vector<Layer> layers;
};
/* "1 10 20 30 39" -> the layers whose output a drafter wants. rad-convert flattens a config.json
* array into a space-separated string (tools/rad_convert.cpp, flatten_json), so this is the shape
* every list-valued key arrives in. Out-of-range entries are DROPPED rather than clamped: a
* drafter naming a layer this model does not have is paired with the wrong target, and silently
* reading layer 41 instead of 49 would turn that into a quality mystery. */
static int parse_tap_layers(const char* s, int64_t n_layer, int* out, int max) {
if (!s) return 0;
int n = 0;
for (const char* p = s; *p && n < max;) {
while (*p == ' ' || *p == ',') ++p;
if (!*p) break;
char* e = nullptr;
const long v = std::strtol(p, &e, 10);
if (e == p) break;
p = e;
if (v >= 0 && v < n_layer) out[n++] = (int)v;
}
return n;
}
static Model g_model[MAX_RANKS];
/* THE CONTAINER'S QUANTISATION DESCRIPTOR IS THE SELECTION KEY AND IT IS CHECKED ANYWAY, because
* `--arch` can override the architecture id at convert time and a plugin that is handed the wrong
* checkpoint should say which half disagrees rather than fail at a name map. */
/* The quantisation descriptor is the selection key but is checked anyway: `--arch` can override
* the architecture id at convert time, and a plugin handed the wrong checkpoint should say which
* half disagrees rather than fail at a name map. */
static int check_format(const RadModelMeta* meta) {
const char* q = meta->quant ? meta->quant : "";
#if LLAMA_DENSE_SRC
if (*q) {
fprintf(stderr, "radiance: llama_dense_fp8 serves an UNQUANTISED checkpoint and this one "
"declares '%s'. The fp8 sibling (llama_fp8) reads that one.\n", q);
fprintf(stderr, "radiance: llama_dense_fp8 serves an unquantised checkpoint and this one "
"declares '%s'; llama_fp8 serves it\n", q);
return RAD_E_UNSUPPORTED;
}
#else
if (std::strncmp(q, "fp8", 3) != 0) {
fprintf(stderr, "radiance: llama_fp8 serves a block-scaled fp8 checkpoint and this one "
"declares '%s'. A dense bf16 one is served by llama_dense_fp8, after a "
"recipe has quantised it.\n", *q ? q : "(unquantised)");
"declares '%s'; llama_dense_fp8 serves a dense bf16 one\n",
*q ? q : "(unquantised)");
return RAD_E_UNSUPPORTED;
}
#endif
@@ -160,62 +130,23 @@ static int check_format(const RadModelMeta* meta) {
return RAD_OK;
}
/* THE DSPARK DRAFTER. The Geom is the TRUNK'S with the drafter's own geometry substituted in: the
* vocabulary, the deployment bounds, the rank and the wire stay the trunk's (they are properties
* of this serve, not of the checkpoint) and everything about the layers comes from `draft.*`.
/* The DSpark drafter. Its Geom is the trunk's with the drafter's layer geometry from `draft.*`
* substituted; the vocabulary, deployment bounds, rank and wire stay the trunk's.
*
* Nothing here is defaulted quietly. A container whose `draft.` config does not describe this
* drafter, or describes one this file does not implement, is refused BY NAME: a drafter that
* silently ignored a scale factor or a head type would simply accept less, which reads as a
* quality mystery and not as a missing feature (spec §17).
* Nothing is defaulted quietly: a `draft.` config that does not describe a drafter this file
* implements is refused by name, because a silently ignored scale factor or head type would only
* lower acceptance (spec §17).
*
* THE ONE THING THAT IS DELIBERATELY IGNORED is the confidence head, and rad_block_dspark.h argues
* it: `AcceptRatePredictor` exists so a serving loop can shorten the block adaptively, this
* engine's depth is fixed at declare time, and the head changes nothing the drafter proposes. */
* The confidence head (`AcceptRatePredictor`) is deliberately ignored: it drives adaptive block
* length, this engine's depth is fixed at declare, and it changes nothing the drafter proposes
* (rad_block_dspark.h). */
static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx,
Model& m, int base_layer) {
Names& nm = m.nm;
Geom dg = m.g; /* n_embd, the vocabulary, the deployment bounds, the rank, the wire */
dg.n_layer = rad_meta_geti(meta, "draft.num_hidden_layers", 0);
dg.head_dim = rad_meta_geti(meta, "draft.head_dim", 0);
dg.n_head_all = rad_meta_geti(meta, "draft.num_attention_heads", 0);
dg.n_head_kv_all = rad_meta_geti(meta, "draft.num_key_value_heads", 0);
dg.n_ff_all = rad_meta_geti(meta, "draft.intermediate_size", 0);
dg.eps = (float)rad_meta_getf(meta, "draft.rms_norm_eps", 0.0);
dg.theta = (float)rad_meta_getf(meta, "draft.rope_parameters.rope_theta",
rad_meta_getf(meta, "draft.rope_theta", 0.0));
/* PLAIN RMS NORM: the reference norms with Qwen3RMSNorm and the config says `model_type:
* qwen3`. It happens to agree with this MiniCPM5 target, and it is asked of the DRAFTER's
* config anyway -- two conventions in one process is exactly why this is a second Geom. */
dg.wadd = 0.0f;
dg.wadd_qk = 0.0f;
if (dg.n_layer <= 0 || dg.head_dim <= 0 || dg.n_head_all <= 0 || dg.n_head_kv_all <= 0 ||
dg.n_ff_all <= 0 || !(dg.eps > 0.0f) || !(dg.theta > 0.0f)) {
fprintf(stderr, "radiance: the container's `draft.` config does not describe a DSpark "
"drafter: layers %lld, heads %lld / kv %lld, head_dim %lld, ffn %lld, "
"rms_eps %g, rope_theta %g\n",
(long long)dg.n_layer, (long long)dg.n_head_all, (long long)dg.n_head_kv_all,
(long long)dg.head_dim, (long long)dg.n_ff_all, (double)dg.eps, (double)dg.theta);
return RAD_E_INVAL;
}
RAD_ARCH_TRY(yarn_from(dg, meta, "draft.", dg.head_dim));
int s;
if ((s = divide_or_fail(dg.n_head_all, dg.world, "draft num_attention_heads", &dg.n_head)) < 0) return s;
if ((s = divide_or_fail(dg.n_head_kv_all, dg.world, "draft num_key_value_heads", &dg.n_head_kv)) < 0) return s;
if ((s = divide_or_fail(dg.n_ff_all, dg.world, "draft intermediate_size", &dg.n_ff)) < 0) return s;
/* THE DRAFTER BORROWS THE TARGET'S EMBEDDING TABLE AND lm_head -- it ships neither -- so a
* vocabulary that disagrees is not a drafter for this target at all. */
const long long dvocab = rad_meta_geti(meta, "draft.vocab_size", dg.n_vocab_all);
if (dvocab != dg.n_vocab_all) {
fprintf(stderr, "radiance: the DSpark drafter claims a %lld-token vocabulary and the "
"target has %lld. The drafter ships no embedding table and no lm_head; it "
"reads the target's, so the two have to be the same vocabulary.\n",
dvocab, (long long)dg.n_vocab_all);
return RAD_E_UNSUPPORTED;
}
Geom dg{};
/* Plain RMS norm: the reference uses Qwen3RMSNorm (`model_type: qwen3`). It matches this
* MiniCPM5 target, but it is the drafter's convention, set on the drafter's own Geom. */
RAD_ARCH_TRY(drafter_geom(dg, m.g, meta, "DSpark"));
Dspark::Config dc{};
dc.n_taps = m.n_taps;
@@ -223,11 +154,10 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil
dc.causal = rad_meta_geti(meta, "draft.is_causal", 0) ? 1 : 0;
if (rad_meta_geti(meta, "draft.use_sliding_window", 0))
dc.window = rad_meta_geti(meta, "draft.sliding_window", 0);
/* HOW MANY CANDIDATES THE MARKOV WALK RE-RANKS. The reference biases all 130560 logits and
* takes the argmax; this re-ranks the base head's top K, which is the only approximation in
* the head and it can cost acceptance and nothing else (rad_block_dspark.h). K is bounded by
* the selector at 32; 16 is 8 KiB of codebook a position, which is free, and it is read from
* the container so a checkpoint can ask for more. */
/* How many candidates the Markov walk re-ranks. The reference biases all 130560 logits and
* takes the argmax; this re-ranks the base head's top K, the head's only approximation, which
* can cost acceptance but not correctness (rad_block_dspark.h). The selector bounds K at 32;
* the default 16 is 8 KiB of codebook per position, and a checkpoint may ask for more. */
dc.top_k = rad_meta_geti(meta, "draft.markov_top_k", 16);
if (dc.top_k < 1 || dc.top_k > 32) {
fprintf(stderr, "radiance: draft.markov_top_k is %lld; the Markov walk scores at most 32 "
@@ -235,15 +165,14 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil
return RAD_E_UNSUPPORTED;
}
/* THE BLOCK IS THE NUMBER OF NOISE ROWS AND ALSO THE NUMBER OF DRAFTS -- every row predicts
* the token after it, the anchor row included. So the engine's depth IS `block_size`, where a
* Dflash2 container's is `block_size - 1`, and that off-by-one is the whole reason
* RadDrafterDecl carries a `block` field. Getting it wrong runs, drafts, and accepts nothing.
/* The block is both the number of noise rows and the number of drafts: every row, the anchor
* included, predicts the token after it. So the depth is `block_size`, where a DFlash2
* container's is `block_size - 1` (see RadDrafterDecl::block). A wrong value runs but accepts
* nothing.
*
* A BLOCK-DIFFUSION DRAFTER WAS TRAINED FOR ONE BLOCK LENGTH, so a serve that asks for another
* depth is refused here rather than run: the reference denoises exactly `block_size` masked
* positions and there is no partial block. The probe reports `draft_depth_fixed`, so the
* engine will normally have picked this already. */
* A block-diffusion drafter is trained for one block length and has no partial block, so any
* other depth is refused. The probe reports `draft_depth_fixed`, so the engine normally picks
* this already. */
const long long trained = rad_meta_geti(meta, "draft.block_size", 0);
if (trained < 2) {
fprintf(stderr, "radiance: this DSpark checkpoint declares no draft.block_size; the "
@@ -260,8 +189,8 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil
}
dc.block = trained;
/* Two things this drafter's family can carry that this implementation does not apply. Each
* changes what the drafter proposes, which is why neither is ignored quietly. */
/* Two variants this implementation does not apply; each changes the proposals, so neither is
* ignored quietly. */
const char* mh = rad_meta_gets(meta, "draft.markov_head_type", "vanilla");
if (dc.rank <= 0 || !mh || std::strcmp(mh, "vanilla") != 0) {
fprintf(stderr, "radiance: this DSpark checkpoint has markov_rank %lld and "
@@ -276,26 +205,12 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil
return RAD_E_UNSUPPORTED;
}
/* THE DRAFTER'S OWN KV CACHE, a group of its own. It cannot share the trunk's: five layers
* against forty-two, and -- when the checkpoint asks for one -- a WINDOW. RAD_KV_WINDOW
* recycles the blocks that fall below it; DSpark's published config sets `sliding_window:
* null`, so this is normally a full cache of five layers. Its width is the trunk's,
* --kv-cache-dtype: one flag says what every attention cache of the deployment is made of. */
RadKVGroupDecl dd{};
dd.kind = dc.window > 0 ? RAD_KV_WINDOW : RAD_KV_FULL;
dd.dtype = rad_kv_cache_dtype(ctx, RAD_BF16);
dd.window = dc.window;
dd.n_head_kv = dg.n_head_kv;
dd.head_dim = dg.head_dim;
m.kv_draft = rad_decl_kv_group(b, nm.f("kv_draft"), &dd);
for (int64_t l = 0; l < dg.n_layer; ++l)
RAD_ARCH_TRY(rad_bind_layer_kv(b, base_layer + (int)l, m.kv_draft));
dg.kv_dtype = rad_dtype_name(dd.dtype);
/* DSpark's published config sets `sliding_window: null`, so this is normally a full cache. */
RAD_ARCH_TRY(drafter_kv_group(b, nm, ctx, dg, dc.window, base_layer, &m.kv_draft));
/* Activation buffers. The context pass is one row a COMMITTED TOKEN and runs over a whole
* prefill chunk; everything after it is one row a DRAFT POSITION, `block` of them a sequence,
* so the two halves are sized apart. None of it is large -- the drafter's cost is its
* weights, not its activations. */
/* Activations. The context pass has one row per committed token over a whole prefill chunk;
* the query pass one row per draft position, `block` per sequence; the two are sized apart.
* All are small: the drafter's cost is its weights. */
const int64_t qd = dg.n_head * dg.head_dim;
const int64_t kvd = dg.n_head_kv * dg.head_dim;
int64_t qr = dg.max_seqs * dc.block;
@@ -308,9 +223,8 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil
dw.c = decl_b(b, nm.f("ds_c"), dg.act_dtype, {dg.max_tok, dg.n_embd});
dw.ck = decl_b(b, nm.f("ds_ck"), dg.act_dtype, {dg.max_tok, kvd});
dw.cv = decl_b(b, nm.f("ds_cv"), dg.act_dtype, {dg.max_tok, kvd});
/* THE FP8 TWINS OF THE CONTEXT PASS. `srcq` is the largest of them by a distance -- 20 MiB at
* a 2048-token chunk, five taps and hidden 2048, against the 40 MiB `draft_src` it shadows --
* and it buys half the bytes of the one GEMM the context pass spends its time in. */
/* FP8 copies for the context pass. `srcq` is the largest (20 MiB at a 2048-token chunk, five
* taps, hidden 2048) and halves the bytes of the context pass's dominant GEMM. */
dw.srcq = decl_b(b, nm.f("ds_srcq"), RAD_F8E4M3, {dg.max_tok, dc.n_taps * dg.n_embd});
dw.srcs = decl_b(b, nm.f("ds_srcs"), RAD_F32,
{dg.max_tok, fp8_blocks(dc.n_taps * dg.n_embd)});
@@ -335,12 +249,11 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil
dw.sel_h = decl_b(b, nm.f("ds_selh"), dg.act_dtype, {sr, dg.n_embd});
dw.dq = decl_b(b, nm.f("ds_dq"), RAD_I8, {sr, dg.n_embd});
dw.ds = decl_b(b, nm.f("ds_ds"), RAD_F32, {sr, 1});
/* GROUP-major, [n_embd/group][ceil(M/16)*16] -- quant_act_i8's asum plane, whose first
* dimension is the K group and not the token. */
/* Group-major, [n_embd/group][ceil(M/16)*16]: quant_act_i8's asum plane, whose first
* dimension is the K group, not the token. */
dw.dsum = decl_b(b, nm.f("ds_dsum"), RAD_F32, {dg.n_embd / 128, spad});
/* THIS RANK'S plane. `head_rows` is the block's own answer to whether the 2-bit head is
* vocab-sharded, asked here rather than re-derived, because a buffer that disagreed with the
* op declared over it is a read past its end. */
/* This rank's plane. `head_rows` is the block's own answer to whether the 2-bit head is
* vocab-sharded; asking it keeps the buffer consistent with the op declared over it. */
dw.dlog = decl_b(b, nm.f("ds_dlog"), dg.act_dtype, {sr, Dspark::head_rows(dg)});
dw.cand = decl_b(b, nm.f("dspark_cand"), RAD_I32, {sr, dc.top_k});
dw.unary = decl_b(b, nm.f("ds_unary"), RAD_F32, {sr, dc.top_k});
@@ -348,23 +261,21 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil
dw.dpair = decl_b(b, nm.f("ds_dpair"), RAD_F32, {sr, dc.top_k, 2});
dw.dgath = decl_b(b, nm.f("ds_dgath"), RAD_F32, {sr, (int64_t)dg.world * dc.top_k, 2});
}
/* NAMED, because the engine reads the draft out of it by name: a block pass proposes through
* the drafter's own walk and never through the sampler's token buffer. */
/* The proposal buffer (RadDrafterDecl::proposal): a block pass proposes through the
* drafter's own walk, never through the sampler's token buffer. */
dw.tokens = decl_b(b, nm.f("dspark_tokens"), RAD_I32, {dg.max_seqs, dc.steps()});
/* A TIED lm_head IS THE ALTERNATIVE SOURCE, and it has to be the same pair the trunk's own
* head reads or the drafter would be projecting onto a different vocabulary. */
/* The same lm_head sources as the trunk's head (embedding as the tied alternative), so both
* project onto one vocabulary. */
RAD_ARCH_TRY(m.ds.declare(b, nm, dg, dc, base_layer, m.kv_draft, m.w_tok, "draft",
nm.ckpt("lm_head.weight"), nm.ckpt("model.embed_tokens.weight"),
dw));
m.have_ds = true;
/* WHAT THE ENGINE DRIVES. Block: a context pass that mirrors the trunk step, then ONE pass
* that fills the whole block. `block` is `depth` and not `depth + 1`, which is this drafter's
* trained convention and the field's reason for existing. `window` must be the number this
* plugin gated its OWN attention with, because the scheduler stages the block pass against
* it: two readings of "how far back can the drafter see" is a drafter attending to positions
* the scheduler did not stage. */
/* Block drafter: a context pass mirroring the trunk step, then one pass filling the whole
* block. `block` is `depth`, not `depth + 1` (this drafter's training convention). `window`
* must be the one this plugin gated its own attention with, since the scheduler stages the
* block pass against it. */
RadDrafterDecl drf{};
drf.name = "dspark";
drf.kind = RAD_DRAFT_BLOCK;
@@ -377,27 +288,22 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil
if (drf.mask_token < 0)
drf.mask_token = (int32_t)rad_meta_geti(meta, "draft.dflash_config.mask_token_id", -1);
if (drf.mask_token < 0) {
fprintf(stderr, "radiance: this DSpark checkpoint declares no mask_token_id, and every "
"position the block has not filled embeds it -- there is nothing to "
"run.\n");
fprintf(stderr, "radiance: this DSpark checkpoint declares no mask_token_id, which every "
"unfilled block position embeds\n");
return RAD_E_FORMAT;
}
RAD_ARCH_TRY(rad_declare_drafter(b, &drf));
rad_note(b, "the DSpark drafter is declared: %lld layers of its own at absolute %d..%d, "
"%lld heads / %lld kv x %lld, ffn %lld, %s attention with a %lld-token window, "
"reading the trunk at %d tap(s). ONE forward over %lld noise row(s) proposes "
"%lld tokens -- there are no rounds -- and EVERY row predicts, the anchor "
"included, so the block is `depth` rows and not `depth + 1`",
rad_note(b, "DSpark drafter: %lld layers at absolute %d..%d, %lld heads / %lld kv x %lld, "
"ffn %lld, %s attention with a %lld-token window, %d trunk tap(s); one forward "
"over %lld noise row(s), the anchor included, proposes %lld tokens",
(long long)dg.n_layer, base_layer, base_layer + (int)dg.n_layer - 1,
(long long)dg.n_head, (long long)dg.n_head_kv, (long long)dg.head_dim,
(long long)dg.n_ff, dc.causal ? "causal" : "non-causal", (long long)dc.window,
m.n_taps, (long long)dc.block, (long long)dc.steps());
rad_note(b, "the rank-%lld Markov bigram head runs as the candidate walk with no hidden "
"projection: `unary + <markov_w1[prev], markov_w2[cand]>` over the draft head's "
"top %lld, decoded greedily. The confidence head is present in the checkpoint and "
"deliberately not applied -- it drives adaptive block length, which this engine "
"does not have, and it changes nothing the drafter proposes",
rad_note(b, "the rank-%lld Markov bigram head walks `unary + <markov_w1[prev], "
"markov_w2[cand]>` over the draft head's top %lld, greedily; the confidence head "
"(adaptive block length) is not applied",
(long long)dc.rank, (long long)dc.top_k);
return RAD_OK;
}
@@ -410,17 +316,15 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
m = Model{};
m.nm = Names(ctx->scope);
RAD_ARCH_TRY(check_format(meta));
/* "bf16" is the ACTIVATION dtype: everything but the two GEMM operands runs at it. */
/* "bf16" is the activation dtype: everything but the two GEMM operands runs at it. */
RAD_ARCH_TRY(geom_from(m.g, meta, ctx, "bf16", RAD_BF16));
Geom& g = m.g;
Names& nm = m.nm;
m.n_main = g.n_layer;
/* THE ROTARY WIDTH IS THE WHOLE HEAD unless the config says otherwise. LLaMA rotates every
* dimension; `partial_rotary_factor` exists in this config schema and Phi-style models set it,
* so it is read rather than assumed -- but the DEFAULT is the family's answer and not a
* refusal, because a checkpoint that states nothing is stating "all of it". */
/* The rotary width is the whole head unless `partial_rotary_factor` says otherwise (Phi-style
* models set it). A config that states nothing means the full head. */
{
double prf = rad_meta_getf(meta, "partial_rotary_factor", 0.0);
if (!(prf > 0.0)) prf = rad_meta_getf(meta, "rope_parameters.partial_rotary_factor", 0.0);
@@ -456,22 +360,19 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_ARCH_TRY(m.a_ffn.declare_qs(b, nm, g, "ffn", g.n_ff));
m.b_hout = decl_b(b, nm.f("h_out"), g.act_dtype, {g.max_logit_rows, g.n_embd});
/* ---- the drafter's taps. Gated on BOTH halves of the question: the container has to carry a
* drafter that reads them, and the deployment has to intend to speculate. The buffer is one
* residual stream per tap -- 40 MiB at a 2048-token chunk, five taps and hidden 2048 -- which
* a server that never drafts should not be carrying.
/* ---- the drafter's taps, only when the container carries a drafter that reads them and the
* deployment speculates: the buffer is one residual stream per tap, 40 MiB at a 2048-token
* chunk with five taps and hidden 2048.
*
* TWO KEY SPELLINGS, because the two checkpoints spell it differently and both are real. A
* SpecForge DFlash config nests the drafter's wiring under `dflash_config`; the published
* DSpark configs (openbmb/MiniCPM5-2B-DSpark and its siblings) put the same fields at the top
* level. Reading the flat one first and falling back keeps one plugin serving both rather
* than making the spelling an architecture. */
* Two key spellings: a SpecForge DFlash config nests the wiring under `dflash_config`, while
* the published DSpark configs (openbmb/MiniCPM5-2B-DSpark and siblings) put it at the top
* level. The flat key is read first. */
if (ctx->max_spec > 0) {
const char* da = rad_meta_gets(meta, "draft.architectures", "");
if (da && (std::strstr(da, "DSpark") || std::strstr(da, "DFlash"))) {
const char* ids = rad_meta_gets(meta, "draft.target_layer_ids", nullptr);
if (!ids) ids = rad_meta_gets(meta, "draft.dflash_config.target_layer_ids", nullptr);
m.n_taps = parse_tap_layers(ids, m.n_main, m.tap_layer, Model::MAX_TAPS);
m.n_taps = parse_layer_ids(ids, m.n_main, m.tap_layer, Model::MAX_TAPS);
if (m.n_taps > 0) {
m.b_dfsrc = decl_b(b, nm.f("draft_src"), g.act_dtype,
{g.max_tok, (int64_t)m.n_taps * g.n_embd});
@@ -493,11 +394,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
m.b_logits = decl_b(b, nm.f("logits"), RAD_F32, {g.max_logit_rows, g.n_vocab});
RAD_ARCH_TRY(rad_declare_logits(b, m.b_logits));
/* ---- the vocabulary edges, both ROW-SHARDED with the vocabulary. A tied head is expressed by
* the alternative source: a model that ships no `lm_head.weight` reads the embedding table as
* both, which is what every small LLaMA derivative does. The head is declared at its codes'
* dtype -- bf16, or E4M3 where a recipe made it block fp8 -- after its map, which is how the
* model is asked. */
/* ---- the vocabulary edges, both row-sharded with the vocabulary. A tied head is the
* alternative source: without `lm_head.weight` the embedding table serves as both, as in small
* LLaMA derivatives. The head is declared at its codes' dtype (bf16, or E4M3 where a recipe
* made it block fp8), queried after its name map entry. */
RAD_ARCH_TRY(map_copy(b, nm.f("token_embd.weight"), nm.ckpt("model.embed_tokens.weight")));
RAD_ARCH_TRY(map_copy(b, nm.f("output_norm.weight"), nm.ckpt("model.norm.weight")));
RAD_ARCH_TRY(map_copy_alt(b, nm.f("output.weight"), nm.ckpt("lm_head.weight"),
@@ -516,9 +416,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_INT("vocab_offset", g.vocab_off)),
RAD_WEIGHTS(m.w_tok)),
{}, {m.b_x});
/* EXACT, unconditionally: each token's embedding comes from exactly one rank and every other
* contributes a zero, so a lossy wire would be pure loss on a value that is already the
* answer. The same argument arch/qwen35_fp8 makes about its own. */
/* Always exact: each token's embedding comes from one rank and the others contribute zero, so
* a lossy wire would only add error. */
if (g.world > 1)
m.op_embed_ar = rw(b, RAD_OP(b, "all_reduce",
RAD_PARAMS(RAD_INT("world_size", g.world),
@@ -532,37 +431,11 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
MlpFP8::Wire mw{};
mw.x = m.b_x; mw.h = m.a_h; mw.gate_up = m.b_gate_up; mw.ffn = m.a_ffn;
auto is_tap = [&](int64_t l) {
for (int t = 0; t < m.n_taps; ++t)
if (m.tap_layer[t] == (int)l) return true;
return false;
};
m.layers.resize((size_t)m.n_main);
for (int64_t l = 0; l < m.n_main; ++l) {
Layer& lay = m.layers[(size_t)l];
/* THE RESIDUAL FOLD, and the rule is arch/qwen35_fp8's with exactly one of its exceptions:
* there are no ablation switches here, so every block folds its predecessor's delta into
* its own norm and the LAST layer's feed-forward keeps its own add -- for the final norm,
* which is a plain `rmsnorm` with no residual operand.
*
* A TAP IS THE OTHER READER OF `x` BETWEEN BLOCKS, and it forces the add to stay where it
* was. What the drafter conditions on has to be that layer's REAL output, so a tap layer's
* feed-forward keeps its own add and the block after it has nothing left to fold. Get this
* wrong and the taps carry a residual stream missing its last delta -- a drafter that
* still runs, still proposes, and is quietly conditioned on the wrong thing.
*
* `ar_in`/`ar_out` move the collective with the add, one boundary at a time, at two ranks
* only: at tp1 there is none and at four the fused kernel does not exist. They need no
* change for the taps -- a tap layer's MLP has `add`, so it does not defer its all-reduce,
* and the next mixer does not fold, so it looks for none. The two stay consistent because
* both are derived from the same two flags. */
const bool tp2 = g.world == 2;
const bool mix_fold = l > 0 && !is_tap(l - 1);
const bool mlp_add = is_tap(l) || (l + 1 >= m.n_main);
const int mix_ar_in = !tp2 ? kArNone
: mix_fold ? kArAny
: (l == 0 && m.op_embed_ar) ? kArExact : kArNone;
/* Residual fold and collective placement: rad_fp8.h, layer_fold. */
const LayerFold f = layer_fold(g, l, m.n_main, m.tap_layer, m.n_taps, m.op_embed_ar != 0);
const char* pre = "model.layers";
AttnFP8::Src as{};
@@ -573,15 +446,15 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
as.o = nm.ckpt("%s.%lld.self_attn.o_proj.weight", pre, (long long)l);
RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, m.kv_full));
RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, m.kv_full, m.n_rot, aw, as,
mix_fold, /*add_out=*/false, mix_ar_in, tp2));
f.mix_fold, false, f.mix_ar_in, f.mix_ar_out));
MlpFP8::Src ms{};
ms.norm = nm.ckpt("%s.%lld.post_attention_layernorm.weight", pre, (long long)l);
ms.gate = nm.ckpt("%s.%lld.mlp.gate_proj.weight", pre, (long long)l);
ms.up = nm.ckpt("%s.%lld.mlp.up_proj.weight", pre, (long long)l);
ms.down = nm.ckpt("%s.%lld.mlp.down_proj.weight", pre, (long long)l);
RAD_ARCH_TRY(lay.mlp.declare(b, nm, g, (int)l, mw, ms, /*fold=*/true, mlp_add,
tp2 ? kArAny : kArNone, !mlp_add && tp2));
RAD_ARCH_TRY(lay.mlp.declare(b, nm, g, (int)l, mw, ms, true, f.mlp_add,
f.mlp_ar_in, f.mlp_ar_out));
}
m.op_final_norm = rw(b, RAD_OP(b, "rmsnorm",
@@ -602,27 +475,18 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_WEIGHTS(m.w_lm_head)),
{m.b_hout}, {m.b_logits});
/* THE DRAFTER IS DECLARED LAST, AFTER `op_logits`, AND THE ORDER IS LOAD-BEARING.
/* The drafter is declared last, after `op_logits`. Do not move it.
*
* DECLARATION ORDER IS WHAT THE BUFFER PLANNER'S LIVENESS IS: a transient's range is
* [first_def, last_use] over OP INDICES, and two buffers whose ranges are disjoint may share
* bytes (core/build/rad_bufplan.cpp). The drafter's passes run AFTER the whole trunk pass, so
* declaring them here is the order that says so. Declared in the MIDDLE -- before
* `op_final_norm`, say -- the trunk's own tail ops get indices past the drafter's, the planner
* reads that as "these live after the drafter is done", and it shares bytes between a trunk
* buffer and a drafter buffer that are both live inside one engine step.
* Declaration order is the buffer planner's liveness: a transient lives over [first_def,
* last_use] in op indices, and buffers with disjoint ranges may share bytes
* (core/build/rad_bufplan.cpp). The drafter's passes run after the whole trunk, so they must
* be declared after it; declared earlier, the trunk's tail ops would look live after the
* drafter, and trunk and drafter buffers live in one step would share bytes.
*
* THE SYMPTOM OF GETTING IT WRONG IS NOT A CRASH: the trunk's output stays fluent, because row
* 0 of a verify step is computed before the clobber lands, and EVERY DRAFT IS REJECTED, because
* rows 1..n_spec are not. Speculation looks broken and the drafter looks wrong while it is
* proposing exactly the right tokens. It is the one class of fault the op oracle cannot see: a
* buffer clobbered before an op runs makes the device and the host compute the same wrong
* answer and agree.
*
* The same declaration can be correct at one world size and wrong at another: the shard
* halves every sharded buffer and the greedy packer's offsets are a function of the sizes.
* arch/qwen35_fp8 declares its drafter here for the same reason; this is not a stylistic
* match. */
* The failure is silent: the trunk output stays correct (row 0 of a verify step is computed
* before the clobber) but every draft is rejected. The op oracle cannot see it, since device
* and host compute the same wrong answer from a clobbered input. It can also depend on world
* size, because sharding changes buffer sizes and so the packer's offsets. */
if (m.n_taps > 0)
RAD_ARCH_TRY(declare_dspark(b, meta, ctx, m, (int)m.n_main));
@@ -632,51 +496,41 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
(long long)g.head_dim, (long long)m.n_rot, (long long)g.n_ff,
(long long)g.vocab_off, (long long)(g.vocab_off + g.n_vocab));
#if LLAMA_DENSE_SRC
rad_note(b, "the checkpoint is DENSE bf16 and its recipe quantised every linear to E4M3 with "
"a bf16 [N/%d,K/%d] scale plane -- one amax per block, scale = amax/448 rounded "
"to bf16. The container is half the checkpoint's size and the graph is the one "
"llama_fp8 declares over a checkpoint that shipped that way",
rad_note(b, "a dense bf16 checkpoint, every linear quantised by the recipe to E4M3 with a "
"bf16 [N/%d,K/%d] scale plane (scale = block amax / 448, rounded to bf16)",
RAD_FP8_BLOCK, RAD_FP8_BLOCK);
#else
rad_note(b, "weights are block-scaled fp8: E4M3 [N,K] with a bf16 [N/%d,K/%d] scale plane, "
"one stored weight taken by gemm_nt_q as two operands",
RAD_FP8_BLOCK, RAD_FP8_BLOCK);
#endif
rad_note(b, "NOT fp8 in the checkpoint: every norm, the embedding and the lm_head. The head "
"is declared at what the model holds -- block fp8 where a recipe made it so");
rad_note(b, "bf16 in the checkpoint: every norm, the embedding and the lm_head, which is "
"declared at what the container holds");
if (m.n_taps > 0) {
char ids[64] = {};
int off = 0;
for (int t = 0; t < m.n_taps && off < (int)sizeof(ids) - 8; ++t)
off += snprintf(ids + off, sizeof(ids) - (size_t)off, t ? " %d" : "%d",
m.tap_layer[t]);
rad_note(b, "the drafter reads the residual stream at layer(s) %s into a [%lld, %lld] "
"buffer -- %lld tap(s) x %lld. Those layers keep their own residual add and "
"the layer after each one has nothing to fold, so the tap sees the real "
"output rather than a stream still missing its last delta",
ids, (long long)g.max_tok, (long long)((int64_t)m.n_taps * g.n_embd),
(long long)m.n_taps, (long long)g.n_embd);
rad_note(b, "the drafter reads the residual stream after layer(s) %s into a [%lld, %lld] "
"buffer; those layers keep their own residual add",
ids, (long long)g.max_tok, (long long)((int64_t)m.n_taps * g.n_embd));
}
rad_note(b, "q, k and v are ONE fused [%lld, %lld] projection and `rope` rotates the fused "
"row in place -- q heads then k heads, v untouched. There is no per-head q/k norm "
"and no output gate in this family; qwen35 has both and that is the whole "
"difference between the two attention blocks",
rad_note(b, "q, k and v are one fused [%lld, %lld] projection, rotated in place by `rope` "
"(q heads, then k heads); no per-head q/k norm and no output gate",
(long long)(q_dim + 2 * kv_dim), (long long)g.n_embd);
return RAD_OK;
}
static void step(RadCtx* c, const RadBatch* batch) {
Model& m = g_model[rad_rank(c)];
/* A DRAFTER PASS, AND THE SIGN SAYS WHICH KIND. For a block-diffusion drafter the two kinds
* are the two passes: POSITIVE is the QUERY pass, which proposes the whole block in one go,
* and NEGATIVE is the CONTEXT pass, which turns the tokens the trunk just committed into the
* drafter's own K and V. There is no third kind and no round counter -- this drafter runs its
* layers once per step. Zero is a trunk step, so a zeroed RadBatch runs the model. */
/* A drafter pass; the sign gives the kind. Positive is the query pass, proposing the whole
* block at once; negative is the context pass, turning the tokens the trunk just committed
* into the drafter's own K and V. Zero is a trunk step. */
if (batch->draft_pass != 0) {
if (!m.have_ds) {
rad_step_fail(c, "the batch asks for a drafter pass and no drafter is declared: the "
"container carries no `draft.` block, or declare ran at max_spec 0, "
"which is the engine saying it would not draft");
"container carries no `draft.` block, or declare ran at max_spec 0");
return;
}
if (batch->draft_pass > 0) m.ds.step(c, batch);
@@ -694,9 +548,8 @@ static void step(RadCtx* c, const RadBatch* batch) {
for (size_t li = 0; li < m.layers.size(); ++li) {
m.layers[li].attn.step(c, batch);
m.layers[li].mlp.step(c, batch);
/* The tap, into the column slice this one owns. It runs on the PREFILL and DECODE passes
* alike, because the drafter conditions on every committed token's residual stream and
* the rows it wants are whatever this batch just computed. */
/* The tap, into its column slice. Runs on prefill and decode alike: the drafter
* conditions on every committed token's residual stream. */
for (int t = 0; t < m.n_taps; ++t) {
if (m.tap_layer[t] != (int)li) continue;
RAD_ISSUE(c, m.op_tap, brows(m.b_x, batch->n_tok),
@@ -717,15 +570,12 @@ static void step(RadCtx* c, const RadBatch* batch) {
}
}
/* WHAT THIS CONTAINER CAN SPECULATE TO. A plain llama container answers zero -- there is no MTP
* head in this family and no draft block -- and one converted with `--draft-model` against a
* DSpark checkpoint answers its trained block length.
/* The speculation depth this container supports: zero for a plain llama container (no MTP head
* in this family), or the trained block length of a DSpark drafter added with `--draft-model`.
*
* `draft_depth` IS `block_size` AND NOT `block_size - 1`, which is the one number a reader coming
* from arch/qwen35_fp8 will expect to be different: DSpark's anchor row predicts the token after
* it like every other row, so seven noise rows are seven drafts. `draft_depth_fixed` says the
* engine may not pick another -- the drafter denoises a fixed number of masked positions and was
* trained for exactly one block length. */
* `draft_depth` is `block_size`, not `block_size - 1` as in arch/qwen35_fp8: DSpark's anchor row
* predicts too. `draft_depth_fixed` forbids any other depth, since the drafter was trained for
* exactly one block length. */
static int probe(const RadModelMeta* meta, RadArchProbe* out) {
if (!meta || !out) return RAD_E_INVAL;
out->draft_depth = 0;
@@ -743,14 +593,14 @@ static int probe(const RadModelMeta* meta, RadArchProbe* out) {
} /* namespace LLAMA_NS */
#ifndef RAD_ARCH_NO_EXPORTS
/* One more level of expansion so `#NS` sees the expanded namespace; arch/qwen35_fp8 explains it. */
/* One more level of expansion so `#NS` sees the expanded namespace. */
#define LLAMA_PROBE_X(NS) RAD_ARCH_PROBE(NS)
#define LLAMA_PLUGIN_X(NS, ...) RAD_ARCH_PLUGIN(NS, __VA_ARGS__)
LLAMA_PROBE_X(LLAMA_NS)
#if LLAMA_DENSE_SRC
LLAMA_PLUGIN_X(LLAMA_NS, "llama", LLAMA_QUANT, "0.1.0",
"LLaMA-shaped decoders (MiniCPM5, Llama 3, and every derivative that reports "
"model_type llama) from a DENSE bf16 checkpoint, made block-scaled fp8 by a recipe "
"model_type llama) from a dense bf16 checkpoint, made block-scaled fp8 by a recipe "
"at convert time and served fp8 weights / fp8 activations")
#else
LLAMA_PLUGIN_X(LLAMA_NS, "llama", LLAMA_QUANT, "0.1.0",