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:
Code000andClaude Opus 5.5 authored and StillDeadcode committed 2026-11-17 18:41:16 +01:00
1 parent 315aea7db9
commit cfec01ec8d
8 files changed
+515 -30

No files matched your search

+66
View File
@@ -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",