diff --git a/libxe2/xe2_attn_fast.cpp b/libxe2/xe2_attn_fast.cpp index 573f94d9..5541f84e 100644 --- a/libxe2/xe2_attn_fast.cpp +++ b/libxe2/xe2_attn_fast.cpp @@ -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; } diff --git a/libxe2/xe2_attn_flash_esimd.cpp b/libxe2/xe2_attn_flash_esimd.cpp index 65932b64..0fe65156 100644 --- a/libxe2/xe2_attn_flash_esimd.cpp +++ b/libxe2/xe2_attn_flash_esimd.cpp @@ -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 bf16_of(esimd::simd 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 +template 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(-INFINITY), hide); } @@ -265,7 +273,7 @@ template 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 : launch)(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 : launch) + : (gqa <= 8 ? launch : launch); + 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 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(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 : launch)(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 : launch)( + 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 : launch) + : (gqa <= 8 ? launch : launch); + 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