libxe2: prefill attention on DPAS (flash attention) -- prefill 378 -> 468 tok/s at 2234 tokens

The SIMT kernel gives each (query row, kv head) a work-group and scores keys with f32
multiply-adds, so at a 2048-token chunk every row re-reads its keys and values: Qwen3.8-27B's 32
full-attention calls of a 2234-token prompt took 1.41 s (--profile-ops). The ESIMD form takes a work-
group per (8 query rows, kv head), a thread per query head, and shares each page of keys and values
among the 8 rows of all of them: Q K^T on bf16 DPAS with K^T from a transposed 2D load of the page
(a 16-token page is a DPAS's 16 keys), the online softmax in f32 over 32 keys a step, P rounded to
bf16 and P V on DPAS with V by a VNNI-transforming load. Q waits in local memory so the registers
hold the 8 x 256 f32 accumulator. Served for a single sequence's prefill (16+ rows), causal without
a window, bf16 query, cache and output, pages of 16, head_dim 256; everything else keeps the SIMT
kernel (XE2_ATTN_SIMT=1 forces it).

rad-kbench: qwen3.8-27b-w4a16-rtn 240/240 (the flash cases rel_l2 1.6-2.3e-3, tolerance 2e-2),
fixture attn* 237/237. Prefill: 2234 tokens 5.90 -> 4.78 s, 2475 tokens 481 tok/s; the summary of
a 2000-token prompt reads as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-28 01:08:41 +01:00
1 parent 2365a2ae51
commit 13c64a3d78
2 files changed
+209

No files matched your search

+24
View File
@@ -26,9 +26,16 @@
#include "xe2_ops.h"
#include <cmath>
#include <cstdlib>
namespace xe2 {
/* xe2_attn_flash_esimd.cpp */
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);
namespace {
constexpr int kSgs = 8;
@@ -104,6 +111,23 @@ 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 && 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 &&
(sh * 2) % 64 == 0 && (o_rs * 2) % 64 == 0 && !std::getenv("XE2_ATTN_SIMT")) {
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)));
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);
if (rc != RAD_E_UNSUPPORTED) return rc;
}
/* The partitions: as many as the scratch holds for these rows, at most kMaxParts. Each row's
* own visible span is split evenly on the device, so every partition has keys whatever the
* context limit (splitting the limit instead left most partitions empty at a short context and