Archived
radiance 1.7.0
- every model serves without a GPU: libavx covers every op, so `--kernels libavx` alone serves every shipped container - Qwen3-ASR (zivius): speech to text on /v1/audio/transcriptions and live over /v1/realtime - /v1/messages works with Claude Code as it ships (gearwave00001) - a long agent conversation keeps its cached history when the client rewrites its last turn (manueloverride) - a stream's first event goes out with the first token, so llama-benchy measures prefill right - opt-in --prefill-interleave (Smellyyyy, baKon) and --prefill-span (keptn42) - four-rank TP releases the link early; four-rank selftests and docs/TP4.md (goodman-b) - NVFP4 and FP8 compressed-tensors imports (devmilan); tools/rad-export for finetunes and LoRA adapters (baKon) - many fixes, among them lost tokens on stop strings under concurrency, keep-alive, MiniCPM5 tool arguments and the CPU's vision and audio encoders - plugin ABI 17 and changed op schemas: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
b068190326
commit
1e968f6e1d
739 files changed
+128447
-84632
No files matched your search
+121
-184
@@ -2,109 +2,89 @@
|
||||
*
|
||||
* ============================== WHAT IT IS ==============================
|
||||
*
|
||||
* Qwen3.5 / Qwen3-Next ship ONE extra transformer layer beside the trunk, under an `mtp.` prefix
|
||||
* of its own, whose job is to predict token i+2 from the trunk's hidden state at i and the
|
||||
* embedding of the token at i+1. It is the drafter for speculative decoding, and it is a drafter
|
||||
* with no second checkpoint: the embedding table and the lm_head are the trunk's
|
||||
* (`mtp_use_dedicated_embeddings: false`, and no `mtp.lm_head` in the file).
|
||||
* Qwen3.5 / Qwen3-Next ship one extra transformer layer under an `mtp.` prefix that predicts
|
||||
* token i+2 from the trunk's hidden state at i and the embedding of token i+1. It is the
|
||||
* speculative-decoding drafter; the embedding table and lm_head are the trunk's
|
||||
* (`mtp_use_dedicated_embeddings: false`, no `mtp.lm_head`).
|
||||
*
|
||||
* THE INDEXING IS THE WHOLE THING, so it is stated once here and everything else follows:
|
||||
* Indexing:
|
||||
*
|
||||
* head index i reads hidden(i) and embed(token at i + 1)
|
||||
* writes its K and V at KV position i, with rope position i
|
||||
* predicts the token at i + 2
|
||||
*
|
||||
* That is DeepSeek-V3's MTP layout, which this family follows: the head's transformer block runs
|
||||
* over the SAME position indices as the trunk, and the shift lives entirely in which token gets
|
||||
* embedded. Getting it off by one costs nothing visible -- the drafts are simply wrong and the
|
||||
* acceptance rate collapses, while the model's own output stays exactly correct.
|
||||
* This is DeepSeek-V3's MTP layout: the head runs over the trunk's position indices and the shift
|
||||
* is only in which token is embedded. An off-by-one shows only as collapsed acceptance.
|
||||
*
|
||||
* The forward is short and every piece of it already exists in this library:
|
||||
* The forward:
|
||||
*
|
||||
* h = gather(the trunk's final hidden states, at the rows this pass continues from)
|
||||
* e = embed(the token one position on)
|
||||
* x = fc( concat( pre_fc_norm_embedding(e), pre_fc_norm_hidden(h) ) )
|
||||
* x = one gated-GQA layer + one SwiGLU feed-forward, over x <- AttnGatedFP8, MlpFP8
|
||||
* h'= mtp.norm(x)
|
||||
* logits = the TRUNK's lm_head over h'
|
||||
* logits = the trunk's lm_head over h'
|
||||
*
|
||||
* and h' is BOTH the logits input and the `h` of the next draft round. That is the reference's
|
||||
* shape too: a Qwen3-Next MTP forward returns one tensor and the proposer feeds the same tensor
|
||||
* back, unlike an EAGLE head which returns a (normed, unnormed) pair.
|
||||
* h' is both the logits input and the next round's `h`, as in the reference (a Qwen3-Next MTP
|
||||
* forward returns one tensor; an EAGLE head returns a (normed, unnormed) pair).
|
||||
*
|
||||
* ============================== THE TWO KINDS OF PASS ==============================
|
||||
*
|
||||
* RadBatch::draft_pass says which, and the sign is the whole distinction:
|
||||
* By the sign of RadBatch::draft_pass:
|
||||
*
|
||||
* < 0 A HISTORY PASS, one row a token, gathering the trunk's hidden states. It exists
|
||||
* because the head's attention reads its OWN K and V for every earlier position, and
|
||||
* nothing else in the engine writes those: the trunk's prefill fills layers 0..n-1 and
|
||||
* leaves the head's layer untouched. Without it the head attends to whatever the pool
|
||||
* last held, which is not wrong output -- a bad draft is only a rejected draft -- but it
|
||||
* is a drafter that proposes noise. The scheduler runs one over the positions the last
|
||||
* step added, the verified ones included.
|
||||
* < 0 History pass, one row per token, gathering the trunk's hidden states. The head's
|
||||
* attention reads its own K and V for every earlier position, which the trunk's prefill
|
||||
* does not write; without this pass the head attends to stale pool contents and proposes
|
||||
* noise. The scheduler runs it over the positions the last step added, verified ones
|
||||
* included.
|
||||
*
|
||||
* It is also DRAFT ROUND 1 for every sequence that drafts: that sequence's last history
|
||||
* row is the committed position with the token the trunk chose after it -- round 1's
|
||||
* whole input -- so the pass ends in the lm_head over those rows (RadBatch::draft_out_ids)
|
||||
* and leaves the head's output at them where round 2 reads it.
|
||||
* It is also draft round 1 for every drafting sequence: its last history row is the
|
||||
* committed position plus the token the trunk chose after it, so the pass ends in the
|
||||
* lm_head over those rows (RadBatch::draft_out_ids) and leaves the head's output there
|
||||
* for round 2.
|
||||
*
|
||||
* > 1 A DRAFT ROUND, one row a sequence, reading the head's own output from the round before,
|
||||
* which is already in the same buffer. Every round ends in the lm_head, because a draft is
|
||||
* a token. There is no round-1 pass; the history pass is it.
|
||||
* > 1 Draft round, one row per sequence, reading the previous round's output from the same
|
||||
* buffer and ending in the lm_head. There is no separate round-1 pass.
|
||||
*
|
||||
* ============================== WHAT IS AND IS NOT FP8 ==============================
|
||||
* ============================== PRECISION ==============================
|
||||
*
|
||||
* Read off mtp.safetensors rather than assumed, exactly as qwen35_fp8.cpp reads the trunk:
|
||||
* As stored in mtp.safetensors:
|
||||
*
|
||||
* F8_E4M3 + scale layers.0.self_attn.{q,k,v,o}_proj, layers.0.mlp.{gate,up,down}_proj
|
||||
* BF16, no scale fc, norm, pre_fc_norm_embedding, pre_fc_norm_hidden, and every norm inside
|
||||
* the layer
|
||||
*
|
||||
* `fc` is [5120, 10240] and bf16, so it is a plain `gemm_nt`, issued ONCE over the whole chunk.
|
||||
* libr4d's bf16 GEMM row for this shape is C_LE("M", 65536) and K here is 10240, a multiple of 16,
|
||||
* so the wide row serves the whole chunk in one launch. That matters because the history pass runs
|
||||
* one row a TOKEN over a whole prefill chunk: banding this at 64 and slicing it would be dozens of
|
||||
* launches a chunk for a GEMM whose traffic is microseconds.
|
||||
* `fc` ([5120, 10240], bf16) is a plain `gemm_nt` issued once over the whole chunk: libr4d's bf16
|
||||
* row is C_LE("M", 65536) and K = 10240 is a multiple of 16. The history pass has one row per
|
||||
* token of a prefill chunk, so slicing at 64 would cost dozens of launches.
|
||||
*
|
||||
* ============================== THE CONCAT ORDER ==============================
|
||||
*
|
||||
* `fc` reads 10240 columns and the two halves are the normed embedding and the normed hidden
|
||||
* state. WHICH HALF IS FIRST IS NOT IN THE CHECKPOINT: both orders are one contiguous [5120,10240]
|
||||
* matrix and no shape, name or dtype distinguishes them. DeepSeek-V3's MTP -- the design this one
|
||||
* follows -- concatenates `enorm(embedding)` then `hnorm(hidden)`, and that is the order here.
|
||||
*
|
||||
* Both readings are self-consistent, libref shares whichever one the graph picks, and no per-op
|
||||
* oracle can reach it -- but it has a CHEAP EMPIRICAL ANSWER, the draft acceptance rate. A head
|
||||
* wired the right way round accepts most of what it proposes; wired the wrong way round it
|
||||
* proposes noise and accepts nothing, while the model's own output stays perfectly correct
|
||||
* because a rejected draft costs only time.
|
||||
* `fc` reads the normed embedding and the normed hidden state concatenated. The order is not in
|
||||
* the checkpoint (both are one [5120, 10240] matrix); this follows DeepSeek-V3's MTP:
|
||||
* `enorm(embedding)` then `hnorm(hidden)`. Per-op checks cannot tell the orders apart; the draft
|
||||
* acceptance rate does (the wrong order accepts almost nothing).
|
||||
*
|
||||
* ============================== THE NORM GAINS ==============================
|
||||
*
|
||||
* GemmaRMSNorm's `1 + w` (Geom::wadd) applies here and the checkpoint proves it rather than
|
||||
* suggesting it: pre_fc_norm_hidden sits at -0.06 .. -0.24 and pre_fc_norm_embedding at
|
||||
* -0.27 .. -0.61 -- NEGATIVE gains, which only `1 + w` explains. `mtp.norm` sits at 0.59 .. 1.31,
|
||||
* which reads as either, so it takes the same `final_wadd` the trunk's output norm takes -- one
|
||||
* question, asked once, answered in one place.
|
||||
* GemmaRMSNorm's `1 + w` (Geom::wadd) applies: pre_fc_norm_hidden is -0.06 .. -0.24 and
|
||||
* pre_fc_norm_embedding -0.27 .. -0.61, negative gains that only `1 + w` explains. `mtp.norm`
|
||||
* (0.59 .. 1.31) is ambiguous, so it takes the trunk output norm's `final_wadd`.
|
||||
*
|
||||
* ============================== THE KV CACHE ==============================
|
||||
*
|
||||
* The head's attention layer is bound to the TRUNK'S full-attention group as one more layer, at
|
||||
* absolute index n_main. Two reasons, and neither is thrift:
|
||||
* The head's attention is bound to the trunk's full-attention group as one more layer, at
|
||||
* absolute index n_main:
|
||||
*
|
||||
* - the slot mapping and the block table are the trunk's. The head writes its K and V at the
|
||||
* slot the trunk's token for that position occupies, so a rejected draft's entry is
|
||||
* overwritten by the next step's correct token AT THE SAME ADDRESS. Draft rounds run in
|
||||
* position order and each writes its own position before attending to it, so there is no
|
||||
* rollback to get wrong -- which is why vLLM's EAGLE/MTP proposer needs no second block
|
||||
* manager either.
|
||||
* - a second group would need its own page geometry, its own free list and its own eviction,
|
||||
* for a cache one seventeenth the size of the one already there.
|
||||
* - It shares the trunk's slot mapping and block table, writing K and V at the slot of the
|
||||
* trunk's token for that position. A rejected draft's entry is overwritten by the next step's
|
||||
* correct token at the same address, and each round writes its position before attending to
|
||||
* it, so no rollback is needed (as in vLLM's EAGLE/MTP proposer).
|
||||
* - A second group would need its own page geometry, free list and eviction for a cache one
|
||||
* seventeenth the size.
|
||||
*
|
||||
* The cost is that the group's page grows from 16 layers to 17 -- 6% more attention KV -- and it
|
||||
* is paid only when the deployment declares speculation, because that is when the head is
|
||||
* declared at all.
|
||||
* The group's page grows from 16 layers to 17 (6% more attention KV), only when speculation is
|
||||
* declared.
|
||||
*/
|
||||
#ifndef RAD_BLOCK_MTP_FP8_H
|
||||
#define RAD_BLOCK_MTP_FP8_H
|
||||
@@ -113,20 +93,16 @@
|
||||
#include "rad_block_mlp_fp8.h"
|
||||
#include "rad_block_vit.h"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
namespace rad {
|
||||
namespace arch {
|
||||
|
||||
/* THE 2-BIT DRAFT HEAD IS A WEIGHT THE MODEL HOLDS OR DOES NOT. `head` is the lm_head under a name
|
||||
* of its own, which a recipe quantises to the 2-bit grid -- u2 codes, an f16 scale and a u8 zero a
|
||||
* group of 128 -- so one checkpoint tensor makes two logical weights. This declares its name map
|
||||
* (to `lm`, the checkpoint's head) and answers whether the model holds it at two bits, which is
|
||||
* when the draft rounds read it. A checkpoint served as it stands, or a container converted
|
||||
* without the rule, holds the bf16 head alone, and the draft rounds read that.
|
||||
/* Declares the name map of the 2-bit draft head `head` (the checkpoint lm_head `lm` under its own
|
||||
* name, quantised by a recipe to u2 codes with an f16 scale and u8 zero per group of 128) and
|
||||
* returns whether the model holds it at two bits. If not (no such recipe), draft rounds read the
|
||||
* bf16 head.
|
||||
*
|
||||
* Called ONCE, by the plugin, before it declares the head's buffers -- a name map is declared once
|
||||
* -- and the block reads the answer back from whether those buffers exist. */
|
||||
* Called once by the plugin, before it declares the head's buffers; the block infers the answer
|
||||
* from whether those buffers exist. */
|
||||
inline bool mtp_draft_head_q2(RadBuilder* b, const char* head, const char* lm) {
|
||||
if (!head || !lm) return false;
|
||||
if (map_copy(b, head, lm) < 0) return false;
|
||||
@@ -134,41 +110,34 @@ inline bool mtp_draft_head_q2(RadBuilder* b, const char* head, const char* lm) {
|
||||
return weight_enc(b, head, &e) && e.n_planes > 0 && e.plane[0].dtype == RAD_U2;
|
||||
}
|
||||
|
||||
/* TEMPLATED ON THE FEED-FORWARD, because the head is a TRUNK LAYER and a trunk layer's
|
||||
* feed-forward is dense in one member of this family and routed in the next. Qwen3.6-35B-A3B's
|
||||
* `mtp.layers.0` carries `mlp.experts.*`, `mlp.gate` and a shared expert, exactly as its trunk
|
||||
* layers do; a head hard-wired to MlpFP8 could not declare it, and the model would have a
|
||||
* container with a drafter in it and no way to run one.
|
||||
*
|
||||
* `Ffn` is MlpFP8 or MoeFP8. Both take the same declare() arity over their own Wire and Src -- the
|
||||
* MoE block's Config travels inside its Src for exactly this reason -- so nothing below has to
|
||||
* know which one it has. */
|
||||
/* Templated on the feed-forward, since the head mirrors a trunk layer, which is dense or routed
|
||||
* by model (Qwen3.6-35B-A3B's `mtp.layers.0` has `mlp.experts.*`, `mlp.gate` and a shared expert).
|
||||
* `Ffn` is MlpFP8 or MoeFP8; both have the same declare() signature over their own Wire and Src
|
||||
* (MoeFP8's Config travels inside its Src for this reason). */
|
||||
template <class Ffn>
|
||||
struct MtpBlockFP8 {
|
||||
struct Wire {
|
||||
/* [max_tok, n_embd] the TRUNK's final hidden states for the step just run -- the plugin's
|
||||
* `h`, which gather_rows already reads for the trunk's own logits. A pass names the rows
|
||||
* it wants through out_ids and never has to know how they were laid out. */
|
||||
/* [max_tok, n_embd] the trunk's final hidden states for the step just run (the plugin's
|
||||
* `h`). A pass names the rows it wants through out_ids. */
|
||||
rad_buf src = 0;
|
||||
/* [max_tok, n_embd] IN: the hidden state this pass predicts from. OUT: the head's own,
|
||||
* which is the next draft round's input. Sized for a whole prefill chunk because a
|
||||
* history pass is one row a token. */
|
||||
/* [max_tok, n_embd] in: the hidden state this pass predicts from; out: the head's own,
|
||||
* the next round's input. Sized for a prefill chunk (history passes are one row per
|
||||
* token). */
|
||||
rad_buf hout = 0;
|
||||
rad_buf e = 0; /* [max_tok, n_embd] the input token's embedding */
|
||||
rad_buf c = 0; /* [max_tok, 2 * n_embd] the two normed halves, fc's input */
|
||||
rad_buf x = 0; /* [max_tok, n_embd] the head's own residual stream */
|
||||
rad_buf logits = 0; /* [max_logit_rows, n_vocab] the trunk's, shared */
|
||||
/* THE 2-BIT DRAFT HEAD's working set -- see the block comment at op_dlogits. Every one of
|
||||
* these is a draft round's, so they are sized at max_logit_rows and not at max_tok: a
|
||||
* history pass never reaches any of them. */
|
||||
/* The 2-bit draft head's working set (see op_dlogits), sized at max_logit_rows: only
|
||||
* drafting rows reach it. */
|
||||
rad_buf dq = 0; /* [max_logit_rows, n_embd] int8 codes */
|
||||
rad_buf ds = 0; /* [max_logit_rows, 1] f32, one scale a row */
|
||||
rad_buf dsum = 0; /* [n_embd/group, mpad] f32, GROUP-major (quant_act_i8's asum) */
|
||||
rad_buf dsum = 0; /* [n_embd/group, mpad] f32, group-major (quant_act_i8's asum) */
|
||||
rad_buf dlog = 0; /* [max_logit_rows, n_vocab] bf16, the coarse logits */
|
||||
rad_buf didx = 0; /* [max_logit_rows, 1] i32, the proposed token */
|
||||
rad_buf dval = 0; /* [max_logit_rows, 1] f32, its logit */
|
||||
/* The layer's working set is the TRUNK's -- those buffers are dead by the time any of
|
||||
* this runs, and a second set would be 150 MiB of activation planes for one layer. */
|
||||
/* The layer reuses the trunk's working set, which is dead by the time the head runs (a
|
||||
* second set would be 150 MiB). */
|
||||
AttnGatedFP8::Wire attn{};
|
||||
typename Ffn::Wire mlp{};
|
||||
};
|
||||
@@ -178,9 +147,8 @@ struct MtpBlockFP8 {
|
||||
const char* norm = nullptr;
|
||||
const char* pre_norm_hidden = nullptr;
|
||||
const char* pre_norm_embedding = nullptr;
|
||||
/* The checkpoint's lm_head, again. The trunk maps it as `output.weight`; the draft head is
|
||||
* a second logical weight over the SAME tensor, which the recipe quantises to 2-bit codes
|
||||
* (mtp_draft_head_q2). One source, two weights, no second checkpoint tensor. */
|
||||
/* The checkpoint's lm_head. The trunk maps it as `output.weight`; the draft head is a
|
||||
* second logical weight over the same tensor, quantised to 2 bits (mtp_draft_head_q2). */
|
||||
const char* lm = nullptr;
|
||||
AttnGatedFP8::Src attn{};
|
||||
typename Ffn::Src mlp{};
|
||||
@@ -188,8 +156,8 @@ struct MtpBlockFP8 {
|
||||
|
||||
Geom g{};
|
||||
Wire w{};
|
||||
int layer = 0; /* ABSOLUTE, so the KV group's layer_slot lookup finds it */
|
||||
int64_t rows = 0; /* the widest pass: one row a token of a prefill chunk */
|
||||
int layer = 0; /* absolute, for the KV group's layer_slot lookup */
|
||||
int64_t rows = 0; /* the widest pass: one row per token of a prefill chunk */
|
||||
int64_t off_e = 0, off_h = 0; /* the two halves' column offsets inside `c` */
|
||||
rad_weight w_tok = 0, w_lm = 0, w_fc = 0, w_norm = 0, w_pre_h = 0, w_pre_e = 0;
|
||||
rad_weight w_dh = 0, w_dhs = 0; /* the 2-bit lm_head and its (scale, zero) plane */
|
||||
@@ -197,9 +165,9 @@ struct MtpBlockFP8 {
|
||||
AttnGatedFP8 attn{};
|
||||
Ffn mlp{};
|
||||
rad_op op_gather = 0, op_embed = 0, op_embed_ar = 0, op_pre_h = 0, op_pre_e = 0, op_fc = 0;
|
||||
/* THE PROMPT'S MEDIA ROWS over the head's own embedding: its history pass embeds the prompt
|
||||
* too, one position on, and an image token's embedding is the encoder's there as well. Set
|
||||
* `media` before declare when the program has an encoder. */
|
||||
/* Media rows over the head's embedding: the history pass embeds the prompt too (one position
|
||||
* on), and an image token's embedding comes from the encoder. Set `media` before declare when
|
||||
* the program has an encoder. */
|
||||
bool media = false;
|
||||
MediaRows mrows{};
|
||||
rad_op op_norm = 0, op_dsel = 0, op_dback = 0, op_logits = 0;
|
||||
@@ -220,14 +188,12 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
rows = g.max_tok;
|
||||
if (rows <= 0 || !w_tok || !w_lm) return RAD_E_INVAL;
|
||||
|
||||
/* The normed embedding first, then the normed hidden state; see THE CONCAT ORDER above. */
|
||||
/* The normed embedding first, then the normed hidden state (see the header). */
|
||||
off_e = 0;
|
||||
off_h = g.n_embd;
|
||||
|
||||
/* The head's own weights. SHARD_NONE on all four: three are per-channel norms over the
|
||||
* residual stream, which is replicated, and `fc` reads and writes that same stream -- a
|
||||
* column-sharded fc would need an all-reduce to buy 52 MiB a rank back, which is the wrong
|
||||
* side of the trade at one GEMM per draft round. */
|
||||
/* The head's own weights, all replicated: three norms over the replicated stream, and `fc`,
|
||||
* which column-sharded would save 52 MiB per rank at the cost of an all-reduce per round. */
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("mtp.pre_fc_norm_hidden.weight"), src.pre_norm_hidden));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("mtp.pre_fc_norm_embedding.weight"), src.pre_norm_embedding));
|
||||
RAD_ARCH_TRY(map_copy(b, nm.f("mtp.norm.weight"), src.norm));
|
||||
@@ -242,9 +208,8 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
w_fc = decl_w(b, nm.f("mtp.fc.weight"), RAD_BF16, {g.n_embd, 2 * g.n_embd},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
|
||||
/* A SECOND gather_rows and a SECOND embed_lookup, over the trunk's own buffers and weights.
|
||||
* They are separate ops rather than re-issues of the trunk's because an op's read and write
|
||||
* sets are what the buffer planner reads for liveness, and these write somewhere else. */
|
||||
/* Separate gather_rows and embed_lookup ops over the trunk's buffers and weights, because
|
||||
* they write different buffers and liveness comes from each op's read/write sets. */
|
||||
op_gather = rw(b, RAD_OP(b, "gather_rows",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
@@ -257,9 +222,8 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
RAD_WEIGHTS(w_tok)),
|
||||
{}, {w.e});
|
||||
if (media) RAD_ARCH_TRY(mrows.declare(b, rows, g.n_embd, w.e));
|
||||
/* The trunk's table is vocab-sharded, so this gather is a partial one and needs the same
|
||||
* exact reduction the trunk does. Its own op rather than the trunk's because it writes a
|
||||
* different buffer, which is the rule the gather above states. */
|
||||
/* The trunk's table is vocab-sharded, so the lookup is partial and needs an exact
|
||||
* all-reduce. */
|
||||
if (g.world > 1)
|
||||
op_embed_ar = rw(b, RAD_OP(b, "all_reduce",
|
||||
RAD_PARAMS(RAD_INT("world_size", g.world),
|
||||
@@ -289,11 +253,10 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
|
||||
RAD_ARCH_TRY(attn.declare(b, nm, g, l, kv, rotary_dim, w.attn, src.attn));
|
||||
|
||||
/* DRAFT ROUND 1'S ROWS OUT OF A HISTORY PASS (RadBatch::draft_out_ids), one a drafting
|
||||
* sequence in round order, to the front of the residual stream `x` -- before the feed-forward,
|
||||
* which then runs over those rows alone. Through `hout`, which is dead from the fc's read to
|
||||
* the norm's write, because a gather in place could overwrite a row another sequence has yet to
|
||||
* read. */
|
||||
/* Moves round 1's rows of a history pass (RadBatch::draft_out_ids, one per drafting sequence)
|
||||
* to the front of `x` before the feed-forward, which then runs on those rows only. Goes through
|
||||
* `hout` (dead between fc's read and the norm's write), since an in-place gather could
|
||||
* overwrite a row not yet read. */
|
||||
op_dsel = 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)),
|
||||
@@ -315,8 +278,8 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
RAD_WEIGHTS(w_norm)),
|
||||
{w.x}, {w.hout});
|
||||
|
||||
/* The trunk's lm_head, over the head's hidden state. Banded at the logits buffer's own extent
|
||||
* rather than at `rows`: it runs over one row a drafting sequence. */
|
||||
/* The trunk's lm_head over the head's hidden state, banded at the logits buffer's extent: it
|
||||
* runs over one row per drafting sequence. */
|
||||
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),
|
||||
@@ -324,47 +287,31 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
RAD_WEIGHTS(w_lm)),
|
||||
{w.hout}, {w.logits});
|
||||
|
||||
/* ---- THE 2-BIT DRAFT HEAD -------------------------------------------------------------
|
||||
/* ---- The 2-bit draft head.
|
||||
*
|
||||
* `logits_gemm` IS ALREADY AT THE MEMORY ROOFLINE for the bf16 lm_head's 2.54 GB, so the only
|
||||
* lever it has is FEWER BYTES. That matters here and not at the trunk, because a speculative
|
||||
* step runs the projection THREE times: once to verify, and once per draft round.
|
||||
* `logits_gemm` is at the memory roofline for the bf16 lm_head's 2.54 GB, and a speculative
|
||||
* step runs the projection once to verify plus once per draft round. Drafts are verified by the
|
||||
* exact head, so a coarser draft head costs only acceptance, never an emitted token.
|
||||
*
|
||||
* The two draft ones do not need the exact distribution. A draft is a PROPOSAL; the target
|
||||
* verifies every one of them with its own untouched bf16 head, and speculative decoding is
|
||||
* distribution-preserving -- so a worse draft costs ACCEPTANCE and cannot change a token the
|
||||
* model emits. That is the one place in the step where precision buys throughput outright.
|
||||
* The draft rounds read a 2-bit copy: 318 MB of codes and 40 MB of (scale, zero) dwords, an
|
||||
* eighth of the traffic. libr4d's `gemm_w2a8_nt` takes codes and the scale/zero planes as two
|
||||
* operands, laid out as `r4d.w2a8.frag.g128` at load (libr4d/r4d_layout.cpp). The grid is
|
||||
* asymmetric (four levels are too few for a symmetric one), so the activation side uses
|
||||
* `quant_act_i8`'s three-output form, whose per-group row sums subtract the stored zero.
|
||||
*
|
||||
* So the drafter reads a 2-bit copy: 318 MB of codes and 40 MB of (scale, zero) dwords against
|
||||
* 2.54 GB, an eighth of the traffic. It is a logical weight of its own over the checkpoint's
|
||||
* head (mtp_draft_head_q2), quantised by the recipe; libr4d's `gemm_w2a8_nt` takes its codes
|
||||
* and its scale and zero planes as two operands and lays them out as `r4d.w2a8.frag.g128` at
|
||||
* load (libr4d/r4d_layout.cpp). The grid is asymmetric -- four levels are not enough for a
|
||||
* symmetric one -- which is why the activation side is `quant_act_i8`'s three-output form: the
|
||||
* per-group row sums are what subtract the stored zero.
|
||||
*
|
||||
* AND IT DOES NOT GO THROUGH THE SAMPLER. The 2-bit GEMM writes bf16 and the sampler's chain
|
||||
* reads the f32 logits plane, so a draft round would need a cast of a 248K-wide row to reach
|
||||
* it -- for an argmax. `row_topk` at R=1 is that argmax, on the bf16 the GEMM already wrote,
|
||||
* and the engine reads the proposed id straight out of `didx`. The trunk's logits plane is
|
||||
* left alone, which is also what lets a draft round sample nothing at all.
|
||||
* The draft head bypasses the sampler, which reads f32 logits: `row_topk` at R=1 takes the
|
||||
* argmax on the GEMM's bf16 output and the engine reads the id from `didx`. The trunk's logits
|
||||
* plane is untouched.
|
||||
*/
|
||||
enum { MTP_W2_GROUP = 128 }; /* libr4d's R4D_GEMM_W2A8_GROUP, and the layout's g128 tag */
|
||||
/* The plugin declared the working set only where mtp_draft_head_q2 found the head at two
|
||||
* bits, which declared its name map too. */
|
||||
/* The plugin declares the working set only when mtp_draft_head_q2 found a 2-bit head (and
|
||||
* declared its name map). */
|
||||
q2 = src.lm && w.dq && w.ds && w.dsum && w.dlog && w.didx && w.dval;
|
||||
if (q2) {
|
||||
/* REPLICATED, AT THE WHOLE VOCABULARY, and that is a decision rather than an omission.
|
||||
*
|
||||
* The trunk's lm_head is row-sharded and its logits are merged across ranks by the
|
||||
* sampler. This head does NOT go through the sampler -- the comment above op_dlogits says
|
||||
* why -- so its argmax is a `row_topk` over one rank's plane, and a sharded plane would
|
||||
* make every rank propose only tokens from its own half. Replicating it costs 341 MiB a
|
||||
* card at 2 bits (against the 1.27 GiB a rank saves on the trunk head) and keeps the
|
||||
* draft path a single-rank computation, which is what it is.
|
||||
*
|
||||
* The stored bytes are also tile-major (16 output rows a packed block), so the relayout
|
||||
* would have to be handed a row share that is a whole number of tiles. */
|
||||
/* Replicated over the whole vocabulary. Without the sampler's cross-rank merge, a sharded
|
||||
* plane would let each rank propose only its own vocabulary slice. Replication costs 341
|
||||
* MiB per card at 2 bits and keeps the draft path single-rank. (The stored bytes are also
|
||||
* tile-major, 16 output rows per packed block, so a shard would have to be whole tiles.) */
|
||||
const char* dhn = nm.f("mtp.draft_head.weight");
|
||||
w_dh = decl_view(b, dhn, nullptr, "codes", RAD_U2, {g.n_vocab_all, g.n_embd},
|
||||
RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
|
||||
@@ -395,11 +342,9 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
{w.dlog}, {w.didx, w.dval});
|
||||
}
|
||||
|
||||
/* THE HEAD'S OUTPUT STAYS LIVE THROUGH THE WHOLE HEAD. It is the next round's input, written
|
||||
* before this pass's lm_head runs; declared as read by the lm_head's first op alone, the
|
||||
* buffer planner would let the ops after it take its bytes, and the next round would continue
|
||||
* from what they wrote. Nothing fails: acceptance falls. Read by the head's last op as well,
|
||||
* it survives every pass of the step. */
|
||||
/* Keep `hout` live through the head's last op: it is the next round's input, and if only the
|
||||
* lm_head's first op read it, the planner could give its bytes to later ops (silently lowering
|
||||
* acceptance). */
|
||||
{
|
||||
const rad_op last = q2 ? op_dtopk : op_logits;
|
||||
if (rad_op_reads(b, last, &w.hout, 1) < 0) return RAD_E_INVAL;
|
||||
@@ -407,17 +352,16 @@ inline int MtpBlockFP8<Ffn>::declare(RadBuilder* b, Names& nm, const Geom& geom,
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* One pass of the head. The batch is the one the scheduler built for it: n_tok rows, `token_ids`
|
||||
* already shifted one position on (batch.cpp, token_at), `positions` the head's own indices, and
|
||||
* the KV group batch carrying the slot each row writes at -- so every op below runs at n_tok rows
|
||||
* and the attention layer needs no special case at all. */
|
||||
/* One pass of the head over the scheduler's batch for it: n_tok rows, `token_ids` already shifted
|
||||
* one position on (batch.cpp, token_at), `positions` the head's indices, and the KV group batch
|
||||
* carrying each row's slot, so the attention layer needs no special case. */
|
||||
template <class Ffn>
|
||||
inline void MtpBlockFP8<Ffn>::step(RadCtx* c, const RadBatch* batch) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
const int r = batch->draft_pass;
|
||||
|
||||
/* The history pass gathers; the rounds after it read the head's own output, which the pass
|
||||
* before left in the same buffer. */
|
||||
/* The history pass gathers; later rounds read the previous pass's output from the same
|
||||
* buffer. */
|
||||
if (r == 1) {
|
||||
rad_step_fail(c, "the MTP head was handed a draft-round-1 pass: that round is carried by "
|
||||
"the history pass (RadBatch::draft_out_ids) and has no pass of its own");
|
||||
@@ -437,21 +381,16 @@ inline void MtpBlockFP8<Ffn>::step(RadCtx* c, const RadBatch* batch) const {
|
||||
|
||||
RAD_ISSUE(c, op_fc, brows(w.c, T), RAD_W(w_fc), brows(w.x, T));
|
||||
|
||||
/* A HISTORY PASS THAT DRAFTS NOTHING ONLY LEAVES K AND V BEHIND, and they come from the
|
||||
* block's input. The query, the attention, its output, the feed-forward, the norm and the
|
||||
* lm_head compute the head's output, which only a drafting row reads -- and every prefill
|
||||
* chunk but a prompt's last drafts nothing. `n_draft_out` is the batch's and the same on every
|
||||
* rank, so a collective skipped here is skipped on all of them. */
|
||||
/* A history pass with no drafting rows (every prefill chunk but a prompt's last) only needs
|
||||
* K and V. `n_draft_out` is the same on every rank, so skipped collectives match. */
|
||||
if (r < 0 && batch->n_draft_out <= 0) {
|
||||
attn.store_kv(c, batch);
|
||||
return;
|
||||
}
|
||||
attn.step(c, batch);
|
||||
|
||||
/* A HISTORY PASS THAT DRAFTS GOES ON WITH ITS DRAFTING ROWS ALONE. They are round 1, and
|
||||
* nothing reads the head's output at any other row: the feed-forward, the norm and the lm_head
|
||||
* would run over a whole prompt chunk for a handful of rows. So round 1's rows come to the
|
||||
* front of `x` first, and everything after runs as a pass of those rows. */
|
||||
/* A history pass with drafting rows continues on those rows alone (round 1): they are moved
|
||||
* to the front of `x` and the rest of the head runs over them. */
|
||||
int64_t L = T; /* the rows the stream goes on with */
|
||||
if (r < 0) {
|
||||
L = batch->n_draft_out;
|
||||
@@ -464,12 +403,12 @@ inline void MtpBlockFP8<Ffn>::step(RadCtx* c, const RadBatch* batch) const {
|
||||
|
||||
RAD_ISSUE(c, op_norm, brows(w.x, L), RAD_W(w_norm), brows(w.hout, L));
|
||||
if (q2) {
|
||||
/* `dsum` is GROUP-major [n_embd/group][ceil(L/16)*16] and not row-major, so it is passed
|
||||
* whole: there is no row slice of it that means anything. */
|
||||
/* `dsum` is group-major [n_embd/group][ceil(L/16)*16], so it has no row slice; pass it
|
||||
* whole. */
|
||||
RAD_ISSUE_N(c, op_dquant, L, brows(w.hout, L), brows(w.dq, L), brows(w.ds, L),
|
||||
RAD_B(w.dsum));
|
||||
/* The trailing `b_ref` is mxfp4's and absent here; an absent optional operand is a null
|
||||
* tensor in a full-length list, not a shorter list (rad_fp8.h says why). */
|
||||
/* The trailing `b_ref` is mxfp4's and absent; operands are positional, so pass RAD_NONE
|
||||
* (rad_fp8.h). */
|
||||
RAD_ISSUE_N(c, op_dlogits, L, brows(w.dq, L), brows(w.ds, L), RAD_W(w_dh), RAD_W(w_dhs),
|
||||
brows(w.dlog, L), RAD_B(w.dsum), RAD_NONE);
|
||||
RAD_ISSUE_N(c, op_dtopk, L, brows(w.dlog, L), brows(w.didx, L), brows(w.dval, L),
|
||||
@@ -479,10 +418,8 @@ inline void MtpBlockFP8<Ffn>::step(RadCtx* c, const RadBatch* batch) const {
|
||||
}
|
||||
}
|
||||
|
||||
/* The dense head, which is the 27B's and every other member of this family whose feed-forward is
|
||||
* one SwiGLU. The routed one is MtpBlockFP8<MoeFP8>, named by the plugin that wants it -- this
|
||||
* header does not include rad_block_moe_fp8.h, because a dense plugin should not compile a routed
|
||||
* block it will never declare. */
|
||||
/* The dense head (SwiGLU feed-forward). The routed one is MtpBlockFP8<MoeFP8>, named by the
|
||||
* plugin that needs it; this header does not include rad_block_moe_fp8.h. */
|
||||
using MtpFP8 = MtpBlockFP8<MlpFP8>;
|
||||
|
||||
} /* namespace arch */
|
||||
|
||||
Reference in new issue
Block a user