/* 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 #include namespace xe2 { namespace { namespace esimd = sycl::ext::intel::esimd; namespace xmx = sycl::ext::intel::esimd::xmx; 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) { constexpr int KS = HD / 16; /* head-dim steps */ constexpr int kQBytes = 8 * HD * 2; /* a thread's staged query rows */ const int64_t nqb = (rows + 7) / 8; 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, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { using namespace esimd; slm_init(); const int t = (int)it.get_local_id(0); 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 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)(t * kQBytes); const uint16_t* qh = qb + hq * q_hs; #pragma unroll for (int kk = 0; kk < KS / 2; ++kk) { 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(); 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) { simd S[2]; int64_t blk[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); #pragma unroll for (int k = 0; k < KS; ++k) { simd kt = load_2d(pg, 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 (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; #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 c = 0; c < KS; ++c) { simd vb = load_2d(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); } } 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); } }); }); } } // namespace /* attn_paged_fast's prefill form; 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 (head_dim != 256 || gqa < 1 || gqa > kMaxGqa) return RAD_E_UNSUPPORTED; if (rows < 16) { /* The decode / verify form: the keys split kMaxP ways (partitions past the context exit at * once), the parts merged by a sub-group a (row, head). */ constexpr int kMaxP = 32; const int64_t q_heads = kv_heads * gqa, n = rows * q_heads * kMaxP; const int64_t need = n * (2 + 256) * (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 * 256; int rc = (gqa <= 8 ? launch<256, true, 8> : launch<256, 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[16]; for (int e = 0; e < 16; ++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) * 256 + lane * 16; for (int e = 0; e < 16; ++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 * 16; for (int e = 0; e < 16; ++e) orow[e] = rad_f32_to_bf16(acc[e] * inv); }); }); } 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); } } // namespace xe2