From 3467cc9d4ee2d07044f05bcb468d582d4ae5f44e Mon Sep 17 00:00:00 2001 From: maxfridbe Date: Wed, 30 Sep 2026 15:02:17 -0500 Subject: [PATCH] sycl: XMX prompt attention v2 (1.5x v1, k8v4 parity complete), long-context x KV-type table v2: 64-cell chunks, V packed two cells at a time as 32-byte stores, K^T as 4-byte pairs with INT8 converted on the load, hi+lo Q in one accumulator per scale group, one accumulator update per chunk. qsa_prompt_attn_parity passes in every mode and kv_hybrid_parity passes completely with it (its last step needs a tensor-core-class prompt kernel). 13.4 ms per chunk vs v1's 18.7 and the FP32 fallback's 5.5: the attention is gather-bound, so it stays opt-in (STRATA_PROMPT_ATTN_XMX=1, or =32 for 32-cell chunks). INTEL.md: 128K and 256K prompts with int8, q4_0 and k8v4 KV on the B70. All complete; q4_0 keeps every expert in VRAM at 128K (decode 32.7 tok/s after the prompt); at 256K all three evict experts and decode drops to 4-5. --- docs/INTEL.md | 24 + .../kernels/cuda/qsa_prompt_attn_xmx.dp.cpp | 412 ++++++++++-------- 2 files changed, 244 insertions(+), 192 deletions(-) diff --git a/docs/INTEL.md b/docs/INTEL.md index 70f39066..8b2a6429 100644 --- a/docs/INTEL.md +++ b/docs/INTEL.md @@ -287,6 +287,30 @@ output tokens between paths, never only timings. It costs ~1 s on short prompts (2,184 tokens: 610 vs 792 tok/s), so the port borrows by default only above a 32K context (`--prefill-borrow` / `--no-prefill-borrow` decide explicitly). +**Long contexts by KV type (2026-09-30, borrowing on, `--vram-reserve-mib 2048 --prefill 4096`).** Same text +repeated to length, 64 greedy tokens after it: + +| context | KV | prompt | decode after | peak VRAM | experts in VRAM (of 12,288) | +|---|---|---|---|---|---| +| 128K | int8 | 960 tok/s | 8.5 tok/s | 30.9 GB | 12,002 | +| 128K | q4_0 | 951 tok/s | 32.7 tok/s | 30.4 GB | 12,288 | +| 128K | k8v4 | 983 tok/s | 23.4 tok/s | 30.7 GB | 12,241 | +| 256K | q4_0 | 778 tok/s | 5.0 tok/s | 30.9 GB | 11,814 | +| 256K | k8v4 | 752 tok/s | 3.8 tok/s | 30.7 GB | 11,294 | +| 256K | int8 | 718 tok/s | 3.7 tok/s | 30.7 GB | 10,923 | + +Every configuration completes and answers coherently; decode after the prompt is set by how many experts the KV +leaves room for. At 128K use `--kv q4_0`. At 256K all three evict 470-1,400 experts; KV streaming +(`--kv-resident`) is the next thing to try there. k8v4 works through the FP32 attention fallback; its dedicated +prompt kernel is the XMX one below. + +**XMX prompt attention v2.** 64-cell chunks, vector-packed K^T and V, hi+lo Q in one accumulator per scale group, +one accumulator update per chunk: 1.4-1.5x faster than v1 and correct in all modes (`qsa_prompt_attn_parity`, +and `kv_hybrid_parity` passes completely with it), but still ~2x slower than the FP32 fallback (13.4 vs 5.5 ms per +chunk, INT8, 32K context). This attention is gather-bound: each query position selects its own ~2,000 cells, so +the K/V fetch dominates and only 12 of the 16 matrix rows are real heads. Opt-in: `STRATA_PROMPT_ATTN_XMX=1` +(64-cell chunks, 120 KB of local memory) or `=32`. + **Keeping up with upstream.** A merge of upstream `main` into `b70` leaves the copies in `sycl/` behind wherever upstream touched a file they mirror. They are refreshed by re-migration, not by hand (done for 0.1.25-0.1.27, 2026-09-30): diff --git a/sycl/src/kernels/cuda/qsa_prompt_attn_xmx.dp.cpp b/sycl/src/kernels/cuda/qsa_prompt_attn_xmx.dp.cpp index 9d3aa4df..15516ee7 100644 --- a/sycl/src/kernels/cuda/qsa_prompt_attn_xmx.dp.cpp +++ b/sycl/src/kernels/cuda/qsa_prompt_attn_xmx.dp.cpp @@ -1,11 +1,17 @@ -// SYCL port: the prompt attention on XMX (joint_matrix) - the port of qsa_prompt_attn.cu's tensor-core kernel. +// SYCL port: the prompt attention on XMX (joint_matrix) - the port of qsa_prompt_attn.cu's tensor-core kernel, v2. // -// One work-group per (query position, KV head): 16 query rows (the 12 heads of the group, 4 zero rows) against the -// position's selected cells, 16 cells per chunk. Scores S = Q K^T and the update O += P V run on joint_matrix -// (FP16 x FP16 -> FP32, 16x16x16, sub-group 16); Q and P are split into hi + lo FP16 halves exactly as the CUDA kernel -// does, and the per-64-dim INT8 scales of K and V are applied outside the matrix products the same way (K's after the -// group's product, V's folded into P). The online softmax and the accumulator merge are scalar over local memory. -// KV modes: 0 FP16 K/V, 1 INT8 K/V with scales, 3 INT8 K + q4_0 V dequantized to FP16. +// One work-group per (query position, KV head): 16 query rows (the group's 12 heads + 4 zero rows) against the +// position's selected cells, CH cells per chunk. Scores S = Q K^T and the update O += P V run on joint_matrix (FP16 x +// FP16 -> FP32, 16x16x16, sub-group 16). Q and P are split into hi + lo FP16 halves exactly as the CUDA kernel does, and +// the per-64-dim INT8 scales of K and V are applied outside the products the same way (K's after each scale group's +// product, V's folded into P relative to the chunk's largest). KV modes: 0 FP16 K/V, 1 INT8 K/V with scales, 3 hybrid +// K8V4 (INT8 K + Q4_0 V dequantized to FP16). +// +// v1 (16 cells per chunk) was correct but 3x slower than the FP32 fallback: its time went to staging (2-byte scattered +// stores into the packed B layouts) and to 5 barriers + a softmax pass + an accumulator read-modify-write per 16 cells. +// v2: 64-cell chunks (a quarter of the per-cell overhead), V packed two cells at a time as 32-byte contiguous stores, +// K^T packed as 4-byte pairs with INT8 converted on the load, hi and lo halves of Q accumulated into one accumulator per +// scale group, and one accumulator update per chunk after the whole P.V product has run in registers. #include "strata/kernels/qsa_prompt_attn_xmx.hpp" #include "strata/kernels/qsa_decode_attn.hpp" #include "strata/kernels/qsa.hpp" @@ -17,67 +23,69 @@ #include #include #include +#include namespace strata::kernels { namespace { namespace jm = sycl::ext::oneapi::experimental::matrix; -constexpr int HD = 256, G = 12, CH = 16, SG = 16, NSG = 8, THREADS = SG * NSG, QS = HD + 8, PS = CH + 8; +constexpr int HD = 256, G = 12, SG = 16, NSG = 8, THREADS = SG * NSG, QS = HD + 8; constexpr int QK4_0 = 32; struct q4_0_block { uint16_t d; uint8_t qs[QK4_0 / 2]; }; -struct Smem { - sycl::half* qh; // [16][QS] - sycl::half* ql; // [16][QS] - sycl::half* kp; // packed K^T: (k/2)*(CH*2) + c*2 + (k&1), k < HD, c < CH - sycl::half* vp; // packed V: (c/2)*(HD*2) + d*2 + (c&1) - sycl::half* pv; // [4 groups][2 hi/lo][16][PS] P tiles - float* ks; // [CH][4] - float* vs; // [CH][4] - float* s; // [16][PS] scores, then probabilities - float* acc; // [16][HD] - float* tmp; // [NSG][16][16] - float* misc; // qmax[8], mrow[16], lsum[16], alpha[16], vup[4], vdown[4] - long long* row; // [CH] +// local memory layout for CH cells per chunk; the P tiles reuse the K^T region (K is done once the scores are) +template +struct Layout { + static constexpr int PS = CH + 8; // P / score row stride + static constexpr int NT = CH / 16; // cell tiles + static constexpr int UNITS = NT * 4; // (cell tile, scale group) score units + static constexpr size_t q = 2 * 16 * QS * 2; // Q hi, Q lo (halves) + static constexpr size_t kp = (size_t) CH * HD * 2; // packed K^T, then the P tiles + static constexpr size_t pv = (size_t) 4 * 2 * 16 * PS * 2; + static constexpr size_t kp_or_pv = kp > pv ? kp : pv; + static constexpr size_t vp = (size_t) CH * HD * 2; // packed V + static constexpr size_t parts = (size_t) (UNITS > 16 ? UNITS : 16) * 256 * 4; // score units, then P.V tiles + static constexpr size_t s = (size_t) 16 * PS * 4; // probabilities + static constexpr size_t acc = (size_t) 16 * HD * 4; + static constexpr size_t scal = (size_t) CH * 4 * 4 * 2; // ks, vs + static constexpr size_t misc = 80 * 4; + static constexpr size_t row = (size_t) CH * 8; + static constexpr size_t bytes = q + kp_or_pv + vp + parts + s + acc + scal + misc + row + 16 * 12; }; -constexpr size_t kSmemBytes = (size_t) 2 * 16 * QS * 2 + (size_t) 2 * CH * HD * 2 + (size_t) 4 * 2 * 16 * PS * 2 + - (size_t) 2 * CH * 4 * 4 + (size_t) 16 * PS * 4 + (size_t) 16 * HD * 4 + (size_t) NSG * 256 * 4 + - (size_t) 72 * 4 + (size_t) CH * 8 + 64; - -__dpct_inline__ Smem carve(uint8_t* base) { - Smem m; - uint8_t* p = base; - auto take = [&](size_t bytes) { uint8_t* r = p; p += (bytes + 15) & ~(size_t) 15; return r; }; - m.qh = (sycl::half*) take(16 * QS * 2); - m.ql = (sycl::half*) take(16 * QS * 2); - m.kp = (sycl::half*) take((size_t) CH * HD * 2); - m.vp = (sycl::half*) take((size_t) CH * HD * 2); - m.pv = (sycl::half*) take((size_t) 4 * 2 * 16 * PS * 2); - m.ks = (float*) take(CH * 4 * 4); - m.vs = (float*) take(CH * 4 * 4); - m.s = (float*) take(16 * PS * 4); - m.acc = (float*) take((size_t) 16 * HD * 4); - m.tmp = (float*) take((size_t) NSG * 256 * 4); - m.misc = (float*) take(72 * 4); - m.row = (long long*) take(CH * 8); - return m; -} template __dpct_inline__ auto lptr(T* p) { return sycl::address_space_cast(p); } -__dpct_inline__ float sg_max(sycl::sub_group sg, float v) { - return sycl::reduce_over_group(sg, v, sycl::maximum()); -} - -template -void prompt_attn_xmx_kernel(const float* __restrict__ q, QsaAttnPools p, const int32_t* __restrict__ ids, - const int32_t* __restrict__ steps, int n_kv_heads, int page_size, float scale_log2, - float* __restrict__ attn, int cap, uint8_t* smem) { +template +void prompt_attn_xmx2(const float* __restrict__ q, QsaAttnPools p, const int32_t* __restrict__ ids, + const int32_t* __restrict__ steps, int n_kv_heads, int page_size, float scale_log2, + float* __restrict__ attn, int cap, uint8_t* smem) { + using L = Layout; + constexpr int PS = L::PS, NT = L::NT, UNITS = L::UNITS; auto it = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto sg = it.get_sub_group(); - Smem S = carve(smem); + uint8_t* at = smem; + auto take = [&](size_t b) { uint8_t* r = at; at += (b + 15) & ~(size_t) 15; return r; }; + sycl::half* qh = (sycl::half*) take(L::q / 2); + sycl::half* ql = (sycl::half*) take(L::q / 2); + sycl::half* kp = (sycl::half*) take(L::kp_or_pv); + sycl::half* ptile = kp; // [g][hi/lo][16][PS], after the scores + sycl::half* vp = (sycl::half*) take(L::vp); + float* parts = (float*) take(L::parts); + float* sp = (float*) take(L::s); + float* acc = (float*) take(L::acc); + float* ks = (float*) take(L::scal / 2); + float* vs = (float*) take(L::scal / 2); + float* misc = (float*) take(L::misc); + long long* rows = (long long*) take(L::row); + float* qmax = misc; // [8] + float* mrow = misc + 8; // [16] + float* lsum = misc + 24; // [16] + float* alpha = misc + 40; // [16] + float* vup = misc + 56; // [4] + float* vdown = misc + 60; // [4] + const int qi = (int) it.get_group(2), kvh = (int) it.get_group(1); const int n_head = n_kv_heads * G; q += (size_t) qi * n_head * HD + (size_t) kvh * G * HD; @@ -85,36 +93,31 @@ void prompt_attn_xmx_kernel(const float* __restrict__ q, QsaAttnPools p, const i ids += (size_t) qi * cap; const int n = steps[(size_t) qi * kStepCount + kStepWidth]; const int t = (int) it.get_local_id(2), lane = (int) sg.get_local_id()[0], sgid = (int) sg.get_group_id()[0]; - float* qmax = S.misc; - float* mrow = S.misc + 8; - float* lsum = S.misc + 24; - float* alpha = S.misc + 40; - float* vup = S.misc + 56; - float* vdown = S.misc + 60; - // q: 12 heads + 4 zero rows, scaled so its largest value sits near 2^14 (exact), split into hi + lo halves + + // q: scaled so its largest value sits near 2^14 (exact), split into hi + lo halves (as the CUDA kernel) float qm = 0.0f; for (int i = t; i < G * HD; i += THREADS) qm = sycl::fmax(qm, sycl::fabs(q[i])); - qm = sg_max(sg, qm); + qm = sycl::reduce_over_group(sg, qm, sycl::maximum()); if (lane == 0) qmax[sgid] = qm; it.barrier(sycl::access::fence_space::local_space); qm = 0.0f; for (int i = 0; i < NSG; ++i) qm = sycl::fmax(qm, qmax[i]); - int qe = 0; // frexp's exponent (qm < 2^qe) from the float's bits: normal numbers only, qm > 0 is + int qe = 0; // frexp's exponent from the bits (qm < 2^qe); qm > 0 is a normal number here if (qm > 0.0f) qe = (int) ((sycl::bit_cast(qm) >> 23) & 0xFFu) - 126; const float qup = sycl::ldexp(1.0f, 14 - qe), qdown = sycl::ldexp(scale_log2, qe - 14); for (int i = t; i < 16 * HD; i += THREADS) { const int h = i / HD, d = i % HD; const float x = h < G ? q[(size_t) h * HD + d] * qup : 0.0f; const sycl::half hi = sycl::half(x); - S.qh[h * QS + d] = hi; - S.ql[h * QS + d] = sycl::half(x - (float) hi); + qh[h * QS + d] = hi; + ql[h * QS + d] = sycl::half(x - (float) hi); } - for (int i = t; i < 16 * HD; i += THREADS) S.acc[i] = 0.0f; + for (int i = t; i < 16 * HD; i += THREADS) acc[i] = 0.0f; if (t < 16) { mrow[t] = -INFINITY; lsum[t] = 0.0f; } - // the K^T tile is the same for every chunk position: B(k, c) packed by k pairs + for (int c0 = 0; c0 < n; c0 += CH) { const int nh = sycl::min(CH, n - c0); - it.barrier(sycl::access::fence_space::local_space); // the previous chunk is done with kp, vp, s, tmp + // the chunk's rows (the previous chunk's P.V only reads vp/ptile, not rows) if (t < CH) { long long r = -1; if (t < nh) { @@ -122,70 +125,79 @@ void prompt_attn_xmx_kernel(const float* __restrict__ q, QsaAttnPools p, const i const long long page = (long long) p.page_table[cell / page_size]; r = (page * n_kv_heads + kvh) * page_size + (cell % page_size); } - S.row[t] = r; + rows[t] = r; } - it.barrier(sycl::access::fence_space::local_space); - // K rows -> packed K^T (raw INT8 codes as halves in modes 1 and 3; the scale comes after the product) + it.barrier(sycl::access::fence_space::local_space); // rows ready; the previous chunk is done with kp/vp/parts + // K rows -> packed K^T: element (dim k, cell c) at kp[(k/2)*(CH*2) + c*2 + (k&1)]; one item = 8 dims of a cell for (int i = t; i < CH * (HD / 8); i += THREADS) { const int c = i / (HD / 8), piece = i % (HD / 8); - const long long r = S.row[c]; - sycl::half h[8]; - if (r < 0) { - for (int j = 0; j < 8; ++j) h[j] = sycl::half(0.0f); - } else if constexpr (KV_MODE == 0) { - const sycl::vec v = *reinterpret_cast*>(p.k_pool + r * HD + piece * 8); - for (int j = 0; j < 8; ++j) h[j] = sycl::bit_cast(v[j]); - } else { - const sycl::vec v = *reinterpret_cast*>(p.k_q + r * HD + piece * 8); - for (int j = 0; j < 8; ++j) h[j] = sycl::half((float) v[j]); - } - for (int j = 0; j < 8; j += 2) { - const int k = piece * 8 + j; - sycl::half2* dst = reinterpret_cast(S.kp + (k >> 1) * (CH * 2) + c * 2); - *dst = sycl::half2(h[j], h[j + 1]); + const long long r = rows[c]; + uint32_t w[4] = {0u, 0u, 0u, 0u}; + if (r >= 0) { + if constexpr (KV_MODE == 0) { + const sycl::vec v = *reinterpret_cast*>(p.k_pool + r * HD + piece * 8); + for (int j = 0; j < 4; ++j) w[j] = v[j]; + } else { + const sycl::vec v = *reinterpret_cast*>(p.k_q + r * HD + piece * 8); + for (int j = 0; j < 4; ++j) { + const sycl::half2 h2((float) v[2 * j], (float) v[2 * j + 1]); + w[j] = sycl::bit_cast(h2); + } + } } + uint32_t* dst = reinterpret_cast(kp); + for (int j = 0; j < 4; ++j) dst[(size_t) (piece * 4 + j) * CH + c] = w[j]; } - // V rows -> packed V (pairs of cells) + // V rows -> packed V: element (cell c, dim d) at vp[(c/2)*(HD*2) + d*2 + (c&1)]; one item = a cell pair x 8 dims, + // written as 8 contiguous uint32 (32 bytes) if constexpr (KV_MODE == 3) { constexpr int BLKS = HD / QK4_0; constexpr int BYTES = BLKS * (int) sizeof(q4_0_block); - for (int i = t; i < CH * BLKS; i += THREADS) { - const int c = i / BLKS, b = i % BLKS; - const long long r = S.row[c]; - float d = 0.0f; - const q4_0_block* blk = nullptr; - if (r >= 0) { - blk = reinterpret_cast(p.v_q4 + r * BYTES) + b; - d = (float) sycl::bit_cast(blk->d); + for (int i = t; i < (CH / 2) * BLKS; i += THREADS) { + const int cp = i / BLKS, b = i % BLKS; + float v0[QK4_0], v1[QK4_0]; + for (int e = 0; e < 2; ++e) { + const long long r = rows[2 * cp + e]; + float* v = e ? v1 : v0; + if (r < 0) { for (int j = 0; j < QK4_0; ++j) v[j] = 0.0f; continue; } + const q4_0_block* blk = reinterpret_cast(p.v_q4 + r * BYTES) + b; + const float d = (float) sycl::bit_cast(blk->d); + for (int j = 0; j < QK4_0 / 2; ++j) { + v[j] = (float) ((int) (blk->qs[j] & 0x0F) - 8) * d; + v[j + QK4_0 / 2] = (float) ((int) (blk->qs[j] >> 4) - 8) * d; + } } - for (int j = 0; j < QK4_0 / 2; ++j) { - const float lo = blk ? (float) ((int) (blk->qs[j] & 0x0F) - 8) * d : 0.0f; - const float hi = blk ? (float) ((int) (blk->qs[j] >> 4) - 8) * d : 0.0f; - const int d0 = b * QK4_0 + j, d1 = d0 + QK4_0 / 2; - S.vp[(c >> 1) * (HD * 2) + d0 * 2 + (c & 1)] = sycl::half(lo); - S.vp[(c >> 1) * (HD * 2) + d1 * 2 + (c & 1)] = sycl::half(hi); + uint32_t* dst = reinterpret_cast(vp) + (size_t) cp * HD + b * QK4_0; + for (int j = 0; j < QK4_0; j += 8) { + sycl::vec o; + for (int u = 0; u < 8; ++u) o[u] = sycl::bit_cast(sycl::half2(v0[j + u], v1[j + u])); + *reinterpret_cast*>(dst + j) = o; } } } else { - for (int i = t; i < CH * (HD / 8); i += THREADS) { - const int c = i / (HD / 8), piece = i % (HD / 8); - const long long r = S.row[c]; - sycl::half h[8]; - if (r < 0) { - for (int j = 0; j < 8; ++j) h[j] = sycl::half(0.0f); - } else if constexpr (KV_MODE == 1) { - const sycl::vec v = *reinterpret_cast*>(p.v_q + r * HD + piece * 8); - for (int j = 0; j < 8; ++j) h[j] = sycl::half((float) v[j]); - } else { - const sycl::vec v = *reinterpret_cast*>(p.v_pool + r * HD + piece * 8); - for (int j = 0; j < 8; ++j) h[j] = sycl::bit_cast(v[j]); + for (int i = t; i < (CH / 2) * (HD / 8); i += THREADS) { + const int cp = i / (HD / 8), piece = i % (HD / 8); + sycl::half h0[8], h1[8]; + for (int e = 0; e < 2; ++e) { + const long long r = rows[2 * cp + e]; + sycl::half* h = e ? h1 : h0; + if (r < 0) { for (int j = 0; j < 8; ++j) h[j] = sycl::half(0.0f); continue; } + if constexpr (KV_MODE == 1) { + const sycl::vec v = *reinterpret_cast*>(p.v_q + r * HD + piece * 8); + for (int j = 0; j < 8; ++j) h[j] = sycl::half((float) v[j]); + } else { + const sycl::vec v = *reinterpret_cast*>(p.v_pool + r * HD + piece * 8); + for (int j = 0; j < 8; ++j) h[j] = sycl::bit_cast(v[j]); + } } - for (int j = 0; j < 8; ++j) S.vp[(c >> 1) * (HD * 2) + (piece * 8 + j) * 2 + (c & 1)] = h[j]; + sycl::vec o; + for (int j = 0; j < 8; ++j) o[j] = sycl::bit_cast(sycl::half2(h0[j], h1[j])); + *reinterpret_cast*>(reinterpret_cast(vp) + (size_t) cp * HD + piece * 8) = o; } } for (int i = t; i < CH * 4; i += THREADS) { const int c = i / 4, g = i % 4; - const long long r = S.row[c]; + const long long r = rows[c]; float a = 0.0f, b = 0.0f; if (r >= 0) { if constexpr (KV_MODE == 1) { @@ -198,50 +210,58 @@ void prompt_attn_xmx_kernel(const float* __restrict__ q, QsaAttnPools p, const i a = b = 1.0f; } } - S.ks[c * 4 + g] = a; - S.vs[c * 4 + g] = b; + ks[c * 4 + g] = a; + vs[c * 4 + g] = b; } it.barrier(sycl::access::fence_space::local_space); - // scores: sub-group sg takes dim group g = sg & 3 with the hi (sg < 4) or lo (sg >= 4) half of q - { - const int g = sgid & 3; - const sycl::half* qa = (sgid < 4 ? S.qh : S.ql); + // scores: unit u = (cell tile u / 4, scale group u % 4); hi and lo of Q into one accumulator (same K scale) + for (int u = sgid; u < UNITS; u += NSG) { + const int nt = u / 4, g = u % 4; jm::joint_matrix C; jm::joint_matrix_fill(sg, C, 0.0f); for (int kk = 0; kk < 4; ++kk) { const int k0 = (g * 4 + kk) * 16; - jm::joint_matrix A; jm::joint_matrix B; - jm::joint_matrix_load(sg, A, lptr(qa + k0), (size_t) QS); - jm::joint_matrix_load(sg, B, lptr(S.kp + (k0 >> 1) * (CH * 2)), (size_t) (CH * 2)); + jm::joint_matrix_load(sg, B, lptr(kp + (k0 >> 1) * (CH * 2) + nt * 16 * 2), (size_t) (CH * 2)); + jm::joint_matrix A; + jm::joint_matrix_load(sg, A, lptr(qh + k0), (size_t) QS); + jm::joint_matrix_mad(sg, C, A, B, C); + jm::joint_matrix_load(sg, A, lptr(ql + k0), (size_t) QS); jm::joint_matrix_mad(sg, C, A, B, C); } - jm::joint_matrix_store(sg, C, lptr(S.tmp + sgid * 256), (size_t) 16, jm::layout::row_major); + jm::joint_matrix_store(sg, C, lptr(parts + (size_t) u * 256), (size_t) 16, jm::layout::row_major); + } + // the V scale per group, folded into P relative to the chunk's largest (times 2^14, as in CUDA) + if (t < 4) { + float vmax = 0.0f; + for (int c = 0; c < CH; ++c) vmax = sycl::fmax(vmax, vs[c * 4 + t]); + vup[t] = vmax > 0.0f ? 16384.0f / vmax : 0.0f; + vdown[t] = vmax * (1.0f / 16384.0f); } it.barrier(sycl::access::fence_space::local_space); - for (int i = t; i < 16 * CH; i += THREADS) { - const int r = i / CH, c = i % CH; - float v = 0.0f; - for (int g = 0; g < 4; ++g) v += (S.tmp[g * 256 + r * 16 + c] + S.tmp[(g + 4) * 256 + r * 16 + c]) * S.ks[c * 4 + g]; - S.s[r * PS + c] = c < nh ? v * qdown : -INFINITY; - } - it.barrier(sycl::access::fence_space::local_space); - // online softmax: row t/8, 2 cells per thread, 8 lanes per row (within one sub-group) + // online softmax: row r = t / 8, CH / 8 cells per thread, the 8 lanes of a row in one sub-group { constexpr int PER = CH / 8; const int r = t >> 3, sub = t & 7; float x[PER], mx = -INFINITY; - for (int j = 0; j < PER; ++j) { x[j] = S.s[r * PS + sub * PER + j]; mx = sycl::fmax(mx, x[j]); } + for (int j = 0; j < PER; ++j) { + const int c = sub * PER + j, nt = c >> 4, cc = c & 15; + float v = 0.0f; + for (int g = 0; g < 4; ++g) v += parts[(size_t) (nt * 4 + g) * 256 + r * 16 + cc] * ks[c * 4 + g]; + x[j] = c < nh ? v * qdown : -INFINITY; + mx = sycl::fmax(mx, x[j]); + } for (int o = 1; o < 8; o <<= 1) mx = sycl::fmax(mx, sycl::permute_group_by_xor(sg, mx, o)); const float m_old = mrow[r]; const float m_new = sycl::fmax(m_old, mx); float sum = 0.0f; for (int j = 0; j < PER; ++j) { const float e = x[j] == -INFINITY ? 0.0f : sycl::exp2(x[j] - m_new); - S.s[r * PS + sub * PER + j] = e; + sp[r * PS + sub * PER + j] = e; sum += e; } for (int o = 1; o < 8; o <<= 1) sum += sycl::permute_group_by_xor(sg, sum, o); + sycl::group_barrier(sg); // every lane of the row read m_old before lane 0 moves it if (sub == 0) { const float a = m_old == -INFINITY ? 0.0f : sycl::exp2(m_old - m_new); alpha[r] = a; @@ -249,48 +269,47 @@ void prompt_attn_xmx_kernel(const float* __restrict__ q, QsaAttnPools p, const i mrow[r] = m_new; } } - // the V scale per dim group, folded into P relative to the chunk's largest (times 2^14, as in CUDA) - if (t < 4) { - float vmax = 0.0f; - for (int c = 0; c < CH; ++c) vmax = sycl::fmax(vmax, S.vs[c * 4 + t]); - vup[t] = vmax > 0.0f ? 16384.0f / vmax : 0.0f; - vdown[t] = vmax * (1.0f / 16384.0f); + it.barrier(sycl::access::fence_space::local_space); + // P tiles into the K^T region: groups share one pair when V carries no per-group scale (modes 0 and 3) + constexpr int NGP = KV_MODE == 1 ? 4 : 1; + for (int i = t; i < NGP * 16 * (CH / 2); i += THREADS) { + const int g = i / (16 * (CH / 2)), rc = i % (16 * (CH / 2)), r = rc / (CH / 2), c = (rc % (CH / 2)) * 2; + const float w0 = vs[c * 4 + g] * vup[g], w1 = vs[(c + 1) * 4 + g] * vup[g]; + const float p0 = sp[r * PS + c] * w0, p1 = sp[r * PS + c + 1] * w1; + const sycl::half h0 = sycl::half(p0), h1 = sycl::half(p1); + *reinterpret_cast(ptile + ((size_t) (g * 2 + 0) * 16 + r) * PS + c) = sycl::half2(h0, h1); + *reinterpret_cast(ptile + ((size_t) (g * 2 + 1) * 16 + r) * PS + c) = + sycl::half2(sycl::half(p0 - (float) h0), sycl::half(p1 - (float) h1)); } it.barrier(sycl::access::fence_space::local_space); - // P tiles: pv[g][hi/lo][r][c] = s[r][c] * vs[c][g] * vup[g], split into hi + lo halves - for (int i = t; i < 4 * 16 * CH; i += THREADS) { - const int g = i / (16 * CH), rc = i % (16 * CH), r = rc / CH, c = rc % CH; - const float pw = S.s[r * PS + c] * S.vs[c * 4 + g] * vup[g]; - const sycl::half hi = sycl::half(pw); - S.pv[((g * 2 + 0) * 16 + r) * PS + c] = hi; - S.pv[((g * 2 + 1) * 16 + r) * PS + c] = sycl::half(pw - (float) hi); - } - it.barrier(sycl::access::fence_space::local_space); - // p.v: sub-group sg owns dims [32 sg, 32 sg + 32) = two B tiles of scale group sg / 2 + // P.V: sub-group sg owns dims [32 sg, 32 sg + 32) (two B tiles) = V scale group sg / 2; the whole chunk's product + // in registers, then one update of its own accumulator columns { - const int g = sgid >> 1; - const sycl::half* ah = S.pv + ((g * 2 + 0) * 16) * PS; - const sycl::half* al = S.pv + ((g * 2 + 1) * 16) * PS; - jm::joint_matrix Ah, Al; - jm::joint_matrix_load(sg, Ah, lptr(ah), (size_t) PS); - jm::joint_matrix_load(sg, Al, lptr(al), (size_t) PS); - const float a_vdown = vdown[g]; + const int g = sgid >> 1, gp = KV_MODE == 1 ? g : 0; + const sycl::half* ah = ptile + (size_t) (gp * 2 + 0) * 16 * PS; + const sycl::half* al = ptile + (size_t) (gp * 2 + 1) * 16 * PS; + float* tmp = parts + (size_t) sgid * 512; for (int dt = 0; dt < 2; ++dt) { const int d0 = sgid * 32 + dt * 16; - jm::joint_matrix B; - jm::joint_matrix_load(sg, B, lptr(S.vp + d0 * 2), (size_t) (HD * 2)); jm::joint_matrix C; jm::joint_matrix_fill(sg, C, 0.0f); - jm::joint_matrix_mad(sg, C, Ah, B, C); - jm::joint_matrix_mad(sg, C, Al, B, C); - jm::joint_matrix_store(sg, C, lptr(S.tmp + sgid * 256), (size_t) 16, jm::layout::row_major); - sycl::group_barrier(sg); - for (int i = lane; i < 256; i += SG) { - const int r = i >> 4, c = i & 15; - float* a = S.acc + r * HD + d0 + c; - *a = sycl::fma(*a, alpha[r], S.tmp[sgid * 256 + i] * a_vdown); + for (int ks16 = 0; ks16 < CH; ks16 += 16) { + jm::joint_matrix B; + jm::joint_matrix_load(sg, B, lptr(vp + (size_t) (ks16 >> 1) * (HD * 2) + d0 * 2), (size_t) (HD * 2)); + jm::joint_matrix A; + jm::joint_matrix_load(sg, A, lptr(ah + ks16), (size_t) PS); + jm::joint_matrix_mad(sg, C, A, B, C); + jm::joint_matrix_load(sg, A, lptr(al + ks16), (size_t) PS); + jm::joint_matrix_mad(sg, C, A, B, C); } - sycl::group_barrier(sg); + jm::joint_matrix_store(sg, C, lptr(tmp + dt * 256), (size_t) 16, jm::layout::row_major); + } + sycl::group_barrier(sg); + const float vd = vdown[g]; + for (int i = lane; i < 512; i += SG) { + const int dt = i >> 8, rc = i & 255, r = rc >> 4, cc = rc & 15; + float* a = acc + r * HD + sgid * 32 + dt * 16 + cc; + *a = sycl::fma(*a, alpha[r], tmp[i] * vd); } } } @@ -298,14 +317,15 @@ void prompt_attn_xmx_kernel(const float* __restrict__ q, QsaAttnPools p, const i for (int i = t; i < G * HD; i += THREADS) { const int h = i / HD, d = i % HD; const float l = lsum[h]; - attn[(size_t) h * HD + d] = l > 0.0f ? S.acc[h * HD + d] / l : 0.0f; + attn[(size_t) h * HD + d] = l > 0.0f ? acc[h * HD + d] / l : 0.0f; } } -template +template bool launch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, const QsaShapes& s, float* attn, int64_t n_q, dpct::queue_ptr st) { const float scale_log2 = 1.4426950408889634f / std::sqrt((float) HD); + constexpr size_t bytes = Layout::bytes; for (int64_t q0 = 0; q0 < n_q; q0 += 65535) { const int64_t nb = n_q - q0 < 65535 ? n_q - q0 : 65535; const float* qq = q + q0 * s.n_head * HD; @@ -314,44 +334,52 @@ bool launch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const float* aq = attn + q0 * s.n_head * HD; const int nkv = (int) s.n_head_kv, ps = (int) s.page_size, capi = (int) cap; st->submit([&](sycl::handler& cgh) { - sycl::local_accessor slm(sycl::range<1>(kSmemBytes), cgh); + sycl::local_accessor slm(sycl::range<1>(bytes), cgh); cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, (size_t) nkv, (size_t) nb * THREADS), sycl::range<3>(1, 1, THREADS)), [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(16)]] { - prompt_attn_xmx_kernel(qq, pools, idq, stq, nkv, ps, scale_log2, aq, capi, - slm.get_multi_ptr().get()); + prompt_attn_xmx2(qq, pools, idq, stq, nkv, ps, scale_log2, aq, capi, + slm.get_multi_ptr().get()); }); }); } return true; } + +template +bool dispatch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, + const QsaShapes& s, float* attn, int64_t n_q, dpct::queue_ptr st) { + if (pools.k_q != nullptr && pools.v_q4 != nullptr) { + if (!pools.k_scale) return false; + return launch<3, CH>(q, pools, ids, steps, cap, s, attn, n_q, st); + } + if (pools.k_q != nullptr) { + if (!pools.v_q || !pools.k_scale || !pools.v_scale) return false; + return launch<1, CH>(q, pools, ids, steps, cap, s, attn, n_q, st); + } + if (!pools.k_pool || !pools.v_pool) return false; + return launch<0, CH>(q, pools, ids, steps, cap, s, attn, n_q, st); +} } // namespace bool qsa_prompt_attn_xmx(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap, const QsaShapes& s, float* attn, int64_t n_q, void* stream) { - // Off unless asked for: correct (qsa_prompt_attn_parity: 1e-6 of scale, like the CUDA kernel) but 3x slower than the - // FP32 fallback on the B70 (18.7 vs 5.5 ms per chunk at 32k context, 2026-09-30) - the chunk staging and packing - // dominate, not the products. Kept as the base for a tuned version (bigger chunks, vector packing). - static const bool on = std::getenv("STRATA_PROMPT_ATTN_XMX") && std::getenv("STRATA_PROMPT_ATTN_XMX")[0] == '1'; - if (!on || n_q <= 0) return false; + // STRATA_PROMPT_ATTN_XMX=1 (64-cell chunks) or =32 (32-cell chunks); unset or 0: the caller's fallback + static const int mode = [] { const char* v = std::getenv("STRATA_PROMPT_ATTN_XMX"); return v ? std::atoi(v) : 0; }(); + if (mode == 0 || n_q <= 0) return false; if (pools.k_q4 != nullptr || s.head_dim != HD || s.n_head != (int64_t) G * s.n_head_kv || cap <= 0 || !ids || !steps || !pools.page_table) return false; dpct::queue_ptr st = strata::q_of(stream); - static const bool fits = [&] { - const size_t have = st->get_device().get_info(); - if (have < kSmemBytes) std::fprintf(stderr, "strata: prompt attention on XMX needs %zu bytes of local memory, the device has %zu\n", kSmemBytes, have); - return have >= kSmemBytes; - }(); - if (!fits) return false; - if (pools.k_q != nullptr && pools.v_q4 != nullptr) { - if (!pools.k_scale) return false; - return launch<3>(q, pools, ids, steps, cap, s, attn, n_q, st); + const size_t have = st->get_device().get_info(); + const bool ch64 = mode != 32 && have >= Layout<64>::bytes; + static bool told = false; + if (!told) { + told = true; + std::fprintf(stderr, "strata: prompt attention on XMX, %d-cell chunks (%zu of %zu bytes of local memory)\n", + ch64 ? 64 : 32, ch64 ? Layout<64>::bytes : Layout<32>::bytes, have); } - if (pools.k_q != nullptr) { - if (!pools.v_q || !pools.k_scale || !pools.v_scale) return false; - return launch<1>(q, pools, ids, steps, cap, s, attn, n_q, st); - } - if (!pools.k_pool || !pools.v_pool) return false; - return launch<0>(q, pools, ids, steps, cap, s, attn, n_q, st); + if (!ch64 && have < Layout<32>::bytes) return false; + return ch64 ? dispatch<64>(q, pools, ids, steps, cap, s, attn, n_q, st) + : dispatch<32>(q, pools, ids, steps, cap, s, attn, n_q, st); } } // namespace strata::kernels