/* xe2_attn_flash_quad.cpp -- attn_paged_fast's prefill flash attention, four threads a (16 query * rows, query head), at the default register file. * * The 8-row form (xe2_attn_flash_esimd.cpp) holds a thread's whole 8 x 256 f32 accumulator, which * takes it to the large register file and 4 threads an EU, and its stall samples were scoreboard * waits: too few threads to cover the latency of each step's loads and DPAS chains. Here a step is * 64 keys, four pages, and thread qd of a quad scores page qd for all 16 rows (two DPAS chains on * each key tile), the quad swaps row maxima, then probabilities and row sums, through local * memory, and each thread runs P V for its quarter of the head dims: a 16 x 64 f32 accumulator, * small enough for 128 registers and 8 threads an EU. A work-group is one 16-row group of one kv * head, gqa quads; every thread walks the same keys, so work-group barriers order the swaps. * * Every value is the 8-row form's. The softmax runs in the two 32-key halves of a step in turn, * as that form's steps did: the same maxima and bases, a row's sum page by page, the accumulator * rescaled between halves, and each accumulator takes the pages' DPAS in key order. Rows 0-7 walk * to rows 8-15's end; those keys are past their position, so hidden: P = 0, corr = 1, and * O + 0 * V = O. */ #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; 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); } template int launch_quad(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 qhn) { constexpr int KS = HD / 16; /* head-dim steps */ constexpr int CQ = KS / 4; /* a thread's output column blocks */ constexpr int VB = CQ; /* value loads a batch: the thread's four */ constexpr int kQ8 = 8 * HD * 2; /* 8 staged query rows */ constexpr int kQHead = 2 * kQ8; /* a head's 16 */ constexpr uint32_t kX = QSLM ? RG * QH * kQHead : 0; /* the swap area: a head's [qd] maxima, sums, P */ constexpr int kXHead = 4 * (16 * 4 + 16 * 4 + 16 * 16 * 2); const int64_t nrg = (rows + 16 * RG - 1) / (16 * RG); const int ngrp = gqa / qhn; /* work-groups a (16 rows, kv head): qhn <= QH query heads each */ return submit([&] { q.parallel_for(sycl::nd_range<1>{(size_t)(nrg * kv_heads * ngrp * 4 * qhn * RG), (size_t)(4 * qhn * RG)}, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { using namespace esimd; slm_init(); const int tl = (int)it.get_local_id(0); const int rgi = tl / (4 * qhn), tq = tl % (4 * qhn); /* the row group, the thread in it */ const int t = tq >> 2, qd = tq & 3; const int64_t wg = (int64_t)it.get_group(0); const int64_t hg = wg % ngrp, wr = wg / ngrp; const int64_t kh = wr % kv_heads, rw0 = (wr / kv_heads) * 16 * RG, r0 = rw0 + 16 * rgi; const int64_t hq = kh * gqa + hg * qhn + 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 < 16 ? (rows - r0 > 0 ? rows - r0 : 0) : 16); const int64_t jhi0 = pos0 + nvalid; const int64_t jhi = jhi0 < jend ? jhi0 : jend; /* the work-group's walk: its last row group's keys, so every thread joins every barrier * (an earlier group's extra keys are past its rows' positions: hidden) */ const int64_t rlast = (rw0 + 16 * RG < rows ? rw0 + 16 * RG : rows); const int64_t jl0 = ctx - rows + rlast; const int64_t jhw = jl0 < jend ? jl0 : jend; /* the head's 16 query rows to local memory, [half][step][row][16]: threads 0 and 1 of * the quad a half each (split four ways, by head-dim steps as well, the output was * wrong; not understood, and the staging is once a work-group) */ const uint32_t qh0 = (uint32_t)((rgi * QH + t) * kQHead); { const int hh = qd & 1; const uint16_t* qh = qb + hq * q_hs; if (KA) { /* Q^T a head-dim step: [8 u32 of dim pairs][16 rows], the B operand of S^T = K Q^T */ if (qd == 0 && r0 < rows) { #pragma clang loop unroll(full) for (int k = 0; k < KS; ++k) { simd qt = load_2d( (const uint32_t*)qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1), (unsigned)(q_rs * 2 - 1), k * 8, (int)r0); slm_block_store(qh0 + k * 512, qt); } } } else if (QSLM && qd < 2 && r0 + 8 * hh < rows) { #pragma clang loop unroll(full) for (int k2 = 0; k2 < KS / 2; ++k2) { /* pairs of head-dim steps */ simd qq = load_2d( qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1), (unsigned)(q_rs * 2 - 1), k2 * 32, (int)(r0 + 8 * hh)); slm_block_store(qh0 + hh * kQ8 + k2 * 512, qq); } } } const uint32_t xb = kX + (uint32_t)((rgi * QH + t) * kXHead); const uint32_t xmax = xb, xsum = xb + 4 * 64, xp = xb + 8 * 64; /* [qd][16] f32, [qd][16] f32, [qd][2][128] bf16 */ barrier(); simd O[2][CQ]; #pragma clang loop unroll(full) for (int h = 0; h < 2; ++h) #pragma clang loop unroll(full) for (int c = 0; c < CQ; ++c) O[h][c] = 0.0f; simd m = -INFINITY, l = 0.0f; const simd lane(0, 1); const unsigned pw = (unsigned)(so * 2 - 1), ph = 15u; for (int64_t j0 = 0; j0 < jhw; j0 += 64) { /* the next step's four pages to L1, a page a thread of the first quad */ if (tl < 4 && j0 + 64 + 16 * tl < jhw) { const int64_t bn = ld_index(btdt, btb, ((j0 + 64 + 16 * tl) / 16) * bt_cs); if (bn >= 0) { const char* base = (const char*)(cb + bn * sb + kh * sh); const simd lo(0, 64); #pragma clang loop unroll(full) 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); } } const bool two = j0 + 32 < jhw; /* uniform: the step's second half has keys */ int64_t blk[4]; #pragma clang loop unroll(full) for (int p = 0; p < 4; ++p) { const int64_t jp = j0 + 16 * p; blk[p] = jp < jhi ? ld_index(btdt, btb, (jp / 16) * bt_cs) : -1; } /* this thread's page: rows 0-7 and 8-15, two chains on one key tile */ const int64_t bo = blk[qd]; simd S0 = 0.0f, S1 = 0.0f; simd mo; if constexpr (KA) { /* S^T: keys 0-7 (S0) and 8-15 (S1) of the page by the 16 query rows, K the A * operand (plain 2D loads), the row maxima vertical across the key rows */ if (bo >= 0) { const uint16_t* pk = cb + bo * sb + kh * sh; #pragma clang loop unroll(full) for (int k = 0; k < KS; ++k) { simd qt = slm_block_load(qh0 + k * 512); simd k0 = load_2d(pk, pw, ph, pw, k * 16, 0); simd k1 = load_2d(pk, pw, ph, pw, k * 16, 8); S0 = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16, xmx::dpas_argument_type::bf16>(S0, qt, simd(k0.template bit_cast_view())); S1 = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16, xmx::dpas_argument_type::bf16>(S1, qt, simd(k1.template bit_cast_view())); } } S0 *= scale; S1 *= scale; const simd qpos = lane + (int32_t)pos0; const simd_mask<16> qbad = lane >= nvalid; mo = -INFINITY; #pragma clang loop unroll(full) for (int kr = 0; kr < 8; ++kr) { const int32_t ja = (int32_t)(j0 + 16 * qd + kr), jb = ja + 8; simd_mask<16> h0 = (ja > qpos) | qbad, h1 = (jb > qpos) | qbad; if (bo < 0 || ja >= (int32_t)jhi) h0 = 1; if (bo < 0 || jb >= (int32_t)jhi) h1 = 1; S0.template select<16, 1>(kr * 16).merge(simd(-INFINITY), h0); S1.template select<16, 1>(kr * 16).merge(simd(-INFINITY), h1); mo = max(mo, simd(S0.template select<16, 1>(kr * 16))); mo = max(mo, simd(S1.template select<16, 1>(kr * 16))); } } else { if (bo >= 0) { const uint32_t* pk = (const uint32_t*)(cb + bo * sb + kh * sh); #pragma clang loop unroll(full) for (int k = 0; k < KS; ++k) { simd qa0, qa1; if constexpr (QSLM) { qa0 = slm_block_load(qh0 + k * 256); qa1 = slm_block_load(qh0 + kQ8 + k * 256); } else { /* from the query rows, through L1 */ const uint16_t* qh = qb + hq * q_hs; simd u0 = load_2d( qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1), (unsigned)(q_rs * 2 - 1), k * 16, (int)r0); simd u1 = load_2d( qh, (unsigned)(HD * 2 - 1), (unsigned)(rows - 1), (unsigned)(q_rs * 2 - 1), k * 16, (int)(r0 + 8)); qa0 = u0.template bit_cast_view(); qa1 = u1.template bit_cast_view(); } simd kt = load_2d(pk, pw, ph, pw, k * 8, 0); S0 = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16, xmx::dpas_argument_type::bf16>(S0, kt, qa0); S1 = xmx::dpas<8, 8, float, float, uint32_t, uint32_t, xmx::dpas_argument_type::bf16, xmx::dpas_argument_type::bf16>(S1, kt, qa1); } } S0 *= scale; S1 *= scale; const simd jj = lane + (int32_t)(j0 + 16 * qd); #pragma clang loop unroll(full) for (int r = 0; r < 8; ++r) { simd_mask<16> h0 = (jj > (int32_t)(pos0 + r)) | (jj >= (int32_t)jhi); simd_mask<16> h1 = (jj > (int32_t)(pos0 + 8 + r)) | (jj >= (int32_t)jhi); if (bo < 0 || r >= nvalid) h0 = 1; if (bo < 0 || 8 + r >= nvalid) h1 = 1; S0.template select<16, 1>(r * 16).merge(simd(-INFINITY), h0); S1.template select<16, 1>(r * 16).merge(simd(-INFINITY), h1); mo[r] = hmax(simd(S0.template select<16, 1>(r * 16))); mo[8 + r] = hmax(simd(S1.template select<16, 1>(r * 16))); } } slm_block_store(xmax + qd * 64, mo); barrier(); /* the halves' maxima and bases, in turn: half 0 from m, half 1 from half 0's */ simd mh[2], baseh[2], corrh[2]; simd mprev = m; #pragma clang loop unroll(full) for (int h = 0; h < 2; ++h) { if (h == 1 && !two) break; const simd a0 = slm_block_load(xmax + (2 * h) * 64); const simd a1 = slm_block_load(xmax + (2 * h + 1) * 64); simd mx = a1; mx.merge(a0, a0 > a1); mh[h] = max(mprev, mx); baseh[h] = mh[h]; baseh[h].merge(simd(0.0f), mh[h] == -INFINITY); corrh[h] = exp(mprev - baseh[h]); mprev = mh[h]; } const int myh = qd >> 1; simd ro = 0.0f; if constexpr (KA) { if (myh == 0 || two) { const simd bs = baseh[myh]; #pragma clang loop unroll(full) for (int kr = 0; kr < 8; ++kr) { S0.template select<16, 1>(kr * 16) = exp(simd(S0.template select<16, 1>(kr * 16)) - bs); S1.template select<16, 1>(kr * 16) = exp(simd(S1.template select<16, 1>(kr * 16)) - bs); } /* a row's sum over its 16 keys in reduce<>'s order: lanes i and i + 8, then * i + 4, i + 2, i + 1 -- key rows here */ simd t8[8]; #pragma clang loop unroll(full) for (int i = 0; i < 8; ++i) t8[i] = simd(S0.template select<16, 1>(i * 16)) + simd(S1.template select<16, 1>(i * 16)); #pragma clang loop unroll(full) for (int i = 0; i < 4; ++i) t8[i] = t8[i] + t8[i + 4]; #pragma clang loop unroll(full) for (int i = 0; i < 2; ++i) t8[i] = t8[i] + t8[i + 2]; ro = t8[0] + t8[1]; } slm_block_store(xsum + qd * 64, ro); /* P to [query][key], two keys a 32-bit lane: the layout the P V step reads */ const simd qo = simd(lane) * 32u + (uint32_t)(xp + qd * 512); #pragma clang loop unroll(full) for (int kp = 0; kp < 8; ++kp) { const int ka = 2 * kp, kb = ka + 1; /* key rows: 0-7 in S0, 8-15 in S1 */ simd fa = ka < 8 ? simd(S0.template select<16, 1>(ka * 16)) : simd(S1.template select<16, 1>((ka - 8) * 16)); simd fb = kb < 8 ? simd(S0.template select<16, 1>(kb * 16)) : simd(S1.template select<16, 1>((kb - 8) * 16)); simd ua = fa.template bit_cast_view(), ub = fb.template bit_cast_view(); ua = (ua + 0x7FFFu + ((ua >> 16) & 1u)) >> 16; ub = (ub + 0x7FFFu + ((ub >> 16) & 1u)) >> 16; slm_scatter(qo + (uint32_t)(ka * 2), ua | (ub << 16)); } } else { if (myh == 0 || two) { const simd bs = baseh[myh]; #pragma clang loop unroll(full) for (int r = 0; r < 8; ++r) { simd x = exp(S0.template select<16, 1>(r * 16) - (float)bs[r]); S0.template select<16, 1>(r * 16) = x; ro[r] = reduce(x, std::plus<>()); simd y = exp(S1.template select<16, 1>(r * 16) - (float)bs[8 + r]); S1.template select<16, 1>(r * 16) = y; ro[8 + r] = reduce(y, std::plus<>()); } } slm_block_store(xsum + qd * 64, ro); slm_block_store(xp + qd * 512, bf16_of(S0)); slm_block_store(xp + qd * 512 + 256, bf16_of(S1)); } barrier(); #pragma clang loop unroll(full) for (int h = 0; h < 2; ++h) { if (h == 1 && !two) break; simd rs = 0.0f; rs += slm_block_load(xsum + (2 * h) * 64); rs += slm_block_load(xsum + (2 * h + 1) * 64); const simd corr = corrh[h]; l = l * corr + rs; if ((corr != 1.0f).any()) { #pragma clang loop unroll(full) for (int c = 0; c < CQ; ++c) #pragma clang loop unroll(full) for (int r = 0; r < 8; ++r) { O[0][c].template select<16, 1>(r * 16) *= (float)corr[r]; O[1][c].template select<16, 1>(r * 16) *= (float)corr[8 + r]; } } #pragma clang loop unroll(full) for (int p = 2 * h; p < 2 * h + 2; ++p) { if (blk[p] < 0) continue; const simd pa0 = slm_block_load(xp + p * 512); const simd pa1 = slm_block_load(xp + p * 512 + 256); const uint16_t* pg = cb + blk[p] * sb + kh * sh; #pragma clang loop unroll(full) for (int cv = 0; cv < CQ; cv += VB) { simd vb[VB]; #pragma clang loop unroll(full) for (int b = 0; b < VB; ++b) vb[b] = load_2d(pg, pw, ph, pw, HD + (qd * CQ + cv + b) * 16, 0); #pragma clang loop unroll(full) for (int b = 0; b < VB; ++b) { O[0][cv + b] = xmx::dpas<8, 8, float, float, uint16_t, uint16_t, xmx::dpas_argument_type::bf16, xmx::dpas_argument_type::bf16>(O[0][cv + b], vb[b], pa0); O[1][cv + b] = xmx::dpas<8, 8, float, float, uint16_t, uint16_t, xmx::dpas_argument_type::bf16, xmx::dpas_argument_type::bf16>(O[1][cv + b], vb[b], pa1); } } } } m = mprev; } simd inv = 1.0f / l; inv.merge(simd(0.0f), l <= 0.0f); #pragma clang loop unroll(full) for (int h = 0; h < 2; ++h) #pragma clang loop unroll(full) for (int r = 0; r < 8; ++r) { if (8 * h + r >= nvalid) break; simd row; #pragma clang loop unroll(full) for (int c = 0; c < CQ; ++c) { simd v = O[h][c].template select<16, 1>(r * 16) * (float)inv[8 * h + 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 + 8 * h + r) * o_rs + hq * o_hs + qd * (HD / 4), row); } }); }); } } // namespace /* RAD_E_UNSUPPORTED when the call is not one it serves (the caller then runs the 8-row form). */ int attn_flash_quad(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) { if ((head_dim != 64 && head_dim != 128 && head_dim != 256) || so != 2 * head_dim || gqa < 1 || gqa > 8 || rows < 16) return RAD_E_UNSUPPORTED; #ifndef XE2_FAQ_KA #define XE2_FAQ_KA 1 #endif /* gqa <= 6: two 16-row groups a work-group, 48 threads walking the same pages (126 KiB of staged * queries and swap area at head_dim 256; one group a work-group was 28.3 TFLOPS at 64k, two 30.3); * gqa 7-8 one */ #define XE2_QUAD(HD) \ (gqa <= 6 ? launch_quad(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, (int)gqa) \ : launch_quad(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, (int)gqa)) if (head_dim == 64) return XE2_QUAD(64); if (head_dim == 128) return XE2_QUAD(128); return XE2_QUAD(256); #undef XE2_QUAD } } // namespace xe2