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

+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);