diff --git a/libxe2/xe2_attn_fast.cpp b/libxe2/xe2_attn_fast.cpp index deb58f3d..97659d4f 100644 --- a/libxe2/xe2_attn_fast.cpp +++ b/libxe2/xe2_attn_fast.cpp @@ -26,9 +26,16 @@ #include "xe2_ops.h" #include +#include namespace xe2 { +/* xe2_attn_flash_esimd.cpp */ +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); + namespace { constexpr int kSgs = 8; @@ -104,6 +111,23 @@ int attn_paged_fast(const RadArgs* a, RadStream st) { const bool vec_kv = KV->dtype == RAD_BF16 && sd == 1 && head_dim % 8 == 0 && sb % 8 == 0 && sh % 8 == 0 && so % 8 == 0 && rad_ptr_aligned(KV->data, 16); + /* A single sequence's prefill on DPAS (xe2_attn_flash_esimd.cpp): bf16 throughout, pages of 16, + * causal without a window, every 2D load's base and pitch on its alignment. */ + if (n_seq == 1 && rows >= 16 && causal && window == 0 && !KD && !VD && Q->dtype == RAD_BF16 && + KV->dtype == RAD_BF16 && O->dtype == RAD_BF16 && q_ds == 1 && o_ds == 1 && sd == 1 && block_size == 16 && + rad_ptr_aligned(Q->data, 64) && rad_ptr_aligned(KV->data, 64) && rad_ptr_aligned(O->data, 64) && + (q_rs * 2) % 64 == 0 && (q_hs * 2) % 64 == 0 && (so * 2) % 64 == 0 && (sb * 2) % 64 == 0 && + (sh * 2) % 64 == 0 && (o_rs * 2) % 64 == 0 && !std::getenv("XE2_ATTN_SIMT")) { + sycl::queue* fq = queue_of(st); + if (!fq) return RAD_E_DEVICE; + const float fscale = (float)rad_args_getf_or(a, "scale", (double)(1.0f / std::sqrt((float)head_dim))); + const int rc = attn_flash_esimd(*fq, head_dim, (const uint16_t*)Q->data, q_rs, q_hs, (const uint16_t*)KV->data, + sb, sh, so, (const char*)BT->data, BT->dtype, bt_cs, (const char*)SU->data, + SU->dtype, rows, kv_heads, gqa, fscale, max_blocks * block_size, + (uint16_t*)O->data, o_rs, o_hs); + if (rc != RAD_E_UNSUPPORTED) return rc; + } + /* The partitions: as many as the scratch holds for these rows, at most kMaxParts. Each row's * own visible span is split evenly on the device, so every partition has keys whatever the * context limit (splitting the limit instead left most partitions empty at a short context and diff --git a/libxe2/xe2_attn_flash_esimd.cpp b/libxe2/xe2_attn_flash_esimd.cpp new file mode 100644 index 00000000..fd9c7b27 --- /dev/null +++ b/libxe2/xe2_attn_flash_esimd.cpp @@ -0,0 +1,185 @@ +/* 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); +} + +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) { + 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 * 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 wg = (int64_t)it.get_group(0); + 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 */ + const int64_t jhi = jhi0 < jend ? jhi0 : jend; + /* 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 = 0; 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)jend); + 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); + simd bb; +#pragma unroll + for (int r = 0; r < 8; ++r) bb.template select<16, 1>(r * 16) = (float)base[r]; + simd P[2]; + simd rs = 0.0f; +#pragma unroll + for (int p = 0; p < 2; ++p) { + P[p] = exp(S[p] - bb); +#pragma unroll + for (int r = 0; r < 8; ++r) rs[r] += reduce(simd(P[p].template select<16, 1>(r * 16)), std::plus<>()); + } + l = l * corr + rs; + simd cb8; +#pragma unroll + for (int r = 0; r < 8; ++r) cb8.template select<16, 1>(r * 16) = (float)corr[r]; +#pragma unroll + for (int c = 0; c < KS; ++c) O[c] *= cb8; +#pragma unroll + for (int p = 0; p < 2; ++p) { + if (blk[p] < 0) continue; + const simd pa = bf16_of(P[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; + } + 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) { + if (head_dim != 256 || gqa < 1 || gqa > kMaxGqa) return RAD_E_UNSUPPORTED; + return launch<256>(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); +} + +} // namespace xe2