libxe2: split-KV paged attention up to head_dim 512

attn_paged_fast stopped at head_dim 256 (16 values a lane) and sent wider heads to the exact
kernel, so Gemma 4's global layers (head_dim 512) ran xe2_attn_paged_ref at decode and prefill.
The kernel is now instantiated at 16 values a lane (head_dim <= 256: unchanged, its times match
to 0.1%) and at 32 (head_dim 512, four 16-byte value loads a lane).

rad-kbench on the Gemma 4 E2B container, head_dim 512: decode 192 -> 43 us (short context) and
2040 -> 256 us (long); q_len 256 55.4 -> 19.6 ms; 320/320 cases within tolerance. Gemma 4 E2B
end to end: decode 58.6 -> 63.0 tok/s, 2.4k-token prefill 724 -> 1537 tok/s, decode at that
context 29 -> 59 tok/s; answers checked (a story, a 120-paragraph count).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
aneinanderandClaude Opus 5.5 committed 2026-11-08 20:29:25 +01:00
1 parent 0c7606423b
commit d35027aaf7
1 file changed
+18 -7
+18 -7
View File
@@ -46,7 +46,8 @@ namespace {
constexpr int kSgs = 8;
constexpr size_t kWg = kSgs * kSub;
constexpr int kMaxPer = 16; /* head_dim / 16, up to head_dim 256 */
constexpr int kMaxPer = 32; /* head_dim / 16, up to head_dim 512; instantiated at 16 (head_dim <= 256,
the form every model up to 256 runs, unchanged) and at 32 (Gemma 4's 512) */
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
@@ -164,12 +165,13 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
auto launch = [&]<int MP>() -> int {
int rc = submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * kv_heads * P) * kWg, kWg},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const int64_t g = (int64_t)it.get_group(0);
const int64_t p = g % P, kh = (g / P) % kv_heads, tok = g / (P * kv_heads);
float* qloc = *sycl::ext::oneapi::group_local_memory_for_overwrite<float[kSgs * kSub * kMaxPer]>(
float* qloc = *sycl::ext::oneapi::group_local_memory_for_overwrite<float[kSgs * kSub * MP]>(
it.get_group());
const sycl::sub_group sg = it.get_sub_group();
const int s = (int)sg.get_group_linear_id();
@@ -214,13 +216,13 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
for (int64_t hl = s; hl < gqa; hl += kSgs) {
const int64_t hq = kh * gqa + hl;
const int64_t qoff = tok * q_rs + hq * q_hs;
float qv[kMaxPer], acc[kMaxPer];
for (int e = 0; e < kMaxPer; ++e) {
float qv[MP], acc[MP];
for (int e = 0; e < MP; ++e) {
qv[e] = e < per ? rad_load_f32(qb, qdt, qoff + (lane * per + e) * q_ds) : 0.0f;
acc[e] = 0.0f;
}
/* The query in local memory, so a lane can score a whole key by itself. */
float* qs = qloc + (size_t)s * kSub * kMaxPer;
float* qs = qloc + (size_t)s * kSub * MP;
for (int e = 0; e < per; ++e) qs[lane * per + e] = qv[e];
sycl::group_barrier(sg);
float m = -INFINITY, l = 0.0f;
@@ -273,6 +275,13 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
const sycl::vec<uint16_t, 8> v1 = *(const sycl::vec<uint16_t, 8>*)(vr + 8);
for (int e = 0; e < 8; ++e) acc[e] += wj * (rad_bf16_to_f32(v0[e]) * vds);
for (int e = 0; e < 8; ++e) acc[8 + e] += wj * (rad_bf16_to_f32(v1[e]) * vds);
} else if (MP == 32 && vec_kv && per == 32) {
/* head_dim 512 (Gemma 4's global layers): four 16-byte loads a lane */
const uint16_t* vr = (const uint16_t*)cb + kbj + head_dim + lane * 32;
for (int u = 0; u < 4; ++u) {
const sycl::vec<uint16_t, 8> vv = *(const sycl::vec<uint16_t, 8>*)(vr + u * 8);
for (int e = 0; e < 8; ++e) acc[u * 8 + e] += wj * (rad_bf16_to_f32(vv[e]) * vds);
}
} else {
for (int e = 0; e < per; ++e)
acc[e] += wj * (rad_load_f32(cb, cdt, kbj + (head_dim + lane * per + e) * sd) * vds);
@@ -321,8 +330,8 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
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;
float L = 0.0f, out[MP];
for (int e = 0; e < MP; ++e) out[e] = 0.0f;
if (M != -INFINITY)
for (int64_t p = 0; p < Pn; ++p) {
const float* q0 = ps + p * state;
@@ -336,6 +345,8 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
rad_store_f32(ob, odt, ooff + (lane * per + e) * o_ds, out[e] * inv);
});
});
};
return per > 16 ? launch.template operator()<32>() : launch.template operator()<16>();
}
} // namespace xe2