Archived
New models and formats: - IQR for Qwen3.8-27B: weights on the int8 grid under GPTQ, a trellis down each GPTQ column (IQR-V2), and IQR-V3, a computed trellis code under one scale a row, kept at its codes' width in VRAM and decoded in the GEMM; 15 IQR-V3 recipes from ~2 to ~7 bits a weight (data/recipes/q38-27b-*-v3.recipe, docs/IQR.md) - libhalo: a kernel library for Strix Halo (gfx1151), preferred there over libr3d - Nemotron 3.5 Embed 1B and 8B (nemotron35_embed): text and image embeddings through a Pixtral tower, on the card and on the CPU - Nemotron 3.5 Rerank 8B - /v1/systemone: Cloudflare's clef and clef-flash, Convai Innovations' Laya and Mica beside LiquidAI d1; scored steps read the candidates' logits from the head table with no lm_head pass - LFM2's 8-bit trunk: block-fp8 or int8 linears with the fused producers, and the d1-3B recipe - DeepSeek-V4.1's reply format with DSML tool calls and typed argument tags Weights on the drive: - The file tier reads only the experts the pass routes rows to; the draft head's weights stay in the host pool - A hot VRAM cache for routed experts read off the drive, reads deposited straight into free host pool slots, and copyless evictions; the file tier's counters are now ssd_* - GLM-5 Next shards its routed experts by expert, not by width, so they can live on the drive - The stall watch counts drive reads as progress, and the DMA lane's room check reads the lane's own completion fence Quantisation tooling: - --calib-corpus, --calib-dir and --calib-layers record each linear's input Hessian; beside --kld-record the reference's own run records them - --kld-top N records a sparse KL reference (the N most likely tokens and the tail's mass) - GPTQ in a third of the time with the same codes, trellis GPTQ on the card, vector codebooks with coordinate descent, and rad-convert --reuse repeated, each container supplying what the earlier ones lack Speculation: - Prompt lookup alone (--spec-ngram K, --spec-ngram-min/-max): each sequence keeps an index of its n-grams, and a proposal is verified only when its measured step cost says it pays; --draft-window adaptive follows it Speed: - IQR decode: the narrow tile splits K for four times the int8 tile's waves and folds each 128-K block once; at 8 tokens down and the output projections run in 0.61-0.82x and 0.70-0.92x FP8's time (2-6 bits); IQ6R-S-V3 with its DFlash2 drafter steps in 26.3 ms at TP2, against 29.4 ms before and FP8's 27.3 - Qwen MXFP4 feed-forward: the gated fold up to 16 rows, not 8 - libr4d: the gated prefill fold, the short-prompt split row, and the rows forms of rmsnorm and rope; KLATE again for the head-256 gqa6 paged prefill; gram_accum on the matrix cores Features: - /health, /v1/health and /ping answer 503 with "status": "stuck" once the device has advanced nothing for a minute or a queue has faulted, so a supervisor can restart a wedged server (#356) - --image-gen-max-pixels and --image-gen-max-refs size an image generator's buffers (#344) - The startup report lists only the options not at their default, with a count; -v lists all (#250) - --preempt-wait-ms bounds how long a preempted prefill waits for its whole-recompute room - --placement layer_offload spills dense layers so one --max-model-len sequence fits - Startup refuses amdgpu with CWSR off and says how to fix it; --proceed-with-incompatible-configuration starts anyway under an UNSUPPORTED banner Fixes: - The n-gram read-ahead hands each row to the reader once and reads only as far ahead as the row cache holds, and pauses while decode reads: about one drive read a row instead of two (#355) - A file-tier window reads each of its runs once, not once a run per run - The decode split law's verify floor is bounded by the batch's workgroups: max_seqs 256 no longer declares 64x the scratch (#342) - A prefetch DMA list's mark stays out of the signal pool until the engine has read it, and a prefill stage copies only the blocks its own tables name (#343) - The split-KV row sum is read with fetch-inactive (#328) - LFM2 on the host backend: libref and libavx serve the row-form ops it declares (#348) - The preempted-prefill gate skips a KV group that holds no blocks (#352) - Every request entry point bounded, gated and sanitised (JSON depth, invalid UTF-8, control bytes in the log, the dashboard's CSP, cross-origin browsers on the loopback-only routes); a prompt id outside the vocabulary refused at admission; --api-key "" refused at startup - The telemetry the HTTP thread reads is atomic - An RMS norm row no longer depends on how many rows its launch carries; prefill attention's rescale is decided by live rows only - A --kld-record prefill chunk that finishes no sequence gets its logits on a tied-head model - A kernel's lazily made counter block is zeroed outside the lock other ranks take (a TP2 first-step hang) Changes: Plugin ABI 32. Every plugin exports a layout fingerprint of the ABI structs and the loader refuses a plugin without it or with another (#302), so out-of-tree plugins must be rebuilt. Appended: RadBuildCtx calib_* and gen_max_*, RadArchProbe gen_max_*, RadBatch score_only and plugin_words, RadDeviceProps::unified, RadChatFormat arg_json_value_prefix.
549 lines
30 KiB
C++
549 lines
30 KiB
C++
/* qwen35_bf16 -- Qwen3.5 / 3.6 / 3.8 dense, bf16 weights and bf16 activations.
|
|
*
|
|
* Qwen3.5, 3.6 and 3.8 dense models report the architecture id `qwen35`; `qwen35moe` is the same
|
|
* graph with a routed FFN. Dimensions come from metadata, so this file serves every size (0.8B to
|
|
* 27B). docs/ARCHITECTURES.md has the geometry and its plausible-but-wrong traps.
|
|
*
|
|
* One plugin implements one architecture id for one quantisation scheme (spec §2.4); a quantised
|
|
* variant is a separate plugin composing the same blocks against other ops (e.g.
|
|
* rmsnorm_had_quant_i8, gemm_nt_q), so declare() below has no branch on quantisation.
|
|
*
|
|
* ============================== WHAT IT SERVES ==============================
|
|
*
|
|
* The unquantised release (quantisation descriptor ""), every projection a bf16 GEMM. The
|
|
* block-scaled fp8 release is qwen35_fp8's. The lm_head is declared at whatever the model holds,
|
|
* so a recipe-made block-fp8 head uses the fp8 logits kernel.
|
|
*
|
|
* ============================== THE SHAPE, AT 27B ==============================
|
|
*
|
|
* 64 layers, full_attention_interval 4 -> layers 3,7,...,63 are full attention (16 of them),
|
|
* the other 48 are gated delta net
|
|
* hidden 5120 ffn 17408 vocab 248320 (lm_head not tied)
|
|
* 24 heads / 4 kv heads / head_dim 256 -> GQA 6; head_dim is read, not derived
|
|
* (n_embd / n_head would be 213)
|
|
* attn_output_gate -> q_proj is [n_embd, 2 * n_head * head_dim], query and gate interleaved
|
|
* partial_rotary_factor 0.25 -> MROPE over the first 64 of 256 head dims, theta 1e7
|
|
* linear: 16 key heads / 48 value heads of 128, conv width 4, fp32 recurrent state
|
|
*
|
|
* ============================== WHAT THIS PLUGIN DOES NOT DECLARE ==============================
|
|
*
|
|
* The MTP / NextN head (one extra decoder block plus fc, enorm, hnorm) is not declared here; it
|
|
* is not needed for text generation. It sits beside the trunk under its own `mtp.` prefix, so
|
|
* `nextn_predict_layers` is not subtracted from the layer count (see declare()); running it as a
|
|
* trunk layer would be a different model.
|
|
*
|
|
* The vision tower (27 blocks, hidden 1152, 16 heads of 72, patch 16, out 5120) is declared
|
|
* through arch/common/rad_block_vit.h, only when the container carries one and the deployment
|
|
* asked for media.
|
|
*/
|
|
#include <arch/rad_arch.h>
|
|
#include <arch/rad_block_attn_gated.h>
|
|
#include <arch/rad_block_gdn.h>
|
|
#include <arch/rad_block_mlp.h>
|
|
#include <arch/rad_block_vit.h>
|
|
#include <arch/rad_calib_full.h>
|
|
|
|
#include <vector>
|
|
|
|
namespace qwen35_bf16 {
|
|
|
|
using namespace rad::arch;
|
|
|
|
/* One trunk layer, of one of two kinds; both small structs are held rather than a union. `full`
|
|
* is the only branch in step(), and it is fixed per layer by the model. */
|
|
struct Layer {
|
|
int full = 0;
|
|
AttnGatedBF16 attn;
|
|
GdnBF16 gdn;
|
|
MlpBF16 mlp;
|
|
/* The layer's calibration inputs (rad_calib_full.h): the attention or delta net's, its output
|
|
* projection's, the feed-forward's and down_proj's; -1 where the run does not record it. */
|
|
int cs_in = -1, cs_o = -1, cs_mlp = -1, cs_down = -1;
|
|
};
|
|
|
|
struct Model {
|
|
Names nm{""};
|
|
Geom g{};
|
|
GdnBF16::Config gcfg{};
|
|
int64_t n_rot = 0;
|
|
int64_t n_main = 0; /* trunk layers, excluding the MTP block */
|
|
|
|
rad_kvgroup kv_full = 0, kv_state = 0, kv_conv = 0;
|
|
|
|
/* the residual stream and the two vocabulary edges */
|
|
rad_buf b_x = 0, b_h = 0, b_hout = 0, b_logits = 0;
|
|
/* full attention */
|
|
rad_buf b_qg = 0, b_k = 0, b_v = 0, b_q = 0, b_gate = 0, b_attn = 0;
|
|
/* gated delta net */
|
|
rad_buf b_in = 0, b_z = 0, b_gq = 0, b_gk = 0, b_gv = 0;
|
|
rad_buf b_gdec = 0, b_beta = 0, b_kkt = 0, b_go = 0;
|
|
/* feed-forward */
|
|
rad_buf b_gate_up = 0, b_ffn = 0;
|
|
|
|
rad_weight w_tok = 0, w_out_norm = 0, w_lm_head = 0;
|
|
rad_op op_embed = 0, op_embed_ar = 0, op_final_norm = 0, op_gather = 0, op_logits = 0;
|
|
/* Set by a plugin built over this one whose head answers a request from its words
|
|
* (RadBatch::plugin_words): a `fill` of the logits rows that plugin declared. A step every
|
|
* sampled row of which carries words then skips the lm_head and leaves those zero rows for the
|
|
* sampler, whose token the request discards. Zero, the lm_head runs as it always has, and the
|
|
* graph this plugin declares is unchanged. */
|
|
rad_op op_logits_fill = 0;
|
|
|
|
/* ---- media (arch/common/rad_block_vit.h): the vision tower and the scatter of its rows,
|
|
* declared only when the container carries a tower and the deployment asked for media. */
|
|
bool media = false;
|
|
VisionTower vit{};
|
|
MediaRows mrows{};
|
|
|
|
std::vector<Layer> layers;
|
|
|
|
/* --calib-dir: the whole Hessian of every linear's input over a --calib-corpus run. */
|
|
CalibFull calib;
|
|
int cs_head = -1; /* the lm_head's input, recorded with the last layer */
|
|
};
|
|
|
|
/* Declare runs once per rank, in one process, on the rank's own thread (spec §1, §9), so the
|
|
* model state is indexed by rank and read back in step() with rad_rank(). */
|
|
static Model g_model[MAX_RANKS];
|
|
|
|
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) return RAD_E_INVAL;
|
|
|
|
Model& m = g_model[ctx->rank];
|
|
m = Model{};
|
|
m.nm = Names(ctx->scope);
|
|
RAD_ARCH_TRY(geom_from(m.g, meta, ctx, "bf16", RAD_BF16));
|
|
|
|
/* A routed model is a different graph, served by the qwen35moe plugin. */
|
|
if (meta->n_expert > 0) {
|
|
fprintf(stderr, "radiance: qwen35_bf16 refuses a checkpoint with %lld experts; that is "
|
|
"qwen35moe\n", (long long)meta->n_expert);
|
|
return RAD_E_UNSUPPORTED;
|
|
}
|
|
|
|
Geom& g = m.g;
|
|
Names& nm = m.nm;
|
|
|
|
/* ---- geometry RadModelMeta does not name, from the container's free-form header. Both
|
|
* spellings are accepted: HuggingFace keys (from safetensors) and llama.cpp's (from GGUF). */
|
|
bool have_interval = false;
|
|
const int64_t interval = meta_int2(meta, "qwen35.full_attention_interval",
|
|
"full_attention_interval", 4, &have_interval);
|
|
const int64_t nextn = meta_int2(meta, "qwen35.nextn_predict_layers",
|
|
"mtp_num_hidden_layers", 0);
|
|
if (interval <= 0 || nextn < 0 || nextn >= g.n_layer) {
|
|
fprintf(stderr, "radiance: qwen35: full_attention_interval %lld / nextn %lld out of range "
|
|
"for %lld layers\n",
|
|
(long long)interval, (long long)nextn, (long long)g.n_layer);
|
|
return RAD_E_INVAL;
|
|
}
|
|
RAD_ARCH_TRY(check_layer_schedule("qwen35", meta_text_gets(meta, "layer_types", nullptr),
|
|
g.n_layer, interval, {"full_attention"}));
|
|
/* Every declared layer is a trunk layer: `mtp_num_hidden_layers` is not part of
|
|
* `num_hidden_layers` in this family (the MTP block ships separately under `mtp.`).
|
|
* Subtracting it would drop full-attention layer 63 and silently run a trunk one layer short.
|
|
* `nextn` is used only for the note below. */
|
|
m.n_main = g.n_layer;
|
|
|
|
/* Qwen3.5's norms are GemmaRMSNorm, `x_hat * (1 + w)` (vLLM imports GemmaRMSNorm as
|
|
* Qwen3_5RMSNorm). This covers input_layernorm, post_attention_layernorm, the final norm and
|
|
* the q/k head norms, but not the delta net's ssm_norm, a plain RMSNormGated (gains ~0.88).
|
|
* The checkpoint confirms it: layer norm weights of 0.07..0.29 and partly negative q/k norm
|
|
* weights only make sense as `1 + w`. The final norm (0.68..0.96) is ambiguous and follows the
|
|
* reference implementation. */
|
|
g.wadd = 1.0f;
|
|
g.wadd_qk = 1.0f;
|
|
const float final_wadd = 1.0f;
|
|
|
|
/* The rotary width: partial_rotary_factor 0.25 over head_dim 256 is 64. A container that
|
|
* omits it is refused rather than defaulted to a full rotation, which would give fluent wrong
|
|
* text (spec §17). */
|
|
m.n_rot = meta_int2(meta, "qwen35.rope.dimension_count", "rope_dimension_count", 0);
|
|
if (m.n_rot <= 0) {
|
|
/* Both spellings occur in this family: Qwen3.8-27B-FP8 states `partial_rotary_factor`
|
|
* at the top level and inside `rope_parameters`, Qwen3.5-0.8B only inside
|
|
* `rope_parameters` (rad-convert flattens nested config with a dot). */
|
|
double prf = rad_meta_getf(meta, "partial_rotary_factor", 0.0);
|
|
if (!(prf > 0.0)) prf = rad_meta_getf(meta, "rope_parameters.partial_rotary_factor", 0.0);
|
|
if (prf > 0.0) m.n_rot = (int64_t)(g.head_dim * prf);
|
|
}
|
|
if (m.n_rot <= 0 || m.n_rot > g.head_dim || (m.n_rot & 1)) {
|
|
fprintf(stderr, "radiance: qwen35: no usable rotary width -- give the container "
|
|
"qwen35.rope.dimension_count or partial_rotary_factor (head_dim %lld)\n",
|
|
(long long)g.head_dim);
|
|
return RAD_E_INVAL;
|
|
}
|
|
|
|
RAD_ARCH_TRY(yarn_from(g, meta, "", m.n_rot));
|
|
|
|
/* The linear-attention geometry. llama.cpp maps the GGUF ssm keys onto these:
|
|
* ssm.state_size -> the key AND value head width, ssm.group_count -> key heads,
|
|
* ssm.time_step_rank -> value heads, ssm.inner_size -> value heads * value width. */
|
|
GdnBF16::Config& gc = m.gcfg;
|
|
const int64_t n_head_k_all = meta_int2(meta, "qwen35.ssm.group_count",
|
|
"linear_num_key_heads", 0);
|
|
const int64_t n_head_v_all = meta_int2(meta, "qwen35.ssm.time_step_rank",
|
|
"linear_num_value_heads", 0);
|
|
gc.head_k = meta_int2(meta, "qwen35.ssm.state_size", "linear_key_head_dim", 0);
|
|
gc.head_v = meta_int2(meta, "qwen35.ssm.value_head_dim", "linear_value_head_dim", 0);
|
|
gc.conv_width = meta_int2(meta, "qwen35.ssm.conv_kernel", "linear_conv_kernel_dim", 0);
|
|
if (gc.head_v <= 0) {
|
|
const int64_t inner = meta_int2(meta, "qwen35.ssm.inner_size", "linear_inner_size", 0);
|
|
if (inner > 0 && n_head_v_all > 0) gc.head_v = inner / n_head_v_all;
|
|
}
|
|
if (n_head_k_all <= 0 || n_head_v_all <= 0 || gc.head_k <= 0 || gc.head_v <= 0 ||
|
|
gc.conv_width <= 0) {
|
|
fprintf(stderr, "radiance: qwen35: 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)n_head_k_all, (long long)n_head_v_all, (long long)gc.head_k,
|
|
(long long)gc.head_v, (long long)gc.conv_width);
|
|
return RAD_E_INVAL;
|
|
}
|
|
/* The chunked scan's tile length, not a model parameter: libr4d's
|
|
* gdn_chunk_scan_k128_v128_c64_bf16, and also the linear-state checkpoint interval
|
|
* (spec §7.3). Note: gdn_conv_prep takes it while gdn_chunk_scan derives it from the kernel,
|
|
* and nothing checks that the two agree. */
|
|
gc.chunk = 64;
|
|
|
|
int s;
|
|
if ((s = divide_or_fail(n_head_k_all, g.world, "linear_num_key_heads", &gc.n_head_k)) < 0)
|
|
return s;
|
|
if ((s = divide_or_fail(n_head_v_all, g.world, "linear_num_value_heads", &gc.n_head_v)) < 0)
|
|
return s;
|
|
|
|
const int64_t q_dim = g.q_dim();
|
|
const int64_t kv_dim = g.kv_dim();
|
|
const int64_t v_dim = gc.v_dim();
|
|
const int64_t conv_dim = gc.conv_dim();
|
|
|
|
/* ---- three KV groups (spec §7.2). Every layer is bound below to the group it owns state
|
|
* in; a group with no bound layers is a declare error, since the bound layer count is how
|
|
* many state instances a sequence holds.
|
|
*
|
|
* The full-attention group's block size is not declared: it comes from the resolved attention
|
|
* kernel and is read back with rad_kv_block_size() inside the block. */
|
|
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;
|
|
m.kv_full = rad_decl_kv_group(b, nm.f("kv_attn"), &fd);
|
|
g.kv_dtype = rad_dtype_name(fd.dtype);
|
|
|
|
/* The recurrent state is fp32, as `mamba_ssm_dtype: float32` asks; an f16 state changes the
|
|
* generated text (qwen35_fp8.cpp). It costs 48 x 128 x 128 x 4 = 3 MiB per layer, 144 MiB
|
|
* across the 48 linear layers per sequence, the largest item in the memory plan. */
|
|
RadKVGroupDecl sd{};
|
|
sd.kind = RAD_KV_LINEAR;
|
|
sd.dtype = RAD_F32;
|
|
sd.n_head_kv = gc.n_head_v;
|
|
sd.state_dim[0] = gc.head_v;
|
|
sd.state_dim[1] = gc.head_k;
|
|
m.kv_state = rad_decl_kv_group(b, nm.f("kv_gdn_state"), &sd);
|
|
|
|
/* The conv window: conv_width-1 + n_spec entries of the whole convolved row, counted in heads
|
|
* so the channel split never straddles a q/k/v boundary at any tensor-parallel width. */
|
|
RadKVGroupDecl cd{};
|
|
cd.kind = RAD_KV_CONV;
|
|
cd.dtype = RAD_BF16;
|
|
cd.n_head_kv = 2 * gc.n_head_k + gc.n_head_v;
|
|
cd.head_dim = gc.head_k;
|
|
cd.conv_width = gc.conv_width;
|
|
m.kv_conv = rad_decl_kv_group(b, nm.f("kv_gdn_conv"), &cd);
|
|
|
|
/* ---- activation buffers, sized at max_tok (the worst step) and shared by every layer of a
|
|
* kind; liveness analysis over the declared read and write sets fits them in one arena
|
|
* (spec §3.1). */
|
|
m.b_x = decl_b(b, nm.f("x"), g.act_dtype, {g.max_tok, g.n_embd});
|
|
m.b_h = decl_b(b, nm.f("h"), g.act_dtype, {g.max_tok, g.n_embd});
|
|
|
|
/* The query-and-gate projection is rank 3, [tokens, heads, 2*head_dim], so the per-head
|
|
* interleaving is addressable: query and gate are the first and second head_dim columns of the
|
|
* last dimension, i.e. ordinary column slices. Flattened, no operand could name them. */
|
|
m.b_qg = decl_b(b, nm.f("attn_qg"), g.act_dtype, {g.max_tok, g.n_head, 2 * g.head_dim});
|
|
m.b_k = decl_b(b, nm.f("attn_k"), g.act_dtype, {g.max_tok, kv_dim});
|
|
m.b_v = decl_b(b, nm.f("attn_v"), g.act_dtype, {g.max_tok, kv_dim});
|
|
m.b_q = decl_b(b, nm.f("attn_q"), g.act_dtype, {g.max_tok, q_dim});
|
|
m.b_gate = decl_b(b, nm.f("attn_gate"),g.act_dtype, {g.max_tok, q_dim});
|
|
m.b_attn = decl_b(b, nm.f("attn_out"), g.act_dtype, {g.max_tok, q_dim});
|
|
|
|
m.b_in = decl_b(b, nm.f("gdn_in"), g.act_dtype, {g.max_tok, gc.in_dim()});
|
|
m.b_z = decl_b(b, nm.f("gdn_z"), g.act_dtype, {g.max_tok, v_dim});
|
|
m.b_gq = decl_b(b, nm.f("gdn_q"), g.act_dtype, {g.max_tok, gc.n_head_k, gc.head_k});
|
|
m.b_gk = decl_b(b, nm.f("gdn_k"), g.act_dtype, {g.max_tok, gc.n_head_k, gc.head_k});
|
|
m.b_gv = decl_b(b, nm.f("gdn_v"), g.act_dtype, {g.max_tok, gc.n_head_v, gc.head_v});
|
|
/* Decay and beta are f32: the decay is a cumulative sum whose intra-chunk span reaches
|
|
* hundreds of nats on real prefill inputs, and a bf16 running sum would lose the low bits that
|
|
* every decay factor exponentiates. */
|
|
m.b_gdec = decl_b(b, nm.f("gdn_g"), RAD_F32, {g.max_tok, gc.n_head_v});
|
|
m.b_beta = decl_b(b, nm.f("gdn_beta"), RAD_F32, {g.max_tok, gc.n_head_v});
|
|
/* bf16, not f32: gdn_kkt_solve writes this plane as bf16 and gdn_chunk_scan reads it as
|
|
* bf16. */
|
|
m.b_kkt = decl_b(b, nm.f("gdn_kkt"), RAD_BF16, {g.max_tok, gc.n_head_v, gc.chunk});
|
|
m.b_go = decl_b(b, nm.f("gdn_o"), g.act_dtype, {g.max_tok, gc.n_head_v, gc.head_v});
|
|
|
|
m.b_gate_up = decl_b(b, nm.f("gate_up"), g.act_dtype, {g.max_tok, 2 * g.n_ff});
|
|
m.b_ffn = decl_b(b, nm.f("ffn"), g.act_dtype, {g.max_tok, g.n_ff});
|
|
/* The lm_head runs over the sampled rows only: `max_logit_rows` is one row per sequence, or
|
|
* per verified draft position. At 248320 vocab that is 63 MiB of logits instead of 8 GiB.
|
|
* gather_rows selects the hidden states into the narrow `b_hout` (n_embd wide) before the
|
|
* GEMM. */
|
|
m.b_hout = decl_b(b, nm.f("h_out"), 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));
|
|
|
|
/* ---- vocabulary edges, both row-sharded. The lm_head is sharded because the sampler runs a
|
|
* distributed top-k and full logits are never gathered (spec §9, §13); replicating the
|
|
* embedding would cost 248320 x 5120 bf16 (2.43 GiB) per rank. `embed_lookup` takes a
|
|
* `vocab_offset`: a rank gathers the ids in its slice and writes zero rows for the rest, and
|
|
* the all_reduce below sums the ranks into x. */
|
|
RAD_ARCH_TRY(map_copy(b, nm.f("token_embd.weight"),
|
|
nm.ckpt("model.language_model.embed_tokens.weight")));
|
|
RAD_ARCH_TRY(map_copy(b, nm.f("output_norm.weight"),
|
|
nm.ckpt("model.language_model.norm.weight")));
|
|
/* The 27B does not tie its lm_head (`tie_word_embeddings: false`); the second source serves
|
|
* the small family members that do. The first source present wins. */
|
|
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_out_norm = decl_w(b, nm.f("output_norm.weight"), RAD_F32, {g.n_embd},
|
|
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_model());
|
|
/* Row-sharded with the vocabulary: g.n_vocab is this rank's share (rad_arch.h). Declared at
|
|
* its codes' dtype (bf16 as shipped, E4M3 where a recipe made it block fp8), which selects the
|
|
* logits kernel. */
|
|
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());
|
|
|
|
m.op_embed = rw(b, RAD_OP(b, "embed_lookup",
|
|
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), 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.b_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, g.max_tok, g.n_embd, m.b_x));
|
|
/* Always exact, not g.wire_exact: this sums disjoint rows (each token from one rank, zeros
|
|
* from the others), so a lossy wire would only add error. */
|
|
if (g.world > 1)
|
|
m.op_embed_ar = rw(b, RAD_OP(b, "all_reduce",
|
|
RAD_PARAMS(RAD_INT("world_size", g.world),
|
|
RAD_RANGE("numel", g.n_embd, g.max_tok * g.n_embd),
|
|
RAD_STR("dtype", g.dtype), RAD_INT("exact", 1)),
|
|
RAD_NOWEIGHTS),
|
|
{m.b_x}, {m.b_x});
|
|
|
|
/* ---- the layers. */
|
|
AttnGatedBF16::Wire aw{};
|
|
aw.x = m.b_x; aw.h = m.b_h; aw.qg = m.b_qg; aw.k = m.b_k; aw.v = m.b_v;
|
|
aw.q = m.b_q; aw.gate = m.b_gate; aw.attn = m.b_attn;
|
|
|
|
GdnBF16::Wire gw{};
|
|
gw.x = m.b_x; gw.h = m.b_h; gw.in = m.b_in; gw.z = m.b_z;
|
|
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.b_go;
|
|
|
|
MlpBF16::Wire mw{};
|
|
mw.x = m.b_x; mw.h = m.b_h; mw.gate_up = m.b_gate_up; mw.ffn = m.b_ffn;
|
|
|
|
/* The calibration tap: the widest split input is down_proj's (the feed-forward's width), and
|
|
* no input is wider. */
|
|
RAD_ARCH_TRY(m.calib.begin(b, nm, g, ctx, m.n_main, g.n_ff * g.world,
|
|
std::max({ g.n_embd, q_dim * g.world, v_dim * g.world, g.n_ff * g.world })));
|
|
|
|
m.layers.resize((size_t)m.n_main);
|
|
for (int64_t l = 0; l < m.n_main; ++l) {
|
|
Layer& lay = m.layers[(size_t)l];
|
|
lay.full = is_full_attention(l, interval) ? 1 : 0;
|
|
|
|
const char* pre = "model.language_model.layers";
|
|
if (lay.full) {
|
|
AttnGatedBF16::Src as{};
|
|
as.norm = nm.ckpt("%s.%lld.input_layernorm.weight", pre, (long long)l);
|
|
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);
|
|
RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, m.kv_full));
|
|
RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, m.kv_full, m.n_rot, aw, as));
|
|
} else {
|
|
GdnBF16::Src gs{};
|
|
gs.norm = nm.ckpt("%s.%lld.input_layernorm.weight", pre, (long long)l);
|
|
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));
|
|
RAD_ARCH_TRY(lay.gdn.declare(b, nm, g, gc, (int)l, m.kv_state, m.kv_conv, gw, gs));
|
|
}
|
|
if (m.calib.records(l)) {
|
|
const std::string L = std::string(pre) + "." + std::to_string(l);
|
|
lay.cs_in = (int)m.calib.sites.size();
|
|
RAD_ARCH_TRY(m.calib.site(b, g, l, L + ".input",
|
|
lay.full ? L + ".self_attn.q_proj," + L + ".self_attn.k_proj," + L + ".self_attn.v_proj"
|
|
: L + ".linear_attn.in_proj_qkv," + L + ".linear_attn.in_proj_z," +
|
|
L + ".linear_attn.in_proj_a," + L + ".linear_attn.in_proj_b",
|
|
m.b_h, g.n_embd, g.n_embd));
|
|
lay.cs_o = (int)m.calib.sites.size();
|
|
if (lay.full)
|
|
RAD_ARCH_TRY(m.calib.site(b, g, l, L + ".self_attn.o_proj.input", L + ".self_attn.o_proj",
|
|
m.b_attn, q_dim, q_dim * g.world));
|
|
else
|
|
RAD_ARCH_TRY(m.calib.site(b, g, l, L + ".linear_attn.out_proj.input", L + ".linear_attn.out_proj",
|
|
m.b_go, v_dim, v_dim * g.world));
|
|
}
|
|
|
|
MlpBF16::Src ms{};
|
|
ms.norm = nm.ckpt("%s.%lld.post_attention_layernorm.weight", pre, (long long)l);
|
|
ms.gate = nm.ckpt("%s.%lld.mlp.gate_proj.weight", pre, (long long)l);
|
|
ms.up = nm.ckpt("%s.%lld.mlp.up_proj.weight", pre, (long long)l);
|
|
ms.down = nm.ckpt("%s.%lld.mlp.down_proj.weight", pre, (long long)l);
|
|
RAD_ARCH_TRY(lay.mlp.declare(b, nm, g, (int)l, mw, ms));
|
|
if (m.calib.records(l)) {
|
|
const std::string L = std::string(pre) + "." + std::to_string(l);
|
|
lay.cs_mlp = (int)m.calib.sites.size();
|
|
RAD_ARCH_TRY(m.calib.site(b, g, l, L + ".post_attention.input",
|
|
L + ".mlp.gate_proj," + L + ".mlp.up_proj", m.b_h, g.n_embd, g.n_embd));
|
|
lay.cs_down = (int)m.calib.sites.size();
|
|
RAD_ARCH_TRY(m.calib.site(b, g, l, L + ".mlp.down_proj.input", L + ".mlp.down_proj",
|
|
m.b_ffn, g.n_ff, g.n_ff * g.world));
|
|
}
|
|
}
|
|
|
|
m.op_final_norm = rw(b, RAD_OP(b, "rmsnorm",
|
|
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
|
|
RAD_F64("eps", g.eps), RAD_F64("wadd", final_wadd), RAD_STR("dtype", g.dtype)),
|
|
RAD_WEIGHTS(m.w_out_norm)),
|
|
{m.b_x}, {m.b_h});
|
|
/* The lm_head's input, with the run that records the last layer. */
|
|
if (m.calib.on && m.calib.hi == m.n_main) {
|
|
m.cs_head = (int)m.calib.sites.size();
|
|
RAD_ARCH_TRY(m.calib.site(b, g, -1, "lm_head.input", "lm_head", m.b_h, g.n_embd, g.n_embd));
|
|
}
|
|
|
|
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.b_h}, {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});
|
|
|
|
/* ---- notes for --debug-graph. */
|
|
rad_note(b, "rank %d/%d: %lld trunk layers, %lld full attention + %lld gated delta net, "
|
|
"n_head=%lld n_head_kv=%lld head_dim=%lld n_ff=%lld vocab=[%lld,%lld)",
|
|
g.rank, g.world, (long long)m.n_main,
|
|
(long long)(m.n_main / interval), (long long)(m.n_main - m.n_main / interval),
|
|
(long long)g.n_head, (long long)g.n_head_kv, (long long)g.head_dim,
|
|
(long long)g.n_ff, (long long)g.vocab_off, (long long)(g.vocab_off + g.n_vocab));
|
|
rad_note(b, "gdn: %lld key heads x %lld, %lld value heads x %lld, conv width %lld, chunk %lld; "
|
|
"conv row %lld channels, projection row %lld",
|
|
(long long)gc.n_head_k, (long long)gc.head_k, (long long)gc.n_head_v,
|
|
(long long)gc.head_v, (long long)gc.conv_width, (long long)gc.chunk,
|
|
(long long)conv_dim, (long long)gc.in_dim());
|
|
rad_note(b, "ssm_a_log holds A_log itself: the kernels form -exp(A_log) * softplus(a + dt), "
|
|
"so a GGUF's ssm_a = -exp(A_log) must be converted back");
|
|
rad_note(b, "rope: %s over %lld of %lld head dims%s%s", g.rope_mode, (long long)m.n_rot,
|
|
(long long)g.head_dim, g.rope_mc ? ", sections " : "", g.rope_sections);
|
|
if (!have_interval)
|
|
rad_note(b, "the container did not state full_attention_interval; assuming %lld, which "
|
|
"makes layers 3,7,... full attention", (long long)interval);
|
|
rad_note(b, "logits for at most %lld sampled rows a step (of %lld tokens): gather_rows "
|
|
"selects RadBatch::out_ids before the lm_head", (long long)g.max_logit_rows,
|
|
(long long)g.max_tok);
|
|
if (nextn > 0)
|
|
rad_note(b, "the %lld MTP/NextN layer(s) under `mtp.` are not declared: this plugin "
|
|
"serves the trunk only", (long long)nextn);
|
|
|
|
/* The vision tower, last, so its activations share the arena with every buffer above. */
|
|
if (m.media) RAD_ARCH_TRY(m.vit.declare(b, meta, ctx, nm, g.n_embd, (int)g.n_layer + 64));
|
|
return RAD_OK;
|
|
}
|
|
|
|
static void step(RadCtx* c, const RadBatch* batch) {
|
|
Model& m = g_model[rad_rank(c)];
|
|
|
|
/* An encoder pass runs only the vision tower (RadBatch::enc). */
|
|
if (batch->enc) {
|
|
m.vit.step(c, batch);
|
|
return;
|
|
}
|
|
|
|
RAD_ISSUE(c, m.op_embed,
|
|
praw(batch->token_ids, RAD_I32, batch->n_tok), RAD_W(m.w_tok),
|
|
brows(m.b_x, batch->n_tok));
|
|
m.mrows.step(c, batch, m.b_x, batch->n_tok);
|
|
/* The vocab shard's reduction; declared only when world > 1, so tp1 is unchanged. */
|
|
if (m.op_embed_ar)
|
|
RAD_ISSUE_N(c, m.op_embed_ar, (int64_t)batch->n_tok * m.g.n_embd,
|
|
brows(m.b_x, batch->n_tok), RAD_NONE);
|
|
|
|
const int64_t T = batch->n_tok;
|
|
for (const Layer& l : m.layers) {
|
|
/* Under calibration a block's input norm is issued once ahead of the block, so its output
|
|
* is read before the block overwrites `h`; the block computes it again, the same op on
|
|
* the same bytes. */
|
|
if (l.cs_in >= 0) {
|
|
if (l.full) RAD_ISSUE(c, l.attn.op_norm, brows(m.b_x, T), RAD_W(l.attn.w_norm), brows(m.b_h, T));
|
|
else RAD_ISSUE(c, l.gdn.op_norm, brows(m.b_x, T), RAD_W(l.gdn.w_norm), brows(m.b_h, T));
|
|
m.calib.tap(c, (size_t)l.cs_in, T);
|
|
}
|
|
if (l.full) l.attn.step(c, batch);
|
|
else l.gdn.step(c, batch);
|
|
if (l.cs_o >= 0) m.calib.tap(c, (size_t)l.cs_o, T);
|
|
if (l.cs_mlp >= 0) {
|
|
RAD_ISSUE(c, l.mlp.op_norm, brows(m.b_x, T), RAD_W(l.mlp.w_norm), brows(m.b_h, T));
|
|
m.calib.tap(c, (size_t)l.cs_mlp, T);
|
|
}
|
|
l.mlp.step(c, batch);
|
|
if (l.cs_down >= 0) m.calib.tap(c, (size_t)l.cs_down, T);
|
|
}
|
|
|
|
RAD_ISSUE(c, m.op_final_norm,
|
|
brows(m.b_x, batch->n_tok), RAD_W(m.w_out_norm), brows(m.b_h, batch->n_tok));
|
|
if (m.cs_head >= 0) m.calib.tap(c, (size_t)m.cs_head, T);
|
|
m.calib.after_step(c, T);
|
|
|
|
/* The lm_head runs only on the rows the sampler asked for; a mid-prompt prefill chunk has
|
|
* n_out zero and skips it. */
|
|
if (batch->n_out > 0) {
|
|
if (m.op_logits_fill && plugin_words_cover_rows(batch)) {
|
|
RAD_ISSUE_N(c, m.op_logits_fill, batch->n_out, brows(m.b_logits, batch->n_out));
|
|
return;
|
|
}
|
|
RAD_ISSUE_N(c, m.op_gather, batch->n_out,
|
|
brows(m.b_h, batch->n_tok),
|
|
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));
|
|
}
|
|
}
|
|
|
|
} /* namespace qwen35_bf16 */
|
|
|
|
#ifndef RAD_ARCH_NO_EXPORTS
|
|
RAD_ARCH_PLUGIN(qwen35_bf16, "qwen35", "", "0.1.0",
|
|
"Qwen3.5 / 3.6 / 3.8 dense (0.8B..27B), bf16 weights and activations, gated "
|
|
"attention over paged KV every fourth layer and gated delta net between")
|
|
#endif
|