radiance 1.1.0

- The VRAM split has no ratio: --expert-vs-cache-ratio is gone (now an unknown option). The
  experts keep what the pinned host pool cannot hold, the KV cache takes the rest and lends idle
  blocks to the experts. Flash-Next at tp2: 7.9 GiB of KV a card, up from 4.7. Startup, /stats,
  /metrics and the dashboard report the most the cache can hold.
- Linear-state snapshots live in pinned host memory, not VRAM (--checkpoint-slots sizes the
  store). Prefix-cache misses on agent sessions fixed; a published conversation is no longer
  trimmed away.
- Any world size from the shipped container (baKon): --tp 3 on Flash-Next with no padded
  checkpoint -- the attention world, uneven delta-net heads, the fill op.
- Kernels (keptn42): moe_gemm_q decode loads and one-trip run search, hc_read's prefill form
  (+3.7% prefill at 18K), hc_up row groups up to eight, the fused router launch. Each is the rule
  where it was measured faster; no environment switches.
- Tool calls: a typed argument is always JSON (Python True/None no longer leak into arguments --
  Codeberg #21); nine further reply-parser fixes; the reply path inside a call is ~12x cheaper on
  the host.
- The kernel changes: prefill +2-3% and decode -2% a step on 2x R9700 at tp2, identical text.
This commit is contained in:
Deadcode committed 2026-10-06 14:04:49 +02:00
1 parent 9f5feb24ee
commit 82b5835c02
101 files changed
+5105 -3767

No files matched your search

+22 -15
View File
@@ -580,10 +580,11 @@ struct Geom {
* -- three ranks over Flash-Next's two KV heads -- serves attention on the largest multiple
* of the head count below it (`attn_world`, here 2: split, or replicated when that is still
* above the count) and the remaining ranks are ATTENTION-ZERO: they declare the same heads and
* the same program as their peers, read nothing for the attention projections
* (rad_weight_shard_zero) and add exactly zero to the reduce. `attn_rank` is this rank's
* index inside the attention world (an attention-zero rank clamps to the last). When
* attn_world == world nothing here is used and every block declares as it always did. */
* the same program as their peers, compute no attention (a `fill` of the block's output and
* a zero KV group, rad_kv_group_zero) and add exactly zero to the reduce. `attn_rank` is this
* rank's index inside the attention world (an attention-zero rank clamps to the last). When
* attn_world == world nothing here is used and every block declares as it always did. Set
* only for a plugin that asks for it (geom_from's `attn_world_ok`). */
int attn_world = 1;
int attn_rank = 0;
bool attn_zero = false;
@@ -728,20 +729,15 @@ inline long long meta_int2(const RadModelMeta* m, const char* gguf_key, const ch
return v == sentinel ? dflt : v;
}
/* `arch_splits_ff`: the plugin divides the feed-forward width across the ranks ITSELF (qwen4exp:
* the routed experts' width is cut by whole fp8 blocks, by parity, at any world up to the block
* count). With it set, a width the world does not divide is left whole in g.n_ff and the plugin
* takes its slice from there; without it the refusal stands, because for every other plugin g.n_ff
* IS the per-rank width. A width that does divide is unaffected either way. */
/* THE REMAINDER-FIRST ORDER OF THE RANKS. What does not divide evenly over the world and is a
* HEAD'S worth of work -- the extra delta-net head -- goes to the ranks with NO attention first:
* they pay nothing for attention (rad_block_attn_gated_fp8.h: one fill) and keep no attention
* cache (rad_kv_group_zero), so they have the time for it, and the rank that held the extra head
* AND attention -- the step's slowest -- is lighter for it. An expert's extra block is not a
* head's worth (qwen4exp_fp8.cpp says why it stays on the rotation). This is a rank's PLACE in that order: the attention-zero ranks
* take places 0..n-1 whatever `parity` is, and the attention ranks follow in today's rotation by
* `parity` -- so where no rank is attention-zero (every world the heads divide) the place is
* exactly (rank - parity) mod world and every cut is what it always was, byte for byte. */
* head's worth (qwen4exp_fp8.cpp says why it stays on the rotation). This is a rank's PLACE in
* that order: the attention-zero ranks take places 0..n-1 whatever `parity` is, and the attention
* ranks follow in the rotation by `parity` -- so where no rank is attention-zero (every world the
* heads divide) the place is exactly (rank - parity) mod world and every cut is the even one. */
inline int64_t remainder_place(const Geom& g, int rank, int parity) {
const int nz = g.world - g.attn_world;
if (rank >= g.attn_world) return rank - g.attn_world;
@@ -749,8 +745,19 @@ inline int64_t remainder_place(const Geom& g, int rank, int parity) {
return nz + ((rank - parity) % aw + aw) % aw;
}
/* `arch_splits_ff`: the plugin divides the feed-forward width across the ranks ITSELF (qwen4exp:
* the routed experts' width is cut by whole fp8 blocks, by parity, at any world up to the block
* count). With it set, a width the world does not divide is left whole in g.n_ff and the plugin
* takes its slice from there; without it the refusal stands, because for every other plugin g.n_ff
* IS the per-rank width. A width that does divide is unaffected either way. */
/* `attn_world`: the plugin's attention block serves a SMALLER ATTENTION WORLD (Geom::attn_world)
* -- it declares the attention-zero rank's fill and zero KV group and spans the attention
* projections by `attn_rank`. Only rad_block_attn_gated_fp8.h does. Without it a world the KV head
* count neither divides nor is a multiple of is refused here, by name, rather than declared with
* an even shard the block would then cut wrongly. */
inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c,
const char* dtype, uint32_t act_dtype, bool arch_splits_ff = false) {
const char* dtype, uint32_t act_dtype, bool arch_splits_ff = false,
bool attn_world_ok = false) {
if (!m || !c) return RAD_E_INVAL;
if (c->is_draft) {
/* A drafter declared into this plugin would need a second set of per-rank handles and a
@@ -819,7 +826,7 @@ inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c,
g.attn_world = g.world;
g.attn_rank = g.rank;
g.attn_zero = false;
if (g.world > g.n_head_kv_all && g.world % g.n_head_kv_all != 0) {
if (attn_world_ok && g.world > g.n_head_kv_all && g.world % g.n_head_kv_all != 0) {
g.attn_world = (int)(g.n_head_kv_all * (g.world / g.n_head_kv_all));
g.attn_zero = g.rank >= g.attn_world;
g.attn_rank = g.attn_zero ? g.attn_world - 1 : g.rank;
+55 -77
View File
@@ -76,19 +76,6 @@ namespace arch {
* decide, at declare time and identically on both ranks, whether the op exists at all. */
static const int64_t kMoeSortRows = 1024;
/* R4D_ROUTER_FUSED: the router's GEMM in the same launch as `router_topk_scatter`
* (`router_gemm_topk_scatter`), at decode shapes. 1 also computes the shared expert's one-column
* gate in that launch, 2 leaves the gate its own `gemm_nt`; unset or 0 declares nothing new and the
* graph is the one without it. Read once, at declare, so a recorded pass and a live one issue the
* same ops. Every output is byte-identical either way (see libr4d's r4d_moe.hip). */
inline int moe_router_fused_mode() {
static const int v = [] {
const char* e = std::getenv("R4D_ROUTER_FUSED");
const int x = e ? std::atoi(e) : 0;
return (x == 1 || x == 2) ? x : 0;
}();
return v;
}
/* The skinny bf16 GEMM's decode band, which is where the fused op's GEMM is that GEMM's own
* arithmetic; past it the router runs the tiled kernel and the pair is issued as before. */
static const int64_t kMoeRouterFusedRows = 64;
@@ -379,9 +366,10 @@ struct MoeFP8 {
* a prefill chunk is past it. */
rad_op op_topk_scatter = 0;
int64_t topk_scatter_rows = 0;
/* `router_gemm_topk_scatter` (R4D_ROUTER_FUSED), if declared: the router GEMM in front of the
* pair above, up to `router_fused_rows` tokens, and with `router_fused_gate` the shared
* expert's gate (`op_sgate`'s work) as well. 0 issues the router and the pair as always. */
/* `router_gemm_topk_scatter`, if it resolves: the router GEMM in front of the pair above, up
* to `router_fused_rows` tokens, and with `router_fused_gate` the shared expert's gate
* (`op_sgate`'s work) as well. 0 -- the op did not resolve -- issues the router and the pair
* as always, and so does a pass of more rows. */
rad_op op_router_fused = 0;
int64_t router_fused_rows = 0;
bool router_fused_gate = false;
@@ -595,6 +583,9 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
/* A rank whose slice of the shared arm is empty (see Config) declares no arm, and the
* all-reduce carries the others' share. */
const bool shared = c.n_ff_shared > 0 && src.shared;
/* Whether `w.sgate` is out of the sharing pool -- set where the shared arm says so, and what
* the fused router's gate write relies on (see there). */
bool sgate_concurrent = false;
/* THE INPUT MAY BE PREPARED BY THE ARCHITECTURE, and `src.norm == nullptr` is how it says so.
*
@@ -1358,6 +1349,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
RAD_ARCH_TRY(rad_buf_concurrent(b, w.sff.x));
RAD_ARCH_TRY(rad_buf_concurrent(b, w.sout));
RAD_ARCH_TRY(rad_buf_concurrent(b, w.sgate));
sgate_concurrent = true;
/* The gate/up rows and the down columns of this rank's slice -- the even split, or the
* uneven one Config states -- and the split of gate from up stays, because that is a
* property of the stacked checkpoint tensor and not of the rank count. */
@@ -1390,66 +1382,51 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
{w.h.x}, {w.sgate});
}
/* ---- R4D_ROUTER_FUSED: the router GEMM and `router_topk_scatter` as one op, and with mode 1
* the shared gate as well. Declared HERE, after the gate's weight, and not beside the router:
* the op names both weights, and declaring it late only lengthens the planned lifetime of the
* buffers it touches, never shortens one -- each of them is also declared by the op it stands
* in for, at that op's place. Only where the pair itself is declared, and only up to the
* pair's own row bound. */
if (moe_router_fused_mode() && op_topk_scatter) {
/* ---- THE ROUTER GEMM AND `router_topk_scatter` AS ONE OP, with the shared expert's gate in
* the same launch wherever the layer has one. Asked for wherever the pair itself is declared,
* up to the pair's own row bound and the skinny GEMM's decode band; a plugin that does not
* serve it leaves the handle 0 and the router and the pair are issued as declared above.
* Declared HERE, after the gate's weight, and not beside the router: the op names both
* weights, and declaring it late only lengthens the planned lifetime of the buffers it
* touches, never shortens one -- each of them is also declared by the op it stands in for, at
* that op's place.
*
* EXCEPT `w.sgate`, WHICH THE FUSED OP WRITES AHEAD OF ITS DECLARED WRITER. The gate is issued
* at the router's place, before `op_sgate`'s declared index, so a plan bounded by the linear
* order could lend `w.sgate`'s bytes to a buffer that is live in between. It cannot here
* because the shared arm took `w.sgate` out of the sharing pool (rad_buf_concurrent), and the
* declaration refuses a fused gate on a layer where that has not happened.
*
* NOT rw()'s braced lists and not one RAD_OP per combination: the parameter list (a protect
* list or not), the weights and the write set (the gate or not) are conditional, so each is
* one array cut to its length. */
if (op_topk_scatter) {
router_fused_rows = topk_scatter_rows < kMoeRouterFusedRows ? topk_scatter_rows
: kMoeRouterFusedRows;
router_fused_gate = moe_router_fused_mode() == 1 && op_sgate != 0;
const int64_t fr = router_fused_rows;
if (router_fused_gate && !prot_list)
op_router_fused = rw(b, RAD_OP(b, "router_gemm_topk_scatter",
RAD_PARAMS(RAD_RANGE("M", 1, fr), RAD_INT("K", g.n_embd),
RAD_INT("n_expert", c.n_expert_all),
RAD_INT("n_local", c.n_expert),
RAD_INT("top_k", c.top_k),
RAD_INT("norm", c.norm_topk),
RAD_STR("dtype", g.dtype),
RAD_INT("expert_base", c.expert_base)),
RAD_WEIGHTS(w_router, w_sgate)),
{w.h.x}, {w.logits, w.ids, w.ew, w.sorted, w.eoff, w.ecnt,
w.sgate});
else if (router_fused_gate)
op_router_fused = rw(b, RAD_OP(b, "router_gemm_topk_scatter",
RAD_PARAMS(RAD_RANGE("M", 1, fr), RAD_INT("K", g.n_embd),
RAD_INT("n_expert", c.n_expert_all),
RAD_INT("n_local", c.n_expert),
RAD_INT("top_k", c.top_k),
RAD_INT("norm", c.norm_topk),
RAD_STR("dtype", g.dtype),
RAD_INT("expert_base", c.expert_base),
RAD_STR("protect", prot_list)),
RAD_WEIGHTS(w_router, w_sgate)),
{w.h.x}, {w.logits, w.ids, w.ew, w.sorted, w.eoff, w.ecnt,
w.sgate});
else if (!prot_list)
op_router_fused = rw(b, RAD_OP(b, "router_gemm_topk_scatter",
RAD_PARAMS(RAD_RANGE("M", 1, fr), RAD_INT("K", g.n_embd),
RAD_INT("n_expert", c.n_expert_all),
RAD_INT("n_local", c.n_expert),
RAD_INT("top_k", c.top_k),
RAD_INT("norm", c.norm_topk),
RAD_STR("dtype", g.dtype),
RAD_INT("expert_base", c.expert_base)),
RAD_WEIGHTS(w_router)),
{w.h.x}, {w.logits, w.ids, w.ew, w.sorted, w.eoff, w.ecnt});
else
op_router_fused = rw(b, RAD_OP(b, "router_gemm_topk_scatter",
RAD_PARAMS(RAD_RANGE("M", 1, fr), RAD_INT("K", g.n_embd),
RAD_INT("n_expert", c.n_expert_all),
RAD_INT("n_local", c.n_expert),
RAD_INT("top_k", c.top_k),
RAD_INT("norm", c.norm_topk),
RAD_STR("dtype", g.dtype),
RAD_INT("expert_base", c.expert_base),
RAD_STR("protect", prot_list)),
RAD_WEIGHTS(w_router)),
{w.h.x}, {w.logits, w.ids, w.ew, w.sorted, w.eoff, w.ecnt});
if (!op_router_fused) router_fused_gate = false;
const bool gate = op_sgate != 0;
if (gate && !sgate_concurrent) {
fprintf(stderr, "radiance: layer %d: the fused router writes the shared gate ahead of "
"its declared writer, which needs `sgate` out of the sharing pool, "
"and it is not\n", l);
return RAD_E_STATE;
}
const RadParam fp[] = {
RAD_RANGE("M", 1, router_fused_rows), RAD_INT("K", g.n_embd),
RAD_INT("n_expert", c.n_expert_all), RAD_INT("n_local", c.n_expert),
RAD_INT("top_k", c.top_k), RAD_INT("norm", c.norm_topk),
RAD_STR("dtype", g.dtype), RAD_INT("expert_base", c.expert_base),
RAD_STR("protect", prot_list),
};
const int n_fp = (int)(sizeof(fp) / sizeof(fp[0])) - (prot_list ? 0 : 1);
const rad_weight fw[2] = { w_router, w_sgate };
op_router_fused = rad_decl_op(b, "router_gemm_topk_scatter", fp, n_fp, fw, gate ? 2 : 1);
if (op_router_fused) {
const rad_buf rd[1] = { w.h.x };
const rad_buf wr[7] = { w.logits, w.ids, w.ew, w.sorted, w.eoff, w.ecnt, w.sgate };
rad_op_reads (b, op_router_fused, rd, 1);
rad_op_writes(b, op_router_fused, wr, gate ? 7 : 6);
}
router_fused_gate = op_router_fused && gate;
}
if (g.world > 1)
@@ -1486,7 +1463,7 @@ inline int MoeFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
* MlpFP8 slice. */
inline void MoeFP8::shared_up(RadCtx* c_, int64_t T, int64_t r0, int64_t rows,
bool gate_done) const {
/* `gate_done`: the fused router already wrote this pass's gate (R4D_ROUTER_FUSED=1). */
/* `gate_done`: the fused router already wrote this pass's gate. */
if (!gate_done)
RAD_ISSUE_N(c_, op_sgate, rows, brow_slice(w.h.x, r0, rows, g.n_embd), RAD_W(w_sgate),
brow_slice(w.sgate, r0, rows, 1));
@@ -1519,8 +1496,9 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows) const
* a routed layer, a whole dependent dispatch for a kilobyte. */
int32_t* const counts = rad_route_counts(c_, layer, ne);
const RadOperand ecnt = counts ? praw(counts, RAD_I32, ne) : brows(w.ecnt, ne);
/* R4D_ROUTER_FUSED: the router, the pair below and -- with `router_fused_gate` -- the shared
* gate, in one launch with the operands the three are handed. */
/* THE FUSED ROUTER where it resolved and the pass is within its rows: the router, the pair
* below and -- with `router_fused_gate` -- the shared gate, in one launch with the operands
* the three are handed. Otherwise the router and the pair, as declared. */
const bool rfused = op_router_fused && rows <= router_fused_rows;
if (rfused)
RAD_ISSUE_N(c_, op_router_fused, rows, brow_slice(w.h.x, r0, rows, g.n_embd),
-7
View File
@@ -727,13 +727,6 @@ struct LinearFP8 {
int64_t span_lo = 0, int64_t span_hi = 0);
void step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, int64_t r0 = 0,
int64_t rows = 0) const;
/* THIS RANK READS NOTHING FOR IT: the weight and its scale plane are zeros
* (rad_weight_shard_zero). After declare(); for an attention-zero rank (rad_arch.h). */
int zero_share(RadBuilder* b) const {
RAD_ARCH_TRY(rad_weight_shard_zero(b, w));
if (ws) RAD_ARCH_TRY(rad_weight_shard_zero(b, ws));
return RAD_OK;
}
/* A SPAN PER STACKED ROW PART (rad_weight_shard_span_parts), in the weight's rows; the scale
* plane takes the same spans in its own rows. After declare(); every span and total has to be
* whole scale blocks when the plane is 1/128, which head widths of 128 are. */
+5 -2
View File
@@ -320,8 +320,11 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
/* arch_splits_ff: the routed experts' width (640 = five fp8 blocks) is split by the plugin
* below, by whole blocks and parity, at any world up to the block count -- three ranks included,
* which 640 does not divide. geom_from must not refuse what the plugin then answers. */
RAD_ARCH_TRY(geom_from(m.g, meta, ctx, "bf16", RAD_BF16, /*arch_splits_ff=*/true));
* which 640 does not divide. geom_from must not refuse what the plugin then answers.
* attn_world_ok: every attention block this plugin declares is rad_block_attn_gated_fp8.h's,
* which serves the smaller attention world of a world the two KV heads do not divide. */
RAD_ARCH_TRY(geom_from(m.g, meta, ctx, "bf16", RAD_BF16, /*arch_splits_ff=*/true,
/*attn_world_ok=*/true));
Geom& g = m.g;
/* Qwen4-Exp's norms are all `x_hat * (1 + w)` -- `Qwen3NextRMSNorm.forward` is