This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libxe2/xe2_attn_flash_esimd.cpp
T
claudeandClaude Opus 5.5 3582900903 libxe2: DPAS flash attention at head_dim 64 and 128, not only 256
attn_flash_esimd (decode / verify and prefill) and attn_flash_quad (prefill) were written over HD
but gated to head_dim 256 (Qwen3.8's full attention); every other model fell to the SIMT kernel.
Both now dispatch 64, 128 and 256; the decode merge takes HD / 16 values a lane.

MiniCPM5-2B bf16 (head_dim 128, gqa 8) on the Arc Pro B65, same build, XE2_ATTN_SIMT=1 as before:
decode 69.3 -> 83.1 tok/s, prefill at ~2k tokens 1806 -> 8080 tok/s. rad-kbench attn_paged:
fixture 207/207, MiniCPM5 160/160, Qwen3.8-27B 160/160. KL against XE2_EXACT=1 over the
12-document corpus: mean 0.00060, top-1 98.59% (the SIMT kernel: 0.00060, 98.59%).
Qwen3.8-27B int4 unchanged (30.1 tok/s, 1361 tok/s prefill).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 01:16:14 +01:00

350 lines
19 KiB
C++

/* xe2_attn_flash_esimd.cpp -- prefill attention on DPAS ("flash attention") for attn_paged_fast.
*
* The SIMT kernel (xe2_attn_fast.cpp) gives each (query row, kv head) its own work-group and scores
* keys with f32 multiply-adds: at a 2048-token prefill every row re-reads its keys and values, and
* 32 calls of Qwen3.8-27B's head_dim 256 took 1.41 s (27.6 ms a call). Here a work-group is (8 query
* rows, kv head), one thread a query head of the group, so a page of keys and values serves 8 rows
* of every head that reads it:
*
* S = Q K^T bf16 DPAS, 8 rows by 16 keys a step of 16 head dims; K^T comes straight from the
* page by a transposed 2D load (a 16-token page is a DPAS's 16 keys)
* online softmax over 32 keys (two pages) a step, in f32: the row max, exp, the row sum, and the
* accumulator rescaled once a step
* 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
* 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"
#include <sycl/ext/intel/esimd.hpp>
#include <sycl/ext/intel/experimental/esimd/memory.hpp>
#include <cmath>
namespace xe2 {
namespace {
namespace esimd = sycl::ext::intel::esimd;
namespace xmx = sycl::ext::intel::esimd::xmx;
namespace xesimd = sycl::ext::intel::experimental::esimd;
constexpr int kMaxGqa = 16;
inline esimd::simd<uint16_t, 128> bf16_of(esimd::simd<float, 128> v) SYCL_ESIMD_FUNCTION {
using namespace esimd;
simd<uint32_t, 128> u = v.template bit_cast_view<uint32_t>();
return simd<uint16_t, 128>((u + 0x7FFFu + ((u >> 16) & 1u)) >> 16);
}
/* SPLIT (the decode / verify form, a few rows): the keys are also split P ways, a work-group a
* (row block, kv head, partition) -- one 8-row block alone is kv_heads work-groups of gqa threads,
* each walking the whole context. A partition's unnormalised state (m, l, O) goes to scratch,
* [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>
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) {
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) */
constexpr int VB = SPLIT ? 1 : 8;
constexpr int kQBytes = 8 * HD * 2; /* a thread's staged query rows */
const int64_t nqb = (rows + 8 * RB - 1) / (8 * RB);
namespace intelex = sycl::ext::intel::experimental;
syclex::properties props{intelex::grf_size<256>};
return submit([&] {
q.parallel_for(sycl::nd_range<1>{(size_t)(nqb * kv_heads * NP * gqa * RB), (size_t)(gqa * RB)}, props,
[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
using namespace esimd;
slm_init<MAXG * RB * kQBytes>();
const int tl = (int)it.get_local_id(0);
const int t = tl % gqa, rb = tl / gqa;
const int64_t wg0 = (int64_t)it.get_group(0);
const int part = (int)(wg0 % NP);
const int64_t wg = wg0 / NP;
const int64_t kh = wg % kv_heads, r0 = ((wg / kv_heads) * RB + rb) * 8;
const bool idle = r0 >= rows; /* a row block past the end: through the barrier, then out */
const int64_t hq = kh * gqa + t;
const int64_t ctx = ld_index(sudt, sub, 0);
const int64_t jend = ctx < jcap ? ctx : jcap;
const int64_t pos0 = ctx - rows + r0;
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;
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 je = jbeg + per * 32;
if (je < jhi) jhi = je;
if (jbeg >= jhi) { /* uniform over the work-group: no keys here */
for (int r = 0; r < nvalid; ++r) {
const int64_t pi = ((r0 + r) * kv_heads * gqa + hq) * NP + part;
ml[pi * 2] = -INFINITY;
ml[pi * 2 + 1] = 0.0f;
}
return;
}
}
/* the 8 query rows of head hq to local memory, [step][row][16]: a step's A operand one load */
const uint32_t qsl = (uint32_t)(tl * kQBytes);
const uint16_t* qh = qb + hq * q_hs;
#pragma unroll
for (int kk = 0; kk < KS / 2; ++kk) {
if (idle) break;
simd<uint16_t, 256> qq = load_2d<uint16_t, 16, 8, 2, false>(qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1),
(unsigned)(q_rs * 2 - 1), kk * 32, (int)r0);
slm_block_store<uint16_t, 256>(qsl + kk * 512, qq);
}
barrier();
if (idle) return;
simd<float, 128> O[KS];
#pragma unroll
for (int c = 0; c < KS; ++c) O[c] = 0.0f;
simd<float, 8> m = -INFINITY, l = 0.0f;
const simd<int32_t, 16> lane(0, 1);
const unsigned pw = (unsigned)(so * 2 - 1), ph = 15u; /* a page: 16 rows of so elements */
for (int64_t j0 = jbeg; j0 < jhi; j0 += 32) {
if constexpr (PF > 0) {
/* the next step's two pages to L1 while this one computes: a page of one kv head
* is 16 rows of so elements, one prefetch lane a 64-byte line, 2 KiB an instruction */
const int pp = tl < 2 ? tl : -1;
const int64_t jn = j0 + 32 * PF + 16 * pp;
if (pp >= 0 && jn < jhi) {
const int64_t bn = ld_index(btdt, btb, (jn / 16) * bt_cs);
if (bn >= 0) {
const char* base = (const char*)(cb + bn * sb + kh * sh);
const simd<uint32_t, 32> lo(0, 64);
#pragma unroll
for (int i = 0; i < 16 * 2 * HD * 2 / 2048; ++i) /* so == 2 HD: the caller checks */
xesimd::lsc_prefetch<uint32_t, 1, xesimd::lsc_data_size::default_size, xesimd::cache_hint::cached,
xesimd::cache_hint::cached, 32>((const uint32_t*)(base + i * 2048), lo);
}
}
}
simd<float, 128> S[2];
int64_t blk[2];
/* the two pages' score chains interleaved a head-dim step at a time: each was 16 DPAS
* deep on one accumulator, each waiting on the last (the same sums in the same order) */
const uint32_t* pgk[2];
#pragma unroll
for (int p = 0; p < 2; ++p) {
const int64_t jp = j0 + 16 * p;
blk[p] = jp < jhi ? ld_index(btdt, btb, (jp / 16) * bt_cs) : -1;
S[p] = 0.0f;
pgk[p] = (const uint32_t*)(cb + (blk[p] >= 0 ? blk[p] : 0) * sb + kh * sh);
}
if (blk[0] >= 0 && blk[1] >= 0) {
#pragma unroll
for (int k = 0; k < KS; ++k) {
simd<uint32_t, 64> qa = slm_block_load<uint32_t, 64>(qsl + k * 256);
simd<uint32_t, 128> k0 = load_2d<uint32_t, 8, 16, 1, true>(pgk[0], pw, ph, pw, k * 8, 0);
simd<uint32_t, 128> k1 = load_2d<uint32_t, 8, 16, 1, true>(pgk[1], pw, ph, pw, k * 8, 0);
S[0] = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16,
xmx::dpas_argument_type::bf16>(S[0], k0, qa);
S[1] = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16,
xmx::dpas_argument_type::bf16>(S[1], k1, qa);
}
} else {
#pragma unroll
for (int p = 0; p < 2; ++p) {
if (blk[p] < 0) continue;
#pragma unroll
for (int k = 0; k < KS; ++k) {
simd<uint32_t, 128> kt = load_2d<uint32_t, 8, 16, 1, true>(pgk[p], pw, ph, pw, k * 8, 0);
simd<uint32_t, 64> qa = slm_block_load<uint32_t, 64>(qsl + k * 256);
S[p] = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16,
xmx::dpas_argument_type::bf16>(S[p], kt, qa);
}
}
}
/* scale; hide keys past a row's position, past the context, or on an unmapped page */
simd<float, 8> mx;
#pragma unroll
for (int p = 0; p < 2; ++p) {
S[p] *= scale;
const simd<int32_t, 16> jj = lane + (int32_t)(j0 + 16 * p);
#pragma unroll
for (int r = 0; r < 8; ++r) {
simd_mask<16> hide = (jj > (int32_t)(pos0 + r)) | (jj >= (int32_t)jhi);
if (blk[p] < 0 || r >= nvalid) hide = 1;
S[p].template select<16, 1>(r * 16).merge(simd<float, 16>(-INFINITY), hide);
}
}
#pragma unroll
for (int r = 0; r < 8; ++r) {
const float a0 = hmax<float>(simd<float, 16>(S[0].template select<16, 1>(r * 16)));
const float a1 = hmax<float>(simd<float, 16>(S[1].template select<16, 1>(r * 16)));
mx[r] = a0 > a1 ? a0 : a1;
}
simd<float, 8> mn = max(m, mx);
simd<float, 8> base = mn;
base.merge(simd<float, 8>(0.0f), mn == -INFINITY); /* a row with nothing visible yet */
const simd<float, 8> corr = exp(m - base);
/* P = exp(S - base) in place over S, and the accumulator's rescale, a row at a time
* against scalar factors: the same values without 128-wide broadcast copies (the
* register file overflowed: 4 KiB of spill in the step loop) */
simd<float, 8> rs = 0.0f;
#pragma unroll
for (int p = 0; p < 2; ++p)
#pragma unroll
for (int r = 0; r < 8; ++r) {
simd<float, 16> x = S[p].template select<16, 1>(r * 16) - (float)base[r];
x = exp(x);
S[p].template select<16, 1>(r * 16) = x;
rs[r] += reduce<float>(x, std::plus<>());
}
l = l * corr + rs;
/* x * 1.0f is x: the rescale only when some row's max moved (rare deep in a context) */
if ((corr != 1.0f).any()) {
#pragma unroll
for (int c = 0; c < KS; ++c)
#pragma unroll
for (int r = 0; r < 8; ++r) O[c].template select<16, 1>(r * 16) *= (float)corr[r];
}
#pragma unroll
for (int p = 0; p < 2; ++p) {
if (blk[p] < 0) continue;
const simd<uint16_t, 128> pa = bf16_of(S[p]);
const uint16_t* pg = cb + blk[p] * sb + kh * sh;
#pragma unroll
for (int cv = 0; cv < KS; cv += VB) {
simd<uint16_t, 256> vb[VB];
#pragma unroll
for (int b = 0; b < VB; ++b)
vb[b] = load_2d<uint16_t, 16, 16, 1, false, true>(pg, pw, ph, pw, HD + (cv + b) * 16, 0);
#pragma unroll
for (int b = 0; b < VB; ++b)
O[cv + b] = xmx::dpas<8, 8, float, float, uint16_t, uint16_t, xmx::dpas_argument_type::bf16,
xmx::dpas_argument_type::bf16>(O[cv + b], vb[b], pa);
}
}
m = mn;
}
if constexpr (SPLIT) {
#pragma unroll
for (int r = 0; r < 8; ++r) {
if (r >= nvalid) break;
const int64_t pi = ((r0 + r) * kv_heads * gqa + hq) * NP + part;
ml[pi * 2] = m[r];
ml[pi * 2 + 1] = l[r];
simd<float, HD> row;
#pragma unroll
for (int c = 0; c < KS; ++c) row.template select<16, 1>(c * 16) = O[c].template select<16, 1>(r * 16);
block_store<float, HD>(op + pi * HD, row);
}
return;
}
simd<float, 8> inv = 1.0f / l;
inv.merge(simd<float, 8>(0.0f), l <= 0.0f); /* no visible key: a zero row, as libref */
#pragma unroll
for (int r = 0; r < 8; ++r) {
if (r >= nvalid) break;
simd<uint16_t, HD> row;
#pragma unroll
for (int c = 0; c < KS; ++c) {
simd<float, 16> v = O[c].template select<16, 1>(r * 16) * (float)inv[r];
simd<uint32_t, 16> u = v.template bit_cast_view<uint32_t>();
row.template select<16, 1>(c * 16) = simd<uint16_t, 16>((u + 0x7FFFu + ((u >> 16) & 1u)) >> 16);
}
block_store<uint16_t, HD>(ob + (r0 + r) * o_rs + hq * o_hs, row);
}
});
});
}
/* The decode / verify form at head_dim HD: the keys split kMaxP ways (partitions past the context exit at
* once), the parts merged by a sub-group a (row, head), HD / 16 values a lane. */
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) {
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;
const int64_t need = n * (2 + HD) * (int64_t)sizeof(float);
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);
if (rc != RAD_OK) return rc;
return submit([&] {
q.parallel_for(sycl::nd_range<1>{(size_t)(rows * q_heads) * 16, 16},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(16)]] {
const int64_t g = (int64_t)it.get_group(0); /* row * q_heads + head */
const int lane = (int)it.get_local_id(0);
const float* mlg = ml + g * kMaxP * 2;
float M = -INFINITY;
for (int p = 0; p < kMaxP; ++p)
if (mlg[p * 2 + 1] > 0.0f) M = sycl::fmax(M, mlg[p * 2]);
float L = 0.0f, acc[PL];
for (int e = 0; e < PL; ++e) acc[e] = 0.0f;
if (M != -INFINITY)
for (int p = 0; p < kMaxP; ++p) {
const float lp = mlg[p * 2 + 1];
if (!(lp > 0.0f)) continue;
const float f = sycl::exp(mlg[p * 2] - M);
L += lp * f;
const float* o = op + (g * kMaxP + p) * HD + lane * PL;
for (int e = 0; e < PL; ++e) acc[e] += o[e] * f;
}
const float inv = L > 0.0f ? 1.0f / L : 0.0f;
const int64_t row = g / q_heads, hq = g % q_heads;
uint16_t* orow = ob + row * o_rs + hq * o_hs + lane * PL;
for (int e = 0; e < PL; ++e) orow[e] = rad_f32_to_bf16(acc[e] * inv);
});
});
}
/* The prefill form at head_dim HD. */
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) {
/* 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);
}
} // namespace
/* attn_paged_fast's DPAS form at head_dim 64, 128 or 256; RAD_E_UNSUPPORTED when the call is not one
* it serves (the caller then runs the SIMT kernel). */
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;
#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);
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);
default: return RAD_E_UNSUPPORTED;
}
#undef XE2_FLASH_ARGS
}
} // namespace xe2