diff --git a/libxe2/xe2_attn_flash_esimd.cpp b/libxe2/xe2_attn_flash_esimd.cpp index 77ade71d..d5c80f7b 100644 --- a/libxe2/xe2_attn_flash_esimd.cpp +++ b/libxe2/xe2_attn_flash_esimd.cpp @@ -19,6 +19,7 @@ #include "xe2_common.h" #include +#include #include @@ -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 bf16_of(esimd::simd 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 +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 */ + /* 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(); - const int t = (int)it.get_local_id(0); + 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) * 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 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; @@ -102,6 +111,23 @@ int launch(sycl::queue& q, const uint16_t* qb, int64_t q_rs, int64_t q_hs, const 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 @@ -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(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 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); + 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; @@ -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); }