okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-q4-sycl (41717f29c): the Intel Arc (SYCL) engine follows the compiled geometry, so q4_lite/q4_mid/q4_big build; default build unchanged (CUDA SASS 3573/3573, HIP gfx1201 3920/3920, gfx1151 string offsets only, Metal 85/85 objects, SYCL 22/22 checksums); A750 gate 2 q4_lite 0.00021 vs floor 0.00017; one conflict with f-clean-6-0 in sycl/src/prefill/prefill.cpp resolved as tested by Q4-SYMERGE (Qwen4 prep)db137d917

main
21.8 KiB390 linesraw
1// SYCL port: the prompt attention on XMX (joint_matrix) - the port of qsa_prompt_attn.cu's tensor-core kernel, v2.
2//
3// One work-group per (query position, KV head): 16 query rows (the group's 12 heads + 4 zero rows) against the
4// 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
5// FP16 -> FP32, 16x16x16, sub-group 16). Q and P are split into hi + lo FP16 halves exactly as the CUDA kernel does, and
6// 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
7// 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
8// K8V4 (INT8 K + Q4_0 V dequantized to FP16).
9//
10// v1 (16 cells per chunk) was correct but 3x slower than the FP32 fallback: its time went to staging (2-byte scattered
11// stores into the packed B layouts) and to 5 barriers + a softmax pass + an accumulator read-modify-write per 16 cells.
12// v2: 64-cell chunks (a quarter of the per-cell overhead), V packed two cells at a time as 32-byte contiguous stores,
13// K^T packed as 4-byte pairs with INT8 converted on the load, hi and lo halves of Q accumulated into one accumulator per
14// scale group, and one accumulator update per chunk after the whole P.V product has run in registers.
15#include "strata/kernels/qsa_prompt_attn_xmx.hpp"
16#include "strata/kprof.hpp"
17#include "strata/core/geometry.hpp"
18#include "strata/kernels/qsa_decode_attn.hpp"
19#include "strata/kernels/qsa.hpp"
20#include "strata/kernels/kv_q8.hpp"
21#include "strata/sycl_queue.hpp"
22#include <sycl/sycl.hpp>
23#include <sycl/ext/oneapi/matrix/matrix.hpp>
24#include <dpct/dpct.hpp>
25#include <cmath>
26#include <cstdint>
27#include <cstdio>
28#include <cstdlib>
29
30namespace strata::kernels {
31namespace {
32namespace jm = sycl::ext::oneapi::experimental::matrix;
33constexpr int HD = 256, G = strata::core::kCompiledGeometry.n_head / strata::core::kCompiledGeometry.n_head_kv, SG = 16, NSG = 8, THREADS = SG * NSG, QS = HD + 8;
34static_assert(G >= 1 && G <= 16, "qsa_prompt_attn_xmx: query heads per KV head must be 1..16 (one 16-row tile)");
35static_assert(HD == strata::core::kCompiledGeometry.head_dim && G == strata::core::kCompiledGeometry.n_head / strata::core::kCompiledGeometry.n_head_kv,
36              "qsa_prompt_attn_xmx is compiled for core/geometry.hpp");
37constexpr int QK4_0 = 32;
38struct q4_0_block { uint16_t d; uint8_t qs[QK4_0 / 2]; };
39
40// local memory layout for CH cells per chunk; the P tiles reuse the K^T region (K is done once the scores are)
41template <int CH>
42struct Layout {
43    static constexpr int PS = CH + 8;                        // P / score row stride
44    static constexpr int NT = CH / 16;                       // cell tiles
45    static constexpr int UNITS = NT * 4;                     // (cell tile, scale group) score units
46    static constexpr size_t q = 2 * 16 * QS * 2;             // Q hi, Q lo (halves)
47    static constexpr size_t kp = (size_t) CH * HD * 2;       // packed K^T, then the P tiles
48    static constexpr size_t pv = (size_t) 4 * 2 * 16 * PS * 2;
49    static constexpr size_t kp_or_pv = kp > pv ? kp : pv;
50    static constexpr size_t vp = (size_t) CH * HD * 2;       // packed V
51    static constexpr size_t parts = (size_t) (UNITS > 16 ? UNITS : 16) * 256 * 4;   // score units, then P.V tiles
52    static constexpr size_t s = (size_t) 16 * PS * 4;        // probabilities
53    static constexpr size_t acc = (size_t) 16 * HD * 4;
54    static constexpr size_t scal = (size_t) CH * 4 * 4 * 2;  // ks, vs
55    static constexpr size_t misc = 80 * 4;
56    static constexpr size_t row = (size_t) CH * 8;
57    static constexpr size_t bytes = q + kp_or_pv + vp + parts + s + acc + scal + misc + row + 16 * 12;
58};
59
60template <typename T>
61__dpct_inline__ auto lptr(T* p) {
62    return sycl::address_space_cast<sycl::access::address_space::local_space, sycl::access::decorated::no>(p);
63}
64
65template <int KV_MODE, int CH>
66void prompt_attn_xmx2(const float* __restrict__ q, QsaAttnPools p, const int32_t* __restrict__ ids,
67                      const int32_t* __restrict__ steps, int n_kv_heads, int page_size, float scale_log2,
68                      float* __restrict__ attn, int cap, uint8_t* smem) {
69    using L = Layout<CH>;
70    constexpr int PS = L::PS, NT = L::NT, UNITS = L::UNITS;
71    auto it = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
72    auto sg = it.get_sub_group();
73    uint8_t* at = smem;
74    auto take = [&](size_t b) { uint8_t* r = at; at += (b + 15) & ~(size_t) 15; return r; };
75    sycl::half* qh = (sycl::half*) take(L::q / 2);
76    sycl::half* ql = (sycl::half*) take(L::q / 2);
77    sycl::half* kp = (sycl::half*) take(L::kp_or_pv);
78    sycl::half* ptile = kp;                                   // [g][hi/lo][16][PS], after the scores
79    sycl::half* vp = (sycl::half*) take(L::vp);
80    float* parts = (float*) take(L::parts);
81    float* sp = (float*) take(L::s);
82    float* acc = (float*) take(L::acc);
83    float* ks = (float*) take(L::scal / 2);
84    float* vs = (float*) take(L::scal / 2);
85    float* misc = (float*) take(L::misc);
86    long long* rows = (long long*) take(L::row);
87    float* qmax = misc;            // [8]
88    float* mrow = misc + 8;        // [16]
89    float* lsum = misc + 24;       // [16]
90    float* alpha = misc + 40;      // [16]
91    float* vup = misc + 56;        // [4]
92    float* vdown = misc + 60;      // [4]
93
94    const int qi = (int) it.get_group(2), kvh = (int) it.get_group(1);
95    const int n_head = n_kv_heads * G;
96    q += (size_t) qi * n_head * HD + (size_t) kvh * G * HD;
97    attn += (size_t) qi * n_head * HD + (size_t) kvh * G * HD;
98    ids += (size_t) qi * cap;
99    const int n = steps[(size_t) qi * kStepCount + kStepWidth];
100    const int t = (int) it.get_local_id(2), lane = (int) sg.get_local_id()[0], sgid = (int) sg.get_group_id()[0];
101
102    // q: scaled so its largest value sits near 2^14 (exact), split into hi + lo halves (as the CUDA kernel)
103    float qm = 0.0f;
104    for (int i = t; i < G * HD; i += THREADS) qm = sycl::fmax(qm, sycl::fabs(q[i]));
105    qm = sycl::reduce_over_group(sg, qm, sycl::maximum<float>());
106    if (lane == 0) qmax[sgid] = qm;
107    it.barrier(sycl::access::fence_space::local_space);
108    qm = 0.0f;
109    for (int i = 0; i < NSG; ++i) qm = sycl::fmax(qm, qmax[i]);
110    int qe = 0;   // frexp's exponent from the bits (qm < 2^qe); qm > 0 is a normal number here
111    if (qm > 0.0f) qe = (int) ((sycl::bit_cast<uint32_t>(qm) >> 23) & 0xFFu) - 126;
112    const float qup = sycl::ldexp(1.0f, 14 - qe), qdown = sycl::ldexp(scale_log2, qe - 14);
113    for (int i = t; i < 16 * HD; i += THREADS) {
114        const int h = i / HD, d = i % HD;
115        const float x = h < G ? q[(size_t) h * HD + d] * qup : 0.0f;
116        const sycl::half hi = sycl::half(x);
117        qh[h * QS + d] = hi;
118        ql[h * QS + d] = sycl::half(x - (float) hi);
119    }
120    for (int i = t; i < 16 * HD; i += THREADS) acc[i] = 0.0f;
121    if (t < 16) { mrow[t] = -INFINITY; lsum[t] = 0.0f; }
122
123    for (int c0 = 0; c0 < n; c0 += CH) {
124        const int nh = sycl::min(CH, n - c0);
125        // the chunk's rows (the previous chunk's P.V only reads vp/ptile, not rows)
126        if (t < CH) {
127            long long r = -1;
128            if (t < nh) {
129                const int cell = ids[c0 + t];
130                const long long page = (long long) p.page_table[cell / page_size];
131                r = (page * n_kv_heads + kvh) * page_size + (cell % page_size);
132            }
133            rows[t] = r;
134        }
135        it.barrier(sycl::access::fence_space::local_space);   // rows ready; the previous chunk is done with kp/vp/parts
136        // 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
137        for (int i = t; i < CH * (HD / 8); i += THREADS) {
138            const int c = i / (HD / 8), piece = i % (HD / 8);
139            const long long r = rows[c];
140            uint32_t w[4] = {0u, 0u, 0u, 0u};
141            if (r >= 0) {
142                if constexpr (KV_MODE == 0) {
143                    const sycl::vec<uint32_t, 4> v = *reinterpret_cast<const sycl::vec<uint32_t, 4>*>(p.k_pool + r * HD + piece * 8);
144                    for (int j = 0; j < 4; ++j) w[j] = v[j];
145                } else {
146                    const sycl::vec<int8_t, 8> v = *reinterpret_cast<const sycl::vec<int8_t, 8>*>(p.k_q + r * HD + piece * 8);
147                    for (int j = 0; j < 4; ++j) {
148                        const sycl::half2 h2((float) v[2 * j], (float) v[2 * j + 1]);
149                        w[j] = sycl::bit_cast<uint32_t>(h2);
150                    }
151                }
152            }
153            uint32_t* dst = reinterpret_cast<uint32_t*>(kp);
154            for (int j = 0; j < 4; ++j) dst[(size_t) (piece * 4 + j) * CH + c] = w[j];
155        }
156        // 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,
157        // written as 8 contiguous uint32 (32 bytes)
158        if constexpr (KV_MODE == 3) {
159            constexpr int BLKS = HD / QK4_0;
160            constexpr int BYTES = BLKS * (int) sizeof(q4_0_block);
161            for (int i = t; i < (CH / 2) * BLKS; i += THREADS) {
162                const int cp = i / BLKS, b = i % BLKS;
163                float v0[QK4_0], v1[QK4_0];
164                for (int e = 0; e < 2; ++e) {
165                    const long long r = rows[2 * cp + e];
166                    float* v = e ? v1 : v0;
167                    if (r < 0) { for (int j = 0; j < QK4_0; ++j) v[j] = 0.0f; continue; }
168                    const q4_0_block* blk = reinterpret_cast<const q4_0_block*>(p.v_q4 + r * BYTES) + b;
169                    const float d = (float) sycl::bit_cast<sycl::half>(blk->d);
170                    for (int j = 0; j < QK4_0 / 2; ++j) {
171                        v[j] = (float) ((int) (blk->qs[j] & 0x0F) - 8) * d;
172                        v[j + QK4_0 / 2] = (float) ((int) (blk->qs[j] >> 4) - 8) * d;
173                    }
174                }
175                uint32_t* dst = reinterpret_cast<uint32_t*>(vp) + (size_t) cp * HD + b * QK4_0;
176                for (int j = 0; j < QK4_0; j += 8) {
177                    sycl::vec<uint32_t, 8> o;
178                    for (int u = 0; u < 8; ++u) o[u] = sycl::bit_cast<uint32_t>(sycl::half2(v0[j + u], v1[j + u]));
179                    *reinterpret_cast<sycl::vec<uint32_t, 8>*>(dst + j) = o;
180                }
181            }
182        } else {
183            for (int i = t; i < (CH / 2) * (HD / 8); i += THREADS) {
184                const int cp = i / (HD / 8), piece = i % (HD / 8);
185                sycl::half h0[8], h1[8];
186                for (int e = 0; e < 2; ++e) {
187                    const long long r = rows[2 * cp + e];
188                    sycl::half* h = e ? h1 : h0;
189                    if (r < 0) { for (int j = 0; j < 8; ++j) h[j] = sycl::half(0.0f); continue; }
190                    if constexpr (KV_MODE == 1) {
191                        const sycl::vec<int8_t, 8> v = *reinterpret_cast<const sycl::vec<int8_t, 8>*>(p.v_q + r * HD + piece * 8);
192                        for (int j = 0; j < 8; ++j) h[j] = sycl::half((float) v[j]);
193                    } else {
194                        const sycl::vec<uint16_t, 8> v = *reinterpret_cast<const sycl::vec<uint16_t, 8>*>(p.v_pool + r * HD + piece * 8);
195                        for (int j = 0; j < 8; ++j) h[j] = sycl::bit_cast<sycl::half>(v[j]);
196                    }
197                }
198                sycl::vec<uint32_t, 8> o;
199                for (int j = 0; j < 8; ++j) o[j] = sycl::bit_cast<uint32_t>(sycl::half2(h0[j], h1[j]));
200                *reinterpret_cast<sycl::vec<uint32_t, 8>*>(reinterpret_cast<uint32_t*>(vp) + (size_t) cp * HD + piece * 8) = o;
201            }
202        }
203        for (int i = t; i < CH * 4; i += THREADS) {
204            const int c = i / 4, g = i % 4;
205            const long long r = rows[c];
206            float a = 0.0f, b = 0.0f;
207            if (r >= 0) {
208                if constexpr (KV_MODE == 1) {
209                    a = (float) sycl::bit_cast<sycl::half>(p.k_scale[r * (HD / KV_Q8_GROUP) + g]);
210                    b = (float) sycl::bit_cast<sycl::half>(p.v_scale[r * (HD / KV_Q8_GROUP) + g]);
211                } else if constexpr (KV_MODE == 3) {
212                    a = (float) sycl::bit_cast<sycl::half>(p.k_scale[r * (HD / KV_Q8_GROUP) + g]);
213                    b = 1.0f;
214                } else {
215                    a = b = 1.0f;
216                }
217            }
218            ks[c * 4 + g] = a;
219            vs[c * 4 + g] = b;
220        }
221        it.barrier(sycl::access::fence_space::local_space);
222        // scores: unit u = (cell tile u / 4, scale group u % 4); hi and lo of Q into one accumulator (same K scale)
223        for (int u = sgid; u < UNITS; u += NSG) {
224            const int nt = u / 4, g = u % 4;
225            jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 16, 16> C;
226            jm::joint_matrix_fill(sg, C, 0.0f);
227            for (int kk = 0; kk < 4; ++kk) {
228                const int k0 = (g * 4 + kk) * 16;
229                jm::joint_matrix<sycl::sub_group, sycl::half, jm::use::b, 16, 16, jm::layout::ext_intel_packed> B;
230                jm::joint_matrix_load(sg, B, lptr(kp + (k0 >> 1) * (CH * 2) + nt * 16 * 2), (size_t) (CH * 2));
231                jm::joint_matrix<sycl::sub_group, sycl::half, jm::use::a, 16, 16, jm::layout::row_major> A;
232                jm::joint_matrix_load(sg, A, lptr(qh + k0), (size_t) QS);
233                jm::joint_matrix_mad(sg, C, A, B, C);
234                jm::joint_matrix_load(sg, A, lptr(ql + k0), (size_t) QS);
235                jm::joint_matrix_mad(sg, C, A, B, C);
236            }
237            jm::joint_matrix_store(sg, C, lptr(parts + (size_t) u * 256), (size_t) 16, jm::layout::row_major);
238        }
239        // the V scale per group, folded into P relative to the chunk's largest (times 2^14, as in CUDA)
240        if (t < 4) {
241            float vmax = 0.0f;
242            for (int c = 0; c < CH; ++c) vmax = sycl::fmax(vmax, vs[c * 4 + t]);
243            vup[t] = vmax > 0.0f ? 16384.0f / vmax : 0.0f;
244            vdown[t] = vmax * (1.0f / 16384.0f);
245        }
246        it.barrier(sycl::access::fence_space::local_space);
247        // online softmax: row r = t / 8, CH / 8 cells per thread, the 8 lanes of a row in one sub-group
248        {
249            constexpr int PER = CH / 8;
250            const int r = t >> 3, sub = t & 7;
251            float x[PER], mx = -INFINITY;
252            for (int j = 0; j < PER; ++j) {
253                const int c = sub * PER + j, nt = c >> 4, cc = c & 15;
254                float v = 0.0f;
255                for (int g = 0; g < 4; ++g) v += parts[(size_t) (nt * 4 + g) * 256 + r * 16 + cc] * ks[c * 4 + g];
256                x[j] = c < nh ? v * qdown : -INFINITY;
257                mx = sycl::fmax(mx, x[j]);
258            }
259            for (int o = 1; o < 8; o <<= 1) mx = sycl::fmax(mx, sycl::permute_group_by_xor(sg, mx, o));
260            const float m_old = mrow[r];
261            const float m_new = sycl::fmax(m_old, mx);
262            float sum = 0.0f;
263            for (int j = 0; j < PER; ++j) {
264                const float e = x[j] == -INFINITY ? 0.0f : sycl::exp2(x[j] - m_new);
265                sp[r * PS + sub * PER + j] = e;
266                sum += e;
267            }
268            for (int o = 1; o < 8; o <<= 1) sum += sycl::permute_group_by_xor(sg, sum, o);
269            sycl::group_barrier(sg);   // every lane of the row read m_old before lane 0 moves it
270            if (sub == 0) {
271                const float a = m_old == -INFINITY ? 0.0f : sycl::exp2(m_old - m_new);
272                alpha[r] = a;
273                lsum[r] = sycl::fma(lsum[r], a, sum);
274                mrow[r] = m_new;
275            }
276        }
277        it.barrier(sycl::access::fence_space::local_space);
278        // P tiles into the K^T region: groups share one pair when V carries no per-group scale (modes 0 and 3)
279        constexpr int NGP = KV_MODE == 1 ? 4 : 1;
280        for (int i = t; i < NGP * 16 * (CH / 2); i += THREADS) {
281            const int g = i / (16 * (CH / 2)), rc = i % (16 * (CH / 2)), r = rc / (CH / 2), c = (rc % (CH / 2)) * 2;
282            const float w0 = vs[c * 4 + g] * vup[g], w1 = vs[(c + 1) * 4 + g] * vup[g];
283            const float p0 = sp[r * PS + c] * w0, p1 = sp[r * PS + c + 1] * w1;
284            const sycl::half h0 = sycl::half(p0), h1 = sycl::half(p1);
285            *reinterpret_cast<sycl::half2*>(ptile + ((size_t) (g * 2 + 0) * 16 + r) * PS + c) = sycl::half2(h0, h1);
286            *reinterpret_cast<sycl::half2*>(ptile + ((size_t) (g * 2 + 1) * 16 + r) * PS + c) =
287                sycl::half2(sycl::half(p0 - (float) h0), sycl::half(p1 - (float) h1));
288        }
289        it.barrier(sycl::access::fence_space::local_space);
290        // 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
291        // in registers, then one update of its own accumulator columns
292        {
293            const int g = sgid >> 1, gp = KV_MODE == 1 ? g : 0;
294            const sycl::half* ah = ptile + (size_t) (gp * 2 + 0) * 16 * PS;
295            const sycl::half* al = ptile + (size_t) (gp * 2 + 1) * 16 * PS;
296            float* tmp = parts + (size_t) sgid * 512;
297            for (int dt = 0; dt < 2; ++dt) {
298                const int d0 = sgid * 32 + dt * 16;
299                jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 16, 16> C;
300                jm::joint_matrix_fill(sg, C, 0.0f);
301                for (int ks16 = 0; ks16 < CH; ks16 += 16) {
302                    jm::joint_matrix<sycl::sub_group, sycl::half, jm::use::b, 16, 16, jm::layout::ext_intel_packed> B;
303                    jm::joint_matrix_load(sg, B, lptr(vp + (size_t) (ks16 >> 1) * (HD * 2) + d0 * 2), (size_t) (HD * 2));
304                    jm::joint_matrix<sycl::sub_group, sycl::half, jm::use::a, 16, 16, jm::layout::row_major> A;
305                    jm::joint_matrix_load(sg, A, lptr(ah + ks16), (size_t) PS);
306                    jm::joint_matrix_mad(sg, C, A, B, C);
307                    jm::joint_matrix_load(sg, A, lptr(al + ks16), (size_t) PS);
308                    jm::joint_matrix_mad(sg, C, A, B, C);
309                }
310                jm::joint_matrix_store(sg, C, lptr(tmp + dt * 256), (size_t) 16, jm::layout::row_major);
311            }
312            sycl::group_barrier(sg);
313            const float vd = vdown[g];
314            for (int i = lane; i < 512; i += SG) {
315                const int dt = i >> 8, rc = i & 255, r = rc >> 4, cc = rc & 15;
316                float* a = acc + r * HD + sgid * 32 + dt * 16 + cc;
317                *a = sycl::fma(*a, alpha[r], tmp[i] * vd);
318            }
319        }
320    }
321    it.barrier(sycl::access::fence_space::local_space);
322    for (int i = t; i < G * HD; i += THREADS) {
323        const int h = i / HD, d = i % HD;
324        const float l = lsum[h];
325        attn[(size_t) h * HD + d] = l > 0.0f ? acc[h * HD + d] / l : 0.0f;
326    }
327}
328
329template <int KV_MODE, int CH>
330bool launch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap,
331            const QsaShapes& s, float* attn, int64_t n_q, dpct::queue_ptr st) {
332    const float scale_log2 = 1.4426950408889634f / std::sqrt((float) HD);
333    constexpr size_t bytes = Layout<CH>::bytes;
334    for (int64_t q0 = 0; q0 < n_q; q0 += 65535) {
335        const int64_t nb = n_q - q0 < 65535 ? n_q - q0 : 65535;
336        const float* qq = q + q0 * s.n_head * HD;
337        const int32_t* idq = ids + q0 * cap;
338        const int32_t* stq = steps + q0 * kStepCount;
339        float* aq = attn + q0 * s.n_head * HD;
340        const int nkv = (int) s.n_head_kv, ps = (int) s.page_size, capi = (int) cap;
341        strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler& cgh) {
342            sycl::local_accessor<uint8_t, 1> slm(sycl::range<1>(bytes), cgh);
343            cgh.parallel_for(sycl::nd_range<3>(sycl::range<3>(1, (size_t) nkv, (size_t) nb * THREADS), sycl::range<3>(1, 1, THREADS)),
344                             [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(16)]] {
345                                 prompt_attn_xmx2<KV_MODE, CH>(qq, pools, idq, stq, nkv, ps, scale_log2, aq, capi,
346                                                               slm.get_multi_ptr<sycl::access::decorated::no>().get());
347                             });
348        });
349    }
350    return true;
351}
352
353template <int CH>
354bool dispatch(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap,
355              const QsaShapes& s, float* attn, int64_t n_q, dpct::queue_ptr st) {
356    if (pools.k_q != nullptr && pools.v_q4 != nullptr) {
357        if (!pools.k_scale) return false;
358        return launch<3, CH>(q, pools, ids, steps, cap, s, attn, n_q, st);
359    }
360    if (pools.k_q != nullptr) {
361        if (!pools.v_q || !pools.k_scale || !pools.v_scale) return false;
362        return launch<1, CH>(q, pools, ids, steps, cap, s, attn, n_q, st);
363    }
364    if (!pools.k_pool || !pools.v_pool) return false;
365    return launch<0, CH>(q, pools, ids, steps, cap, s, attn, n_q, st);
366}
367}  // namespace
368
369bool qsa_prompt_attn_xmx(const float* q, const QsaAttnPools& pools, const int32_t* ids, const int32_t* steps, int64_t cap,
370                         const QsaShapes& s, float* attn, int64_t n_q, void* stream) {
371    // STRATA_PROMPT_ATTN_XMX=1 (64-cell chunks) or =32 (32-cell chunks); unset or 0: the caller's fallback
372    static const int mode = [] { const char* v = std::getenv("STRATA_PROMPT_ATTN_XMX"); return v ? std::atoi(v) : 0; }();
373    if (mode == 0 || n_q <= 0) return false;
374    if (pools.k_q4 != nullptr || s.head_dim != HD || s.n_head != (int64_t) G * s.n_head_kv || cap <= 0 || !ids || !steps ||
375        !pools.page_table)
376        return false;
377    dpct::queue_ptr st = strata::q_of(stream);
378    const size_t have = st->get_device().get_info<sycl::info::device::local_mem_size>();
379    const bool ch64 = mode != 32 && have >= Layout<64>::bytes;
380    static bool told = false;
381    if (!told) {
382        told = true;
383        std::fprintf(stderr, "strata: prompt attention on XMX, %d-cell chunks (%zu of %zu bytes of local memory)\n",
384                     ch64 ? 64 : 32, ch64 ? Layout<64>::bytes : Layout<32>::bytes, have);
385    }
386    if (!ch64 && have < Layout<32>::bytes) return false;
387    return ch64 ? dispatch<64>(q, pools, ids, steps, cap, s, attn, n_q, st)
388                : dispatch<32>(q, pools, ids, steps, cap, s, attn, n_q, st);
389}
390}  // namespace strata::kernels