Archived
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:
1 parent
946a6f9e2b
commit
ca7a7212ec
20 files changed
+598
-44
No files matched your search
+22
-1
@@ -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() {
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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),
|
||||
|
||||
Reference in new issue
Block a user