okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
db137d917
main
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 { 43static constexpr int PS = CH + 8 ;// P / score row stride 44static constexpr int NT = CH /16 ;// cell tiles 45static constexpr int UNITS = NT * 4 ;// (cell tile, scale group) score units 46static constexpr size_t q = 2 * 16 * QS * 2 ;// Q hi, Q lo (halves) 47static constexpr size_t kp = (size_t )CH * HD * 2 ;// packed K^T, then the P tiles 48static constexpr size_t pv = (size_t )4 * 2 * 16 * PS * 2 ; 49static constexpr size_t kp_or_pv = kp > pv ?kp :pv ; 50static constexpr size_t vp = (size_t )CH * HD * 2 ;// packed V 51static constexpr size_t parts = (size_t ) (UNITS > 16 ?UNITS :16 )* 256 * 4 ;// score units, then P.V tiles 52static constexpr size_t s = (size_t )16 * PS * 4 ;// probabilities 53static constexpr size_t acc = (size_t )16 * HD * 4 ; 54static constexpr size_t scal = (size_t )CH * 4 * 4 * 2 ;// ks, vs 55static constexpr size_t misc = 80 * 4 ; 56static constexpr size_t row = (size_t )CH * 8 ; 57static 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 ) { 62return 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 , 67const int32_t * __restrict__steps ,int n_kv_heads ,int page_size ,float scale_log2 , 68float * __restrict__attn ,int cap ,uint8_t * smem ) { 69using L = Layout < CH > ; 70constexpr int PS = L ::PS ,NT = L ::NT ,UNITS = L ::UNITS ; 71auto it = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 72auto sg = it .get_sub_group (); 73uint8_t * at = smem ; 74auto 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 ); 80float * parts = (float * )take (L ::parts ); 81float * sp = (float * )take (L ::s ); 82float * acc = (float * )take (L ::acc ); 83float * ks = (float * )take (L ::scal /2 ); 84float * vs = (float * )take (L ::scal /2 ); 85float * misc = (float * )take (L ::misc ); 86long long * rows = (long long * )take (L ::row ); 87float * qmax = misc ;// [8] 88float * mrow = misc + 8 ;// [16] 89float * lsum = misc + 24 ;// [16] 90float * alpha = misc + 40 ;// [16] 91float * vup = misc + 56 ;// [4] 92float * vdown = misc + 60 ;// [4] 93 94const int qi = (int )it .get_group (2 ),kvh = (int )it .get_group (1 ); 95const int n_head = n_kv_heads * G ; 96q += (size_t )qi * n_head * HD + (size_t )kvh * G * HD ; 97attn += (size_t )qi * n_head * HD + (size_t )kvh * G * HD ; 98ids += (size_t )qi * cap ; 99const int n = steps [(size_t )qi * kStepCount + kStepWidth ]; 100const 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) 103float qm = 0.0f ; 104for (int i = t ;i < G * HD ;i += THREADS )qm = sycl::fmax (qm , sycl::fabs (q [i ])); 105qm = sycl::reduce_over_group (sg ,qm , sycl::maximum < float > ()); 106if (lane == 0 )qmax [sgid ]= qm ; 107it .barrier (sycl::access::fence_space::local_space ); 108qm = 0.0f ; 109for (int i = 0 ;i < NSG ;++ i )qm = sycl::fmax (qm ,qmax [i ]); 110int qe = 0 ;// frexp's exponent from the bits (qm < 2^qe); qm > 0 is a normal number here 111if (qm > 0.0f )qe = (int ) ((sycl::bit_cast < uint32_t > (qm ) >>23 )& 0xFFu )- 126 ; 112const float qup = sycl::ldexp (1.0f ,14 - qe ),qdown = sycl::ldexp (scale_log2 ,qe - 14 ); 113for (int i = t ;i < 16 * HD ;i += THREADS ) { 114const int h = i /HD ,d = i %HD ; 115const float x = h < G ?q [(size_t )h * HD + d ]* qup :0.0f ; 116const sycl::half hi = sycl::half (x ); 117qh [h * QS + d ]= hi ; 118ql [h * QS + d ]= sycl::half (x - (float )hi ); 119 } 120for (int i = t ;i < 16 * HD ;i += THREADS )acc [i ]= 0.0f ; 121if (t < 16 ) {mrow [t ]= - INFINITY ;lsum [t ]= 0.0f ; } 122 123for (int c0 = 0 ;c0 < n ;c0 += CH ) { 124const int nh = sycl::min (CH ,n - c0 ); 125// the chunk's rows (the previous chunk's P.V only reads vp/ptile, not rows) 126if (t < CH ) { 127long long r = -1 ; 128if (t < nh ) { 129const int cell = ids [c0 + t ]; 130const long long page = (long long )p .page_table [cell /page_size ]; 131r = (page * n_kv_heads + kvh )* page_size + (cell %page_size ); 132 } 133rows [t ]= r ; 134 } 135it .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 137for (int i = t ;i < CH * (HD /8 );i += THREADS ) { 138const int c = i / (HD /8 ),piece = i % (HD /8 ); 139const long long r = rows [c ]; 140uint32_t w [4 ]= {0u ,0u ,0u ,0u }; 141if (r >=0 ) { 142if constexpr (KV_MODE == 0 ) { 143const sycl::vec < uint32_t ,4 > v = * reinterpret_cast < const sycl::vec < uint32_t ,4 >*> (p .k_pool + r * HD + piece * 8 ); 144for (int j = 0 ;j < 4 ;++ j )w [j ]= v [j ]; 145 }else { 146const sycl::vec < int8_t ,8 > v = * reinterpret_cast < const sycl::vec < int8_t ,8 >*> (p .k_q + r * HD + piece * 8 ); 147for (int j = 0 ;j < 4 ;++ j ) { 148const sycl::half2 h2 ((float )v [2 * j ], (float )v [2 * j + 1 ]); 149w [j ]= sycl::bit_cast < uint32_t > (h2 ); 150 } 151 } 152 } 153uint32_t * dst = reinterpret_cast < uint32_t *> (kp ); 154for (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) 158if constexpr (KV_MODE == 3 ) { 159constexpr int BLKS = HD /QK4_0 ; 160constexpr int BYTES = BLKS * (int )sizeof (q4_0_block ); 161for (int i = t ;i < (CH /2 )* BLKS ;i += THREADS ) { 162const int cp = i /BLKS ,b = i %BLKS ; 163float v0 [QK4_0 ],v1 [QK4_0 ]; 164for (int e = 0 ;e < 2 ;++ e ) { 165const long long r = rows [2 * cp + e ]; 166float * v = e ?v1 :v0 ; 167if (r < 0 ) {for (int j = 0 ;j < QK4_0 ;++ j )v [j ]= 0.0f ;continue ; } 168const q4_0_block * blk = reinterpret_cast < const q4_0_block *> (p .v_q4 + r * BYTES )+ b ; 169const float d = (float ) sycl::bit_cast < sycl::half > (blk -> d ); 170for (int j = 0 ;j < QK4_0 /2 ;++ j ) { 171v [j ]= (float ) ((int ) (blk -> qs [j ]& 0x0F )- 8 )* d ; 172v [j + QK4_0 /2 ]= (float ) ((int ) (blk -> qs [j ] >>4 )- 8 )* d ; 173 } 174 } 175uint32_t * dst = reinterpret_cast < uint32_t *> (vp )+ (size_t )cp * HD + b * QK4_0 ; 176for (int j = 0 ;j < QK4_0 ;j += 8 ) { 177 sycl::vec < uint32_t ,8 > o ; 178for (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 { 183for (int i = t ;i < (CH /2 )* (HD /8 );i += THREADS ) { 184const int cp = i / (HD /8 ),piece = i % (HD /8 ); 185 sycl::half h0 [8 ],h1 [8 ]; 186for (int e = 0 ;e < 2 ;++ e ) { 187const long long r = rows [2 * cp + e ]; 188 sycl::half * h = e ?h1 :h0 ; 189if (r < 0 ) {for (int j = 0 ;j < 8 ;++ j )h [j ]= sycl::half (0.0f );continue ; } 190if constexpr (KV_MODE == 1 ) { 191const sycl::vec < int8_t ,8 > v = * reinterpret_cast < const sycl::vec < int8_t ,8 >*> (p .v_q + r * HD + piece * 8 ); 192for (int j = 0 ;j < 8 ;++ j )h [j ]= sycl::half ((float )v [j ]); 193 }else { 194const sycl::vec < uint16_t ,8 > v = * reinterpret_cast < const sycl::vec < uint16_t ,8 >*> (p .v_pool + r * HD + piece * 8 ); 195for (int j = 0 ;j < 8 ;++ j )h [j ]= sycl::bit_cast < sycl::half > (v [j ]); 196 } 197 } 198 sycl::vec < uint32_t ,8 > o ; 199for (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 } 203for (int i = t ;i < CH * 4 ;i += THREADS ) { 204const int c = i /4 ,g = i %4 ; 205const long long r = rows [c ]; 206float a = 0.0f ,b = 0.0f ; 207if (r >=0 ) { 208if constexpr (KV_MODE == 1 ) { 209a = (float ) sycl::bit_cast < sycl::half > (p .k_scale [r * (HD /KV_Q8_GROUP )+ g ]); 210b = (float ) sycl::bit_cast < sycl::half > (p .v_scale [r * (HD /KV_Q8_GROUP )+ g ]); 211 }else if constexpr (KV_MODE == 3 ) { 212a = (float ) sycl::bit_cast < sycl::half > (p .k_scale [r * (HD /KV_Q8_GROUP )+ g ]); 213b = 1.0f ; 214 }else { 215a = b = 1.0f ; 216 } 217 } 218ks [c * 4 + g ]= a ; 219vs [c * 4 + g ]= b ; 220 } 221it .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) 223for (int u = sgid ;u < UNITS ;u += NSG ) { 224const 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 ); 227for (int kk = 0 ;kk < 4 ;++ kk ) { 228const 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) 240if (t < 4 ) { 241float vmax = 0.0f ; 242for (int c = 0 ;c < CH ;++ c )vmax = sycl::fmax (vmax ,vs [c * 4 + t ]); 243vup [t ]= vmax > 0.0f ?16384.0f /vmax :0.0f ; 244vdown [t ]= vmax * (1.0f /16384.0f ); 245 } 246it .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 { 249constexpr int PER = CH /8 ; 250const int r = t >>3 ,sub = t & 7 ; 251float x [PER ],mx = - INFINITY ; 252for (int j = 0 ;j < PER ;++ j ) { 253const int c = sub * PER + j ,nt = c >>4 ,cc = c & 15 ; 254float v = 0.0f ; 255for (int g = 0 ;g < 4 ;++ g )v += parts [(size_t ) (nt * 4 + g )* 256 + r * 16 + cc ]* ks [c * 4 + g ]; 256x [j ]= c < nh ?v * qdown :- INFINITY ; 257mx = sycl::fmax (mx ,x [j ]); 258 } 259for (int o = 1 ;o < 8 ;o <<=1 )mx = sycl::fmax (mx , sycl::permute_group_by_xor (sg ,mx ,o )); 260const float m_old = mrow [r ]; 261const float m_new = sycl::fmax (m_old ,mx ); 262float sum = 0.0f ; 263for (int j = 0 ;j < PER ;++ j ) { 264const float e = x [j ]== - INFINITY ?0.0f : sycl::exp2 (x [j ]- m_new ); 265sp [r * PS + sub * PER + j ]= e ; 266sum += e ; 267 } 268for (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 270if (sub == 0 ) { 271const float a = m_old == - INFINITY ?0.0f : sycl::exp2 (m_old - m_new ); 272alpha [r ]= a ; 273lsum [r ]= sycl::fma (lsum [r ],a ,sum ); 274mrow [r ]= m_new ; 275 } 276 } 277it .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) 279constexpr int NGP = KV_MODE == 1 ?4 :1 ; 280for (int i = t ;i < NGP * 16 * (CH /2 );i += THREADS ) { 281const int g = i / (16 * (CH /2 )),rc = i % (16 * (CH /2 )),r = rc / (CH /2 ),c = (rc % (CH /2 ))* 2 ; 282const float w0 = vs [c * 4 + g ]* vup [g ],w1 = vs [(c + 1 )* 4 + g ]* vup [g ]; 283const float p0 = sp [r * PS + c ]* w0 ,p1 = sp [r * PS + c + 1 ]* w1 ; 284const 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 } 289it .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 { 293const int g = sgid >>1 ,gp = KV_MODE == 1 ?g :0 ; 294const sycl::half * ah = ptile + (size_t ) (gp * 2 + 0 )* 16 * PS ; 295const sycl::half * al = ptile + (size_t ) (gp * 2 + 1 )* 16 * PS ; 296float * tmp = parts + (size_t )sgid * 512 ; 297for (int dt = 0 ;dt < 2 ;++ dt ) { 298const 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 ); 301for (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 ); 313const float vd = vdown [g ]; 314for (int i = lane ;i < 512 ;i += SG ) { 315const int dt = i >>8 ,rc = i & 255 ,r = rc >>4 ,cc = rc & 15 ; 316float * a = acc + r * HD + sgid * 32 + dt * 16 + cc ; 317* a = sycl::fma (* a ,alpha [r ],tmp [i ]* vd ); 318 } 319 } 320 } 321it .barrier (sycl::access::fence_space::local_space ); 322for (int i = t ;i < G * HD ;i += THREADS ) { 323const int h = i /HD ,d = i %HD ; 324const float l = lsum [h ]; 325attn [(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 , 331const QsaShapes & s ,float * attn ,int64_t n_q , dpct::queue_ptr st ) { 332const float scale_log2 = 1.4426950408889634f / std::sqrt ((float )HD ); 333constexpr size_t bytes = Layout < CH > ::bytes ; 334for (int64_t q0 = 0 ;q0 < n_q ;q0 += 65535 ) { 335const int64_t nb = n_q - q0 < 65535 ?n_q - q0 :65535 ; 336const float * = q + q0 * s .n_head * HD ; 337const int32_t * idq = ids + q0 * cap ; 338const int32_t * stq = steps + q0 * kStepCount ; 339float * aq = attn + q0 * s .n_head * HD ; 340const 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 ); 343cgh .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 )]] { 345prompt_attn_xmx2 < KV_MODE ,CH > (pools ,idq ,stq ,nkv ,ps ,scale_log2 ,aq ,capi , 346slm .get_multi_ptr < sycl::access::decorated::no > ().get ()); 347 }); 348 }); 349 } 350return 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 , 355const QsaShapes & s ,float * attn ,int64_t n_q , dpct::queue_ptr st ) { 356if (pools .k_q != nullptr && pools .v_q4 != nullptr ) { 357if (!pools .k_scale )return false; 358return launch < 3 ,CH > (q ,pools ,ids ,steps ,cap ,s ,attn ,n_q ,st ); 359 } 360if (pools .k_q != nullptr ) { 361if (!pools .v_q || !pools .k_scale || !pools .v_scale )return false; 362return launch < 1 ,CH > (q ,pools ,ids ,steps ,cap ,s ,attn ,n_q ,st ); 363 } 364if (!pools .k_pool || !pools .v_pool )return false; 365return 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 , 370const 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 372static const int mode = [] {const char * v = std::getenv ("STRATA_PROMPT_ATTN_XMX" );return v ? std::atoi (v ) :0 ; }(); 373if (mode == 0 || n_q <=0 )return false; 374if (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 ) 376return false; 377 dpct::queue_ptr st = strata::q_of (stream ); 378const size_t have = st -> get_device ().get_info < sycl::info::device::local_mem_size > (); 379const bool ch64 = mode != 32 && have >=Layout < 64 > ::bytes ; 380static bool told = false; 381if (!told ) { 382told = true; 383 std::fprintf (stderr ,"strata: prompt attention on XMX, %d-cell chunks (%zu of %zu bytes of local memory)\n" , 384ch64 ?64 :32 ,ch64 ?Layout < 64 > ::bytes :Layout < 32 > ::bytes ,have ); 385 } 386if (!ch64 && have < Layout < 32 > ::bytes )return false; 387return 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