libxe2: split-KV attention keeps 64 keys a partition and skips the empty ones -- 212 -> 114 us a verify call

At a verify's 8 rows and a ~300-token context the 32 partitions held ~10 keys each, and every one
loaded the query, wrote a full state and was merged. A partition now takes at least 64 keys; the
ones past a row's keys write only an end marker (l = -1) and return, and the merge stops at it.
Tolerance-held as before: fixture 158/158 (rel_l2 <= 2.9e-4), the 27B 124/124.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-24 15:23:51 +02:00
1 parent 5092098123
commit 531146b4e8
1 file changed
+23 -5
+23 -5
View File
@@ -12,7 +12,7 @@
* rows, so the bytes come from memory once a work-group.
*
* The partition count P is chosen at launch from the rows: a decode step (one row, a few heads)
* splits each row's visible keys evenly 32 ways, on the device from the row's own context, so the
* splits each row's visible keys evenly up to 32 ways, at least kMinPart keys a partition, on the device from the row's own context, so the
* whole card works on one token; a prefill step has parallelism in its rows and takes P = 1. With P > 1 each partition's state goes
* to scratch (64 MiB, the `scratch` hook) and pass 2 -- a sub-group a (row, query head) -- merges
* them; with P = 1 pass 1 writes the normalised output itself.
@@ -36,6 +36,9 @@ constexpr size_t kWg = kSgs * kSub;
constexpr int kMaxPer = 16; /* head_dim / 16, up to head_dim 256 */
constexpr int64_t kScratch = (int64_t)64 << 20; /* pass 1's partial states */
constexpr int64_t kMaxParts = 32; /* a row's visible keys split this many ways at most */
constexpr int64_t kMinPart = 64; /* and into partitions of at least this many keys: at a verify's
8 rows and a 300-token context, 32 partitions of ~10 keys
cost 212 us a call, ~4 of 64 cost 69 */
int64_t row_stride(const RadTensor* t, int64_t cols) {
if (!t || t->rank < 2) return cols;
@@ -155,10 +158,21 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
if (!causal) jhi = sycl::min(jend, pos + window);
}
if (P > 1) {
/* at least kMinPart keys a partition: a short context leaves the rest empty
* (they exit at once and the merge skips them) rather than one step each */
const int64_t span = sycl::max(jhi - jlo, (int64_t)0);
const int64_t ch = (span + P - 1) / P;
const int64_t ch = sycl::max((span + P - 1) / P, kMinPart);
jlo += p * ch;
jhi = sycl::min(jhi, jlo + ch);
if (jlo >= jhi) { /* uniform: past the row's keys; l = -1 ends the merge here */
for (int64_t hl = s; hl < gqa; hl += kSgs)
if (lane == 0) {
float* ps = part + ((tok * q_heads + kh * gqa + hl) * P + p) * state;
ps[0] = -INFINITY;
ps[1] = -1.0f;
}
return;
}
}
for (int64_t hl = s; hl < gqa; hl += kSgs) {
const int64_t hq = kh * gqa + hl;
@@ -263,13 +277,17 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
if (qlen <= 0) return;
const float* ps = part + (tok * q_heads + hq) * P * state;
float M = -INFINITY;
for (int64_t p = 0; p < P; ++p)
if (ps[p * state + 1] > 0.0f) M = sycl::fmax(M, ps[p * state]);
int64_t Pn = P; /* the partitions holding keys: a prefix, ended by l = -1 */
for (int64_t p = 0; p < P; ++p) {
const float lp = ps[p * state + 1];
if (lp < 0.0f) { Pn = p; break; }
if (lp > 0.0f) M = sycl::fmax(M, ps[p * state]);
}
const int64_t ooff = tok * o_rs + hq * o_hs;
float L = 0.0f, out[kMaxPer];
for (int e = 0; e < kMaxPer; ++e) out[e] = 0.0f;
if (M != -INFINITY)
for (int64_t p = 0; p < P; ++p) {
for (int64_t p = 0; p < Pn; ++p) {
const float* q0 = ps + p * state;
if (!(q0[1] > 0.0f)) continue; /* an empty partition */
const float f = sycl::exp(q0[0] - M);