libxe2: sliding-window attention on the DPAS kernels

attn_paged_fast sent any window > 0 to the SIMT split-KV kernel, so every sliding-window layer
(Gemma 2/3/4, Mistral-style and gpt-oss models) missed the DPAS decode and prefill kernels. They now
take the window: each 8-row block starts at its first row's bound (pos - window + 1, on a whole
32-key step; the split decode form partitions from there) and the mask hides keys below each row's
own bound, as libref's attn_paged. The window form is its own instantiation (WIN), so the window-0
kernels are unchanged: the Qwen3.5-9B's attention times match a rebuild of the old code to 0.5%.
The quad prefill form keeps window 0; a windowed prefill takes the esimd prefill form.

rad-kbench, Gemma 4 E2B (head_dim 256, window 512): decode 101 -> 23.5 us and 23 -> 18 us;
q_len 128 757 -> 92 us; q_len 512 2833 -> 98 us; 320/320 within tolerance. Qwen3.5-9B 160/160,
default fixture 2144/2144.

Gemma 4 E2B end to end: decode 63.0 -> 74.4 tok/s, 2.4k-token prefill 1537 -> 1886 tok/s; a story,
a 120-paragraph count and a password at paragraph 77 of 120 answered correctly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
aneinanderandClaude Opus 5.5 committed 2026-11-08 21:08:31 +01:00
1 parent d35027aaf7
commit dd20835f18
2 files changed
+44 -28

No files matched your search

