radiance 1.12.0

New models:
- Fish Audio drama-3: speech with speaker tags and inline direction, voices cloned from reference
  clips (the `references` field of /v1/audio/speech and the streaming session), two cards
- Gemma 4 E2B/E4B (KV-shared layers, per-layer embeddings, clipped vision linears), with
  NVIDIA-Nemotron-3.5-Bolt-E4B in bf16 with DFlash and vision on one card, and E4B recipes

Speed:
- MXFP4 GEMMs for one to 64 rows and a tiled prefill GEMM at TN=4 from 1024 rows: the 27B MXFP4
  with DFlash2 decodes about 1% faster at c1, 5% at c4 and 14% at c16
- The fp8 and int8 LM heads read their weight once for 65 to 128 rows
- A prefill step of one sequence runs in two pieces on a second lane at two ranks (qwen35_fp8,
  gemma3): a 45K-token prompt on the 27B fp8 in 15.1 s instead of 16.2 s
- Gemma 3's fused all-reduce junctions also under a lossy wire
- --draft-short-from N: a block drafter runs its shorter block from N decoding sequences on
  (off by default; with 8 on the 27B MXFP4: c8 +12%, c16 +18%, c64 +32%, c1 unchanged)
- --tp-attn-split rows: GLM-5.3-Flash's MLA attention split by rows over the ranks, 18 to 21%
  faster prefill; lossy, `heads` stays the default
- --draft-coupling on: Qwen3.8-Flash-Next's MTP drafter samples its proposals from its reranked
  top-8 and the target's draw is coupled to it; pays at high temperatures (c1 at T 1.0: 155 ->
  178 tok/s), costs at low ones; off by default

Features:
- Edited prompts on windowed models (Gemma 4) reuse their prefix: periodic window snapshots, and
  snapshot-backed prefix KV kept across recomputes (an edited tail on Gemma 4 31B: 96 s -> 2.6 s)
- DELETE /sessions/<label> and DELETE /sessions?id=<id> forget one stored conversation
- --state-cache-dtype f16: the delta-net state cache at half the memory (qwen35 plugins; lossy)

Fixes:
- A browser's CORS preflight allows DELETE

Changes: Plugin ABI 27: out-of-tree plugins must be rebuilt. The environment switches the PRs
added are flags now (--tp-attn-split, --draft-coupling, --state-cache-dtype).
This commit is contained in:
137 files changed
+12959 -1126

No files matched your search

