qwen4exp: verify/decode pass launch order, prefetch width, chain polling, turnover slots

Lossless (no kernel arithmetic or operand change): the router weight is read beside the mixer
all-reduce; k/v projections before q|gate; the indexer's keys behind the attention launches within
the exact bound; verify-height prefetch at 32 workgroups (`prefetch` takes an optional `blocks`
param) and the shared down arm before the routed down from 2 rows (kVerifyRows); chain waits poll by
grid size (R4D_CHAIN_TINY/SMALL/MID); the mover's kept-free turnover slots follow the swaps the
engine asked for (MoverConfig::turnover_adaptive) instead of a fixed 640.

arch_test: new a_flash_next_decode_step_orders_its_small_launches; the pinned graph fingerprints and
step hashes of qwen4exp_fp8 move (re-pinned). place_test: the adaptive turnover. r4d_selftest: the
`blocks` param of prefetch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Code000andClaude Opus 5.5 authored and StillDeadcode committed 2026-11-17 20:33:07 +01:00
1 parent 946a6f9e2b
commit ca7a7212ec
20 files changed
+598 -44

No files matched your search

+22 -1
View File
@@ -676,8 +676,13 @@ inline void prefetch_beside(RadCtx* c, rad_op op, rad_weight w, int64_t T) {
}
/* The `prefetch` of `w`'s first `bytes` where a library serves it; 0 otherwise. A plugin declares
* none where the deployment turns the reads off (RadBuildCtx::weight_prefetch_off). */
inline rad_op decl_prefetch(RadBuilder* b, rad_weight w, int64_t bytes) {
inline rad_op decl_prefetch(RadBuilder* b, rad_weight w, int64_t bytes, int64_t blocks = 0) {
if (!w || bytes <= 0 || !rad_op_declared(b, "prefetch")) return 0;
/* `blocks`: the workgroups reading, where the architecture measured its own count (docs/OPS.md);
* 0 leaves the kernel's. Stated only when asked, so a declaration without it is what it was. */
if (blocks > 0)
return RAD_OP(b, "prefetch", RAD_PARAMS(RAD_INT("bytes", bytes), RAD_INT("blocks", blocks)),
RAD_WEIGHTS(w));
return RAD_OP(b, "prefetch", RAD_PARAMS(RAD_INT("bytes", bytes)), RAD_WEIGHTS(w));
}
@@ -1482,6 +1487,22 @@ inline void batch_split(const RadBatch* b, int64_t* n_seq_dec, int64_t* n_tok_de
* always; no later layer issues a query, a score or a select. */
enum QsaUse : int { kQsaSelect = 0, kQsaUnread = 1, kQsaOnce = 2 };
/* Ops of a chain that is independent of a block's own, for the block to issue behind its ordered
* launches. A recorded pass lets a launch start beside the last ordered one when it touches
* nothing in flight (Ctx::haz_mark). A dependent chain issued in one piece is therefore a row of
* ordered launches, each paid in turn; issued one op behind each of the block's own ordered
* launches, every op of it is unordered behind the block's launch in front of it, and still
* behind its own predecessor, which that launch already waited for. The block calls `at(k)` for
* k = 0, 1, 2, ... behind its ordered launches in turn, and the owner issues what is left once
* the block returns. The chain must read nothing the block writes and write nothing the block
* touches, and its buffers must be excluded from sharing (rad_buf_concurrent): they are in use at
* launches the linear order puts after their last declared use. */
struct SideOps {
void (*fn)(void* u, int stage) = nullptr;
void* u = nullptr;
void at(int stage) const { if (fn) fn(u, stage); }
};
/* RADIANCE_DEBUG_QSA_EAGER: every step is kQsaSelect, the issue sequence before the forms above
* existed. An instrument for comparing the two (the text must not move); read once. */
inline bool qsa_eager() {
+33 -6
View File
@@ -126,6 +126,14 @@ struct AttnGatedFP8 {
* (decl_prefetch, rad_arch.h), issued behind the q, k and v projections on decode steps
* (prefetch_beside). 0 where none is declared. */
rad_op op_pf_o = 0;
/* The same read as a second op for steps of at least pf_o_v_rows rows, where the
* architecture measured another workgroup count for them (decl_prefetch's `blocks`); 0 where
* every step takes op_pf_o. */
rad_op op_pf_o_v = 0;
int64_t pf_o_v_rows = 0;
/* The most rows of a decode step whose k and v projections are issued in front of the q|gate
* projection (step()); 0, the default, keeps the declared order on every step. */
int64_t kv_first_rows = 0;
NormQuantFP8 nq_h{};
rad_op op_fused = 0; /* qk_norm_rope_gate, if it resolves */
@@ -183,8 +191,12 @@ struct AttnGatedFP8 {
/* The block's tail, declared last by every rank: the all-reduce and the trailing add. */
int declare_out(RadBuilder* b, bool add_out);
/* `own_ar` false: the caller issues the all-reduce itself (GdnFP8::step says when). */
/* `side`: an independent chain's ops, issued behind this block's ordered launches (SideOps,
* rad_arch.h): one behind the prologue and the store, one behind the attention, one behind
* the gate where the gate is a launch of its own. Null, and on the paths that return early,
* nothing is asked of it. */
void step(RadCtx* c, const RadBatch* batch, bool own_ar = true,
ArPieces* pieces = nullptr) const;
ArPieces* pieces = nullptr, const SideOps* side = nullptr) const;
bool issues_ar(int64_t T) const { return op_ar && !ar_taken(g, T, ar_out, ar_out_take); }
void issue_ar(RadCtx* c, int64_t r0, int64_t rows) const {
RAD_ISSUE_N(c, op_ar_ov ? op_ar_ov : op_ar, rows * g.n_embd,
@@ -621,7 +633,7 @@ inline void AttnGatedFP8::store_kv(RadCtx* c, const RadBatch* batch) const {
}
inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
ArPieces* pieces) const {
ArPieces* pieces, const SideOps* side) const {
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
const int32_t* slot = kvb ? kvb->slot_mapping : nullptr;
const int32_t* table = kvb ? kvb->block_table : nullptr;
@@ -683,11 +695,23 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
if (op_add) RAD_ISSUE(c, op_add, brows(w.x, T), brows(w.h.x, T), brows(w.x, T));
return;
}
qg.step(c, w.h, w.qg, T);
kp.step(c, w.h, w.k, T);
vp.step(c, w.h, w.v, T);
/* The three projections read the same input and none reads another's output, so a recorded
* pass starts them together (Ctx::haz_mark) whatever their order; the order decides which
* the card launches first. On a decode step of up to `kv_first_rows` rows the two small ones
* go first: behind the wide q|gate projection their launches wait until its blocks are all
* on the card, and the prologue then waits for them. Qwen3.8-Flash-Next at tp 2, chain 3
* with coupling (twelve trunk layers and the head's): -0.66% a step. */
if (T <= kv_first_rows && batch->phase == RAD_PHASE_DECODE) {
kp.step(c, w.h, w.k, T);
vp.step(c, w.h, w.v, T);
qg.step(c, w.h, w.qg, T);
} else {
qg.step(c, w.h, w.qg, T);
kp.step(c, w.h, w.k, T);
vp.step(c, w.h, w.v, T);
}
/* The out projection's weight, read beside the prologue and the attention (op_pf_o). */
prefetch_beside(c, op_pf_o, o.w, T);
prefetch_beside(c, op_pf_o_v && T >= pf_o_v_rows ? op_pf_o_v : op_pf_o, o.w, T);
/* Fused or unfused prologue. The fused kernel takes the whole [q|gate] projection and splits it
* itself, and rotates k in place like the unfused ops. It reads one rotary component per
@@ -732,6 +756,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
}
if (!fused_kv && !folded_stream) store_cache(c, kvb, slot, T);
if (side) side->at(0);
/* Sparse attention issues one row per query. `attn_paged` indexes its block table and length
* by row, and a row need not be a sequence, so T queries issued as T rows of q_len 1, each with
@@ -900,6 +925,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
/* Every attention launch of this layer is issued: start the next layer's stage copy, which
* crosses the link under the out projection and the next layer's projections. */
if (kvb && kvb->stream) kv_stage_next(c, kv, layer);
if (side) side->at(1);
if (gated) {
/* the sparse attention gated and quantised in its merge */
} else if (op_sig) {
@@ -913,6 +939,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
bcol(w.qg, g.head_dim, g.head_dim, T), brows(w.attn.x, T),
brows(w.attn.cq(), T), brows(w.attn.cs(), T), brows(w.attn.x, T));
}
if (side && !gated) side->at(2);
o.step(c, w.attn, w.h.x, T);
if (own_ar && op_ar && !ar_taken(g, T, ar_out, ar_out_take))
+8 -2
View File
@@ -134,6 +134,11 @@ struct GdnFP8 {
* pf_out_rows rows and pf_out_seqs sequences. 0 where none is declared. */
rad_op op_pf_out = 0;
int64_t pf_out_rows = 0, pf_out_seqs = 0;
/* The same read as a second op for steps of at least pf_out_v_rows rows, where the
* architecture measured another workgroup count for them (decl_prefetch's `blocks`); 0 where
* every step takes op_pf_out. */
rad_op op_pf_out_v = 0;
int64_t pf_out_v_rows = 0;
rad_op op_ar = 0, op_add = 0;
/* The next block absorbs this block's closing all-reduce (see rad_block_attn_gated_fp8.h). */
bool ar_out = false;
@@ -508,11 +513,12 @@ inline void GdnFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
* holds nothing behind it), else on the second lane behind a join from lane 0 (as
* prefetch_beside). */
if (op_pf_out && D > 0 && D <= pf_out_seqs && P == 0 && T <= pf_out_rows) {
const rad_op pf = op_pf_out_v && T >= pf_out_v_rows ? op_pf_out_v : op_pf_out;
if (prefetch_lane0()) {
RAD_ISSUE(c, op_pf_out, RAD_W(out.w));
RAD_ISSUE(c, pf, RAD_W(out.w));
} else {
if (rad_lane_join(c, 0, 1) == RAD_OK && rad_lane(c, 1) == RAD_OK)
RAD_ISSUE(c, op_pf_out, RAD_W(out.w));
RAD_ISSUE(c, pf, RAD_W(out.w));
rad_lane(c, 0);
}
}
+18 -1
View File
@@ -388,6 +388,10 @@ struct MoeFP8 {
* which the shared arm's first stage then runs beside the router instead (pass()). */
bool gu_trellis = false;
static constexpr int64_t kSharedFirstRows = 100;
/* The steps, by their rows [lo, hi], whose shared down projection is issued in front of the
* routed down GEMM instead of behind it (pass()); hi 0, the default, keeps it behind on
* every step. */
int64_t sh_down_first_lo = 0, sh_down_first_hi = 0;
LinearFP8 sh_gate_up{}, sh_down{};
rad_op op_router = 0, op_topk = 0, op_scatter = 0;
/* `op_scatter` is moe_scatter_inv and `op_gather` moe_gather_inv: the sort writes `w.inv` and
@@ -1942,6 +1946,19 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows, bool o
(rfused && router_fused_gate) || (sfused && sigmoid_scatter_gate));
gq_exp.step(c_, w.egu, rows * k, 0, rows * k);
if (shared && !(op_sh_gated && rows <= sh_gated_rows)) gq_sh.step(c_, w.sgu, T, r0, rows);
/* The shared arm's down projection in front of the routed down GEMM on a step of
* [sh_down_first_lo, sh_down_first_hi] rows. Behind it (below) the two still run together,
* but the card launches the arm only once every block of the routed GEMM is on it; at a
* verify step's forty routed rows that is a few microseconds before that GEMM ends, and the
* gather then waits for the arm. In front, the arm is the ordered launch behind the two
* quantisers and the routed GEMM starts beside it (it reads nothing the arm writes): the same
* start for the routed GEMM, and the arm is done long before it. At one row the routed GEMM
* is a quarter of the blocks, the arm behind it is launched in time, and in front it only
* delays that GEMM's launch (+0.38% a step), hence the lower bound. The arm's buffers are
* excluded from sharing (declare), so the order is free. Qwen3.8-Flash-Next at tp 2 with
* coupling, chain 1 / 2 / 3 (two / three / four rows): -0.28% / -0.21% / -0.25% a step. */
const bool sh_dn_first = shared && T >= sh_down_first_lo && T <= sh_down_first_hi;
if (sh_dn_first) sh_down.step(c_, w.sff, w.sout, T, r0, rows);
if (c.expert_bf16)
RAD_ISSUE_N(c_, op_dn, rows, brows(w.eff.x, rows * k), RAD_WTAB(w_dn[0], ne),
brows(w.sorted, rows * k), brows(w.eoff, ne + 1), brows(w.edn, rows * k));
@@ -1976,7 +1993,7 @@ inline void MoeFP8::pass(RadCtx* c_, int64_t T, int64_t r0, int64_t rows, bool o
RAD_ISSUE_N(c_, op_pdn, rows, brows(pff.x, rows * k), RAD_WTAB(w_pdn[0], np),
brows(w.sorted, rows * k), poff, brows(w.edn, rows * k));
}
if (shared) sh_down.step(c_, w.sff, w.sout, T, r0, rows);
if (shared && !sh_dn_first) sh_down.step(c_, w.sff, w.sout, T, r0, rows);
/* The calibration tap reads the GEMMs' operands and writes nothing they touch, so it does not
* change the model's output. */
if (op_gram_gu && !calib_done) {
+43 -16
View File
@@ -135,7 +135,30 @@ struct QsaIndexer {
/* Stores the pass's block keys and tail exactly as step() does, with no query, score or
* selection (see AttnGatedFP8::store_kv). */
void store(RadCtx* c, const ActFP8& in, const RadBatch* batch) const;
bool store_keys(RadCtx* c, const RadBatch* batch) const;
/* `part` 0 issues all of it; 1, 2 and 3 its three stages apart and in that order (the work
* list, the block keys, the tail store), for a caller that issues them behind another block's
* launches (deferrable, below). */
bool store_keys(RadCtx* c, const RadBatch* batch, int part = 0) const;
/* Whether this step's keys and tail may be issued behind the attention block's launches
* instead of in front of them (SideOps, rad_arch.h): nothing reads a selection of this
* layer's (`use` as step() resolves it), so the attention reads nothing the three ops write.
* The caller answers for the buffers (Wire: qk, stage, page, bpos, nc excluded from
* sharing). */
bool deferrable(const RadBatch* batch, int use, bool first) const {
if (!on() || qsa_eager()) return false;
/* step() stores no keys for a batch without the attention's group; leave that to it. */
if (!kv_batch(batch, c.kv_attn)) return false;
if (use == kQsaOnce) use = first ? kQsaSelect : kQsaUnread;
return use == kQsaUnread;
}
/* What step() issues on a deferrable step, a piece at a time: the projection, then
* store_keys's stages 1..3. */
void step_proj(RadCtx* c, const ActFP8& in, const RadBatch* batch) const {
proj.step(c, in, w.qk, batch->n_tok);
}
void step_part(RadCtx* c, const RadBatch* batch, int part) const {
(void)store_keys(c, batch, part);
}
};
inline int QsaIndexer::declare(RadBuilder* b, Names& nm, const Geom& geom, int l,
@@ -275,7 +298,7 @@ inline int QsaIndexer::declare(RadBuilder* b, Names& nm, const Geom& geom, int l
/* Stores the block keys of the blocks the pass completes and the tail of raw keys after them,
* from the projection's raw-key head. False when the step has no batch for the indexer's
* groups. */
inline bool QsaIndexer::store_keys(RadCtx* c_, const RadBatch* batch) const {
inline bool QsaIndexer::store_keys(RadCtx* c_, const RadBatch* batch, int part) const {
const int64_t T = batch->n_tok, S = batch->n_seq;
const int64_t nw = (c.heads + 1) * c.head_dim;
const int64_t koff = c.heads * c.head_dim; /* the raw-key head, after the query heads */
@@ -297,20 +320,24 @@ inline bool QsaIndexer::store_keys(RadCtx* c_, const RadBatch* batch) const {
const bool mc = g.rope_mc && batch->rope_pos;
RadOperand bpos = g.rope_mc ? RAD_B_COL(w.bpos, 0, mc ? 3 : 1) : brows(w.bpos, work);
if (g.rope_mc) bpos.rows = work;
RAD_ISSUE_N(c_, op_work, T,
bcol_at(w.qk, 0, nw, koff, c.head_dim, T),
praw(batch->positions, RAD_I32, T),
praw(batch->cu_seqlens, RAD_I32, S + 1),
kv_cache(c.kv_tail, layer),
praw2(tlb->state_index, RAD_I32, S, tw),
praw2(bkb->block_table, RAD_I32, S, bkb->block_table_pitch),
mc ? praw2(batch->rope_pos, RAD_I32, 3, T) : RAD_NONE,
brows(w.stage, work * c.ratio), brows(w.page, work), bpos,
brows(w.nc, S));
RAD_ISSUE_N(c_, op_bkey, work,
brows(w.stage, work * c.ratio), RAD_W(w_kn),
brows(w.page, work), bpos,
kv_cache(kv_bk, layer));
if (part <= 1)
RAD_ISSUE_N(c_, op_work, T,
bcol_at(w.qk, 0, nw, koff, c.head_dim, T),
praw(batch->positions, RAD_I32, T),
praw(batch->cu_seqlens, RAD_I32, S + 1),
kv_cache(c.kv_tail, layer),
praw2(tlb->state_index, RAD_I32, S, tw),
praw2(bkb->block_table, RAD_I32, S, bkb->block_table_pitch),
mc ? praw2(batch->rope_pos, RAD_I32, 3, T) : RAD_NONE,
brows(w.stage, work * c.ratio), brows(w.page, work), bpos,
brows(w.nc, S));
if (part == 1) return true;
if (part == 0 || part == 2)
RAD_ISSUE_N(c_, op_bkey, work,
brows(w.stage, work * c.ratio), RAD_W(w_kn),
brows(w.page, work), bpos,
kv_cache(kv_bk, layer));
if (part == 2) return true;
RAD_ISSUE_N(c_, op_tail, T,
bcol_at(w.qk, 0, nw, koff, c.head_dim, T),
praw(batch->positions, RAD_I32, T),
+108 -2
View File
@@ -114,11 +114,35 @@ using namespace rad::arch;
* and 13.17): a read here pays ~2 us of launch for what it saves. */
constexpr int64_t kGdnOutPrefetchBytes = (int64_t)8 << 20;
constexpr int64_t kGdnOutPrefetchSeqs = 4;
/* The rows from which a decode step is taken for a speculative verify, a step of several rows:
* where the shared arm's place and the reads' workgroup count below help, against the one row of
* a plain decode step, where both cost. ms a step at tp 2 with coupling, in one server with the
* rounds interleaved, at two / three / four rows (chain 1 / 2 / 3) and at one: the arm's down
* projection in front -0.28% / -0.21% / -0.25% and +0.38%; the reads at 32 workgroups -0.65% /
* -0.35% / -0.31% and +0.41%. */
constexpr int64_t kVerifyRows = 2;
/* A full-attention layer's out projection likewise, read behind its q, k and v projections
* (AttnGatedFP8::op_pf_o): the prologue and the attention between them and the projection are
* short launches with the memory idle. Twelve layers, so less: five interleaved rounds, ms a step
* without -> with, one stream 12.51 -> 12.45 (every round), two 16.88 -> 16.85. */
constexpr int64_t kAttnOutPrefetchBytes = (int64_t)8 << 20;
/* The workgroups of this model's weight reads on a verify step (the `prefetch` op's `blocks`, from
* kVerifyRows rows): 32, half the kernel's own 64. The reads run beside the delta net's update,
* the attention's prologue and the connections' reductions, all latency-bound kernels of a few
* loads a block whose loads queue behind the read's, and at four rows the update lasts half again
* as long as at one, so a slower read still lands. Chain 3 with coupling at tp 2, ms a step
* against 64 in one server, interleaved: 16 +0.77%, 24 +0.14%, 32 -0.31%, 48 -0.24%, 128 +2.0%,
* 256 +7.4%; -0.6% at 32 with the router's read beside. A one-row step keeps 64 (kVerifyRows). */
constexpr int64_t kPrefetchBlocksVerify = 32;
/* A layer's router likewise, read behind its mixer's out projection, beside the mixer's reduction
* and the feed-forward's connection read (both latency, with the card's memory mostly idle): the
* router is 2.5 MB of bf16 that its GEMM otherwise streams cold in front of a top-k that waits for
* all of it. Chain 3 with coupling at tp 2, ms a step without -> with, each the mean of two
* interleaved rounds in one server: 12.19 -> 12.13 (-0.42% and -0.93%); 12.14 -> 12.09 in another
* session. The same read of the shared expert's two weights, or of the indexer's projection, moved
* nothing (-0.02%, -0.04%, +0.04%), and of the mixer's first projection in front of its
* connection read cost 1.0%: a read there takes the memory from the connection's own. */
constexpr int64_t kRouterPrefetchBytes = (int64_t)4 << 20;
struct Layer {
int full = 0;
@@ -136,6 +160,11 @@ struct Layer {
/* The layer's calibration inputs (rad_calib_full.h, site_i8), -1 where this run records none:
* the mixer's input (the delta net's in projection, or q, k and v) and its output projection's. */
int cs_in = -1, cs_o = -1;
/* The router's weight read beside the mixer's reduction (kRouterPrefetchBytes); 0 where the
* deployment reads no weights ahead. */
rad_op op_pf_router = 0;
/* The same at a verify step's workgroup count (kPrefetchBlocksVerify), from kVerifyRows rows. */
rad_op op_pf_router_v = 0;
};
struct Model {
@@ -235,6 +264,9 @@ struct Model {
rad_weight w_tok = 0, w_lm_head = 0;
rad_op op_embed = 0, op_embed_ar = 0, op_rope_cs = 0, op_gather = 0, op_logits = 0;
/* The indexer's within-step buffers store_keys touches are excluded from sharing (declare),
* so a layer's keys may be issued behind its attention's launches (step_serial, QsaSide). */
bool qsa_defer = false;
/* ---- media (arch/common/rad_block_vit.h): the vision tower and the scatter of its rows over
* the token embeddings, declared only when the container carries a tower and the deployment
@@ -884,6 +916,18 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
{ g.max_tok, m.qsacfg.topk + 1 });
m.qsaw.nsel = decl_b(b, nm.f("qsa_nsel"), RAD_I32, { g.max_tok });
m.qsaw.sequ = decl_b(b, nm.f("qsa_sequ"), RAD_I32, { g.max_tok });
if (m.qsaw.qk && m.qsaw.stage && m.qsaw.page && m.qsaw.bpos && m.qsaw.nc) {
/* What store_keys reads and writes is in use behind the attention's launches when a
* layer's keys are deferred behind them (QsaSide), past the last use the linear order
* gives these buffers, so the planner must not lay an attention buffer over them.
* A few MiB of arena (qk is the largest, max_tok x 640 bf16). */
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.qk));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.stage));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.page));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.bpos));
RAD_ARCH_TRY(rad_buf_concurrent(b, m.qsaw.nc));
m.qsa_defer = true;
}
if (!m.qsaw.qk || !m.qsaw.q || !m.qsaw.stage || !m.qsaw.page ||
!m.qsaw.bpos || !m.qsaw.nc || !m.qsaw.score || !m.qsaw.sel || !m.qsaw.nsel ||
!m.qsaw.sequ)
@@ -1325,6 +1369,20 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
RAD_ARCH_TRY(m.mixer.declare(b, nm, g, nm.f("output_hc"), mc, mixw, mixs));
}
/* ---- the issue order of a decode step's small launches (AttnGatedFP8::kv_first_rows,
* MoeFP8::sh_down_first_lo): at decode and verify heights the k and v projections go in
* front of the q|gate projection, and from kVerifyRows rows the shared down projection in
* front of the routed down GEMM, so the card launches the small ones while the wide one is
* still being placed. The same kernels on the same operands; only what a recorded pass
* starts first changes. */
{
for (Layer& lay : m.layers) {
if (lay.full) lay.attn.kv_first_rows = kPrefetchRows;
lay.mlp.sh_down_first_lo = kVerifyRows;
lay.mlp.sh_down_first_hi = kPrefetchRows;
}
}
/* ---- the delta nets' out projections, read beside their decode recurrent updates
* (kGdnOutPrefetchBytes), and the full-attention layers' beside the attention
* (kAttnOutPrefetchBytes). At more than one rank; --weight-prefetch off declares none. */
@@ -1332,11 +1390,23 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
for (Layer& lay : m.layers) {
if (lay.full) {
lay.attn.op_pf_o = decl_prefetch(b, lay.attn.o.w, kAttnOutPrefetchBytes);
lay.attn.op_pf_o_v = decl_prefetch(b, lay.attn.o.w, kAttnOutPrefetchBytes,
kPrefetchBlocksVerify);
lay.attn.pf_o_v_rows = kVerifyRows;
continue;
}
lay.gdn.op_pf_out = decl_prefetch(b, lay.gdn.out.w, kGdnOutPrefetchBytes);
lay.gdn.pf_out_rows = kPrefetchRows;
lay.gdn.pf_out_seqs = kGdnOutPrefetchSeqs;
lay.gdn.op_pf_out_v = decl_prefetch(b, lay.gdn.out.w, kGdnOutPrefetchBytes,
kPrefetchBlocksVerify);
lay.gdn.pf_out_v_rows = kVerifyRows;
}
for (Layer& lay : m.layers) {
if (!lay.mlp.w_router) continue;
lay.op_pf_router = decl_prefetch(b, lay.mlp.w_router, kRouterPrefetchBytes);
lay.op_pf_router_v = decl_prefetch(b, lay.mlp.w_router, kRouterPrefetchBytes,
kPrefetchBlocksVerify);
}
}
@@ -1486,6 +1556,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
/* Set before declare(), as for the trunk: `attn_paged` declares `sel` and `seqused` as
* reads, which must precede the op. */
m.mtp.attn.kv_block = 4;
/* The head's layer takes the trunk's issue order at decode heights (declare, above). */
m.mtp.attn.kv_first_rows = kPrefetchRows;
m.mtp.mlp.sh_down_first_lo = kVerifyRows;
m.mtp.mlp.sh_down_first_hi = kPrefetchRows;
if (m.qsaw.qk) {
m.mtp.attn.qsa_sel = m.qsaw.sel;
m.mtp.attn.qsa_nsel = m.qsaw.nsel;
@@ -1737,6 +1811,18 @@ static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int
}
}
/* A full-attention layer's indexer keys behind its attention's launches (QsaIndexer::deferrable,
* SideOps): store_keys's three stages in turn, one a call, and whatever is left at flush(). */
struct QsaSide {
RadCtx* c = nullptr;
const QsaIndexer* q = nullptr;
const RadBatch* batch = nullptr;
int next = 1;
void one() { if (next <= 3) q->step_part(c, batch, next++); }
void flush() { while (next <= 3) one(); }
static void at(void* u, int) { ((QsaSide*)u)->one(); }
};
/* The trunk one stream at a time over the whole step: every step a span of 1 makes, and every
* other where step_pipelined's conditions do not hold. */
static void step_serial(RadCtx* c, const RadBatch* batch, Model& m, const int32_t* pcu) {
@@ -1771,12 +1857,32 @@ static void step_serial(RadCtx* c, const RadBatch* batch, Model& m, const int32_
/* The indexer runs first, since the attention reads its selection. The block's input is
* already normed and quantised by the connection (`ext_in`), so `w.h` is final. */
if (l.full) {
l.qsa.step(c, l.attn.w.h, batch, pcu, l.attn.qsa_use(batch), qsa_first);
const int use = l.attn.qsa_use(batch);
/* At decode heights within the exactness bound nothing of the layer reads the
* indexer's keys or tail (they are for later steps), so only the projection stays in
* front, beside the attention's own, and the three launches behind it ride behind the
* attention's (QsaSide) instead of holding its projections back in turn: -0.3% to
* -0.8% a step on chain 3 with coupling at tp 2. */
if (m.qsa_defer && T <= kPrefetchRows && batch->phase == RAD_PHASE_DECODE &&
l.qsa.deferrable(batch, use, qsa_first)) {
QsaSide sd{c, &l.qsa, batch};
const SideOps so{QsaSide::at, &sd};
l.qsa.step_proj(c, l.attn.w.h, batch);
l.attn.step(c, batch, true, nullptr, &so);
sd.flush();
} else {
l.qsa.step(c, l.attn.w.h, batch, pcu, use, qsa_first);
l.attn.step(c, batch);
}
if (l.qsa.on()) qsa_first = false;
l.attn.step(c, batch);
}
else l.gdn.step(c, batch);
if (l.cs_o >= 0) m.calib.tap(c, (size_t)l.cs_o, T);
/* The router's weight, read beside the write's reduction and the read behind it. */
if (batch->phase == RAD_PHASE_DECODE)
prefetch_beside(c, l.op_pf_router_v && T >= kVerifyRows ? l.op_pf_router_v
: l.op_pf_router,
l.mlp.w_router, T);
l.hc_mix.write(c, T, 0, T);
dbg_resid(c, (int)li, "mix", g.n_embd, m.b_h, m.a_x.x);
l.hc_ffn.read_pieces(c, T);