Archived
qsa: skip score/select where no attention reads them; select once per pass below the exact bound
Architecture-only (no kernel, op schema or graph change). The attention block tells the indexer whether it reads the selection (AttnGatedFP8::qsa_use): dense steps (<= 1024 tokens of context) issue no query norm, qsa_score or qsa_select; single-row-per-sequence steps under the exact bound read the block table; multi-row steps under the bound select once per pass (the selection is layer-invariant there, every complete block in block order) and the other QSA layers reuse it. Block keys and tail run every step. The exact host bound (RadBatch::kv_used_max_h) decides which side of the bound a step is on and keys the pass. RADIANCE_DEBUG_QSA_EAGER=1 restores the old issue pattern. arch_test: the pinned step hashes of qwen4exp_fp8 move (graphs unchanged) and two tests pin the new issue pattern; ref_test pins the equalities it rests on (qsa_select below the bound). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
315aea7db9
commit
cfec01ec8d
8 files changed
+515
-30
No files matched your search
@@ -1466,6 +1466,29 @@ inline void batch_split(const RadBatch* b, int64_t* n_seq_dec, int64_t* n_tok_de
|
||||
}
|
||||
}
|
||||
|
||||
/* What a step's attention reads of the QSA selection (AttnGatedFP8::qsa_use), which decides what
|
||||
* the indexer issues for the layer (QsaIndexer::step, docs/QSA.md "Below the bound"):
|
||||
*
|
||||
* kQsaSelect score and select, per layer: a step past the exactness bound, a streamed group,
|
||||
* and every step of a block that did not ask for the forms below;
|
||||
* kQsaUnread nothing reads the selection: the step is entirely at or below the bound and its
|
||||
* attention is the dense one, or the sparse row over the sequence's own block table
|
||||
* (one row a sequence), which is what the selection would be. The indexer stores its
|
||||
* keys and tail and issues no query, score or select;
|
||||
* kQsaOnce the attention reads a selection (the sparse row a query) on a step entirely at or
|
||||
* below the bound: every complete block is taken whatever it scores, so the
|
||||
* selection depends on the positions and the block table alone and is the same
|
||||
* bytes at every layer. The first full-attention layer of the pass issues it as
|
||||
* always; no later layer issues a query, a score or a select. */
|
||||
enum QsaUse : int { kQsaSelect = 0, kQsaUnread = 1, kQsaOnce = 2 };
|
||||
|
||||
/* 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() {
|
||||
static const bool on = std::getenv("RADIANCE_DEBUG_QSA_EAGER") != nullptr;
|
||||
return on;
|
||||
}
|
||||
|
||||
/* The step batch's entry for one declared KV group (built once per step by the core, spec §8). */
|
||||
inline const RadKVGroupBatch* kv_batch(const RadBatch* b, rad_kvgroup group) {
|
||||
for (int i = 0; i < b->n_kv_groups; ++i)
|
||||
|
||||
@@ -79,6 +79,21 @@ struct AttnGatedFP8 {
|
||||
/* `sel`'s row pitch is `topk + 1`; the extra slot is the partial page
|
||||
* (r4d_qsa_select_bf16.hip). */
|
||||
int64_t qsa_topk = 0;
|
||||
/* The architecture's word that a sequence with one row in the step has that row at its
|
||||
* context's last position (`seqused - 1`: a plain decode row, a serial head's round), so at or
|
||||
* below `qsa_exact_to` such a row's selection is the head of the sequence's block table and its
|
||||
* `seqused` the sequence's (qsa_select: every complete block in block order, then the partial
|
||||
* page, and `pos + 1`). The sparse row is then issued over the table and the indexer selects
|
||||
* nothing (qsa_use). Off: such a step keeps the selection. */
|
||||
bool qsa_table_rows = false;
|
||||
/* The architecture's pass key carries whether this group's exact host bound
|
||||
* (RadBatch::kv_used_max_h, the furthest context any row of the step can read) is within
|
||||
* `qsa_exact_to`, so that bound may say whether the step needs scores (qsa_within) in place of
|
||||
* the rounded `max_ctx_len + max_q_len`, which a 4-row step leaves at 1025 tokens. It never
|
||||
* moves the choice between the dense and the sparse kernel, which stays the rounded bound's:
|
||||
* the two agree on the keys, not on the last bit of the sum. Off: the rounded bound, which the
|
||||
* core's key holds. */
|
||||
bool qsa_exact_host = false;
|
||||
/* Query rows per sparse attention launch, a scratch budget. The decode kernel's split-KV
|
||||
* partials are sized at declare from `max_seqs` and `q_len`, so issuing more rows than
|
||||
* `max_seqs` means declaring a larger value, which grows the scratch arena (the maximum over
|
||||
@@ -185,6 +200,41 @@ struct AttnGatedFP8 {
|
||||
* components agree, so plane 0 of an M-RoPE batch's positions is each token's position) and
|
||||
* the gate quantised by gate_quant_fp8. A streamed group never takes it: the pieces path's
|
||||
* plain store never writes the host mirror. */
|
||||
/* The rounded host bound of the furthest context a row of the step reads; by it a step of
|
||||
* several rows a sequence takes the dense or the sparse kernel (step(), `sparse`). */
|
||||
int64_t qsa_reach(const RadBatch* batch) const {
|
||||
return (int64_t)batch->max_ctx_len + batch->max_q_len;
|
||||
}
|
||||
/* Whether every row of the step is at or below the exactness bound, by the tightest host
|
||||
* ceiling there is: the group's exact one where the pass key holds its side of the bound
|
||||
* (qsa_exact_host), else the rounded one. Every row's position + 1 is at most either. */
|
||||
bool qsa_within(const RadBatch* batch, const RadKVGroupBatch* kvb) const {
|
||||
if (qsa_exact_to <= 0) return false;
|
||||
int64_t reach = qsa_reach(batch);
|
||||
if (qsa_exact_host && kvb && batch->kv && batch->kv_used_max_h) {
|
||||
const int64_t tight = batch->kv_used_max_h[kvb - batch->kv];
|
||||
if (tight > 0 && tight < reach) reach = tight;
|
||||
}
|
||||
return reach <= qsa_exact_to;
|
||||
}
|
||||
/* What this step's attention reads of the selection (QsaUse, rad_arch.h); the architecture
|
||||
* hands it to the indexer, which runs first, and step() takes its path by the same call. */
|
||||
int qsa_use(const RadBatch* batch) const {
|
||||
if (!qsa_sel || !qsa_sequ || op_fill || qsa_eager()) return kQsaSelect;
|
||||
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
|
||||
/* A streamed group may be ring-bound, where every pass is sparse and the resolve rewrites
|
||||
* the selection; it keeps the selection at every context. */
|
||||
if (!kvb || kvb->stream || !kvb->block_table || !kvb->seqused) return kQsaSelect;
|
||||
if (!qsa_within(batch, kvb)) return kQsaSelect;
|
||||
/* One row a sequence is the sparse row at every context: over the table where the
|
||||
* architecture vouches for it, else over a selection. */
|
||||
if (batch->n_tok == batch->n_seq)
|
||||
return (qsa_table_rows && batch->max_q_len == 1) ? kQsaUnread : kQsaOnce;
|
||||
/* Several rows a sequence: the dense attention, which reads the table, where the rounded
|
||||
* bound is within the budget too, else the sparse row a query over a selection. */
|
||||
return qsa_reach(batch) <= qsa_exact_to ? kQsaUnread : kQsaOnce;
|
||||
}
|
||||
|
||||
bool takes_pieces(const RadBatch* batch, const ArPieces* pieces) const {
|
||||
const int64_t T = batch->n_tok;
|
||||
const RadKVGroupBatch* kvb = kv_batch(batch, kv);
|
||||
@@ -694,7 +744,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
* Launches are chunked because the split-KV scratch is sized at declare from `max_seqs` and
|
||||
* `q_len` (rows = max_seqs * q_len_max * heads); more rows are refused as RAD_E_SCRATCH at
|
||||
* issue. Rows of q_len 1 up to the declared max_seqs always fit. */
|
||||
const int64_t reach = (int64_t)batch->max_ctx_len + batch->max_q_len;
|
||||
const int64_t reach = qsa_reach(batch);
|
||||
/* A pass on a streamed group's ring (a ring smaller than the id space, and not staged) must
|
||||
* take the sparse path: on the ring a block id is not a pool address, so the dense path would
|
||||
* read ring slots as pages. `T == n_seq` alone misses a verify pass, whose T is
|
||||
@@ -708,6 +758,19 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
const bool sparse = qsa_sel && qsa_sequ &&
|
||||
(ring_bound || T == batch->n_seq || qsa_exact_to <= 0 ||
|
||||
reach > qsa_exact_to);
|
||||
/* At or below the bound a step of one row a sequence has no selection (qsa_use): the sparse
|
||||
* row reads the sequence's own block table and length, which is what qsa_select would have
|
||||
* written for it (the field says why), so the launch, its kernel and its bytes are the sparse
|
||||
* row's. */
|
||||
const bool by_table = sparse && T == batch->n_seq && qsa_use(batch) == kQsaUnread;
|
||||
const int64_t tpitch = kvb ? kvb->block_table_pitch : 0;
|
||||
auto sel_rows = [&](int64_t off, int64_t rows) {
|
||||
return by_table ? praw2(table + off * tpitch, RAD_I32, rows, tpitch)
|
||||
: brow_slice(qsa_sel, off, rows, qsa_topk + 1);
|
||||
};
|
||||
auto sequ_rows = [&](int64_t off, int64_t rows) {
|
||||
return by_table ? praw(used + off, RAD_I32, rows) : brow_slice(qsa_sequ, off, rows, 1);
|
||||
};
|
||||
const bool gated = sparse && op_attn_gq;
|
||||
/* A streamed group's selection, per launch: make the rows' blocks resident and rewrite `sel`
|
||||
* as ring slots, after this layer's store (a block copied in from the mirror must carry this
|
||||
@@ -715,6 +778,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
* and never resolves, so a prefill cannot overflow the ring. */
|
||||
const KVStreamLayer res_l = op_resolve ? kv_stream_layer(c, kv, layer, kvb) : KVStreamLayer{};
|
||||
auto resolve_sel = [&](int64_t off, int64_t rows) {
|
||||
if (by_table) return; /* never a streamed group (qsa_use) */
|
||||
if (kv_stage_layer(c, kv, kvb, layer)) return;
|
||||
if (!res_l.host || res_l.blocks <= 0 || res_l.slots <= 0 || rows <= 0) return;
|
||||
kv_resolve_issue(c, op_resolve, res_l, brow_slice(qsa_sel, off, rows, qsa_topk + 1),
|
||||
@@ -736,8 +800,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
RAD_ISSUE_N(c, op_attn_gq, 1,
|
||||
brow_slice(w.q, off, rows, qw),
|
||||
kv_cache(kv, layer),
|
||||
brow_slice(qsa_sel, off, rows, qsa_topk + 1),
|
||||
brow_slice(qsa_sequ, off, rows, 1),
|
||||
sel_rows(off, rows), sequ_rows(off, rows),
|
||||
RAD_NONE, RAD_NONE, RAD_NONE,
|
||||
brow_slice(w.attn.x, off, rows, qw),
|
||||
bcol_at(w.qg, off, g.n_head * 2 * hd, hd, hd, rows),
|
||||
@@ -760,8 +823,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
RAD_ISSUE_N(c, op_attn_gq, 1,
|
||||
brow_slice(w.q, off, rows, qw),
|
||||
kv_cache(kv, layer),
|
||||
brow_slice(qsa_sel, off, rows, qsa_topk + 1),
|
||||
brow_slice(qsa_sequ, off, rows, 1),
|
||||
sel_rows(off, rows), sequ_rows(off, rows),
|
||||
RAD_NONE, RAD_NONE, RAD_NONE,
|
||||
brow_slice(w.attn.x, off, rows, qw),
|
||||
bcol_at(w.qg, off, g.n_head * 2 * hd, hd, hd, rows),
|
||||
@@ -777,8 +839,7 @@ inline void AttnGatedFP8::step(RadCtx* c, const RadBatch* batch, bool own_ar,
|
||||
RAD_ISSUE_N(c, op_attn, 1,
|
||||
brow_slice(w.q, off, rows, qw),
|
||||
kv_cache(kv, layer),
|
||||
brow_slice(qsa_sel, off, rows, qsa_topk + 1),
|
||||
brow_slice(qsa_sequ, off, rows, 1),
|
||||
sel_rows(off, rows), sequ_rows(off, rows),
|
||||
RAD_NONE, RAD_NONE, RAD_NONE,
|
||||
brow_slice(w.attn.x, off, rows, qw));
|
||||
}
|
||||
|
||||
@@ -552,7 +552,7 @@ inline void MtpHcBlock::step(RadCtx* c, const RadBatch* batch) const {
|
||||
|
||||
/* The indexer runs first because the attention reads its selection; `w.h` is already final,
|
||||
* written by the connection above. */
|
||||
qsa.step(c, attn.w.h, batch, qsa_win_cu);
|
||||
qsa.step(c, attn.w.h, batch, qsa_win_cu, attn.qsa_use(batch));
|
||||
attn.step(c, batch);
|
||||
|
||||
hc_attn.write(c, T, 0, T);
|
||||
|
||||
+37
-5
@@ -9,7 +9,8 @@
|
||||
* Contents: the projection and query path, the block-key path with its two states, and the score
|
||||
* and selection. Below `budget + ratio - 1` tokens of context every visible block is selected, so
|
||||
* the sparse path must reproduce the dense path's text byte for byte; that is the test for any
|
||||
* change here.
|
||||
* change here. A step entirely at or below that bound therefore needs no selection at all, and
|
||||
* step() issues only the block-key path for it (its `use`; docs/QSA.md, "Below the bound").
|
||||
*/
|
||||
#ifndef RAD_QSA_H
|
||||
#define RAD_QSA_H
|
||||
@@ -121,9 +122,16 @@ struct QsaIndexer {
|
||||
int declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const Config& cfg,
|
||||
const Wire& wire, const ActFP8& in, rad_kvgroup* bk);
|
||||
/* `win_cu`, device: {0, plane_rows}, the one sequence of a window of a step wider than the
|
||||
* score plane (Config::plane_rows); null where no step is. */
|
||||
* score plane (Config::plane_rows); null where no step is.
|
||||
*
|
||||
* `use` is what the layer's attention reads of the selection this step (QsaUse, rad_arch.h;
|
||||
* AttnGatedFP8::qsa_use): the default issues the query, the scores and the selection as every
|
||||
* step did before. `first` is whether this is the first full-attention layer the pass issues:
|
||||
* at or below the bound the selection is made there once and read by every layer after it (the
|
||||
* buffers are the model's, live across its layers, and nothing else writes them). The default
|
||||
* makes it at every layer. */
|
||||
void step(RadCtx* c, const ActFP8& in, const RadBatch* batch,
|
||||
const int32_t* win_cu = nullptr) const;
|
||||
const int32_t* win_cu = nullptr, int use = kQsaSelect, bool first = true) const;
|
||||
/* 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;
|
||||
@@ -319,13 +327,31 @@ inline void QsaIndexer::store(RadCtx* c_, const ActFP8& in, const RadBatch* batc
|
||||
}
|
||||
|
||||
inline void QsaIndexer::step(RadCtx* c_, const ActFP8& in, const RadBatch* batch,
|
||||
const int32_t* win_cu) const {
|
||||
const int32_t* win_cu, int use, bool first) const {
|
||||
if (!on()) return;
|
||||
const int64_t T = batch->n_tok, S = batch->n_seq;
|
||||
const int64_t qw = c.heads * c.head_dim;
|
||||
const int64_t nw = (c.heads + 1) * c.head_dim;
|
||||
|
||||
/* What the attention reads this step (QsaUse); anything asked of a step that cannot give it is
|
||||
* the full issue, which every reader accepts.
|
||||
*
|
||||
* kQsaOnce: the attention reads a selection on a step entirely at or below the exactness
|
||||
* bound. A row there has at most `topk` complete blocks, so qsa_select takes every one whose
|
||||
* key is not 0 -- every block with a page and a score that is a number, whatever the number --
|
||||
* and what it writes (the pages in block order, the partial page, their count, seqused) is a
|
||||
* function of the positions and the block table: the same bytes from every layer's scores. So
|
||||
* the pass's first full-attention layer issues the selection exactly as it always did, and
|
||||
* every later layer reads that one and issues none of its own: no query, no score. */
|
||||
if (use == kQsaOnce) use = first ? kQsaSelect : kQsaUnread;
|
||||
if (use != kQsaUnread) use = kQsaSelect;
|
||||
/* RADIANCE_DEBUG_QSA_EAGER: the issue sequence as it was, every layer its own. */
|
||||
if (qsa_eager()) use = kQsaSelect;
|
||||
|
||||
/* The projection always: its raw-key head is the block keys' and the tail's. */
|
||||
proj.step(c_, in, w.qk, T);
|
||||
/* The query heads are read by the score alone. */
|
||||
if (use == kQsaSelect) {
|
||||
/* One fused launch or `heads` + 1 unfused. The fused form takes the whole projection at its
|
||||
* row pitch and writes the query heads contiguous and rotated. `n_head` 0 is the k-only form:
|
||||
* no q_gate, q_w or q_out, and `gate_out` absent. `T <= qprep_rows` is a correctness bound
|
||||
@@ -342,8 +368,10 @@ inline void QsaIndexer::step(RadCtx* c_, const ActFP8& in, const RadBatch* batch
|
||||
bcol_at(w.q, 0, qw, h * c.head_dim, c.head_dim, T));
|
||||
RAD_ISSUE_N(c_, op_rope, T, brows(w.q, T), rope_posmc(g, batch, T));
|
||||
}
|
||||
}
|
||||
|
||||
/* The keys first: the selection below reads the blocks they complete. */
|
||||
/* The keys first: the selection below reads the blocks they complete. The stateful half runs
|
||||
* whatever the attention reads: later steps score the block keys and build on the tail. */
|
||||
const RadKVGroupBatch* bkb = kv_batch(batch, kv_bk);
|
||||
const RadKVGroupBatch* atb = kv_batch(batch, c.kv_attn);
|
||||
if (!atb || !store_keys(c_, batch)) return;
|
||||
@@ -352,6 +380,10 @@ inline void QsaIndexer::step(RadCtx* c_, const ActFP8& in, const RadBatch* batch
|
||||
* is a query row, so each query of a prefill chunk selects from the shared history, as the
|
||||
* checkpoint does. */
|
||||
if (!c.emit_sel) return;
|
||||
/* Nothing reads a selection of this layer's: the step is at or below the exactness bound and
|
||||
* its attention reads the block table, or the selection the pass's first full-attention layer
|
||||
* made (AttnGatedFP8::qsa_use). */
|
||||
if (use == kQsaUnread) return;
|
||||
|
||||
/* Narrow the score columns to the live history. Both kernels size their grid from the
|
||||
* operand's width (the live count is device data), so the full `max_ctx` width would make a
|
||||
|
||||
Reference in new issue
Block a user