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
|
||||
|
||||
@@ -1172,10 +1172,17 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
lay.attn.qsa_nsel = m.qsaw.nsel;
|
||||
lay.attn.qsa_sequ = m.qsaw.sequ;
|
||||
lay.attn.qsa_topk = m.qsacfg.topk;
|
||||
/* The exactness bound, below which prefill issues the cheaper dense attention.
|
||||
* Decode ignores it and always takes the sparse path, keeping that path exercised
|
||||
* on every step. */
|
||||
/* The exactness bound, at or below which a step of several rows a sequence
|
||||
* (prefill, a verify) issues the cheaper dense attention. Plain decode keeps the
|
||||
* sparse row's launch at every context, so that kernel is exercised on every
|
||||
* step. */
|
||||
lay.attn.qsa_exact_to = m.qsacfg.budget + m.qsacfg.ratio - 1;
|
||||
/* At or below that bound the indexer scores nothing (AttnGatedFP8::qsa_use): a
|
||||
* row alone in its sequence is at the context's last position on this model (a
|
||||
* plain decode row; the core's and the card's `seqused` is its position + 1), and
|
||||
* pass_key() carries the exact bound's side of it. */
|
||||
lay.attn.qsa_table_rows = true;
|
||||
lay.attn.qsa_exact_host = true;
|
||||
}
|
||||
RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, m.kv_full));
|
||||
/* An attention-zero rank keeps no attention cache: its block is a fill of zeros
|
||||
@@ -1485,6 +1492,10 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
|
||||
m.mtp.attn.qsa_sequ = m.qsaw.sequ;
|
||||
m.mtp.attn.qsa_topk = m.qsacfg.topk;
|
||||
m.mtp.attn.qsa_exact_to = m.qsacfg.budget + m.qsacfg.ratio - 1;
|
||||
/* As the trunk's layers: a round's row is the head's context's last position
|
||||
* (ADV_ROUND), and the history pass is one row only where it commits one. */
|
||||
m.mtp.attn.qsa_table_rows = true;
|
||||
m.mtp.attn.qsa_exact_host = true;
|
||||
ts.qsa = m.qsacfg;
|
||||
ts.qsa.mode = m.mtp.attn.mode;
|
||||
ts.qsa.qk_proj = nm.ckpt("%s.layers.0.self_attn.indexer.index_qk_proj.weight", mpre);
|
||||
@@ -1666,6 +1677,9 @@ static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int
|
||||
if (m.ple_layer == 0) m.ple.step(c, batch);
|
||||
for (int64_t p = 0; p < K; ++p) m.layers[0].hc_mix.read(c, T, r0(p), rn(p));
|
||||
}
|
||||
/* The pass's first full-attention layer with an indexer makes what every such layer shares of
|
||||
* its step (QsaIndexer::step, `first`). */
|
||||
bool qsa_first = true;
|
||||
for (int64_t li = 0; li < n; ++li) {
|
||||
const Layer& l = m.layers[(size_t)li];
|
||||
/* The mixer finishes its output a piece at a time where its last stages are per row and
|
||||
@@ -1674,7 +1688,11 @@ static void step_pipelined(RadCtx* c, const RadBatch* batch, Model& m, const int
|
||||
MixAr ms{c, &l};
|
||||
ArPieces pc{};
|
||||
pc.rows = R; pc.done = mix_ar_piece; pc.u = &ms; pc.cu = gcu;
|
||||
if (l.full) { l.qsa.step(c, l.attn.w.h, batch, pcu); l.attn.step(c, batch, false, &pc); }
|
||||
if (l.full) {
|
||||
l.qsa.step(c, l.attn.w.h, batch, pcu, l.attn.qsa_use(batch), qsa_first);
|
||||
if (l.qsa.on()) qsa_first = false;
|
||||
l.attn.step(c, batch, false, &pc);
|
||||
}
|
||||
else l.gdn.step(c, batch, false, &pc);
|
||||
auto ar_mix = [&](int64_t p) {
|
||||
if (l.full) l.attn.issue_ar(c, r0(p), rn(p));
|
||||
@@ -1739,6 +1757,9 @@ static void step_serial(RadCtx* c, const RadBatch* batch, Model& m, const int32_
|
||||
if (m.op_rope_cs && T <= qk_fuse_rows(g) && !rope_mixed(batch))
|
||||
RAD_ISSUE_N(c, m.op_rope_cs, T, rope_pos1(batch, T), RAD_B(m.b_rope_cs));
|
||||
|
||||
/* The pass's first full-attention layer with an indexer makes what every such layer shares of
|
||||
* its step (QsaIndexer::step, `first`). */
|
||||
bool qsa_first = true;
|
||||
for (int64_t li = 0; li < (int64_t)m.layers.size(); ++li) {
|
||||
const Layer& l = m.layers[(size_t)li];
|
||||
/* Before the attention connection reads the stream, which is where the reference puts it. */
|
||||
@@ -1749,7 +1770,11 @@ static void step_serial(RadCtx* c, const RadBatch* batch, Model& m, const int32_
|
||||
if (l.cs_in >= 0) m.calib.tap(c, (size_t)l.cs_in, T);
|
||||
/* 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.step(c, batch); }
|
||||
if (l.full) {
|
||||
l.qsa.step(c, l.attn.w.h, batch, pcu, l.attn.qsa_use(batch), qsa_first);
|
||||
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);
|
||||
l.hc_mix.write(c, T, 0, T);
|
||||
@@ -1827,23 +1852,37 @@ static void step(RadCtx* c, const RadBatch* batch) {
|
||||
uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) {
|
||||
if (!c || !batch) return 0;
|
||||
if (batch->enc) return 1;
|
||||
Model& m = g_model[rad_slot(c)];
|
||||
/* Which side of the indexer's exactness bound the step is on by the attention group's exact
|
||||
* host bound, by which the full-attention layers and the indexer decide whether the step
|
||||
* needs scores (AttnGatedFP8::qsa_within, qsa_exact_host). The core's key holds only the
|
||||
* rounded context bound, so the side is keyed here: one more recording of each pass kind as a
|
||||
* request crosses the bound. `sel` is 2 within the bound and 0 past it or where it is
|
||||
* unknown, which is where the blocks fall back to the rounded bound the core keys. */
|
||||
uint64_t sel = 0;
|
||||
if (used_max_h && batch->kv && m.kv_full && m.qsacfg.budget > 0 && m.qsacfg.ratio > 0)
|
||||
for (int i = 0; i < batch->n_kv_groups; ++i)
|
||||
if (batch->kv[i].group == m.kv_full) {
|
||||
if (used_max_h[i] > 0 &&
|
||||
used_max_h[i] <= m.qsacfg.budget + m.qsacfg.ratio - 1) sel = 2;
|
||||
break;
|
||||
}
|
||||
/* A decode pass, a speculative verify or an MTP draft included, issues every launch at the
|
||||
* step's keyed context bound (RadArgs::kv_used_max_h), never the live maximum, so the core's
|
||||
* key holds its geometry. */
|
||||
if (batch->phase == RAD_PHASE_DECODE) return 1;
|
||||
if (batch->phase == RAD_PHASE_DECODE) return 1 | sel;
|
||||
/* Mixed batches split the query rows into decode and prefill subsets before attention, which
|
||||
* read the live maximum. The whole-batch host summary cannot key those subset geometries
|
||||
* exactly, nor a drafter's or a verify's rows inside a prefill pass. */
|
||||
if (batch->phase == RAD_PHASE_MIXED || batch->draft_pass != 0 || batch->n_spec > 0) return 0;
|
||||
|
||||
Model& m = g_model[rad_slot(c)];
|
||||
bool has_full_attention = false;
|
||||
for (const Layer& layer : m.layers) {
|
||||
if (!layer.full) continue;
|
||||
if (layer.attn.kv_block != 4) return 0;
|
||||
has_full_attention = true;
|
||||
}
|
||||
if (!has_full_attention) return 1;
|
||||
if (!has_full_attention) return 1 | sel;
|
||||
if (m.g.head_dim != 256 || (m.g.gqa() != 6 && m.g.gqa() != 12) || !m.kv_full ||
|
||||
!used_max_h || !batch->kv || batch->n_kv_groups <= 0)
|
||||
return 0;
|
||||
@@ -1869,7 +1908,10 @@ uint64_t pass_key(RadCtx* c, const RadBatch* batch, const int32_t* used_max_h) {
|
||||
const int bq = nwarps * 16 / m.g.gqa();
|
||||
const int splits = rad_prefill_splits_for_live(bq, tile, q_len, total_q, num_seqs,
|
||||
kv_heads, m.g.max_ctx, used, wgps);
|
||||
return rad_prefill_grid_key(q_len, bq, splits, kv_heads, num_seqs, nwarps * 32);
|
||||
const uint64_t grid = rad_prefill_grid_key(q_len, bq, splits, kv_heads, num_seqs, nwarps * 32);
|
||||
/* 0 stays "unknown"; the side of the bound rides in a bit the grid key's mix does not leave
|
||||
* meaningful, so two sides are two keys. */
|
||||
return grid ? ((grid << 2) | 1 | sel) : 0;
|
||||
}
|
||||
|
||||
static int probe(const RadModelMeta* meta, RadArchProbe* out) {
|
||||
|
||||
+66
-3
@@ -64,9 +64,68 @@ With `pos + 1 <= budget + ratio - 1 = 2051` visible tokens there are at most 512
|
||||
the top-k keeps all of them and the tail is always attended, so QSA equals dense causal attention
|
||||
bit for bit. Below the bound, a step with more than one query per sequence (prefill, mixed or
|
||||
speculative) issues dense attention, which reads each key once per query tile where the sparse
|
||||
path reads a table per query. Plain decode (one query per sequence) takes the sparse path at every
|
||||
context: there is no tile to amortise, and the sparse path stays exercised. The bound is the test
|
||||
for every change here (see "Checking").
|
||||
path reads a table per query. Plain decode (one query per sequence) takes the sparse row's launch
|
||||
at every context: there is no tile to amortise, and the sparse kernel stays exercised. The bound is
|
||||
the test for every change here (see "Checking").
|
||||
|
||||
## Below the bound
|
||||
|
||||
At or below the bound the selection does not depend on the scores: every complete block, in
|
||||
block order, then the partial page. So a step that is *entirely* at or below it scores and selects
|
||||
at most once for the whole pass, and where nothing reads a selection not at all. The attention
|
||||
block decides (`AttnGatedFP8::qsa_use`, `arch/common/rad_block_attn_gated_fp8.h`) and hands the
|
||||
answer to the indexer, which runs first (`QsaIndexer::step`'s `use`):
|
||||
|
||||
| the step, at or below the bound | attention (unchanged) | the indexer issues |
|
||||
|---|---|---|
|
||||
| several rows a sequence, rounded bound within the budget (a chain verify, a head's history pass, prefill, mixed) | dense, over the block table | per layer: the projection, `qsa_work`, `qsa_block_key`, `qsa_tail_store` |
|
||||
| one row a sequence (plain decode, a serial head's round) | the sparse row's launch over the sequence's **block table** and `seqused` | the same four |
|
||||
| several rows a sequence, rounded bound past the budget (the same steps between 1025 and 2047 tokens) | the sparse row a query, over the selection | the first full-attention layer of the pass: everything, as before; every other layer: the same four |
|
||||
| anything past the bound, a streamed attention group, `RADIANCE_DEBUG_QSA_EAGER` | as before | everything, per layer |
|
||||
|
||||
**The attention's kernel never moves.** Which kernel a step's attention runs is decided exactly as
|
||||
before (dense by the rounded bound, the sparse row otherwise): the dense and the sparse kernel read
|
||||
the same keys below the bound but sum them in another order, and a verify's rows feed the next
|
||||
step, so a different kernel at the same context is other text at a near-tie. Only *who makes*
|
||||
the selection changes, and it is byte for byte the selection each layer made for itself.
|
||||
|
||||
**The stateful half always runs.** Block keys and the tail are what later steps score and build
|
||||
on; only the query side and the scoring are skipped.
|
||||
|
||||
**The block table in place of the selection** is exact because of what `qsa_select` writes below
|
||||
the bound for a row at position `p` of a sequence whose context ends there: `sel` is the table's
|
||||
first `ceil((p + 1) / ratio)` entries and `seqused` is `p + 1`, the sequence's own. The launch is
|
||||
the same op and kernel (`attn_paged_gate_quant`, `q_len` 1), its split count does not read the
|
||||
table's width, and it sums pages in the same order, so the bytes are the selection's. It holds for
|
||||
a row that is alone in its sequence and at the context's last position, which the architecture
|
||||
vouches for (`qsa_table_rows`: a plain decode row and a serial head's round on Qwen4-Exp), and it
|
||||
is taken only when `max_q_len == 1`.
|
||||
|
||||
**One selection a pass** (`kQsaOnce`) is exact because below the bound a row has at most `topk`
|
||||
complete blocks, so `qsa_select` takes every block whose key is not 0 -- any block with a page and
|
||||
a score that is a number -- and what it writes (the pages in block order, the partial page, the
|
||||
count, `seqused`) depends on the positions and the block table alone: every layer's scores give the
|
||||
same bytes. The first full-attention layer of the pass therefore issues its selection exactly as
|
||||
before (query, scores, `qsa_select`) and every later layer's attention reads that one; the later
|
||||
layers issue no query, no score and no select. `sel`, `nsel` and `seqused` are the model's buffers,
|
||||
live across its layers, and nothing else writes them. The MTP head has one such layer and so issues
|
||||
what it always did.
|
||||
|
||||
One consequence is deliberate: a block whose score is not a number (a NaN block key) is dropped
|
||||
or kept for the whole pass as the first layer's score has it, and is always kept where no
|
||||
selection is made, as the dense attention always kept it.
|
||||
|
||||
**Whether a step is within the bound is the group's exact ceiling.** `max_ctx_len` is rounded up to
|
||||
1024 tokens so a recorded pass replays (`core/sched/batch.cpp`, `bound_bucket`); by it a 4-row
|
||||
verify is "past" the budget from 1025 tokens of context on. `RadBatch::kv_used_max_h[group]` is the
|
||||
exact host ceiling of every row's `position + 1` (context plus this step's rows, the furthest a
|
||||
row issued ahead can be), and `qsa_within` reads it where the architecture's pass key carries
|
||||
which side of the bound it is on (`qsa_exact_host`; `qwen4exp_fp8.cpp`, `pass_key`). The cost is
|
||||
one more recording of each pass kind when a request crosses the bound.
|
||||
|
||||
Per chain verify step of Qwen3.8-Flash-Next (12 QSA layers) this removes 36 launches up to 1024
|
||||
tokens of context and 33 from there to the bound, and 3 a head pass up to 1024 tokens (its rounds
|
||||
up to the bound).
|
||||
|
||||
## Attention over a selected set
|
||||
|
||||
@@ -203,6 +262,10 @@ with:
|
||||
form, and split against unsplit at bit identity on mostly-tied scores, so the tie rule decides;
|
||||
- **`RADIANCE_DEBUG_QSA`**: prints `nc`, `nsel`, `seqused` and the head of the table for the first
|
||||
few steps. `nc` above `topk` is intended dropping, not a divergence. It synchronises the stream.
|
||||
- **`RADIANCE_DEBUG_QSA_EAGER`**: every step scores and selects per layer, the issue sequence
|
||||
before "Below the bound" existed (`tests/arch_test.cpp`'s pinned step hashes are the old ones
|
||||
under it). Greedy text with and without it must be byte-identical, drafts included: at or below
|
||||
the bound the two differ only in what is launched, never in a byte the attention reads.
|
||||
|
||||
**Count the reach, not the prompt.** The bound is on the context each query sees, so a prompt a
|
||||
few tokens under it crosses the bound during decode and is expected to start differing there.
|
||||
+203
-5
@@ -2582,6 +2582,199 @@ TEST(qsa_without_a_stream_the_selection_is_never_resolved) {
|
||||
CHECK_EQ(attn, selects);
|
||||
}
|
||||
|
||||
/* At or below the exactness bound nothing reads the selection, so the indexer issues none of its
|
||||
* query side (docs/QSA.md, "Below the bound"; AttnGatedFP8::qsa_use, QsaIndexer::step): a step of
|
||||
* several rows a sequence -- a chain verify -- attends densely over the block table, and a step of
|
||||
* one row a sequence issues the sparse row's launch over the sequence's own table and length. The
|
||||
* stateful half (work list, block keys, tail) runs on every step either way. Above the bound, by
|
||||
* the rounded context bound, both score and select per layer and attend over the selection. */
|
||||
TEST(qsa_below_the_exact_bound_the_indexer_issues_no_query_score_or_select) {
|
||||
RadModelMeta meta = qwen4exp_meta();
|
||||
RadBuildCtx c = build_ctx(0, 1);
|
||||
c.max_tok = 64; c.max_seqs = 4; c.max_spec = 3; c.max_ctx = 200000;
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
|
||||
const qwen4exp_fp8::Model& m = qwen4exp_fp8::g_model[0];
|
||||
size_t full = 0;
|
||||
for (const auto& l : m.layers) full += l.full ? 1 : 0;
|
||||
REQUIRE(full > 0);
|
||||
|
||||
struct Seen { size_t work = 0, bkey = 0, tail = 0, score = 0, select = 0, qprep = 0;
|
||||
size_t attn = 0, by_table = 0, by_sel = 0, dense = 0; };
|
||||
auto look = [&](const Kit& k) {
|
||||
RadCtx ctx;
|
||||
ctx.b = &b;
|
||||
ctx.batch = &k.b;
|
||||
qwen4exp_fp8::step(&ctx, &k.b);
|
||||
Seen s;
|
||||
for (const RecIssue& i : ctx.issues) {
|
||||
const RecOp* o = b.op_of(i.op);
|
||||
if (!o) continue;
|
||||
if (o->op == "qsa_work") ++s.work;
|
||||
else if (o->op == "qsa_block_key") ++s.bkey;
|
||||
else if (o->op == "qsa_tail_store") ++s.tail;
|
||||
else if (o->op == "qsa_score") ++s.score;
|
||||
else if (o->op == "qsa_select") ++s.select;
|
||||
else if (o->op == "attn_paged" || o->op == "attn_paged_gate_quant") {
|
||||
++s.attn;
|
||||
const RadOperand& t = i.opd[2];
|
||||
const RadOperand& u = i.opd[3];
|
||||
if (t.kind == RAD_OPK_BUF && t.handle == m.qsaw.sel) {
|
||||
++s.by_sel;
|
||||
CHECK(u.kind == RAD_OPK_BUF && u.handle == m.qsaw.sequ);
|
||||
} else {
|
||||
CHECK(t.kind == RAD_OPK_RAW && t.raw == (void*)k.table.data());
|
||||
CHECK(u.kind == RAD_OPK_RAW && u.raw == (void*)k.used.data());
|
||||
/* the sparse row's launch (q_len 1, a row a sequence) or the dense one */
|
||||
if (o->op == "attn_paged_gate_quant" || (i.opd.size() > 6 && i.opd[6].kind == RAD_OPK_NONE))
|
||||
++s.by_table;
|
||||
else
|
||||
++s.dense;
|
||||
}
|
||||
}
|
||||
for (const auto& l : m.layers)
|
||||
if (l.full && l.qsa.op_qprep && i.op == l.qsa.op_qprep) ++s.qprep;
|
||||
}
|
||||
return s;
|
||||
};
|
||||
|
||||
/* A chain verify of two sequences, 4 rows each, at contexts 40 and 47. */
|
||||
{
|
||||
const auto k = kit(b, { 4, 4 }, {});
|
||||
const Seen s = look(*k);
|
||||
CHECK_EQ(s.work, full); CHECK_EQ(s.bkey, full); CHECK_EQ(s.tail, full);
|
||||
CHECK_EQ(s.score, (size_t)0); CHECK_EQ(s.select, (size_t)0); CHECK_EQ(s.qprep, (size_t)0);
|
||||
CHECK_EQ(s.attn, full); CHECK_EQ(s.dense, full);
|
||||
CHECK_EQ(s.by_sel, (size_t)0);
|
||||
}
|
||||
/* A plain decode step: one row a sequence, the sparse row over the block table. */
|
||||
{
|
||||
const auto k = kit(b, { 1, 1, 1 }, {});
|
||||
const Seen s = look(*k);
|
||||
CHECK_EQ(s.work, full); CHECK_EQ(s.bkey, full); CHECK_EQ(s.tail, full);
|
||||
CHECK_EQ(s.score, (size_t)0); CHECK_EQ(s.select, (size_t)0); CHECK_EQ(s.qprep, (size_t)0);
|
||||
CHECK_EQ(s.attn, full); CHECK_EQ(s.by_table, full);
|
||||
CHECK_EQ(s.by_sel, (size_t)0); CHECK_EQ(s.dense, (size_t)0);
|
||||
}
|
||||
/* The same two steps past the bound by the rounded context bound: everything, per layer, and
|
||||
* the attention over the selection. */
|
||||
for (int rows : { 4, 1 }) {
|
||||
const auto k = kit(b, { rows, rows }, {});
|
||||
k->b.max_ctx_len = 3072;
|
||||
const Seen s = look(*k);
|
||||
CHECK_EQ(s.work, full); CHECK_EQ(s.bkey, full); CHECK_EQ(s.tail, full);
|
||||
CHECK_EQ(s.score, full); CHECK_EQ(s.select, full); CHECK_EQ(s.qprep, full);
|
||||
CHECK(s.attn >= full);
|
||||
CHECK_EQ(s.by_sel, s.attn); CHECK_EQ(s.dense, (size_t)0); CHECK_EQ(s.by_table, (size_t)0);
|
||||
}
|
||||
/* One row a sequence is the table's only where every sequence has one: a sequence with no
|
||||
* row beside one with two is the same T == n_seq and attends over a selection, which at or
|
||||
* below the bound is the first full-attention layer's for the whole pass. */
|
||||
{
|
||||
const auto k = kit(b, { 1, 1 }, {});
|
||||
k->b.max_q_len = 2;
|
||||
const Seen s = look(*k);
|
||||
CHECK_EQ(s.select, (size_t)1); CHECK_EQ(s.score, (size_t)1); CHECK_EQ(s.qprep, (size_t)1);
|
||||
CHECK_EQ(s.work, full); CHECK_EQ(s.bkey, full); CHECK_EQ(s.tail, full);
|
||||
CHECK_EQ(s.by_sel, s.attn); CHECK_EQ(s.by_table, (size_t)0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Whether a step is within the indexer's budget is read from the attention group's exact host
|
||||
* ceiling where the batch carries it (RadBatch::kv_used_max_h), not from the context bound rounded
|
||||
* up to 1024 tokens: a 4-row verify at 1500 tokens of context is within it though
|
||||
* max_ctx_len + max_q_len (2048 + 4) is past it. The kernel does not move with that -- such a step
|
||||
* keeps the sparse row a query, as before, because the dense and the sparse kernel agree on the
|
||||
* keys and not on the last bit -- but its selection is every block, the same bytes at every layer,
|
||||
* so the pass's first full-attention layer issues it as always and the others issue none
|
||||
* (kQsaOnce). The pass key carries the side, so the two are two recordings
|
||||
* (qwen4exp_fp8::pass_key). */
|
||||
TEST(qsa_reads_the_exact_host_bound_and_keys_its_side_of_the_exact_bound) {
|
||||
RadModelMeta meta = qwen4exp_meta();
|
||||
RadBuildCtx c = build_ctx(0, 1);
|
||||
c.max_tok = 64; c.max_seqs = 4; c.max_spec = 3; c.max_ctx = 200000;
|
||||
RadBuilder b;
|
||||
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
|
||||
const qwen4exp_fp8::Model& m = qwen4exp_fp8::g_model[0];
|
||||
const int64_t exact_to = m.qsacfg.budget + m.qsacfg.ratio - 1;
|
||||
REQUIRE(exact_to > 1024);
|
||||
size_t full = 0;
|
||||
for (const auto& l : m.layers) full += l.full ? 1 : 0;
|
||||
REQUIRE(full > 1);
|
||||
|
||||
struct Seen { size_t score = 0, select = 0, work = 0, attn = 0, by_sel = 0, first_sel = 0, first_attn = 0; };
|
||||
auto look = [&](const Kit& k) {
|
||||
RadCtx ctx;
|
||||
ctx.b = &b;
|
||||
ctx.batch = &k.b;
|
||||
qwen4exp_fp8::step(&ctx, &k.b);
|
||||
CHECK(ctx.step_fail.empty());
|
||||
Seen s;
|
||||
size_t at = 0;
|
||||
for (const RecIssue& i : ctx.issues) {
|
||||
++at;
|
||||
const RecOp* o = b.op_of(i.op);
|
||||
if (!o) continue;
|
||||
if (o->op == "qsa_score") ++s.score;
|
||||
else if (o->op == "qsa_work") ++s.work;
|
||||
else if (o->op == "qsa_select") {
|
||||
++s.select;
|
||||
if (!s.first_sel) s.first_sel = at;
|
||||
} else if (o->op == "attn_paged" || o->op == "attn_paged_gate_quant") {
|
||||
++s.attn;
|
||||
if (!s.first_attn) s.first_attn = at;
|
||||
if (i.opd[2].kind == RAD_OPK_BUF && i.opd[2].handle == m.qsaw.sel) ++s.by_sel;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
};
|
||||
const auto k = kit(b, { 4 }, {});
|
||||
k->b.max_ctx_len = 2048; /* 1500 rounded up */
|
||||
std::vector<int32_t> umax((size_t)k->b.n_kv_groups, 0);
|
||||
/* No host ceiling: the rounded bound decides, and 2048 + 4 is past the bound. */
|
||||
{
|
||||
const Seen s = look(*k);
|
||||
CHECK_EQ(s.score, full); CHECK_EQ(s.select, full); CHECK_EQ(s.work, full);
|
||||
CHECK(s.attn >= full); CHECK_EQ(s.by_sel, s.attn);
|
||||
}
|
||||
RadCtx kc;
|
||||
kc.b = &b;
|
||||
kc.batch = &k->b;
|
||||
const uint64_t key_none = qwen4exp_fp8::pass_key(&kc, &k->b, nullptr);
|
||||
/* 1504 rows of context at most: within the bound. The same sparse launches, every layer's,
|
||||
* over the first layer's selection; the stateful half still at every layer. */
|
||||
for (auto& u : umax) u = 1504;
|
||||
k->b.kv_used_max_h = umax.data();
|
||||
{
|
||||
const Seen s = look(*k);
|
||||
CHECK_EQ(s.score, (size_t)1); CHECK_EQ(s.select, (size_t)1); CHECK_EQ(s.work, full);
|
||||
CHECK(s.attn >= full); CHECK_EQ(s.by_sel, s.attn);
|
||||
CHECK(s.first_sel > 0 && s.first_sel < s.first_attn);
|
||||
}
|
||||
const uint64_t key_in = qwen4exp_fp8::pass_key(&kc, &k->b, umax.data());
|
||||
/* The last step within it, and the first past it. */
|
||||
for (auto& u : umax) u = (int32_t)exact_to;
|
||||
CHECK_EQ(look(*k).score, (size_t)1);
|
||||
CHECK_EQ(qwen4exp_fp8::pass_key(&kc, &k->b, umax.data()), key_in);
|
||||
for (auto& u : umax) u = (int32_t)exact_to + 1;
|
||||
CHECK_EQ(look(*k).score, full);
|
||||
const uint64_t key_out = qwen4exp_fp8::pass_key(&kc, &k->b, umax.data());
|
||||
CHECK(key_in != 0 && key_out != 0);
|
||||
CHECK(key_in != key_out);
|
||||
CHECK_EQ(key_out, key_none);
|
||||
/* One row a sequence at the same context is the block table's whatever the rounded bound
|
||||
* says: no selection at all. */
|
||||
{
|
||||
const auto k1 = kit(b, { 1 }, {});
|
||||
k1->b.max_ctx_len = 2048;
|
||||
std::vector<int32_t> u1((size_t)k1->b.n_kv_groups, 1501);
|
||||
k1->b.kv_used_max_h = u1.data();
|
||||
const Seen s = look(*k1);
|
||||
CHECK_EQ(s.score, (size_t)0); CHECK_EQ(s.select, (size_t)0); CHECK_EQ(s.work, full);
|
||||
CHECK_EQ(s.attn, full); CHECK_EQ(s.by_sel, (size_t)0);
|
||||
}
|
||||
}
|
||||
|
||||
/* The plugin declares by each weight's stored encoding. Flash-Next's experts, gated-residual
|
||||
* mixing matrices, MTP fc and 2-bit draft head each have a recipe-chosen format the plugin reads
|
||||
* from rad_weight_encoding, at convert and at load alike. With int4-rotated, E4M3-row and u2
|
||||
@@ -13123,11 +13316,16 @@ TEST(a_4096_budget_without_a_span_is_the_pinned_graph_and_steps) {
|
||||
uint64_t graph, steps;
|
||||
};
|
||||
const Case cases[] = {
|
||||
{ "qwen4exp_fp8", 1, 0, 0x64fffe1f970fe9a3ull, 0x7215c4b326f1908dull },
|
||||
{ "qwen4exp_fp8", 2, 0, 0xe616c803347c8d63ull, 0xbc2ee0bd2f930ffaull },
|
||||
{ "qwen4exp_fp8", 2, 1, 0x5551ab9dbf8f611cull, 0xff6eee327db26cdcull },
|
||||
{ "qwen4exp_fp8", 4, 0, 0x015eb0ed4d0951a1ull, 0x7342ae88d275d694ull },
|
||||
{ "qwen4exp_fp8", 4, 3, 0x0e4fc56b33d75da3ull, 0x46d8d1d25e99bae2ull },
|
||||
/* Flash-Next's step hashes moved once, with the graphs unchanged: the fingerprint's
|
||||
* decode step, verify, history pass and round are below the indexer's exactness bound,
|
||||
* where the indexer now issues no query, score or select and a one-row sequence attends
|
||||
* over its block table (docs/QSA.md, "Below the bound"). Its prefill and mixed steps,
|
||||
* past the bound, are issued as before. */
|
||||
{ "qwen4exp_fp8", 1, 0, 0x64fffe1f970fe9a3ull, 0x65d43fc170c2c598ull },
|
||||
{ "qwen4exp_fp8", 2, 0, 0xe616c803347c8d63ull, 0x521774d19f34781full },
|
||||
{ "qwen4exp_fp8", 2, 1, 0x5551ab9dbf8f611cull, 0x3afe8bf2df1eaa7bull },
|
||||
{ "qwen4exp_fp8", 4, 0, 0x015eb0ed4d0951a1ull, 0xe78350c7803a9437ull },
|
||||
{ "qwen4exp_fp8", 4, 3, 0x0e4fc56b33d75da3ull, 0xdb6d50824b015389ull },
|
||||
{ "qwen35_fp8", 1, 0, 0x0f08ba5027cba71aull, 0xa7ec3cf6252cd441ull },
|
||||
{ "qwen35_fp8", 2, 1, 0xe7780f241685a18bull, 0xdd5d65ef158c4136ull },
|
||||
{ "qwen35moe_fp8", 1, 0, 0xb1e6173bd121dc2aull, 0x2b5989b2a6dbc7dfull },
|
||||
|
||||
@@ -8908,6 +8908,72 @@ TEST(qsa_select_orders_above_then_ties_and_appends_the_partial_page) {
|
||||
CHECK_EQ(r.su[0], 1LL * 4 + 3 + 1);
|
||||
}
|
||||
|
||||
/* At or below the exactness bound (every complete block fits in topk) a row at the end of its
|
||||
* sequence's context selects the head of the block table, in table order, then the partial page,
|
||||
* and its seqused is the context's length, whatever the blocks score. The attention block issues
|
||||
* such a row over the sequence's own table and length and the indexer selects nothing
|
||||
* (AttnGatedFP8::qsa_use, docs/QSA.md "Below the bound"); this is the equality that rests on. */
|
||||
TEST(qsa_select_below_the_bound_is_the_block_table_and_the_context_length) {
|
||||
const int64_t ratio = 4, topk = 8, nb = 12;
|
||||
std::vector<int64_t> bt;
|
||||
for (int64_t b = 0; b < nb; ++b) bt.push_back(100 + 7 * ((b * 5) % nb)); /* scattered pages */
|
||||
uint32_t g = 12345;
|
||||
for (int64_t p = 0; p <= topk * ratio + ratio - 2; ++p) { /* every position up to the bound */
|
||||
for (int variant = 0; variant < 3; ++variant) {
|
||||
std::vector<float> sc((size_t)nb);
|
||||
for (int64_t b = 0; b < nb; ++b) {
|
||||
g = g * 1664525u + 1013904223u;
|
||||
/* scores as they come: positive, equal ones, and exact zeros (a relu's) */
|
||||
sc[(size_t)b] = variant == 0 ? (float)((g >> 20) % 7) * 0.25f
|
||||
: variant == 1 ? 0.0f : 3.5f;
|
||||
}
|
||||
const int64_t n = (p + 1) / ratio; /* complete blocks, this row's */
|
||||
const Sel r = qsa_select_run(sc, nb, bt, n, { p }, topk, ratio);
|
||||
REQUIRE_EQ(r.sel.size(), (size_t)(topk + 1));
|
||||
const int64_t pages = (p + ratio) / ratio; /* ceil((p + 1) / ratio) */
|
||||
CHECK_EQ(r.nsel[0], pages);
|
||||
CHECK_EQ(r.su[0], p + 1);
|
||||
for (int64_t i = 0; i <= topk; ++i)
|
||||
CHECK_EQ(r.sel[(size_t)i], i < pages ? bt[(size_t)i] : -1LL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The rows of a chain verify or a prefill chunk at or below the bound select the same bytes
|
||||
* whatever they score -- here over a plane of zeros against random scores: every row its own
|
||||
* complete blocks in table order, the partial page, the count and seqused. The sparse attention of
|
||||
* such a step (between 1025 and 2047 tokens of context a multi-row step is the sparse row a query)
|
||||
* therefore reads, at every layer, the selection the pass's first full-attention layer issued
|
||||
* (QsaIndexer::step, kQsaOnce). The last positions put a row on exactly `topk` blocks plus a
|
||||
* partial page. */
|
||||
TEST(qsa_select_of_a_chains_rows_over_a_constant_plane_is_the_scored_selection_below_the_bound) {
|
||||
const int64_t ratio = 4, topk = 8, nb = 16, M = 4;
|
||||
std::vector<int64_t> bt;
|
||||
for (int64_t b = 0; b < nb; ++b) bt.push_back(200 + 3 * ((b * 7) % nb)); /* scattered pages */
|
||||
uint32_t g = 777;
|
||||
int partial_at_topk = 0;
|
||||
for (int64_t p0 = 0; p0 + M - 1 <= topk * ratio + ratio - 2; ++p0) {
|
||||
std::vector<int64_t> pos;
|
||||
for (int64_t j = 0; j < M; ++j) pos.push_back(p0 + j);
|
||||
const int64_t nc = (pos.back() + 1) / ratio; /* the sequence's, after the step */
|
||||
std::vector<float> sc((size_t)(M * nb));
|
||||
for (auto& v : sc) { g = g * 1664525u + 1013904223u; v = (float)((g >> 20) % 5) * 0.5f; }
|
||||
const Sel scored = qsa_select_run(sc, nb, bt, nc, pos, topk, ratio);
|
||||
const Sel flat = qsa_select_run(std::vector<float>((size_t)(M * (topk + 1)), 0.0f), topk + 1,
|
||||
bt, nc, pos, topk, ratio);
|
||||
REQUIRE_EQ(scored.sel.size(), (size_t)(M * (topk + 1)));
|
||||
REQUIRE_EQ(flat.sel.size(), scored.sel.size());
|
||||
CHECK(flat.sel == scored.sel);
|
||||
CHECK(flat.nsel == scored.nsel);
|
||||
CHECK(flat.su == scored.su);
|
||||
for (int64_t j = 0; j < M; ++j) {
|
||||
CHECK_EQ(flat.su[(size_t)j], pos[(size_t)j] + 1);
|
||||
if ((pos[(size_t)j] + 1) / ratio == topk && flat.nsel[(size_t)j] == topk + 1) ++partial_at_topk;
|
||||
}
|
||||
}
|
||||
CHECK(partial_at_topk > 0);
|
||||
}
|
||||
|
||||
/* ================================================================== kv_resolve
|
||||
* The residency map of a streamed group: selected block ids in, ring slots out, a block copied in
|
||||
* from the host mirror when it is not resident (docs/OPS.md "kv_resolve",
|
||||
|
||||
Reference in new issue
Block a user