libxe2: prefill flash attention four row blocks a work-group, the next pages prefetched, V loads in batches of 8

At depth the kernel fell from 18 to 13.6 TFLOPS (a 2048-row chunk of Qwen3.8-27B's 24/4 heads,
head_dim 256, over 8k -> 64k of context): every 8-row work-group streamed its kv head's whole history,
and the stall samples were scoreboard waits on each step's loads, issued just before their DPAS.

- gqa <= 8: four 8-row blocks a work-group (the threads of one Xe-core walk the same pages, so a
  page comes from L2 once for 32 rows; 128 KiB of staged queries), and two of its threads prefetch
  the next step's two pages to L1 (one gather lane a 64-byte line). Each alone was worth little or
  lost (prefetch from 8-row work-groups: 7.9 TFLOPS at 64k); together 13.6 -> 19.6.
- the value loop's 2D loads 8 at a time before their DPAS (2, 4, 16 measured; the score loop's
  batched the same way spilled and lost).
- the accumulator rescale skipped when no row's max moved (x * 1.0f is x).

Every sum is in the order it was, so the output is unchanged: probe output bit-identical at
2k..64k, rad-kbench attn --all-rows 227/227, full replay 1410/1410, greedy 8/8.

Probe, 2048 rows over a context of 2k / 8k / 16k / 32k / 64k: 13.9 / 18.3 / 16.8 / 15.2 / 13.6 ->
16.7 / 25.0 / 24.0 / 23.4 / 22.3 TFLOPS. Fresh prompts (mtp4fc, MTP 6): ~2.1k 1423 tok/s, ~24k 1177,
~47k 952 (bench-standard's 32k / 64k depths measured 1077 / 806 at 7f95c56).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-11-03 01:29:41 +01:00
1 parent 7f95c567ad
commit 2da2eb0b6c
1 file changed
+52 -13
+52 -13
View File
@@ -19,6 +19,7 @@
#include "xe2_common.h"
#include <sycl/ext/intel/esimd.hpp>
#include <sycl/ext/intel/experimental/esimd/memory.hpp>
#include <cmath>
@@ -28,6 +29,7 @@ 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;
@@ -43,26 +45,31 @@ 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>
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 + 7) / 8;
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), (size_t)gqa}, props,
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 * kQBytes>();
const int t = (int)it.get_local_id(0);
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) * 8;
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;
@@ -86,15 +93,17 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
}
}
/* 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)(t * kQBytes);
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;
@@ -102,6 +111,23 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
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
@@ -175,20 +201,28 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const
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)
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];
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 c = 0; c < KS; ++c) {
simd<uint16_t, 256> vb = load_2d<uint16_t, 16, 16, 1, false, true>(pg, pw, ph, pw, HD + c * 16, 0);
O[c] = xmx::dpas<8, 8, float, float, uint16_t, uint16_t, xmx::dpas_argument_type::bf16,
xmx::dpas_argument_type::bf16>(O[c], vb, pa);
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;
@@ -273,6 +307,11 @@ int attn_flash_esimd(sycl::queue& q, int64_t head_dim, const uint16_t* qb, int64
});
});
}
/* gqa <= 8: four 8-row blocks a work-group (their key and value loads shared through L1, 128 KiB of
* staged queries) and the next step's pages prefetched */
if (gqa <= 8 && so == 2 * 256)
return launch<256, 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<256, false, 8> : launch<256, 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);
}