From c5fe770657925eef1780eb57578822ed741288c1 Mon Sep 17 00:00:00 2001 From: claude Date: Mon, 2 Nov 2026 12:26:55 +0100 Subject: [PATCH] libxe2: prefill flash attention's two pages' score chains interleaved S = Q K^T for a step's two pages ran as two chains of 16 DPAS, one after the other, each DPAS waiting on the last's accumulator. With both pages mapped they now advance together a head-dim step at a time, the same sums in the same order; a step with one page keeps the single chain. (Tried before the large-GRF fix it was slower: the extra K operand spilled.) Probe fa.cpp (24/4 heads, head_dim 256): 8192 tokens 15.6 -> 18.9 TFLOPS, 2048 tokens 13.4 -> 13.5; output identical. rad-kbench attn --all-rows 227/227. qwen3.8-27b-w4a16 (mtp4fc), fresh prompts: prefill ~6.9k tokens 1301 -> 1326 tok/s, ~2.1k 1376 -> 1381; greedy 8/8 identical. Co-Authored-By: Claude Opus 5.5 --- libxe2/xe2_attn_flash_esimd.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/libxe2/xe2_attn_flash_esimd.cpp b/libxe2/xe2_attn_flash_esimd.cpp index 4fdab71f..77ade71d 100644 --- a/libxe2/xe2_attn_flash_esimd.cpp +++ b/libxe2/xe2_attn_flash_esimd.cpp @@ -104,16 +104,34 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const for (int64_t j0 = jbeg; j0 < jhi; j0 += 32) { simd 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; - if (blk[p] >= 0) { - const uint32_t* pg = (const uint32_t*)(cb + blk[p] * sb + kh * sh); + 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 qa = slm_block_load(qsl + k * 256); + simd k0 = load_2d(pgk[0], pw, ph, pw, k * 8, 0); + simd k1 = load_2d(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 kt = load_2d(pg, pw, ph, pw, k * 8, 0); + simd kt = load_2d(pgk[p], pw, ph, pw, k * 8, 0); simd qa = slm_block_load(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);