This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/arch/qwen4exp_fp8/qwen4exp_fp8.cpp
T
Code000andClaude Opus 5.5 ca7a7212ec qwen4exp: verify/decode pass launch order, prefetch width, chain polling, turnover slots
Lossless (no kernel arithmetic or operand change): the router weight is read beside the mixer
all-reduce; k/v projections before q|gate; the indexer's keys behind the attention launches within
the exact bound; verify-height prefetch at 32 workgroups (`prefetch` takes an optional `blocks`
param) and the shared down arm before the routed down from 2 rows (kVerifyRows); chain waits poll by
grid size (R4D_CHAIN_TINY/SMALL/MID); the mover's kept-free turnover slots follow the swaps the
engine asked for (MoverConfig::turnover_adaptive) instead of a fixed 640.

arch_test: new a_flash_next_decode_step_orders_its_small_launches; the pinned graph fingerprints and
step hashes of qwen4exp_fp8 move (re-pinned). place_test: the adaptive turnover. r4d_selftest: the
`blocks` param of prefetch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 20:33:07 +01:00

2056 lines
121 KiB
C++

/* qwen4exp_fp8 -- Qwen4-Exp (`model_type: qwen4_exp`), the architecture of Qwen3.8-Flash-Next.
* The checkpoint is dense bf16, quantised ahead of time by a recipe
* (data/recipes/qwen4exp-w4nl64-i8-hc8m.recipe is the served one): int8 or fp8 trunk linears with
* matching activations, four-bit routed experts, or bf16 throughout.
*
* ============================== RELATION TO QWEN3.5 MOE ==============================
*
* In transformers it is `Qwen4ExpTextModel`, a subclass of `Qwen3_5MoeTextModel`, which this tree
* already serves (arch/qwen35moe_fp8). Most of this file composes the same blocks:
*
* reused the Gated DeltaNet (conv width 4, key/value head dim 128), gated
* full attention (`attn_output_gate`: q_proj twice as wide, half of
* it a sigmoid gate on the output), partial rotary (64 of 256 dims),
* the routed feed-forward with a gated shared expert, the MTP
* position.
* new the gated residual (arch/common/rad_block_hc.h), the n-gram / PLE
* layer (arch/common/rad_block_ple.h), QSA's indexer
* (arch/common/rad_qsa.h), stacked dense expert tensors
* (tools/rad_convert.cpp, slice_stacked), gqa 12, and the delta net's
* sigmoid output gate.
*
* Qwen3.8-Flash-Next: 48 layers of 12 x (3 x GatedDeltaNet -> MoE, then 1 x QSA -> MoE), hidden
* 2560, hc_count 4 so the residual stream is 10240, 512 experts of 640 at top-10 plus a shared 640,
* 24 query heads and 2 KV heads at head_dim 256, vocab 248320.
*
* ============================== THE RESIDUAL STREAM IS FOUR TIMES AS WIDE ==============================
*
* The checkpoint has no `input_layernorm`, `post_attention_layernorm` or final `norm`. Instead
* every block has a gated residual connection: a grouped norm over the 10240-wide stream, a
* low-rank data-dependent read gate that mixes the four sub-streams down to 2560, and four
* data-dependent scalars that add the block's output back into all four. There are 96, plus a 97th
* with no write half that collapses the stream for the lm_head.
*
* arch/common/rad_block_hc.h holds the arithmetic and docs/OPS.md's `hc_read` / `hc_write` rows
* the schema. The blocks do not own their norms: this plugin passes `Src::norm = nullptr`, the fp8
* blocks' "input already normed and quantised" mode, which forgoes folding the residual add and
* the all-reduce into the next norm. The all-reduce rides in the gated residual's write instead,
* where a kernel serves that (HyperConn::Config::reduce, see declare()).
*
* ============================== WHAT IS AND IS NOT FP8 ==============================
*
* The checkpoint is bf16 throughout (no `quantization_config`), so the quantisation descriptor is
* empty and the served formats come from a recipe (data/recipes/qwen4exp-*.recipe). The plugin
* declares each weight at its encoding, and blocks that read several formats choose their ops by
* it:
*
* block fp8 or int8 self_attn.{q,k,v,o}_proj, linear_attn.{in_proj_qkv, in_proj_z, out_proj},
* the shared expert's three (LinearFP8); the indexer's qk projection
* unless the recipe leaves it bf16, as the served one does
* fp8, four-bit every routed expert's gate_up and down, each layer and projection in its
* or bf16 own form (MoeFP8::probe)
* E4M3 rows or bf16 each connection's input_mix_weight_{down,up}, whose lowrank of 320 is 2.5
* scale blocks and so is scaled a row group at a time (HyperConn), and the
* MTP head's fc_hidden / fc_embedding
* fp8, int8, bf16 the lm_head (whichever logits_gemm kernel reads it)
* BF16 every norm gain, A_log, dt_bias, conv1d, in_proj_a / in_proj_b (48 rows is
* not a whole scale block), the router, shared_expert_gate, embed_tokens,
* the PLE projections and inject weights
*
* ============================== HOW TENSOR PARALLEL SPLITS AN EXPERT ==============================
*
* `moe_intermediate_size` is 640 = 5 x 128. An even tp2 share, 320, is not a whole number of
* 128-column blocks, on which the fp8 scale block, the 4-bit scale group and the rotation sit. So
* each rank takes three blocks of the experts of one parity and two of the other's; the router is
* replicated so both ranks route alike, each rank computes its slice of every routed expert, and
* the block's existing end-of-block all-reduce sums the partials, with no all-to-all. See the
* section in declare().
*
* ============================== PLE AND QSA ==============================
*
* PLE, the n-gram embedding, is 51B of the model's 180B parameters: a 320,001,536-row table of
* 160-dim embeddings, gathered 16 rows per token by a hash of the last three token ids, read once
* at layer 1 and injected into the wide stream.
*
* QSA's indexer picks the best `indexer_budget / indexer_compress_ratio` = 512 blocks of 4 tokens
* per query and attends only to those. Below `indexer_budget + indexer_compress_ratio - 1` = 2051
* tokens of context every complete block is selected, so QSA equals dense causal attention bit for
* bit; the engine issues the cheaper dense attention there, and it serves as the oracle for QSA.
* Above it the selection is per query on both prefill and decode (docs/QSA.md,
* arch/common/rad_qsa.h).
*
* The vision tower is declared through arch/common/rad_block_vit.h, only when the container
* carries one and the deployment asked for media; rad-convert writes only declared weights.
*/
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <arch/rad_arch.h>
#include <rad_prefill_split.h>
#include <arch/rad_block_attn_gated_fp8.h>
#include <arch/rad_block_gdn_fp8.h>
#include <arch/rad_block_hc.h>
#include <arch/rad_block_moe_fp8.h>
#include <arch/rad_block_mtp_hc.h>
#include <arch/rad_block_ple.h>
#include <arch/rad_calib_full.h>
#include <arch/rad_qsa.h>
#include <arch/rad_block_vit.h>
namespace qwen4exp_fp8 {
using namespace rad::arch;
/* The delta net's out projection read into the on-die cache beside its decode recurrent update
* (GdnFP8::op_pf_out): the update runs one workgroup per (sequence, head) and leaves the memory
* mostly idle, and the projection behind it otherwise streams its weight cold. All of the weight
* (the read stops at its end), on steps of up to kPrefetchRows rows and kGdnOutPrefetchSeqs
* sequences. Flash-Next at tp 2 on two R9700s under the draft head at depth 3, ms a step without ->
* with: one stream 12.78 -> 12.58, two 17.12 -> 16.95, four 23.57 -> 23.52 (level, as eight with
* the window opened to it). The connection matrices read the same way, beside the recurrent update
* or beside the writes in front of the reads, cost more than they gave (one stream 12.69 -> 12.88
* and 13.17): a read here pays ~2 us of launch for what it saves. */
constexpr int64_t kGdnOutPrefetchBytes = (int64_t)8 << 20;
constexpr int64_t kGdnOutPrefetchSeqs = 4;
/* The rows from which a decode step is taken for a speculative verify, a step of several rows:
* where the shared arm's place and the reads' workgroup count below help, against the one row of
* a plain decode step, where both cost. ms a step at tp 2 with coupling, in one server with the
* rounds interleaved, at two / three / four rows (chain 1 / 2 / 3) and at one: the arm's down
* projection in front -0.28% / -0.21% / -0.25% and +0.38%; the reads at 32 workgroups -0.65% /
* -0.35% / -0.31% and +0.41%. */
constexpr int64_t kVerifyRows = 2;
/* A full-attention layer's out projection likewise, read behind its q, k and v projections
* (AttnGatedFP8::op_pf_o): the prologue and the attention between them and the projection are
* short launches with the memory idle. Twelve layers, so less: five interleaved rounds, ms a step
* without -> with, one stream 12.51 -> 12.45 (every round), two 16.88 -> 16.85. */
constexpr int64_t kAttnOutPrefetchBytes = (int64_t)8 << 20;
/* The workgroups of this model's weight reads on a verify step (the `prefetch` op's `blocks`, from
* kVerifyRows rows): 32, half the kernel's own 64. The reads run beside the delta net's update,
* the attention's prologue and the connections' reductions, all latency-bound kernels of a few
* loads a block whose loads queue behind the read's, and at four rows the update lasts half again
* as long as at one, so a slower read still lands. Chain 3 with coupling at tp 2, ms a step
* against 64 in one server, interleaved: 16 +0.77%, 24 +0.14%, 32 -0.31%, 48 -0.24%, 128 +2.0%,
* 256 +7.4%; -0.6% at 32 with the router's read beside. A one-row step keeps 64 (kVerifyRows). */
constexpr int64_t kPrefetchBlocksVerify = 32;
/* A layer's router likewise, read behind its mixer's out projection, beside the mixer's reduction
* and the feed-forward's connection read (both latency, with the card's memory mostly idle): the
* router is 2.5 MB of bf16 that its GEMM otherwise streams cold in front of a top-k that waits for
* all of it. Chain 3 with coupling at tp 2, ms a step without -> with, each the mean of two
* interleaved rounds in one server: 12.19 -> 12.13 (-0.42% and -0.93%); 12.14 -> 12.09 in another
* session. The same read of the shared expert's two weights, or of the indexer's projection, moved
* nothing (-0.02%, -0.04%, +0.04%), and of the mixer's first projection in front of its
* connection read cost 1.0%: a read there takes the memory from the connection's own. */
constexpr int64_t kRouterPrefetchBytes = (int64_t)4 << 20;
struct Layer {
int full = 0;
HyperConn hc_mix; /* in front of the attention or the delta net */
HyperConn hc_ffn; /* in front of the routed feed-forward */
AttnGatedFP8 attn;
QsaIndexer qsa; /* full-attention layers only; inert when the checkpoint has none */
GdnFP8 gdn;
MoeFP8 mlp;
/* The control vector's projection, applied to the wide stream right after this layer's
* feed-forward write -- where llama.cpp's `build_cvec(res_hc, il)` sits in the `qwen4exp`
* graph. Inert (no op) when the deployment has no vector or this layer is outside its range
* (arch/common/rad_block_hc.h). */
HcProject proj;
/* The layer's calibration inputs (rad_calib_full.h, site_i8), -1 where this run records none:
* the mixer's input (the delta net's in projection, or q, k and v) and its output projection's. */
int cs_in = -1, cs_o = -1;
/* The router's weight read beside the mixer's reduction (kRouterPrefetchBytes); 0 where the
* deployment reads no weights ahead. */
rad_op op_pf_router = 0;
/* The same at a verify step's workgroup count (kPrefetchBlocksVerify), from kVerifyRows rows. */
rad_op op_pf_router_v = 0;
};
struct Model {
Names nm{""};
Geom g{};
GdnFP8::Config gcfg{};
MoeFP8::Config moecfg{};
/* The trunk's linears are int8 (LinearFP8::i8), as read from the first delta-net layer's input
* projection before the activation buffers are declared. */
bool trunk_i8 = false;
/* The trunk's int8 linears are stored rotated (i8a8h), read from the same weight: every
* reader of the block input then takes the rotated codes the connections write. */
bool trunk_rot = false;
/* --calib-dir over the int8 trunk: the Hessian of each mixer linear's input, from the codes its
* GEMM reads (rad_calib_full.h). */
CalibFull calib;
HyperConn::Config hccfg{};
QsaIndexer::Config qsacfg{};
QsaIndexer::Wire qsaw{};
rad_kvgroup kv_qsa_bkey = 0; /* created by the first QSA layer's declare() */
int64_t n_rot = 0;
rad_kvgroup kv_full = 0, kv_state = 0, kv_conv = 0;
/* PLE lives on exactly one layer; -1 when the checkpoint has none or this build does not
* declare that layer. */
PleLayer ple;
int64_t ple_layer = -1;
/* The wide stream and the narrow block input. `b_h` is the residual stream, hc * n_embd per
* token; `a_x` is what a connection's read produces and where its block writes the delta. One
* of each for the whole model: a read is immediately followed by its block and its write, so
* nothing in `a_x` or `b_inj` outlives the write that consumes it. Declaration order determines
* the planner's liveness (see the note at declare()), so this holds by this file's order. */
rad_buf b_h = 0;
ActFP8 a_x{};
rad_buf b_inj = 0;
/* the attention block's own activations, shared across the 12 attention layers */
rad_buf b_qg = 0, b_k = 0, b_v = 0, b_q = 0, b_rope_cs = 0, b_gate = 0;
ActFP8 a_attn{};
/* the delta net's */
ActFP8 a_in{}, a_go{};
rad_buf b_ab = 0, b_gq = 0, b_gk = 0, b_gv = 0, b_gdec = 0, b_beta = 0, b_kkt = 0;
/* the routed feed-forward's */
rad_buf b_rlogits = 0, b_eids = 0, b_ew = 0, b_sorted = 0, b_eoff = 0, b_ecnt = 0;
rad_buf b_egu = 0, b_edn = 0, b_emoe = 0, b_sgu = 0, b_sout = 0, b_sgate = 0;
ActFP8 a_eff{}, a_sff{};
/* The rotated codes of the block input, for the routed arm only: `q` and `s` are used and `x`
* stays zero, since the bf16 source is `a_x`, unrotated. Declared only when the container asks
* for a rotation; zero handles tell the MoE block the pair is absent. */
ActFP8 a_hr{};
/* The routed arm's activations are int8 (MoeFP8::a8): int8 connection codes under experts
* with an int8 row. */
bool arm8 = false;
rad_buf b_hout = 0, b_logits = 0;
/* ---- the multi-token-prediction head (arch/common/rad_block_mtp_hc.h). Declared only when
* the deployment speculates, since its ~2.5 GiB of weights would otherwise be loaded and never
* issued. rad-convert declares at a non-zero max_spec so the container carries everything the
* model can serve. */
bool have_mtp = false;
MtpHcBlock mtp{};
rad_buf b_mtp_hin = 0, b_mtp_hw = 0, b_mtp_e = 0, b_mtp_hout = 0;
rad_buf b_mtp_dq = 0, b_mtp_ds = 0, b_mtp_dsum = 0;
rad_buf b_mtp_dlog = 0, b_mtp_didx = 0, b_mtp_dval = 0;
/* --draft-coupling: [max_seqs] draws staged before the serial passes and [max_spec,
* max_seqs] states read back after them. PERSIST: the engine writes them from the host and the
* head writes the states on the card, across a step boundary. */
rad_buf b_mtp_couple_draws = 0, b_mtp_couple_state = 0;
rad_buf b_mtp_dpair = 0, b_mtp_dgath = 0;
HcEnter enter;
HyperConn mixer; /* the 97th connection: no write half */
/* Whether this rank has said that it pipelines a prefill step (step_pipelined), once. */
bool pipe_noted = false;
/* Whether the deployment spans (RadBuildCtx::max_mixed_tok below a max_tok wider than a MoE
* pass): a step wider than a pass is then one sequence, the connections read it a pass at a
* time and the indexer scores it in windows. `pipe_on` adds ranks whose blocks issue their own
* all-reduce at such a step (declare): it is pipelined (step_pipelined) and its all-reduces
* are declared at `overlap` 1. Without a span neither is set and none of these paths is
* declared or taken. A sizing declare runs at a max_tok no wider than a pass and takes both
* from the real declare, so it declares the same ops. */
bool span_on = false;
bool pipe_on = false;
/* {0, pass, T} for a step wider than a pass (pipe_bounds): the windows of the indexer's score
* plane and the delta net's pieces. A transient (declare), uploaded by every step that reads
* it -- the first two from the pinned host copy `pipe_cu_h`, made once. */
rad_buf b_pipe_cu = 0;
int32_t* pipe_cu_h = nullptr;
bool pipe_cu_tried = false;
std::vector<Layer> layers;
rad_weight w_tok = 0, w_lm_head = 0;
rad_op op_embed = 0, op_embed_ar = 0, op_rope_cs = 0, op_gather = 0, op_logits = 0;
/* The indexer's within-step buffers store_keys touches are excluded from sharing (declare),
* so a layer's keys may be issued behind its attention's launches (step_serial, QsaSide). */
bool qsa_defer = false;
/* ---- media (arch/common/rad_block_vit.h): the vision tower and the scatter of its rows over
* the token embeddings, declared only when the container carries a tower and the deployment
* asked for media (RadBuildCtx::max_enc_patches). */
bool media = false;
VisionTower vit{};
MediaRows mrows{};
};
static Model g_model[MAX_SLOTS];
/* The n-gram table's row count, by prime search.
*
* Each of the `heads` hash heads has its own vocabulary: the 1st, 2nd, ... nth prime above
* `ngram_vocab_size_base - 1`; the table is their sum, padded to `divisor`
* (`Qwen4ExpTextNGramEmbedding.__init__`). The per-head sizes are in the checkpoint
* (`ngram_heads_vocab_sizes`) and the served hash reads them there; only the total is derived,
* because a weight's shape must be declared before any tensor is opened. rad-convert checks it:
* a multi-source concat's declared leading extent must equal its sources'.
*
* Cost: 16 primes near 2e7 by trial division, once at declare. */
static bool ngram_is_prime(int64_t v) {
if (v < 2) return false;
if (v % 2 == 0) return v == 2;
for (int64_t d = 3; d * d <= v; d += 2)
if (v % d == 0) return false;
return true;
}
static int64_t ngram_table_rows(int64_t base, int64_t heads, int64_t divisor) {
if (base <= 1 || heads <= 0) return 0;
int64_t total = 0, p = base - 1;
for (int64_t i = 0; i < heads; ++i) {
do { ++p; } while (!ngram_is_prime(p));
total += p;
}
return divisor > 1 ? ((total + divisor - 1) / divisor) * divisor : total;
}
/* A rank's place in the shared expert's uneven block split, where the first places take one
* block more. MoeFP8::declare hands a plain expert's extra 64-wide tiles to the first ranks by
* index; the shared expert's extra blocks start at the rank after the last of those, so the two
* remainders land on different ranks when there are enough. (Routed experts leave no remainder:
* the parity split at two ranks and the four-class split at four are even.) At four ranks and 640
* columns (ten tiles, five blocks) ranks 0 and 1 take a tile more and rank 2 the block; at two the
* place is the rank. */
static int64_t shared_place(int64_t n_ff_all, int world, int rank) {
const int64_t first = (n_ff_all / 64) % world;
return (rank - first + world) % world;
}
/* Columns [lo, hi) of the slice at `place` when `nb` fp8 blocks split over `world` places, the
* first nb % world places taking one block more. */
static void block_slice(int64_t nb, int world, int64_t place, int64_t* lo, int64_t* hi) {
const int64_t base = nb / world, extra = nb % world;
const int64_t b0 = place * base + std::min<int64_t>(place, extra);
*lo = b0 * RAD_FP8_BLOCK;
*hi = (b0 + base + (place < extra ? 1 : 0)) * RAD_FP8_BLOCK;
}
static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* ctx) {
if (!b || !meta || !ctx) return RAD_E_INVAL;
if (ctx->rank < 0 || ctx->rank >= MAX_RANKS || rad_build_slot(ctx) >= MAX_SLOTS)
return RAD_E_INVAL;
/* A sizing declare writes scratch: the step reads g_model, filled by the real declare at the
* real max_tok (RadBuildCtx::shape_probe). */
static thread_local Model probe_model;
Model& m = ctx->shape_probe ? probe_model : g_model[rad_build_slot(ctx)];
/* The reset drops the pinned window copy (pipe_bounds); free it first -- a re-declare of the
* same rank (every rebuild, every test case) otherwise overwrites the pointer and leaks the
* pin. The copy is read only by a step's memcpys, and no step runs across a declare. */
if (m.pipe_cu_h) rad_dev_free(m.pipe_cu_h, RAD_MEM_HOST_PINNED);
m = Model{};
m.nm = Names(ctx->scope ? ctx->scope : "");
Names& nm = m.nm;
/* arch_splits_ff: the plugin splits the routed experts' width (640 = five fp8 blocks) itself,
* by whole blocks and parity, at any world up to the block count (including three, which 640
* does not divide), so geom_from must not refuse it. attn_world_ok: this plugin's attention
* blocks (rad_block_attn_gated_fp8.h) serve a smaller attention world when the two KV heads do
* not divide the world. */
RAD_ARCH_TRY(geom_from(m.g, meta, ctx, "bf16", RAD_BF16, /*arch_splits_ff=*/true,
/*attn_world_ok=*/true));
Geom& g = m.g;
/* Qwen4-Exp's norms are all `x_hat * (1 + w)` (`Qwen3NextRMSNorm.forward` is
* `_norm(x.float()) * (1.0 + weight.float())`, weight initialised to zeros); the delta net's
* gated output norm is plain `weight * x` (initialised to ones). Taken from the reference. */
g.wadd = 1.0f;
g.wadd_qk = 1.0f;
/* ---- the gated residual's geometry. */
m.hccfg.hc = meta_int2(meta, "qwen4exp.hc_count", "hc_count", 0);
m.hccfg.lowrank = meta_int2(meta, "qwen4exp.hc_lowrank", "hc_lowrank", 0);
if (m.hccfg.hc <= 1 || m.hccfg.lowrank <= 0) {
fprintf(stderr, "radiance: qwen4exp: the container does not describe the gated residual "
"(hc_count %lld, hc_lowrank %lld). Every norm in this architecture is one "
"of these connections, so there is nothing to fall back to.\n",
(long long)m.hccfg.hc, (long long)m.hccfg.lowrank);
return RAD_E_INVAL;
}
/* Every block's all-reduce rides in the following write where a kernel serves that
* (HyperConn::Config::reduce): the block's partial sum is the write's input, so the pair is one
* launch. Each block is told below whether its connection's fused op resolved. */
m.hccfg.reduce = 1;
const int64_t HN = m.hccfg.hc * g.n_embd;
/* ---- partial rotary: 0.25 of head_dim 256 is 64; the other 192 dims pass through. */
{
const double pr = rad_meta_getf(meta, "text_config.rope_parameters.partial_rotary_factor",
rad_meta_getf(meta, "rope_parameters.partial_rotary_factor",
rad_meta_getf(meta, "partial_rotary_factor", 1.0)));
m.n_rot = (int64_t)((double)g.head_dim * pr);
if (m.n_rot <= 0 || m.n_rot > g.head_dim || (m.n_rot % 2)) {
fprintf(stderr, "radiance: qwen4exp: partial_rotary_factor %.6g of head_dim %lld is "
"%lld rotary dims, which is not an even count in (0, head_dim]\n",
pr, (long long)g.head_dim, (long long)m.n_rot);
return RAD_E_INVAL;
}
}
RAD_ARCH_TRY(yarn_from(g, meta, "", m.n_rot));
/* There is deliberately no serve-time layer slice. A model too large for the device needs a
* converter flag that writes a named partial container, not a switch that silently serves less
* than the model. */
/* ---- the layer schedule. */
const int64_t interval = meta_int2(meta, "qwen4exp.full_attention_interval",
"full_attention_interval", 0);
if (interval <= 1) {
fprintf(stderr, "radiance: qwen4exp: full_attention_interval is %lld; this is a hybrid "
"architecture and the interval decides which layers are linear\n",
(long long)interval);
return RAD_E_INVAL;
}
/* The config file says "full_attention" for the QSA layers (HuggingFace's post_init rewrites
* them to "qwen_sparse_attention" in memory), so both spellings mean a full layer. */
RAD_ARCH_TRY(check_layer_schedule("qwen4exp", meta_text_gets(meta, "layer_types", nullptr),
g.n_layer, interval,
{"full_attention", "qwen_sparse_attention"}));
/* ---- the delta net. */
{
GdnFP8::Config& gc = m.gcfg;
const int64_t nk = meta_int2(meta, "qwen4exp.ssm.key_head_count",
"linear_num_key_heads", 0);
const int64_t nv = meta_int2(meta, "qwen4exp.ssm.value_head_count",
"linear_num_value_heads", 0);
gc.head_k = meta_int2(meta, "qwen4exp.ssm.key_head_dim", "linear_key_head_dim", 0);
gc.head_v = meta_int2(meta, "qwen4exp.ssm.value_head_dim", "linear_value_head_dim", 0);
gc.conv_width = meta_int2(meta, "qwen4exp.ssm.conv_kernel", "linear_conv_kernel_dim", 0);
gc.chunk = 64;
/* `output_gate_type` is a checkpoint property: Qwen3.5/3.6 leave it unset and mean silu,
* Qwen4-Exp states "sigmoid". libr4d's gated norm has both. */
const char* ogt = rad_meta_gets(meta, "text_config.output_gate_type",
rad_meta_gets(meta, "output_gate_type", nullptr));
if (!ogt || !*ogt) ogt = rad_meta_gets(meta, "text_config.hidden_act",
rad_meta_gets(meta, "hidden_act", "silu"));
gc.act = ogt;
if (nk <= 0 || nv <= 0 || gc.head_k <= 0 || gc.head_v <= 0 || gc.conv_width <= 0) {
fprintf(stderr, "radiance: qwen4exp: the container does not describe the linear "
"layers (key heads %lld, value heads %lld, key width %lld, value "
"width %lld, conv width %lld)\n",
(long long)nk, (long long)nv, (long long)gc.head_k, (long long)gc.head_v,
(long long)gc.conv_width);
return RAD_E_INVAL;
}
/* Heads split evenly when they can and unevenly otherwise: 16 K heads over three ranks
* are 6, 5 and 5, each rank's V heads the same multiple of its K heads (48 / 16 = 3 ->
* 18, 15, 15), declared as spans of the container's rows (rad_block_gdn_fp8.h,
* Config::k_first). Nothing is padded; the kernels take any head count. A V count that is
* not a multiple of the K count is refused. */
gc.n_head_k_all = nk;
gc.n_head_v_all = nv;
if (g.world > 1 && (nk % g.world != 0 || nv % g.world != 0)) {
if (nv % nk != 0) {
fprintf(stderr, "radiance: qwen4exp: %lld value heads over %lld key heads do not "
"group, so an uneven split over %d ranks is not defined\n",
(long long)nv, (long long)nk, g.world);
return RAD_E_INVAL;
}
const int64_t ratio = nv / nk, base = nk / g.world, rem = nk % g.world;
/* Extra heads go first to ranks without attention (rad_arch.h remainder_place);
* with none, this is the rank's own place. */
const int64_t pl = remainder_place(g, g.rank, 0);
gc.n_head_k = base + (pl < rem ? 1 : 0);
gc.k_first = pl * base + (pl < rem ? pl : rem);
gc.n_head_v = ratio * gc.n_head_k;
gc.v_first = ratio * gc.k_first;
rad_note(b, "rank %d takes delta-net K heads [%lld, %lld) of %lld and V heads "
"[%lld, %lld) of %lld", g.rank,
(long long)gc.k_first, (long long)(gc.k_first + gc.n_head_k), (long long)nk,
(long long)gc.v_first, (long long)(gc.v_first + gc.n_head_v), (long long)nv);
} else {
int s;
if ((s = divide_or_fail(nk, g.world, "linear_num_key_heads", &gc.n_head_k)) < 0) return s;
if ((s = divide_or_fail(nv, g.world, "linear_num_value_heads", &gc.n_head_v)) < 0) return s;
}
}
/* ---- the routed feed-forward.
*
* n_ff is `moe_intermediate_size`, divided by world_size when the world divides it and whole
* otherwise (geom_from's `arch_splits_ff`). At more than one rank the split below sets the
* rank's width, by whole fp8 blocks and parity (see the file header). */
{
MoeFP8::Config& mc = m.moecfg;
mc.n_expert = meta_int2(meta, "qwen4exp.expert_count", "num_experts", 0);
mc.top_k = meta_int2(meta, "qwen4exp.expert_used_count", "num_experts_per_tok", 0);
mc.n_ff_exp = g.n_ff;
mc.n_ff_shared = meta_int2(meta, "qwen4exp.expert_shared_feed_forward_length",
"shared_expert_intermediate_size", 0);
/* The shared expert is split by whole fp8 scale blocks: evenly when possible, otherwise
* as evenly as whole blocks allow, by the rank's place (shared_place). Its 640 columns are
* five blocks: three on rank 0 and two on rank 1; at four ranks two on rank 2 and one on
* each other. See MoeFP8::Config::shared_lo. */
if (mc.n_ff_shared > 0 && g.world > 1 && (mc.n_ff_shared / g.world) % RAD_FP8_BLOCK == 0) {
int s;
if ((s = divide_or_fail(mc.n_ff_shared, g.world,
"shared_expert_intermediate_size", &mc.n_ff_shared)) < 0)
return s;
} else if (mc.n_ff_shared > 0 && g.world > 1) {
if (mc.n_ff_shared % RAD_FP8_BLOCK) {
fprintf(stderr, "radiance: the shared expert is %lld columns, which is not a whole "
"number of %d-column fp8 scale blocks, so it cannot be split "
"across %d ranks.\n", (long long)mc.n_ff_shared, RAD_FP8_BLOCK,
g.world);
return RAD_E_INVAL;
}
const int64_t nb = mc.n_ff_shared / RAD_FP8_BLOCK;
block_slice(nb, g.world, shared_place(g.n_ff_all, g.world, g.rank),
&mc.shared_lo, &mc.shared_hi);
mc.n_ff_shared = mc.shared_hi - mc.shared_lo;
rad_note(b, "the shared expert does not split evenly into %d-column fp8 scale blocks: "
"rank %d computes columns [%lld, %lld) of it, %lld of %lld blocks, and the "
"all-reduce sums the partials", RAD_FP8_BLOCK, g.rank,
(long long)mc.shared_lo, (long long)mc.shared_hi,
(long long)(mc.n_ff_shared / RAD_FP8_BLOCK), (long long)nb);
}
mc.norm_topk = 1;
/* GPTQ calibration run, enabled by the same setting that names the output directory (a
* calibration with nowhere to write would be a 1.34 GB allocation and two launches per
* layer for nothing). A separate run from serving; see MoeFP8::Config::calib. */
mc.calib = calib_dir() != nullptr ? 1 : 0;
/* Tokens per pass. 2048 is the linear-state checkpoint interval the scheduler clamps the
* prefill chunk to, so the shipped configuration is one pass. It also sizes four buffers at
* `rows * top_k`, so a smaller max_tok lowers it. */
mc.rows = g.max_tok < 2048 ? g.max_tok : 2048;
if (mc.n_expert <= 0 || mc.top_k <= 0 || mc.top_k > mc.n_expert) {
fprintf(stderr, "radiance: qwen4exp: the container describes %lld expert(s) with "
"top_k %lld\n", (long long)mc.n_expert, (long long)mc.top_k);
return RAD_E_INVAL;
}
/* ---- the experts' stored format (block fp8, four-bit, four-bit rotated, or plain bf16),
* read from one expert's encoding (MoeFP8::probe) before anything is sized by it: the
* split between ranks, the rotated copy of the block input, the connection in front of
* every routed block, and for bf16 every linear in the model. The encoding comes from the
* container, checkpoint or recipe being applied, so a container is always served in the
* format it holds.
*
* The probed expert is the first one this rank owns under expert parallelism: the answer
* decides whether that split is used, and the expert is declared on this rank either way
* (whole, or a slice under the parity split), so the block skips exactly the map the probe
* made. */
mc.probed_expert = (g.world > 1 && mc.n_expert % g.world == 0)
? (int64_t)g.rank * (mc.n_expert / g.world) : 0;
{
MoeFP8::Src ps{};
RAD_ARCH_TRY(moe_expert_sources(b, nm, "format",
"model.language_model.layers.0.mlp.experts", &ps));
RAD_ARCH_TRY(MoeFP8::probe(b, nm, 0, ps, &mc));
}
/* Plain experts mean a container converted without a quantising recipe, so every linear
* is plain too: the blocks issue bf16 GEMMs over bf16 activations and nothing quantises
* (Geom::w_bf16). Each linear refuses a quantised encoding then. The converse is allowed:
* a quantised container may keep a linear plain (the QSA indexer's projection), which
* LinearFP8 reads as bf16. */
g.w_bf16 = mc.expert_bf16 != 0;
/* The connections in front of the blocks then emit no codes, since nothing reads them. */
m.hccfg.quant = g.w_bf16 ? 0 : 1;
/* ============================== EVERY RANK HOLDS A SLICE OF EVERY EXPERT ==============================
*
* An expert is 640 columns, five 128-column blocks (the fp8 scale block, the 4-bit scale
* group and the rotation width), so two ranks cannot halve one.
*
* The split is by parity: each parity's five blocks are cut contiguously over the ranks,
* and the rank taking the extra block alternates with the expert's parity
* (MoeFP8::Config::ff_lo). At two ranks, rank r's slice of expert e is the first three
* blocks when e % 2 == r and the last two otherwise. The router is replicated, every rank
* computes its slice of every routed expert, and the block's all-reduce sums the partials.
*
* At four ranks parity alone would give ranks 0 and 1 three blocks of every expert pair
* and ranks 2 and 3 two, leaving 2 and 3 waiting in the all-reduce. So the extra block
* rotates over four places by expert number mod 4 (four slice tables, `moe_gemm_q_mod4`),
* and every rank computes five blocks of every four experts. A layer keeping two experts
* plain bf16 has 510 quantised ones, and its first two classes are one expert longer.
* Other remainders stay apart: the plain experts' extra tiles go to ranks 0 and 1
* (MoeFP8::declare), the shared expert's extra block to rank 2 (shared_place).
*
* Not expert parallelism (rank r owning experts [r*E/W, (r+1)*E/W) whole): bytes per rank
* are equal on average, but routing is random, and a verify step's forty routed slots leave
* one rank about five experts ahead, costing about a millisecond per step at one sequence
* in the all-reduce wait. With parity the ranks differ by a fifth of an expert per unit of
* even/odd imbalance. Whole experts would also break the recipe's pairing of protected
* experts, which keeps each rank's quantised-expert count even for the two parity tables.
*
* A calibration run does split whole experts per rank: the down projection's Hessian spans
* an expert's whole K, and a slice would lack the cross terms. Whole experts give each rank
* the full Hessian of every expert it holds, which the packer sums (rad_arch.h,
* calib_dir). */
mc.n_expert_all = mc.n_expert;
/* bf16 experts are also split whole per rank: a slice of a bf16 expert is two row ranges
* and a column range, which the file tier cannot read as one run, and a bf16 model is
* served to calibrate and compare, not for decode speed. */
if (g.world > 1 && (mc.calib || mc.expert_bf16)) {
int s;
int64_t per = 0;
if ((s = divide_or_fail(mc.n_expert_all, g.world, "expert_count", &per)) < 0) return s;
mc.n_ff_exp = g.n_ff_all;
mc.n_expert = per;
mc.expert_base = (int64_t)g.rank * per;
rad_note(b, "%s: rank %d owns experts [%lld, %lld) of %lld whole.",
mc.calib ? "calibration (each expert's Hessian is over its whole width)"
: "bf16 experts", g.rank,
(long long)mc.expert_base, (long long)(mc.expert_base + mc.n_expert),
(long long)mc.n_expert_all);
} else if (g.world > 1) {
if (g.n_ff_all % RAD_FP8_BLOCK) {
fprintf(stderr, "radiance: qwen4exp: an expert is %lld columns, which is not a whole "
"number of %d-column blocks and cannot be split across ranks.\n",
(long long)g.n_ff_all, RAD_FP8_BLOCK);
return RAD_E_INVAL;
}
const int64_t nb = g.n_ff_all / RAD_FP8_BLOCK;
if (nb % g.world == 0) {
mc.n_ff_exp = g.n_ff_all / g.world;
} else if (g.world <= nb) {
/* The uneven split is two slices per rank: each parity's blocks are cut
* contiguously across the ranks and the rank taking the odd block alternates with
* the parity, so slices are whole blocks and every block has exactly one owner.
* Widths are ceil(nb/W) and floor(nb/W). Every rank must get at least one block of
* each parity, since an empty slice still joins the all-reduce, so the world may
* not exceed nb. */
for (int q = 0; q < 2; ++q) {
/* This rank's place in expert parity q's split: first for its own parity.
* Experts do not go remainder-first (rad_arch.h remainder_place): an expert
* block is a whole GEMM over every routed token, and a rank taking the extra
* block of every class becomes the slowest rank (at three ranks on
* Flash-Next, a 50/25/25 split cost 25% per step single-stream). Only the
* delta net's remainder, a head's worth of work, goes to the rank without
* attention. */
block_slice(nb, g.world, (g.rank - q + g.world) % g.world, &mc.ff_lo[q],
&mc.ff_hi[q]);
}
mc.n_ff_exp = std::max(mc.ff_hi[0] - mc.ff_lo[0], mc.ff_hi[1] - mc.ff_lo[1]);
/* At four ranks, four classes by expert number mod 4 (MoeFP8::Config::ff_lo4),
* used by every layer instead: the same cut with the rank's place shifted by the
* class, so the extra block rotates over all four ranks. Every block still has one
* owner. */
if (g.world == 4) {
for (int q = 0; q < 4; ++q) {
block_slice(nb, 4, (g.rank - q + 4) % 4, &mc.ff_lo4[q], &mc.ff_hi4[q]);
mc.n_ff_exp = std::max(mc.n_ff_exp, mc.ff_hi4[q] - mc.ff_lo4[q]);
}
rad_note(b, "routed experts at four classes: rank %d computes columns [%lld, "
"%lld) [%lld, %lld) [%lld, %lld) [%lld, %lld) of the experts "
"numbered 0, 1, 2 and 3 mod 4, %lld blocks per four experts like "
"every rank",
g.rank, (long long)mc.ff_lo4[0], (long long)mc.ff_hi4[0],
(long long)mc.ff_lo4[1], (long long)mc.ff_hi4[1],
(long long)mc.ff_lo4[2], (long long)mc.ff_hi4[2],
(long long)mc.ff_lo4[3], (long long)mc.ff_hi4[3], (long long)nb);
} else {
rad_note(b, "routed experts: rank %d computes columns [%lld, %lld) of each "
"even expert and [%lld, %lld) of each odd one, of %lld; %lld "
"blocks of %d do not split evenly over %lld ranks, so the extra "
"block alternates with the expert's parity",
g.rank, (long long)mc.ff_lo[0], (long long)mc.ff_hi[0],
(long long)mc.ff_lo[1], (long long)mc.ff_hi[1],
(long long)g.n_ff_all, (long long)nb, RAD_FP8_BLOCK,
(long long)g.world);
}
} else {
fprintf(stderr, "radiance: qwen4exp: an expert is %lld blocks of %d, which %d ranks "
"cannot each take a whole block of in both parities. Lower the rank "
"count, or serve whole experts (a bf16 container or a calibration "
"run does).\n",
(long long)nb, RAD_FP8_BLOCK, g.world);
return RAD_E_INVAL;
}
}
}
/* ---- the n-gram embedding's layer and the indexer's geometry, each noted where it changes
* what this build computes. */
{
const char* ple = rad_meta_gets(meta, "text_config.ple_layer_ids",
rad_meta_gets(meta, "ple_layer_ids", nullptr));
if (ple && *ple) {
/* One-indexed: `ple_layer_ids: [2]` is layer_idx 1 (the reference uses
* `config.ple_layer_ids.index(layer_idx + 1)` and validates [1, num_hidden_layers]). */
m.ple_layer = (int64_t)std::strtoll(ple, nullptr, 10) - 1;
if (m.ple_layer < 0 || m.ple_layer >= g.n_layer) {
/* Outside the declared layers: the n-gram embedding is not in this build, and the
* note says so rather than dropping it silently. */
rad_note(b, "ple_layer_ids = %s is outside the %lld layers declared, so the n-gram "
"embedding is not in this build.", ple, (long long)g.n_layer);
m.ple_layer = -1;
}
}
}
{
const int64_t budget = meta_int2(meta, "qwen4exp.indexer_budget", "indexer_budget", 0);
const int64_t ratio = meta_int2(meta, "qwen4exp.indexer_compress_ratio",
"indexer_compress_ratio", 0);
m.qsacfg.budget = budget;
m.qsacfg.ratio = ratio;
m.qsacfg.heads = meta_int2(meta, "qwen4exp.indexer_n_heads", "indexer_n_heads", 0);
m.qsacfg.head_dim = meta_int2(meta, "qwen4exp.indexer_head_dim", "indexer_head_dim", 0);
m.qsacfg.rot = m.n_rot;
m.qsacfg.topk = (ratio > 0) ? budget / ratio : 0;
m.qsacfg.max_blocks = (ratio > 0) ? (g.max_ctx + ratio - 1) / ratio : 0;
m.qsacfg.max_work = (ratio > 0) ? (g.max_tok + ratio - 1) / ratio + g.max_seqs : 0;
/* The tail's ring (see QsaIndexer::Config::ring), sized for `max_spec`, the deepest
* speculation any step can carry. */
m.qsacfg.ring = ratio + g.max_spec;
if (budget > 0 && ratio > 0) {
/* The exactness bound: with `pos + 1 <= budget + ratio - 1` tokens visible there are
* at most budget/ratio complete blocks, the indexer's top-k keeps all of them, and the
* tail is always attended, so QSA equals dense causal attention bit for bit. */
const int64_t exact_to = budget + ratio - 1;
/* The context bound is not capped: prefill selects per query exactly as decode does
* (arch/common/rad_qsa.h, docs/QSA.md). There is deliberately no force-dense switch,
* since above the bound dense attention is not the trained model. */
if (g.max_ctx > exact_to)
rad_note(b, "QSA: sparse past %lld tokens of context, %lld blocks of %lld per "
"query at prefill and decode; at or below that the engine issues the "
"bit-identical dense attention",
(long long)exact_to, (long long)(budget / ratio), (long long)ratio);
else
rad_note(b, "QSA: dense attention is exact to %lld tokens of context and "
"--max-model-len is %lld, so the indexer selects every visible block",
(long long)exact_to, (long long)g.max_ctx);
}
}
rad_note(b, "gated residual: %lld streams of %lld (%lld wide), low-rank gate %lld; %lld "
"connections plus the mixer",
(long long)m.hccfg.hc, (long long)g.n_embd, (long long)HN,
(long long)m.hccfg.lowrank, (long long)(2 * g.n_layer));
/* ---- the KV groups: the same three as qwen35_fp8. */
{
RadKVGroupDecl fd{};
fd.kind = RAD_KV_FULL;
fd.dtype = rad_kv_cache_dtype(ctx, RAD_BF16);
fd.n_head_kv = g.n_head_kv;
fd.head_dim = g.head_dim;
/* The MTP head's layer shares this group. */
m.kv_full = rad_decl_kv_group(b, nm.f("kv_attn"), &fd);
g.kv_dtype = rad_dtype_name(fd.dtype);
RadKVGroupDecl sd{};
sd.kind = RAD_KV_LINEAR;
sd.dtype = RAD_F32; /* f16 trades draft acceptance for a sliver of step time */
sd.n_head_kv = m.gcfg.n_head_v;
sd.state_dim[0] = m.gcfg.head_v;
sd.state_dim[1] = m.gcfg.head_k;
m.kv_state = rad_decl_kv_group(b, nm.f("kv_gdn_state"), &sd);
RadKVGroupDecl cd{};
cd.kind = RAD_KV_CONV;
cd.dtype = RAD_BF16;
cd.n_head_kv = 2 * m.gcfg.n_head_k + m.gcfg.n_head_v;
cd.head_dim = m.gcfg.head_k;
cd.conv_width = m.gcfg.conv_width;
m.kv_conv = rad_decl_kv_group(b, nm.f("kv_gdn_conv"), &cd);
if (!m.kv_full || !m.kv_state || !m.kv_conv) return RAD_E_INVAL;
/* ---- the QSA indexer's tail, which is state, not a buffer.
*
* It carries an unfinished block's raw index keys across steps, so it is indexed by
* sequence and layer, which a step-local buffer cannot express (arch/common/rad_qsa.h,
* Wire). As a linear group the engine keeps a slot stable for the sequence's life, zeroes
* it on allocation, and saves and restores it with the linear state on prefix-cache
* checkpoints, so a resume onto a half-built block reads defined data.
*
* `n_head_kv` 1 and `state_dim` {ring, head_dim}: `ring` raw keys, a few KiB per layer per
* sequence. */
if (m.qsacfg.heads > 0 && m.qsacfg.head_dim > 0 && m.qsacfg.ring > 0) {
RadKVGroupDecl td{};
td.kind = RAD_KV_LINEAR;
td.dtype = g.act_dtype;
td.n_head_kv = 1;
td.state_dim[0] = m.qsacfg.ring;
td.state_dim[1] = m.qsacfg.head_dim;
m.qsacfg.kv_tail = rad_decl_kv_group(b, nm.f("kv_qsa_tail"), &td);
if (!m.qsacfg.kv_tail) return RAD_E_INVAL;
}
}
/* ---- the trunk's stored format, read from the first delta-net layer's input projection as
* the experts' is from one expert. The activation buffers below carry an int8 code pair only
* when the trunk's linears are int8 (LinearFP8::i8), so it must be known first. The probe maps
* the weight through the layer's own LinearFP8, which then does not map it again. */
m.layers.resize((size_t)g.n_layer);
{
const char* pre0 = "model.language_model.layers";
for (int64_t l = 0; l < g.n_layer; ++l) {
if (is_full_attention(l, interval)) continue;
RAD_ARCH_TRY(m.layers[(size_t)l].gdn.in.probe(
b, nm, g, nm.f("blk.%lld.ssm_inz", (long long)l),
{ nm.ckpt("%s.%lld.linear_attn.in_proj_qkv.weight", pre0, (long long)l),
nm.ckpt("%s.%lld.linear_attn.in_proj_z.weight", pre0, (long long)l) }));
m.trunk_i8 = m.layers[(size_t)l].gdn.in.probed_i8;
/* Stored rotated (i8a8h: int8 codes or a narrower form on their grid, /fwht128): its
* readers take had_quant_act_i8g's codes, one scale a 128 of K as the connections
* write them. */
RadEncoding enc{};
m.trunk_rot = m.trunk_i8 &&
weight_enc(b, nm.f("blk.%lld.ssm_inz.weight", (long long)l), &enc) &&
(linear_is_w8h(enc) || linear_is_wnh(enc));
if (m.trunk_rot && linear_w8h_group(enc) != RAD_FP8_BLOCK) {
fprintf(stderr, "radiance: qwen4exp: the trunk's rotated int8 linears keep a scale "
"a %lld of K; the connections write the block input's codes at one "
"a %d\n", (long long)linear_w8h_group(enc), RAD_FP8_BLOCK);
return RAD_E_UNSUPPORTED;
}
break;
}
}
/* ---- buffers. */
const int64_t q_dim = g.q_dim();
const int64_t kv_dim = g.kv_dim();
const int64_t v_dim = m.gcfg.v_dim();
const int64_t conv_dim = m.gcfg.conv_dim();
const int64_t T = g.max_tok;
const int64_t ER = m.moecfg.rows * m.moecfg.top_k;
m.b_h = decl_b(b, nm.f("h_wide"), g.act_dtype, {T, HN});
m.a_x.x = decl_b(b, nm.f("x"), g.act_dtype, {T, g.n_embd});
if (ctx->shape_probe) {
m.span_on = g_model[rad_build_slot(ctx)].span_on;
m.pipe_on = g_model[rad_build_slot(ctx)].pipe_on;
} else {
m.span_on = ctx->max_mixed_tok > 0 && ctx->max_mixed_tok < g.max_tok &&
g.max_tok > m.moecfg.rows;
/* Only where the blocks issue their own all-reduce at a step wider than a pass: at two
* ranks the gated residual's fused write takes every step's (ar_exact_write_rows), so such
* a step runs serially. */
m.pipe_on = m.span_on && g.world > 1 && ar_exact_write_rows(g) < g.max_tok;
}
/* The pipelined step's all-reduces run on the second lane, in place over `x`, so it is the one
* buffer that lane touches; it is live over the whole program anyway. */
if (m.pipe_on) RAD_ARCH_TRY(rad_buf_concurrent(b, m.a_x.x));
/* A transient although only its last entry changes per step: a persistent buffer sits in the
* fixed region under every transient, and one more alignment unit there moves every
* activation by 256 bytes, which on Flash-Next at four ranks slows the MoE gather by 73 % and
* a prefill by 3 %. Every step that reads it rewrites it (pipe_bounds). */
if (m.span_on)
m.b_pipe_cu = decl_b(b, nm.f("pipe_cu"), RAD_I32, {4});
/* Under a span every connection reads a step wider than a pass a pass at a time
* (HyperConn::read_pieces, and the pipelined step's own reads), so the reads are declared no
* wider than a pass and their scratch is sized for that band. */
if (m.span_on) m.hccfg.read_rows = m.moecfg.rows;
RAD_ARCH_TRY(m.a_x.declare_qs(b, nm, g, "x", g.n_embd, 0, m.trunk_i8));
/* The connections write int8 codes themselves when every reader of their codes is an int8
* linear: the trunk's are, and the routed experts then read the rotated copy or the bf16
* plane. When experts read the plain E4M3 codes, the connections keep those and the int8
* linears quantise for themselves. */
/* A read's rotated copy takes its plain codes' dtype, so beside int8 codes the routed arm is
* int8 too: the experts read had_quant_act_i8g's codes through the grouped GEMM's int8 rows
* (MoeFP8::a8), which the rotated four-bit plane, the codebook at E4M3 scales a 64 or 32 of K
* and rotated int8 experts have. The codebook at a bf16 scale a 128 and the five-bit one do
* not: under them the connections keep E4M3 codes, as for unrotated experts. */
const bool exp8 = m.moecfg.expert_rot && !m.moecfg.expert_w5 && !m.moecfg.expert_w6 &&
(!m.moecfg.expert_nl || m.moecfg.expert_g64);
m.hccfg.codes_i8 = (m.trunk_i8 && (exp8 || m.moecfg.expert_bf16)) ? 1 : 0;
m.a_x.q8_fed = m.hccfg.codes_i8 != 0;
m.arm8 = m.hccfg.codes_i8 != 0 && exp8;
/* A rotated trunk and the routed experts read one pair: the connections write the block
* input's rotated codes as its own (HyperConn::xrot), and the routed arm takes those
* (MoeFP8's gate_up input) with no second copy. */
if (m.trunk_rot) {
if (!m.hccfg.codes_i8) {
fprintf(stderr, "radiance: qwen4exp: a rotated int8 trunk shares the block input's "
"rotated codes with the routed experts, which then are rotated and "
"have an int8 row (four-bit, the codebook at a 64 or 32 of K, int8), "
"or plain bf16\n");
return RAD_E_UNSUPPORTED;
}
m.a_x.q8_rot = true;
}
m.b_inj = decl_b(b, nm.f("hc_inj"), g.act_dtype, {T, m.hccfg.hc});
m.b_qg = decl_b(b, nm.f("attn_qg"), g.act_dtype, {T, g.n_head, 2 * g.head_dim});
m.b_k = decl_b(b, nm.f("attn_k"), g.act_dtype, {T, kv_dim});
m.b_v = decl_b(b, nm.f("attn_v"), g.act_dtype, {T, kv_dim});
m.b_q = decl_b(b, nm.f("attn_q"), g.act_dtype, {T, q_dim});
m.a_attn.x = decl_b(b, nm.f("attn_out"), g.act_dtype, {T, q_dim});
RAD_ARCH_TRY(m.a_attn.declare_qs(b, nm, g, "attn_out", q_dim, 0, m.trunk_i8));
/* The gate epilogue in front of the int8 out projection writes its int8 codes itself. */
m.a_attn.q8_fed = m.trunk_i8;
m.a_attn.q8_rot = m.trunk_rot;
/* The bf16 gate's own plane (AttnGatedFP8::Wire::gate); the fp8 path fuses it away. */
if (g.w_bf16) m.b_gate = decl_b(b, nm.f("attn_gate"), g.act_dtype, {T, q_dim});
/* The QSA indexer's planes, shared by every full-attention layer like `attn_out`. Declared
* only when the checkpoint carries an indexer; otherwise the layer's declare() is a no-op. */
if (m.qsacfg.heads > 0 && m.qsacfg.head_dim > 0) {
m.qsaw.qk = decl_b(b, nm.f("qsa_qk"), g.act_dtype,
{ g.max_tok, (m.qsacfg.heads + 1) * m.qsacfg.head_dim });
m.qsaw.q = decl_b(b, nm.f("qsa_q"), g.act_dtype,
{ g.max_tok, m.qsacfg.heads * m.qsacfg.head_dim });
const int64_t hd = m.qsacfg.head_dim, rt = m.qsacfg.ratio, wk = m.qsacfg.max_work;
m.qsaw.stage = decl_b(b, nm.f("qsa_stage"), g.act_dtype, { wk * rt, hd });
m.qsaw.page = decl_b(b, nm.f("qsa_page"), RAD_I32, { wk });
/* Three components a block under M-RoPE (arch/common/rad_qsa.h). */
m.qsaw.bpos = g.rope_mc ? decl_b(b, nm.f("qsa_bpos"), RAD_I32, { wk, 3 })
: decl_b(b, nm.f("qsa_bpos"), RAD_I32, { wk });
m.qsaw.nc = decl_b(b, nm.f("qsa_nc"), RAD_I32, { g.max_seqs });
/* A pass tall under a span whose shared steps fit in a pass (RadBuildCtx::
* max_mixed_tok): a wider step is one sequence and scores and selects in windows of a
* pass (QsaIndexer::step, with the bounds in b_pipe_cu). The plane is the largest buffer
* the span would otherwise grow, max_ctx / ratio floats a query row (512 MiB more at 4096
* rows and 256K of context). */
const int64_t qsa_rows = (m.span_on && ctx->max_mixed_tok <= m.moecfg.rows)
? m.moecfg.rows : g.max_tok;
m.qsacfg.plane_rows = qsa_rows < g.max_tok ? qsa_rows : 0;
/* One row per query, not per sequence (arch/common/rad_qsa.h). The score plane is the
* only buffer whose size is the product of two deployment bounds, so an oversized one is
* refused here, naming the two flags, rather than failing later as an allocation. */
const int64_t sc_elems = qsa_rows * m.qsacfg.max_blocks;
if (sc_elems > (int64_t)512 * 1024 * 1024) {
fprintf(stderr,
"radiance: qwen4exp: the QSA score plane would be %lld MiB -- %lld rows "
"(--max-num-batched-tokens %lld) by --max-model-len %lld / compress ratio %lld. "
"Lower either.\n",
(long long)(sc_elems * 4 / (1024 * 1024)), (long long)qsa_rows,
(long long)g.max_tok, (long long)g.max_ctx, (long long)m.qsacfg.ratio);
return RAD_E_UNSUPPORTED;
}
m.qsaw.score = decl_b(b, nm.f("qsa_score"), RAD_F32,
{ qsa_rows, m.qsacfg.max_blocks });
m.qsaw.sel = decl_b(b, nm.f("qsa_sel"), RAD_I32,
{ g.max_tok, m.qsacfg.topk + 1 });
m.qsaw.nsel = decl_b(b, nm.f("qsa_nsel"), RAD_I32, { g.max_tok });
m.qsaw.sequ = decl_b(b, nm.f("qsa_sequ"), RAD_I32, { g.max_tok });
if (m.qsaw.qk && m.qsaw.stage && m.qsaw.page && m.qsaw.bpos && m.qsaw.nc) {
/* What store_keys reads and writes is in use behind the attention's launches when a
* layer's keys are deferred behind them (QsaSide), past the last use the linear order
* gives these buffers, so the planner must not lay an attention buffer over them.
* A few MiB of arena (qk is the largest, max_tok x 640 bf16). */
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.qk));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.stage));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.page));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.bpos));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.nc));
m.qsa_defer = true;
}
if (!m.qsaw.qk || !m.qsaw.q || !m.qsaw.stage || !m.qsaw.page ||
!m.qsaw.bpos || !m.qsaw.nc || !m.qsaw.score || !m.qsaw.sel || !m.qsaw.nsel ||
!m.qsaw.sequ)
return RAD_E_INVAL;
}
m.a_in.x = decl_b(b, nm.f("gdn_in"), g.act_dtype, {T, conv_dim + v_dim});
RAD_ARCH_TRY(m.a_in.declare_qs(b, nm, g, "gdn_in", conv_dim + v_dim));
m.b_ab = decl_b(b, nm.f("gdn_ab"), g.act_dtype, {T, m.gcfg.ab_dim()});
m.b_gq = decl_b(b, nm.f("gdn_q"), g.act_dtype, {T, m.gcfg.n_head_k, m.gcfg.head_k});
m.b_gk = decl_b(b, nm.f("gdn_k"), g.act_dtype, {T, m.gcfg.n_head_k, m.gcfg.head_k});
m.b_gv = decl_b(b, nm.f("gdn_v"), g.act_dtype, {T, m.gcfg.n_head_v, m.gcfg.head_v});
/* Both f32, not the activation dtype; `gdn_conv_prep` requires it and otherwise fails the
* first step with "dtype this kernel does not serve". The log decay is a cumulative sum whose
* differences the chunked scan exponentiates, and bf16's eight mantissa bits are not enough for
* it or for beta. */
m.b_gdec = decl_b(b, nm.f("gdn_gdec"), RAD_F32, {T, m.gcfg.n_head_v});
m.b_beta = decl_b(b, nm.f("gdn_beta"), RAD_F32, {T, m.gcfg.n_head_v});
m.b_kkt = decl_b(b, nm.f("gdn_kkt"), RAD_BF16, {T, m.gcfg.n_head_v, m.gcfg.chunk});
m.a_go.x = decl_b(b, nm.f("gdn_o"), g.act_dtype, {T, m.gcfg.n_head_v, m.gcfg.head_v});
RAD_ARCH_TRY(m.a_go.declare_qs(b, nm, g, "gdn_o", v_dim, 0, m.trunk_i8));
/* The delta net's output epilogue also writes its int8 codes; its prefill quantiser is the
* int8 one. */
m.a_go.q8_fed = m.trunk_i8;
m.a_go.q8_rot = m.trunk_rot;
/* The model's expert count, not this rank's: the router is replicated and scores every
* expert. */
m.b_rlogits = decl_b(b, nm.f("moe_logits"), g.act_dtype, {T, m.moecfg.n_expert_all});
m.b_eids = decl_b(b, nm.f("moe_ids"), RAD_I32, {T, m.moecfg.top_k});
m.b_ew = decl_b(b, nm.f("moe_ew"), g.act_dtype, {T, m.moecfg.top_k});
m.b_eoff = decl_b(b, nm.f("moe_eoff"), RAD_I32, {m.moecfg.n_expert + 1});
/* PERSIST, not transient. `moe_scatter` writes the per-expert histogram and no declared op
* reads it (rad_route_report is a handoff, not an op), so as a transient liveness would let a
* later buffer alias it, and a wide prefill chunk would corrupt the counts (even negative).
*
* The model's routing table (rad_route_table): a row for each trunk layer and one for the
* draft head, at the model's expert count, which is the core's pitch whatever share of the
* experts this rank computes. Each router writes its layer's row, so a report copies nothing
* and a layer's counts last until its next pass. */
m.b_ecnt = decl_b(b, nm.f("moe_ecnt"), RAD_I32,
{g.n_layer + 1, m.moecfg.n_expert_all}, RAD_BUF_PERSIST);
RAD_ARCH_TRY(rad_route_table(b, m.b_ecnt));
m.b_sorted = decl_b(b, nm.f("moe_sorted"), RAD_I32, {ER});
m.b_egu = decl_b(b, nm.f("moe_egu"), g.act_dtype, {ER, 2 * m.moecfg.n_ff_exp});
m.a_eff.x = decl_b(b, nm.f("moe_eff"), g.act_dtype, {ER, m.moecfg.n_ff_exp});
/* ER rows, not max_tok: an expert plane has one row per (token, slot). declare_qs's default
* would make it `top_k` times too short and the gated quantiser would overrun it (see
* ActFP8::declare_qs). */
if (m.arm8) {
/* The int8 routed arm's two pairs, rotated at a scale a 128 and written by their
* producers: the gated product's by gated_had_quant_i8g, the input's by the read. */
/* A trellis gate_up may be paired with W5NL down. Keep its E4M3 pair as well so that
* the gated intermediate can use the down projection's own activation format. */
RAD_ARCH_TRY(m.a_eff.declare_qs(b, nm, g, "moe_eff", m.moecfg.n_ff_exp, ER, true,
m.moecfg.expert_iqr != 0, RAD_FP8_BLOCK));
m.a_eff.q8_fed = m.a_eff.q8_rot = true;
if (!m.trunk_rot) {
RAD_ARCH_TRY(m.a_hr.declare_qs(b, nm, g, "moe_hr", g.n_embd, 0, true, false,
RAD_FP8_BLOCK));
m.a_hr.q8_fed = m.a_hr.q8_rot = true;
}
} else {
RAD_ARCH_TRY(m.a_eff.declare_qs(b, nm, g, "moe_eff", m.moecfg.n_ff_exp, ER));
if (m.moecfg.expert_rot)
RAD_ARCH_TRY(m.a_hr.declare_qs(b, nm, g, "moe_hr", g.n_embd));
}
m.b_edn = decl_b(b, nm.f("moe_edn"), g.act_dtype, {ER, g.n_embd});
m.b_emoe = decl_b(b, nm.f("moe_out"), g.act_dtype, {T, g.n_embd});
if (m.moecfg.n_ff_shared > 0) {
m.b_sgu = decl_b(b, nm.f("moe_sgu"), g.act_dtype, {T, 2 * m.moecfg.n_ff_shared});
m.a_sff.x = decl_b(b, nm.f("moe_sff"), g.act_dtype, {T, m.moecfg.n_ff_shared});
RAD_ARCH_TRY(m.a_sff.declare_qs(b, nm, g, "moe_sff", m.moecfg.n_ff_shared, 0,
m.trunk_i8));
/* The shared expert's gated quantiser in front of its int8 down projection writes its codes
* too. */
m.a_sff.q8_fed = m.trunk_i8;
m.a_sff.q8_rot = m.trunk_rot;
m.b_sout = decl_b(b, nm.f("moe_sout"), g.act_dtype, {T, g.n_embd});
m.b_sgate = decl_b(b, nm.f("moe_sgate"), g.act_dtype, {T, 1});
}
m.b_hout = decl_b(b, nm.f("hout"), g.act_dtype, {g.max_logit_rows, g.n_embd});
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 as in qwen35_fp8. This checkpoint ships
* `lm_head.weight` (tie_word_embeddings is false); the alternative source covers a smaller
* family member that ties them.
*
* The head is declared over all its planes at the codes' dtype (bf16 as shipped, block fp8
* where a recipe makes it so); the selector then picks a `logits_gemm` kernel whose layout
* reads that encoding. The name maps come first, since they are how the head is queried. */
RAD_ARCH_TRY(map_copy(b, nm.f("token_embd.weight"),
nm.ckpt("model.language_model.embed_tokens.weight")));
RAD_ARCH_TRY(map_copy_alt(b, nm.f("output.weight"), nm.ckpt("lm_head.weight"),
nm.ckpt("model.language_model.embed_tokens.weight")));
m.w_tok = decl_w(b, nm.f("token_embd.weight"), RAD_BF16, {g.n_vocab, g.n_embd},
RAD_ACCESS_VOCAB, RAD_SHARD_ROW, grp_model());
m.w_lm_head = decl_w(b, nm.f("output.weight"),
weight_codes_dtype(b, nm.f("output.weight"), RAD_BF16),
{g.n_vocab, g.n_embd}, RAD_ACCESS_VOCAB, RAD_SHARD_ROW, grp_model());
if (!m.w_tok || !m.w_lm_head) return RAD_E_INVAL;
/* The rank's rows, stated explicitly: a vocabulary the world does not divide (248320 at three
* ranks) is split by rad_arch.h's rule (ceil to a 128-row block, last rank short), not the
* core's even share. Where it divides, the span equals the even share. */
if (g.world > 1) {
RAD_ARCH_TRY(rad_weight_shard_span(b, m.w_tok, g.vocab_off, g.vocab_off + g.n_vocab));
RAD_ARCH_TRY(rad_weight_shard_span(b, m.w_lm_head, g.vocab_off, g.vocab_off + g.n_vocab));
}
m.op_embed = rw(b, RAD_OP(b, "embed_lookup",
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("n_embd", g.n_embd),
RAD_INT("n_vocab", g.n_vocab), RAD_STR("dtype", g.dtype),
RAD_INT("vocab_offset", g.vocab_off)),
RAD_WEIGHTS(m.w_tok)),
{}, {m.a_x.x});
/* The encoder's rows overwrite the lookup before its reduction (see MediaRows). */
m.media = VisionTower::present(meta) && ctx->max_enc_patches > 0;
if (m.media) RAD_ARCH_TRY(m.mrows.declare(b, T, g.n_embd, m.a_x.x));
/* Always exact: this sums disjoint rows, a token's embedding coming from exactly one rank. */
if (g.world > 1)
m.op_embed_ar = rw(b, RAD_OP(b, "all_reduce",
RAD_PARAMS(RAD_INT("world_size", g.world),
RAD_RANGE("numel", g.n_embd, T * g.n_embd),
RAD_STR("dtype", g.dtype), RAD_INT("exact", 1)),
RAD_NOWEIGHTS),
{m.a_x.x}, {m.a_x.x});
/* The stream's initial value: hc identical copies of the embedding. */
RAD_ARCH_TRY(m.enter.declare(b, g, m.hccfg.hc, m.a_x.x, m.b_h));
/* The control vector's per-token switch, when this deployment found one (HcProject). All zeros
* is the stock model, so a request that does not ask for the vector is unaffected. */
const rad_buf proj_on = HcProject::declare_mask(b, nm.f("projection_on"), g, ctx->cvec);
/* ---- the layers.
*
* Declaration order is the buffer planner's liveness: a transient lives over [first
* definition, last use] in op indices, and transients with disjoint ranges may share bytes
* (core/build/rad_bufplan.cpp). The buffers above are shared by every layer, so the order
* below must match step()'s issue order (read, block, write, ...). Declaring a connection out
* of order would alias buffers live in the same step, silently corrupting the output.
*/
m.op_rope_cs = rw(b, decl_rotary(b, g, "rope_table",
RAD_PARAMS(RAD_RANGE("M", 1, qk_fuse_rows(g)),
RAD_INT("rot", m.n_rot), RAD_F64("theta", g.theta),
RAD_F64("scale", g.rope_scale), RAD_STR("dtype", "f32")),
RAD_NOWEIGHTS),
{}, {m.b_rope_cs = decl_b(b, nm.f("rope_cs"), RAD_F32,
{rope_table_rows(g), m.n_rot})});
AttnGatedFP8::Wire aw{};
aw.x = m.b_h; aw.h = m.a_x; aw.qg = m.b_qg; aw.k = m.b_k; aw.v = m.b_v;
aw.q = m.b_q; aw.attn = m.a_attn; aw.gate = m.b_gate;
aw.cos_sin = m.op_rope_cs ? m.b_rope_cs : 0;
/* The indexer reads the same rope table: its rotation uses `m.n_rot`, `g.theta` and
* `g.rope_scale`, as the table does, which saves the trunk layers and the head's passes a norm
* and rope each. Set here because the table is declared after `m.qsaw`'s buffers;
* `tw.qsa = m.qsaw` below passes it on to the MTP head. */
m.qsaw.cos_sin = aw.cos_sin;
GdnFP8::Wire gw{};
gw.x = m.b_h; gw.h = m.a_x; gw.in = m.a_in; gw.ab = m.b_ab;
gw.q = m.b_gq; gw.k = m.b_gk; gw.v = m.b_gv;
gw.gdec = m.b_gdec; gw.beta = m.b_beta; gw.kkt = m.b_kkt; gw.o = m.a_go;
MoeFP8::Wire mw{};
mw.x = m.b_h; mw.h = m.a_x; mw.hr = m.a_hr;
mw.logits = m.b_rlogits; mw.ids = m.b_eids; mw.ew = m.b_ew;
mw.sorted = m.b_sorted; mw.eoff = m.b_eoff; mw.ecnt = m.b_ecnt;
mw.ecnt_pitch = m.moecfg.n_expert_all;
mw.egu = m.b_egu; mw.eff = m.a_eff; mw.edn = m.b_edn; mw.moe = m.b_emoe;
mw.sgu = m.b_sgu; mw.sff = m.a_sff; mw.sout = m.b_sout; mw.sgate = m.b_sgate;
HyperConn::Wire hw{};
hw.h = m.b_h; hw.x = m.a_x; hw.inj = m.b_inj;
m.layers.resize((size_t)g.n_layer);
const char* pre = "model.language_model.layers";
/* The pipelined step's all-reduces run beside the other piece's work, so each block declares
* its collective a second time at `overlap` 1, for the rows that leave the CUs to that work
* (the lane rows of libr4d/r4d_rows.cpp). */
const int pipe_overlap = m.pipe_on ? 1 : 0;
/* GPTQ calibration of the trunk (rad_calib_full.h). A bf16 trunk is recorded at any rank count
* from the bf16 inputs, a column-split one gathered to its whole width; an int8 trunk at one
* rank from the codes the connections and the mixers write themselves, so each tap reads what
* the GEMM after it reads. */
if (ctx->calib_dir && *ctx->calib_dir && m.trunk_i8 &&
(g.world > 1 || !m.a_x.q8_fed || !m.a_attn.q8_fed || !m.a_go.q8_fed)) {
fprintf(stderr, "radiance: qwen4exp: --calib-dir records an int8 trunk under int8-routed "
"experts at --tp 1 only\n");
return RAD_E_UNSUPPORTED;
}
RAD_ARCH_TRY(m.calib.begin(b, nm, g, ctx, g.n_layer, std::max(q_dim, v_dim) * g.world,
std::max<int64_t>(g.n_embd, std::max(q_dim, v_dim) * g.world)));
for (int64_t l = 0; l < g.n_layer; ++l) {
Layer& lay = m.layers[(size_t)l];
lay.full = is_full_attention(l, interval) ? 1 : 0;
lay.attn.ar_overlap = lay.gdn.ar_overlap = lay.mlp.ar_overlap = pipe_overlap;
/* PLE comes first, before the attention connection reads the stream: the reference adds
* it into `hidden_states` at the top of the decoder layer, so the following gated residual
* normalises a stream that already includes the n-gram features. It is declared here, not
* hoisted, so its buffers' liveness starts where they are used. */
if (l == m.ple_layer) {
PleLayer::Config pc{};
pc.hc = m.hccfg.hc;
pc.embed_dim = meta_int2(meta, "qwen4exp.ple_embed_dim", "ple_embed_dim", 0);
pc.ngram = meta_int2(meta, "qwen4exp.ngram_size", "ngram_size", 0);
const int64_t per_ngram =
meta_int2(meta, "qwen4exp.heads_per_ngram", "heads_per_ngram", 0);
pc.heads = (pc.ngram - 1) * per_ngram;
pc.eos = meta_int2(meta, "qwen4exp.eos_token_id", "eos_token_id", -1);
pc.shards = meta_int2(meta, "qwen4exp.split_ngram_parts", "split_ngram_parts", 0);
pc.conv_width = meta_int2(meta, "qwen4exp.ple_conv_kernel_size",
"ple_conv_kernel_size", 0);
/* The dilation is the n-gram size (`conv_dilation = config.ngram_size` in the
* reference), not a separate key and not 1. */
pc.dilation = pc.ngram;
pc.rows = ngram_table_rows(
meta_int2(meta, "qwen4exp.ngram_vocab_size_base", "ngram_vocab_size_base", 0),
pc.heads,
meta_int2(meta, "qwen4exp.make_ngram_vocab_size_divisible_by",
"make_ngram_vocab_size_divisible_by", 1));
PleLayer::Wire pw{};
pw.h = m.b_h;
PleLayer::Src ps{};
ps.shard_prefix = nm.ckpt("%s.%lld.ple.ple_embedding.ngram_embedding.shard_",
pre, (long long)l);
ps.shard_suffix = ".weight";
ps.mult = nm.ckpt("%s.%lld.ple.ple_embedding.layer_multipliers",
pre, (long long)l);
ps.vocab_sizes = nm.ckpt("%s.%lld.ple.ple_embedding.ngram_heads_vocab_sizes",
pre, (long long)l);
ps.offsets = nm.ckpt("%s.%lld.ple.ple_embedding.ngram_heads_offsets",
pre, (long long)l);
ps.key_proj = nm.ckpt("%s.%lld.ple.key_proj.weight", pre, (long long)l);
ps.value_proj = nm.ckpt("%s.%lld.ple.value_proj.weight", pre, (long long)l);
ps.norm_key = nm.ckpt("%s.%lld.ple.norm_key.weight", pre, (long long)l);
ps.norm_query = nm.ckpt("%s.%lld.ple.norm_query.weight", pre, (long long)l);
ps.norm_conv = nm.ckpt("%s.%lld.ple.norm_conv.weight", pre, (long long)l);
ps.conv1d = nm.ckpt("%s.%lld.ple.conv1d.weight", pre, (long long)l);
RAD_ARCH_TRY(m.ple.declare(b, nm, g, pc, (int)l, pw, ps));
}
HyperConn::Src mixs{};
mixs.norm = nm.ckpt("%s.%lld.attn_hyper_connection.hc_norm.weight", pre, (long long)l);
mixs.mix_down = nm.ckpt("%s.%lld.attn_hyper_connection.input_mix_weight_down.weight",
pre, (long long)l);
mixs.mix_up = nm.ckpt("%s.%lld.attn_hyper_connection.input_mix_weight_up.weight",
pre, (long long)l);
mixs.inject_w = nm.ckpt("%s.%lld.attn_hyper_connection.block_inject_weight.weight",
pre, (long long)l);
RAD_ARCH_TRY(lay.hc_mix.declare(b, nm, g, nm.f("blk.%lld.attn_hc", (long long)l),
m.hccfg, hw, mixs));
/* `Src::norm = nullptr` is the blocks' "input already normed and quantised" mode, and
* `add_out = false` stops them adding into the stream; the connection's write half does
* that with the four gains the read produced. `fold` and `ar_in` are off because both fuse
* into a norm these blocks do not have. */
if (lay.full) {
AttnGatedFP8::Src as{};
as.qg = nm.ckpt("%s.%lld.self_attn.q_proj.weight", pre, (long long)l);
as.k = nm.ckpt("%s.%lld.self_attn.k_proj.weight", pre, (long long)l);
as.v = nm.ckpt("%s.%lld.self_attn.v_proj.weight", pre, (long long)l);
as.q_norm = nm.ckpt("%s.%lld.self_attn.q_norm.weight", pre, (long long)l);
as.k_norm = nm.ckpt("%s.%lld.self_attn.k_norm.weight", pre, (long long)l);
as.o = nm.ckpt("%s.%lld.self_attn.o_proj.weight", pre, (long long)l);
/* The KV page is the QSA compress block, so the indexer's selection is a paged block
* table rather than a new attention kernel (docs/QSA.md); the attention rows must
* declare the same 4 or nothing resolves. Page size only affects the block-table walk:
* pages of 4 and 16 cost the same at long context and give identical text. */
lay.attn.kv_block = 4;
/* The selection must be set before the attention is declared: `attn_paged` declares
* `sel` and `seqused` as reads, and a read declared after the op does not count
* (rad_block_attn_gated_fp8.h). The indexer itself is declared after the attention
* block, where the sparse attention reads its ops.
*
* Below the indexer's budget the selection is every visible block, so sparse and dense
* paths must give identical text; any difference is a defect. */
if (m.qsaw.qk) {
lay.attn.qsa_sel = m.qsaw.sel;
lay.attn.qsa_nsel = m.qsaw.nsel;
lay.attn.qsa_sequ = m.qsaw.sequ;
lay.attn.qsa_topk = m.qsacfg.topk;
/* The exactness bound, at or below which a step of several rows a sequence
* (prefill, a verify) issues the cheaper dense attention. Plain decode keeps the
* sparse row's launch at every context, so that kernel is exercised on every
* step. */
lay.attn.qsa_exact_to = m.qsacfg.budget + m.qsacfg.ratio - 1;
/* At or below that bound the indexer scores nothing (AttnGatedFP8::qsa_use): a
* row alone in its sequence is at the context's last position on this model (a
* plain decode row; the core's and the card's `seqused` is its position + 1), and
* pass_key() carries the exact bound's side of it. */
lay.attn.qsa_table_rows = true;
lay.attn.qsa_exact_host = true;
}
RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, m.kv_full));
/* An attention-zero rank keeps no attention cache: its block is a fill of zeros
* (rad_block_attn_gated_fp8.h), and the indexer reads only the shared block table. The
* call is idempotent. */
if (g.attn_zero && m.kv_full) RAD_ARCH_TRY(rad_kv_group_zero(b, m.kv_full));
lay.attn.ar_out_take = lay.hc_mix.ar_take();
RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, m.kv_full, m.n_rot, aw, as,
false, false, kArNone, lay.hc_mix.op_ar_write != 0));
/* The indexer, after the attention block so its ops sit where the sparse attention
* reads them. It reads the same normed fp8 input as the projections
* (arch/common/rad_qsa.h, docs/QSA.md). */
if (m.qsaw.qk) {
QsaIndexer::Config qc = m.qsacfg;
qc.mode = lay.attn.mode;
qc.qk_proj = nm.ckpt("%s.%lld.self_attn.indexer.index_qk_proj.weight",
pre, (long long)l);
qc.q_norm = nm.ckpt("%s.%lld.self_attn.indexer.q_layernorm.weight",
pre, (long long)l);
qc.k_norm = nm.ckpt("%s.%lld.self_attn.indexer.k_layernorm.weight",
pre, (long long)l);
qc.kv_attn = m.kv_full;
qc.emit_sel = lay.attn.qsa_sel != 0;
RAD_ARCH_TRY(lay.qsa.declare(b, nm, g, (int)l, qc, m.qsaw, aw.h,
&m.kv_qsa_bkey));
}
} else {
GdnFP8::Src gs{};
gs.in_qkv = nm.ckpt("%s.%lld.linear_attn.in_proj_qkv.weight", pre, (long long)l);
gs.in_a = nm.ckpt("%s.%lld.linear_attn.in_proj_a.weight", pre, (long long)l);
gs.in_b = nm.ckpt("%s.%lld.linear_attn.in_proj_b.weight", pre, (long long)l);
gs.in_z = nm.ckpt("%s.%lld.linear_attn.in_proj_z.weight", pre, (long long)l);
gs.conv1d = nm.ckpt("%s.%lld.linear_attn.conv1d.weight", pre, (long long)l);
gs.a_log = nm.ckpt("%s.%lld.linear_attn.A_log", pre, (long long)l);
gs.dt_bias = nm.ckpt("%s.%lld.linear_attn.dt_bias", pre, (long long)l);
gs.out_norm = nm.ckpt("%s.%lld.linear_attn.norm.weight", pre, (long long)l);
gs.out = nm.ckpt("%s.%lld.linear_attn.out_proj.weight", pre, (long long)l);
RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, m.kv_state));
RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, m.kv_conv));
lay.gdn.ar_out_take = lay.hc_mix.ar_take();
RAD_ARCH_TRY(lay.gdn.declare(b, nm, g, m.gcfg, (int)l, m.kv_state, m.kv_conv, gw, gs,
false, false, kArNone, lay.hc_mix.op_ar_write != 0));
}
HyperConn::Src ffns{};
ffns.norm = nm.ckpt("%s.%lld.mlp_hyper_connection.hc_norm.weight", pre, (long long)l);
ffns.mix_down = nm.ckpt("%s.%lld.mlp_hyper_connection.input_mix_weight_down.weight",
pre, (long long)l);
ffns.mix_up = nm.ckpt("%s.%lld.mlp_hyper_connection.input_mix_weight_up.weight",
pre, (long long)l);
ffns.inject_w = nm.ckpt("%s.%lld.mlp_hyper_connection.block_inject_weight.weight",
pre, (long long)l);
/* The rotated expert plane's input codes come out of this read, beside the plain ones
* the shared arm takes, so the routed arm declares no quantiser of its own. */
HyperConn::Config fcfg = m.hccfg;
HyperConn::Wire fw = hw;
if (m.moecfg.expert_rot && !m.trunk_rot) {
fcfg.rotate = 1; fw.rq = m.a_hr.cq(); fw.rs = m.a_hr.cs();
}
RAD_ARCH_TRY(lay.hc_ffn.declare(b, nm, g, nm.f("blk.%lld.ffn_hc", (long long)l),
fcfg, fw, ffns));
MoeFP8::Src ms{};
ms.cfg = m.moecfg;
/* The legacy expert tap can be limited to selected layers, including the MTP
* layer below. Keep the whole-expert placement selected by m.moecfg.calib. */
ms.cfg.calib = m.moecfg.calib && calib_layer_on((int)l);
ms.router = nm.ckpt("%s.%lld.mlp.gate.weight", pre, (long long)l);
RAD_ARCH_TRY(moe_expert_sources(b, nm, "layer",
nm.ckpt("%s.%lld.mlp.experts", pre, (long long)l), &ms));
if (m.moecfg.n_ff_shared > 0) {
ms.shared = nm.ckpt("%s.%lld.mlp.shared_expert", pre, (long long)l);
ms.shared_gate = nm.ckpt("%s.%lld.mlp.shared_expert_gate.weight", pre, (long long)l);
}
lay.mlp.ar_out_take = lay.hc_ffn.ar_take();
lay.mlp.hr_in = fcfg.rotate != 0;
RAD_ARCH_TRY(lay.mlp.declare(b, nm, g, (int)l, mw, ms, false, false, kArNone,
lay.hc_ffn.op_ar_write != 0));
/* The write behind the block takes its gather where it can, declared after the block so
* the buffers it reads are the block's outputs in declaration order. */
{
HyperConn::GatherSrc gs{};
gs.ye = lay.mlp.w.edn;
gs.ew = lay.mlp.w.ew;
gs.sorted = lay.mlp.w.sorted;
if (lay.mlp.gfold) { gs.sh = lay.mlp.w.sout; gs.sg = lay.mlp.w.sgate; }
gs.top_k = lay.mlp.c.top_k;
gs.rows = lay.mlp.c.rows;
RAD_ARCH_TRY(lay.hc_ffn.declare_gather(b, gs));
lay.mlp.gather_out_rows = lay.hc_ffn.gather_rows;
lay.mlp.gather_out_rows6 = lay.hc_ffn.gather_rows6;
}
/* The control vector's projection for this layer, if the vector steers it. */
RAD_ARCH_TRY(lay.proj.declare(b, nm.f("blk.%lld.proj", (long long) l), g, l, ctx->cvec,
proj_on, m.b_h, m.hccfg.hc));
if (m.calib.records(l)) {
/* Named by the container's weights, which is how a quantiser asks for a Hessian. */
const ActFP8& ao = lay.full ? m.a_attn : m.a_go;
const int64_t ko = lay.full ? q_dim : v_dim; /* this rank's columns of the output */
const std::string L = "blk." + std::to_string(l);
const std::string in_stem = L + (lay.full ? ".attn_qkv.input" : ".ssm_inz.input");
const std::string in_lin = lay.full ? L + ".attn_qg," + L + ".attn_k," + L + ".attn_v"
: L + ".ssm_inz";
const std::string o_stem = L + (lay.full ? ".attn_output.input" : ".ssm_out.input");
const std::string o_lin = L + (lay.full ? ".attn_output" : ".ssm_out");
lay.cs_in = (int)m.calib.sites.size();
if (m.trunk_i8) {
const uint32_t dom = m.trunk_rot ? (uint32_t)RAD_FP8_BLOCK : 0u;
RAD_ARCH_TRY(m.calib.site_i8(b, g, l, in_stem, in_lin, m.a_x.q8, m.a_x.s8, g.n_embd, dom));
lay.cs_o = (int)m.calib.sites.size();
RAD_ARCH_TRY(m.calib.site_i8(b, g, l, o_stem, o_lin, ao.q8, ao.s8, ko, dom));
} else {
RAD_ARCH_TRY(m.calib.site(b, g, l, in_stem, in_lin, m.a_x.x, g.n_embd, g.n_embd));
lay.cs_o = (int)m.calib.sites.size();
RAD_ARCH_TRY(m.calib.site(b, g, l, o_stem, o_lin, ao.x, ko, ko * g.world));
}
}
}
/* Each routed layer predicts the next one's experts at its router input (MoeFP8::xp_next). */
for (size_t l = 0; l + 1 < m.layers.size(); ++l) m.layers[l].mlp.xp_next = &m.layers[l + 1].mlp;
/* ---- the 97th connection: no write half, no fp8 copy. It produces the bf16 hidden state the
* lm_head reads; its `hc_norm` takes the place of a final norm (the checkpoint has no
* `model.language_model.norm.weight`). */
{
HyperConn::Config mc = m.hccfg;
mc.inject = 0;
mc.quant = 0;
HyperConn::Wire mixw{};
mixw.h = m.b_h; mixw.x.x = m.a_x.x; /* q/s deliberately absent */
HyperConn::Src mixs{};
mixs.norm = nm.ckpt("model.language_model.hyper_connection_mixer.hc_norm.weight");
mixs.mix_down = nm.ckpt("model.language_model.hyper_connection_mixer."
"input_mix_weight_down.weight");
mixs.mix_up = nm.ckpt("model.language_model.hyper_connection_mixer."
"input_mix_weight_up.weight");
RAD_ARCH_TRY(m.mixer.declare(b, nm, g, nm.f("output_hc"), mc, mixw, mixs));
}
/* ---- the issue order of a decode step's small launches (AttnGatedFP8::kv_first_rows,
* MoeFP8::sh_down_first_lo): at decode and verify heights the k and v projections go in
* front of the q|gate projection, and from kVerifyRows rows the shared down projection in
* front of the routed down GEMM, so the card launches the small ones while the wide one is
* still being placed. The same kernels on the same operands; only what a recorded pass
* starts first changes. */
{
for (Layer& lay : m.layers) {
if (lay.full) lay.attn.kv_first_rows = kPrefetchRows;
lay.mlp.sh_down_first_lo = kVerifyRows;
lay.mlp.sh_down_first_hi = kPrefetchRows;
}
}
/* ---- the delta nets' out projections, read beside their decode recurrent updates
* (kGdnOutPrefetchBytes), and the full-attention layers' beside the attention
* (kAttnOutPrefetchBytes). At more than one rank; --weight-prefetch off declares none. */
if (g.world > 1 && !ctx->weight_prefetch_off && rad_op_declared(b, "prefetch")) {
for (Layer& lay : m.layers) {
if (lay.full) {
lay.attn.op_pf_o = decl_prefetch(b, lay.attn.o.w, kAttnOutPrefetchBytes);
lay.attn.op_pf_o_v = decl_prefetch(b, lay.attn.o.w, kAttnOutPrefetchBytes,
kPrefetchBlocksVerify);
lay.attn.pf_o_v_rows = kVerifyRows;
continue;
}
lay.gdn.op_pf_out = decl_prefetch(b, lay.gdn.out.w, kGdnOutPrefetchBytes);
lay.gdn.pf_out_rows = kPrefetchRows;
lay.gdn.pf_out_seqs = kGdnOutPrefetchSeqs;
lay.gdn.op_pf_out_v = decl_prefetch(b, lay.gdn.out.w, kGdnOutPrefetchBytes,
kPrefetchBlocksVerify);
lay.gdn.pf_out_v_rows = kVerifyRows;
}
for (Layer& lay : m.layers) {
if (!lay.mlp.w_router) continue;
lay.op_pf_router = decl_prefetch(b, lay.mlp.w_router, kRouterPrefetchBytes);
lay.op_pf_router_v = decl_prefetch(b, lay.mlp.w_router, kRouterPrefetchBytes,
kPrefetchBlocksVerify);
}
}
m.op_gather = rw(b, RAD_OP(b, "gather_rows",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows),
RAD_INT("n", g.n_embd), RAD_STR("dtype", g.dtype)),
RAD_NOWEIGHTS),
{m.a_x.x}, {m.b_hout});
m.op_logits = rw(b, RAD_OP(b, "logits_gemm",
RAD_PARAMS(RAD_RANGE("M", 1, g.max_logit_rows),
RAD_INT("n_vocab", g.n_vocab), RAD_INT("n_embd", g.n_embd),
RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(m.w_lm_head)),
{m.b_hout}, {m.b_logits});
if (!m.op_embed || !m.op_gather || !m.op_logits) return RAD_E_INVAL;
/* ---- the multi-token-prediction head (arch/common/rad_block_mtp_hc.h).
*
* Declared after op_logits; do not move it. Declaration order is the planner's liveness, and a
* drafter declared before the trunk's tail ops would share a trunk buffer with one of the
* head's, giving correct output with zero acceptance.
*
* Declared only when the checkpoint carries a head and the deployment speculates; at
* max_spec 0 the weights stay off the card (the container still carries them, since
* rad-convert declares at a non-zero max_spec). */
const int64_t nextn = meta_int2(meta, "qwen4exp.mtp_num_hidden_layers",
"mtp_num_hidden_layers", 0);
if (nextn > 0 && g.max_spec > 0) {
const char* mpre = "mtp";
/* Sized at max_tok, not max_logit_rows: the history pass that fills the head's own K and V
* has one row per token over a whole prefill chunk. */
m.b_mtp_hin = decl_b(b, nm.f("mtp_hin"), g.act_dtype, {T, HN});
m.b_mtp_hw = decl_b(b, nm.f("mtp_hw"), g.act_dtype, {T, HN});
m.b_mtp_e = decl_b(b, nm.f("mtp_e"), g.act_dtype, {T, g.n_embd});
m.b_mtp_hout = decl_b(b, nm.f("mtp_hout"), g.act_dtype, {g.max_logit_rows, g.n_embd});
/* The 2-bit draft head, when the model holds one: asked once here, since the answer
* decides the head's buffers and the block reads it back from them. */
if (mtp_draft_head_q2(b, MtpHcBlock::head_name(nm), nm.ckpt("lm_head.weight"))) {
const int64_t dmpad = ((g.max_logit_rows + 15) / 16) * 16;
m.b_mtp_dq = decl_b(b, nm.f("mtp_dq"), RAD_I8, {g.max_logit_rows, g.n_embd});
m.b_mtp_ds = decl_b(b, nm.f("mtp_ds"), RAD_F32, {g.max_logit_rows, 1});
m.b_mtp_dsum = decl_b(b, nm.f("mtp_dsum"), RAD_F32, {g.n_embd / 128, dmpad});
/* This rank's rows, not the model's: the 2-bit head is vocab-parallel and the merge
* reassembles it (rad_arch.h). */
m.b_mtp_dlog = decl_b(b, nm.f("mtp_dlog"), g.act_dtype,
{g.max_logit_rows, vocab_head_rows(g)});
if (vocab_head_sharded(g)) {
m.b_mtp_dpair = decl_b(b, nm.f("mtp_dpair"), RAD_F32,
{g.max_logit_rows, kDraftTopR, 2});
m.b_mtp_dgath = decl_b(b, nm.f("mtp_dgath"), RAD_F32,
{g.max_logit_rows, (int64_t)g.world * kDraftTopR, 2});
}
/* The proposal: a draft round's token comes from row_topk, never the sampler.
* kDraftTopR columns, of which a draft reads column 0; the rest are the coarse head's
* other candidates, which `logit_rerank` scores first, and the width also gives the
* vocab-parallel all-gather a 16-byte row (rad_block_mtp_hc.h, kDraftTopR). */
m.b_mtp_didx = decl_b(b, nm.f("mtp_didx"), RAD_I32, {g.max_logit_rows, kDraftTopR});
m.b_mtp_dval = decl_b(b, nm.f("mtp_dval"), RAD_F32, {g.max_logit_rows, kDraftTopR});
/* --draft-coupling (RadBuildCtx::draft_coupling). Q is the law of the 2-bit candidate
* head's reranked top-8; a plain bf16 head leaves one candidate and nothing to couple
* over. The engine checks the drafter shape (a linear serial chain). */
if (ctx->draft_coupling) {
if (!m.b_mtp_didx || g.max_spec < 1 || g.max_spec > RAD_MTP_COUPLE_MAX_ROUNDS) {
rad_note(b, "--draft-coupling on requires the 2-bit candidate head's reranked "
"top8 and max_spec 1..%d",
RAD_MTP_COUPLE_MAX_ROUNDS);
return RAD_E_UNSUPPORTED;
}
m.b_mtp_couple_draws = decl_b(b, nm.f("mtp_couple_draws"), RAD_U32,
{g.max_seqs, RAD_MTP_COUPLE_DRAW_WORDS},
RAD_BUF_PERSIST);
m.b_mtp_couple_state = decl_b(b, nm.f("mtp_couple_state"), RAD_U32,
{g.max_spec, g.max_seqs,
RAD_MTP_COUPLE_STATE_WORDS},
RAD_BUF_PERSIST);
if (!m.b_mtp_couple_draws || !m.b_mtp_couple_state) return RAD_E_INVAL;
rad_note(b, "MTP coupled: the head samples x~Q from its reranked top8 each round "
"(FP64), and the sampler couples the target's own draw to it; %lld "
"round(s), fixed-seed tokens are NOT preserved",
(long long)g.max_spec);
}
}
MtpHcBlock::Wire tw{};
tw.src = m.b_h; tw.hin = m.b_mtp_hin; tw.hw = m.b_mtp_hw;
tw.e = m.b_mtp_e; tw.inj = m.b_inj; tw.hout = m.b_mtp_hout;
tw.logits = m.b_logits; tw.x = m.a_x;
tw.dq = m.b_mtp_dq; tw.ds = m.b_mtp_ds; tw.dsum = m.b_mtp_dsum;
tw.dlog = m.b_mtp_dlog; tw.didx = m.b_mtp_didx; tw.dval = m.b_mtp_dval;
tw.dpair = m.b_mtp_dpair; tw.dgath = m.b_mtp_dgath;
tw.couple_draws = m.b_mtp_couple_draws; tw.couple_state = m.b_mtp_couple_state;
/* The head reuses the trunk's working buffers, all dead by the time a draft round runs (a
* second set would be 150 MiB). Only the wide stream and the carry are its own, since
* they must survive the round that produced them. */
tw.attn = aw; tw.attn.x = m.b_mtp_hw;
tw.mlp = mw; tw.mlp.x = m.b_mtp_hw;
/* The QSA wire is within-step scratch and a draft round is its own step, so the head
* shares all of it. The QSA tail is per-layer state (`kv_qsa_tail`), not in the wire, and
* declare() binds the head's layer to it like any other. */
tw.qsa = m.qsaw;
MtpHcBlock::Src ts{};
ts.fc_hidden = nm.ckpt("%s.fc_hidden.weight", mpre);
ts.fc_embedding = nm.ckpt("%s.fc_embedding.weight", mpre);
ts.pre_norm_hidden = nm.ckpt("%s.pre_fc_norm_hidden.weight", mpre);
ts.pre_norm_embedding = nm.ckpt("%s.pre_fc_norm_embedding.weight", mpre);
ts.hc_attn.norm = nm.ckpt("%s.layers.0.attn_hyper_connection.hc_norm.weight", mpre);
ts.hc_attn.mix_down = nm.ckpt("%s.layers.0.attn_hyper_connection."
"input_mix_weight_down.weight", mpre);
ts.hc_attn.mix_up = nm.ckpt("%s.layers.0.attn_hyper_connection."
"input_mix_weight_up.weight", mpre);
ts.hc_attn.inject_w = nm.ckpt("%s.layers.0.attn_hyper_connection."
"block_inject_weight.weight", mpre);
ts.hc_mlp.norm = nm.ckpt("%s.layers.0.mlp_hyper_connection.hc_norm.weight", mpre);
ts.hc_mlp.mix_down = nm.ckpt("%s.layers.0.mlp_hyper_connection."
"input_mix_weight_down.weight", mpre);
ts.hc_mlp.mix_up = nm.ckpt("%s.layers.0.mlp_hyper_connection."
"input_mix_weight_up.weight", mpre);
ts.hc_mlp.inject_w = nm.ckpt("%s.layers.0.mlp_hyper_connection."
"block_inject_weight.weight", mpre);
/* The head's own mixer has no `block_inject_weight`, like the trunk's 97th connection:
* nothing follows it to write back into. */
ts.hc_mix.norm = nm.ckpt("%s.hyper_connection_mixer.hc_norm.weight", mpre);
ts.hc_mix.mix_down = nm.ckpt("%s.hyper_connection_mixer.input_mix_weight_down.weight",
mpre);
ts.hc_mix.mix_up = nm.ckpt("%s.hyper_connection_mixer.input_mix_weight_up.weight", mpre);
ts.attn.qg = nm.ckpt("%s.layers.0.self_attn.q_proj.weight", mpre);
ts.attn.k = nm.ckpt("%s.layers.0.self_attn.k_proj.weight", mpre);
ts.attn.v = nm.ckpt("%s.layers.0.self_attn.v_proj.weight", mpre);
ts.attn.q_norm = nm.ckpt("%s.layers.0.self_attn.q_norm.weight", mpre);
ts.attn.k_norm = nm.ckpt("%s.layers.0.self_attn.k_norm.weight", mpre);
ts.attn.o = nm.ckpt("%s.layers.0.self_attn.o_proj.weight", mpre);
ts.mlp.cfg = m.moecfg;
ts.mlp.cfg.calib = m.moecfg.calib && calib_layer_on((int)g.n_layer);
ts.mlp.router = nm.ckpt("%s.layers.0.mlp.gate.weight", mpre);
RAD_ARCH_TRY(moe_expert_sources(b, nm, "mtp", nm.ckpt("%s.layers.0.mlp.experts", mpre),
&ts.mlp));
if (m.moecfg.n_ff_shared > 0) {
ts.mlp.shared = nm.ckpt("%s.layers.0.mlp.shared_expert", mpre);
ts.mlp.shared_gate = nm.ckpt("%s.layers.0.mlp.shared_expert_gate.weight", mpre);
}
/* Set before declare(), as for the trunk: `attn_paged` declares `sel` and `seqused` as
* reads, which must precede the op. */
m.mtp.attn.kv_block = 4;
/* The head's layer takes the trunk's issue order at decode heights (declare, above). */
m.mtp.attn.kv_first_rows = kPrefetchRows;
m.mtp.mlp.sh_down_first_lo = kVerifyRows;
m.mtp.mlp.sh_down_first_hi = kPrefetchRows;
if (m.qsaw.qk) {
m.mtp.attn.qsa_sel = m.qsaw.sel;
m.mtp.attn.qsa_nsel = m.qsaw.nsel;
m.mtp.attn.qsa_sequ = m.qsaw.sequ;
m.mtp.attn.qsa_topk = m.qsacfg.topk;
m.mtp.attn.qsa_exact_to = m.qsacfg.budget + m.qsacfg.ratio - 1;
/* As the trunk's layers: a round's row is the head's context's last position
* (ADV_ROUND), and the history pass is one row only where it commits one. */
m.mtp.attn.qsa_table_rows = true;
m.mtp.attn.qsa_exact_host = true;
ts.qsa = m.qsacfg;
ts.qsa.mode = m.mtp.attn.mode;
ts.qsa.qk_proj = nm.ckpt("%s.layers.0.self_attn.indexer.index_qk_proj.weight", mpre);
ts.qsa.q_norm = nm.ckpt("%s.layers.0.self_attn.indexer.q_layernorm.weight", mpre);
ts.qsa.k_norm = nm.ckpt("%s.layers.0.self_attn.indexer.k_layernorm.weight", mpre);
ts.qsa.kv_attn = m.kv_full;
ts.qsa.emit_sel = m.mtp.attn.qsa_sel != 0;
}
m.mtp.media = m.media;
RAD_ARCH_TRY(m.mtp.declare(b, nm, g, (int)g.n_layer, m.kv_full, &m.kv_qsa_bkey, m.n_rot,
m.w_tok, m.w_lm_head, m.hccfg, g.max_spec, tw, ts));
/* A head pass refills the rope table ahead of the head's ops (step), while the carry
* `mtp_hin` still holds the rows the previous pass left. The refill is the trunk's
* `rope_table` op, so the table's planned life would end at its last declared reader,
* which is in the trunk when no head op reads the table (no fused prologue), and the
* planner could then put the carry in the table's bytes. Declared as written by the
* head's first op, the table is live where the carry is. */
if (m.op_rope_cs && rad_op_writes(b, m.mtp.op_gather, &m.b_rope_cs, 1) < 0)
return RAD_E_INVAL;
m.have_mtp = true;
/* Serial drafter: `max_spec` dependent passes, each embedding the last one's proposal.
* The 2-bit head writes its own argmax into `mtp_didx`; without it `mtp_didx` is 0 and the
* bf16 head goes through the ordinary sampler chain. */
RadDrafterDecl dd{};
dd.name = "mtp";
dd.kind = RAD_DRAFT_SERIAL;
dd.depth = ctx->max_spec;
dd.proposal = m.b_mtp_didx;
dd.proposal_pitch = kDraftTopR; /* the pick is column 0; see kDraftTopR */
dd.mask_token = -1;
RAD_ARCH_TRY(rad_declare_drafter(b, &dd));
rad_note(b, "MTP head: one gated-attention layer with 512 routed experts over the wide "
"stream, %d serial draft round(s)", ctx->max_spec);
} else if (nextn > 0) {
rad_note(b, "the checkpoint's `mtp.` head is not loaded: this deployment does not "
"speculate");
}
/* The vision tower, last: an encoder pass issues nothing else, so its activations share the
* arena with every buffer above. Its weight groups start past every text layer and the MTP
* head. */
if (m.media) RAD_ARCH_TRY(m.vit.declare(b, meta, ctx, nm, g.n_embd, (int)g.n_layer + 64));
return RAD_OK;
}
/* ============================== A prefill step wider than one MoE pass ==============================
*
* Every layer of a prefill chunk makes two all-reduces, the mixer's and the MoE's, each in a hard
* chain: mixer -> all-reduce -> write -> read -> MoE -> all-reduce -> write -> read. At four ranks
* a chunk's 97 tiered-int8 two-shots are about a quarter of its time, with nothing beside them on
* one stream.
*
* Under --prefill-span the scheduler hands a prefill alone in its step several checkpoint
* intervals, each piece the interval-sized chunk a span of 1 makes. Everything a piece can run by
* itself runs a piece at a time, and every all-reduce goes out on the second lane, beside the
* other piece's work on the first:
*
* the mixer the delta net runs whole a piece at a time (projection, convolution, solve and
* scan over the piece's bounds, the state handed on through the cache as between
* two chunks), and the attention runs its gated sparse path a piece at a time;
* each hands its first piece's all-reduce off behind that piece's out projection
* (ArPieces), so it runs beside the second piece's mixer;
* the MoE a pass a piece: the mixer's all-reduce of piece 1 runs beside piece 0's write,
* read and pass, the MoE's of piece 0 beside piece 1's;
* the last one the MoE's all-reduce of piece 1 runs beside piece 0's write and the next
* layer's read.
*
* The indexer's selection and the embedding run over the whole step.
*
* A piece computes the bytes the narrower chunk does: every all-reduce message has a piece's rows,
* so the tiered-int8 shards and their owners are the ones a span of 1 sends; every connection read
* is a piece, so its down product splits K the same way (HyperConn::read_pieces); every MoE pass
* and delta-net piece is a chunk's, and attention rows are their own. The first interval of a
* prompt is never spanned (Scheduler::plan_chunk), so the dense attention below QSA's exactness
* bound is taken where it was.
*
* The second lane's requirements (Ctx::lane1_) hold: every collective of the step is on it,
* including the embedding's; none carries an arrival counter; and the one buffer it touches is out
* of the sharing pool (declare). The collectives there are declared at `overlap` 1, whose rows
* leave the CUs to the work beside them. */
static bool pipelined(const Model& m, int64_t T) {
const int64_t R = m.moecfg.rows;
if (!m.pipe_on || R <= 0 || T <= R) return false;
if (m.moecfg.calib || m.calib.on || dbg_resid_on() || !m.op_embed_ar) return false;
for (const Layer& l : m.layers) {
if (!(l.full ? l.attn.issues_ar(T) : l.gdn.issues_ar(T))) return false;
if (!l.mlp.issues_ar(T, R)) return false;
}
return true;
}
/* The mixer's reduction of a piece the block has finished (ArPieces), on the second lane behind
* the out projection that wrote it. */
struct MixAr {
RadCtx* c = nullptr;
const Layer* l = nullptr;
};
static void mix_ar_piece(void* u, int64_t r0, int64_t n) {
const MixAr& s = *(const MixAr*)u;
rad_lane(s.c, 1);
rad_lane_join(s.c, 0, 1);
if (s.l->full) s.l->attn.issue_ar(s.c, r0, n);
else s.l->gdn.issue_ar(s.c, r0, n);
rad_lane(s.c, 0);
}
/* The bounds a step wider than a pass is cut by, on the card: {0, pass, T}. The first two are the
* window of the indexer's score plane (QsaIndexer::step, Config::plane_rows) and the delta net's
* first piece (ArPieces::cu), copied from the pinned host copy; T is copied on the card from the
* step's cu_seqlens. Both are issued on the step's stream ahead of every layer by each step of one
* sequence wider than a pass, since the buffer is a transient (declare). Null where no step can be
* wider than a pass (no b_pipe_cu) or the pinned copy could not be allocated. */
static const int32_t* pipe_bounds(RadCtx* c, const RadBatch* batch, Model& m) {
if (!m.b_pipe_cu) return nullptr;
if (!m.pipe_cu_tried) {
m.pipe_cu_tried = true;
m.pipe_cu_h = (int32_t*)rad_dev_alloc(2 * sizeof(int32_t), RAD_MEM_HOST_PINNED);
if (m.pipe_cu_h) {
m.pipe_cu_h[0] = 0;
m.pipe_cu_h[1] = (int32_t)m.moecfg.rows;
} else if (m.g.rank == 0) {
fprintf(stderr, "radiance: qwen4exp: no pinned host memory for the window bounds; a "
"step wider than a pass cannot run\n");
}
}
if (!m.pipe_cu_h) return nullptr;
int32_t* d = (int32_t*)rad_buf_ptr(c, m.b_pipe_cu);
if (batch->n_tok > m.moecfg.rows && batch->n_seq == 1) {
const RadStream s = rad_stream(c);
if (rad_memcpy_async(d, m.pipe_cu_h, 2 * sizeof(int32_t), s) != RAD_OK ||
rad_memcpy_async(d + 2, batch->cu_seqlens + 1, sizeof(int32_t), s) != RAD_OK)
return nullptr;
}
return d;
}
static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int32_t* pcu) {
const Geom& g = m.g;
const int64_t T = batch->n_tok, R = m.moecfg.rows;
const int64_t K = (T + R - 1) / R;
auto r0 = [&](int64_t p) { return p * R; };
auto rn = [&](int64_t p) { return T - p * R < R ? T - p * R : R; };
/* A collective on the second lane, ordered behind lane 0 where it reads what lane 0 just
* wrote (a join to a lane not yet built is a no-op). */
auto on1 = [&](bool behind0, auto&& issue) {
rad_lane(c, 1);
if (behind0) rad_lane_join(c, 0, 1);
issue();
rad_lane(c, 0);
};
auto wait1 = [&] { rad_lane_join(c, 1, 0); };
if (!m.pipe_noted) {
m.pipe_noted = true;
if (g.rank == 0)
fprintf(stderr, "radiance: qwen4exp: prefill steps wider than a MoE pass are pipelined: "
"%lld rows in %lld pieces of %lld, all-reduces on the second lane\n",
(long long)T, (long long)K, (long long)R);
}
RAD_ISSUE(c, m.op_embed,
praw(batch->token_ids, RAD_I32, T), RAD_W(m.w_tok), brows(m.a_x.x, T));
m.mrows.step(c, batch, m.a_x.x, T);
on1(true, [&] { RAD_ISSUE_N(c, m.op_embed_ar, T * g.n_embd, brows(m.a_x.x, T), RAD_NONE); });
wait1();
if (m.ple_layer >= 0) m.ple.ids(c, batch);
m.enter.step(c, T);
if (m.op_rope_cs && T <= qk_fuse_rows(g) && !rope_mixed(batch))
RAD_ISSUE_N(c, m.op_rope_cs, T, rope_pos1(batch, T), RAD_B(m.b_rope_cs));
/* The delta net runs a piece at a time over the bounds (ArPieces::cu) in a step of one
* prefill sequence in two pieces. */
const int32_t* gcu = pcu && K == 2 && batch->n_seq == 1 && batch->phase == RAD_PHASE_PREFILL
? pcu : nullptr;
const int64_t n = (int64_t)m.layers.size();
if (n > 0) {
if (m.ple_layer == 0) m.ple.step(c, batch);
for (int64_t p = 0; p < K; ++p) m.layers[0].hc_mix.read(c, T, r0(p), rn(p));
}
/* The pass's first full-attention layer with an indexer makes what every such layer shares of
* its step (QsaIndexer::step, `first`). */
bool qsa_first = true;
for (int64_t li = 0; li < n; ++li) {
const Layer& l = m.layers[(size_t)li];
/* The mixer finishes its output a piece at a time where its last stages are per row and
* hands the first pieces' reductions to the second lane as it goes (ArPieces), so the
* first piece's all-reduce runs beside the next piece's attention or out projection. */
MixAr ms{c, &l};
ArPieces pc{};
pc.rows = R; pc.done = mix_ar_piece; pc.u = &ms; pc.cu = gcu;
if (l.full) {
l.qsa.step(c, l.attn.w.h, batch, pcu, l.attn.qsa_use(batch), qsa_first);
if (l.qsa.on()) qsa_first = false;
l.attn.step(c, batch, false, &pc);
}
else l.gdn.step(c, batch, false, &pc);
auto ar_mix = [&](int64_t p) {
if (l.full) l.attn.issue_ar(c, r0(p), rn(p));
else l.gdn.issue_ar(c, r0(p), rn(p));
};
/* A block that handed nothing leaves the first piece's all-reduce to be issued here. */
if (pc.handed == 0) on1(true, [&] { ar_mix(0); });
wait1();
int64_t issued = pc.handed > 0 ? pc.handed : 1;
bool behind = pc.handed == 0; /* lane 1 already behind the whole mixer */
for (int64_t p = 0; p < K; ++p) {
if (p + 1 < K && p + 1 >= issued) {
on1(!behind, [&] { ar_mix(p + 1); });
behind = true;
issued = p + 2;
}
l.hc_mix.write(c, T, r0(p), rn(p));
l.hc_ffn.read(c, T, r0(p), rn(p));
l.mlp.pass(c, T, r0(p), rn(p), false);
/* Lane 0 waits here for the next piece's mixer all-reduce and the last piece's MoE
* one, both issued before this pass; lane 1 then takes this piece's behind its
* gather. */
wait1();
on1(true, [&] { l.mlp.issue_ar(c, r0(p), rn(p)); });
}
l.mlp.route_report(c);
/* The writes, and the next layer's reads beside the last piece's all-reduce. PLE adds into
* every row of the stream before the next layer reads it, so there the reads wait. */
const Layer* nx = li + 1 < n ? &m.layers[(size_t)li + 1] : nullptr;
const bool ple_next = nx && li + 1 == m.ple_layer;
for (int64_t p = 0; p < K; ++p) {
if (p == K - 1) wait1();
l.hc_ffn.write(c, T, r0(p), rn(p));
/* The projection of this piece, before the next layer reads it. */
l.proj.step(c, batch, r0(p), rn(p));
if (nx && !ple_next) nx->hc_mix.read(c, T, r0(p), rn(p));
}
if (ple_next) {
m.ple.step(c, batch);
for (int64_t p = 0; p < K; ++p) nx->hc_mix.read(c, T, r0(p), rn(p));
}
}
}
/* A full-attention layer's indexer keys behind its attention's launches (QsaIndexer::deferrable,
* SideOps): store_keys's three stages in turn, one a call, and whatever is left at flush(). */
struct QsaSide {
RadCtx* c = nullptr;
const QsaIndexer* q = nullptr;
const RadBatch* batch = nullptr;
int next = 1;
void one() { if (next <= 3) q->step_part(c, batch, next++); }
void flush() { while (next <= 3) one(); }
static void at(void* u, int) { ((QsaSide*)u)->one(); }
};
/* The trunk one stream at a time over the whole step: every step a span of 1 makes, and every
* other where step_pipelined's conditions do not hold. */
static void step_serial(RadCtx* c, const RadBatch* batch, Model& m, const int32_t* pcu) {
const Geom& g = m.g;
const int64_t T = batch->n_tok;
RAD_ISSUE(c, m.op_embed,
praw(batch->token_ids, RAD_I32, T), RAD_W(m.w_tok), brows(m.a_x.x, T));
m.mrows.step(c, batch, m.a_x.x, T);
/* Unconditional, unlike qwen35_fp8's: the first block has no norm to fold it into, only a
* gated-residual read. */
if (m.op_embed_ar)
RAD_ISSUE_N(c, m.op_embed_ar, T * g.n_embd, brows(m.a_x.x, T), RAD_NONE);
/* The PLE's hash, ahead of every layer: its host gather waits for it, and the layers issued
* between the two keep the device busy while the host gathers (rad_block_ple.h). */
if (m.ple_layer >= 0) m.ple.ids(c, batch);
m.enter.step(c, T);
if (m.op_rope_cs && T <= qk_fuse_rows(g) && !rope_mixed(batch))
RAD_ISSUE_N(c, m.op_rope_cs, T, rope_pos1(batch, T), RAD_B(m.b_rope_cs));
/* The pass's first full-attention layer with an indexer makes what every such layer shares of
* its step (QsaIndexer::step, `first`). */
bool qsa_first = true;
for (int64_t li = 0; li < (int64_t)m.layers.size(); ++li) {
const Layer& l = m.layers[(size_t)li];
/* Before the attention connection reads the stream, which is where the reference puts it. */
if (li == m.ple_layer) m.ple.step(c, batch);
l.hc_mix.read_pieces(c, T);
/* The calibration taps read the codes the mixer's GEMMs read: its input before it runs,
* its output projection's input before the feed-forward's connection reuses the pair. */
if (l.cs_in >= 0) m.calib.tap(c, (size_t)l.cs_in, T);
/* The indexer runs first, since the attention reads its selection. The block's input is
* already normed and quantised by the connection (`ext_in`), so `w.h` is final. */
if (l.full) {
const int use = l.attn.qsa_use(batch);
/* At decode heights within the exactness bound nothing of the layer reads the
* indexer's keys or tail (they are for later steps), so only the projection stays in
* front, beside the attention's own, and the three launches behind it ride behind the
* attention's (QsaSide) instead of holding its projections back in turn: -0.3% to
* -0.8% a step on chain 3 with coupling at tp 2. */
if (m.qsa_defer && T <= kPrefetchRows && batch->phase == RAD_PHASE_DECODE &&
l.qsa.deferrable(batch, use, qsa_first)) {
QsaSide sd{c, &l.qsa, batch};
const SideOps so{QsaSide::at, &sd};
l.qsa.step_proj(c, l.attn.w.h, batch);
l.attn.step(c, batch, true, nullptr, &so);
sd.flush();
} else {
l.qsa.step(c, l.attn.w.h, batch, pcu, use, qsa_first);
l.attn.step(c, batch);
}
if (l.qsa.on()) qsa_first = false;
}
else l.gdn.step(c, batch);
if (l.cs_o >= 0) m.calib.tap(c, (size_t)l.cs_o, T);
/* The router's weight, read beside the write's reduction and the read behind it. */
if (batch->phase == RAD_PHASE_DECODE)
prefetch_beside(c, l.op_pf_router_v && T >= kVerifyRows ? l.op_pf_router_v
: l.op_pf_router,
l.mlp.w_router, T);
l.hc_mix.write(c, T, 0, T);
dbg_resid(c, (int)li, "mix", g.n_embd, m.b_h, m.a_x.x);
l.hc_ffn.read_pieces(c, T);
l.mlp.step(c, batch);
l.hc_ffn.write(c, T, 0, T);
/* The request's control vector, on the stream the write just finished. */
l.proj.step(c, batch, 0, T);
dbg_resid(c, (int)li, "ffn", g.n_embd, m.b_h, m.a_x.x);
}
m.calib.after_step(c, T);
}
static void step(RadCtx* c, const RadBatch* batch) {
Model& m = g_model[rad_slot(c)];
const Geom& g = m.g;
const int64_t T = batch->n_tok;
/* An encoder pass runs only the vision tower (RadBatch::enc). */
if (batch->enc) {
m.vit.step(c, batch);
return;
}
/* An MTP pass runs only the head. The trunk already left its wide stream in `h`; the head
* turns the rows it wants into a draft token or one more position of its own attention
* history, by the sign of `draft_pass` (rad_block_mtp_hc.h). Zero is a trunk step. */
if (batch->draft_pass != 0) {
if (!m.have_mtp) {
rad_step_fail(c, "the batch asks for a drafter pass and no drafter is declared: the "
"container carries no `mtp.` head, or declare ran at max_spec 0");
return;
}
/* Fill the head's own cos/sin rows. A draft round runs past the committed end (round k
* writes K and V at position head_i + k - 1), at rows the preceding trunk step never
* wrote. Without this, later rounds would rotate by stale rows: only acceptance drops, and
* because the indexer reads the same table, the head's QSA selection and so the accepted
* text shift at tie level at long context. A row depends only on its position, so
* re-filling is idempotent. */
if (m.op_rope_cs && T <= qk_fuse_rows(g) && !rope_mixed(batch))
RAD_ISSUE_N(c, m.op_rope_cs, T, rope_pos1(batch, T), RAD_B(m.b_rope_cs));
/* The n-gram rows of the pass's newest tokens, for the verify step that runs them:
* hashed ahead of the head, fetched once the whole pass is issued (PleLayer::hint_ids). */
if (m.ple_layer >= 0) m.ple.hint_ids(c, batch);
m.mtp.qsa_win_cu = pipe_bounds(c, batch, m);
m.mtp.step(c, batch);
if (m.ple_layer >= 0) m.ple.hint_fetch(c, batch);
return;
}
const int32_t* pcu = pipe_bounds(c, batch, m);
if (pipelined(m, T)) step_pipelined(c, batch, m, pcu);
else step_serial(c, batch, m, pcu);
m.mixer.read_pieces(c, T);
if (batch->n_out > 0) {
RAD_ISSUE_N(c, m.op_gather, batch->n_out,
brows(m.a_x.x, T), praw(batch->out_ids, RAD_I32, batch->n_out),
brows(m.b_hout, batch->n_out));
RAD_ISSUE_N(c, m.op_logits, batch->n_out,
brows(m.b_hout, batch->n_out), RAD_W(m.w_lm_head),
brows(m.b_logits, batch->n_out));
}
}
/* The default draft depth, used by `--num-speculative-tokens auto` and by rad-convert.
*
* 3 is an operating point, not a model property (`mtp_num_hidden_layers` only says the one-layer
* head exists), so `draft_depth_fixed` stays 0. Throughput peaks at 3 on this model: per-position
* acceptance stays high at every depth, but each extra verify row costs real time because the
* weights do not fit device memory (more experts streamed, more n-gram rows faulted from the
* mapped table). */
uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) {
if (!c || !batch) return 0;
if (batch->enc) return 1;
Model& m = g_model[rad_slot(c)];
/* Which side of the indexer's exactness bound the step is on by the attention group's exact
* host bound, by which the full-attention layers and the indexer decide whether the step
* needs scores (AttnGatedFP8::qsa_within, qsa_exact_host). The core's key holds only the
* rounded context bound, so the side is keyed here: one more recording of each pass kind as a
* request crosses the bound. `sel` is 2 within the bound and 0 past it or where it is
* unknown, which is where the blocks fall back to the rounded bound the core keys. */
uint64_t sel = 0;
if (used_max_h && batch->kv && m.kv_full && m.qsacfg.budget > 0 && m.qsacfg.ratio > 0)
for (int i = 0; i < batch->n_kv_groups; ++i)
if (batch->kv[i].group == m.kv_full) {
if (used_max_h[i] > 0 &&
used_max_h[i] <= m.qsacfg.budget + m.qsacfg.ratio - 1) sel = 2;
break;
}
/* A decode pass, a speculative verify or an MTP draft included, issues every launch at the
* step's keyed context bound (RadArgs::kv_used_max_h), never the live maximum, so the core's
* key holds its geometry. */
if (batch->phase == RAD_PHASE_DECODE) return 1 | sel;
/* Mixed batches split the query rows into decode and prefill subsets before attention, which
* read the live maximum. The whole-batch host summary cannot key those subset geometries
* exactly, nor a drafter's or a verify's rows inside a prefill pass. */
if (batch->phase == RAD_PHASE_MIXED || batch->draft_pass != 0 || batch->n_spec > 0) return 0;
bool has_full_attention = false;
for (const Layer& layer : m.layers) {
if (!layer.full) continue;
if (layer.attn.kv_block != 4) return 0;
has_full_attention = true;
}
if (!has_full_attention) return 1 | sel;
if (m.g.head_dim != 256 || (m.g.gqa() != 6 && m.g.gqa() != 12) || !m.kv_full ||
!used_max_h || !batch->kv || batch->n_kv_groups <= 0)
return 0;
int group = -1;
for (int i = 0; i < batch->n_kv_groups; ++i)
if (batch->kv[i].group == m.kv_full) { group = i; break; }
if (group < 0) return 0;
/* Qwen always passes cu_seqlens, so fill_paged takes q_len = min(band, total_q). That equals
* n_tok only for one sequence; a ragged or multi-sequence launch's band is not the host group
* max, and mixed decode-prefix / prefill-suffix launches use the suffix's own KV lengths. */
if (batch->n_seq != 1 || batch->n_tok <= 0 || m.g.max_ctx <= 0) return 0;
const int q_len = (int)batch->n_tok;
const int total_q = (int)batch->n_tok;
const int num_seqs = 1;
const int kv_heads = (int)m.g.n_head_kv;
const int used = used_max_h[group];
if (used <= 0) return 0; // Missing host summary is not the same as an empty KV cache.
const int wgps = rad_ctx_wgp_count(c);
if (wgps <= 0) return 0;
constexpr int tile = 48, nwarps = 24;
const int bq = nwarps * 16 / m.g.gqa();
const int splits = rad_prefill_splits_for_live(bq, tile, q_len, total_q, num_seqs,
kv_heads, m.g.max_ctx, used, wgps);
const uint64_t grid = rad_prefill_grid_key(q_len, bq, splits, kv_heads, num_seqs, nwarps * 32);
/* 0 stays "unknown"; the side of the bound rides in a bit the grid key's mix does not leave
* meaningful, so two sides are two keys. */
return grid ? ((grid << 2) | 1 | sel) : 0;
}
static int probe(const RadModelMeta* meta, RadArchProbe* out) {
if (!meta || !out) return RAD_E_INVAL;
/* The gated attention block declares the streamed store and the resolve
* (rad_block_attn_gated_fp8.h). */
out->kv_stream_ok = 1;
out->draft_depth = 0;
if (meta_int2(meta, "qwen4exp.mtp_num_hidden_layers", "mtp_num_hidden_layers", 0) > 0)
out->draft_depth = 3;
out->shape_probe_ok = 1; /* declare() writes scratch under shape_probe */
/* The gated residual applies a control vector after every block's write (HcProject). */
out->control_vector_ok = 1;
/* A pass is about 930 launches, nearly all dependent, and the kernels of its decode heights
* have chained forms: on two R9700 a full pass under the draft head went from 13.9 to 12.5 ms
* and the step without the head from 9.98 to 9.17 ms at one sequence. */
out->chain_pays = RAD_CHAIN_ALWAYS;
return RAD_OK;
}
} /* namespace qwen4exp_fp8 */
#ifndef RAD_ARCH_NO_EXPORTS
RAD_ARCH_PROBE(qwen4exp_fp8)
/* Every per-instance state above is indexed by slot (rad_build_slot / rad_slot). */
RAD_ARCH_MULTI_INSTANCE()
RAD_ARCH_PLUGIN(qwen4exp_fp8, "qwen4exp", "", "0.1.0",
"Qwen4-Exp (Qwen3.8-Flash-Next): hybrid Gated DeltaNet / gated attention, a gated "
"residual four streams wide and 512 routed experts, from a dense bf16 checkpoint "
"quantised ahead of time by a recipe")
extern "C" uint64_t rad_arch_pass_key(RadCtx* c, const RadBatch* batch,
const int32_t* used_max_h) {
return qwen4exp_fp8::pass_key(c, batch, used_max_h);
}
#endif