+7 -6
View File
@@ -34,7 +34,8 @@ namespace xe2 {
int attn_flash_esimd(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb,
int64_t sb, int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub,
uint32_t sudt, int64_t rows, int64_t kv_heads, int64_t gqa, float scale, int64_t jcap,
uint16_t* ob, int64_t o_rs, int64_t o_hs, void* scratch, int64_t scratch_bytes);
uint16_t* ob, int64_t o_rs, int64_t o_hs, void* scratch, int64_t scratch_bytes,
int64_t window);
/* xe2_attn_flash_quad.cpp: the prefill form at the default register file, four threads a (16 rows,
* query head); RAD_E_UNSUPPORTED where it does not serve, and attn_flash_esimd then runs */
int attn_flash_quad(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb,
@@ -118,9 +119,9 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
const bool vec_kv = KV->dtype == RAD_BF16 && sd == 1 && head_dim % 8 == 0 && sb % 8 == 0 &&
sh % 8 == 0 && so % 8 == 0 && rad_ptr_aligned(KV->data, 16);
/* A single sequence's prefill on DPAS (xe2_attn_flash_esimd.cpp): bf16 throughout, pages of 16,
* causal without a window, every 2D load's base and pitch on its alignment. */
if (n_seq == 1 && (rows >= 16 || !std::getenv("XE2_ATTN_DEC_SIMT")) && causal && window == 0 && !KD && !VD && Q->dtype == RAD_BF16 &&
/* A single sequence on DPAS (xe2_attn_flash_esimd.cpp): bf16 throughout, pages of 16, causal, any
* sliding window (the quad prefill form without one), every 2D load's base and pitch on its alignment. */
if (n_seq == 1 && (rows >= 16 || !std::getenv("XE2_ATTN_DEC_SIMT")) && causal && window >= 0 && !KD && !VD && Q->dtype == RAD_BF16 &&
KV->dtype == RAD_BF16 && O->dtype == RAD_BF16 && q_ds == 1 && o_ds == 1 && sd == 1 && block_size == 16 &&
rad_ptr_aligned(Q->data, 64) && rad_ptr_aligned(KV->data, 64) && rad_ptr_aligned(O->data, 64) &&
(q_rs * 2) % 64 == 0 && (q_hs * 2) % 64 == 0 && (so * 2) % 64 == 0 && (sb * 2) % 64 == 0 &&
@@ -128,7 +129,7 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
sycl::queue* fq = queue_of(st);
if (!fq) return RAD_E_DEVICE;
const float fscale = (float)rad_args_getf_or(a, "scale", (double)(1.0f / std::sqrt((float)head_dim)));
if (rows >= 16 && !std::getenv("XE2_ATTN_NO_QUAD")) {
if (rows >= 16 && window == 0 && !std::getenv("XE2_ATTN_NO_QUAD")) {
const int rq = attn_flash_quad(*fq, head_dim, (const uint16_t*)Q->data, q_rs, q_hs, (const uint16_t*)KV->data,
sb, sh, so, (const char*)BT->data, BT->dtype, bt_cs, (const char*)SU->data,
SU->dtype, rows, kv_heads, gqa, fscale, max_blocks * block_size,
@@ -138,7 +139,7 @@ int attn_paged_fast(const RadArgs* a, RadStream st) {
const int rc = attn_flash_esimd(*fq, head_dim, (const uint16_t*)Q->data, q_rs, q_hs, (const uint16_t*)KV->data,
sb, sh, so, (const char*)BT->data, BT->dtype, bt_cs, (const char*)SU->data,
SU->dtype, rows, kv_heads, gqa, fscale, max_blocks * block_size,
(uint16_t*)O->data, o_rs, o_hs, a->scratch, a->scratch_bytes);
(uint16_t*)O->data, o_rs, o_hs, a->scratch, a->scratch_bytes, window);
if (rc != RAD_E_UNSUPPORTED) return rc;
}
+37 -22
View File
@@ -13,7 +13,7 @@
* O += P V bf16 DPAS, P rounded to bf16, V by a VNNI-transforming 2D load
*
* Q is staged in local memory (8 rows x head_dim bf16 a thread) to leave the registers to the f32
* accumulator (8 x 256 floats). One sequence, causal, no window, bf16 query, cache and output, pages
* accumulator (8 x 256 floats). One sequence, causal, any sliding window, bf16 query, cache and output, pages
* of 16, head_dim 256: the prefill of a single-sequence deployment. Not libref's order (it scores
* in f32 and adds exp(s - max) * v key by key): held to rad-kbench's attention tolerance. */
#include "xe2_common.h"
@@ -45,11 +45,11 @@ inline esimd::simd<uint16_t, 128> bf16_of(esimd::simd<float, 128> v) SYCL_ESIMD_
* [row][head][P] of (m, l) in `ml` and of HD floats in `op`, and merge_parts adds them. */
/* MAXG: the staged queries' local memory, a thread each (16 x 4 KiB was 64 KiB a work-group, two
* work-groups an Xe-core) */
template <int HD, bool SPLIT, int MAXG = kMaxGqa, int RB = 1, int PF = 0>
template <int HD, bool SPLIT, int MAXG = kMaxGqa, int RB = 1, int PF = 0, bool WIN = false>
int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb, int64_t sb,
int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub, uint32_t sudt,
int64_t rows, int64_t kv_heads, int gqa, float scale, int64_t jcap, uint16_t* ob, int64_t o_rs,
int64_t o_hs, int NP = 1, float* ml = nullptr, float* op = nullptr) {
int64_t o_hs, int NP = 1, float* ml = nullptr, float* op = nullptr, int64_t window = 0) {
constexpr int KS = HD / 16; /* head-dim steps */
/* the value loop's loads VB at a time, then their DPAS: one wait a batch instead of one a
* column block (8 was best at 16k-64k; the decode form keeps one at a time) */
@@ -77,10 +77,17 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
const int nvalid = (int)(rows - r0 < 8 ? rows - r0 : 8);
const int64_t jhi0 = pos0 + nvalid; /* causal: keys below the last row's position + 1 */
int64_t jhi = jhi0 < jend ? jhi0 : jend;
int64_t jbeg = 0;
/* a sliding window: row r sees keys pos0 + r - window + 1 .. pos0 + r; the walk starts at
* the block's first row's bound, on a whole 32-key step, and the mask below hides the rest */
int64_t jlo = 0;
if constexpr (WIN) {
jlo = pos0 - window + 1;
jlo = jlo < 0 ? 0 : (jlo / 32) * 32;
}
int64_t jbeg = jlo;
if constexpr (SPLIT) { /* this partition's whole 32-key steps */
const int64_t nst = (jhi + 31) / 32, per = (nst + NP - 1) / NP;
jbeg = part * per * 32;
const int64_t nst = (jhi - jlo + 31) / 32, per = (nst + NP - 1) / NP;
jbeg = jlo + part * per * 32;
const int64_t je = jbeg + per * 32;
if (je < jhi) jhi = je;
if (jbeg >= jhi) { /* uniform over the work-group: no keys here */
@@ -173,6 +180,7 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
#pragma unroll
for (int r = 0; r < 8; ++r) {
simd_mask<16> hide = (jj > (int32_t)(pos0 + r)) | (jj >= (int32_t)jhi);
if constexpr (WIN) hide |= jj < (int32_t)(pos0 + r - window + 1);
if (blk[p] < 0 || r >= nvalid) hide = 1;
S[p].template select<16, 1>(r * 16).merge(simd<float, 16>(-INFINITY), hide);
}
@@ -265,7 +273,7 @@ template <int HD>
int flash_decode(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb, int64_t sb,
int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub, uint32_t sudt,
int64_t rows, int64_t kv_heads, int64_t gqa, float scale, int64_t jcap, uint16_t* ob, int64_t o_rs,
int64_t o_hs, void* scratch, int64_t scratch_bytes) {
int64_t o_hs, void* scratch, int64_t scratch_bytes, int64_t window) {
constexpr int kMaxP = 32;
constexpr int PL = HD / 16; /* values a lane in the merge */
const int64_t q_heads = kv_heads * gqa, n = rows * q_heads * kMaxP;
@@ -273,8 +281,11 @@ int flash_decode(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs,
if (!scratch || scratch_bytes < need || !rad_ptr_aligned(scratch, 64)) return RAD_E_UNSUPPORTED;
float* op = (float*)scratch;
float* ml = op + n * HD;
int rc = (gqa <= 8 ? launch<HD, true, 8> : launch<HD, true, kMaxGqa>)(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads,
(int)gqa, scale, jcap, ob, o_rs, o_hs, kMaxP, ml, op);
/* WIN: the sliding-window form, its own instantiation so the window-0 kernel is unchanged */
auto* fn = window > 0 ? (gqa <= 8 ? launch<HD, true, 8, 1, 0, true> : launch<HD, true, kMaxGqa, 1, 0, true>)
: (gqa <= 8 ? launch<HD, true, 8> : launch<HD, true, kMaxGqa>);
int rc = fn(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads,
(int)gqa, scale, jcap, ob, o_rs, o_hs, kMaxP, ml, op, window);
if (rc != RAD_OK) return rc;
return submit([&] {
q.parallel_for(sycl::nd_range<1>{(size_t)(rows * q_heads) * 16, 16},
@@ -309,14 +320,17 @@ template <int HD>
int flash_prefill(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb, int64_t sb,
int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub, uint32_t sudt,
int64_t rows, int64_t kv_heads, int64_t gqa, float scale, int64_t jcap, uint16_t* ob, int64_t o_rs,
int64_t o_hs) {
int64_t o_hs, int64_t window) {
/* gqa <= 8: four 8-row blocks a work-group (their key and value loads shared through L1, 128 KiB of
* staged queries at head_dim 256) and the next step's pages prefetched */
if (gqa <= 8 && so == 2 * HD)
return launch<HD, false, 8, 4, 1>(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads,
(int)gqa, scale, jcap, ob, o_rs, o_hs, 1, nullptr, nullptr);
return (gqa <= 8 ? launch<HD, false, 8> : launch<HD, false, kMaxGqa>)(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads, (int)gqa,
scale, jcap, ob, o_rs, o_hs, 1, nullptr, nullptr);
return (window > 0 ? launch<HD, false, 8, 4, 1, true> : launch<HD, false, 8, 4, 1>)(
q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads, (int)gqa, scale, jcap, ob,
o_rs, o_hs, 1, nullptr, nullptr, window);
auto* fn = window > 0 ? (gqa <= 8 ? launch<HD, false, 8, 1, 0, true> : launch<HD, false, kMaxGqa, 1, 0, true>)
: (gqa <= 8 ? launch<HD, false, 8> : launch<HD, false, kMaxGqa>);
return fn(q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads, (int)gqa,
scale, jcap, ob, o_rs, o_hs, 1, nullptr, nullptr, window);
}
} // namespace
@@ -326,21 +340,22 @@ int flash_prefill(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs
int attn_flash_esimd(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const uint16_t* cb,
int64_t sb, int64_t sh, int64_t so, const char* btb, uint32_t btdt, int64_t bt_cs, const char* sub,
uint32_t sudt, int64_t rows, int64_t kv_heads, int64_t gqa, float scale, int64_t jcap,
uint16_t* ob, int64_t o_rs, int64_t o_hs, void* scratch, int64_t scratch_bytes) {
if (gqa < 1 || gqa > kMaxGqa) return RAD_E_UNSUPPORTED;
uint16_t* ob, int64_t o_rs, int64_t o_hs, void* scratch, int64_t scratch_bytes,
int64_t window) {
if (gqa < 1 || gqa > kMaxGqa || window < 0) return RAD_E_UNSUPPORTED;
#define XE2_FLASH_ARGS q, qb, q_rs, q_hs, cb, sb, sh, so, btb, btdt, bt_cs, sub, sudt, rows, kv_heads, gqa, scale, jcap, ob, o_rs, o_hs
if (rows < 16) {
switch (head_dim) {
case 64: return flash_decode<64>(XE2_FLASH_ARGS, scratch, scratch_bytes);
case 128: return flash_decode<128>(XE2_FLASH_ARGS, scratch, scratch_bytes);
case 256: return flash_decode<256>(XE2_FLASH_ARGS, scratch, scratch_bytes);
case 64: return flash_decode<64>(XE2_FLASH_ARGS, scratch, scratch_bytes, window);
case 128: return flash_decode<128>(XE2_FLASH_ARGS, scratch, scratch_bytes, window);
case 256: return flash_decode<256>(XE2_FLASH_ARGS, scratch, scratch_bytes, window);
default: return RAD_E_UNSUPPORTED;
}
}
switch (head_dim) {
case 64: return flash_prefill<64>(XE2_FLASH_ARGS);
case 128: return flash_prefill<128>(XE2_FLASH_ARGS);
case 256: return flash_prefill<256>(XE2_FLASH_ARGS);
case 64: return flash_prefill<64>(XE2_FLASH_ARGS, window);
case 128: return flash_prefill<128>(XE2_FLASH_ARGS, window);
case 256: return flash_prefill<256>(XE2_FLASH_ARGS, window);
default: return RAD_E_UNSUPPORTED;
}
#undef XE2_FLASH_ARGS