Archived
radiance 1.14.0
New models and formats: - libr3d: a kernel library for RDNA3 (gfx11) cards - nomic-embed-text-v2-moe (the nomicbert plugin) - Gemma 4 E2B with its per-layer MLP width, and Google's unmodified E2B/E4B directories in bf16 - DeepSeek-V4.1-Flash converted straight from its release: the Engram tables, the fused imatrix and rad-convert --chat-template for a release that ships no template - GGUF Q8_0, Q4_K and Q6_K tensors import as their planes; rad-convert writes them without a recipe - Qwen3.8-Flash-Next-FP8 converts and serves through qwen4exp_fp8 Speed: - hc_read's decode kernels as one grid (34.7 -> 25.2 us at 4 rows) and its tiled up-mix pipelined - gemm_fp8a8: the narrow form's scales staged through LDS (Flash-Next int8 at 32 rows 83.6 -> 68.3 us), the gated three-tile form at 8 waves (gate_up at 48 rows 272.9 -> 180.6 us) - Qwen3.8-27B: the delta net's out-projection read beside its decode recurrent update (x1 19.40 -> 19.10 ms a step), and the verify's split-KV attention sized by context depth (x3 at 180K 42.4 -> 40.3 ms a step) - Hy-Memory (HYV3) decode: fused QK norm, RoPE and KV store, residual adds folded into the norms, coalesced one-token expert GEMMs - spechub: the coupled verify walks the live candidates only (151 -> 31 us a launch) Speculation: - DFlash2: lossless coupled acceptance at T > 0 (--draft-coupling on) - A block drafter's proposals verified as a tree, on by default for the Gemma 4 31B's DFlash2, with a block-16 tree that runs its short block under load Features: - Decode-hot residency: --draft-expert-placement host, --expert-profile, --expert-pin-top, and --expert-stats FILE for the routed experts each layer selects - --ngram-placement ram=DIR: one n-gram table shared by engines on separate cards - Prefix cache: --prefix-resume exact (default) resumes a hit on the cold run's own chunk grid, so it answers bit-identically; snapshots at a head other prompts share, from 528-token prompts, and for tip-only one drafter window below the tip; hits on models whose KV groups have different block sizes; the snapshot store keeps a floor of 8 ahead of --host-headroom-mib - Typed tool-call argument tags; rad-chatparse --request renders and reads back one request as the server does - MiniMax-H3: keyframes ride a latent upscale - Gemma 3's fused all-reduce junction on the lossy wire Fixes: - A speculative step shorter than its band (an MTP step of 4 rows at gqa 8) no longer stops the engine with a scratch refusal at issue (#293) - --p2p off: prefill back to 1.12.0's rate; 1.13.0's push check read a host-memory slot over the link twice (#323) - A chat template that cannot render a conversation is refused at load, not mid-request - imatrix entries found under llama.cpp's spelling; the W4A8 prefill's tail scale read; the GDN conv kernels on an f32 weight; dense integer rows at the group they compute (a Q8_0 weight at group 32) - --expert-profile refuses a profile recorded on another model - hc_read's one-grid wait traps past its deadline instead of reading a stale value Changes: Plugin ABI 31, unchanged. RADIANCE_DRAFT_TREE, DFLASH2_TREE_NODES, DFLASH2_TREE_TEMP and DFLASH_Q_TEMP are gone (trees run where the probe says they pay; temperatures 1.25 / 0.6).
This commit is contained in:
1 parent
481fcd5df0
commit
4a61d7296b
160 files changed
+8790
-6819
No files matched your search
@@ -123,6 +123,16 @@ struct Dflash2 {
|
||||
int64_t group = 16;
|
||||
int64_t sel_rank = 256; /* the codebooks' width */
|
||||
int64_t sel_topk = 16; /* candidates kept per position */
|
||||
/* Propose a tree of `steps()` nodes instead of the selector's one path (`dflash_tree`,
|
||||
* rad_drafter_tree): best-first over the candidate lattice by path probability, each
|
||||
* edge's probability the softmax of its scores over the K candidates divided by
|
||||
* `tree_temp`. False: the greedy walk. */
|
||||
bool tree = false;
|
||||
double tree_temp = 1.0;
|
||||
/* A tree's draft nodes a step, 0 for `steps()`. More than the block drafts is a wider tree
|
||||
* over the same lattice (its depth is still at most `steps()`); the proposal buffers, the
|
||||
* drafter's declared depth and the verify are this wide. */
|
||||
int64_t tree_nodes = 0;
|
||||
int64_t window = 0; /* sliding attention width, 0 for none */
|
||||
int causal = 0;
|
||||
/* The target's embedding scale, applied to the query block as the drafter was trained
|
||||
@@ -132,15 +142,18 @@ struct Dflash2 {
|
||||
* pass (prefetch_beside, rad_arch.h); 0 reads none. */
|
||||
int64_t prefetch = 0;
|
||||
|
||||
/* Also declare the short block's query pass (kShortBlock rows) for --draft-short-from; an
|
||||
* architecture that does not call rad_drafter_short_block leaves it off and its graph as
|
||||
* it was. */
|
||||
/* Also declare the short block's query pass (kShortBlock rows, or `short_rows`) for
|
||||
* --draft-short-from; an architecture that does not call rad_drafter_short_block leaves
|
||||
* it off and its graph as it was. A tree's selector at that block grows a tree of its own
|
||||
* over the shorter pass, `short_rows - 1` nodes or fewer. */
|
||||
int short_block = 0;
|
||||
/* --draft-coupling: the temperature of the draft law Q the coupled walk samples from
|
||||
* (dflash_couple_propose's `q_temp`). Any value keeps the output exact; it only moves
|
||||
* acceptance, sum min(P, Q). */
|
||||
double q_temp = 1.0;
|
||||
int64_t short_rows = 0;
|
||||
int64_t steps() const { return block - 1; }
|
||||
int64_t nodes() const { return tree && tree_nodes > 0 ? tree_nodes : steps(); }
|
||||
int64_t ng(int64_t n_embd) const { return group ? n_embd / group : 0; }
|
||||
/* The kernel projection emits [2, taps, NG] per token (prepare's set, then finish's);
|
||||
* `dflash_conv` reads a slice of it at the full row pitch. */
|
||||
@@ -198,6 +211,8 @@ struct Dflash2 {
|
||||
/* --draft-coupling: [max_seqs, DRAW_WORDS] draws the engine stages, and [steps, max_seqs,
|
||||
* STATE_WORDS] states it reads back (abi/rad_spechub.h). Both or neither. */
|
||||
rad_buf couple_draws = 0, couple_state = 0;
|
||||
/* [max_seqs, steps] i32 a tree's parents (Config::tree, rad_drafter_tree); 0 otherwise */
|
||||
rad_buf parents = 0;
|
||||
};
|
||||
|
||||
/* Whether the 2-bit draft head is vocab-sharded (rad_arch.h: vocab_head_sharded), so whoever
|
||||
@@ -277,6 +292,7 @@ struct Dflash2 {
|
||||
* and the selector strides by it, so each has an op of its own; every other op is per row. */
|
||||
rad_op op_aconv_lo[MAX_LAYERS] = {}, op_fconv_lo[MAX_LAYERS] = {}, op_sel_lo = 0;
|
||||
int64_t block_lo = 0;
|
||||
int64_t sel_lo_nodes = 0; /* a tree's nodes at the short block (op_sel_lo) */
|
||||
rad_op op_qrope = 0, op_krope = 0, op_store = 0, op_attn = 0;
|
||||
rad_op op_act = 0, op_ar = 0, op_add = 0, op_final = 0;
|
||||
/* The residual add folded into the norm after it (add_rmsnorm), issued in place of op_add and
|
||||
@@ -718,7 +734,10 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
|
||||
* layers. Planned from those alone, its range would end before the coefficients' begins and
|
||||
* the plan could lay one over the other. Read by the last convolution as well, it is live
|
||||
* wherever the coefficients are. */
|
||||
block_lo = cfg.short_block && cfg.block > kShortBlock ? kShortBlock : 0;
|
||||
{
|
||||
const int64_t rows = cfg.short_rows > 0 ? cfg.short_rows : kShortBlock;
|
||||
block_lo = cfg.short_block && rows >= 2 && cfg.block > rows ? rows : 0;
|
||||
}
|
||||
for (int64_t l = 0; block_lo && l < g.n_layer; ++l) {
|
||||
const int i = (int)l;
|
||||
op_aconv_lo[i] = rw(b, RAD_OP(b, "dflash_conv",
|
||||
@@ -943,15 +962,65 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
|
||||
RAD_WEIGHTS(w_selp)),
|
||||
{w.sel_h}, {w.hp});
|
||||
/* `anchor_stride` is `block`: the operand is the step's token_ids, `block` per sequence with
|
||||
* the anchor first. */
|
||||
op_sel = rw(b, RAD_OP(b, "dflash_select",
|
||||
* the anchor first.
|
||||
*
|
||||
* A tree (Config::tree): `dflash_tree` grows `steps` nodes best-first over the same lattice
|
||||
* and the same edge scores, writing each node's parent beside its token. Where no library
|
||||
* serves it the block falls back to the chain and says so; the caller reads `cfg.tree` after
|
||||
* declare to decide whether to tell the core (rad_drafter_tree). --draft-coupling samples one
|
||||
* path per sequence, so a coupled drafter proposes the chain. */
|
||||
if (cfg.tree && (w.couple_draws || w.couple_state)) {
|
||||
rad_note(b, "DFlash2: --draft-coupling samples a chain; the drafter proposes no tree");
|
||||
cfg.tree = false;
|
||||
}
|
||||
if (cfg.tree) {
|
||||
op_sel = RAD_OP(b, "dflash_tree",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("steps", cfg.steps()),
|
||||
RAD_INT("top_k", cfg.sel_topk), RAD_INT("rank", cfg.sel_rank),
|
||||
RAD_INT("n_vocab", g.n_vocab_all),
|
||||
RAD_INT("anchor_stride", cfg.block),
|
||||
RAD_INT("nodes", cfg.nodes()), RAD_F64("temp", cfg.tree_temp),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_pred, w_succ));
|
||||
if (op_sel && rad_op_resolved(b, op_sel)) {
|
||||
rw(b, op_sel, {w.cand, w.unary, w.hp}, {w.tokens, w.parents});
|
||||
} else {
|
||||
rad_note(b, "DFlash2: no library serves dflash_tree here; the drafter proposes its "
|
||||
"selector's chain");
|
||||
cfg.tree = false;
|
||||
op_sel = 0;
|
||||
}
|
||||
}
|
||||
if (!cfg.tree) op_sel = rw(b, RAD_OP(b, "dflash_select",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("steps", cfg.steps()),
|
||||
RAD_INT("top_k", cfg.sel_topk), RAD_INT("rank", cfg.sel_rank),
|
||||
RAD_INT("n_vocab", g.n_vocab_all),
|
||||
RAD_INT("anchor_stride", cfg.block), RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_pred, w_succ)),
|
||||
{w.cand, w.unary, w.hp}, {w.tokens});
|
||||
if (block_lo)
|
||||
if (block_lo && cfg.tree) {
|
||||
/* A tree over the short block's own lattice (the pass attends across its block, so it is
|
||||
* not the full pass's prefix): as many nodes as the block drafts, or the tree's own count
|
||||
* where that is fewer. */
|
||||
sel_lo_nodes = std::min<int64_t>(cfg.nodes(), block_lo - 1);
|
||||
op_sel_lo = RAD_OP(b, "dflash_tree",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("steps", block_lo - 1),
|
||||
RAD_INT("top_k", cfg.sel_topk), RAD_INT("rank", cfg.sel_rank),
|
||||
RAD_INT("n_vocab", g.n_vocab_all),
|
||||
RAD_INT("anchor_stride", block_lo),
|
||||
RAD_INT("nodes", sel_lo_nodes), RAD_F64("temp", cfg.tree_temp),
|
||||
RAD_STR("dtype", g.dtype)),
|
||||
RAD_WEIGHTS(w_pred, w_succ));
|
||||
if (op_sel_lo && rad_op_resolved(b, op_sel_lo)) {
|
||||
rw(b, op_sel_lo, {w.cand, w.unary, w.hp}, {w.tokens, w.parents});
|
||||
} else {
|
||||
rad_note(b, "DFlash2: no library serves dflash_tree at the short block; the tree keeps "
|
||||
"its own block under load");
|
||||
op_sel_lo = 0;
|
||||
block_lo = 0;
|
||||
sel_lo_nodes = 0;
|
||||
}
|
||||
} else if (block_lo)
|
||||
op_sel_lo = rw(b, RAD_OP(b, "dflash_select",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("steps", block_lo - 1),
|
||||
RAD_INT("top_k", cfg.sel_topk), RAD_INT("rank", cfg.sel_rank),
|
||||
@@ -1197,10 +1266,19 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
|
||||
}
|
||||
|
||||
RAD_ISSUE_N(c, op_hp, S, brows(w.sel_h, S), RAD_W(w_selp), brows(w.hp, S));
|
||||
RAD_ISSUE_N(c, lo ? op_sel_lo : op_sel, batch->n_seq, bcol(w.cand, 0, cfg.sel_topk, S),
|
||||
bcol(w.unary, 0, cfg.sel_topk, S), brows(w.hp, S),
|
||||
praw(batch->token_ids, RAD_I32, T), RAD_W(w_pred), RAD_W(w_succ),
|
||||
brows(w.tokens, batch->n_seq));
|
||||
/* A tree's outputs as views exactly its nodes wide, at the buffers' row pitch: at the short
|
||||
* block it grows fewer nodes than a row holds, and the proposals are read at the full pitch. */
|
||||
const int64_t nn = lo ? sel_lo_nodes : cfg.nodes();
|
||||
if (cfg.tree)
|
||||
RAD_ISSUE_N(c, lo ? op_sel_lo : op_sel, batch->n_seq, bcol(w.cand, 0, cfg.sel_topk, S),
|
||||
bcol(w.unary, 0, cfg.sel_topk, S), brows(w.hp, S),
|
||||
praw(batch->token_ids, RAD_I32, T), RAD_W(w_pred), RAD_W(w_succ),
|
||||
bcol(w.tokens, 0, nn, batch->n_seq), bcol(w.parents, 0, nn, batch->n_seq));
|
||||
else
|
||||
RAD_ISSUE_N(c, lo ? op_sel_lo : op_sel, batch->n_seq, bcol(w.cand, 0, cfg.sel_topk, S),
|
||||
bcol(w.unary, 0, cfg.sel_topk, S), brows(w.hp, S),
|
||||
praw(batch->token_ids, RAD_I32, T), RAD_W(w_pred), RAD_W(w_succ),
|
||||
brows(w.tokens, batch->n_seq));
|
||||
if (op_couple)
|
||||
RAD_ISSUE_N(c, lo ? op_couple_lo : op_couple, batch->n_seq,
|
||||
bcol(w.cand, 0, cfg.sel_topk, S), bcol(w.unary, 0, cfg.sel_topk, S),
|
||||
|
||||
@@ -1419,9 +1419,9 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
|
||||
}
|
||||
|
||||
if (separate_fp32_combine) {
|
||||
/* BF16 add widens both inputs to FP32, adds, then rounds once to BF16: exactly the
|
||||
* former cast + cast + F32 add + cast chain. Keep the routed gather's BF16 rounding
|
||||
* and the separate TP reductions; a local gather cannot fold those reductions. */
|
||||
/* BF16 add widens both inputs to FP32, adds, then rounds once to BF16: the same bytes as
|
||||
* cast + cast + F32 add + cast. The routed gather's BF16 rounding and the separate TP
|
||||
* reductions stay; a local gather cannot fold those reductions. */
|
||||
op_combine = rw(b, RAD_OP(b, "add",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
|
||||
RAD_STR("dtype", "bf16")),
|
||||
|
||||
@@ -178,7 +178,8 @@ inline int PleLayer::declare(RadBuilder* b, Names& nm, const Geom& geom, const C
|
||||
/* E4M3 with one scale for all 320 million rows (Qwen3.8-Flash-Next-FP8's choice; E4M3 has its
|
||||
* own exponent, a row's dynamic range is 2.3x, and the scale sits on a three-octave plateau).
|
||||
* One stored weight, `fp8_e4m3*bf16[*x*]`: codes plus a one-element scale plane, made by the
|
||||
* recipe (data/recipes/qwen4exp-w4.recipe); the gather takes them as two operands. The name map
|
||||
* recipe (data/recipes/qwen4exp-w4.recipe) or read as that release ships it (its shards under
|
||||
* one `weight_scale`); the gather takes them as two operands. The name map
|
||||
* joins the shards along rows and is declared first, since the encoding is looked up through
|
||||
* it. RAD_ACCESS_VOCAB: it is a vocabulary table, just not the token one. */
|
||||
const char* tn = nm.f("blk.%d.ple_ngram.weight", layer);
|
||||
|
||||
@@ -188,6 +188,10 @@ inline bool dflash2_probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
out->draft_depth = (int)(bs - 1);
|
||||
out->draft_depth_fixed = 1;
|
||||
out->chain_pays = 1;
|
||||
/* Its proposals verified as a tree of the same node count (dflash_tree, rad_drafter_tree): on
|
||||
* 4x R9700 +7% single-stream decode and +6-9% aggregate at 1-16 users over the selector's
|
||||
* chain (BetterBench, thinking off; on two cards +4% and +6-12%). */
|
||||
out->tree_pays = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+10
-12
@@ -831,12 +831,10 @@ struct G3Junction {
|
||||
/* The tallest step that takes the fp8 junction's lossy twin (G3JunctionFP8::op_ar6); a taller one
|
||||
* keeps the standalone lossy all-reduce, unfused. The fused kernel gives each block one row, so past
|
||||
* a few sequences the standalone all-reduce, spread over more blocks, wins despite its extra
|
||||
* launches. TranslateGemma-27B MXFP4 + DFlash2 at tp 2 on 2x R9700, --tp-wire wht6, production's
|
||||
* flags (--draft-short-from 8), greedy, aggregate tok/s, two rounds, limit never / 16 / 24 / any:
|
||||
* 2 streams (16 rows) 350.7 346.2 / 371.8 367.7 / 370.4 367.0 / 357.7 356.0; 3 (24 rows) 455.8
|
||||
* 451.2 / 455.1 454.2 / 462.8 460.6 / 437.3 436.8; 4 (32 rows) 553.7 553.6 / 557.9 554.2 / 553.9
|
||||
* 560.2 / 521.0 523.0; 8 (32 rows, the short block) 717.4 718.0 / 717.2 720.5 / 716.7 720.8 /
|
||||
* 662.4 661.9. One stream's 8 rows go out exact under the default --tp-wire-min-kb (op_ar). */
|
||||
* launches. TranslateGemma-27B MXFP4 + DFlash2 at tp 2 on 2x R9700 (--tp-wire wht6,
|
||||
* --draft-short-from 8, greedy): fusing raises aggregate throughput by 1-6% up to 24 rows and
|
||||
* lowers it by 6-8% at 32. One stream's 8 rows go out exact under the default --tp-wire-min-kb
|
||||
* (op_ar). */
|
||||
constexpr int64_t kAr6MaxRows = 24;
|
||||
|
||||
/* ---- the MXFP4 trunk's junction (libr4d `rmsnorm_quant_fp8`): the add of the branch `x` into
|
||||
@@ -848,10 +846,10 @@ struct G3JunctionFP8 {
|
||||
rad_op op = 0;
|
||||
/* At two ranks: the branch's all-reduce and post-norm folded in front
|
||||
* (ar_rmsnorm_post_quant_fp8), `x` then being this rank's partial sum. One launch. `op_ar` sends
|
||||
* the exact wire, taken by the steps whose message goes out exact (ar_wire_is_exact: a decode
|
||||
* step of a few rows, under --tp-wire-min-kb); `op_ar6` the rotated 6-bit one, declared under a
|
||||
* lossy --tp-wire and taken by the steps above that up to kAr6MaxRows, byte-identical to the
|
||||
* standalone lossy all-reduce, rmsnorm and rmsnorm_quant_fp8 it replaces. */
|
||||
* the exact wire, for the steps whose message goes out exact (ar_wire_is_exact: a decode step
|
||||
* of a few rows, under --tp-wire-min-kb). `op_ar6` sends the rotated 6-bit one: declared under
|
||||
* a lossy --tp-wire, it takes the steps above that up to kAr6MaxRows and is byte-identical to
|
||||
* the standalone lossy all-reduce, rmsnorm and rmsnorm_quant_fp8 it replaces. */
|
||||
rad_op op_ar = 0, op_ar6 = 0;
|
||||
Geom gm{};
|
||||
rad_buf x = 0, res = 0, q = 0, s = 0;
|
||||
@@ -883,8 +881,8 @@ struct G3JunctionFP8 {
|
||||
const rad_buf wa[4] = { x, q, s, res };
|
||||
rad_op_reads (b, op_ar, rd, 2);
|
||||
rad_op_writes(b, op_ar, wa, 4);
|
||||
/* The lossy twin where that wire is reachable and the row is whole 64-element
|
||||
* groups (the kernel pads a partial last 512-element chunk). */
|
||||
/* The lossy twin, under a lossy wire and for a row of whole 64-element groups
|
||||
* (the kernel pads a partial last 512-element chunk). */
|
||||
if (!g.wire_exact && n % 64 == 0) {
|
||||
const RadParam p6[] = { RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", n),
|
||||
RAD_F64("eps", g.eps), RAD_INT("group", RAD_FP8_BLOCK),
|
||||
|
||||
@@ -423,6 +423,12 @@ struct Gemma4Attn {
|
||||
* the copies of k and v too, ahead of the unfused prologue, which reads them dense. */
|
||||
rad_op op_attn_row = 0, op_attn_dec = 0, op_lift_q = 0, op_lift_k = 0, op_lift_v = 0;
|
||||
int64_t dec_hi = 0;
|
||||
/* Tree verification (rad_drafter_tree): attention that honours RadBatch::spec_parent over a
|
||||
* step's decode rows, reading q in the row (attn_paged_qrow_tree) or dense (attn_paged_tree,
|
||||
* after op_tree_lift copies it out). Declared only when `tree` is set before declare; 0 where
|
||||
* no library serves it, which the model reads as "no trees". */
|
||||
bool tree = false;
|
||||
rad_op op_attn_tree = 0, op_tree_lift = 0;
|
||||
/* the feed-forward's gate_up read into the cache beside this block's all-reduce, or 0 (see
|
||||
* "Prefetch beside the reductions") */
|
||||
rad_op op_pf = 0;
|
||||
@@ -697,6 +703,20 @@ int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const
|
||||
}
|
||||
}
|
||||
|
||||
/* The tree form over the decode rows: a verify block of 1 + n_spec rows a sequence, so the
|
||||
* split-KV band (q_len * gqa <= 64) covers it at every class here. In the row where the
|
||||
* prologue leaves q there, else dense after a copy. */
|
||||
if (tree) {
|
||||
const int64_t thi = std::min<int64_t>(
|
||||
g.max_tok, std::max<int64_t>({ (int64_t)1, 64 / cls.gqa(), (int64_t)g.max_spec + 1 }));
|
||||
if (fused && op_qnr && rad_op_declared(b, "attn_paged_qrow_tree"))
|
||||
op_attn_tree = attn("attn_paged_qrow_tree", {w.qkv}, thi);
|
||||
if (!op_attn_tree && rad_op_declared(b, "attn_paged_tree")) {
|
||||
op_attn_tree = attn("attn_paged_tree", {w.q}, thi);
|
||||
if (op_attn_tree && fused && op_qnr) op_tree_lift = lift(qd, w.q);
|
||||
}
|
||||
}
|
||||
|
||||
/* With the cache store folded into the prologue where a library serves it
|
||||
* (qkv_norm_rope_kv_store: the same prologue, then kv_store of the finished k and v from the
|
||||
* same launch, byte for byte), one launch a layer fewer. Declared after the attention ops,
|
||||
@@ -947,26 +967,51 @@ void Gemma4Attn::attend(RadCtx* c, const RadBatch* batch) const {
|
||||
|
||||
if (op_kv) RAD_ISSUE(c, op_kv, K, V, praw(slot, RAD_I32, T), kv_cache(kv, layer));
|
||||
|
||||
/* attn_paged reads q dense: one row of the row's slice is, a taller step copies q out. */
|
||||
rad_op a_op = op_attn;
|
||||
RadOperand a_q = brows(w.q, T);
|
||||
if (op_attn_row) {
|
||||
a_op = op_attn_row;
|
||||
a_q = Q;
|
||||
} else if (op_attn_dec && batch->max_q_len <= dec_hi) {
|
||||
a_op = op_attn_dec;
|
||||
a_q = Q;
|
||||
} else if (row) {
|
||||
if (T == 1) a_q = Q;
|
||||
else RAD_ISSUE_N(c, op_lift_q, T, Q, brows(w.q, T));
|
||||
/* A tree step (rad_drafter_tree): every decode row's verify block sees, inside the block,
|
||||
* only that row's ancestors (RadBatch::spec_parent), so the decode rows go through the tree
|
||||
* form. On a step of decode rows alone that is the step's attention; beside a prefill chunk the
|
||||
* ordinary form runs over every row first and the tree form then rewrites the decode rows,
|
||||
* which come first (spec §8). */
|
||||
const bool tree_step = op_attn_tree && batch->spec_parent && batch->draft_pass == 0 &&
|
||||
batch->n_seq_decode > 0;
|
||||
if (!tree_step || batch->n_seq_decode < batch->n_seq) {
|
||||
/* attn_paged reads q dense: one row of the row's slice is, a taller step copies q out. */
|
||||
rad_op a_op = op_attn;
|
||||
RadOperand a_q = brows(w.q, T);
|
||||
if (op_attn_row) {
|
||||
a_op = op_attn_row;
|
||||
a_q = Q;
|
||||
} else if (op_attn_dec && batch->max_q_len <= dec_hi) {
|
||||
a_op = op_attn_dec;
|
||||
a_q = Q;
|
||||
} else if (row) {
|
||||
if (T == 1) a_q = Q;
|
||||
else RAD_ISSUE_N(c, op_lift_q, T, Q, brows(w.q, T));
|
||||
}
|
||||
RAD_ISSUE_N(c, a_op, batch->max_q_len,
|
||||
a_q, kv_cache(kv, layer),
|
||||
praw2(table, RAD_I32, batch->n_seq, kvb ? kvb->block_table_pitch : 0),
|
||||
praw(used, RAD_I32, batch->n_seq),
|
||||
RAD_NONE, RAD_NONE,
|
||||
praw(batch->cu_seqlens, RAD_I32, batch->n_seq + 1),
|
||||
brows(w.attn, T));
|
||||
}
|
||||
if (tree_step) {
|
||||
const int64_t Td = batch->n_tok_decode, Sd = batch->n_seq_decode;
|
||||
RadOperand tq = row ? bcol(w.qkv, 0, qd, Td) : brows(w.q, Td);
|
||||
if (op_tree_lift) {
|
||||
RAD_ISSUE_N(c, op_tree_lift, Td, bcol(w.qkv, 0, qd, Td), brows(w.q, Td));
|
||||
tq = brows(w.q, Td);
|
||||
}
|
||||
RAD_ISSUE_N(c, op_attn_tree, batch->max_q_len_decode,
|
||||
tq, kv_cache(kv, layer),
|
||||
praw2(table, RAD_I32, Sd, kvb ? kvb->block_table_pitch : 0),
|
||||
praw(used, RAD_I32, Sd),
|
||||
RAD_NONE, RAD_NONE,
|
||||
praw(batch->cu_seqlens, RAD_I32, Sd + 1),
|
||||
brows(w.attn, Td),
|
||||
praw(batch->spec_parent, RAD_I32, Td));
|
||||
}
|
||||
RAD_ISSUE_N(c, a_op, batch->max_q_len,
|
||||
a_q, kv_cache(kv, layer),
|
||||
praw2(table, RAD_I32, batch->n_seq, kvb ? kvb->block_table_pitch : 0),
|
||||
praw(used, RAD_I32, batch->n_seq),
|
||||
RAD_NONE, RAD_NONE,
|
||||
praw(batch->cu_seqlens, RAD_I32, batch->n_seq + 1),
|
||||
brows(w.attn, T));
|
||||
}
|
||||
|
||||
/* A slice's rows see exactly the keys they see in the whole step: the cache holds the step's
|
||||
@@ -1811,6 +1856,9 @@ struct Model {
|
||||
Dflash2 df2{};
|
||||
Dflash2::Wire df2w{};
|
||||
bool have_df2 = false;
|
||||
/* Tree verification (RadBuildCtx::draft_tree): every layer's attention has its tree form, and
|
||||
* the drafter proposes trees once it has declared dflash_tree too. */
|
||||
bool tree = false;
|
||||
/* The prefill overlap runs attention per slice: a trim a KV group (sliding, global). */
|
||||
SliceTrim trim[2]{};
|
||||
int n_trim = 0;
|
||||
@@ -1877,6 +1925,13 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
|
||||
Dflash2::Config dc{};
|
||||
dc.n_taps = m.n_taps;
|
||||
dc.block = ctx->max_spec + 1;
|
||||
/* A tree (m.tree, asked before the trunk) is `max_spec` nodes grown over the lattice of the
|
||||
* block the drafter was trained at, whatever its width: the block is the trained one and
|
||||
* --num-speculative-tokens counts nodes. */
|
||||
if (m.tree && gemma4::dflash_block(meta) > 1) {
|
||||
dc.block = gemma4::dflash_block(meta);
|
||||
dc.tree_nodes = ctx->max_spec;
|
||||
}
|
||||
dc.taps = df2_geti(meta, "conv_kernel_size", 2);
|
||||
dc.group = df2_geti(meta, "conv_group_size", 16);
|
||||
dc.sel_rank = df2_geti(meta, "selector_rank", 0);
|
||||
@@ -1985,13 +2040,39 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
|
||||
dw.dpair = decl_b(b, nm.f("df_dpair"), RAD_F32, {sr, tw, 2});
|
||||
dw.dgath = decl_b(b, nm.f("df_dgath"), RAD_F32, {sr, (int64_t)dg.world * tw, 2});
|
||||
}
|
||||
dw.tokens = decl_b(b, nm.f("dflash_tokens"), RAD_I32, {dg.max_seqs, dc.steps()});
|
||||
const int64_t nodes = dc.tree_nodes > 0 ? dc.tree_nodes : dc.steps();
|
||||
dw.tokens = decl_b(b, nm.f("dflash_tokens"), RAD_I32, {dg.max_seqs, nodes});
|
||||
/* Trees (RadBuildCtx::draft_tree; the trunk's attention already has its tree form): the
|
||||
* selector's lattice grown best-first into `steps` nodes, each edge weighed by the softmax of
|
||||
* its scores over a temperature. 1.25 measured best at 7 nodes on the speculator benchmarks
|
||||
* (greedy, and sampled at 0.7 and 1.0); its path probabilities are calibrated there. */
|
||||
if (m.tree) {
|
||||
dc.tree = true;
|
||||
dc.tree_temp = 1.25;
|
||||
dw.parents = decl_b(b, nm.f("dflash_parents"), RAD_I32, {dg.max_seqs, nodes});
|
||||
/* A block of 16 (the DBloom drafter): its 15-node tree's verify rows pay at one sequence
|
||||
* and not at many, so the query pass is declared at half the block too, over which the
|
||||
* drafter grows a 7-node tree of its own: the drafter's short block
|
||||
* (rad_drafter_short_block), which the core runs from two decoding sequences on
|
||||
* (--draft-short-from auto). A shorter block's tree verifies as few rows as that already. */
|
||||
if (dc.block >= 16 && (dc.block & (dc.block - 1)) == 0) {
|
||||
dc.short_block = 1;
|
||||
dc.short_rows = dc.block / 2;
|
||||
}
|
||||
}
|
||||
|
||||
/* The head copy comes from the tied table, under the block's own name; the recipe makes it
|
||||
* 8-bit. */
|
||||
RAD_ARCH_TRY(m.df2.declare(b, nm, dg, dc, (int)m.n_main, m.kv_draft, m.w_tok, "draft",
|
||||
nm.ckpt("model.language_model.embed_tokens.weight"), dw));
|
||||
m.have_df2 = true;
|
||||
m.tree = m.df2.cfg.tree; /* false where dflash_tree did not resolve */
|
||||
if (!m.tree && nodes != dc.steps()) {
|
||||
fprintf(stderr, "radiance: gemma4: a DFlash2 tree of %lld nodes needs dflash_tree, which no "
|
||||
"library serves here; the block's chain is %lld\n", (long long)nodes,
|
||||
(long long)dc.steps());
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
RadDrafterDecl drf{};
|
||||
drf.name = "dflash2";
|
||||
@@ -1999,7 +2080,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
|
||||
drf.depth = ctx->max_spec;
|
||||
drf.block = dc.block;
|
||||
drf.proposal = dw.tokens;
|
||||
drf.proposal_pitch = dc.steps();
|
||||
drf.proposal_pitch = nodes;
|
||||
drf.window = dc.window;
|
||||
drf.mask_token = (int32_t)df2_geti(meta, "mask_token_id", -1);
|
||||
if (drf.mask_token < 0) {
|
||||
@@ -2008,6 +2089,17 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
|
||||
return RAD_E_FORMAT;
|
||||
}
|
||||
RAD_ARCH_TRY(rad_declare_drafter(b, &drf));
|
||||
if (m.tree) {
|
||||
RAD_ARCH_TRY(rad_drafter_tree(b, dw.parents));
|
||||
if (m.df2.block_lo > 0) {
|
||||
RAD_ARCH_TRY(rad_drafter_short_block(b, m.df2.block_lo));
|
||||
rad_note(b, "DFlash2 drafter: a short block of %lld rows, a tree of %lld nodes over it",
|
||||
(long long)m.df2.block_lo, (long long)m.df2.sel_lo_nodes);
|
||||
}
|
||||
rad_note(b, "DFlash2 drafter: a tree of %lld nodes a step (softmax temperature %.3g) over "
|
||||
"the lattice of its %lld-row block, verified in %lld rows", (long long)nodes,
|
||||
dc.tree_temp, (long long)dc.block, (long long)nodes + 1);
|
||||
}
|
||||
|
||||
rad_note(b, "DFlash2 drafter: %lld layers at absolute %lld.., %lld heads / %lld kv x %lld, ffn "
|
||||
"%lld, %s attention with a %lld-token window, %d trunk taps; one forward proposes "
|
||||
@@ -2351,6 +2443,14 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
Gemma4Attn::Wire gw{}; gw.x = m.b_x; gw.h = m.b_h; gw.q = m.b_qg; gw.k = m.b_kg; gw.v = m.b_vg; gw.attn = m.b_atting;
|
||||
Gemma4Mlp::Wire mw{}; mw.x = m.b_x; mw.h = m.b_h; mw.gu = m.b_gu; mw.ffn = m.b_ffn;
|
||||
|
||||
/* Tree verification for a DFlash2 drafter (RadBuildCtx::draft_tree, which the engine sets
|
||||
* from this plugin's probe; rad_drafter_tree): asked of every layer's attention here, before
|
||||
* the drafter, and used only if every layer has it. Not where a layer reads another layer's
|
||||
* cache (KV sharing, Gemma 4 E2B/E4B): untested there. */
|
||||
const bool tree_want = ctx->max_spec > 0 && m.n_taps > 0 && gemma4::dflash_kind(meta) == 2 &&
|
||||
kv_shared == 0 && ctx->draft_tree != 0;
|
||||
bool tree_all = tree_want;
|
||||
|
||||
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];
|
||||
@@ -2360,8 +2460,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
const Gemma4Attn::Wire& aw = lay.global ? gw : sw;
|
||||
/* A KV-shared layer is not bound: the group's pages hold only the layers that write. */
|
||||
if (kv_src[(size_t)l] < 0) RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, kg));
|
||||
lay.attn.tree = tree_want;
|
||||
RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, cgeo, kg, aw, pre,
|
||||
lay.global ? &m.b_qkvg : &m.b_qkvs, kv_src[(size_t)l]));
|
||||
if (tree_want && !lay.attn.op_attn_tree) tree_all = false;
|
||||
/* The double-wide layers write their own pair of buffers (see b_gu_w). */
|
||||
Gemma4Mlp::Wire lw = mw;
|
||||
const bool wide = wide_mlp && l >= first_shared;
|
||||
@@ -2626,6 +2728,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
* declaration order is the buffer planner's liveness. It reads the taps the trunk copied into
|
||||
* `dflash_src` and the trunk's tied table, as its embedding and, where the container holds no
|
||||
* 8-bit copy, as its head. */
|
||||
if (tree_want && !tree_all)
|
||||
rad_note(b, "DFlash2 tree: no library serves tree attention for every layer here "
|
||||
"(attn_paged_qrow_tree / attn_paged_tree); the drafter proposes a chain");
|
||||
m.tree = tree_all;
|
||||
if (m.n_taps > 0 && gemma4::dflash_kind(meta) == 2) {
|
||||
RAD_ARCH_TRY(declare_dflash2(b, meta, ctx, m));
|
||||
} else if (m.n_taps > 0) {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
* returns each sequence's pooled row. Attention is bidirectional on every layer, so nothing is
|
||||
* paged: a token attends to later tokens, and there is nothing to carry between steps.
|
||||
*
|
||||
* ============================== THE SHAPE ==============================
|
||||
* Shape:
|
||||
*
|
||||
* 12 layers, hidden 768, 12 heads of 64, ffn 3072, vocab 250048, type vocab 1
|
||||
* post-norm block, the config's `prenorm: false`:
|
||||
@@ -20,14 +20,12 @@
|
||||
* output: the mean of every real token, then cast to the f32 pooled buffer
|
||||
*
|
||||
* The expert tables are one concatenated tensor each: w1 [8 * 3072, 768] as [out, in], w2
|
||||
* [8 * 3072, 768] as [in, out] per expert, so w2 is sliced then transposed at convert
|
||||
* (RAD_MAP_TRANSPOSE with src_index and src_rows). Every expert's two tensors take distinct group
|
||||
* slots.
|
||||
* [8 * 3072, 768] as [in, out] per expert, so w2 is sliced, then transposed (RAD_MAP_TRANSPOSE
|
||||
* with src_index and src_rows). Every expert's two tensors take distinct group slots.
|
||||
*
|
||||
* The block order and the unused shared experts.bias were settled against the production
|
||||
* llama.cpp embedder by scripts/nomic_ref.py; the winning variant and the vectors it produced are
|
||||
* in tests/data/nomic/golden.json (norm=post, bias=none). The checkpoint's `experts.bias` tensors
|
||||
* are therefore not declared and stay unconverted.
|
||||
* The block order and the omitted shared bias match llama.cpp's embedder for this model
|
||||
* (post-norm, no expert bias). The checkpoint's `experts.bias` tensors are not declared and stay
|
||||
* unconverted.
|
||||
*/
|
||||
#include <arch/rad_arch.h>
|
||||
|
||||
@@ -40,10 +38,8 @@ namespace nomicbert {
|
||||
|
||||
using namespace rad::arch;
|
||||
|
||||
/* Post-norm, the reading scripts/nomic_ref.py and tests/data/nomic/golden.json settle: the
|
||||
* attention is on the raw stream, then LN1, then the feed-forward on LN1's output, then LN2 over
|
||||
* that output plus the feed-forward's. The shared experts.bias is not applied (bias=none there). */
|
||||
constexpr bool kPostNorm = true;
|
||||
/* Post-norm: attention on the raw stream, then LN1, then the feed-forward on LN1's output, then
|
||||
* LN2 over that output plus the feed-forward's. */
|
||||
|
||||
struct Layer {
|
||||
bool moe = false;
|
||||
@@ -77,10 +73,9 @@ struct Model {
|
||||
|
||||
static Model g_model[MAX_RANKS];
|
||||
|
||||
/* The layer schedule, read rather than derived: a wrong interval would turn a dense layer into a
|
||||
* MoE layer whose tensors the checkpoint does not hold, which convert refuses but only after the
|
||||
* graph was built. Layer l is routed when (l + 1) % moe_every_n_layers == 0, as nomic's
|
||||
* modeling_hf_nomic_bert builds it. */
|
||||
/* The layer schedule, read from moe_every_n_layers rather than derived: a wrong interval would
|
||||
* declare a MoE layer whose tensors the checkpoint does not hold. Layer l is routed when
|
||||
* (l + 1) % moe_every_n_layers == 0, as nomic's modeling_hf_nomic_bert builds it. */
|
||||
static bool is_moe_layer(int64_t l, int64_t every) {
|
||||
return every > 0 && (l + 1) % every == 0;
|
||||
}
|
||||
@@ -99,7 +94,7 @@ static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
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;
|
||||
/* One 475M-parameter encoder on one card; tensor parallelism would only add an all-reduce per
|
||||
/* A 475M-parameter encoder fits one card; tensor parallelism would only add an all-reduce per
|
||||
* layer. */
|
||||
if (ctx->world_size > 1) {
|
||||
fprintf(stderr, "radiance: nomicbert runs on one card (--tp 1); the model is 475M "
|
||||
@@ -325,8 +320,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
src("mlp.router.layer.weight")));
|
||||
y.w_router = decl_w(b, nm.f("blk.%lld.ffn_router.weight", ll), RAD_BF16,
|
||||
{m.n_expert, E}, A, N, gl);
|
||||
/* mlp.experts.bias is deliberately not mapped: the verified variant applies no
|
||||
* expert bias, so the tensor is left in the checkpoint. */
|
||||
/* mlp.experts.bias is not mapped (no expert bias, see the header). */
|
||||
/* Each projection is a consecutive run of expert handles: all w1 in expert order,
|
||||
* then all w2. A grouped GEMM takes the run's first handle and its length. The name
|
||||
* maps come first: declare reads a weight's planned encoding when the weight is
|
||||
@@ -356,9 +350,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
}
|
||||
}
|
||||
|
||||
const rad_buf att_in = kPostNorm ? m.b_x : m.b_h;
|
||||
y.op_qkv = rw(b, gemm_bias_op(3 * E, E, y.w_qkv, y.b_qkv, nullptr),
|
||||
{att_in}, {m.b_qkv});
|
||||
{m.b_x}, {m.b_qkv});
|
||||
y.op_rope = rw(b, RAD_OP(b, "rope",
|
||||
RAD_PARAMS(RAD_RANGE("M", 1, T), RAD_INT("head_dim", g.head_dim),
|
||||
RAD_INT("n_head", g.n_head),
|
||||
@@ -381,12 +374,8 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
if (!y.moe) {
|
||||
y.op_fc1 = rw(b, gemm_bias_op(g.n_ff, E, y.w_fc1, y.b_fc1, "gelu"),
|
||||
{m.b_h}, {m.b_ff});
|
||||
if constexpr (kPostNorm)
|
||||
y.op_fc2 = rw(b, gemm_bias_op(E, g.n_ff, y.w_fc2, y.b_fc2, nullptr),
|
||||
{m.b_ff, m.b_h}, {m.b_o});
|
||||
else
|
||||
y.op_fc2 = rw(b, gemm_bias_op(E, g.n_ff, y.w_fc2, y.b_fc2, nullptr),
|
||||
{m.b_ff, m.b_x}, {m.b_x});
|
||||
y.op_fc2 = rw(b, gemm_bias_op(E, g.n_ff, y.w_fc2, y.b_fc2, nullptr),
|
||||
{m.b_ff, m.b_h}, {m.b_o});
|
||||
} else {
|
||||
y.op_router = rw(b, gemm_op(m.n_expert, E, y.w_router), {m.b_h}, {m.b_logits});
|
||||
y.op_topk = rw(b, RAD_OP(b, "router_topk",
|
||||
@@ -416,16 +405,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
RAD_STR("act", "none")),
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_edn, m.b_ew, m.b_sorted}, {m.b_emoe});
|
||||
if constexpr (kPostNorm)
|
||||
y.op_add = add_op(m.b_h, m.b_emoe, m.b_o);
|
||||
else
|
||||
y.op_add = add_op(m.b_x, m.b_emoe, m.b_x);
|
||||
y.op_add = add_op(m.b_h, m.b_emoe, m.b_o);
|
||||
}
|
||||
|
||||
if constexpr (kPostNorm)
|
||||
y.op_norm2 = rw(b, layernorm_op(y.w_norm2, y.b_norm2), {m.b_o}, {m.b_x});
|
||||
else
|
||||
y.op_norm2 = rw(b, layernorm_op(y.w_norm2, y.b_norm2), {m.b_x}, {m.b_h});
|
||||
y.op_norm2 = rw(b, layernorm_op(y.w_norm2, y.b_norm2), {m.b_o}, {m.b_x});
|
||||
|
||||
if (!y.op_qkv || !y.op_rope || !y.op_attn || !y.op_out || !y.op_norm1 || !y.op_norm2)
|
||||
return RAD_E_INVAL;
|
||||
@@ -446,13 +429,14 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
RAD_NOWEIGHTS),
|
||||
{m.b_mean}, {m.b_pooled});
|
||||
|
||||
rad_note(b, "embedding model: %lld layers (%lld routed every %lld), hidden %lld, ffn %lld, "
|
||||
"%lld heads of %lld, %lld experts top %lld; post-norm %d, no expert bias; "
|
||||
int64_t n_routed = 0;
|
||||
for (const Layer& y : m.layers) n_routed += y.moe ? 1 : 0;
|
||||
rad_note(b, "embedding model: %lld layers (%lld routed, every %lld), hidden %lld, ffn %lld, "
|
||||
"%lld heads of %lld, %lld experts top %lld; post-norm, no expert bias; "
|
||||
"mean-pooled to %lld",
|
||||
(long long)g.n_layer, (long long)m.n_expert, (long long)every, (long long)E,
|
||||
(long long)g.n_layer, (long long)n_routed, (long long)every, (long long)E,
|
||||
(long long)g.n_ff, (long long)g.n_head, (long long)g.head_dim,
|
||||
(long long)m.n_expert, (long long)m.n_expert_used, kPostNorm ? 1 : 0,
|
||||
(long long)E);
|
||||
(long long)m.n_expert, (long long)m.n_expert_used, (long long)E);
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
@@ -474,43 +458,22 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
for (int64_t l = 0; l < g.n_layer; ++l) {
|
||||
const Layer& y = m.layers[(size_t)l];
|
||||
|
||||
auto attention = [&] {
|
||||
const rad_buf att_in = kPostNorm ? m.b_x : m.b_h;
|
||||
RAD_ISSUE_N(c, y.op_qkv, T, brows(att_in, T), RAD_W(y.w_qkv), RAD_W(y.b_qkv),
|
||||
RAD_NONE, brows(m.b_qkv, T));
|
||||
RAD_ISSUE(c, y.op_rope, brows(m.b_qkv, T), pos);
|
||||
RAD_ISSUE(c, y.op_attn, bcol(m.b_qkv, 0, E, T), bcol(m.b_qkv, E, E, T),
|
||||
bcol(m.b_qkv, 2 * E, E, T), cu, brows(m.b_att, T));
|
||||
RAD_ISSUE_N(c, y.op_out, T, brows(m.b_att, T), RAD_W(y.w_out), RAD_W(y.b_out),
|
||||
brows(m.b_x, T), brows(m.b_x, T));
|
||||
};
|
||||
|
||||
if constexpr (kPostNorm) {
|
||||
attention();
|
||||
RAD_ISSUE(c, y.op_norm1, brows(m.b_x, T), RAD_W(y.w_norm1), RAD_W(y.b_norm1),
|
||||
brows(m.b_h, T));
|
||||
} else {
|
||||
RAD_ISSUE(c, y.op_norm1, brows(m.b_x, T), RAD_W(y.w_norm1), RAD_W(y.b_norm1),
|
||||
brows(m.b_h, T));
|
||||
attention();
|
||||
}
|
||||
RAD_ISSUE_N(c, y.op_qkv, T, brows(m.b_x, T), RAD_W(y.w_qkv), RAD_W(y.b_qkv),
|
||||
RAD_NONE, brows(m.b_qkv, T));
|
||||
RAD_ISSUE(c, y.op_rope, brows(m.b_qkv, T), pos);
|
||||
RAD_ISSUE(c, y.op_attn, bcol(m.b_qkv, 0, E, T), bcol(m.b_qkv, E, E, T),
|
||||
bcol(m.b_qkv, 2 * E, E, T), cu, brows(m.b_att, T));
|
||||
RAD_ISSUE_N(c, y.op_out, T, brows(m.b_att, T), RAD_W(y.w_out), RAD_W(y.b_out),
|
||||
brows(m.b_x, T), brows(m.b_x, T));
|
||||
RAD_ISSUE(c, y.op_norm1, brows(m.b_x, T), RAD_W(y.w_norm1), RAD_W(y.b_norm1),
|
||||
brows(m.b_h, T));
|
||||
|
||||
if (!y.moe) {
|
||||
RAD_ISSUE_N(c, y.op_fc1, T, brows(m.b_h, T), RAD_W(y.w_fc1), RAD_W(y.b_fc1),
|
||||
RAD_NONE, brows(m.b_ff, T));
|
||||
if constexpr (kPostNorm)
|
||||
RAD_ISSUE_N(c, y.op_fc2, T, brows(m.b_ff, T), RAD_W(y.w_fc2),
|
||||
RAD_W(y.b_fc2), brows(m.b_h, T), brows(m.b_o, T));
|
||||
else
|
||||
RAD_ISSUE_N(c, y.op_fc2, T, brows(m.b_ff, T), RAD_W(y.w_fc2),
|
||||
RAD_W(y.b_fc2), brows(m.b_x, T), brows(m.b_x, T));
|
||||
if constexpr (kPostNorm)
|
||||
RAD_ISSUE(c, y.op_norm2, brows(m.b_o, T), RAD_W(y.w_norm2), RAD_W(y.b_norm2),
|
||||
brows(m.b_x, T));
|
||||
RAD_ISSUE_N(c, y.op_fc2, T, brows(m.b_ff, T), RAD_W(y.w_fc2),
|
||||
RAD_W(y.b_fc2), brows(m.b_h, T), brows(m.b_o, T));
|
||||
} else {
|
||||
if constexpr (!kPostNorm)
|
||||
RAD_ISSUE(c, y.op_norm2, brows(m.b_x, T), RAD_W(y.w_norm2), RAD_W(y.b_norm2),
|
||||
brows(m.b_h, T));
|
||||
RAD_ISSUE(c, y.op_router, brows(m.b_h, T), RAD_W(y.w_router), brows(m.b_logits, T));
|
||||
RAD_ISSUE(c, y.op_topk, brows(m.b_logits, T), brows(m.b_ids, T), brows(m.b_ew, T));
|
||||
RAD_ISSUE(c, y.op_scatter, brows(m.b_ids, T), brows(m.b_sorted, T * K),
|
||||
@@ -524,14 +487,10 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
brows(m.b_edn, T * K));
|
||||
RAD_ISSUE_N(c, y.op_gather, T, brows(m.b_edn, T * K), brows(m.b_ew, T),
|
||||
brows(m.b_sorted, T * K), RAD_NONE, RAD_NONE, brows(m.b_emoe, T));
|
||||
if constexpr (kPostNorm)
|
||||
RAD_ISSUE(c, y.op_add, brows(m.b_h, T), brows(m.b_emoe, T), brows(m.b_o, T));
|
||||
else
|
||||
RAD_ISSUE(c, y.op_add, brows(m.b_x, T), brows(m.b_emoe, T), brows(m.b_x, T));
|
||||
if constexpr (kPostNorm)
|
||||
RAD_ISSUE(c, y.op_norm2, brows(m.b_o, T), RAD_W(y.w_norm2), RAD_W(y.b_norm2),
|
||||
brows(m.b_x, T));
|
||||
RAD_ISSUE(c, y.op_add, brows(m.b_h, T), brows(m.b_emoe, T), brows(m.b_o, T));
|
||||
}
|
||||
RAD_ISSUE(c, y.op_norm2, brows(m.b_o, T), RAD_W(y.w_norm2), RAD_W(y.b_norm2),
|
||||
brows(m.b_x, T));
|
||||
}
|
||||
|
||||
RAD_ISSUE(c, m.op_mean, brows(m.b_x, T), cu, brows(m.b_mean, S));
|
||||
|
||||
@@ -434,11 +434,11 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
|
||||
/* --draft-coupling (RadBuildCtx::draft_coupling): the walk samples each draft from Q, its
|
||||
* scores' softmax over the best eight candidates, and the sampler couples the target's own draw
|
||||
* to it (abi/rad_spechub.h). The planes are named as the MTP head's, which is how the engine
|
||||
* finds them. RADIANCE_DFLASH_Q_TEMP sets Q's temperature; any value keeps the output exact
|
||||
* and moves only acceptance. The default, 0.6, is where held-out acceptance peaked on
|
||||
* Qwen3.8-27B MXFP4 + its DFlash2 drafter at request temperature 1 (tokens a step at x4, 32
|
||||
* rows: ordinary rule 3.52; Q at 1.0 3.71, 0.7 3.88, 0.5 3.85, 0.35 3.77): the walk's scores
|
||||
* are flatter than the target's law, and a sharper Q puts its mass where the target's is. */
|
||||
* finds them. Q's temperature keeps the output exact at any value and moves only acceptance;
|
||||
* 0.6 is where held-out acceptance peaked on Qwen3.8-27B MXFP4 + its DFlash2 drafter at request
|
||||
* temperature 1 (tokens a step at x4, 32 rows: ordinary rule 3.52; Q at 1.0 3.71, 0.7 3.88,
|
||||
* 0.5 3.85, 0.35 3.77): the walk's scores are flatter than the target's law, and a sharper Q
|
||||
* puts its mass where the target's is. */
|
||||
if (ctx->draft_coupling) {
|
||||
if (dc.steps() > RAD_MTP_COUPLE_MAX_ROUNDS || dc.sel_topk > RAD_DFLASH_COUPLE_MAX_K ||
|
||||
dc.sel_rank % RAD_DFLASH_EDGE_PARTS || dc.sel_rank > RAD_DFLASH_COUPLE_MAX_R) {
|
||||
@@ -449,15 +449,6 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
dc.q_temp = 0.6;
|
||||
if (const char* e = std::getenv("RADIANCE_DFLASH_Q_TEMP")) {
|
||||
const double t = std::strtod(e, nullptr);
|
||||
if (!(t > 0.0) || t > 100.0) {
|
||||
fprintf(stderr, "radiance: RADIANCE_DFLASH_Q_TEMP=%s is not a temperature in "
|
||||
"(0, 100]\n", e);
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
dc.q_temp = t;
|
||||
}
|
||||
dw.couple_draws = decl_b(b, nm.f("mtp_couple_draws"), RAD_U32,
|
||||
{dg.max_seqs, RAD_MTP_COUPLE_DRAW_WORDS}, RAD_BUF_PERSIST);
|
||||
dw.couple_state = decl_b(b, nm.f("mtp_couple_state"), RAD_U32,
|
||||
|
||||
Reference in new issue
Block a user