diff --git a/CMakeLists.txt b/CMakeLists.txt index 6abff545..ec4b73ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.21) # VERSION is what /version reports and what a client checks compatibility against. It is NOT the # ABI version -- that lives in abi/rad_abi.h and is read out of the header below, because # a plugin contract and a server release do not move together. -project(radiance VERSION 1.2.2 LANGUAGES C CXX) +project(radiance VERSION 1.2.3 LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/NOTICE b/NOTICE index e9e7e043..b2776281 100644 --- a/NOTICE +++ b/NOTICE @@ -1,18 +1,19 @@ radiance Copyright 2026 Deadcode and the radiance contributors -Written by Deadcode, with contributions from tcclaviger (the N-rank all-reduce kernels), ggz14 -(the MXFP4 GEMM, the A-tiled MXFP4 prefill GEMM and the GDN kernels' overflow guards), slydlake -(the MXFP4 decode and prefill GEMMs and the MXFP4 linear, stop strings after the reasoning block, -and the draft refill on a prefix hit), goodman-b (four-rank tensor parallelism -for Qwen3.8-Flash-Next), keptn42 (the four-rank all-reduce served in place, the fused four-rank -writes, the n-gram table in RAM, the four-class expert split, the Grafana dashboard, the routed -GEMM's decode loads and run search, the gated residual's prefill read form and row groups, the -fused router launch, the adaptive draft window, the MoE prefill forms and the sorted operand's -decode kernel), mechkbfan (the token embedding in the pinned host pool), Code000 (the n-gram -gather on the card and the MTP head's history-pass skip), baKon (three-rank tensor parallelism, -serving any world size from the shipped container, the request log, an uneven world's per-rank -KV and weights split, and --rope-scaling) and hazyumps (static YaRN for the Qwen rotary paths). +Written by Deadcode, with contributions from tcclaviger (the N-rank all-reduce kernels), ggz14 (the +MXFP4 GEMM, the A-tiled MXFP4 prefill GEMM and the GDN kernels' overflow guards), slydlake (the +MXFP4 decode and prefill GEMMs, the MXFP4 linear and the DFlash2 drafter's MXFP4 linears, stop +strings after the reasoning block, and the draft refill on a prefix hit), goodman-b (four-rank +tensor parallelism for Qwen3.8-Flash-Next), keptn42 (the four-rank all-reduce served in place, the +fused four-rank writes, the n-gram table in RAM, the four-class expert split, the Grafana +dashboard, the routed GEMM's decode loads and run search, the gated residual's prefill read form +and row groups, the fused router launch, the adaptive draft window, the MoE prefill forms and the +sorted operand's decode kernel), mechkbfan (the token embedding in the pinned host pool), Code000 +(the n-gram gather on the card and the MTP head's history-pass skip), baKon (three-rank tensor +parallelism, serving any world size from the shipped container, the request log, an uneven world's +per-rank KV and weights split, and --rope-scaling) and hazyumps (static YaRN for the Qwen rotary +paths). This product includes software from the following projects, each under the MIT License. The license text is in the file named beside each. diff --git a/arch/common/rad_block_dflash2.h b/arch/common/rad_block_dflash2.h index 8a99535e..8f1738f0 100644 --- a/arch/common/rad_block_dflash2.h +++ b/arch/common/rad_block_dflash2.h @@ -65,8 +65,10 @@ * The head reads the 2-BIT copy of the lm_head, exactly as the MTP head does and for exactly the * reason rad_block_mtp_fp8.h gives at op_dlogits: a draft is a proposal, the target verifies every * one of them with its own untouched head, and speculative decoding is distribution-preserving -- - * so a coarser draft costs acceptance and cannot change a token the model emits. Here it also buys - * something MTP does not need: the head stays a SINGLE-RANK computation, so the candidate ids are + * so a coarser draft costs acceptance and cannot change a token the model emits. Its candidates + * are then rescored against the target's own head where that head is fp8 or int8 (op_drerank), so + * the coarseness costs only which candidates are proposed, not how they are ranked. The 2-bit head + * also buys something MTP does not need: the head stays a SINGLE-RANK computation, so the candidate ids are * global without an all-gather and the selector's codebooks can be indexed directly. * * ============================== THE NORMS ARE PLAIN ============================== @@ -230,10 +232,11 @@ struct Dflash2 { /* model-scope weights. * - * THE SIX LINEARS ARE FP8 AND THE CHECKPOINT IS BF16. rad_fp8.h's DenseFP8 declares each as - * block fp8 and the recipe makes it so -- and the reason is bytes: the drafter is read WHOLE on - * every decode step, which makes it one of the largest single items in the decode budget - * after the trunk's own GEMMs. + * THE SIX LINEARS ARE FP8 OR MXFP4 AND THE CHECKPOINT IS BF16. rad_fp8.h's DenseFP8 declares + * each as block fp8 or OCP-MXFP4, whichever the recipe made it -- and the reason is bytes: the + * drafter is read WHOLE on every decode step, which makes it one of the largest single items in + * the decode budget after the trunk's own GEMMs. On Qwen3.8-27B at depth 7, MXFP4 rounded to + * nearest accepts ~1% fewer tokens a step than fp8 and takes ~1.5 ms off a ~34 ms step. * Nothing it computes can change an emitted token, because every draft is verified against the * trunk exactly; the only thing quantising it can move is acceptance. */ DenseFP8 w_fc{}; @@ -284,6 +287,14 @@ struct Dflash2 { /* head and selector */ rad_op op_gather = 0, op_dquant = 0, op_dlogits = 0, op_dtopk = 0; rad_op op_dgather = 0, op_dmerge = 0; /* the vocab-parallel pair; 0 when the head is whole */ + rad_op op_drerank = 0; /* the candidates rescored against the target's head */ + /* THE TARGET'S lm_head, set by the plugin before declare when its plane is one logit_rerank + * reads (fp8 or int8 rows); 0 leaves the coarse head's own scores as the candidates'. */ + rad_weight w_lm = 0; + /* How many candidates the coarse head proposes for the rerank: logit_rerank's and row_topk's + * ceiling. Rescoring is exact, so a wider set can only find more of the exact top sel_topk. */ + static constexpr int64_t kRerankR = 32; + int64_t top_w = 0; /* the coarse top-K's width: kRerankR or sel_topk */ rad_op op_hp = 0, op_sel = 0; int declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& c, int base_layer, @@ -471,8 +482,10 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co {w.q}, {w.attn}); /* ---------------------------------------------------------------- the context pass */ - /* EVERY LINEAR HERE IS `fp8a8`, and every activation that feeds one gets a `quant_act_fp8` in - * front of it. The weights are E4M3 with a 128x128 scale plane (rad_fp8.h's DenseFP8 says + /* EVERY LINEAR HERE IS `fp8a8` -- or `mxfp4a8` where the recipe made the drafter 4-bit; DenseFP8 + * reads which off the container and declares the GEMM (`gemm()`), and either one reads the + * same E4M3 pair -- and every activation that feeds one gets a `quant_act_fp8` in + * front of it. The fp8 weights are E4M3 with a 128x128 scale plane (rad_fp8.h's DenseFP8 says * why), and there is no `fp8a16` band anywhere in this block: that kernel is M <= 1, and * neither pass here ever runs at one row -- a context pass is a committed chunk and a query * pass is `block` rows by construction. @@ -489,12 +502,7 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), {w.src}, {w.srcq, w.srcs}); - op_cfc = rw(b, RAD_OP(b, "gemm_nt_q", - RAD_PARAMS(RAD_RANGE("M", 1, crows), RAD_INT("N", g.n_embd), - RAD_INT("K", cfg.n_taps * g.n_embd), - RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "fp8a8")), - RAD_WEIGHTS(w_fc.w, w_fc.s)), - {w.srcq, w.srcs}, {w.c}); + op_cfc = w_fc.gemm(b, crows, g.n_embd, cfg.n_taps * g.n_embd, {w.srcq, w.srcs}, {w.c}); /* IN PLACE. Safe for any implementation that computes a row's scale before writing the row, * which none can avoid doing; the per-head q/k norms below run in place for the same reason. */ op_cnorm = rw(b, RAD_OP(b, "rmsnorm", @@ -515,18 +523,8 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co /* TWO OPS OF THE SAME SHAPE, one per weight. An op's declared weight list is what gives a * weight its prefetch interval and its residency class, so two weights want two ops even * where one would issue both. */ - op_ck[i] = rw(b, RAD_OP(b, "gemm_nt_q", - RAD_PARAMS(RAD_RANGE("M", 1, crows), RAD_INT("N", kvd), - RAD_INT("K", g.n_embd), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", "fp8a8")), - RAD_WEIGHTS(w_k[i].w, w_k[i].s)), - {w.cq, w.cs}, {w.ck}); - op_cv[i] = rw(b, RAD_OP(b, "gemm_nt_q", - RAD_PARAMS(RAD_RANGE("M", 1, crows), RAD_INT("N", kvd), - RAD_INT("K", g.n_embd), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", "fp8a8")), - RAD_WEIGHTS(w_v[i].w, w_v[i].s)), - {w.cq, w.cs}, {w.cv}); + op_ck[i] = w_k[i].gemm(b, crows, kvd, g.n_embd, {w.cq, w.cs}, {w.ck}); + op_cv[i] = w_v[i].gemm(b, crows, kvd, g.n_embd, {w.cq, w.cs}, {w.cv}); op_cknorm[i] = rw(b, RAD_OP(b, "rmsnorm", RAD_PARAMS(RAD_RANGE("M", 1, crows * g.n_head_kv), RAD_INT("n", g.head_dim), RAD_F64("eps", g.eps), @@ -603,24 +601,9 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co RAD_INT("K", g.n_embd), RAD_STR("dtype", g.dtype)), RAD_WEIGHTS(w_acp[i])), {w.h}, {w.coef}); - op_qp[i] = rw(b, RAD_OP(b, "gemm_nt_q", - RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("N", qd), - RAD_INT("K", g.n_embd), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", "fp8a8")), - RAD_WEIGHTS(w_q[i].w, w_q[i].s)), - {w.h2q, w.h2s}, {w.q}); - op_kp[i] = rw(b, RAD_OP(b, "gemm_nt_q", - RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("N", kvd), - RAD_INT("K", g.n_embd), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", "fp8a8")), - RAD_WEIGHTS(w_k[i].w, w_k[i].s)), - {w.h2q, w.h2s}, {w.k}); - op_vp[i] = rw(b, RAD_OP(b, "gemm_nt_q", - RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("N", kvd), - RAD_INT("K", g.n_embd), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", "fp8a8")), - RAD_WEIGHTS(w_v[i].w, w_v[i].s)), - {w.h2q, w.h2s}, {w.v}); + op_qp[i] = w_q[i].gemm(b, qrows, qd, g.n_embd, {w.h2q, w.h2s}, {w.q}); + op_kp[i] = w_k[i].gemm(b, qrows, kvd, g.n_embd, {w.h2q, w.h2s}, {w.k}); + op_vp[i] = w_v[i].gemm(b, qrows, kvd, g.n_embd, {w.h2q, w.h2s}, {w.v}); op_qn[i] = rw(b, RAD_OP(b, "rmsnorm", RAD_PARAMS(RAD_RANGE("M", 1, qrows * g.n_head), RAD_INT("n", g.head_dim), RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd_qk), @@ -633,12 +616,7 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co RAD_STR("dtype", g.dtype)), RAD_WEIGHTS(w_kn[i])), {w.k}, {w.k}); - op_op[i] = rw(b, RAD_OP(b, "gemm_nt_q", - RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("N", g.n_embd), - RAD_INT("K", qd), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", "fp8a8")), - RAD_WEIGHTS(w_o[i].w, w_o[i].s)), - {w.atq, w.ats}, {w.h}); + op_op[i] = w_o[i].gemm(b, qrows, g.n_embd, qd, {w.atq, w.ats}, {w.h}); op_fnorm[i] = rw(b, RAD_OP(b, "rmsnorm", RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("n", g.n_embd), RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd), @@ -650,12 +628,7 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co RAD_INT("K", g.n_embd), RAD_STR("dtype", g.dtype)), RAD_WEIGHTS(w_fcp[i])), {w.h}, {w.coef}); - op_gu[i] = rw(b, RAD_OP(b, "gemm_nt_q", - RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("N", 2 * g.n_ff), - RAD_INT("K", g.n_embd), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", "fp8a8")), - RAD_WEIGHTS(w_gu[i].w, w_gu[i].s)), - {w.h2q, w.h2s}, {w.gate_up}); + op_gu[i] = w_gu[i].gemm(b, qrows, 2 * g.n_ff, g.n_embd, {w.h2q, w.h2s}, {w.gate_up}); /* THE SAME FOLD THE TRUNK'S MLP HAS, over the same weights and into the same activation; * rad_block_mlp_fp8.h declares it beside the pair for the same reason. Unfused, a layer * runs gate_up -> silu_mul -> quant, which is two extra launches and two extra passes over @@ -667,7 +640,7 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co * because M > 64 is a refusal in the kernel and not a slower path. */ { const int64_t hi = qrows < 64 ? qrows : 64; - if (hi > 0) { + if (hi > 0 && !w_gu[i].mx) { /* the fold is an fp8a8 tile; an MXFP4 gate_up takes the pair */ op_gug[i] = rw(b, RAD_OP(b, "gemm_nt_q_gated", RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", 2 * g.n_ff), RAD_INT("K", g.n_embd), @@ -678,12 +651,7 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co if (op_gug[i]) gated_rows = hi; } } - op_dn[i] = rw(b, RAD_OP(b, "gemm_nt_q", - RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("N", g.n_embd), - RAD_INT("K", g.n_ff), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", "fp8a8")), - RAD_WEIGHTS(w_dn[i].w, w_dn[i].s)), - {w.ffq, w.ffs}, {w.h}); + op_dn[i] = w_dn[i].gemm(b, qrows, g.n_embd, g.n_ff, {w.ffq, w.ffs}, {w.h}); } /* The convolutions. `block_size` is the QUERY BLOCK -- the mask that kills the shifted tap at @@ -789,31 +757,56 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co * the pair plane the gather moves. `vocab_off` is omitted entirely when the head is * replicated, so that graph is the one it always was -- an optional parameter present with * the value 0 is not the same declaration as an absent one. */ + const bool rerank = w_lm && (dh_shard || g.world <= 1); /* see op_drerank below */ + if (!rerank) top_w = cfg.sel_topk; op_dtopk = dh_shard ? rw(b, RAD_OP(b, "row_topk", RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("N", head_rows(g)), - RAD_INT("R", cfg.sel_topk), RAD_INT("vocab_off", g.vocab_off), + RAD_INT("R", top_w), RAD_INT("vocab_off", g.vocab_off), RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), {w.dlog}, {w.cand, w.unary, w.dpair}) : rw(b, RAD_OP(b, "row_topk", RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("N", head_rows(g)), - RAD_INT("R", cfg.sel_topk), RAD_STR("dtype", g.dtype)), + RAD_INT("R", top_w), RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), {w.dlog}, {w.cand, w.unary}); + /* THE CANDIDATES ARE RESCORED EXACTLY when the target's head can be read row by row. The + * coarse head proposes its top kRerankR, `logit_rerank` scores those against the target's own + * fp8 head and re-sorts them, and the selector reads the best sel_topk of that order -- the + * candidate set and the unary logits the drafter was trained against, at kRerankR rows of the + * weight a draft row instead of a second, finer copy of the head. On Qwen3.8-27B the 2-bit + * head's own top-1 matches the exact head's on 76% of draft rows and the rescored one on all of + * them; served at depth 7 that is the 4-bit head's speed at half its bytes (330 MiB). + * + * Sharded, each rank rescores only ids it owns (row_topk reports this rank's slice), and the + * gather and merge below then rank exact values. A replicated draft head above one rank + * proposes ids another rank owns, so it keeps its own scores. */ + if (rerank) { + op_drerank = RAD_OP(b, "logit_rerank", + RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("R", top_w), + RAD_INT("n_vocab", head_rows(g)), RAD_INT("n_embd", g.n_embd), + RAD_INT("vocab_off", g.vocab_off), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_lm)); + if (!op_drerank) return RAD_E_INVAL; + rad_buf rd[2] = { w.sel_h, w.cand }; + rad_buf wr[3] = { w.cand, w.unary, w.dpair }; + rad_op_reads (b, op_drerank, rd, 2); + rad_op_writes(b, op_drerank, wr, dh_shard ? 3 : 2); + } if (dh_shard) { /* ROW-INTERLEAVED, so one draft position's pairs from every rank land side by side -- * `row_topk_merge` reads one row at one row stride, the same reason the sampler's own * candidate gather carries `row`. */ op_dgather = rw(b, RAD_OP(b, "all_gather", RAD_PARAMS(RAD_INT("world_size", g.world), - RAD_INT("numel", srows * cfg.sel_topk * 2), + RAD_INT("numel", srows * top_w * 2), RAD_STR("dtype", "f32"), - RAD_INT("row", cfg.sel_topk * 2)), + RAD_INT("row", top_w * 2)), RAD_NOWEIGHTS), {w.dpair}, {w.dgath}); op_dmerge = rw(b, RAD_OP(b, "row_topk_merge", - RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("R", cfg.sel_topk), + RAD_PARAMS(RAD_RANGE("M", 1, srows), RAD_INT("R", top_w), RAD_INT("world_size", g.world), RAD_STR("dtype", g.dtype)), RAD_NOWEIGHTS), @@ -851,16 +844,16 @@ inline void Dflash2::context(RadCtx* c, const RadBatch* batch) const { * rows. */ RAD_ISSUE_N(c, op_qsrc, T, brows(w.src, T), brows(w.srcq, T), brows(w.srcs, T)); RAD_ISSUE_N(c, op_cfc, T, brows(w.srcq, T), brows(w.srcs, T), RAD_W(w_fc.w), RAD_W(w_fc.s), - brows(w.c, T), RAD_NONE, RAD_NONE); + brows(w.c, T), RAD_NONE, w_fc.bref()); RAD_ISSUE_N(c, op_cnorm, T, brows(w.c, T), RAD_W(w_hnorm), brows(w.c, T)); RAD_ISSUE_N(c, op_qc, T, brows(w.c, T), brows(w.cq, T), brows(w.cs, T)); for (int64_t l = 0; l < g.n_layer; ++l) { const int i = (int)l; RAD_ISSUE_N(c, op_ck[i], T, brows(w.cq, T), brows(w.cs, T), - RAD_W(w_k[i].w), RAD_W(w_k[i].s), brows(w.ck, T), RAD_NONE, RAD_NONE); + RAD_W(w_k[i].w), RAD_W(w_k[i].s), brows(w.ck, T), RAD_NONE, w_k[i].bref()); RAD_ISSUE_N(c, op_cv[i], T, brows(w.cq, T), brows(w.cs, T), - RAD_W(w_v[i].w), RAD_W(w_v[i].s), brows(w.cv, T), RAD_NONE, RAD_NONE); + RAD_W(w_v[i].w), RAD_W(w_v[i].s), brows(w.cv, T), RAD_NONE, w_v[i].bref()); RAD_ISSUE_N(c, op_cknorm[i], T * g.n_head_kv, brows(w.ck, T), RAD_W(w_kn[i]), brows(w.ck, T)); RAD_ISSUE_N(c, op_crope, T, brows(w.ck, T), praw(batch->positions, RAD_I32, T)); @@ -898,11 +891,11 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { RAD_ISSUE_N(c, op_qh2, T, brows(w.h2, T), brows(w.h2q, T), brows(w.h2s, T)); RAD_ISSUE_N(c, op_qp[i], T, brows(w.h2q, T), brows(w.h2s, T), - RAD_W(w_q[i].w), RAD_W(w_q[i].s), brows(w.q, T), RAD_NONE, RAD_NONE); + RAD_W(w_q[i].w), RAD_W(w_q[i].s), brows(w.q, T), RAD_NONE, w_q[i].bref()); RAD_ISSUE_N(c, op_kp[i], T, brows(w.h2q, T), brows(w.h2s, T), - RAD_W(w_k[i].w), RAD_W(w_k[i].s), brows(w.k, T), RAD_NONE, RAD_NONE); + RAD_W(w_k[i].w), RAD_W(w_k[i].s), brows(w.k, T), RAD_NONE, w_k[i].bref()); RAD_ISSUE_N(c, op_vp[i], T, brows(w.h2q, T), brows(w.h2s, T), - RAD_W(w_v[i].w), RAD_W(w_v[i].s), brows(w.v, T), RAD_NONE, RAD_NONE); + RAD_W(w_v[i].w), RAD_W(w_v[i].s), brows(w.v, T), RAD_NONE, w_v[i].bref()); RAD_ISSUE_N(c, op_qn[i], T * g.n_head, brows(w.q, T), RAD_W(w_qn[i]), brows(w.q, T)); RAD_ISSUE_N(c, op_kn[i], T * g.n_head_kv, brows(w.k, T), RAD_W(w_kn[i]), brows(w.k, T)); @@ -919,7 +912,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { brows(w.attn, T)); RAD_ISSUE_N(c, op_qat, T, brows(w.attn, T), brows(w.atq, T), brows(w.ats, T)); RAD_ISSUE_N(c, op_op[i], T, brows(w.atq, T), brows(w.ats, T), - RAD_W(w_o[i].w), RAD_W(w_o[i].s), brows(w.h, T), RAD_NONE, RAD_NONE); + RAD_W(w_o[i].w), RAD_W(w_o[i].s), brows(w.h, T), RAD_NONE, w_o[i].bref()); /* BEFORE the closing convolution, not after. The convolution is linear in its input and * its coefficients come from the replicated pre-attention norm, so either order is the * same arithmetic -- and reducing first keeps the residual stream's contract ("what the @@ -941,13 +934,12 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { RAD_NONE); } else { RAD_ISSUE_N(c, op_gu[i], T, brows(w.h2q, T), brows(w.h2s, T), - RAD_W(w_gu[i].w), RAD_W(w_gu[i].s), brows(w.gate_up, T), RAD_NONE, - RAD_NONE); + RAD_W(w_gu[i].w), RAD_W(w_gu[i].s), brows(w.gate_up, T), RAD_NONE, w_gu[i].bref()); RAD_ISSUE_N(c, op_act, T, brows(w.gate_up, T), brows(w.ffn, T)); RAD_ISSUE_N(c, op_qff, T, brows(w.ffn, T), brows(w.ffq, T), brows(w.ffs, T)); } RAD_ISSUE_N(c, op_dn[i], T, brows(w.ffq, T), brows(w.ffs, T), - RAD_W(w_dn[i].w), RAD_W(w_dn[i].s), brows(w.h, T), RAD_NONE, RAD_NONE); + RAD_W(w_dn[i].w), RAD_W(w_dn[i].s), brows(w.h, T), RAD_NONE, w_dn[i].bref()); if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE); RAD_ISSUE_N(c, op_fconv[i], T, brows(w.h, T), bcol(w.coef, half, half, T), wrow_slice(w_fcb[i], 1, 1, cfg.taps * g.n_embd), brows(w.h2, T)); @@ -980,15 +972,22 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { if (op_dmerge) { RAD_ISSUE_N(c, op_dtopk, S, brows(w.dlog, S), brows(w.cand, S), brows(w.unary, S), brows(w.dpair, S)); + if (op_drerank) + RAD_ISSUE_N(c, op_drerank, S, brows(w.sel_h, S), RAD_W(w_lm), brows(w.cand, S), + brows(w.cand, S), brows(w.unary, S), brows(w.dpair, S)); RAD_ISSUE(c, op_dgather, brows(w.dpair, S), brows(w.dgath, S)); RAD_ISSUE_N(c, op_dmerge, S, brows(w.dgath, S), brows(w.cand, S), brows(w.unary, S)); } else { RAD_ISSUE_N(c, op_dtopk, S, brows(w.dlog, S), brows(w.cand, S), brows(w.unary, S), RAD_NONE); + if (op_drerank) + RAD_ISSUE_N(c, op_drerank, S, brows(w.sel_h, S), RAD_W(w_lm), brows(w.cand, S), + brows(w.cand, S), brows(w.unary, S), RAD_NONE); } RAD_ISSUE_N(c, op_hp, S, brows(w.sel_h, S), RAD_W(w_selp), brows(w.hp, S)); - RAD_ISSUE_N(c, op_sel, batch->n_seq, brows(w.cand, S), brows(w.unary, S), brows(w.hp, S), + RAD_ISSUE_N(c, 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)); } diff --git a/arch/common/rad_fp8.h b/arch/common/rad_fp8.h index 8b2fc9ac..e0212f47 100644 --- a/arch/common/rad_fp8.h +++ b/arch/common/rad_fp8.h @@ -1127,11 +1127,35 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in */ struct DenseFP8 { rad_weight w = 0, s = 0; + /* OR AN OCP-MXFP4 WEIGHT, when that is what the container holds for it (a recipe's `codes= + * fp4_e2m1 group=32 scale=e8m0`): e2m1 codes, the E8M0 plane, and that plane again as the + * per-row reference -- LinearFP8's mx branch, without the GEMM. A block that declares its own + * GEMM over a DenseFP8 asks `gemm()` for it and passes `bref()` as the op's last operand, so + * one block serves either format against the same E4M3 activation pair. */ + rad_weight ref = 0; + bool mx = false; int declare(RadBuilder* b, Names& nm, const char* base, int64_t N, int64_t K, int access, int shard, RadWeightGroup grp, std::initializer_list src, std::initializer_list row_parts = {}); + /* `gemm_nt_q` over this weight, `fp8a8` or `mxfp4a8` as it was declared, ranged M 1..hi. */ + rad_op gemm(RadBuilder* b, int64_t hi, int64_t N, int64_t K, + std::initializer_list reads, std::initializer_list writes) const { + if (mx) + return rw(b, RAD_OP(b, "gemm_nt_q", + RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K), + RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8")), + RAD_WEIGHTS(w, s, ref)), + reads, writes); + return rw(b, RAD_OP(b, "gemm_nt_q", + RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K), + RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "fp8a8")), + RAD_WEIGHTS(w, s)), + reads, writes); + } + /* The op's trailing `b_ref` operand: the reference view for MXFP4, absent for fp8. */ + RadOperand bref() const { return mx ? RAD_W(ref) : RAD_NONE; } }; inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t N, int64_t K, @@ -1139,6 +1163,36 @@ inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t std::initializer_list src, std::initializer_list row_parts) { if (N <= 0 || K <= 0 || src.size() == 0) return RAD_E_INVAL; + + /* ---- OCP-MXFP4, read off the encoding the name map resolves to. The exponent grid is one row + * tall, so a part needs only whole 16-row tiles; K takes whole 128s because the E4M3 activation + * in front carries a scale per (row, 128). */ + { + const char* wn = nm.f("%s.weight", base); + RAD_ARCH_TRY(map_linear(b, wn, src)); + RadEncoding enc{}; + mx = weight_enc(b, wn, &enc) && linear_is_mxfp4(enc); + if (mx) { + if (N % 16 || K % RAD_FP8_BLOCK) { + fprintf(stderr, "radiance: '%s' is %lldx%lld per rank; an MXFP4 weight takes rows " + "in 16s and K in whole %d-column activation scale blocks.\n", + base, (long long)N, (long long)K, RAD_FP8_BLOCK); + return RAD_E_INVAL; + } + int64_t p[RAD_MAX_ROW_PARTS] = {0}; + int np = 0; + for (int64_t q : row_parts) { + if (np >= RAD_MAX_ROW_PARTS || q % 16) return RAD_E_INVAL; + p[np++] = q; + } + w = decl_view(b, wn, nullptr, "codes", RAD_FP4E2M1, { N, K }, access, shard, grp, p, np); + s = decl_view(b, nm.f("%s.scale", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP }, + access, shard, grp, p, np); + ref = decl_view(b, nm.f("%s.ref", base), wn, "scale", RAD_E8M0, { N, K / RAD_MX_GROUP }, + access, shard, grp, p, np); + return w && s && ref ? RAD_OK : RAD_E_INVAL; + } + } if (N % RAD_FP8_BLOCK || K % RAD_FP8_BLOCK) { fprintf(stderr, "radiance: '%s' is %lldx%lld per rank, which is not a whole number of " "%dx%d scale blocks. A block-scaled fp8 weight cannot be split inside a " @@ -1163,8 +1217,7 @@ inline int DenseFP8::declare(RadBuilder* b, Names& nm, const char* base, int64_t } const char* wn = nm.f("%s.weight", base); - const char* sn = nm.f("%s.scale", base); - RAD_ARCH_TRY(map_linear(b, wn, src)); + const char* sn = nm.f("%s.scale", base); /* the name map was made above */ w = decl_view(b, wn, nullptr, "codes", RAD_F8E4M3, { N, K }, access, shard, grp, wp, np); s = decl_view(b, sn, wn, "scale", RAD_BF16, { fp8_blocks(N), fp8_blocks(K) }, access, shard, grp, sp, np); diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index f90c7439..54b0aed4 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -493,13 +493,24 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui * vocab-sharded, asked here rather than re-derived, because a buffer that disagreed with the * op declared over it is a read past its end. */ dw.dlog = decl_b(b, nm.f("df_dlog"), dg.act_dtype, {sr, Dflash2::head_rows(dg)}); - dw.cand = decl_b(b, nm.f("df_cand"), RAD_I32, {sr, dc.sel_topk}); - dw.unary = decl_b(b, nm.f("df_unary"), RAD_F32, {sr, dc.sel_topk}); + /* THE CANDIDATES ARE RESCORED against the target's head when logit_rerank can read its plane + * (fp8 or int8 rows) and every candidate a rank proposes is one it owns -- the rule the block + * applies; the planes are then kRerankR wide and the selector reads the first sel_topk. */ + { + const uint32_t hd = weight_codes_dtype(b, nm.f("output.weight"), RAD_BF16); + if ((hd == RAD_F8E4M3 || hd == RAD_I8) && (Dflash2::head_sharded(dg) || dg.world <= 1)) { + m.df.w_lm = m.w_lm_head; + m.df.top_w = Dflash2::kRerankR; + } + } + const int64_t tw = m.df.w_lm ? m.df.top_w : dc.sel_topk; + dw.cand = decl_b(b, nm.f("df_cand"), RAD_I32, {sr, tw}); + dw.unary = decl_b(b, nm.f("df_unary"), RAD_F32, {sr, tw}); /* The vocab-parallel candidate exchange, and only when there is one: this rank's top-R as * (int32 id, float value) pairs and the all-gathered world_size * R of them. */ if (Dflash2::head_sharded(dg)) { - dw.dpair = decl_b(b, nm.f("df_dpair"), RAD_F32, {sr, dc.sel_topk, 2}); - dw.dgath = decl_b(b, nm.f("df_dgath"), RAD_F32, {sr, (int64_t)dg.world * dc.sel_topk, 2}); + 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}); } /* NAMED, because the engine reads the draft out of it by name: a DFlash2 pass proposes through * the selector and never through the sampler's token buffer. */ diff --git a/data/recipes/q38-27b-fp8-df2.recipe b/data/recipes/q38-27b-fp8-df2.recipe index 90381541..01181654 100644 --- a/data/recipes/q38-27b-fp8-df2.recipe +++ b/data/recipes/q38-27b-fp8-df2.recipe @@ -9,20 +9,21 @@ # What the recipe adds: # # - the lm_head, block fp8: the widest GEMM in the model at half the bytes -# - the drafter's linears, block fp8 from its bf16 release: it is read whole on every decode +# - the drafter's linears, OCP-MXFP4 from its bf16 release: it is read whole on every decode # step, and a draft is verified, so its precision can move acceptance and never an emitted token # - the drafter's 2-bit copy of the target's head (u2 codes, an f16 scale and a u8 zero a group -# of 128), read by its draft rounds only +# of 128, least-squares clip search), read by its draft rounds only; the engine rescores its +# top 32 against the target's fp8 head # # The drafter's selector projection, codebooks and convolution weights stay bf16. output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.draft_head.weight rtn codes=u2 zero=u8 group=128 scale=f16 +dflash.draft_head.weight rtn codes=u2 zero=u8 group=128 scale=f16 rule=search -dflash.fc.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.attn_q.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.attn_k.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.attn_v.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.attn_output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.ffn_gate_up.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.ffn_down.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.fc.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_q.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_k.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_v.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_output.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.ffn_gate_up.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.ffn_down.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 diff --git a/data/recipes/q38-27b-quark-mxfp4-df2.recipe b/data/recipes/q38-27b-quark-mxfp4-df2.recipe index d6b94631..75f35177 100644 --- a/data/recipes/q38-27b-quark-mxfp4-df2.recipe +++ b/data/recipes/q38-27b-quark-mxfp4-df2.recipe @@ -7,18 +7,24 @@ # the trunk is served at the 4-bit weights Quark shipped, against E4M3 activations. The checkpoint # is served by the family's quantised plugin (qwen35_fp8). The delta net's a/b projection is # declared bf16, and every MXFP4 value is exact in bf16, so `cast` keeps Quark's values that way. -# The rest is as in q38-27b-fp8-df2.recipe: the head at block fp8, the drafter's linears at block -# fp8, and its 2-bit copy of the target's head. +# The target's head goes block fp8, as in q38-27b-fp8-df2.recipe. +# +# THE DRAFTER is made as small as its acceptance allows, since it is read whole on every decode step +# and a draft is verified, so its precision moves acceptance and never an emitted token: its linears +# at OCP-MXFP4 like the trunk, and a 2-bit copy of the target's head (u2 with a u8 zero, least-squares +# clip search) whose top 32 the engine rescores against the target's fp8 head. Against fp8 linears +# and an unsearched 2-bit head without the rescore, TP1 at depth 7: 108.9 -> 121.6 tok/s one +# stream, 0.75 GiB less weight. blk.*.ssm_ab.weight cast dtype=bf16 output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.draft_head.weight rtn codes=u2 zero=u8 group=128 scale=f16 +dflash.draft_head.weight rtn codes=u2 zero=u8 group=128 scale=f16 rule=search -dflash.fc.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.attn_q.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.attn_k.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.attn_v.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.attn_output.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.ffn_gate_up.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 -dflash.blk.*.ffn_down.weight rtn codes=fp8_e4m3 block=128x128 scale=bf16 +dflash.fc.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_q.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_k.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_v.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.attn_output.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.ffn_gate_up.weight rtn codes=fp4_e2m1 group=32 scale=e8m0 +dflash.blk.*.ffn_down.weight rtn codes=fp4_e2m1 group=32 scale=e8m0