/* 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, 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" #include #include #include 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 bf16_of(esimd::simd v) SYCL_ESIMD_FUNCTION { using namespace esimd; simd u = v.template bit_cast_view(); return simd((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 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 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) */ 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(); 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; /* 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 - 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 */ 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 qq = load_2d(qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1), (unsigned)(q_rs * 2 - 1), kk * 32, (int)r0); slm_block_store(qsl + kk * 512, qq); } barrier(); if (idle) return; simd O[KS]; #pragma unroll for (int c = 0; c < KS; ++c) O[c] = 0.0f; simd m = -INFINITY, l = 0.0f; const simd 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 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((const uint32_t*)(base + i * 2048), lo); } } } 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; 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(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); } } } /* scale; hide keys past a row's position, past the context, or on an unmapped page */ simd mx; #pragma unroll for (int p = 0; p < 2; ++p) { S[p] *= scale; const simd 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 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); } } #pragma unroll for (int r = 0; r < 8; ++r) { const float a0 = hmax(simd(S[0].template select<16, 1>(r * 16))); const float a1 = hmax(simd(S[1].template select<16, 1>(r * 16))); mx[r] = a0 > a1 ? a0 : a1; } simd mn = max(m, mx); simd base = mn; base.merge(simd(0.0f), mn == -INFINITY); /* a row with nothing visible yet */ const simd 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 rs = 0.0f; #pragma unroll for (int p = 0; p < 2; ++p) #pragma unroll for (int r = 0; r < 8; ++r) { simd 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(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 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 vb[VB]; #pragma unroll for (int b = 0; b < VB; ++b) vb[b] = load_2d(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 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(op + pi * HD, row); } return; } simd inv = 1.0f / l; inv.merge(simd(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 row; #pragma unroll for (int c = 0; c < KS; ++c) { simd v = O[c].template select<16, 1>(r * 16) * (float)inv[r]; simd u = v.template bit_cast_view(); row.template select<16, 1>(c * 16) = simd((u + 0x7FFFu + ((u >> 16) & 1u)) >> 16); } block_store(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 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 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; 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; /* 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}, [=](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 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 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 (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 /* 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, 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, 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, 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 } } // namespace xe2