+55 -5
View File
@@ -130,6 +130,11 @@ struct Dflash2 {
/* Bytes of the next GEMM's weight read into the cache beside each all-reduce of the query
* 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. */
int short_block = 0;
int64_t steps() const { return block - 1; }
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);
@@ -260,6 +265,10 @@ struct Dflash2 {
rad_op op_gu[MAX_LAYERS] = {}, op_dn[MAX_LAYERS] = {};
rad_op op_gug[MAX_LAYERS] = {}; /* the gate_up|silu|quant fold; 0 when nothing serves it */
rad_op op_aconv[MAX_LAYERS] = {}, op_fconv[MAX_LAYERS] = {};
/* The short block's (block_lo rows a sequence): the convolutions mask at the block's first row
* 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;
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
@@ -287,6 +296,9 @@ struct Dflash2 {
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;
/* The short block: kShortBlock rows (depth 3, the window that pays at many sequences), when
* the drafter's own block is longer (rad_drafter_short_block). */
static constexpr int64_t kShortBlock = 4;
int declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& c, int base_layer,
rad_kvgroup group, rad_weight tok_embd, const char* ck_prefix,
@@ -695,7 +707,26 @@ 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;
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",
RAD_PARAMS(RAD_RANGE("T", 1, qrows), RAD_INT("hidden_size", g.n_embd),
RAD_INT("taps", cfg.taps), RAD_INT("group_size", cfg.group),
RAD_INT("NG", NG), RAD_INT("block_size", block_lo),
RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_acb[i])),
{w.h, w.coef}, {w.h2});
op_fconv_lo[i] = rw(b, RAD_OP(b, "dflash_conv",
RAD_PARAMS(RAD_RANGE("T", 1, qrows), RAD_INT("hidden_size", g.n_embd),
RAD_INT("taps", cfg.taps), RAD_INT("group_size", cfg.group),
RAD_INT("NG", NG), RAD_INT("block_size", block_lo),
RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_fcb[i])),
{w.h, w.coef}, {w.h2});
}
if (rad_op_reads(b, op_fconv[g.n_layer - 1], &w.attn, 1) < 0) return RAD_E_INVAL;
if (block_lo && rad_op_reads(b, op_fconv_lo[g.n_layer - 1], &w.attn, 1) < 0) return RAD_E_INVAL;
op_qrope = rope(qrows, g.n_head, w.q);
op_krope = rope(qrows, g.n_head_kv, w.k);
op_store = rw(b, RAD_OP(b, "kv_store",
@@ -909,6 +940,14 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co
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)
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),
RAD_INT("n_vocab", g.n_vocab_all),
RAD_INT("anchor_stride", block_lo), RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w_pred, w_succ)),
{w.cand, w.unary, w.hp}, {w.tokens});
/* The reads beside the all-reduces, after every weight they read is declared. */
if (op_ar && cfg.prefetch > 0)
@@ -983,6 +1022,17 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
const int64_t NG = cfg.ng(g.n_embd);
const int64_t half = cfg.taps * NG; /* prepare's coefficients, then finish's */
/* The block this pass was staged at: every drafting sequence carries the same number of rows,
* the drafter's own block or its short one (rad_drafter_short_block). */
const int64_t blk = batch->n_seq > 0 && T % batch->n_seq == 0 ? T / batch->n_seq : 0;
const bool lo = block_lo > 0 && blk == block_lo;
if (!lo && blk != cfg.block) {
rad_step_fail(c, "dflash2: a query pass staged at a block this drafter has no ops for");
return;
}
const rad_op* aconv = lo ? op_aconv_lo : op_aconv;
const rad_op* fconv = lo ? op_fconv_lo : op_fconv;
RAD_ISSUE_N(c, op_embed, T, praw(batch->token_ids, RAD_I32, T), RAD_W(w_tok), brows(w.x, T));
if (op_embed_ar)
RAD_ISSUE_N(c, op_embed_ar, (int64_t)T * g.n_embd, brows(w.x, T), RAD_NONE);
@@ -1005,7 +1055,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
/* ---- attention, wrapped in the two halves of one convolution */
norm(op_anorm[i], op_aan[i], w_anorm[i]);
RAD_ISSUE_N(c, op_acoef[i], T, brows(w.h, T), RAD_W(w_acp[i]), brows(w.coef, T));
RAD_ISSUE_N(c, op_aconv[i], T, brows(w.h, T), bcol(w.coef, 0, half, T),
RAD_ISSUE_N(c, aconv[i], T, brows(w.h, T), bcol(w.coef, 0, half, T),
wrow_slice(w_acb[i], 0, 1, cfg.taps * g.n_embd), brows(w.h2, T));
RAD_ISSUE_N(c, op_qh2, T, brows(w.h2, T), brows(w.h2q, T), brows(w.h2s, T));
@@ -1044,14 +1094,14 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
* replicated norm, so either order gives the same result; reducing first keeps x's
* update agreed across ranks. */
if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE);
RAD_ISSUE_N(c, op_aconv[i], T, brows(w.h, T), bcol(w.coef, half, half, T),
RAD_ISSUE_N(c, aconv[i], T, brows(w.h, T), bcol(w.coef, half, half, T),
wrow_slice(w_acb[i], 1, 1, cfg.taps * g.n_embd), brows(w.h2, T));
add_pending = true;
/* ---- the feed-forward, wrapped in the other convolution */
norm(op_fnorm[i], op_afn[i], w_fnorm[i]);
RAD_ISSUE_N(c, op_fcoef[i], T, brows(w.h, T), RAD_W(w_fcp[i]), brows(w.coef, T));
RAD_ISSUE_N(c, op_fconv[i], T, brows(w.h, T), bcol(w.coef, 0, half, T),
RAD_ISSUE_N(c, fconv[i], T, brows(w.h, T), bcol(w.coef, 0, half, T),
wrow_slice(w_fcb[i], 0, 1, cfg.taps * g.n_embd), brows(w.h2, T));
RAD_ISSUE_N(c, op_qh2, T, brows(w.h2, T), brows(w.h2q, T), brows(w.h2s, T));
if (op_gug[i] && T <= gated_rows) {
@@ -1068,7 +1118,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const {
RAD_W(w_dn[i].w), RAD_W(w_dn[i].s), brows(w.h, T), RAD_NONE, w_dn[i].bref());
for (int j = 0; j < 2; ++j) prefetch_beside(c, op_pff[i][j], pff_w[i][j], T);
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),
RAD_ISSUE_N(c, 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));
add_pending = true;
}
@@ -1107,7 +1157,7 @@ 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, op_sel, batch->n_seq, bcol(w.cand, 0, cfg.sel_topk, 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));
+119 -5
View File
@@ -3,14 +3,23 @@
* complete heads shard as rows of the shared K/V weight. prepare() exposes the normalized q latent
* for the indexer; finish() consumes the indexer's selection. The caller owns the mHC residual.
*
* Under tensor parallelism the 64 heads split over the ranks (GlmTp::heads): the rows of q_b and
* kv_b, the columns of o, an all_reduce of the output; q_a, kv_a and the latent cache are
* replicated. */
* Under tensor parallelism the head form splits the 64 heads over the ranks (GlmTp::heads): the
* rows of q_b and kv_b, the columns of o, an all_reduce of the output; q_a, kv_a and the latent
* cache are replicated. From GlmTp::kSplitRows a prefill-sized step takes the row form instead
* (Wire::rows, Form::rows): each rank runs its run of the step's rows (GlmTp::run_of) through q_b,
* absorption, attention, expansion and the output projection with all 64 heads, then an
* all_gather lays the runs out as the whole step. The latent cache and the selection are whole on
* every rank already, so the split needs no other exchange. The gather moves half the bytes of
* the head form's all-reduce, and the attention loads each key once for all 64 heads of a row.
* The price is the three projections at full width on every rank (126 MB a layer); RowsWire::q
* unset declares neither them nor the form. */
#ifndef RAD_BLOCK_GLM_MLA_H
#define RAD_BLOCK_GLM_MLA_H
#include "rad_fp8.h"
#include "rad_glm_tp.h"
#include <algorithm>
#include <cmath>
#include <vector>
namespace rad {
namespace arch {
@@ -88,11 +97,28 @@ struct MlaLatent {
};
struct GlmMla {
/* How a prefill-sized step under tensor parallelism runs: `heads` keeps the head form at every
* step size and spares the full-width weights' VRAM; `rows` is the row form. The plugin
* chooses (RadBuildCtx::tp_attn_rows); the block takes the choice through whether it is given
* RowsWire buffers. */
enum class Form { heads, rows };
/* The row form's buffers, `rows_for(max_tok)` rows each. `out` (Wire::out) must hold
* world * rows_for(T) rows, since every rank's run is gathered at full length. */
struct RowsWire {
rad_buf q = 0, q_latent = 0, attn_latent = 0, out = 0;
ActFP8 attn{};
};
struct Wire {
ActFP8 input{}, q_low{}, attn{};
rad_buf q_raw = 0, kv_raw = 0, latent = 0, out = 0;
MlaLatent::Wire core{};
RowsWire rows{};
};
/* The gathered row of the row form: 4096 bf16, so a run is ceil(T / world) rows. */
static constexpr int64_t kOutRowBytes = 4096 * 2;
static int64_t rows_for(int64_t T, int world) {
return GlmTp::run_rows(T, world, kOutRowBytes);
}
Geom g{};
Wire w{};
int layer = 0;
@@ -103,6 +129,10 @@ struct GlmMla {
MlaLatent core{};
rad_weight q_gain = 0, kv_gain = 0;
rad_op op_kv_norm = 0, op_store = 0, op_ar = 0;
LinearFP8 rows_q_b{}, rows_output{};
QuantFP8 rows_quant{};
rad_weight rows_kv_b = 0;
rad_op op_rows_absorb = 0, op_rows_attn = 0, op_rows_expand = 0, op_rows_gather = 0;
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Wire& wire,
const char* prefix, rad_kvgroup group, bool reduce = true,
bool defer_finish = false) {
@@ -160,7 +190,69 @@ struct GlmMla {
GlmTp::mla_width(g.world), RAD_SHARD_COL, grp_layer(layer),
{ nm.ckpt("%s.o_proj.weight", prefix) }, w.attn, w.out));
if (reduce) op_ar = GlmTp::reduce(b, g, w.out);
return op_store && (!(reduce && g.world > 1) || op_ar) ? RAD_OK : RAD_E_INVAL;
if (!op_store || ((reduce && g.world > 1) && !op_ar)) return RAD_E_INVAL;
return reduce ? declare_rows(b, nm, prefix) : RAD_OK;
}
/* Declared whatever the step size, as every TP split here is: arena levels declare again at a
* smaller max_tok and must get the same graph. The gather's message bound is the all-reduce's,
* so both share one exchange instance and its scratch. */
int declare_rows(RadBuilder* b, Names& nm, const char* prefix) {
const auto& r = w.rows;
if (!r.q || g.world < 2) return RAD_OK;
if (!r.q_latent || !r.attn_latent || !r.out || !r.attn.x || r.attn.x == r.q) return RAD_E_INVAL;
const int64_t R = rows_for(g.max_tok, g.world);
const auto wg = grp_layer(layer);
/* Whole views of the head form's container entries: one stored weight each, whose
* encoding (block fp8 at f32 scales, the decoded bf16 kv_b) the views inherit. */
rows_q_b.stored = nm.f("blk.%d.attn_q_b.weight", layer);
RAD_ARCH_TRY(rows_q_b.declare(b, nm, g, nm.f("blk.%d.attn_q_b_rows", layer), 16384, 1536,
RAD_SHARD_NONE, wg, { nm.ckpt("%s.q_b_proj.weight", prefix) },
w.q_low, r.q, R));
rows_kv_b = decl_view(b, nm.f("blk.%d.attn_kv_b_rows.weight", layer),
nm.f("blk.%d.attn_kv_b.weight", layer), nullptr, RAD_BF16,
{ 64 * 2 * 256, 512 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, wg);
if (!rows_kv_b) return RAD_E_INVAL;
auto project = [&](const char* mode, rad_buf in, rad_buf out) {
return rw(b,
RAD_OP(b, "mla_project",
RAD_PARAMS(RAD_RANGE("M", 1, R), RAD_INT("heads", 64),
RAD_INT("head_dim", 256), RAD_INT("latent_dim", 512),
RAD_STR("mode", mode), RAD_STR("dtype", "bf16")),
RAD_WEIGHTS(rows_kv_b)),
{ in }, { out });
};
op_rows_absorb = project("absorb", r.q, r.q_latent);
op_rows_attn =
rw(b,
RAD_OP(b, "mla_attn",
RAD_PARAMS(RAD_RANGE("M", 1, R), RAD_INT("latent_dim", 512),
RAD_INT("heads", 64), RAD_INT("blocks", core.c.blocks),
RAD_INT("block_size", 4), RAD_F64("scale", 1.0 / std::sqrt(256.0)),
RAD_STR("dtype", "bf16")),
RAD_NOWEIGHTS),
{ r.q_latent, w.core.selected, w.core.length }, { r.attn_latent });
op_rows_expand = project("expand", r.attn_latent, r.attn.x);
RAD_ARCH_TRY(rows_quant.declare(b, g, r.attn, 16384));
rows_output.stored = nm.f("blk.%d.attn_output.weight", layer);
RAD_ARCH_TRY(rows_output.declare(b, nm, g, nm.f("blk.%d.attn_output_rows", layer), 4096,
16384, RAD_SHARD_NONE, wg,
{ nm.ckpt("%s.o_proj.weight", prefix) }, r.attn, r.out,
R));
op_rows_gather = rw(b,
RAD_OP(b, "all_gather",
RAD_PARAMS(RAD_INT("world_size", g.world),
RAD_RANGE("numel", 4096, g.max_tok * 4096),
RAD_STR("dtype", "bf16")),
RAD_NOWEIGHTS),
{ r.out }, { w.out });
return op_rows_absorb && op_rows_attn && op_rows_expand && op_rows_gather ? RAD_OK
: RAD_E_INVAL;
}
/* From the TP split threshold, and while the last rank's run is not empty (always at that
* threshold for up to 8 ranks); the same answer on every rank, as the gather needs. */
bool rows_take(int64_t T) const {
return op_rows_gather && GlmTp::splits(T, g.world) &&
(g.world - 1) * rows_for(T, g.world) < T;
}
void prepare(RadCtx* ctx, const RadBatch* batch) const {
const auto* kb = kv_batch(batch, kv);
@@ -173,17 +265,39 @@ struct GlmMla {
kv_a.step(ctx, w.input, w.kv_raw, T);
q_norm.step(ctx, T);
RAD_ISSUE_N(ctx, op_kv_norm, T, brows(w.kv_raw, T), RAD_W(kv_gain), brows(w.latent, T));
q_b.step(ctx, w.q_low, w.core.q, T);
if (!rows_take(T)) q_b.step(ctx, w.q_low, w.core.q, T);
RAD_ISSUE_N(ctx, op_store, T, bcol(w.latent, 0, 256, T), bcol(w.latent, 256, 256, T),
praw(kb->slot_mapping, RAD_I32, T), kv_cache(kv, layer));
}
void finish(RadCtx* ctx, const RadBatch* batch) const {
const int64_t T = batch->n_tok;
if (rows_take(T)) {
finish_rows(ctx, T);
return;
}
core.step(ctx, T);
attn_quant.step(ctx, T);
output.step(ctx, w.attn, w.out, T);
if (op_ar) RAD_ISSUE_N(ctx, op_ar, T * 4096, brows(w.out, T), RAD_NONE);
}
/* This rank's rows [first, first + n) end to end; the last rank's run may be short, and the
* gather sends its unwritten tail rows, which land past T. */
void finish_rows(RadCtx* ctx, int64_t T) const {
const auto& r = w.rows;
const GlmTp::Run run = GlmTp::run_of(T, g.world, g.rank, kOutRowBytes);
const int64_t first = run.first, n = run.rows;
rows_q_b.step(ctx, w.q_low, r.q, T, first, n, 0);
RAD_ISSUE_N(ctx, op_rows_absorb, n, brows(r.q, n), RAD_W(rows_kv_b), brows(r.q_latent, n));
RAD_ISSUE_N(ctx, op_rows_attn, n, brows(r.q_latent, n), kv_cache(kv, layer),
brow_slice(w.core.selected, first, n, core.c.blocks),
brow_slice(w.core.length, first, n, 1), brows(r.attn_latent, n));
RAD_ISSUE_N(ctx, op_rows_expand, n, brows(r.attn_latent, n), RAD_W(rows_kv_b),
brows(r.attn.x, n));
rows_quant.step(ctx, n);
rows_output.step(ctx, r.attn, r.out, n);
RAD_ISSUE_N(ctx, op_rows_gather, run.run * 4096, brows(r.out, run.run),
brows(w.out, g.world * run.run));
}
};
}
} // namespace rad::arch
+40
View File
@@ -73,6 +73,7 @@
#include "rad_block_hc.h"
#include "rad_block_moe_fp8.h"
#include "rad_block_mtp_fp8.h" /* mtp_draft_head_q2(): one reading of the 2-bit head */
#include "rad_spechub.h" /* RAD_MTP_COUPLE_*: the coupling wire */
#include "rad_qsa.h"
#include "rad_block_vit.h"
@@ -108,6 +109,9 @@ struct MtpHcBlock {
/* Vocab-parallel pairs, declared only when the head is sharded: (int32 id, f32 value)
* pairs stored as f32, this rank's top-R and the all-gathered world * R. */
rad_buf dpair = 0, dgath = 0;
/* --draft-coupling: [max_seqs] draws and [depth, max_seqs] states, both PERSIST across
* the serial passes (round 1 resets the logical stream, not the plane). */
rad_buf couple_draws = 0, couple_state = 0;
AttnGatedFP8::Wire attn{};
MoeFP8::Wire mlp{};
QsaIndexer::Wire qsa{};
@@ -158,6 +162,9 @@ struct MtpHcBlock {
rad_op op_dsel = 0, op_dback = 0;
rad_op op_logits = 0, op_dquant = 0, op_dlogits = 0, op_dtopk = 0, op_drerank = 0;
rad_op op_dgather = 0, op_dmerge = 0;
/* --draft-coupling: one op a round. The ABI freezes `round` as a parameter, so a round
* cannot be an operand and each gets its own declared op. */
rad_op op_couple[RAD_MTP_COUPLE_MAX_ROUNDS] = {};
/* rad_arch.h's vocab-head rule, asked through the block by whoever sizes `dlog` and the pair
* planes. */
@@ -422,6 +429,25 @@ inline int MtpHcBlock::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
if (!op_dgather || !op_dmerge) return RAD_E_INVAL;
}
if (!op_dquant || !op_dlogits || !op_dtopk) return RAD_E_INVAL;
/* The coupled proposal: one op a round, after the final rerank/merge, over the shortlist
* whose column 0 is the proposal. Linear chains only: x is a token, while a tree reads
* column 0 as a parent index. The engine refuses other drafters; this check keeps a
* mismatched declaration from writing tokens into a tree plane. */
if (w.couple_draws || w.couple_state) {
if (!w.couple_draws || !w.couple_state || depth < 1 ||
depth > RAD_MTP_COUPLE_MAX_ROUNDS || !op_drerank || (g.world > 1 && !op_dmerge))
return RAD_E_UNSUPPORTED;
for (int round = 1; round <= depth; ++round) {
op_couple[round - 1] = rw(b, RAD_OP(b, "mtp_couple_propose",
/* The whole vocabulary: `row_topk` reports global ids and the merge keeps
* them, while `g.n_vocab` is this rank's shard. */
RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("n_vocab", g.n_vocab_all),
RAD_INT("round", round), RAD_INT("max_rounds", depth)),
RAD_NOWEIGHTS),
{ w.didx, w.dval, w.couple_draws }, { w.didx, w.couple_state });
if (!op_couple[round - 1]) return RAD_E_INVAL;
}
}
}
/* Keep the carry `hin` live through the head's last op: it is written before the mixer and
* lm_head run, and the planner could otherwise give its bytes to them (silently lowering
@@ -429,6 +455,7 @@ inline int MtpHcBlock::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
{
rad_op last = op_logits;
if (q2) last = op_dmerge ? op_dmerge : op_drerank ? op_drerank : op_dtopk;
if (op_couple[0]) last = op_couple[depth - 1];
if (rad_op_reads(b, last, &w.hin, 1) < 0) return RAD_E_INVAL;
}
return RAD_OK;
@@ -524,6 +551,19 @@ inline void MtpHcBlock::step(RadCtx* c, const RadBatch* batch) const {
RAD_ISSUE_N(c, op_drerank, L, brows(w.hout, L), RAD_W(w_lm), brows(w.didx, L),
brows(w.didx, L), brows(w.dval, L), RAD_NONE);
}
/* After the final reranked shortlist: column 0 is what ADV_TAKE reads, and a sampled row
* replaces it with its x. Round 1 runs in the history pass (r < 0), later rounds in their
* own passes. */
if (op_couple[0]) {
const int round = r < 0 ? 1 : r;
if (round < 1 || round > depth) {
rad_step_fail(c, "MTP coupled round exceeds the declared draft depth");
return;
}
RAD_ISSUE_N(c, op_couple[round - 1], L, brows(w.didx, L), brows(w.dval, L),
praw(batch->seq_ids, RAD_I32, batch->n_seq),
brows(w.couple_draws, L), RAD_B(w.couple_state));
}
} else {
RAD_ISSUE_N(c, op_logits, L, brows(w.hout, L), RAD_W(w_lm), brows(w.logits, L));
}
+47 -14
View File
@@ -762,6 +762,10 @@ struct LinearFP8 {
* this is set; otherwise the linear reads unwritten arena bytes. */
bool reads_x = false;
bool mapped = false; /* probe() made the name map; declare() does not make it again */
/* The stored weight this linear's views take when it is not `<base>.weight`: a second linear
* over one container entry, e.g. a whole projection beside its tensor-parallel slice. Set
* before declare(); no name map is made, and only block fp8 is served this way. */
const char* stored = nullptr;
/* The weight's access class (rad_builder.h): read every pass by default; a linear run once a
* request (an encoder's) says so, which lets placement keep it off the card. Set before
* declare(). */
@@ -815,8 +819,10 @@ struct LinearFP8 {
rad_weight decl_dense(RadBuilder* b, Names& nm, const char* base, const char* wn, const RadEncoding& enc,
int64_t K,
RadWeightGroup grp);
/* Rows [r0, r0 + rows) of the input into the same rows of `y`, or from row `y0` of `y` when
* that is set: a rank's run of a step's rows written to a buffer sized for the run. */
void step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, int64_t r0 = 0,
int64_t rows = 0) const;
int64_t rows = 0, int64_t y0 = -1) const;
/* A span per stacked row part (rad_weight_shard_span_parts), in weight rows; the scale plane
* gets the same spans in its rows. Call after declare(). With a 1/128 plane every span and
* total must be whole scale blocks. */
@@ -1054,9 +1060,10 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
/* Name map first: the weight's encoding, looked up through it, picks the branch below. */
const char* wn = nm.f("%s.weight", base);
if (!mapped) RAD_ARCH_TRY(map_linear(b, wn, src));
if (!mapped && !stored) RAD_ARCH_TRY(map_linear(b, wn, src));
const char* sw = stored ? stored : wn;
RadEncoding enc{};
const bool known = weight_enc(b, wn, &enc);
const bool known = weight_enc(b, sw, &enc);
const bool plain = known && rad_enc_is(&enc, "plain") && enc.n_planes == 1 &&
enc.plane[0].dtype == RAD_BF16;
@@ -1066,6 +1073,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
* constraints. */
bf16 = g.w_bf16 || plain;
if (bf16) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
if (known && !plain) {
char en[256];
rad_enc_format(&enc, en, sizeof en);
@@ -1105,6 +1117,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
const bool rot8 = known && linear_is_w8h(enc);
i8 = known && (linear_is_w8(enc) || rot8);
if (i8) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
if (a.q8_fed && rot8 != a.q8_rot) {
fprintf(stderr, "radiance: '%s' is a%s int8 weight and its input is fed codes its "
"producer wrote %s\n", wn, rot8 ? " rotated" : " plain",
@@ -1164,6 +1181,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
* codes, and (scale, zero) as `b_scale`. */
paro5 = known && linear_is_paro_w5(enc);
if (paro5) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
const int bits = enc.plane[0].dtype == RAD_U4 ? 4 : 5;
if (!a.x) {
fprintf(stderr, "radiance: '%s' is a rotated %d-bit weight, rotated from its input's "
@@ -1230,6 +1252,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
* whole 128s, the scale group. */
w4 = known && linear_is_w4(enc);
if (w4) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
if (!a.x) {
fprintf(stderr, "radiance: '%s' is a 4-bit weight, read from its input's bf16 plane, "
"and its input has none\n", wn);
@@ -1300,6 +1327,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
paro = known && linear_is_paro_mxfp4(enc);
mx = known && (linear_is_mxfp4(enc) || paro);
if (mx) {
if (stored) {
fprintf(stderr, "radiance: '%s' views '%s', which only a block-fp8 linear "
"may\n", wn, stored);
return RAD_E_INVAL;
}
if (paro) {
/* The rotation is applied to the activation, so this linear needs the input's bf16
* plane (probe() set reads_x) and quantises it itself. */
@@ -1411,10 +1443,10 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
f32_scale = fp8_scale && fp8_scale->dtype == RAD_F32;
/* One stored weight, two operands: the codes and scale plane of `<base>.weight`, block fp8 in
* the checkpoint or made so by rad-convert's recipe. */
w = decl_view(b, wn, nullptr, "codes", RAD_F8E4M3, { N, K },
w = decl_view(b, wn, stored, "codes", RAD_F8E4M3, { N, K },
access, shard, grp, rp.w, rp.n);
ws_row_div = RAD_FP8_BLOCK;
ws = decl_view(b, sn, wn, "scale", f32_scale ? RAD_F32 : RAD_BF16,
ws = decl_view(b, sn, sw, "scale", f32_scale ? RAD_F32 : RAD_BF16,
{ fp8_blocks(N), fp8_blocks(K) }, access, shard, grp, rp.s, rp.n);
if (!w || !ws) return RAD_E_INVAL;
/* An uneven slice; the scale plane takes the same slice in blocks. */
@@ -1446,10 +1478,11 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
* asymmetric integer and mxfp4 grids) are passed as RAD_NONE when unused, since the runtime checks
* the operand count against the schema (core/runtime/issue.cpp). */
inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, int64_t r0,
int64_t rows) const {
int64_t rows, int64_t y0) const {
if (rows <= 0) rows = T - r0;
const int64_t yr = y0 < 0 ? r0 : y0;
if (bf16) {
RAD_ISSUE_N(c, op, rows, brow_slice(a.x, r0, rows, K), RAD_W(w), brow_slice(y, r0, rows, N));
RAD_ISSUE_N(c, op, rows, brow_slice(a.x, r0, rows, K), RAD_W(w), brow_slice(y, yr, rows, N));
return;
}
if (i8) {
@@ -1458,7 +1491,7 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in
brow_slice(a.s8, r0, rows, K / RAD_FP8_BLOCK));
RAD_ISSUE_N(c, op, rows, brow_slice(a.q8, r0, rows, K),
brow_slice(a.s8, r0, rows, K / RAD_FP8_BLOCK),
RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_NONE);
RAD_W(w), RAD_W(ws), brow_slice(y, yr, rows, N), RAD_NONE, RAD_NONE);
return;
}
/* The 4-bit weight's copies of the input start at their buffers' first row: they live only
@@ -1468,13 +1501,13 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in
brow_slice(a8s, 0, rows, K / RAD_FP8_BLOCK));
RAD_ISSUE_N(c, op2, rows, brow_slice(a8q, 0, rows, K),
brow_slice(a8s, 0, rows, K / RAD_FP8_BLOCK), RAD_W(w), RAD_W(ws),
brow_slice(y, r0, rows, N), RAD_NONE, RAD_NONE);
brow_slice(y, yr, rows, N), RAD_NONE, RAD_NONE);
return;
}
if (w4) {
RAD_ISSUE_N(c, op_a16, rows, brow_slice(a.x, r0, rows, K), brow_slice(a16, 0, rows, K));
RAD_ISSUE_N(c, op, rows, brow_slice(a16, 0, rows, K), RAD_NONE, RAD_W(w), RAD_W(ws),
brow_slice(y, r0, rows, N), RAD_NONE, RAD_NONE);
brow_slice(y, yr, rows, N), RAD_NONE, RAD_NONE);
return;
}
if (paro5 && rot_split && rows > rot_split) {
@@ -1483,14 +1516,14 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in
brow_slice(rot_s2, r0, rows, rot_parts * (K / RAD_FP8_BLOCK)));
RAD_ISSUE_N(c, op2, rows, brow_slice(rot_q2, r0, rows, rot_parts * K),
brow_slice(rot_s2, r0, rows, rot_parts * (K / RAD_FP8_BLOCK)),
RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_NONE);
RAD_W(w), RAD_W(ws), brow_slice(y, yr, rows, N), RAD_NONE, RAD_NONE);
return;
}
if (paro5) {
RAD_ISSUE_N(c, op_rot, rows, brow_slice(a.x, r0, rows, K), RAD_W(rot_pairs),
RAD_W(rot_angle), RAD_W(rot_scale), brow_slice(rot_q, r0, rows, rot_parts * K));
RAD_ISSUE_N(c, op, rows, brow_slice(rot_q, r0, rows, rot_parts * K), RAD_NONE,
RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_NONE);
RAD_W(w), RAD_W(ws), brow_slice(y, yr, rows, N), RAD_NONE, RAD_NONE);
return;
}
if (mx) {
@@ -1504,12 +1537,12 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in
brow_slice(as, r0, rows, ak / RAD_FP8_BLOCK));
RAD_ISSUE_N(c, op, rows, brow_slice(aq, r0, rows, ak),
brow_slice(as, r0, rows, ak / RAD_FP8_BLOCK),
RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_W(wref));
RAD_W(w), RAD_W(ws), brow_slice(y, yr, rows, N), RAD_NONE, RAD_W(wref));
return;
}
RAD_ISSUE_N(c, op, rows, brow_slice(a.q, r0, rows, K),
brow_slice(a.s, r0, rows, K / RAD_FP8_BLOCK),
RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_NONE);
RAD_W(w), RAD_W(ws), brow_slice(y, yr, rows, N), RAD_NONE, RAD_NONE);
}
/* ================================================== an fp8 weight with no op of its own
+4 -3
View File
@@ -13,9 +13,10 @@
* row-interleaved all_gather, bit for bit the replicated projection
*
* The one rule for a run: ceil(T / world) rows, rounded up to whole 16-byte words of the gathered
* row, because an all_gather moves whole words. The indexer gathers one i32 length a row beside
* its page tables, so its runs are multiples of four rows. The last rank's run may be short, or
* lie wholly past a small step (`run_of`). */
* row, because an all_gather moves whole words. The MLA gathers 4096-wide bf16 rows, so its runs
* are ceil(T / world); the indexer gathers one i32 length a row beside its page tables, so its
* runs are multiples of four rows. The last rank's run may be short, or lie wholly past a small
* step (`run_of`). */
#ifndef RAD_GLM_TP_H
#define RAD_GLM_TP_H
#include "rad_arch.h"
-42
View File
@@ -1,42 +0,0 @@
#pragma once
// RADIANCE_PREFILL_PIPELINE: whether an architecture with a pipelined prefill (a prefill step of one
// sequence in two pieces, the collectives between blocks on the second lane, beside the other piece's
// compute) uses it.
//
// auto (or unset) at two ranks, where it was measured (Qwen3.8-27B 41k prefill -6.6 %,
// TranslateGemma-27B 6k -4.7 % at a 2048 chunk, bit for bit the serial step);
// on (1, true) also at three, four and eight ranks: libr4d has the narrow `overlap` two-shot
// all-reduce there (the wide rows' entry points at a launch that leaves the CUs to
// compute), but the step has not been measured on such hardware;
// off (0, false) never.
//
// Any other rank count has no narrow row, and a wide all-reduce beside the GEMMs ate the whole gain at
// two (it slows the GEMM 1.45-2.1x), so those stay serial. Past two ranks only the two-shot band is
// narrow: a piece small enough for the one-shot band (at most 524288 elements, e.g. 512 rows of a model
// 1024 wide) still gets the wide one-shot; and eight ranks have no lossy wire, so `on` there pipelines
// on the exact wire only.
#include "rad_arch.h"
#include <cstdlib>
#include <cstring>
namespace rad::arch {
inline bool prefill_pipeline(RadBuilder* b, int world, const char* who) {
const char* e = std::getenv("RADIANCE_PREFILL_PIPELINE");
auto is = [&](const char* v) { return e && !std::strcmp(e, v); };
const bool on = is("on") || is("1") || is("true"), off = is("off") || is("0") || is("false");
if (e && *e && !on && !off && !is("auto"))
rad_note(b, "%s: RADIANCE_PREFILL_PIPELINE=%s is not auto, on or off; taken as auto", who, e);
if (off || world < 2) return false;
if (world == 2) return true;
if (on && (world == 3 || world == 4 || world == 8)) {
rad_note(b, "%s: RADIANCE_PREFILL_PIPELINE=on allows the pipelined prefill at %d ranks, not "
"yet measured on such hardware (it still needs what the architecture checks)",
who, world);
return true;
}
return false;
}
} // namespace rad::arch
+10
View File
@@ -0,0 +1,10 @@
# Built in radiance's tree, or standalone against an installed radiance
# (`cmake -S arch/fishomni -B build -DCMAKE_PREFIX_PATH=<radiance prefix>`); see cmake/RadAddPlugin.cmake.
if(NOT COMMAND rad_add_plugin)
cmake_minimum_required(VERSION 3.21)
project(fishomni LANGUAGES CXX)
find_package(radiance REQUIRED)
rad_plugin_defaults()
endif()
rad_add_plugin(fishomni architectures SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/fishomni.cpp)
File diff suppressed because it is too large. Load diff
+23 -22
View File
@@ -21,7 +21,6 @@
#include <arch/rad_arch.h>
#include <arch/rad_fp8.h>
#include <arch/rad_block_dflash2.h>
#include <arch/rad_prefill_pipe.h>
#include <cstdio>
#include <cstdlib>
@@ -769,7 +768,7 @@ struct G3Junction {
const rad_buf wr[3] = { q8, s8, res };
rad_op_reads (b, op, rd, res ? 2 : 1);
rad_op_writes(b, op, wr, res ? 3 : 2);
if (res && g.world == 2 && g.wire_exact && rad_op_declared(b, "ar_rmsnorm_quant_i8")) {
if (res && g.world == 2 && rad_op_declared(b, "ar_rmsnorm_quant_i8")) {
const RadParam pa[] = { RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", g.n_embd),
RAD_F64("eps", g.eps), RAD_INT("world_size", g.world),
RAD_STR("dtype", g.dtype), RAD_F64("wadd", g.wadd) };
@@ -796,8 +795,11 @@ struct G3Junction {
* embedding stream. */
struct G3JunctionFP8 {
rad_op op = 0;
/* At two ranks on the exact wire: 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. */
/* 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. The kernel
* sends the exact wire only, so under --tp-wire it is declared all the same and taken by the
* steps whose message goes out exact anyway (ar_wire_is_exact: a decode step of a few rows,
* under --tp-wire-min-kb); a wider step keeps the lossy all-reduce, unfused. */
rad_op op_ar = 0;
rad_buf x = 0, res = 0, q = 0, s = 0;
rad_weight w = 0, wp = 0;
@@ -818,7 +820,7 @@ struct G3JunctionFP8 {
const rad_buf wr[3] = { q, s, res };
rad_op_reads (b, op, rd, res ? 2 : 1);
rad_op_writes(b, op, wr, res ? 3 : 2);
if (res && wp && g.world == 2 && g.wire_exact && rad_op_declared(b, "ar_rmsnorm_post_quant_fp8")) {
if (res && wp && g.world == 2 && rad_op_declared(b, "ar_rmsnorm_post_quant_fp8")) {
const rad_weight ws[2] = { w, wp };
const RadParam pa[] = { RAD_RANGE("M", 1, g.max_tok), RAD_INT("n", n), RAD_F64("eps", g.eps),
RAD_INT("group", RAD_FP8_BLOCK), RAD_INT("world_size", g.world),
@@ -938,6 +940,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
RAD_ARCH_TRY(drafter_geom(dg, m.g, meta, "gemma3 DFlash2"));
Dflash2::Config dc{};
dc.short_block = 1; /* rad_drafter_short_block below */
dc.n_taps = m.n_taps;
dc.block = ctx->max_spec + 1;
dc.taps = rad_meta_geti(meta, "draft.dflash_config.conv_kernel_size", 2);
@@ -1049,6 +1052,7 @@ 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.df.block_lo > 0) RAD_ARCH_TRY(rad_drafter_short_block(b, m.df.block_lo));
rad_note(b, "DFlash2 drafter: %lld layers at absolute %d.., %lld heads / %lld kv x %lld, ffn %lld, "
"%s attention with a %lld-token window, %d trunk taps, embedding x %g, %s head",
(long long)dg.n_layer, base_layer, (long long)dg.n_head, (long long)dg.n_head_kv,
@@ -1192,17 +1196,13 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_NOWEIGHTS),
{m.b_x}, {m.b_x});
/* The pipelined prefill (step_pipelined) wherever a step can be wide enough, on an MXFP4 trunk
* with fused junctions (decided below), at the rank counts RADIANCE_PREFILL_PIPELINE allows
* (rad_prefill_pipe.h: two by default). Its second lane touches the stream, the branch
* output, the shared input pair and the bounds, so those leave the sharing pool (the drafter
* taps are copied on the first lane: pipe_taps)
* (Ctx::lane1_); the pair is one for every block so that this costs one pair, not one a
/* The pipelined prefill (step_pipelined), at two ranks wherever a step can be wide enough, on
* an MXFP4 trunk with fused junctions (decided below); two ranks only, as in qwen35_fp8. Its
* second lane touches the stream, the branch output, the shared input pair and the bounds, so
* those leave the sharing pool (Ctx::lane1_; the drafter taps are copied on the first lane,
* pipe_taps). The pair is one for every block so that this costs one pair, not one a
* linear. */
{
m.pipe_on = g.max_tok >= kPipeMinRows && !calib_dir() &&
rad::arch::prefill_pipeline(b, (int)g.world, "gemma3");
}
m.pipe_on = g.world == 2 && g.max_tok >= kPipeMinRows && !calib_dir();
if (m.pipe_on) {
m.pipe_in.mx = true;
m.pipe_in.K = g.n_embd;
@@ -1509,8 +1509,7 @@ static const int32_t* pipe_ready(RadCtx* c, const RadBatch* batch, Model& m, boo
rad_memcpy_async(d, m.pipe_tbl + 4 * T, 4 * (int64_t)sizeof(int32_t), rad_stream(c)) !=
RAD_OK) {
rad_step_fail(c, "gemma3: the pipelined prefill's piece bounds could not be put on the card "
"(no pinned host memory, or the copy failed); RADIANCE_PREFILL_PIPELINE=off "
"runs prefill serially");
"(no pinned host memory, or the copy failed)");
*failed = true;
return nullptr;
}
@@ -1617,7 +1616,8 @@ static void step(RadCtx* c, const RadBatch* batch) {
for (size_t li = 0; li < m.layers.size(); ++li) {
const Layer& l = m.layers[li];
const bool last = li + 1 == m.layers.size();
const bool far = m.fuse_mx_ar;
/* Per step: the fused junction sends the exact wire only (G3JunctionFP8::op_ar). */
const bool far = m.fuse_mx_ar && ar_wire_is_exact(m.g, T);
l.attn.step(c, batch, true, true, far);
if (!far) l.attn.post(c, T);
l.jf_am.step(c, T, far);
@@ -1633,10 +1633,11 @@ static void step(RadCtx* c, const RadBatch* batch) {
for (size_t li = 0; !m.fuse_mx && li < m.layers.size(); ++li) {
const Layer& l = m.layers[li];
const bool last = li + 1 == m.layers.size();
l.attn.step(c, batch, m.fuse, m.fuse, m.fuse_ar);
if (m.fuse) l.j_am.step(c, T, m.fuse_ar);
l.mlp.step(c, batch, m.fuse, m.fuse && !last, m.fuse_ar);
if (m.fuse && !last) l.j_ma.step(c, T, m.fuse_ar);
const bool ar = m.fuse_ar && ar_wire_is_exact(m.g, T); /* exact wire only, as above */
l.attn.step(c, batch, m.fuse, m.fuse, ar);
if (m.fuse) l.j_am.step(c, T, ar);
l.mlp.step(c, batch, m.fuse, m.fuse && !last, ar);
if (m.fuse && !last) l.j_ma.step(c, T, ar);
tap(li);
}
/* With fused junctions the last layer's tail ran unfused (tail_fused false), so the stream is
+28 -5
View File
@@ -46,6 +46,10 @@ struct Layer {
struct Model {
Names nm{};
Geom g{};
/* The MLA's split of a prefill-sized step under TP (RadBuildCtx::tp_attn_rows). The head
* form declares none of the row form's full-width weights (1.29 GiB a card at eight ranks);
* the row form is opt-in because it is not bit-identical to it (docs/ARCHITECTURES.md). */
GlmMla::Form form = GlmMla::Form::heads;
std::vector<Layer> layers;
rad_kvgroup latent = 0, index = 0, tail = 0, state = 0, conv = 0;
/* The streams and the step's stream-wide buffers. */
@@ -102,7 +106,11 @@ static int stream_buffers(RadBuilder* b, Model& m) {
m.h0 = decl_b(b, nm.f("manifold"), RAD_BF16, { T, 16384 });
m.h1 = decl_b(b, nm.f("manifold_mid"), RAD_BF16, { T, 16384 });
m.x = decl_b(b, nm.f("collapsed"), RAD_BF16, { T, 4096 });
m.y = decl_b(b, nm.f("attn_delta"), RAD_BF16, { T, 4096 });
/* The MLA row form gathers every rank's run at full length, so its output is rounded up to
* whole runs; the head form keeps the step's rows. */
const bool rows = g.world > 1 && m.form == GlmMla::Form::rows;
m.y = decl_b(b, nm.f("attn_delta"), RAD_BF16,
{ rows ? g.world * GlmMla::rows_for(T, g.world) : T, 4096 });
m.post = decl_b(b, nm.f("post"), RAD_F32, { T, 4 });
m.comb = decl_b(b, nm.f("mix"), RAD_F32, { T, 16 });
m.act.x = decl_b(b, nm.f("normalized"), RAD_BF16, { T, 4096 });
@@ -132,9 +140,9 @@ static int kda_buffers(RadBuilder* b, Model& m) {
kw.core.out = bf("recurrent_out", W);
return RAD_OK;
}
/* The head form's buffers, then the selection planes the indexer writes and the MLA reads. Under
* TP the selection planes hold every rank's run of rows, which can reach past the step
* (DsaCache::part_rows). */
/* The head form's buffers, then the selection planes the indexer writes and the MLA reads, then
* the row form's (Model::form). Under TP the selection planes hold every rank's run of rows,
* which can reach past the step (DsaCache::part_rows). */
static int mla_buffers(RadBuilder* b, Model& m) {
auto& g = m.g;
auto& nm = m.nm;
@@ -160,6 +168,16 @@ static int mla_buffers(RadBuilder* b, Model& m) {
const int64_t TQ = g.world > 1 ? std::max(T, g.world * part) : T;
aw.core.selected = decl_b(b, nm.f("selected"), RAD_I32, { TQ, 513 });
aw.core.length = decl_b(b, nm.f("length"), RAD_I32, { TQ });
if (g.world > 1 && m.form == GlmMla::Form::rows) {
auto& rows = aw.rows;
const int64_t R = GlmMla::rows_for(T, g.world);
rows.q = decl_b(b, nm.f("rows_query"), RAD_BF16, { R, 16384 });
rows.q_latent = decl_b(b, nm.f("rows_q_latent"), RAD_BF16, { R, 64, 512 });
rows.attn_latent = decl_b(b, nm.f("rows_attn_latent"), RAD_BF16, { R, 64, 512 });
rows.attn.x = decl_b(b, nm.f("rows_expanded"), RAD_BF16, { R, 16384 });
RAD_ARCH_TRY(rows.attn.declare_qs(b, nm, g, "rows_expanded", 16384, R));
rows.out = decl_b(b, nm.f("rows_out"), RAD_BF16, { R, 4096 });
}
return RAD_OK;
}
static int indexer_buffers(RadBuilder* b, Model& m) {
@@ -292,7 +310,10 @@ static int layer(RadBuilder* b, Model& m, Layer& lay, Names& nm, int l, bool dra
decl_w(b, nn, RAD_F32, { 4096 }, RAD_ACCESS_PER_TOKEN, RAD_SHARD_NONE, grp_layer(l));
RAD_ARCH_TRY(lay.norm.declare(b, g, gain, input, m.act, 4096));
if (lay.full) {
RAD_ARCH_TRY(lay.mla.declare(b, nm, g, l, m.aw, ap, m.latent, true, true));
// The MTP layer keeps the head form: its row form would cost another 126 MB a card.
auto aw = m.aw;
if (draft) aw.rows = {};
RAD_ARCH_TRY(lay.mla.declare(b, nm, g, l, aw, ap, m.latent, true, true));
DsaCache::Config c;
c.blocks = (g.max_ctx + 3) / 4 + 1;
c.work = (g.max_tok + 3) / 4 + g.max_seqs;
@@ -429,6 +450,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
auto& g = m.g;
m.nm = Names(ctx->scope);
auto& nm = m.nm;
m.form = ctx->tp_attn_rows ? GlmMla::Form::rows : GlmMla::Form::heads;
RAD_ARCH_TRY(geometry(g, meta, ctx, world));
RAD_ARCH_TRY(stream_buffers(b, m));
RAD_ARCH_TRY(kda_buffers(b, m));
@@ -578,6 +600,7 @@ static int probe(const RadModelMeta* meta, RadArchProbe* out) {
if (k > 0) out->enc_merge = k * k;
}
out->shape_probe_ok = 1;
out->tp_attn_rows_ok = 1;
return RAD_OK;
}
} // namespace glm5next
+27 -26
View File
@@ -87,9 +87,7 @@
#include <arch/rad_block_mtp_fp8.h>
#include <arch/rad_block_dflash2.h>
#include <arch/rad_block_vit.h>
#include <arch/rad_prefill_pipe.h>
#include <cstdlib>
#include <cstring>
#include <vector>
@@ -284,6 +282,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui
RAD_ARCH_TRY(drafter_geom(dg, m.g, meta, "DFlash2"));
Dflash2::Config dc{};
dc.short_block = 1; /* rad_drafter_short_block below */
dc.n_taps = m.n_taps;
dc.block = ctx->max_spec + 1;
dc.taps = rad_meta_geti(meta, "draft.dflash_config.conv_kernel_size", 2);
@@ -434,6 +433,7 @@ 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.df.block_lo > 0) RAD_ARCH_TRY(rad_drafter_short_block(b, m.df.block_lo));
rad_note(b, "DFlash2 drafter: %lld layers at absolute %d..%d, %lld heads / %lld kv x %lld, "
"ffn %lld, %s attention with a %lld-token window, %d trunk tap(s); one forward "
@@ -604,17 +604,17 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
"and head instead of %lld", kvq_name, kvq == RAD_KVQ_V4 ? "E4M3" : "int6",
(long long)(g.head_dim * row16 / 16), (long long)(2 * g.head_dim));
/* The SSM state cache is f32.
/* The SSM state cache is f32, or f16 under --state-cache-dtype f16 (RadBuildCtx::state_dtype).
*
* State reads and writes dominate the two GDN kernels: one [128,128] state is 64 KiB at f32,
* and a speculative step writes one per candidate token per head, since the surviving
* candidate is unknown until after the layer. At depth 7 that is hundreds of MB per rank per
* step; f16 would halve it. libr4d compiles both widths (`SDT`) and the f16 round-trip error is
* ~5e-4 relative (elements are O(0.1)); vLLM's `--mamba-ssm-cache-dtype` is commonly float16.
* It stays f32 because f16 changes the generated text. */
* and a step reads a sequence's state and writes it back (under the anchor form once, whatever
* the draft depth). libr4d compiles both widths (`SDT`, from the cache's dtype); the f16
* round trip is ~5e-4 relative (elements are O(0.1)). f16 halves the state pool, its host
* checkpoints and the state traffic, and changes the generated text, so f32 is the default. */
RadKVGroupDecl sd{};
sd.kind = RAD_KV_LINEAR;
sd.dtype = RAD_F32;
sd.dtype = ctx->state_dtype == RAD_F16 ? RAD_F16 : RAD_F32;
if (sd.dtype == RAD_F16) rad_note(b, "GDN state cache f16 (--state-cache-dtype f16)");
sd.n_head_kv = gc.n_head_v;
sd.state_dim[0] = gc.head_v;
sd.state_dim[1] = gc.head_k;
@@ -890,22 +890,23 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_NOWEIGHTS),
{m.b_x}, {m.b_x});
#if !QWEN35_MOE
/* The pipelined prefill (step_pipelined), wherever a step can be wide enough, at the rank
* counts RADIANCE_PREFILL_PIPELINE allows (rad_prefill_pipe.h: two by default). Its second
* lane touches the residual stream, the normed input in both forms and the bounds, so those
* leave the sharing pool (the drafter taps are copied on the first lane: pipe_taps)
* (Ctx::lane1_); the bounds also because no declared op reads them, which would leave a
* transient dead before the kernels that take them raw. */
/* Not on a trunk whose norms write rotated int8 codes (qwen35_w8a8 where libr4d serves the
/* The pipelined prefill (step_pipelined), at two ranks wherever a step can be wide enough.
* Its second lane needs a narrow `overlap` all-reduce beside the GEMMs; two ranks is the only
* rank count where the step has been timed against the serial one, so elsewhere prefill stays
* serial. The second lane touches the residual stream, the normed input in both forms and the
* bounds, so those leave the sharing pool (Ctx::lane1_; the drafter taps are copied on the
* first lane, pipe_taps); the bounds also because no declared op reads them, which would leave
* a transient dead before the kernels that take them raw.
*
* Not on a trunk whose norms write rotated int8 codes (qwen35_w8a8 where libr4d serves the
* `_had_quant_i8g` forms, a_h.q8_fed): the boundary norms on the second lane
* (NormQuantFP8::step_plain) issue the E4M3 pair only, and that int8 path is unmeasured, so such a
* trunk prefills serially, as before this pipeline. */
* (NormQuantFP8::step_plain) issue the E4M3 pair only, so such a trunk prefills serially. */
if (m.a_h.q8_fed) {
if (g.world > 1 && g.max_tok >= kPipeMinRows && ctx->rank == 0)
if (g.world == 2 && g.max_tok >= kPipeMinRows && ctx->rank == 0)
fprintf(stderr, "radiance: qwen35: the pipelined prefill stays off on this trunk: its norms "
"write rotated int8 codes, which the pipeline's boundary norms do not\n");
} else {
m.pipe_on = g.max_tok >= kPipeMinRows && rad::arch::prefill_pipeline(b, (int)g.world, "qwen35");
m.pipe_on = g.world == 2 && g.max_tok >= kPipeMinRows;
}
if (m.pipe_on) {
RAD_ARCH_TRY(rad_buf_concurrent(b, m.b_x));
@@ -1215,8 +1216,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
* block's all-reduce at `overlap` 1 (a narrow launch that leaves the CUs to the compute beside it;
* the fused all-reduce and norm would hold its blocks on the card while it waits on the link and
* slow the GEMM beside it by a third), then the next block's plain norm, and after a feed-forward
* that keeps its add, the add and the final norm (the drafter taps go on the first lane, pipe_taps). Measured on Qwen3.8-27B at two
* R9700s: a 41k-token prompt 9.0 -> 8.4 s.
* that keeps its add, the add and the final norm (the drafter taps go on the first lane, pipe_taps).
*
* mixer the delta net and the dense attention run whole a piece at a time and hand the
* first piece off behind its out projection (ArPieces), so its boundary runs beside
@@ -1228,12 +1228,12 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
* band of a whole step (pipe_rows), the all-reduce reduces within a row, the delta net carries its
* state through the cache as between two chunks, and each attention piece sees the cache up to its
* own end. The narrow all-reduce and the plain norm write the fused op's bytes, so a pipelined
* step is bit for bit the serial one (Qwen's KL recordings compare equal).
* step is bit for bit the serial one.
*
* The second lane's requirements (Ctx::lane1_) hold: every collective of the step is on it, the
* embedding's included; none carries a device-wide arrival counter (the all-reduce's sequence
* counters order its own launches, which one lane keeps in order); and the buffers it touches left
* the sharing pool at declare -- not the drafter taps, which it no longer touches (pipe_taps). */
* the sharing pool at declare (the drafter taps it does not touch: pipe_taps). */
template <class F>
static void pipe_on1(RadCtx* c, const F& issue) {
@@ -1363,8 +1363,7 @@ static const int32_t* pipe_ready(RadCtx* c, const RadBatch* batch, Model& m, boo
rad_memcpy_async(d, m.pipe_tbl + 3 * T, 3 * (int64_t)sizeof(int32_t), rad_stream(c)) !=
RAD_OK) {
rad_step_fail(c, "qwen35: the pipelined prefill's piece bounds could not be put on the card "
"(no pinned host memory, or the copy failed); RADIANCE_PREFILL_PIPELINE=off "
"runs prefill serially");
"(no pinned host memory, or the copy failed)");
*failed = true;
return nullptr;
}
@@ -1553,6 +1552,8 @@ static int probe(const RadModelMeta* meta, RadArchProbe* out) {
if (hd == 256 && nkv > 0 && meta->n_head == 6 * nkv)
out->kv_quant_ok = (1 << RAD_KVQ_V4) | (1 << RAD_KVQ_K6V4);
}
/* The delta-net state cache is declared at RadBuildCtx::state_dtype. */
out->state_f16_ok = 1;
const char* da = rad_meta_gets(meta, "draft.architectures", "");
if (da && std::strstr(da, "DFlash2")) {
const long long bs = rad_meta_geti(meta, "draft.dflash_config.block_size", 8);
+29
View File
@@ -174,6 +174,10 @@ struct Model {
rad_buf b_mtp_hin = 0, b_mtp_hw = 0, b_mtp_e = 0, b_mtp_hout = 0;
rad_buf b_mtp_dq = 0, b_mtp_ds = 0, b_mtp_dsum = 0;
rad_buf b_mtp_dlog = 0, b_mtp_didx = 0, b_mtp_dval = 0;
/* --draft-coupling: [max_seqs] draws staged before the serial passes and [max_spec,
* max_seqs] states read back after them. PERSIST: the engine writes them from the host and the
* head writes the states on the card, across a step boundary. */
rad_buf b_mtp_couple_draws = 0, b_mtp_couple_state = 0;
rad_buf b_mtp_dpair = 0, b_mtp_dgath = 0;
HcEnter enter;
@@ -1261,6 +1265,30 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
* vocab-parallel all-gather a 16-byte row (rad_block_mtp_hc.h, kDraftTopR). */
m.b_mtp_didx = decl_b(b, nm.f("mtp_didx"), RAD_I32, {g.max_logit_rows, kDraftTopR});
m.b_mtp_dval = decl_b(b, nm.f("mtp_dval"), RAD_F32, {g.max_logit_rows, kDraftTopR});
/* --draft-coupling (RadBuildCtx::draft_coupling). Q is the law of the 2-bit candidate
* head's reranked top-8; a plain bf16 head leaves one candidate and nothing to couple
* over. The engine checks the drafter shape (a linear serial chain). */
if (ctx->draft_coupling) {
if (!m.b_mtp_didx || g.max_spec < 1 || g.max_spec > RAD_MTP_COUPLE_MAX_ROUNDS) {
rad_note(b, "--draft-coupling on requires the 2-bit candidate head's reranked "
"top8 and max_spec 1..%d",
RAD_MTP_COUPLE_MAX_ROUNDS);
return RAD_E_UNSUPPORTED;
}
m.b_mtp_couple_draws = decl_b(b, nm.f("mtp_couple_draws"), RAD_U32,
{g.max_seqs, RAD_MTP_COUPLE_DRAW_WORDS},
RAD_BUF_PERSIST);
m.b_mtp_couple_state = decl_b(b, nm.f("mtp_couple_state"), RAD_U32,
{g.max_spec, g.max_seqs,
RAD_MTP_COUPLE_STATE_WORDS},
RAD_BUF_PERSIST);
if (!m.b_mtp_couple_draws || !m.b_mtp_couple_state) return RAD_E_INVAL;
rad_note(b, "MTP coupled: the head samples x~Q from its reranked top8 each round "
"(FP64), and the sampler couples the target's own draw to it; %lld "
"round(s), fixed-seed tokens are NOT preserved",
(long long)g.max_spec);
}
}
MtpHcBlock::Wire tw{};
@@ -1270,6 +1298,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
tw.dq = m.b_mtp_dq; tw.ds = m.b_mtp_ds; tw.dsum = m.b_mtp_dsum;
tw.dlog = m.b_mtp_dlog; tw.didx = m.b_mtp_didx; tw.dval = m.b_mtp_dval;
tw.dpair = m.b_mtp_dpair; tw.dgath = m.b_mtp_dgath;
tw.couple_draws = m.b_mtp_couple_draws; tw.couple_state = m.b_mtp_couple_state;
/* The head reuses the trunk's working buffers, all dead by the time a draft round runs (a
* second set would be 150 MiB). Only the wide stream and the carry are its own, since
* they must survive the round that produced them. */