okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a8a51b280
main
1#include "b60_layout_state.hpp" 2#include <cstdlib> 3// src/kernels/cuda/qsa_decode_attn.cu - see include/strata/kernels/qsa_decode_attn.hpp. 4#define DPCT_PROFILING_ENABLED 5#include <sycl/sycl.hpp> 6#include "strata/kprof.hpp" 7#include "strata/core/geometry.hpp" 8#include <dpct/dpct.hpp> 9#include "strata/sycl_queue.hpp" 10#include "strata/workload_trace.hpp" 11#include "strata/kernels/qsa_decode_attn.hpp" 12#include "strata/kernels/kv_q8.hpp" 13#include "strata/kernels/kv_q4.hpp" 14 15#include <cfloat> 16#include <cstdio> 17#include <cstdlib> 18// BEGIN PRIVATE QSA TWO STAGE INCLUDES 19#include <climits> 20#include <stdexcept> 21// END PRIVATE QSA TWO STAGE INCLUDES 22#include <cmath> 23 24namespace strata::kernels { 25namespace { 26 27constexpr int HD = 256 ;// head_dim 28constexpr int G = strata::core::kCompiledGeometry .n_head / strata::core::kCompiledGeometry .n_head_kv ;// query heads per KV head (12 for Flash-Next; 1..16, a multiple of 4) 29static_assert(G %4 == 0 && G <=16 ,"qsa_decode_attn: query heads per KV head must be 4, 8, 12 or 16 (float4 probability rows, the 16-lane score reduce)" ); 30static_assert(HD == strata::core::kCompiledGeometry .head_dim && G == strata::core::kCompiledGeometry .n_head / strata::core::kCompiledGeometry .n_head_kv ,"qsa_decode_attn is compiled for core/geometry.hpp" ); 31constexpr int CHUNK = 64 ;// cells per block 32constexpr int THREADS = 256 ; 33constexpr int WARPS = THREADS /32 ; 34 35__dpct_inline__ float warp_sum (float v ) { 36#pragma unroll 37/* 38DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 39masked sub_group function which may not be supported by all compilers or 40runtimes. You may need to adjust the code. 41*/ 42for (int o = 16 ;o > 0 ;o >>=1 )v += 43 dpct::experimental::permute_sub_group_by_xor ( 440xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (),v , 45o ); 46return v ; 47} 48__dpct_inline__ float warp_max (float v ) { 49#pragma unroll 50/* 51DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 52masked sub_group function which may not be supported by all compilers or 53runtimes. You may need to adjust the code. 54*/ 55for (int o = 16 ;o > 0 ;o >>=1 )v = sycl::fmax ( 56v , dpct::experimental::permute_sub_group_by_xor ( 570xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 58v ,o )); 59return v ; 60} 61 62// 8 consecutive values of one cell's key or value row for KV head `kvh`, dimensions [d0, d0+8). 63// Per-format bodies; `load8` below is the KV_MODE dispatcher. value=false is the K side, true the V side. 64__dpct_inline__ void load8_f16 (const QsaAttnPools & p ,bool value ,long long row , 65int d0 ,float * out ) { 66const uint16_t * base = (value ?p .v_pool :p .k_pool )+ row * HD + d0 ; 67const sycl::uint4 raw = * reinterpret_cast < const sycl::uint4 *> (base ); 68const sycl::half2 * h2 = reinterpret_cast < const sycl::half2 *> (& raw ); 69#pragma unroll 70for (int j = 0 ;j < 4 ;++ j ) { 71const sycl::float2 f = 72h2 [j ].template convert < float , sycl::rounding_mode::automatic > (); 73out [2 * j ]= f .x (); 74out [2 * j + 1 ]= f .y (); 75 } 76} 77__dpct_inline__ void load8_q8 (const QsaAttnPools & p ,bool value ,long long row , 78int d0 ,float * out ) { 79const int8_t * codes = (value ?p .v_q :p .k_q )+ row * HD + d0 ; 80const uint16_t sbits = (value ?p .v_scale :p .k_scale )[row * (HD /KV_Q8_GROUP )+ d0 /KV_Q8_GROUP ]; 81const float sc = sycl::vec < sycl::half ,1 > ( 82 sycl::bit_cast < sycl::half ,unsigned short > (sbits )) 83 .convert < float , sycl::rounding_mode::automatic > ()[0 ]; 84const sycl::uint2 raw = * reinterpret_cast < const sycl::uint2 *> (codes ); 85const int8_t * c = reinterpret_cast < const int8_t *> (& raw ); 86#pragma unroll 87for (int j = 0 ;j < 8 ;++ j )out [j ]= (float )c [j ]* sc ; 88} 89__dpct_inline__ void load8_q4 (const QsaAttnPools & p ,bool value ,long long row , 90int d0 ,float * out ) { 91constexpr int bytes_per_head = (HD /QK4_0 )* sizeof (block_q4_0 ); 92const int b = d0 /QK4_0 ; 93const int rem = d0 %QK4_0 ; 94const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ((value ?p .v_q4 :p .k_q4 )+ row * bytes_per_head )+ b ; 95const float d = sycl::vec < sycl::half ,1 > ( 96 sycl::bit_cast < sycl::half ,unsigned short > (blk -> d )) 97 .convert < float , sycl::rounding_mode::automatic > ()[0 ]; 98const int j = (rem == 0 || rem == 16 ) ?0 :8 ; 99const uint8_t * bytes = blk -> qs + j ; 100if (rem < 16 ) { 101#pragma unroll 102for (int k = 0 ;k < 8 ;++ k )out [k ]= (float ) ((int )(bytes [k ]& 0x0F )- 8 )* d ; 103 }else { 104#pragma unroll 105for (int k = 0 ;k < 8 ;++ k )out [k ]= (float ) ((int )(bytes [k ] >>4 )- 8 )* d ; 106 } 107} 108template < int KV_MODE > 109__dpct_inline__ void load8 (const QsaAttnPools & p ,bool value ,long long row , 110int d0 ,float * out ) { 111if constexpr (KV_MODE == 0 )load8_f16 (p ,value ,row ,d0 ,out ); 112else if constexpr (KV_MODE == 1 )load8_q8 (p ,value ,row ,d0 ,out ); 113else if constexpr (KV_MODE == 3 ) { 114// hybrid K8V4: both sides are defined - K unrotated INT8, V rotated Q4_0 - so a value=true call 115// reads the Q4_0 pool instead of dereferencing the null v_q (no call site does today; PR review) 116if (value )load8_q4 (p ,value ,row ,d0 ,out ); 117else load8_q8 (p ,value ,row ,d0 ,out ); 118 }else load8_q4 (p ,value ,row ,d0 ,out ); 119} 120 121template < int KV_MODE ,bool LANE_CELL = false,int RS = 0 > 122/* 123DPCT1110: The total declared local variable size in device function 124attn_chunk_kernel exceeds 128 bytes and may cause high register pressure. 125Consult with your hardware vendor to find the total register size available and 126adjust the code, or use smaller sub-group size to avoid high register pressure. 127*/ 128__dpct_inline__ void 129attn_chunk_kernel (const float * __restrict__q ,QsaAttnPools p , 130const int32_t * __restrict__ids , 131const int32_t * __restrict__step ,int n_kv_heads , 132int page_size ,float scale ,float * __restrict__part_acc , 133float * __restrict__part_m ,float * __restrict__part_l , 134int n_chunks ,int cap = 0 ,long long scratch_stride = 0 , 135QsaAttnPoolsRows prow = {}) { 136// batched form: query blockIdx.z, with its own q row, selection, step and scratch 137auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 138if ( prow . n > 0 ) p = prow . p [ item_ct1 . get_group ( 0 )]; // a batch window's query attends over its own slot's pools 139q += (size_t) item_ct1 . get_group ( 0 ) * (size_t)( n_kv_heads * G ) * HD ; 140ids += (size_t) item_ct1 . get_group ( 0 ) * (size_t) cap ; 141step += (size_t) item_ct1 . get_group ( 0 ) * kStepCount ; 142part_acc += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 143part_m += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 144part_l += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 145auto & sq = 146* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ HD ] > ( 147sycl::ext::oneapi::this_work_item:: get_work_group < 1483 > ()); // 12 KB: this KV head's query heads 149auto & sp = 150* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G * CHUNK ] > ( 151sycl::ext::oneapi::this_work_item:: get_work_group < 1523 > ()); // scores, then probabilities 153auto & srow = 154* sycl::ext::oneapi:: group_local_memory_for_overwrite < long long[ CHUNK ] > ( 155sycl::ext::oneapi::this_work_item:: get_work_group < 1563 > ()); // pool row of each cell (page, kv head, slot) 157/* 158DPCT1098: The '*' expression is used instead of the __ldg call. These 159two expressions do not provide the exact same functionality. Check the 160generated code for potential precision and/or performance issues. 161*/ 162// the scores and probabilities: [head][cell], or - RS - [cell][head] so the value phase reads a cell's 12 probabilities as 3 vectors 163const auto spi = [](int h , int c ) { return RS != 0 ? c * G + h : h * CHUNK + c ; }; 164const int n_ids = * ( step + kStepWidth ); 165const int chunk = item_ct1 . get_group ( 2 ), kvh = item_ct1 . get_group ( 1 ); 166const int t = item_ct1 . get_local_id ( 2 ), lane = t & 31 , warp = t >> 5 ; 167const int c0 = chunk * CHUNK ; 168const int n_here = sycl:: min ( CHUNK , n_ids - c0 ); 169const int slot = kvh * n_chunks + chunk ; 170if ( n_here <= 0 ) { 171if ( t < G ) { part_m [ slot * G + t ] = - FLT_MAX ; part_l [ slot * G + t ] = 0.0f ; } 172return ; 173} 174#pragma unroll 175for (int i = t ; i < G * HD ; i += THREADS ) 176sq [ i / HD ][ i % HD ] = q [(size_t)( kvh * G ) * HD + i ]; 177if ( t < CHUNK ) { 178long long r = -1 ; 179if ( t < n_here ) { 180const int cell = ids [ c0 + t ]; 181const long long page = (long long) p . page_table [ cell / page_size ]; 182// a block the KV streaming could not make resident keeps page -1 (ctl[3]); its cells are masked 183// (score -FLT_MAX, weight 0) instead of being read from before the pool. 184if ( page >= 0 ) r = ( page * n_kv_heads + kvh ) * page_size + ( cell % page_size ); 185} 186srow [ t ] = r ; 187} 188/* 189DPCT1065: Consider replacing sycl::nd_item::barrier() with 190sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 191performance if there is no access to global memory. 192*/ 193item_ct1 . barrier (); 194if constexpr ( LANE_CELL ) { 195// S25 (STRATA_ATTN_LANECELL=1): thread t scores cell t % 64 for heads 3 (t / 64) .. +2 by itself - the same 196// 8-dimension partial per former lane (the identical expression), then the former warp_sum's butterfly as 197// lane 0 ran it (a[l] += a[l + o] for o = 16 .. 1), so every score is bit-identical without 12 shuffle 198// reductions per cell 199constexpr int HPT = G * CHUNK / THREADS ; // heads per thread (3) 200const int c = t % CHUNK , h0 = ( t / CHUNK ) * HPT ; 201if ( c >= n_here || srow [ c ] < 0 ) { 202#pragma unroll 203for (int j = 0 ; j < HPT ; ++ j ) sp [ spi ( h0 + j , c )] = - FLT_MAX ; 204} else { 205float a [ HPT ][ 32 ]; 206#pragma unroll 207for (int l = 0 ; l < 32 ; ++ l ) { 208float k8 [ 8 ]; 209load8 < KV_MODE > ( p , false, srow [ c ], l * 8 , k8 ); 210#pragma unroll 211for (int j = 0 ; j < HPT ; ++ j ) { 212const sycl::float4 qa = 213* reinterpret_cast < const sycl::float4 *> ( 214& sq [ h0 + j ][ l * 8 ]); 215const sycl::float4 qb = 216* reinterpret_cast < const sycl::float4 *> ( 217& sq [ h0 + j ][ l * 8 + 4 ]); 218float s = k8 [ 0 ] * qa . x () + k8 [ 1 ] * qa . y () + k8 [ 2 ] * qa . z () + 219k8 [ 3 ] * qa . w () + k8 [ 4 ] * qb . x () + k8 [ 5 ] * qb . y () + 220k8 [ 6 ] * qb . z () + k8 [ 7 ] * qb . w (); 221a [ j ][ l ] = s ; 222} 223} 224#pragma unroll 225for (int j = 0 ; j < HPT ; ++ j ) { 226#pragma unroll 227for (int o = 16 ; o > 0 ; o >>= 1 ) 228#pragma unroll 229for (int l = 0 ; l < o ; ++ l ) a [ j ][ l ] = a [ j ][ l ] + a [ j ][ l + o ]; 230sp [ spi ( h0 + j , c )] = a [ j ][ 0 ] * scale ; 231} 232} 233} else 234// scores: each warp takes cells warp, warp+8, ...; each lane holds 8 of the 256 dimensions. 235for (int c = warp ; c < CHUNK ; c += WARPS ) { 236if ( c >= n_here || srow [ c ] < 0 ) { 237if ( lane < G ) sp [ spi ( lane , c )] = - FLT_MAX ; 238continue ; 239} 240float k8 [ 8 ]; 241load8 < KV_MODE > ( p , false, srow [ c ], lane * 8 , k8 ); 242if constexpr ( RS == 1 ) { 243// 151-b70: the 12 heads' 32-lane butterfly sums as ONE reduce-scatter - 16 shuffles instead of 60. Each stage pairs the 244// same lanes in the same order as warp_sum (xor 16, 8, 4, 2, 1) and adds own + partner, so every head's sum is the 245// same tree and the same bits; the heads are padded to 16, halved at each stage (the lane keeps the half its bit 246// selects, sends the other), and after the xor-2 stage lane 2h + {0, 1} holds head h's partial pair; xor 1 finishes it. 247float part [ 16 ]; 248#pragma unroll 249for (int h = 0 ; h < G ; ++ h ) { 250const sycl::float4 qa = 251* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 8 ]); 252const sycl::float4 qb = 253* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 8 + 4 ]); 254part [ h ] = k8 [ 0 ] * qa . x () + k8 [ 1 ] * qa . y () + k8 [ 2 ] * qa . z () + 255k8 [ 3 ] * qa . w () + k8 [ 4 ] * qb . x () + k8 [ 5 ] * qb . y () + 256k8 [ 6 ] * qb . z () + k8 [ 7 ] * qb . w (); 257} 258#pragma unroll 259for (int h = G ; h < 16 ; ++ h ) part [ h ] = 0.0f ; 260const auto xr = [](float v , int o ) { 261return dpct::experimental:: permute_sub_group_by_xor ( 2620xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), v , o ); 263}; 264float c8 [ 8 ]; 265const bool b16 = ( lane & 16 ) != 0 , b8 = ( lane & 8 ) != 0 , b4 = ( lane & 4 ) != 0 , b2 = ( lane & 2 ) != 0 ; 266#pragma unroll 267for (int i = 0 ; i < 8 ; ++ i ) { 268const float send = b16 ? part [ i ] : part [ 8 + i ]; 269const float keep = b16 ? part [ 8 + i ] : part [ i ]; 270c8 [ i ] = keep + xr ( send , 16 ); 271} 272float c4 [ 4 ]; 273#pragma unroll 274for (int i = 0 ; i < 4 ; ++ i ) { 275const float send = b8 ? c8 [ i ] : c8 [ 4 + i ]; 276const float keep = b8 ? c8 [ 4 + i ] : c8 [ i ]; 277c4 [ i ] = keep + xr ( send , 8 ); 278} 279float c2 [ 2 ]; 280#pragma unroll 281for (int i = 0 ; i < 2 ; ++ i ) { 282const float send = b4 ? c4 [ i ] : c4 [ 2 + i ]; 283const float keep = b4 ? c4 [ 2 + i ] : c4 [ i ]; 284c2 [ i ] = keep + xr ( send , 4 ); 285} 286const float send1 = b2 ? c2 [ 0 ] : c2 [ 1 ]; 287const float keep1 = b2 ? c2 [ 1 ] : c2 [ 0 ]; 288float s = keep1 + xr ( send1 , 2 ); 289s = s + xr ( s , 1 ); 290const int head = ( lane >> 1 ) & 15 ; 291if (( lane & 1 ) == 0 && head < G ) sp [ spi ( head , c )] = s * scale ; 292} else { 293#pragma unroll 294for (int h = 0 ; h < G ; ++ h ) { 295const sycl::float4 qa = 296* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 8 ]); 297const sycl::float4 qb = 298* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 8 + 4 ]); 299float s = k8 [ 0 ] * qa . x () + k8 [ 1 ] * qa . y () + k8 [ 2 ] * qa . z () + 300k8 [ 3 ] * qa . w () + k8 [ 4 ] * qb . x () + k8 [ 5 ] * qb . y () + 301k8 [ 6 ] * qb . z () + k8 [ 7 ] * qb . w (); 302s = warp_sum ( s ); 303if ( lane == 0 ) sp [ spi ( h , c )] = s * scale ; 304} 305} 306} 307/* 308DPCT1065: Consider replacing sycl::nd_item::barrier() with 309sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 310performance if there is no access to global memory. 311*/ 312item_ct1 . barrier (); 313// per-head chunk max and exp-sum: warp w handles heads w and w+8. 314for (int h = warp ; h < G ; h += WARPS ) { 315const float a = sp [ spi ( h , lane )], b = sp [ spi ( h , lane + 32 )]; 316const float m = warp_max (sycl:: fmax ( a , b )); 317const float ea = ( lane < n_here && srow [ lane ] >= 0 ) 318? sycl::native:: exp ( a - m ) 319: 0.0f ; 320const float eb = ( lane + 32 < n_here && srow [ lane + 32 ] >= 0 ) 321? sycl::native:: exp ( b - m ) 322: 0.0f ; 323sp [ spi ( h , lane )] = ea ; 324sp [ spi ( h , lane + 32 )] = eb ; 325const float l = warp_sum ( ea + eb ); 326if ( lane == 0 ) { part_m [ slot * G + h ] = m ; part_l [ slot * G + h ] = l ; } 327} 328/* 329DPCT1065: Consider replacing sycl::nd_item::barrier() with 330sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 331performance if there is no access to global memory. 332*/ 333item_ct1 . barrier (); 334// values: thread t owns dimension t for all 12 heads. 335float acc [ G ]; 336#pragma unroll 337for (int h = 0 ; h < G ; ++ h ) acc [ h ] = 0.0f ; 338for (int c = 0 ; c < n_here ; ++ c ) { 339if ( srow [ c ] < 0 ) continue ; // masked above, weight 0 340float v ; 341if constexpr ( KV_MODE == 0 ) { 342v = sycl:: vec < sycl::half, 1 > ( 343sycl:: bit_cast < sycl::half, unsigned short > ( 344p . v_pool [ srow [ c ] * HD + t ])) 345. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 346} else if constexpr ( KV_MODE == 1 ) { 347const float sc = 348sycl:: vec < sycl::half, 1 > ( 349sycl:: bit_cast < sycl::half, unsigned short > ( 350p . v_scale [ srow [ c ] * ( HD / KV_Q8_GROUP ) + 351t / KV_Q8_GROUP ])) 352. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 353v = (float) p . v_q [ srow [ c ] * HD + t ] * sc ; 354} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 355constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 356const int b = t / QK4_0 ; 357const int rem = t % QK4_0 ; 358const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + srow [ c ] * bytes_per_head ) + b ; 359const float d = 360sycl:: vec < sycl::half, 1 > ( 361sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 362. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 363const int j = rem < 16 ? rem : ( rem - 16 ); 364const uint8_t byte = blk -> qs [ j ]; 365const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 366v = (float) nibble * d ; 367} 368if constexpr ( RS != 0 ) { 369// the cell's 12 probabilities as three vector loads (the layout is [cell][head]); the same fma per head in the same order 370#pragma unroll 371for (int q4 = 0 ; q4 < G / 4 ; ++ q4 ) { 372const sycl::float4 pv = * reinterpret_cast < const sycl::float4 *> ( & sp [ c * G + 4 * q4 ]); 373acc [ 4 * q4 + 0 ] = sycl:: fma ( pv . x (), v , acc [ 4 * q4 + 0 ]); 374acc [ 4 * q4 + 1 ] = sycl:: fma ( pv . y (), v , acc [ 4 * q4 + 1 ]); 375acc [ 4 * q4 + 2 ] = sycl:: fma ( pv . z (), v , acc [ 4 * q4 + 2 ]); 376acc [ 4 * q4 + 3 ] = sycl:: fma ( pv . w (), v , acc [ 4 * q4 + 3 ]); 377} 378} else { 379#pragma unroll 380for (int h = 0 ; h < G ; ++ h ) acc [ h ] = sycl:: fma ( sp [ spi ( h , c )], v , acc [ h ]); 381} 382} 383#pragma unroll 384for (int h = 0 ; h < G ; ++ h ) part_acc [((size_t) slot * G + h ) * HD + t ] = acc [ h ]; 385} 386 387// PR #540 (sskver): the same attention with fewer shuffles and less shared-memory traffic, bit for bit the kernel 388// above (same operands, same order); +7% prompt speed on a V100, where this kernel reads every prompt chunk. It 389// uses more registers (80 vs 38 on sm_70), so it runs on cards below sm_75 only, and exists only in the experimental 390// build (-DSTRATA_EXPERIMENTAL_SM60=ON): the ready-made engine keeps exactly the kernel above. 391#if defined( STRATA_EXPERIMENTAL_SM60 ) 392// The 12 heads' lane sums (`part[12..15]` zero), reduce-scattered with warp_sum's pairing order (xor 16, 8, 4, 2, 1): 393// every output adds the same two operands at every level, so each sum is bit for bit warp_sum's (float add commutes), 394// for 16 shuffles instead of 12 x 5. Lane l ends holding head head_of_lane(l); lanes l and l^1 agree. 395__device__ __forceinline__ float reduce12 ( const float ( & part )[ 16 ], int lane ) { 396float r8 [ 8 ]; 397#pragma unroll 398for (int i = 0 ; i < 8 ; ++ i ) { 399const bool hi = ( lane & 16 ) != 0 ; 400r8 [ i ] = ( hi ? part [ i + 8 ] : part [ i ]) + __shfl_xor_sync ( 0xffffffffu , hi ? part [ i ] : part [ i + 8 ], 16 ); 401} 402float r4 [ 4 ]; 403#pragma unroll 404for (int i = 0 ; i < 4 ; ++ i ) { 405const bool hi = ( lane & 8 ) != 0 ; 406r4 [ i ] = ( hi ? r8 [ i + 4 ] : r8 [ i ]) + __shfl_xor_sync ( 0xffffffffu , hi ? r8 [ i ] : r8 [ i + 4 ], 8 ); 407} 408float r2 [ 2 ]; 409#pragma unroll 410for (int i = 0 ; i < 2 ; ++ i ) { 411const bool hi = ( lane & 4 ) != 0 ; 412r2 [ i ] = ( hi ? r4 [ i + 2 ] : r4 [ i ]) + __shfl_xor_sync ( 0xffffffffu , hi ? r4 [ i ] : r4 [ i + 2 ], 4 ); 413} 414const bool hi2 = ( lane & 2 ) != 0 ; 415const float r1 = ( hi2 ? r2 [ 1 ] : r2 [ 0 ]) + __shfl_xor_sync ( 0xffffffffu , hi2 ? r2 [ 0 ] : r2 [ 1 ], 2 ); 416return r1 + __shfl_xor_sync ( 0xffffffffu , r1 , 1 ); 417} 418__device__ __forceinline__ int head_of_lane (int lane ) { 419return (( lane >> 4 ) & 1 ) * 8 + (( lane >> 3 ) & 1 ) * 4 + (( lane >> 2 ) & 1 ) * 2 + (( lane >> 1 ) & 1 ); 420} 421 422// One V element of pool row `row`, dimension `d` (the value loop's own thread index), per KV format. 423template < int KV_MODE > 424__device__ __forceinline__ float load_v1 ( const QsaAttnPools & p , long long row , int d ) { 425if constexpr ( KV_MODE == 0 ) { 426return __half2float ( __ushort_as_half ( p . v_pool [ row * HD + d ])); 427} else if constexpr ( KV_MODE == 1 ) { 428const float sc = __half2float ( __ushort_as_half ( p . v_scale [ row * ( HD / KV_Q8_GROUP ) + d / KV_Q8_GROUP ])); 429return (float) p . v_q [ row * HD + d ] * sc ; 430} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 431constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 432const int b = d / QK4_0 ; 433const int rem = d % QK4_0 ; 434const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + b ; 435const float dd = __half2float ( __ushort_as_half ( blk -> d )); 436const int j = rem < 16 ? rem : ( rem - 16 ); 437const uint8_t byte = blk -> qs [ j ]; 438const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 439return (float) nibble * dd ; 440} 441} 442 443template < int KV_MODE > 444__global__ void __launch_bounds__ ( THREADS ) attn_chunk_kernel_pre75 ( const float * __restrict__ q , QsaAttnPools p , 445const int32_t * __restrict__ ids , 446const int32_t * __restrict__ step , int n_kv_heads , 447int page_size , float scale , float * __restrict__ part_acc , 448float * __restrict__ part_m , float * __restrict__ part_l , 449int n_chunks , int cap = 0 , long long scratch_stride = 0 ) { 450// batched form: query blockIdx.z, with its own q row, selection, step and scratch 451q += (size_t) blockIdx . z * (size_t) ( n_kv_heads * G ) * HD ; 452ids += (size_t) blockIdx . z * (size_t) cap ; 453step += (size_t) blockIdx . z * kStepCount ; 454part_acc += (size_t) blockIdx . z * (size_t) scratch_stride ; 455part_m += (size_t) blockIdx . z * (size_t) scratch_stride ; 456part_l += (size_t) blockIdx . z * (size_t) scratch_stride ; 457__shared__ __align__ ( 16 ) float sq [ G ][ HD ]; // 12 KB: this KV head's query heads (dead once the scores are done) 458__shared__ float sp [ G ][ CHUNK ]; // scores 459__shared__ long long srow [ CHUNK ]; // pool row of each cell (page, kv head, slot) 460// probabilities, cell-major: the value loop reads one cell's 12 as three float4. They are first written after the 461// __syncthreads that ends the score phase, the last reader of sq, so they share its storage (64 x 12 of its 256 x 12 462// floats) and the block's shared memory stays what it was before the cell-major layout. 463float ( * const spt )[ G ] = reinterpret_cast < float ( * )[ G ] > ( & sq [ 0 ][ 0 ]); 464static_assert( CHUNK * G <= G * HD , "spt must fit in sq" ); 465const int n_ids = __ldg ( step + kStepWidth ); 466const int chunk = blockIdx . x , kvh = blockIdx . y ; 467const int t = threadIdx . x , lane = t & 31 , warp = t >> 5 ; 468const int c0 = chunk * CHUNK ; 469const int n_here = min ( CHUNK , n_ids - c0 ); 470const int slot = kvh * n_chunks + chunk ; 471if ( n_here <= 0 ) { 472if ( t < G ) { part_m [ slot * G + t ] = - FLT_MAX ; part_l [ slot * G + t ] = 0.0f ; } 473return ; 474} 475for (int i = t ; i < G * HD ; i += THREADS ) sq [ i / HD ][ i % HD ] = q [(size_t) ( kvh * G ) * HD + i ]; 476if ( t < CHUNK ) { 477long long r = -1 ; 478if ( t < n_here ) { 479const int cell = ids [ c0 + t ]; 480const long long page = (long long) p . page_table [ cell / page_size ]; 481// a block the KV streaming could not make resident keeps page -1 (ctl[3]); its cells are masked 482// (score -FLT_MAX, weight 0) instead of being read from before the pool. 483if ( page >= 0 ) r = ( page * n_kv_heads + kvh ) * page_size + ( cell % page_size ); 484} 485srow [ t ] = r ; 486} 487__syncthreads (); 488// scores: each warp takes cells warp, warp+8, ...; each lane holds 8 of the 256 dimensions. NC cells per step 489// (warp + 8 * (i + j)): a lane's q slice comes out of shared memory once for all of them, which cuts the traffic 490// that bounds this phase (24 x 16 B per lane per cell, against the FMAs) by NC; every dot product is the same 491// expression as for a single cell, so the scores are unchanged. 492constexpr int NC = 2 ; // 4 measured slower on a V100 (the registers cost occupancy) 493const int hd = head_of_lane ( lane ); 494for (int i = 0 ; i < CHUNK / WARPS ; i += NC ) { 495int cc [ NC ]; 496bool all_ok = true; 497#pragma unroll 498for (int j = 0 ; j < NC ; ++ j ) { 499cc [ j ] = warp + WARPS * ( i + j ); 500all_ok = all_ok && cc [ j ] < n_here && srow [ cc [ j ]] >= 0 ; 501} 502if ( all_ok ) { 503float kk [ NC ][ 8 ]; 504#pragma unroll 505for (int j = 0 ; j < NC ; ++ j ) load8 < KV_MODE > ( p , false, srow [ cc [ j ]], lane * 8 , kk [ j ]); 506float pp [ NC ][ 16 ]; 507#pragma unroll 508for (int h = 0 ; h < G ; ++ h ) { 509const float4 qa = * reinterpret_cast < const float4 *> ( & sq [ h ][ lane * 8 ]); 510const float4 qb = * reinterpret_cast < const float4 *> ( & sq [ h ][ lane * 8 + 4 ]); 511#pragma unroll 512for (int j = 0 ; j < NC ; ++ j ) 513pp [ j ][ h ] = kk [ j ][ 0 ] * qa . x + kk [ j ][ 1 ] * qa . y + kk [ j ][ 2 ] * qa . z + kk [ j ][ 3 ] * qa . w + 514kk [ j ][ 4 ] * qb . x + kk [ j ][ 5 ] * qb . y + kk [ j ][ 6 ] * qb . z + kk [ j ][ 7 ] * qb . w ; 515} 516#pragma unroll 517for (int j = 0 ; j < NC ; ++ j ) { 518#pragma unroll 519for (int h = G ; h < 16 ; ++ h ) pp [ j ][ h ] = 0.0f ; 520const float sj = reduce12 ( pp [ j ], lane ); 521if (( lane & 1 ) == 0 && hd < G ) sp [ hd ][ cc [ j ]] = sj * scale ; 522} 523} else { 524for (int k = 0 ; k < NC ; ++ k ) { // a masked or out-of-range cell in the group: one cell at a time 525const int c = cc [ k ]; 526if ( c >= n_here || srow [ c ] < 0 ) { 527if ( lane < G ) sp [ lane ][ c ] = - FLT_MAX ; 528continue ; 529} 530float k8 [ 8 ]; 531load8 < KV_MODE > ( p , false, srow [ c ], lane * 8 , k8 ); 532float part [ 16 ]; 533#pragma unroll 534for (int h = 0 ; h < G ; ++ h ) { 535const float4 qa = * reinterpret_cast < const float4 *> ( & sq [ h ][ lane * 8 ]); 536const float4 qb = * reinterpret_cast < const float4 *> ( & sq [ h ][ lane * 8 + 4 ]); 537part [ h ] = k8 [ 0 ] * qa . x + k8 [ 1 ] * qa . y + k8 [ 2 ] * qa . z + k8 [ 3 ] * qa . w + 538k8 [ 4 ] * qb . x + k8 [ 5 ] * qb . y + k8 [ 6 ] * qb . z + k8 [ 7 ] * qb . w ; 539} 540#pragma unroll 541for (int h = G ; h < 16 ; ++ h ) part [ h ] = 0.0f ; 542const float s = reduce12 ( part , lane ); 543if (( lane & 1 ) == 0 && hd < G ) sp [ hd ][ c ] = s * scale ; 544} 545} 546} 547__syncthreads (); 548// per-head chunk max and exp-sum: warp w handles heads w and w+8. 549for (int h = warp ; h < G ; h += WARPS ) { 550const float a = sp [ h ][ lane ], b = sp [ h ][ lane + 32 ]; 551const float m = warp_max ( fmaxf ( a , b )); 552const float ea = ( lane < n_here && srow [ lane ] >= 0 ) ? __expf ( a - m ) : 0.0f ; 553const float eb = ( lane + 32 < n_here && srow [ lane + 32 ] >= 0 ) ? __expf ( b - m ) : 0.0f ; 554spt [ lane ][ h ] = ea ; 555spt [ lane + 32 ][ h ] = eb ; 556const float l = warp_sum ( ea + eb ); 557if ( lane == 0 ) { part_m [ slot * G + h ] = m ; part_l [ slot * G + h ] = l ; } 558} 559__syncthreads (); 560// values: thread t owns dimension t for all 12 heads. 561float acc [ G ]; 562#pragma unroll 563for (int h = 0 ; h < G ; ++ h ) acc [ h ] = 0.0f ; 564// One cell's 12 weights (three broadcast float4 loads) into the 12 accumulators; the cells are folded in 565// ascending order, so the sums are the plain loop's, bit for bit. 566const auto fold = [ & ](int c , float v ) { 567const float4 * w4 = reinterpret_cast < const float4 *> ( spt [ c ]); 568const float4 w0 = w4 [ 0 ], w1 = w4 [ 1 ], w2 = w4 [ 2 ]; 569acc [ 0 ] = fmaf ( w0 . x , v , acc [ 0 ]); acc [ 1 ] = fmaf ( w0 . y , v , acc [ 1 ]); 570acc [ 2 ] = fmaf ( w0 . z , v , acc [ 2 ]); acc [ 3 ] = fmaf ( w0 . w , v , acc [ 3 ]); 571acc [ 4 ] = fmaf ( w1 . x , v , acc [ 4 ]); acc [ 5 ] = fmaf ( w1 . y , v , acc [ 5 ]); 572acc [ 6 ] = fmaf ( w1 . z , v , acc [ 6 ]); acc [ 7 ] = fmaf ( w1 . w , v , acc [ 7 ]); 573acc [ 8 ] = fmaf ( w2 . x , v , acc [ 8 ]); acc [ 9 ] = fmaf ( w2 . y , v , acc [ 9 ]); 574acc [ 10 ] = fmaf ( w2 . z , v , acc [ 10 ]); acc [ 11 ] = fmaf ( w2 . w , v , acc [ 11 ]); 575}; 576int c = 0 ; 577// four cells at a time with their V loads issued together (the loop was one dependent load chain per cell); 578// a group holding a masked cell (page -1, rare) leaves this loop and the scalar one below finishes the chunk. 579for (; c + 4 <= n_here ; c += 4 ) { 580const long long r0 = srow [ c ], r1 = srow [ c + 1 ], r2 = srow [ c + 2 ], r3 = srow [ c + 3 ]; 581if (( r0 | r1 | r2 | r3 ) < 0 ) break ; 582const float v0 = load_v1 < KV_MODE > ( p , r0 , t ), v1 = load_v1 < KV_MODE > ( p , r1 , t ); 583const float v2 = load_v1 < KV_MODE > ( p , r2 , t ), v3 = load_v1 < KV_MODE > ( p , r3 , t ); 584fold ( c , v0 ); 585fold ( c + 1 , v1 ); 586fold ( c + 2 , v2 ); 587fold ( c + 3 , v3 ); 588} 589for (; c < n_here ; ++ c ) { 590if ( srow [ c ] < 0 ) continue ; // masked above, weight 0 591fold ( c , load_v1 < KV_MODE > ( p , srow [ c ], t )); 592} 593#pragma unroll 594for (int h = 0 ; h < G ; ++ h ) part_acc [((size_t) slot * G + h ) * HD + t ] = acc [ h ]; 595} 596#endif // STRATA_EXPERIMENTAL_SM60 597 598__dpct_inline__ void attn_merge_kernel ( const float * __restrict__ part_acc , 599const float * __restrict__ part_m , 600const float * __restrict__ part_l , 601int n_chunks , float * __restrict__ attn , 602long long scratch_stride = 0 ) { 603auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 604part_acc += (size_t) item_ct1 . get_group ( 1 ) * (size_t) scratch_stride ; 605part_m += (size_t) item_ct1 . get_group ( 1 ) * (size_t) scratch_stride ; 606part_l += (size_t) item_ct1 . get_group ( 1 ) * (size_t) scratch_stride ; 607attn += (size_t) item_ct1 . get_group ( 1 ) * 608(size_t) item_ct1 . get_group_range ( 2 ) * HD ; 609const int h = item_ct1 . get_group ( 2 ); // global query head 610const int kvh = h / G , hl = h % G ; 611const int d = item_ct1 . get_local_id ( 2 ); 612float M = - FLT_MAX ; 613#pragma unroll 614for (int c = 0 ; c < n_chunks ; ++ c ) 615M = sycl:: fmax ( M , part_m [( kvh * n_chunks + c ) * G + hl ]); 616float L = 0.0f , acc = 0.0f ; 617for (int c = 0 ; c < n_chunks ; ++ c ) { 618const int slot = kvh * n_chunks + c ; 619const float m = part_m [ slot * G + hl ]; 620if ( m == - FLT_MAX ) continue ; 621const float w = sycl::native:: exp ( m - M ); 622L = sycl:: fma ((float)( part_l [ slot * G + hl ]), (float) w , L ); 623acc = sycl:: fma ((float)( part_acc [((size_t) slot * G + hl ) * HD + d ]), 624(float) w , acc ); 625} 626attn [(size_t) h * HD + d ] = L > 0.0f ? acc / L : 0.0f ; 627} 628 629// Chunks beyond the device-side width contain only -FLT_MAX maxima. Keep the 630// original fold order while issuing eight independent partial reads together. 631__dpct_inline__ void attn_merge_active_kernel ( 632const float * __restrict__ part_acc , const float * __restrict__ part_m , 633const float * __restrict__ part_l , int n_chunks , float * __restrict__ attn , 634long long scratch_stride , const int32_t * __restrict__ steps ) { 635auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 636const int z = item_ct1 . get_group ( 1 ); 637part_acc += (size_t) z * (size_t) scratch_stride ; 638part_m += (size_t) z * (size_t) scratch_stride ; 639part_l += (size_t) z * (size_t) scratch_stride ; 640attn += (size_t) z * (size_t) item_ct1 . get_group_range ( 2 ) * HD ; 641const int width = steps [(size_t) z * kStepCount + kStepWidth ]; 642const int reach = width <= 0 ? 0 : 1 + ( width - 1 ) / CHUNK ; 643const int nc = sycl:: min ( reach , n_chunks ); 644const int h = item_ct1 . get_group ( 2 ), kvh = h / G , hl = h % G ; 645const int d = item_ct1 . get_local_id ( 2 ); 646const float * pm_h = part_m + (size_t) kvh * n_chunks * G + hl ; 647const float * pl_h = part_l + (size_t) kvh * n_chunks * G + hl ; 648const float * pa_h = part_acc + ((size_t) kvh * n_chunks * G + hl ) * HD + d ; 649constexpr int MU = 8 ; 650float M = - FLT_MAX ; 651int c = 0 ; 652for (; c + MU <= nc ; c += MU ) { 653float pm [ MU ]; 654#pragma unroll 655for (int j = 0 ; j < MU ; ++ j ) pm [ j ] = pm_h [(size_t) ( c + j ) * G ]; 656#pragma unroll 657for (int j = 0 ; j < MU ; ++ j ) M = sycl:: fmax ( M , pm [ j ]); 658} 659for (; c < nc ; ++ c ) M = sycl:: fmax ( M , pm_h [(size_t) c * G ]); 660float L = 0.0f , acc = 0.0f ; 661c = 0 ; 662for (; c + MU <= nc ; c += MU ) { 663float pm [ MU ], pl [ MU ], pa [ MU ]; 664#pragma unroll 665for (int j = 0 ; j < MU ; ++ j ) { 666pm [ j ] = pm_h [(size_t) ( c + j ) * G ]; 667pl [ j ] = pl_h [(size_t) ( c + j ) * G ]; 668pa [ j ] = pa_h [(size_t) ( c + j ) * G * HD ]; 669} 670#pragma unroll 671for (int j = 0 ; j < MU ; ++ j ) { 672if ( pm [ j ] == - FLT_MAX ) continue ; 673const float w = sycl::native:: exp ( pm [ j ] - M ); 674L = sycl:: fma ( pl [ j ], w , L ); 675acc = sycl:: fma ( pa [ j ], w , acc ); 676} 677} 678for (; c < nc ; ++ c ) { 679const float m = pm_h [(size_t) c * G ]; 680if ( m == - FLT_MAX ) continue ; 681const float w = sycl::native:: exp ( m - M ); 682L = sycl:: fma ( pl_h [(size_t) c * G ], w , L ); 683acc = sycl:: fma ( pa_h [(size_t) c * G * HD ], w , acc ); 684} 685attn [(size_t) h * HD + d ] = L > 0.0f ? acc / L : 0.0f ; 686} 687 688bool merge_active_on ( const sycl::device & d ) { 689static const bool on = strata:: bm_default_on ( "STRATA_ATTN_MERGE_ACTIVE" , d ); 690return on ; 691} 692 693// BEGIN PRIVATE QSA TWO STAGE HELPER 694 695struct QsaMergeGroupPartial { float m , l , acc ; }; 696__dpct_inline__ QsaMergeGroupPartial qsa_merge_group_partial ( 697const float * __restrict__ part_acc , const float * __restrict__ part_m , 698const float * __restrict__ part_l , int n_chunks , int kvh , int hl , int d , 699int first , int last ) { 700float M = - FLT_MAX ; 701#pragma unroll 702for (int c = first ; c < last ; ++ c ) 703M = sycl:: fmax ( M , part_m [( kvh * n_chunks + c ) * G + hl ]); 704float L = 0.0f , acc = 0.0f ; 705for (int c = first ; c < last ; ++ c ) { 706const int slot = kvh * n_chunks + c ; 707const float m = part_m [ slot * G + hl ]; 708if ( m == - FLT_MAX ) continue ; 709const float w = sycl::native:: exp ( m - M ); 710L = sycl:: fma ((float)( part_l [ slot * G + hl ]), (float) w , L ); 711acc = sycl:: fma ((float)( part_acc [((size_t) slot * G + hl ) * HD + d ]), 712(float) w , acc ); 713} 714return { M , L , acc }; 715} 716 717constexpr int QsaMergeGroupChunks = 32 ; 718class QsaMergeTwoStageOriginal; 719class QsaMergeTwoStagePartial; 720class QsaMergeTwoStageFinal; 721 722void launch_qsa_merge_two_stage_original ( const float * part_acc , 723const float * part_m , const float * part_l , int n_chunks , float * out , 724sycl::queue * queue ) { 725auto props = sycl::ext::oneapi::experimental::properties{}; 726queue -> parallel_for < QsaMergeTwoStageOriginal > ( 727sycl:: nd_range < 3 > (sycl:: range < 3 > ( 1 , 1 , size_t( 24 ) * THREADS ), 728sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 729[ = ](sycl::nd_item < 3 > ) { 730attn_merge_kernel ( part_acc , part_m , part_l , n_chunks , out , 0 ); 731}); 732} 733 734void launch_qsa_merge_two_stage_partial ( const float * part_acc , 735const float * part_m , const float * part_l , int n_chunks , 736float * group_acc , float * group_m , float * group_l , int n_groups , 737float * out , sycl::queue * queue ) { 738static_assert( HD == 256 && G == 12 && THREADS == 256 ); 739auto props = sycl::ext::oneapi::experimental::properties{}; 740queue -> parallel_for < QsaMergeTwoStagePartial > ( 741sycl:: nd_range < 3 > (sycl:: range < 3 > ( 1 , 1 , size_t( 24 ) * n_groups * THREADS ), 742sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 743[ = ](sycl::nd_item < 3 > item ) { 744const size_t index = item . get_group ( 2 ); 745const int h = int( index / n_groups ), group = int( index % n_groups ); 746const int kvh = h / G , hl = h % G ; 747const int d = int( item . get_local_id ( 2 )); 748const int first = group * QsaMergeGroupChunks ; 749const int last = sycl:: min ( first + QsaMergeGroupChunks , n_chunks ); 750const auto partial = qsa_merge_group_partial ( 751part_acc , part_m , part_l , n_chunks , kvh , hl , d , first , last ); 752const int slot = ( kvh * n_groups + group ) * G + hl ; 753// Keep an unnormalised acc: the original final helper applies L once. 754group_acc [size_t( slot ) * HD + d ] = partial . acc ; 755if ( d == 0 ) { group_m [ slot ] = partial . m ; group_l [ slot ] = partial . l ; } 756}); 757// The caller's in-order queue/capture supplies the inter-kernel dependency. 758// No new wait/event/node API, SLM or global barrier is introduced here. 759queue -> parallel_for < QsaMergeTwoStageFinal > ( 760sycl:: nd_range < 3 > (sycl:: range < 3 > ( 1 , 1 , size_t( 24 ) * THREADS ), 761sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 762[ = ](sycl::nd_item < 3 > ) { 763attn_merge_kernel ( group_acc , group_m , group_l , n_groups , out , 0 ); 764}); 765} 766// END PRIVATE QSA TWO STAGE HELPER 767#if defined( STRATA_EXPERIMENTAL_SM60 ) 768// the current device is below sm_75 (per device: a layer split can mix cards); STRATA_ATTN_PRE75=0 turns PR #540's 769// kernel off (A/B) 770bool pre75_attn () { 771static const bool off = [] { 772const char * e = std:: getenv ( "STRATA_ATTN_PRE75" ); 773return e != nullptr && e [ 0 ] == '0' ; 774}(); 775static int cc [ 64 ] = {}; 776int dev = 0 ; 777if ( off ) return false; 778if ( cudaGetDevice ( & dev ) != cudaSuccess || dev < 0 || dev >= 64 ) { cudaGetLastError (); return false; } 779if ( cc [ dev ] == 0 ) { 780int major = 0 , minor = 0 ; 781if ( cudaDeviceGetAttribute ( & major , cudaDevAttrComputeCapabilityMajor , dev ) != cudaSuccess || 782cudaDeviceGetAttribute ( & minor , cudaDevAttrComputeCapabilityMinor , dev ) != cudaSuccess ) { 783cudaGetLastError (); 784return false; 785} 786cc [ dev ] = 10 * major + minor ; 787} 788return cc [ dev ] < 75 ; 789} 790#define STRATA_ATTN_CHUNK ( M ) (pre75_attn() ? attn_chunk_kernel_pre75<M> : attn_chunk_kernel<M>) 791#else 792#define STRATA_ATTN_CHUNK ( M ) attn_chunk_kernel<M> 793#endif 794 795// ---- SYCL port: the prompt path's chunk kernel with the scores computed one work-item per cell (attn_bench). 796// The warp kernel above splits a cell's 256 dimensions over a sub-group and adds them up with a 5-step shuffle 797// tree per query head - 60 shuffles a cell. Here work-item c holds cell c's key row (16-byte loads) and all 12 798// heads' dot products, reading the query heads from local memory, where every work-item reads the same element 799// at once. 128-cell chunks. The values pass is the warp kernel's. FP32, but another summation order than the warp 800// kernel: not bitwise (relative error ~2e-7 in attn_bench); attn_bench: 1.55-1.69x the warp kernel's time. 801constexpr int PC_CHUNK = 128 ; 802 803// 16 consecutive key values of row `row`, dimensions [d0, d0+16) 804template < int KV_MODE > 805__dpct_inline__ void load16_k ( const QsaAttnPools & p , long long row , int d0 , float * out ) { 806if constexpr ( KV_MODE == 0 ) { 807load8_f16 ( p , false, row , d0 , out ); 808load8_f16 ( p , false, row , d0 + 8 , out + 8 ); 809} else if constexpr ( KV_MODE == 1 || KV_MODE == 3 ) { 810const int8_t * codes = p . k_q + row * HD + d0 ; 811const float sc = sycl:: vec < sycl::half, 1 > (sycl:: bit_cast < sycl::half, unsigned short > ( 812p . k_scale [ row * ( HD / KV_Q8_GROUP ) + d0 / KV_Q8_GROUP ])) 813. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 814const sycl::int4 raw = * reinterpret_cast < const sycl::int4 *> ( codes ); 815const int8_t * c = reinterpret_cast < const int8_t *> ( & raw ); 816#pragma unroll 817for (int j = 0 ; j < 16 ; ++ j ) out [ j ] = (float) c [ j ] * sc ; 818} else { // q4_0 (rotated): element j of a 32-block in qs[j]'s low nibble, j + 16 in its high one 819load8_q4 ( p , false, row , d0 , out ); 820load8_q4 ( p , false, row , d0 + 8 , out + 8 ); 821} 822} 823 824template < int KV_MODE > 825__dpct_inline__ float load1_v ( const QsaAttnPools & p , long long row , int t ) { 826if constexpr ( KV_MODE == 0 ) { 827return sycl:: vec < sycl::half, 1 > (sycl:: bit_cast < sycl::half, unsigned short > ( p . v_pool [ row * HD + t ])) 828. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 829} else if constexpr ( KV_MODE == 1 ) { 830const float sc = sycl:: vec < sycl::half, 1 > (sycl:: bit_cast < sycl::half, unsigned short > ( 831p . v_scale [ row * ( HD / KV_Q8_GROUP ) + t / KV_Q8_GROUP ])) 832. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 833return (float) p . v_q [ row * HD + t ] * sc ; 834} else { 835constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 836const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + t / QK4_0 ; 837const float d = sycl:: vec < sycl::half, 1 > (sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 838. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 839const int rem = t % QK4_0 ; 840const uint8_t byte = blk -> qs [ rem < 16 ? rem : rem - 16 ]; 841return (float) (( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 )) * d ; 842} 843} 844 845template < int KV_MODE > 846__dpct_inline__ void attn_chunk_percell_kernel ( const float * __restrict__ q , QsaAttnPools p , 847const int32_t * __restrict__ ids , const int32_t * __restrict__ step , 848int n_kv_heads , int page_size , float scale , float * __restrict__ part_acc , 849float * __restrict__ part_m , float * __restrict__ part_l , int n_chunks , 850int cap , long long scratch_stride ) { 851auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 852auto sg = item_ct1 . get_sub_group (); 853q += (size_t) item_ct1 . get_group ( 0 ) * (size_t) ( n_kv_heads * G ) * HD ; 854ids += (size_t) item_ct1 . get_group ( 0 ) * (size_t) cap ; 855step += (size_t) item_ct1 . get_group ( 0 ) * kStepCount ; 856part_acc += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 857part_m += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 858part_l += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 859auto & sq = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ HD ] > ( 860sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 861auto & sp = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ PC_CHUNK ] > ( 862sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 863auto & srow = * sycl::ext::oneapi:: group_local_memory_for_overwrite < long long[ PC_CHUNK ] > ( 864sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 865const int n_ids = * ( step + kStepWidth ); 866const int chunk = item_ct1 . get_group ( 2 ), kvh = item_ct1 . get_group ( 1 ); 867const int t = item_ct1 . get_local_id ( 2 ); 868const int lane = (int) sg . get_local_id ()[ 0 ], warp = (int) sg . get_group_id ()[ 0 ]; 869const int SGS = (int) sg . get_local_range ()[ 0 ], NW = THREADS / SGS ; 870const int c0 = chunk * PC_CHUNK ; 871const int n_here = sycl:: min ( PC_CHUNK , n_ids - c0 ); 872const int slot = kvh * n_chunks + chunk ; 873if ( n_here <= 0 ) { 874if ( t < G ) { part_m [ slot * G + t ] = - FLT_MAX ; part_l [ slot * G + t ] = 0.0f ; } 875return ; 876} 877for (int i = t ; i < G * HD ; i += THREADS ) sq [ i / HD ][ i % HD ] = q [(size_t) ( kvh * G ) * HD + i ]; 878for (int c = t ; c < PC_CHUNK ; c += THREADS ) { 879long long r = -1 ; 880if ( c < n_here ) { 881const int cell = ids [ c0 + c ]; 882const long long page = (long long) p . page_table [ cell / page_size ]; 883if ( page >= 0 ) r = ( page * n_kv_heads + kvh ) * page_size + ( cell % page_size ); // -1: masked (KV streaming) 884} 885srow [ c ] = r ; 886} 887item_ct1 . barrier (sycl::access::fence_space:: local_space ); 888for (int c = t ; c < PC_CHUNK ; c += THREADS ) { 889if ( c >= n_here || srow [ c ] < 0 ) { 890#pragma unroll 891for (int h = 0 ; h < G ; ++ h ) sp [ h ][ c ] = - FLT_MAX ; 892continue ; 893} 894float acc_s [ G ]; 895#pragma unroll 896for (int h = 0 ; h < G ; ++ h ) acc_s [ h ] = 0.0f ; 897for (int d = 0 ; d < HD ; d += 16 ) { 898float kf [ 16 ]; 899load16_k < KV_MODE > ( p , srow [ c ], d , kf ); 900#pragma unroll 901for (int h = 0 ; h < G ; ++ h ) { // a 16-dimension block in four short chains, then into the cell's sum: 902float b0 = 0.0f , b1 = 0.0f , b2 = 0.0f , b3 = 0.0f ; // FP32 error near the warp kernel's tree 903#pragma unroll 904for (int j = 0 ; j < 16 ; j += 4 ) { 905b0 = sycl:: fma ( kf [ j ], sq [ h ][ d + j ], b0 ); 906b1 = sycl:: fma ( kf [ j + 1 ], sq [ h ][ d + j + 1 ], b1 ); 907b2 = sycl:: fma ( kf [ j + 2 ], sq [ h ][ d + j + 2 ], b2 ); 908b3 = sycl:: fma ( kf [ j + 3 ], sq [ h ][ d + j + 3 ], b3 ); 909} 910acc_s [ h ] += ( b0 + b1 ) + ( b2 + b3 ); 911} 912} 913#pragma unroll 914for (int h = 0 ; h < G ; ++ h ) sp [ h ][ c ] = acc_s [ h ] * scale ; 915} 916item_ct1 . barrier (sycl::access::fence_space:: local_space ); 917for (int h = warp ; h < G ; h += NW ) { 918float m = - FLT_MAX ; 919for (int c = lane ; c < PC_CHUNK ; c += SGS ) m = sycl:: fmax ( m , sp [ h ][ c ]); 920m = sycl:: reduce_over_group ( sg , m , sycl:: maximum < float > ()); 921float l = 0.0f ; 922for (int c = lane ; c < PC_CHUNK ; c += SGS ) { 923const float e = ( c < n_here && srow [ c ] >= 0 ) ? sycl::native:: exp ( sp [ h ][ c ] - m ) : 0.0f ; 924sp [ h ][ c ] = e ; 925l += e ; 926} 927l = sycl:: reduce_over_group ( sg , l , sycl:: plus < float > ()); 928if ( lane == 0 ) { part_m [ slot * G + h ] = m ; part_l [ slot * G + h ] = l ; } 929} 930item_ct1 . barrier (sycl::access::fence_space:: local_space ); 931float acc [ G ]; 932#pragma unroll 933for (int h = 0 ; h < G ; ++ h ) acc [ h ] = 0.0f ; 934for (int c = 0 ; c < n_here ; ++ c ) { 935if ( srow [ c ] < 0 ) continue ; // masked above, weight 0 936const float v = load1_v < KV_MODE == 3 ? 2 : KV_MODE > ( p , srow [ c ], t ); 937#pragma unroll 938for (int h = 0 ; h < G ; ++ h ) acc [ h ] = sycl:: fma ( sp [ h ][ c ], v , acc [ h ]); 939} 940#pragma unroll 941for (int h = 0 ; h < G ; ++ h ) part_acc [((size_t) slot * G + h ) * HD + t ] = acc [ h ]; 942} 943 944template < int KV_MODE > 945void launch_percell (dpct::queue_ptr st , const float * q , const QsaAttnPools & pools , const int32_t * ids , 946const int32_t * steps , int64_t cap , const QsaShapes & s , float * part_acc , float * part_m , float * part_l , 947int n_chunks , long long stride , int64_t n_q , float scale ) { 948const sycl::range < 3 > grid ((size_t) n_q , (size_t) s . n_head_kv , (size_t) n_chunks ); 949st -> parallel_for (sycl:: nd_range < 3 > ( grid * sycl:: range < 3 > ( 1 , 1 , THREADS ), sycl:: range < 3 > ( 1 , 1 , THREADS )), 950[ = ](sycl::nd_item < 3 > ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 951attn_chunk_percell_kernel < KV_MODE > ( q , pools , ids , steps , (int) s . n_head_kv , (int) s . page_size , 952scale , part_acc , part_m , part_l , n_chunks , (int) cap , stride ); 953}); 954} 955 956} // namespace 957 958 959static void launch_qsa_chunk_down (int, const float * , const QsaAttnPools & , const int32_t * , const int32_t * ,int64_t, 960const QsaShapes & ,float * ,float * ,int64_t,void * ); 961bool qsa_chunk_down_supported (int64_t cap , int64_t n_q , const QsaShapes & s , void * stream ) { 962static const bool enabled = [] { 963const char * v = std:: getenv ( "STRATA_SYCL_QSA_CHUNK_DOWN" ); 964return ! v || v [ 0 ] == '1' ; 965}(); 966const bool batch = n_q >= 1 && n_q <= 6 ; 967const bool shape = ( cap == 2051 && ( batch || n_q == 32 )) || ( cap == 32768 && batch ); 968return enabled && shape && s . n_head == 24 && s . n_head_kv == 2 && s . page_size == 4 && 969strata:: intel_gpu_gen (strata:: q_of ( stream ) -> get_device ()) == strata::IntelGpuGen:: Battlemage ; 970} 971// BEGIN PRIVATE QSA TWO STAGE ENTRY 972uint64_t qsa_merge_two_stage_scratch_floats (int n_chunks ) { 973if ( n_chunks <= 0 || n_chunks > INT_MAX / 24 ) 974throw std:: invalid_argument ( "QSA two-stage scratch arguments" ); 975const uint64_t n_groups = (uint64_t( n_chunks ) + 31 ) / 32 ; 976return n_groups * 24 * ( HD + 2 ) + 64 ; 977} 978 979void qsa_merge_two_stage_probe (int mode , const float * scratch , int n_chunks , 980float * group_scratch , float * out , void * stream ) { 981if ( mode < 0 || mode > 1 || ! scratch || ! out || ! stream || n_chunks <= 0 || 982n_chunks > INT_MAX / 24 || ( mode == 1 && ! group_scratch )) 983throw std:: invalid_argument ( "QSA two-stage diagnostic arguments" ); 984auto * queue = strata:: q_of ( stream ); 985if (! queue -> is_in_order ()) 986throw std:: invalid_argument ( "QSA two-stage probe requires an in-order queue" ); 987const float * part_m = scratch + size_t( n_chunks ) * 24 * HD ; 988const float * part_l = part_m + size_t( n_chunks ) * 24 ; 989if ( mode == 0 ) { 990launch_qsa_merge_two_stage_original ( scratch , part_m , part_l , n_chunks , out , queue ); 991return ; 992} 993const int n_groups = ( n_chunks + 31 ) / 32 ; 994float * group_m = group_scratch + size_t( n_groups ) * 24 * HD ; 995float * group_l = group_m + size_t( n_groups ) * 24 ; 996launch_qsa_merge_two_stage_partial ( scratch , part_m , part_l , n_chunks , 997group_scratch , group_m , group_l , n_groups , out , queue ); 998} 999 1000// END PRIVATE QSA TWO STAGE ENTRY 1001void qsa_decode_attn_batch ( const float * q , const QsaAttnPools & pools , const int32_t * ids , const int32_t * steps , 1002int64_t cap , const QsaShapes & s , float * scratch , float * attn , int64_t n_q , void * stream ) { 1003if ( n_q <= 0 ) return ; 1004if ( s . head_dim != HD || s . n_head != (int64_t) G * s . n_head_kv || cap <= 0 || ! scratch || ! ids || ! steps || 1005! pools . page_table || n_q > 65535 ) { 1006std:: fprintf ( stderr , "qsa_decode_attn_batch: unsupported geometry or missing buffers\n" ); 1007std:: exit ( 1 ); 1008} 1009const int kv_mode = pools . k_q4 != nullptr ? 2 : ( pools . k_q != nullptr && pools . v_q4 != nullptr ? 3 1010: ( pools . k_q != nullptr ? 1 : 0 )); 1011strata::workload_trace::Scope trace ( "decode.qsa_attention" , stream , n_q , s . n_head , s . head_dim , 1012cap , s . n_head_kv , kv_mode ); 1013if ( qsa_chunk_down_supported ( cap , n_q , s , stream ) && kv_mode == 1 && pools . v_q ) { 1014launch_qsa_chunk_down ( 1 , q , pools , ids , steps , cap , s , scratch , attn , n_q , stream ); 1015return ; 1016} 1017const int n_chunks = (int) (( cap + CHUNK - 1 ) / CHUNK ); 1018// per query: [acc: n_chunks*n_head*HD][m: n_chunks*n_head][l: n_chunks*n_head], all offsets from one stride 1019const long long stride = (long long) qsa_decode_attn_scratch_floats ( cap , s ); 1020float * part_acc = scratch ; 1021float * part_m = scratch + (size_t) n_chunks * s . n_head * HD ; 1022float * part_l = part_m + (size_t) n_chunks * s . n_head ; 1023const float scale = 1.0f / sqrtf ((float) HD ); 1024const dpct::dim3 grid ((unsigned) n_chunks , (unsigned) s . n_head_kv , 1025(unsigned) n_q ); 1026dpct::queue_ptr st = strata:: q_of ( stream ); 1027// S25: the score phase one cell per thread (bit-identical scores, no shuffle reductions). Prompt batches take 1028// it by default: on 2x Arc Pro B65 it reads a 4,106-token prompt 5.2% faster, 8,191 tokens 5.2% and 16,380 1029// tokens 4.8%, with the same tokens; the decode window's few queries per call are 1.4% slower with it (56.6-57.0 1030// against 55.9-56.3 tok/s), so the choice follows the query count. It is a Battlemage (Xe2) default only: on an Arc 1031// A750 (Alchemist) the same switch reads a 4,106-token prompt 0.7% slower (three interleaved runs, 4 of 5 pairs 1032// faster with it off), so the generation comes from the device's architecture id (strata::intel_gpu_gen: the 1033// runtime's architecture enum first, the name only as a fallback), never from the name alone. 1034// STRATA_ATTN_LANECELL=1/0 forces either way on any device. 1035static const int lane_cell_env = [] { 1036const char * v = std:: getenv ( "STRATA_ATTN_LANECELL" ); 1037return v != nullptr && v [ 0 ] != '\0' ? std:: atoi ( v ) : -1 ; 1038}(); 1039const bool lane_cell = lane_cell_env >= 0 1040? lane_cell_env != 0 1041: ( n_q >= 16 && strata:: intel_gpu_gen ( st -> get_device ()) == strata::IntelGpuGen:: Battlemage ); 1042#define STRATA_ATTN_LC ( M ) \ 1043{ \ 1044auto exp_props = sycl::ext::oneapi::experimental::properties{ \ 1045sycl::ext::oneapi::experimental::use_root_sync}; \ 1046dpct::has_capability_or_fail(st->get_device(), {sycl::aspect::fp16}); \ 1047\ 1048strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { \ 1049auto q_ct0 = q; \ 1050QsaAttnPools pools_ct1 = pools; \ 1051auto ids_ct2 = ids; \ 1052auto steps_ct3 = steps; \ 1053auto s_n_head_kv_ct4 = (int)s.n_head_kv; \ 1054auto s_page_size_ct5 = (int)s.page_size; \ 1055auto scale_ct6 = scale; \ 1056auto part_acc_ct7 = part_acc; \ 1057auto part_m_ct8 = part_m; \ 1058auto part_l_ct9 = part_l; \ 1059auto n_chunks_ct10 = n_chunks; \ 1060auto cap_ct11 = (int)cap; \ 1061auto stride_ct12 = stride; \ 1062\ 1063cgh.parallel_for<dpct_kernel_name<class attn_chunk_kernel_6e87d5, \ 1064dpct_kernel_scalar<M>, \ 1065dpct_kernel_scalar<true>>>( \ 1066sycl::nd_range<3>(grid * sycl::range(1, 1, THREADS), \ 1067sycl::range(1, 1, THREADS)), \ 1068exp_props, \ 1069[=](sycl::nd_item<3> item_ct1) \ 1070[[sycl::reqd_sub_group_size(32)]] { \ 1071attn_chunk_kernel<M, true>( \ 1072q_ct0, pools_ct1, ids_ct2, steps_ct3, \ 1073s_n_head_kv_ct4, s_page_size_ct5, scale_ct6, \ 1074part_acc_ct7, part_m_ct8, part_l_ct9, \ 1075n_chunks_ct10, cap_ct11, stride_ct12); \ 1076}); \ 1077}); \ 1078} 1079#define STRATA_ATTN_RSK ( M , V ) \ 1080{ \ 1081auto exp_props = sycl::ext::oneapi::experimental::properties{ \ 1082sycl::ext::oneapi::experimental::use_root_sync}; \ 1083dpct::has_capability_or_fail(st->get_device(), {sycl::aspect::fp16}); \ 1084\ 1085strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { \ 1086auto q_ct0 = q; \ 1087QsaAttnPools pools_ct1 = pools; \ 1088auto ids_ct2 = ids; \ 1089auto steps_ct3 = steps; \ 1090auto s_n_head_kv_ct4 = (int)s.n_head_kv; \ 1091auto s_page_size_ct5 = (int)s.page_size; \ 1092auto scale_ct6 = scale; \ 1093auto part_acc_ct7 = part_acc; \ 1094auto part_m_ct8 = part_m; \ 1095auto part_l_ct9 = part_l; \ 1096auto n_chunks_ct10 = n_chunks; \ 1097auto cap_ct11 = (int)cap; \ 1098auto stride_ct12 = stride; \ 1099\ 1100cgh.parallel_for<dpct_kernel_name<class attn_chunk_kernel_rs_7c2a91, \ 1101dpct_kernel_scalar<M>, \ 1102dpct_kernel_scalar<V>>>( \ 1103sycl::nd_range<3>(grid * sycl::range(1, 1, THREADS), \ 1104sycl::range(1, 1, THREADS)), \ 1105exp_props, \ 1106[=](sycl::nd_item<3> item_ct1) \ 1107[[sycl::reqd_sub_group_size(32)]] { \ 1108attn_chunk_kernel<M, false, V>( \ 1109q_ct0, pools_ct1, ids_ct2, steps_ct3, \ 1110s_n_head_kv_ct4, s_page_size_ct5, scale_ct6, \ 1111part_acc_ct7, part_m_ct8, part_l_ct9, \ 1112n_chunks_ct10, cap_ct11, stride_ct12); \ 1113}); \ 1114}); \ 1115} 1116#undef STRATA_ATTN_RSK_DEFINED 1117// 151-b70: STRATA_ATTN_RS=2 (default): the probabilities in [cell][head] order and the value phase reading a cell's 12 as three vectors - the same 1118// bits (first-token logits md5 equal at 4K and 16K), +0.8..0.9% prompt tok/s on the B70; =1 adds a 16-shuffle reduce-scatter for the scores 1119// (bit-exact too, but -1.5% here: the score phase is not shuffle bound); =0 the layout before 1120static const int rs_on = [] { const char * v = std:: getenv ( "STRATA_ATTN_RS" ); return v ? std:: atoi ( v ) : 2 ; }(); 1121if ( lane_cell ) { 1122if ( kv_mode == 3 ) { STRATA_ATTN_LC ( 3 ); } 1123else if ( kv_mode == 2 ) { STRATA_ATTN_LC ( 2 ); } 1124else if ( kv_mode == 1 ) { STRATA_ATTN_LC ( 1 ); } 1125else { STRATA_ATTN_LC ( 0 ); } 1126} else if ( rs_on == 1 ) { 1127if ( kv_mode == 3 ) { STRATA_ATTN_RSK ( 3 , 1 ); } 1128else if ( kv_mode == 2 ) { STRATA_ATTN_RSK ( 2 , 1 ); } 1129else if ( kv_mode == 1 ) { STRATA_ATTN_RSK ( 1 , 1 ); } 1130else { STRATA_ATTN_RSK ( 0 , 1 ); } 1131} else if ( rs_on == 2 ) { 1132if ( kv_mode == 3 ) { STRATA_ATTN_RSK ( 3 , 2 ); } 1133else if ( kv_mode == 2 ) { STRATA_ATTN_RSK ( 2 , 2 ); } 1134else if ( kv_mode == 1 ) { STRATA_ATTN_RSK ( 1 , 2 ); } 1135else { STRATA_ATTN_RSK ( 0 , 2 ); } 1136} else 1137#undef STRATA_ATTN_LC 1138#undef STRATA_ATTN_RSK 1139if ( kv_mode == 3 ) 1140{ 1141auto exp_props = sycl::ext::oneapi::experimental::properties{ 1142sycl::ext::oneapi::experimental:: use_root_sync }; 1143dpct:: has_capability_or_fail ( st -> get_device (), {sycl::aspect:: fp16 }); 1144 1145strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_chunk_kernel_dfacfc, 1146dpct_kernel_scalar < 3 >>> ( 1147sycl:: nd_range < 3 > ( grid * sycl:: range ( 1 , 1 , THREADS ), 1148sycl:: range ( 1 , 1 , THREADS )), 1149exp_props , 1150[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 1151attn_chunk_kernel < 3 > ( q , pools , ids , steps , (int) s . n_head_kv , 1152(int) s . page_size , scale , part_acc , part_m , 1153part_l , n_chunks , (int) cap , stride ); 1154}); 1155} else if ( kv_mode == 2 ) 1156{ 1157auto exp_props = sycl::ext::oneapi::experimental::properties{ 1158sycl::ext::oneapi::experimental:: use_root_sync }; 1159dpct:: has_capability_or_fail ( st -> get_device (), {sycl::aspect:: fp16 }); 1160 1161strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_chunk_kernel_b0d448, 1162dpct_kernel_scalar < 2 >>> ( 1163sycl:: nd_range < 3 > ( grid * sycl:: range ( 1 , 1 , THREADS ), 1164sycl:: range ( 1 , 1 , THREADS )), 1165exp_props , 1166[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 1167attn_chunk_kernel < 2 > ( q , pools , ids , steps , (int) s . n_head_kv , 1168(int) s . page_size , scale , part_acc , part_m , 1169part_l , n_chunks , (int) cap , stride ); 1170}); 1171} else if ( kv_mode == 1 ) 1172{ 1173auto exp_props = sycl::ext::oneapi::experimental::properties{ 1174sycl::ext::oneapi::experimental:: use_root_sync }; 1175dpct:: has_capability_or_fail ( st -> get_device (), {sycl::aspect:: fp16 }); 1176 1177strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_chunk_kernel_2ad0a4, 1178dpct_kernel_scalar < 1 >>> ( 1179sycl:: nd_range < 3 > ( grid * sycl:: range ( 1 , 1 , THREADS ), 1180sycl:: range ( 1 , 1 , THREADS )), 1181exp_props , 1182[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 1183attn_chunk_kernel < 1 > ( q , pools , ids , steps , (int) s . n_head_kv , 1184(int) s . page_size , scale , part_acc , part_m , 1185part_l , n_chunks , (int) cap , stride ); 1186}); 1187} else { 1188auto exp_props = sycl::ext::oneapi::experimental::properties{ 1189sycl::ext::oneapi::experimental:: use_root_sync }; 1190dpct:: has_capability_or_fail ( st -> get_device (), {sycl::aspect:: fp16 }); 1191 1192strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_chunk_kernel_5c04f7, 1193dpct_kernel_scalar < 0 >>> ( 1194sycl:: nd_range < 3 > ( grid * sycl:: range ( 1 , 1 , THREADS ), 1195sycl:: range ( 1 , 1 , THREADS )), 1196exp_props , 1197[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 1198attn_chunk_kernel < 0 > ( q , pools , ids , steps , (int) s . n_head_kv , 1199(int) s . page_size , scale , part_acc , part_m , 1200part_l , n_chunks , (int) cap , stride ); 1201}); 1202} 1203{ 1204auto exp_props = sycl::ext::oneapi::experimental::properties{ 1205sycl::ext::oneapi::experimental:: use_root_sync }; 1206 1207if ( merge_active_on ( st -> get_device ()) && n_q <= 8 ) { 1208strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_merge_active >> ( 1209sycl:: nd_range < 3 > ( 1210sycl:: range ( 1 , (unsigned) n_q , (unsigned) s . n_head ) * 1211sycl:: range ( 1 , 1 , HD ), 1212sycl:: range ( 1 , 1 , HD )), 1213exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1214attn_merge_active_kernel ( part_acc , part_m , part_l , n_chunks , 1215attn , stride , steps ); 1216}); 1217} else { 1218strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_merge_kernel_338175 >> ( 1219sycl:: nd_range < 3 > ( 1220sycl:: range ( 1 , (unsigned) n_q , (unsigned) s . n_head ) * 1221sycl:: range ( 1 , 1 , HD ), 1222sycl:: range ( 1 , 1 , HD )), 1223exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1224attn_merge_kernel ( part_acc , part_m , part_l , n_chunks , attn , 1225stride ); 1226}); 1227} 1228} 1229/* 1230DPCT1010: SYCL uses exceptions to report errors and does not use the 1231error codes. The cudaGetLastError function call was replaced with 0. You 1232need to rewrite this code. 1233*/ 1234const dpct::err0 e = 0 ; 1235} 1236 1237namespace { 1238template < int M > 1239void attn_rows_chunks (dpct::queue_ptr st , const dpct::dim3 & grid , const float * q , const QsaAttnPools & pools , 1240const int32_t * ids , const int32_t * steps , int n_kv_heads , int page_size , float scale , 1241float * part_acc , float * part_m , float * part_l , int n_chunks , int cap , long long stride , 1242const QsaAttnPoolsRows & rows ) { 1243auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental:: use_root_sync }; 1244dpct:: has_capability_or_fail ( st -> get_device (), {sycl::aspect:: fp16 }); 1245const QsaAttnPools p0 = pools ; 1246const QsaAttnPoolsRows rr = rows ; 1247strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_rows_chunk_kernel_9f2, dpct_kernel_scalar < M >>> ( 1248sycl:: nd_range < 3 > ( grid * sycl:: range ( 1 , 1 , THREADS ), sycl:: range ( 1 , 1 , THREADS )), exp_props , 1249[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 1250attn_chunk_kernel < M, false, 2 > ( q , p0 , ids , steps , n_kv_heads , page_size , scale , part_acc , part_m , part_l , 1251n_chunks , cap , stride , rr ); 1252}); 1253} 1254} // namespace 1255 1256void qsa_decode_attn_batch_rows ( const float * q , const QsaAttnPoolsRows & rows , const int32_t * ids , const int32_t * steps , 1257int64_t cap , const QsaShapes & s , float * scratch , float * attn , int64_t n_q , void * stream ) { 1258if ( n_q <= 0 ) return ; 1259const QsaAttnPools & pools = rows . p [ 0 ]; 1260if ( rows . n != n_q || n_q > kAttnRowsMax || s . head_dim != HD || s . n_head != (int64_t) G * s . n_head_kv || cap <= 0 || 1261! scratch || ! ids || ! steps ) { 1262std:: fprintf ( stderr , "qsa_decode_attn_batch_rows: unsupported geometry or missing buffers\n" ); 1263std:: exit ( 1 ); 1264} 1265static const int rs_on = [] { const char * v = std:: getenv ( "STRATA_ATTN_RS" ); return v ? std:: atoi ( v ) : 2 ; }(); 1266static const int lane_cell_env = [] { 1267const char * v = std:: getenv ( "STRATA_ATTN_LANECELL" ); 1268return v != nullptr && v [ 0 ] != '\0' ? std:: atoi ( v ) : -1 ; 1269}(); 1270const long long stride = (long long) qsa_decode_attn_scratch_floats ( cap , s ); 1271if ( rs_on != 2 || lane_cell_env == 1 ) { // a non-default per-row kernel is selected: the per-row calls 1272for (int64_t z = 0 ; z < n_q ; ++ z ) 1273qsa_decode_attn_batch ( q + (size_t) z * s . n_head * HD , rows . p [ z ], ids + (size_t) z * cap , 1274steps + (size_t) z * kStepCount , cap , s , scratch + (size_t) z * stride , 1275attn + (size_t) z * s . n_head * HD , 1 , stream ); 1276return ; 1277} 1278const int kv_mode = pools . k_q4 != nullptr ? 2 : ( pools . k_q != nullptr && pools . v_q4 != nullptr ? 3 1279: ( pools . k_q != nullptr ? 1 : 0 )); 1280const int n_chunks = (int) (( cap + CHUNK - 1 ) / CHUNK ); 1281float * part_acc = scratch ; 1282float * part_m = scratch + (size_t) n_chunks * s . n_head * HD ; 1283float * part_l = part_m + (size_t) n_chunks * s . n_head ; 1284const float scale = 1.0f / sqrtf ((float) HD ); 1285const dpct::dim3 grid ((unsigned) n_chunks , (unsigned) s . n_head_kv , (unsigned) n_q ); 1286dpct::queue_ptr st = strata:: q_of ( stream ); 1287if ( kv_mode == 3 ) attn_rows_chunks < 3 > ( st , grid , q , pools , ids , steps , (int) s . n_head_kv , (int) s . page_size , scale , part_acc , part_m , part_l , n_chunks , (int) cap , stride , rows ); 1288else if ( kv_mode == 2 ) attn_rows_chunks < 2 > ( st , grid , q , pools , ids , steps , (int) s . n_head_kv , (int) s . page_size , scale , part_acc , part_m , part_l , n_chunks , (int) cap , stride , rows ); 1289else if ( kv_mode == 1 ) attn_rows_chunks < 1 > ( st , grid , q , pools , ids , steps , (int) s . n_head_kv , (int) s . page_size , scale , part_acc , part_m , part_l , n_chunks , (int) cap , stride , rows ); 1290else attn_rows_chunks < 0 > ( st , grid , q , pools , ids , steps , (int) s . n_head_kv , (int) s . page_size , scale , part_acc , part_m , part_l , n_chunks , (int) cap , stride , rows ); 1291{ 1292auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental:: use_root_sync }; 1293strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_merge_rows_kernel_9f2 >> ( 1294sycl:: nd_range < 3 > (sycl:: range ( 1 , (unsigned) n_q , (unsigned) s . n_head ) * sycl:: range ( 1 , 1 , HD ), 1295sycl:: range ( 1 , 1 , HD )), 1296exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1297attn_merge_kernel ( part_acc , part_m , part_l , n_chunks , attn , stride ); 1298}); 1299} 1300} 1301 1302uint64_t qsa_decode_attn_scratch_floats (int64_t cap , const QsaShapes & s ) { 1303const int64_t chunks = ( cap + CHUNK - 1 ) / CHUNK ; 1304return (uint64_t) chunks * (uint64_t) s . n_head * ( HD + 2 ) + 64 ; 1305} 1306 1307void qsa_decode_attn_step ( const float * q , const QsaAttnPools & pools , const int32_t * ids , const int32_t * step , 1308int64_t cap , const QsaShapes & s , float * scratch , float * attn , void * stream ) { 1309if ( s . head_dim != HD || s . n_head != (int64_t) G * s . n_head_kv || cap <= 0 || ! scratch || ! ids || ! step || 1310! pools . page_table ) { 1311std:: fprintf ( stderr , "qsa_decode_attn: unsupported geometry or missing buffers\n" ); 1312std:: exit ( 1 ); 1313} 1314const int kv_mode = pools . k_q4 != nullptr ? 2 : ( pools . k_q != nullptr && pools . v_q4 != nullptr ? 3 1315: ( pools . k_q != nullptr ? 1 : 0 )); 1316if ( kv_mode == 3 ? (! pools . k_scale || ! pools . v_q4 ) 1317: ( kv_mode == 2 ? (! pools . v_q4 ) : ( kv_mode == 1 ? (! pools . v_q || ! pools . k_scale || ! pools . v_scale ) 1318: (! pools . k_pool || ! pools . v_pool )))) { 1319std:: fprintf ( stderr , "qsa_decode_attn: incomplete KV pools\n" ); 1320std:: exit ( 1 ); 1321} 1322if ( qsa_chunk_down_supported ( cap , 1 , s , stream ) && kv_mode == 1 ){ 1323launch_qsa_chunk_down ( 1 , q , pools , ids , step , cap , s , scratch , attn , 1 , stream ); return ; 1324} 1325const int n_chunks = (int) (( cap + CHUNK - 1 ) / CHUNK ); 1326float * part_acc = scratch ; 1327float * part_m = scratch + (size_t) n_chunks * s . n_head * HD ; 1328float * part_l = part_m + (size_t) n_chunks * s . n_head ; 1329const float scale = 1.0f / sqrtf ((float) HD ); 1330const dpct::dim3 grid ((unsigned) n_chunks , (unsigned) s . n_head_kv ); 1331dpct::queue_ptr st = strata:: q_of ( stream ); 1332if ( kv_mode == 3 ) 1333{ 1334auto exp_props = sycl::ext::oneapi::experimental::properties{ 1335sycl::ext::oneapi::experimental:: use_root_sync }; 1336dpct:: has_capability_or_fail ( st -> get_device (), {sycl::aspect:: fp16 }); 1337 1338strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_chunk_kernel_f262e6, 1339dpct_kernel_scalar < 3 >>> ( 1340sycl:: nd_range < 3 > ( grid * sycl:: range ( 1 , 1 , THREADS ), 1341sycl:: range ( 1 , 1 , THREADS )), 1342exp_props , 1343[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 1344attn_chunk_kernel < 3 > ( q , pools , ids , step , (int) s . n_head_kv , 1345(int) s . page_size , scale , part_acc , part_m , 1346part_l , n_chunks , 0 , 0 ); 1347}); 1348} else if ( kv_mode == 2 ) 1349{ 1350auto exp_props = sycl::ext::oneapi::experimental::properties{ 1351sycl::ext::oneapi::experimental:: use_root_sync }; 1352dpct:: has_capability_or_fail ( st -> get_device (), {sycl::aspect:: fp16 }); 1353 1354strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_chunk_kernel_4fd923, 1355dpct_kernel_scalar < 2 >>> ( 1356sycl:: nd_range < 3 > ( grid * sycl:: range ( 1 , 1 , THREADS ), 1357sycl:: range ( 1 , 1 , THREADS )), 1358exp_props , 1359[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 1360attn_chunk_kernel < 2 > ( q , pools , ids , step , (int) s . n_head_kv , 1361(int) s . page_size , scale , part_acc , part_m , 1362part_l , n_chunks , 0 , 0 ); 1363}); 1364} else if ( kv_mode == 1 ) 1365{ 1366auto exp_props = sycl::ext::oneapi::experimental::properties{ 1367sycl::ext::oneapi::experimental:: use_root_sync }; 1368dpct:: has_capability_or_fail ( st -> get_device (), {sycl::aspect:: fp16 }); 1369 1370strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_chunk_kernel_f6855e, 1371dpct_kernel_scalar < 1 >>> ( 1372sycl:: nd_range < 3 > ( grid * sycl:: range ( 1 , 1 , THREADS ), 1373sycl:: range ( 1 , 1 , THREADS )), 1374exp_props , 1375[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 1376attn_chunk_kernel < 1 > ( q , pools , ids , step , (int) s . n_head_kv , 1377(int) s . page_size , scale , part_acc , part_m , 1378part_l , n_chunks , 0 , 0 ); 1379}); 1380} else { 1381auto exp_props = sycl::ext::oneapi::experimental::properties{ 1382sycl::ext::oneapi::experimental:: use_root_sync }; 1383dpct:: has_capability_or_fail ( st -> get_device (), {sycl::aspect:: fp16 }); 1384 1385strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_chunk_kernel_7c8e09, 1386dpct_kernel_scalar < 0 >>> ( 1387sycl:: nd_range < 3 > ( grid * sycl:: range ( 1 , 1 , THREADS ), 1388sycl:: range ( 1 , 1 , THREADS )), 1389exp_props , 1390[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 1391attn_chunk_kernel < 0 > ( q , pools , ids , step , (int) s . n_head_kv , 1392(int) s . page_size , scale , part_acc , part_m , 1393part_l , n_chunks , 0 , 0 ); 1394}); 1395} 1396{ 1397auto exp_props = sycl::ext::oneapi::experimental::properties{ 1398sycl::ext::oneapi::experimental:: use_root_sync }; 1399 1400strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class attn_merge_kernel_c055e7 >> ( 1401sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , (unsigned) s . n_head ) * 1402sycl:: range ( 1 , 1 , HD ), 1403sycl:: range ( 1 , 1 , HD )), 1404exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1405attn_merge_kernel ( part_acc , part_m , part_l , n_chunks , attn , 0 ); 1406}); 1407} 1408/* 1409DPCT1010: SYCL uses exceptions to report errors and does not use the 1410error codes. The cudaGetLastError function call was replaced with 0. You 1411need to rewrite this code. 1412*/ 1413const dpct::err0 e = 0 ; 1414} 1415 1416// SYCL port, prompt path only (prefill.cpp; decode and the draft layer keep qsa_decode_attn_batch): the chunk 1417// attention with the per-cell score kernel above, 128-cell chunks in the same scratch as the warp kernel (its stride 1418// is the 64-cell layout's, so it fits). FP32, another summation order than the warp kernel: not bitwise. 1419void qsa_prompt_attn_percell ( const float * q , const QsaAttnPools & pools , const int32_t * ids , const int32_t * steps , 1420int64_t cap , const QsaShapes & s , float * scratch , float * attn , int64_t n_q , void * stream ) { 1421if ( n_q <= 0 ) return ; 1422const int kv_mode = pools . k_q4 != nullptr ? 2 : ( pools . k_q != nullptr && pools . v_q4 != nullptr ? 3 1423: ( pools . k_q != nullptr ? 1 : 0 )); 1424const long long stride = (long long) qsa_decode_attn_scratch_floats ( cap , s ); 1425const float scale = 1.0f / sqrtf ((float) HD ); 1426dpct::queue_ptr st = strata:: q_of ( stream ); 1427const int nc = (int) (( cap + PC_CHUNK - 1 ) / PC_CHUNK ); 1428float * pa = scratch ; 1429float * pm = scratch + (size_t) nc * s . n_head * HD ; 1430float * pl = pm + (size_t) nc * s . n_head ; 1431if ( kv_mode == 3 ) launch_percell < 3 > ( st , q , pools , ids , steps , cap , s , pa , pm , pl , nc , stride , n_q , scale ); 1432else if ( kv_mode == 2 ) launch_percell < 2 > ( st , q , pools , ids , steps , cap , s , pa , pm , pl , nc , stride , n_q , scale ); 1433else if ( kv_mode == 1 ) launch_percell < 1 > ( st , q , pools , ids , steps , cap , s , pa , pm , pl , nc , stride , n_q , scale ); 1434else launch_percell < 0 > ( st , q , pools , ids , steps , cap , s , pa , pm , pl , nc , stride , n_q , scale ); 1435st -> parallel_for (sycl:: nd_range < 3 > (sycl:: range < 3 > ( 1 , (size_t) n_q , (size_t) s . n_head ) * sycl:: range < 3 > ( 1 , 1 , HD ), 1436sycl:: range < 3 > ( 1 , 1 , HD )), 1437[ = ](sycl::nd_item < 3 > ) { attn_merge_kernel ( pa , pm , pl , nc , attn , stride ); }); 1438} 1439 1440} // namespace strata::kernels 1441 1442#include <stdexcept> 1443 1444namespace strata::kernels { 1445namespace { 1446__dpct_inline__ float lane0_sum (float value ){ 1447auto group = sycl::ext::oneapi::this_work_item:: get_sub_group (); 1448#pragma unroll 1449for (int offset = 16 ; offset > 0 ; offset >>= 1 ) value += sycl:: shift_group_left ( group , value , offset ); 1450return value ; 1451} 1452template < int KV_MODE > 1453/* 1454DPCT1110: The total declared local variable size in device function 1455chunk_down_kernel exceeds 128 bytes and may cause high register pressure. 1456Consult with your hardware vendor to find the total register size available and 1457adjust the code, or use smaller sub-group size to avoid high register pressure. 1458*/ 1459__dpct_inline__ void 1460chunk_down_kernel ( const float * __restrict__ q , QsaAttnPools p , 1461const int32_t * __restrict__ ids , 1462const int32_t * __restrict__ step , int n_kv_heads , 1463int page_size , float scale , float * __restrict__ part_acc , 1464float * __restrict__ part_m , float * __restrict__ part_l , 1465int n_chunks , int cap = 0 , long long scratch_stride = 0 ) { 1466// batched form: query blockIdx.z, with its own q row, selection, step and scratch 1467auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1468q += (size_t) item_ct1 . get_group ( 0 ) * (size_t)( n_kv_heads * G ) * HD ; 1469ids += (size_t) item_ct1 . get_group ( 0 ) * (size_t) cap ; 1470step += (size_t) item_ct1 . get_group ( 0 ) * kStepCount ; 1471part_acc += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 1472part_m += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 1473part_l += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 1474auto & sq = 1475* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ HD ] > ( 1476sycl::ext::oneapi::this_work_item:: get_work_group < 14773 > ()); // 12 KB: this KV head's query heads 1478auto & sp = 1479* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ CHUNK ] > ( 1480sycl::ext::oneapi::this_work_item:: get_work_group < 14813 > ()); // scores, then probabilities 1482auto & srow = 1483* sycl::ext::oneapi:: group_local_memory_for_overwrite < long long[ CHUNK ] > ( 1484sycl::ext::oneapi::this_work_item:: get_work_group < 14853 > ()); // pool row of each cell (page, kv head, slot) 1486/* 1487DPCT1098: The '*' expression is used instead of the __ldg call. These 1488two expressions do not provide the exact same functionality. Check the 1489generated code for potential precision and/or performance issues. 1490*/ 1491const int n_ids = * ( step + kStepWidth ); 1492const int chunk = item_ct1 . get_group ( 2 ), kvh = item_ct1 . get_group ( 1 ); 1493const int t = item_ct1 . get_local_id ( 2 ), lane = t & 31 , warp = t >> 5 ; 1494const int c0 = chunk * CHUNK ; 1495const int n_here = sycl:: min ( CHUNK , n_ids - c0 ); 1496const int slot = kvh * n_chunks + chunk ; 1497if ( n_here <= 0 ) { 1498if ( t < G ) { part_m [ slot * G + t ] = - FLT_MAX ; part_l [ slot * G + t ] = 0.0f ; } 1499return ; 1500} 1501#pragma unroll 1502for (int i = t ; i < G * HD ; i += THREADS ) 1503sq [ i / HD ][ i % HD ] = q [(size_t)( kvh * G ) * HD + i ]; 1504if ( t < CHUNK ) { 1505long long r = -1 ; 1506if ( t < n_here ) { 1507const int cell = ids [ c0 + t ]; 1508const long long page = (long long) p . page_table [ cell / page_size ]; 1509// a block the KV streaming could not make resident keeps page -1 (ctl[3]); its cells are masked 1510// (score -FLT_MAX, weight 0) instead of being read from before the pool. 1511if ( page >= 0 ) r = ( page * n_kv_heads + kvh ) * page_size + ( cell % page_size ); 1512} 1513srow [ t ] = r ; 1514} 1515/* 1516DPCT1065: Consider replacing sycl::nd_item::barrier() with 1517sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1518performance if there is no access to global memory. 1519*/ 1520item_ct1 . barrier (); 1521// scores: each warp takes cells warp, warp+8, ...; each lane holds 8 of the 256 dimensions. 1522for (int c = warp ; c < CHUNK ; c += WARPS ) { 1523if ( c >= n_here || srow [ c ] < 0 ) { 1524if ( lane < G ) sp [ lane ][ c ] = - FLT_MAX ; 1525continue ; 1526} 1527float k8 [ 8 ]; 1528load8 < KV_MODE > ( p , false, srow [ c ], lane * 8 , k8 ); 1529#pragma unroll 1530for (int h = 0 ; h < G ; ++ h ) { 1531const sycl::float4 qa = 1532* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 8 ]); 1533const sycl::float4 qb = 1534* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 8 + 4 ]); 1535float s = k8 [ 0 ] * qa . x () + k8 [ 1 ] * qa . y () + k8 [ 2 ] * qa . z () + 1536k8 [ 3 ] * qa . w () + k8 [ 4 ] * qb . x () + k8 [ 5 ] * qb . y () + 1537k8 [ 6 ] * qb . z () + k8 [ 7 ] * qb . w (); 1538s = lane0_sum ( s ); 1539if ( lane == 0 ) sp [ h ][ c ] = s * scale ; 1540} 1541} 1542/* 1543DPCT1065: Consider replacing sycl::nd_item::barrier() with 1544sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1545performance if there is no access to global memory. 1546*/ 1547item_ct1 . barrier (); 1548// per-head chunk max and exp-sum: warp w handles heads w and w+8. 1549for (int h = warp ; h < G ; h += WARPS ) { 1550const float a = sp [ h ][ lane ], b = sp [ h ][ lane + 32 ]; 1551const float m = warp_max (sycl:: fmax ( a , b )); 1552const float ea = ( lane < n_here && srow [ lane ] >= 0 ) 1553? sycl::native:: exp ( a - m ) 1554: 0.0f ; 1555const float eb = ( lane + 32 < n_here && srow [ lane + 32 ] >= 0 ) 1556? sycl::native:: exp ( b - m ) 1557: 0.0f ; 1558sp [ h ][ lane ] = ea ; 1559sp [ h ][ lane + 32 ] = eb ; 1560const float l = lane0_sum ( ea + eb ); 1561if ( lane == 0 ) { part_m [ slot * G + h ] = m ; part_l [ slot * G + h ] = l ; } 1562} 1563/* 1564DPCT1065: Consider replacing sycl::nd_item::barrier() with 1565sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1566performance if there is no access to global memory. 1567*/ 1568item_ct1 . barrier (); 1569// values: thread t owns dimension t for all 12 heads. 1570float acc [ G ]; 1571#pragma unroll 1572for (int h = 0 ; h < G ; ++ h ) acc [ h ] = 0.0f ; 1573for (int c = 0 ; c < n_here ; ++ c ) { 1574if ( srow [ c ] < 0 ) continue ; // masked above, weight 0 1575float v ; 1576if constexpr ( KV_MODE == 0 ) { 1577v = sycl:: vec < sycl::half, 1 > ( 1578sycl:: bit_cast < sycl::half, unsigned short > ( 1579p . v_pool [ srow [ c ] * HD + t ])) 1580. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1581} else if constexpr ( KV_MODE == 1 ) { 1582const float sc = 1583sycl:: vec < sycl::half, 1 > ( 1584sycl:: bit_cast < sycl::half, unsigned short > ( 1585p . v_scale [ srow [ c ] * ( HD / KV_Q8_GROUP ) + 1586t / KV_Q8_GROUP ])) 1587. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1588v = (float) p . v_q [ srow [ c ] * HD + t ] * sc ; 1589} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 1590constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 1591const int b = t / QK4_0 ; 1592const int rem = t % QK4_0 ; 1593const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + srow [ c ] * bytes_per_head ) + b ; 1594const float d = 1595sycl:: vec < sycl::half, 1 > ( 1596sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 1597. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1598const int j = rem < 16 ? rem : ( rem - 16 ); 1599const uint8_t byte = blk -> qs [ j ]; 1600const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 1601v = (float) nibble * d ; 1602} 1603#pragma unroll 1604for (int h = 0 ; h < G ; ++ h ) acc [ h ] = sycl:: fma ( sp [ h ][ c ], v , acc [ h ]); 1605} 1606#pragma unroll 1607for (int h = 0 ; h < G ; ++ h ) part_acc [((size_t) slot * G + h ) * HD + t ] = acc [ h ]; 1608} 1609 1610 1611class QsaChunkDown; 1612} 1613namespace { 1614__dpct_inline__ void private_load8_vector_codes ( const QsaAttnPools & p ,bool value ,long long row ,int d0 ,float * out ){ 1615const int8_t * codes = ( value ? p . v_q : p . k_q ) + row * HD + d0 ; 1616const uint16_t sbits = ( value ? p . v_scale : p . k_scale )[ row * ( HD / KV_Q8_GROUP ) + d0 / KV_Q8_GROUP ]; 1617const float sc = sycl:: vec < sycl::half, 1 > (sycl:: bit_cast < sycl::half,unsigned short > ( sbits )). convert < float,sycl::rounding_mode::automatic > ()[ 0 ]; 1618const auto raw =* reinterpret_cast < const sycl::vec < int8_t, 8 >*> ( codes ); 1619const auto converted = raw . convert < float,sycl::rounding_mode::automatic > (); 1620#pragma unroll 1621for (int j = 0 ; j < 8 ; ++ j ) out [ j ] = converted [ j ] * sc ; 1622} 1623template < int KV_MODE > 1624/* 1625DPCT1110: The total declared local variable size in device function 1626chunk_down_kernel exceeds 128 bytes and may cause high register pressure. 1627Consult with your hardware vendor to find the total register size available and 1628adjust the code, or use smaller sub-group size to avoid high register pressure. 1629*/ 1630__dpct_inline__ void 1631chunk_vector_codes_kernel ( const float * __restrict__ q , QsaAttnPools p , 1632const int32_t * __restrict__ ids , 1633const int32_t * __restrict__ step , int n_kv_heads , 1634int page_size , float scale , float * __restrict__ part_acc , 1635float * __restrict__ part_m , float * __restrict__ part_l , 1636int n_chunks , int cap = 0 , long long scratch_stride = 0 ) { 1637// batched form: query blockIdx.z, with its own q row, selection, step and scratch 1638auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1639q += (size_t) item_ct1 . get_group ( 0 ) * (size_t)( n_kv_heads * G ) * HD ; 1640ids += (size_t) item_ct1 . get_group ( 0 ) * (size_t) cap ; 1641step += (size_t) item_ct1 . get_group ( 0 ) * kStepCount ; 1642part_acc += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 1643part_m += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 1644part_l += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 1645auto & sq = 1646* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ HD ] > ( 1647sycl::ext::oneapi::this_work_item:: get_work_group < 16483 > ()); // 12 KB: this KV head's query heads 1649auto & sp = 1650* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ CHUNK ] > ( 1651sycl::ext::oneapi::this_work_item:: get_work_group < 16523 > ()); // scores, then probabilities 1653auto & srow = 1654* sycl::ext::oneapi:: group_local_memory_for_overwrite < long long[ CHUNK ] > ( 1655sycl::ext::oneapi::this_work_item:: get_work_group < 16563 > ()); // pool row of each cell (page, kv head, slot) 1657/* 1658DPCT1098: The '*' expression is used instead of the __ldg call. These 1659two expressions do not provide the exact same functionality. Check the 1660generated code for potential precision and/or performance issues. 1661*/ 1662const int n_ids = * ( step + kStepWidth ); 1663const int chunk = item_ct1 . get_group ( 2 ), kvh = item_ct1 . get_group ( 1 ); 1664const int t = item_ct1 . get_local_id ( 2 ), lane = t & 31 , warp = t >> 5 ; 1665const int c0 = chunk * CHUNK ; 1666const int n_here = sycl:: min ( CHUNK , n_ids - c0 ); 1667const int slot = kvh * n_chunks + chunk ; 1668if ( n_here <= 0 ) { 1669if ( t < G ) { part_m [ slot * G + t ] = - FLT_MAX ; part_l [ slot * G + t ] = 0.0f ; } 1670return ; 1671} 1672#pragma unroll 1673for (int i = t ; i < G * HD ; i += THREADS ) 1674sq [ i / HD ][ i % HD ] = q [(size_t)( kvh * G ) * HD + i ]; 1675if ( t < CHUNK ) { 1676long long r = -1 ; 1677if ( t < n_here ) { 1678const int cell = ids [ c0 + t ]; 1679const long long page = (long long) p . page_table [ cell / page_size ]; 1680// a block the KV streaming could not make resident keeps page -1 (ctl[3]); its cells are masked 1681// (score -FLT_MAX, weight 0) instead of being read from before the pool. 1682if ( page >= 0 ) r = ( page * n_kv_heads + kvh ) * page_size + ( cell % page_size ); 1683} 1684srow [ t ] = r ; 1685} 1686/* 1687DPCT1065: Consider replacing sycl::nd_item::barrier() with 1688sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1689performance if there is no access to global memory. 1690*/ 1691item_ct1 . barrier (); 1692// scores: each warp takes cells warp, warp+8, ...; each lane holds 8 of the 256 dimensions. 1693for (int c = warp ; c < CHUNK ; c += WARPS ) { 1694if ( c >= n_here || srow [ c ] < 0 ) { 1695if ( lane < G ) sp [ lane ][ c ] = - FLT_MAX ; 1696continue ; 1697} 1698float k8 [ 8 ]; 1699private_load8_vector_codes ( p , false, srow [ c ], lane * 8 , k8 ); 1700#pragma unroll 1701for (int h = 0 ; h < G ; ++ h ) { 1702const sycl::float4 qa = 1703* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 8 ]); 1704const sycl::float4 qb = 1705* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 8 + 4 ]); 1706float s = k8 [ 0 ] * qa . x () + k8 [ 1 ] * qa . y () + k8 [ 2 ] * qa . z () + 1707k8 [ 3 ] * qa . w () + k8 [ 4 ] * qb . x () + k8 [ 5 ] * qb . y () + 1708k8 [ 6 ] * qb . z () + k8 [ 7 ] * qb . w (); 1709s = lane0_sum ( s ); 1710if ( lane == 0 ) sp [ h ][ c ] = s * scale ; 1711} 1712} 1713/* 1714DPCT1065: Consider replacing sycl::nd_item::barrier() with 1715sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1716performance if there is no access to global memory. 1717*/ 1718item_ct1 . barrier (); 1719// per-head chunk max and exp-sum: warp w handles heads w and w+8. 1720for (int h = warp ; h < G ; h += WARPS ) { 1721const float a = sp [ h ][ lane ], b = sp [ h ][ lane + 32 ]; 1722const float m = warp_max (sycl:: fmax ( a , b )); 1723const float ea = ( lane < n_here && srow [ lane ] >= 0 ) 1724? sycl::native:: exp ( a - m ) 1725: 0.0f ; 1726const float eb = ( lane + 32 < n_here && srow [ lane + 32 ] >= 0 ) 1727? sycl::native:: exp ( b - m ) 1728: 0.0f ; 1729sp [ h ][ lane ] = ea ; 1730sp [ h ][ lane + 32 ] = eb ; 1731const float l = lane0_sum ( ea + eb ); 1732if ( lane == 0 ) { part_m [ slot * G + h ] = m ; part_l [ slot * G + h ] = l ; } 1733} 1734/* 1735DPCT1065: Consider replacing sycl::nd_item::barrier() with 1736sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1737performance if there is no access to global memory. 1738*/ 1739item_ct1 . barrier (); 1740// values: thread t owns dimension t for all 12 heads. 1741float acc [ G ]; 1742#pragma unroll 1743for (int h = 0 ; h < G ; ++ h ) acc [ h ] = 0.0f ; 1744for (int c0 = 0 ; c0 < n_here ; c0 += 8 ) { 1745long long rows [ 8 ]; bool full = c0 + 8 <= n_here ; 1746#pragma unroll 1747for (int j = 0 ; j < 8 ; ++ j ) { 1748rows [ j ] = c0 + j < n_here ? srow [ c0 + j ] : -1 ; 1749full = full && rows [ j ] >= 0 ; 1750} 1751if ( full ) { 1752float values [ 8 ]; 1753#pragma unroll 1754for (int j = 0 ; j < 8 ; ++ j ) { 1755const long long row = rows [ j ]; 1756float v ; 1757if constexpr ( KV_MODE == 0 ) { 1758v = sycl:: vec < sycl::half, 1 > ( 1759sycl:: bit_cast < sycl::half, unsigned short > ( 1760p . v_pool [ row * HD + t ])) 1761. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1762} else if constexpr ( KV_MODE == 1 ) { 1763const float sc = 1764sycl:: vec < sycl::half, 1 > ( 1765sycl:: bit_cast < sycl::half, unsigned short > ( 1766p . v_scale [ row * ( HD / KV_Q8_GROUP ) + 1767t / KV_Q8_GROUP ])) 1768. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1769v = (float) p . v_q [ row * HD + t ] * sc ; 1770} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 1771constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 1772const int b = t / QK4_0 ; 1773const int rem = t % QK4_0 ; 1774const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + b ; 1775const float d = 1776sycl:: vec < sycl::half, 1 > ( 1777sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 1778. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1779const int j = rem < 16 ? rem : ( rem - 16 ); 1780const uint8_t byte = blk -> qs [ j ]; 1781const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 1782v = (float) nibble * d ; 1783} 1784values [ j ] = v ; 1785} 1786#pragma unroll 1787for (int h = 0 ; h < G ; ++ h ) { 1788const sycl::float4 p0 = * reinterpret_cast < const sycl::float4 *> ( & sp [ h ][ c0 ]); 1789const sycl::float4 p1 = * reinterpret_cast < const sycl::float4 *> ( & sp [ h ][ c0 + 4 ]); 1790acc [ h ] = sycl:: fma ( p0 [ 0 ], values [ 0 ], acc [ h ]); 1791acc [ h ] = sycl:: fma ( p0 [ 1 ], values [ 1 ], acc [ h ]); 1792acc [ h ] = sycl:: fma ( p0 [ 2 ], values [ 2 ], acc [ h ]); 1793acc [ h ] = sycl:: fma ( p0 [ 3 ], values [ 3 ], acc [ h ]); 1794acc [ h ] = sycl:: fma ( p1 [ 0 ], values [ 4 ], acc [ h ]); 1795acc [ h ] = sycl:: fma ( p1 [ 1 ], values [ 5 ], acc [ h ]); 1796acc [ h ] = sycl:: fma ( p1 [ 2 ], values [ 6 ], acc [ h ]); 1797acc [ h ] = sycl:: fma ( p1 [ 3 ], values [ 7 ], acc [ h ]); 1798} 1799 1800} else { 1801#pragma unroll 1802for (int j = 0 ; j < 8 ; ++ j ) { 1803const long long row = rows [ j ]; 1804if ( row < 0 ) continue ; 1805float v ; 1806if constexpr ( KV_MODE == 0 ) { 1807v = sycl:: vec < sycl::half, 1 > ( 1808sycl:: bit_cast < sycl::half, unsigned short > ( 1809p . v_pool [ row * HD + t ])) 1810. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1811} else if constexpr ( KV_MODE == 1 ) { 1812const float sc = 1813sycl:: vec < sycl::half, 1 > ( 1814sycl:: bit_cast < sycl::half, unsigned short > ( 1815p . v_scale [ row * ( HD / KV_Q8_GROUP ) + 1816t / KV_Q8_GROUP ])) 1817. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1818v = (float) p . v_q [ row * HD + t ] * sc ; 1819} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 1820constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 1821const int b = t / QK4_0 ; 1822const int rem = t % QK4_0 ; 1823const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + b ; 1824const float d = 1825sycl:: vec < sycl::half, 1 > ( 1826sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 1827. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1828const int j = rem < 16 ? rem : ( rem - 16 ); 1829const uint8_t byte = blk -> qs [ j ]; 1830const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 1831v = (float) nibble * d ; 1832} 1833#pragma unroll 1834for (int h = 0 ; h < G ; ++ h ) acc [ h ] = sycl:: fma ( sp [ h ][ c0 + j ], v , acc [ h ]); 1835} 1836} 1837} 1838 1839#pragma unroll 1840for (int h = 0 ; h < G ; ++ h ) part_acc [((size_t) slot * G + h ) * HD + t ] = acc [ h ]; 1841} 1842 1843template < int KV_MODE > 1844/* 1845DPCT1110: The total declared local variable size in device function 1846chunk_down_kernel exceeds 128 bytes and may cause high register pressure. 1847Consult with your hardware vendor to find the total register size available and 1848adjust the code, or use smaller sub-group size to avoid high register pressure. 1849*/ 1850__dpct_inline__ void 1851b60_qsa_float4_kernel ( const float * __restrict__ q , QsaAttnPools p , 1852const int32_t * __restrict__ ids , 1853const int32_t * __restrict__ step , int n_kv_heads , 1854int page_size , float scale , float * __restrict__ part_acc , 1855float * __restrict__ part_m , float * __restrict__ part_l , 1856int n_chunks , int cap = 0 , long long scratch_stride = 0 ) { 1857// batched form: query blockIdx.z, with its own q row, selection, step and scratch 1858auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1859q += (size_t) item_ct1 . get_group ( 0 ) * (size_t)( n_kv_heads * G ) * HD ; 1860ids += (size_t) item_ct1 . get_group ( 0 ) * (size_t) cap ; 1861step += (size_t) item_ct1 . get_group ( 0 ) * kStepCount ; 1862part_acc += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 1863part_m += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 1864part_l += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 1865auto & sq = 1866* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ HD ] > ( 1867sycl::ext::oneapi::this_work_item:: get_work_group < 18683 > ()); // 12 KB: this KV head's query heads 1869auto & sp = 1870* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ CHUNK ] > ( 1871sycl::ext::oneapi::this_work_item:: get_work_group < 18723 > ()); // scores, then probabilities 1873auto & srow = 1874* sycl::ext::oneapi:: group_local_memory_for_overwrite < long long[ CHUNK ] > ( 1875sycl::ext::oneapi::this_work_item:: get_work_group < 18763 > ()); // pool row of each cell (page, kv head, slot) 1877/* 1878DPCT1098: The '*' expression is used instead of the __ldg call. These 1879two expressions do not provide the exact same functionality. Check the 1880generated code for potential precision and/or performance issues. 1881*/ 1882const int n_ids = * ( step + kStepWidth ); 1883const int chunk = item_ct1 . get_group ( 2 ), kvh = item_ct1 . get_group ( 1 ); 1884const int t = item_ct1 . get_local_id ( 2 ), lane = t & 31 , warp = t >> 5 ; 1885const int c0 = chunk * CHUNK ; 1886const int n_here = sycl:: min ( CHUNK , n_ids - c0 ); 1887const int slot = kvh * n_chunks + chunk ; 1888if ( n_here <= 0 ) { 1889if ( t < G ) { part_m [ slot * G + t ] = - FLT_MAX ; part_l [ slot * G + t ] = 0.0f ; } 1890return ; 1891} 1892#pragma unroll 1893for (int i = t ; i < G * HD ; i += THREADS ) 1894sq [ i / HD ][ i % HD ] = q [(size_t)( kvh * G + i / HD ) * HD + 1895(( i % HD ) % 128 / 4 ) * 8 + (( i % HD ) / 128 ) * 4 + ( i % 4 )]; 1896if ( t < CHUNK ) { 1897long long r = -1 ; 1898if ( t < n_here ) { 1899const int cell = ids [ c0 + t ]; 1900const long long page = (long long) p . page_table [ cell / page_size ]; 1901// a block the KV streaming could not make resident keeps page -1 (ctl[3]); its cells are masked 1902// (score -FLT_MAX, weight 0) instead of being read from before the pool. 1903if ( page >= 0 ) r = ( page * n_kv_heads + kvh ) * page_size + ( cell % page_size ); 1904} 1905srow [ t ] = r ; 1906} 1907/* 1908DPCT1065: Consider replacing sycl::nd_item::barrier() with 1909sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1910performance if there is no access to global memory. 1911*/ 1912item_ct1 . barrier (); 1913// scores: each warp takes cells warp, warp+8, ...; each lane holds 8 of the 256 dimensions. 1914for (int c = warp ; c < CHUNK ; c += WARPS ) { 1915if ( c >= n_here || srow [ c ] < 0 ) { 1916if ( lane < G ) sp [ lane ][ c ] = - FLT_MAX ; 1917continue ; 1918} 1919float k8 [ 8 ]; 1920private_load8_vector_codes ( p , false, srow [ c ], lane * 8 , k8 ); 1921#pragma unroll 1922for (int h = 0 ; h < G ; ++ h ) { 1923const sycl::float4 qa = 1924* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 4 ]); 1925const sycl::float4 qb = 1926* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ 128 + lane * 4 ]); 1927float s = k8 [ 0 ] * qa . x () + k8 [ 1 ] * qa . y () + k8 [ 2 ] * qa . z () + 1928k8 [ 3 ] * qa . w () + k8 [ 4 ] * qb . x () + k8 [ 5 ] * qb . y () + 1929k8 [ 6 ] * qb . z () + k8 [ 7 ] * qb . w (); 1930s = lane0_sum ( s ); 1931if ( lane == 0 ) sp [ h ][ c ] = s * scale ; 1932} 1933} 1934/* 1935DPCT1065: Consider replacing sycl::nd_item::barrier() with 1936sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1937performance if there is no access to global memory. 1938*/ 1939item_ct1 . barrier (); 1940// per-head chunk max and exp-sum: warp w handles heads w and w+8. 1941for (int h = warp ; h < G ; h += WARPS ) { 1942const float a = sp [ h ][ lane ], b = sp [ h ][ lane + 32 ]; 1943const float m = warp_max (sycl:: fmax ( a , b )); 1944const float ea = ( lane < n_here && srow [ lane ] >= 0 ) 1945? sycl::native:: exp ( a - m ) 1946: 0.0f ; 1947const float eb = ( lane + 32 < n_here && srow [ lane + 32 ] >= 0 ) 1948? sycl::native:: exp ( b - m ) 1949: 0.0f ; 1950sp [ h ][ lane ] = ea ; 1951sp [ h ][ lane + 32 ] = eb ; 1952const float l = lane0_sum ( ea + eb ); 1953if ( lane == 0 ) { part_m [ slot * G + h ] = m ; part_l [ slot * G + h ] = l ; } 1954} 1955/* 1956DPCT1065: Consider replacing sycl::nd_item::barrier() with 1957sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1958performance if there is no access to global memory. 1959*/ 1960item_ct1 . barrier (); 1961// values: thread t owns dimension t for all 12 heads. 1962float acc [ G ]; 1963#pragma unroll 1964for (int h = 0 ; h < G ; ++ h ) acc [ h ] = 0.0f ; 1965for (int c0 = 0 ; c0 < n_here ; c0 += 8 ) { 1966long long rows [ 8 ]; bool full = c0 + 8 <= n_here ; 1967#pragma unroll 1968for (int j = 0 ; j < 8 ; ++ j ) { 1969rows [ j ] = c0 + j < n_here ? srow [ c0 + j ] : -1 ; 1970full = full && rows [ j ] >= 0 ; 1971} 1972if ( full ) { 1973float values [ 8 ]; 1974#pragma unroll 1975for (int j = 0 ; j < 8 ; ++ j ) { 1976const long long row = rows [ j ]; 1977float v ; 1978if constexpr ( KV_MODE == 0 ) { 1979v = sycl:: vec < sycl::half, 1 > ( 1980sycl:: bit_cast < sycl::half, unsigned short > ( 1981p . v_pool [ row * HD + t ])) 1982. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1983} else if constexpr ( KV_MODE == 1 ) { 1984const float sc = 1985sycl:: vec < sycl::half, 1 > ( 1986sycl:: bit_cast < sycl::half, unsigned short > ( 1987p . v_scale [ row * ( HD / KV_Q8_GROUP ) + 1988t / KV_Q8_GROUP ])) 1989. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 1990v = (float) p . v_q [ row * HD + t ] * sc ; 1991} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 1992constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 1993const int b = t / QK4_0 ; 1994const int rem = t % QK4_0 ; 1995const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + b ; 1996const float d = 1997sycl:: vec < sycl::half, 1 > ( 1998sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 1999. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2000const int j = rem < 16 ? rem : ( rem - 16 ); 2001const uint8_t byte = blk -> qs [ j ]; 2002const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 2003v = (float) nibble * d ; 2004} 2005values [ j ] = v ; 2006} 2007#pragma unroll 2008for (int h = 0 ; h < G ; ++ h ) { 2009const sycl::float4 p0 = * reinterpret_cast < const sycl::float4 *> ( & sp [ h ][ c0 ]); 2010const sycl::float4 p1 = * reinterpret_cast < const sycl::float4 *> ( & sp [ h ][ c0 + 4 ]); 2011acc [ h ] = sycl:: fma ( p0 [ 0 ], values [ 0 ], acc [ h ]); 2012acc [ h ] = sycl:: fma ( p0 [ 1 ], values [ 1 ], acc [ h ]); 2013acc [ h ] = sycl:: fma ( p0 [ 2 ], values [ 2 ], acc [ h ]); 2014acc [ h ] = sycl:: fma ( p0 [ 3 ], values [ 3 ], acc [ h ]); 2015acc [ h ] = sycl:: fma ( p1 [ 0 ], values [ 4 ], acc [ h ]); 2016acc [ h ] = sycl:: fma ( p1 [ 1 ], values [ 5 ], acc [ h ]); 2017acc [ h ] = sycl:: fma ( p1 [ 2 ], values [ 6 ], acc [ h ]); 2018acc [ h ] = sycl:: fma ( p1 [ 3 ], values [ 7 ], acc [ h ]); 2019} 2020 2021} else { 2022#pragma unroll 2023for (int j = 0 ; j < 8 ; ++ j ) { 2024const long long row = rows [ j ]; 2025if ( row < 0 ) continue ; 2026float v ; 2027if constexpr ( KV_MODE == 0 ) { 2028v = sycl:: vec < sycl::half, 1 > ( 2029sycl:: bit_cast < sycl::half, unsigned short > ( 2030p . v_pool [ row * HD + t ])) 2031. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2032} else if constexpr ( KV_MODE == 1 ) { 2033const float sc = 2034sycl:: vec < sycl::half, 1 > ( 2035sycl:: bit_cast < sycl::half, unsigned short > ( 2036p . v_scale [ row * ( HD / KV_Q8_GROUP ) + 2037t / KV_Q8_GROUP ])) 2038. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2039v = (float) p . v_q [ row * HD + t ] * sc ; 2040} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 2041constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 2042const int b = t / QK4_0 ; 2043const int rem = t % QK4_0 ; 2044const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + b ; 2045const float d = 2046sycl:: vec < sycl::half, 1 > ( 2047sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 2048. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2049const int j = rem < 16 ? rem : ( rem - 16 ); 2050const uint8_t byte = blk -> qs [ j ]; 2051const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 2052v = (float) nibble * d ; 2053} 2054#pragma unroll 2055for (int h = 0 ; h < G ; ++ h ) acc [ h ] = sycl:: fma ( sp [ h ][ c0 + j ], v , acc [ h ]); 2056} 2057} 2058} 2059 2060#pragma unroll 2061for (int h = 0 ; h < G ; ++ h ) part_acc [((size_t) slot * G + h ) * HD + t ] = acc [ h ]; 2062} 2063 2064template < int KV_MODE > 2065/* 2066DPCT1110: The total declared local variable size in device function 2067chunk_down_kernel exceeds 128 bytes and may cause high register pressure. 2068Consult with your hardware vendor to find the total register size available and 2069adjust the code, or use smaller sub-group size to avoid high register pressure. 2070*/ 2071__dpct_inline__ void 2072b60_qsa_head6_kernel ( const float * __restrict__ q , QsaAttnPools p , 2073const int32_t * __restrict__ ids , 2074const int32_t * __restrict__ step , int n_kv_heads , 2075int page_size , float scale , float * __restrict__ part_acc , 2076float * __restrict__ part_m , float * __restrict__ part_l , 2077int n_chunks , int cap = 0 , long long scratch_stride = 0 ) { 2078// batched form: query blockIdx.z, with its own q row, selection, step and scratch 2079auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 2080constexpr int H = 6 ; const int headbase = int( item_ct1 . get_group ( 2 )% 2 ) * H ; 2081q += (size_t) item_ct1 . get_group ( 0 ) * (size_t)( n_kv_heads * G ) * HD ; 2082ids += (size_t) item_ct1 . get_group ( 0 ) * (size_t) cap ; 2083step += (size_t) item_ct1 . get_group ( 0 ) * kStepCount ; 2084part_acc += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 2085part_m += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 2086part_l += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 2087auto & sq = 2088* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ H ][ HD ] > ( 2089sycl::ext::oneapi::this_work_item:: get_work_group < 20903 > ()); // 12 KB: this KV head's query heads 2091auto & sp = 2092* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ H ][ CHUNK ] > ( 2093sycl::ext::oneapi::this_work_item:: get_work_group < 20943 > ()); // scores, then probabilities 2095auto & srow = 2096* sycl::ext::oneapi:: group_local_memory_for_overwrite < long long[ CHUNK ] > ( 2097sycl::ext::oneapi::this_work_item:: get_work_group < 20983 > ()); // pool row of each cell (page, kv head, slot) 2099/* 2100DPCT1098: The '*' expression is used instead of the __ldg call. These 2101two expressions do not provide the exact same functionality. Check the 2102generated code for potential precision and/or performance issues. 2103*/ 2104const int n_ids = * ( step + kStepWidth ); 2105const int chunk = item_ct1 . get_group ( 2 )/ 2 , kvh = item_ct1 . get_group ( 1 ); 2106const int t = item_ct1 . get_local_id ( 2 ), lane = t & 31 , warp = t >> 5 ; 2107const int c0 = chunk * CHUNK ; 2108const int n_here = sycl:: min ( CHUNK , n_ids - c0 ); 2109const int slot = kvh * n_chunks + chunk ; 2110if ( n_here <= 0 ) { 2111if ( t < H ) { part_m [ slot * G + headbase + t ] = - FLT_MAX ; part_l [ slot * G + headbase + t ] = 0.0f ; } 2112return ; 2113} 2114#pragma unroll 2115for (int i = t ; i < H * HD ; i += THREADS ) 2116sq [ i / HD ][ i % HD ] = q [(size_t)( kvh * G + headbase + i / HD ) * HD + 2117(( i % HD ) % 128 / 4 ) * 8 + (( i % HD ) / 128 ) * 4 + ( i % 4 )]; 2118if ( t < CHUNK ) { 2119long long r = -1 ; 2120if ( t < n_here ) { 2121const int cell = ids [ c0 + t ]; 2122const long long page = (long long) p . page_table [ cell / page_size ]; 2123// a block the KV streaming could not make resident keeps page -1 (ctl[3]); its cells are masked 2124// (score -FLT_MAX, weight 0) instead of being read from before the pool. 2125if ( page >= 0 ) r = ( page * n_kv_heads + kvh ) * page_size + ( cell % page_size ); 2126} 2127srow [ t ] = r ; 2128} 2129/* 2130DPCT1065: Consider replacing sycl::nd_item::barrier() with 2131sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 2132performance if there is no access to global memory. 2133*/ 2134item_ct1 . barrier (); 2135// scores: each warp takes cells warp, warp+8, ...; each lane holds 8 of the 256 dimensions. 2136for (int c = warp ; c < CHUNK ; c += WARPS ) { 2137if ( c >= n_here || srow [ c ] < 0 ) { 2138if ( lane < H ) sp [ lane ][ c ] = - FLT_MAX ; 2139continue ; 2140} 2141float k8 [ 8 ]; 2142private_load8_vector_codes ( p , false, srow [ c ], lane * 8 , k8 ); 2143#pragma unroll 2144for (int h = 0 ; h < H ; ++ h ) { 2145const sycl::float4 qa = 2146* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 4 ]); 2147const sycl::float4 qb = 2148* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ 128 + lane * 4 ]); 2149float s = k8 [ 0 ] * qa . x () + k8 [ 1 ] * qa . y () + k8 [ 2 ] * qa . z () + 2150k8 [ 3 ] * qa . w () + k8 [ 4 ] * qb . x () + k8 [ 5 ] * qb . y () + 2151k8 [ 6 ] * qb . z () + k8 [ 7 ] * qb . w (); 2152s = lane0_sum ( s ); 2153if ( lane == 0 ) sp [ h ][ c ] = s * scale ; 2154} 2155} 2156/* 2157DPCT1065: Consider replacing sycl::nd_item::barrier() with 2158sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 2159performance if there is no access to global memory. 2160*/ 2161item_ct1 . barrier (); 2162// per-head chunk max and exp-sum: warp w handles heads w and w+8. 2163for (int h = warp ; h < H ; h += WARPS ) { 2164const float a = sp [ h ][ lane ], b = sp [ h ][ lane + 32 ]; 2165const float m = warp_max (sycl:: fmax ( a , b )); 2166const float ea = ( lane < n_here && srow [ lane ] >= 0 ) 2167? sycl::native:: exp ( a - m ) 2168: 0.0f ; 2169const float eb = ( lane + 32 < n_here && srow [ lane + 32 ] >= 0 ) 2170? sycl::native:: exp ( b - m ) 2171: 0.0f ; 2172sp [ h ][ lane ] = ea ; 2173sp [ h ][ lane + 32 ] = eb ; 2174const float l = lane0_sum ( ea + eb ); 2175if ( lane == 0 ) { part_m [ slot * G + headbase + h ] = m ; part_l [ slot * G + headbase + h ] = l ; } 2176} 2177/* 2178DPCT1065: Consider replacing sycl::nd_item::barrier() with 2179sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 2180performance if there is no access to global memory. 2181*/ 2182item_ct1 . barrier (); 2183// values: thread t owns dimension t for all 12 heads. 2184float acc [ H ]; 2185#pragma unroll 2186for (int h = 0 ; h < H ; ++ h ) acc [ h ] = 0.0f ; 2187for (int c0 = 0 ; c0 < n_here ; c0 += 8 ) { 2188long long rows [ 8 ]; bool full = c0 + 8 <= n_here ; 2189#pragma unroll 2190for (int j = 0 ; j < 8 ; ++ j ) { 2191rows [ j ] = c0 + j < n_here ? srow [ c0 + j ] : -1 ; 2192full = full && rows [ j ] >= 0 ; 2193} 2194if ( full ) { 2195float values [ 8 ]; 2196#pragma unroll 2197for (int j = 0 ; j < 8 ; ++ j ) { 2198const long long row = rows [ j ]; 2199float v ; 2200if constexpr ( KV_MODE == 0 ) { 2201v = sycl:: vec < sycl::half, 1 > ( 2202sycl:: bit_cast < sycl::half, unsigned short > ( 2203p . v_pool [ row * HD + t ])) 2204. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2205} else if constexpr ( KV_MODE == 1 ) { 2206const float sc = 2207sycl:: vec < sycl::half, 1 > ( 2208sycl:: bit_cast < sycl::half, unsigned short > ( 2209p . v_scale [ row * ( HD / KV_Q8_GROUP ) + 2210t / KV_Q8_GROUP ])) 2211. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2212v = (float) p . v_q [ row * HD + t ] * sc ; 2213} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 2214constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 2215const int b = t / QK4_0 ; 2216const int rem = t % QK4_0 ; 2217const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + b ; 2218const float d = 2219sycl:: vec < sycl::half, 1 > ( 2220sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 2221. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2222const int j = rem < 16 ? rem : ( rem - 16 ); 2223const uint8_t byte = blk -> qs [ j ]; 2224const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 2225v = (float) nibble * d ; 2226} 2227values [ j ] = v ; 2228} 2229#pragma unroll 2230for (int h = 0 ; h < H ; ++ h ) { 2231const sycl::float4 p0 = * reinterpret_cast < const sycl::float4 *> ( & sp [ h ][ c0 ]); 2232const sycl::float4 p1 = * reinterpret_cast < const sycl::float4 *> ( & sp [ h ][ c0 + 4 ]); 2233acc [ h ] = sycl:: fma ( p0 [ 0 ], values [ 0 ], acc [ h ]); 2234acc [ h ] = sycl:: fma ( p0 [ 1 ], values [ 1 ], acc [ h ]); 2235acc [ h ] = sycl:: fma ( p0 [ 2 ], values [ 2 ], acc [ h ]); 2236acc [ h ] = sycl:: fma ( p0 [ 3 ], values [ 3 ], acc [ h ]); 2237acc [ h ] = sycl:: fma ( p1 [ 0 ], values [ 4 ], acc [ h ]); 2238acc [ h ] = sycl:: fma ( p1 [ 1 ], values [ 5 ], acc [ h ]); 2239acc [ h ] = sycl:: fma ( p1 [ 2 ], values [ 6 ], acc [ h ]); 2240acc [ h ] = sycl:: fma ( p1 [ 3 ], values [ 7 ], acc [ h ]); 2241} 2242 2243} else { 2244#pragma unroll 2245for (int j = 0 ; j < 8 ; ++ j ) { 2246const long long row = rows [ j ]; 2247if ( row < 0 ) continue ; 2248float v ; 2249if constexpr ( KV_MODE == 0 ) { 2250v = sycl:: vec < sycl::half, 1 > ( 2251sycl:: bit_cast < sycl::half, unsigned short > ( 2252p . v_pool [ row * HD + t ])) 2253. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2254} else if constexpr ( KV_MODE == 1 ) { 2255const float sc = 2256sycl:: vec < sycl::half, 1 > ( 2257sycl:: bit_cast < sycl::half, unsigned short > ( 2258p . v_scale [ row * ( HD / KV_Q8_GROUP ) + 2259t / KV_Q8_GROUP ])) 2260. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2261v = (float) p . v_q [ row * HD + t ] * sc ; 2262} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 2263constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 2264const int b = t / QK4_0 ; 2265const int rem = t % QK4_0 ; 2266const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + b ; 2267const float d = 2268sycl:: vec < sycl::half, 1 > ( 2269sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 2270. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2271const int j = rem < 16 ? rem : ( rem - 16 ); 2272const uint8_t byte = blk -> qs [ j ]; 2273const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 2274v = (float) nibble * d ; 2275} 2276#pragma unroll 2277for (int h = 0 ; h < H ; ++ h ) acc [ h ] = sycl:: fma ( sp [ h ][ c0 + j ], v , acc [ h ]); 2278} 2279} 2280} 2281 2282#pragma unroll 2283for (int h = 0 ; h < H ; ++ h ) part_acc [((size_t) slot * G + headbase + h ) * HD + t ] = acc [ h ]; 2284} 2285 2286class B60QsaHead6; 2287template < int KV_MODE > 2288/* 2289DPCT1110: The total declared local variable size in device function 2290chunk_down_kernel exceeds 128 bytes and may cause high register pressure. 2291Consult with your hardware vendor to find the total register size available and 2292adjust the code, or use smaller sub-group size to avoid high register pressure. 2293*/ 2294__dpct_inline__ void 2295b60_qsa_local_fence_kernel ( const float * __restrict__ q , QsaAttnPools p , 2296const int32_t * __restrict__ ids , 2297const int32_t * __restrict__ step , int n_kv_heads , 2298int page_size , float scale , float * __restrict__ part_acc , 2299float * __restrict__ part_m , float * __restrict__ part_l , 2300int n_chunks , int cap = 0 , long long scratch_stride = 0 ) { 2301// batched form: query blockIdx.z, with its own q row, selection, step and scratch 2302auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 2303q += (size_t) item_ct1 . get_group ( 0 ) * (size_t)( n_kv_heads * G ) * HD ; 2304ids += (size_t) item_ct1 . get_group ( 0 ) * (size_t) cap ; 2305step += (size_t) item_ct1 . get_group ( 0 ) * kStepCount ; 2306part_acc += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 2307part_m += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 2308part_l += (size_t) item_ct1 . get_group ( 0 ) * (size_t) scratch_stride ; 2309auto & sq = 2310* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ HD ] > ( 2311sycl::ext::oneapi::this_work_item:: get_work_group < 23123 > ()); // 12 KB: this KV head's query heads 2313auto & sp = 2314* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ G ][ CHUNK ] > ( 2315sycl::ext::oneapi::this_work_item:: get_work_group < 23163 > ()); // scores, then probabilities 2317auto & srow = 2318* sycl::ext::oneapi:: group_local_memory_for_overwrite < long long[ CHUNK ] > ( 2319sycl::ext::oneapi::this_work_item:: get_work_group < 23203 > ()); // pool row of each cell (page, kv head, slot) 2321/* 2322DPCT1098: The '*' expression is used instead of the __ldg call. These 2323two expressions do not provide the exact same functionality. Check the 2324generated code for potential precision and/or performance issues. 2325*/ 2326const int n_ids = * ( step + kStepWidth ); 2327const int chunk = item_ct1 . get_group ( 2 ), kvh = item_ct1 . get_group ( 1 ); 2328const int t = item_ct1 . get_local_id ( 2 ), lane = t & 31 , warp = t >> 5 ; 2329const int c0 = chunk * CHUNK ; 2330const int n_here = sycl:: min ( CHUNK , n_ids - c0 ); 2331const int slot = kvh * n_chunks + chunk ; 2332if ( n_here <= 0 ) { 2333if ( t < G ) { part_m [ slot * G + t ] = - FLT_MAX ; part_l [ slot * G + t ] = 0.0f ; } 2334return ; 2335} 2336#pragma unroll 2337for (int i = t ; i < G * HD ; i += THREADS ) 2338sq [ i / HD ][ i % HD ] = q [(size_t)( kvh * G + i / HD ) * HD + 2339(( i % HD ) % 128 / 4 ) * 8 + (( i % HD ) / 128 ) * 4 + ( i % 4 )]; 2340if ( t < CHUNK ) { 2341long long r = -1 ; 2342if ( t < n_here ) { 2343const int cell = ids [ c0 + t ]; 2344const long long page = (long long) p . page_table [ cell / page_size ]; 2345// a block the KV streaming could not make resident keeps page -1 (ctl[3]); its cells are masked 2346// (score -FLT_MAX, weight 0) instead of being read from before the pool. 2347if ( page >= 0 ) r = ( page * n_kv_heads + kvh ) * page_size + ( cell % page_size ); 2348} 2349srow [ t ] = r ; 2350} 2351/* 2352DPCT1065: Consider replacing sycl::nd_item::barrier() with 2353sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 2354performance if there is no access to global memory. 2355*/ 2356item_ct1 . barrier (sycl::access::fence_space:: local_space ); 2357// scores: each warp takes cells warp, warp+8, ...; each lane holds 8 of the 256 dimensions. 2358for (int c = warp ; c < CHUNK ; c += WARPS ) { 2359if ( c >= n_here || srow [ c ] < 0 ) { 2360if ( lane < G ) sp [ lane ][ c ] = - FLT_MAX ; 2361continue ; 2362} 2363float k8 [ 8 ]; 2364private_load8_vector_codes ( p , false, srow [ c ], lane * 8 , k8 ); 2365#pragma unroll 2366for (int h = 0 ; h < G ; ++ h ) { 2367const sycl::float4 qa = 2368* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ lane * 4 ]); 2369const sycl::float4 qb = 2370* reinterpret_cast < const sycl::float4 *> ( & sq [ h ][ 128 + lane * 4 ]); 2371float s = k8 [ 0 ] * qa . x () + k8 [ 1 ] * qa . y () + k8 [ 2 ] * qa . z () + 2372k8 [ 3 ] * qa . w () + k8 [ 4 ] * qb . x () + k8 [ 5 ] * qb . y () + 2373k8 [ 6 ] * qb . z () + k8 [ 7 ] * qb . w (); 2374s = lane0_sum ( s ); 2375if ( lane == 0 ) sp [ h ][ c ] = s * scale ; 2376} 2377} 2378/* 2379DPCT1065: Consider replacing sycl::nd_item::barrier() with 2380sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 2381performance if there is no access to global memory. 2382*/ 2383item_ct1 . barrier (sycl::access::fence_space:: local_space ); 2384// per-head chunk max and exp-sum: warp w handles heads w and w+8. 2385for (int h = warp ; h < G ; h += WARPS ) { 2386const float a = sp [ h ][ lane ], b = sp [ h ][ lane + 32 ]; 2387const float m = warp_max (sycl:: fmax ( a , b )); 2388const float ea = ( lane < n_here && srow [ lane ] >= 0 ) 2389? sycl::native:: exp ( a - m ) 2390: 0.0f ; 2391const float eb = ( lane + 32 < n_here && srow [ lane + 32 ] >= 0 ) 2392? sycl::native:: exp ( b - m ) 2393: 0.0f ; 2394sp [ h ][ lane ] = ea ; 2395sp [ h ][ lane + 32 ] = eb ; 2396const float l = lane0_sum ( ea + eb ); 2397if ( lane == 0 ) { part_m [ slot * G + h ] = m ; part_l [ slot * G + h ] = l ; } 2398} 2399/* 2400DPCT1065: Consider replacing sycl::nd_item::barrier() with 2401sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 2402performance if there is no access to global memory. 2403*/ 2404item_ct1 . barrier (sycl::access::fence_space:: local_space ); 2405// values: thread t owns dimension t for all 12 heads. 2406float acc [ G ]; 2407#pragma unroll 2408for (int h = 0 ; h < G ; ++ h ) acc [ h ] = 0.0f ; 2409for (int c0 = 0 ; c0 < n_here ; c0 += 8 ) { 2410long long rows [ 8 ]; bool full = c0 + 8 <= n_here ; 2411#pragma unroll 2412for (int j = 0 ; j < 8 ; ++ j ) { 2413rows [ j ] = c0 + j < n_here ? srow [ c0 + j ] : -1 ; 2414full = full && rows [ j ] >= 0 ; 2415} 2416if ( full ) { 2417float values [ 8 ]; 2418#pragma unroll 2419for (int j = 0 ; j < 8 ; ++ j ) { 2420const long long row = rows [ j ]; 2421float v ; 2422if constexpr ( KV_MODE == 0 ) { 2423v = sycl:: vec < sycl::half, 1 > ( 2424sycl:: bit_cast < sycl::half, unsigned short > ( 2425p . v_pool [ row * HD + t ])) 2426. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2427} else if constexpr ( KV_MODE == 1 ) { 2428const float sc = 2429sycl:: vec < sycl::half, 1 > ( 2430sycl:: bit_cast < sycl::half, unsigned short > ( 2431p . v_scale [ row * ( HD / KV_Q8_GROUP ) + 2432t / KV_Q8_GROUP ])) 2433. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2434v = (float) p . v_q [ row * HD + t ] * sc ; 2435} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 2436constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 2437const int b = t / QK4_0 ; 2438const int rem = t % QK4_0 ; 2439const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + b ; 2440const float d = 2441sycl:: vec < sycl::half, 1 > ( 2442sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 2443. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2444const int j = rem < 16 ? rem : ( rem - 16 ); 2445const uint8_t byte = blk -> qs [ j ]; 2446const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 2447v = (float) nibble * d ; 2448} 2449values [ j ] = v ; 2450} 2451#pragma unroll 2452for (int h = 0 ; h < G ; ++ h ) { 2453const sycl::float4 p0 = * reinterpret_cast < const sycl::float4 *> ( & sp [ h ][ c0 ]); 2454const sycl::float4 p1 = * reinterpret_cast < const sycl::float4 *> ( & sp [ h ][ c0 + 4 ]); 2455acc [ h ] = sycl:: fma ( p0 [ 0 ], values [ 0 ], acc [ h ]); 2456acc [ h ] = sycl:: fma ( p0 [ 1 ], values [ 1 ], acc [ h ]); 2457acc [ h ] = sycl:: fma ( p0 [ 2 ], values [ 2 ], acc [ h ]); 2458acc [ h ] = sycl:: fma ( p0 [ 3 ], values [ 3 ], acc [ h ]); 2459acc [ h ] = sycl:: fma ( p1 [ 0 ], values [ 4 ], acc [ h ]); 2460acc [ h ] = sycl:: fma ( p1 [ 1 ], values [ 5 ], acc [ h ]); 2461acc [ h ] = sycl:: fma ( p1 [ 2 ], values [ 6 ], acc [ h ]); 2462acc [ h ] = sycl:: fma ( p1 [ 3 ], values [ 7 ], acc [ h ]); 2463} 2464 2465} else { 2466#pragma unroll 2467for (int j = 0 ; j < 8 ; ++ j ) { 2468const long long row = rows [ j ]; 2469if ( row < 0 ) continue ; 2470float v ; 2471if constexpr ( KV_MODE == 0 ) { 2472v = sycl:: vec < sycl::half, 1 > ( 2473sycl:: bit_cast < sycl::half, unsigned short > ( 2474p . v_pool [ row * HD + t ])) 2475. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2476} else if constexpr ( KV_MODE == 1 ) { 2477const float sc = 2478sycl:: vec < sycl::half, 1 > ( 2479sycl:: bit_cast < sycl::half, unsigned short > ( 2480p . v_scale [ row * ( HD / KV_Q8_GROUP ) + 2481t / KV_Q8_GROUP ])) 2482. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2483v = (float) p . v_q [ row * HD + t ] * sc ; 2484} else { // modes 2 and 3: V is rotated Q4_0 (kv_q4.hpp); the caller rotates the output back 2485constexpr int bytes_per_head = ( HD / QK4_0 ) * sizeof ( block_q4_0 ); 2486const int b = t / QK4_0 ; 2487const int rem = t % QK4_0 ; 2488const block_q4_0 * blk = reinterpret_cast < const block_q4_0 *> ( p . v_q4 + row * bytes_per_head ) + b ; 2489const float d = 2490sycl:: vec < sycl::half, 1 > ( 2491sycl:: bit_cast < sycl::half, unsigned short > ( blk -> d )) 2492. convert < float, sycl::rounding_mode::automatic > ()[ 0 ]; 2493const int j = rem < 16 ? rem : ( rem - 16 ); 2494const uint8_t byte = blk -> qs [ j ]; 2495const int nibble = ( rem < 16 ) ? (( byte & 0x0F ) - 8 ) : (( byte >> 4 ) - 8 ); 2496v = (float) nibble * d ; 2497} 2498#pragma unroll 2499for (int h = 0 ; h < G ; ++ h ) acc [ h ] = sycl:: fma ( sp [ h ][ c0 + j ], v , acc [ h ]); 2500} 2501} 2502} 2503 2504#pragma unroll 2505for (int h = 0 ; h < G ; ++ h ) part_acc [((size_t) slot * G + h ) * HD + t ] = acc [ h ]; 2506} 2507 2508class B60QsaLocalFence; 2509inline bool b60_next_qsa_pool (){ static const bool on = [] { const char * p = std:: getenv ( "STRATA_PRIVATE_B60_NEXT_POOL" ); return p && p [ 0 ] == '1' ;}(); return on ;} 2510class B60QsaFloat4; 2511thread_local bool b60_qsa_float4_probe_mode = false; 2512 2513class QsaVectorCodes; 2514} 2515static void launch_normal_qsa_vector (int mode , const float * q , const QsaAttnPools & pools , 2516const int32_t * ids , const int32_t * steps ,int64_t cap , const QsaShapes & sh , 2517float * scratch ,float * attn ,int64_t nq ,void * stream ,float * secondary = nullptr ){ 2518if ( nq <= 0 ) return ; 2519const int chunks = int(( cap + CHUNK - 1 )/ CHUNK ); 2520const long long stride = qsa_decode_attn_scratch_floats ( cap , sh ); 2521auto * pm = scratch + size_t( chunks ) * sh . n_head * HD ; 2522auto * pl = pm + size_t( chunks ) * sh . n_head ; 2523auto * queue = strata:: q_of ( stream ); const float scale = 1.f / sqrtf (float( HD )); 2524auto props = sycl::ext::oneapi::experimental::properties{}; 2525if ( b60_layout_requested () && b60_next_qsa_pool () && (( cap == 2051 && nq == 1 && (! b60_single_actor_requested () && queue -> get_device () == static_cast < const sycl::device &> (dpct:: get_device ( 0 )))) || ( cap == 2051 && nq == 1 && ( b60_single_actor_requested () || queue -> get_device () == static_cast < const sycl::device &> (dpct:: get_device ( 1 )))))) { 2526queue -> parallel_for < B60QsaHead6 > ( 2527sycl:: nd_range < 3 > (sycl:: range < 3 > ( nq , sh . n_head_kv ,size_t( chunks ) * 2 * THREADS ),sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 2528[ = ](sycl::nd_item < 3 > )[[ sycl :: reqd_sub_group_size ( 32 )]]{ 2529b60_qsa_head6_kernel < 1 > ( q , pools , ids , steps , sh . n_head_kv , sh . page_size , scale , scratch , pm , pl , chunks , cap , stride ); 2530}); 2531} else 2532if ( b60_layout_requested () && b60_next_qsa_pool () && (( cap == 2051 && nq == 4 && (! b60_single_actor_requested () && queue -> get_device () == static_cast < const sycl::device &> (dpct:: get_device ( 0 )))) || ( cap == 32768 && nq == 1 && ( b60_single_actor_requested () || queue -> get_device () == static_cast < const sycl::device &> (dpct:: get_device ( 1 )))))) { 2533queue -> parallel_for < B60QsaLocalFence > ( 2534sycl:: nd_range < 3 > (sycl:: range < 3 > ( nq , sh . n_head_kv ,size_t( chunks ) * THREADS ),sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 2535[ = ](sycl::nd_item < 3 > )[[ sycl :: reqd_sub_group_size ( 32 )]]{ 2536b60_qsa_local_fence_kernel < 1 > ( q , pools , ids , steps , sh . n_head_kv , sh . page_size , scale , scratch , pm , pl , chunks , cap , stride ); 2537}); 2538} else 2539if ( b60_qsa_float4_probe_mode || ( b60_layout_requested () && (( cap == 2051 && nq >= 1 && nq <= 4 ) || ( cap == 32768 && nq == 1 )))) { 2540queue -> parallel_for < B60QsaFloat4 > ( 2541sycl:: nd_range < 3 > (sycl:: range < 3 > ( nq , sh . n_head_kv ,size_t( chunks ) * THREADS ),sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 2542[ = ](sycl::nd_item < 3 > )[[ sycl :: reqd_sub_group_size ( 32 )]]{ 2543b60_qsa_float4_kernel < 1 > ( q , pools , ids , steps , sh . n_head_kv , sh . page_size , scale , scratch , pm , pl , chunks , cap , stride ); 2544}); 2545} else { 2546queue -> parallel_for < QsaVectorCodes > ( 2547sycl:: nd_range < 3 > (sycl:: range < 3 > ( nq , sh . n_head_kv ,size_t( chunks ) * THREADS ),sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 2548[ = ](sycl::nd_item < 3 > )[[ sycl :: reqd_sub_group_size ( 32 )]]{ 2549chunk_vector_codes_kernel < 1 > ( q , pools , ids , steps , sh . n_head_kv , sh . page_size , scale , scratch , pm , pl , chunks , cap , stride ); 2550}); 2551} 2552if ( secondary ) { 2553qsa_merge_two_stage_probe ( 1 , scratch , chunks , secondary , attn , stream ); 2554return ; 2555} 2556queue -> parallel_for < dpct_kernel_name < class qsa_vector_codes_merge >> ( 2557sycl:: nd_range < 3 > (sycl:: range < 3 > ( 1 , nq ,size_t( sh . n_head ) * THREADS ),sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 2558[ = ](sycl::nd_item < 3 > ){ attn_merge_kernel ( scratch , pm , pl , chunks , attn , stride );}); 2559} 2560static void launch_qsa_chunk_down (int mode , const float * q , const QsaAttnPools & pools , 2561const int32_t * ids , const int32_t * steps ,int64_t cap , const QsaShapes & s , 2562float * scratch ,float * attn ,int64_t nq ,void * stream ){ 2563if ( mode < 0 || mode > 1 ) throw std:: invalid_argument ( "QSA chunk down mode" ); 2564if ( mode == 0 || ! pools . k_q || ! pools . v_q || pools . k_q4 || pools . v_q4 ){ 2565qsa_decode_attn_batch ( q , pools , ids , steps , cap , s , scratch , attn , nq , stream ); return ; 2566} 2567if ( nq <= 0 ) return ; 2568static const bool vector_enabled = [] { 2569const char * v = std:: getenv ( "STRATA_SYCL_QSA_VALUE8_K8_VEC" ); 2570return ! v || v [ 0 ] == '1' ; 2571}(); 2572const bool vector_shape = ( cap == 2051 && ( nq == 1 || nq == 2 || nq == 3 || nq == 4 || nq == 32 )) || ( cap == 32768 && nq == 1 ); 2573if ( vector_enabled && vector_shape ){ launch_normal_qsa_vector ( 1 , q , pools , ids , steps , cap , s , scratch , attn , nq , stream ); return ;} 2574const int chunks = int(( cap + CHUNK - 1 )/ CHUNK ); 2575const long long stride = qsa_decode_attn_scratch_floats ( cap , s ); 2576auto * part_m = scratch + size_t( chunks ) * s . n_head * HD ; 2577auto * part_l = part_m + size_t( chunks ) * s . n_head ; 2578auto * queue = strata:: q_of ( stream ); 2579const float scale = 1.f / sqrtf (float( HD )); 2580auto props = sycl::ext::oneapi::experimental::properties{}; 2581queue -> parallel_for < QsaChunkDown > ( 2582sycl:: nd_range < 3 > (sycl:: range < 3 > ( nq , s . n_head_kv ,size_t( chunks ) * THREADS ),sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 2583[ = ](sycl::nd_item < 3 > )[[ sycl :: reqd_sub_group_size ( 32 )]]{ 2584chunk_down_kernel < 1 > ( q , pools , ids , steps , s . n_head_kv , s . page_size , scale , 2585scratch , part_m , part_l , chunks , cap , stride ); 2586}); 2587queue -> parallel_for < dpct_kernel_name < class qsa_chunk_down_merge >> ( 2588sycl:: nd_range < 3 > (sycl:: range < 3 > ( 1 , nq ,size_t( s . n_head ) * THREADS ),sycl:: range < 3 > ( 1 , 1 , THREADS )), props , 2589[ = ](sycl::nd_item < 3 > ){ attn_merge_kernel ( scratch , part_m , part_l , chunks , attn , stride );}); 2590} 2591 2592bool qsa_decode_attn_batch_try_two_stage ( const float * q , const QsaAttnPools & pools , 2593const int32_t * ids , const int32_t * steps , int64_t cap , const QsaShapes & s , 2594float * scratch , float * attn , int64_t n_q , float * secondary , 2595uint64_t secondary_floats , void * stream ) { 2596// The sole caller owns parts_ and a one-query scratch. Keep every other 2597// caller, geometry, storage mode and existing dispatch fallback unchanged. 2598if ( cap != 32768 || n_q != 1 || s . n_head != 24 || s . n_head_kv != 2 || 2599s . head_dim != 256 || s . page_size != 4 || ! q || ! ids || ! steps || 2600! scratch || ! attn || ! secondary || ! pools . page_table || 2601! pools . k_q || ! pools . v_q || ! pools . k_scale || ! pools . v_scale || 2602pools . k_q4 || pools . v_q4 || ! qsa_chunk_down_supported ( cap , n_q , s , stream )) 2603return false; 2604static const bool vector_enabled = [] { 2605const char * v = std:: getenv ( "STRATA_SYCL_QSA_VALUE8_K8_VEC" ); 2606return ! v || v [ 0 ] == '1' ; 2607}(); 2608auto * queue = strata:: q_of ( stream ); 2609const int chunks = int(( cap + CHUNK - 1 ) / CHUNK ); 2610const uint64_t used = qsa_merge_two_stage_scratch_floats ( chunks ); 2611if (! vector_enabled || ! queue -> is_in_order () || secondary_floats < used ) 2612return false; 2613const auto disjoint = []( const void * a , uint64_t an , const void * b , uint64_t bn ) { 2614const uintptr_t av = reinterpret_cast < uintptr_t > ( a ), bv = reinterpret_cast < uintptr_t > ( b ); 2615if ( av > UINTPTR_MAX - an || bv > UINTPTR_MAX - bn ) return false; 2616return av + an <= bv || bv + bn <= av ; 2617}; 2618const uint64_t bytes = used * sizeof (float); 2619if (! disjoint ( secondary , bytes , scratch , qsa_decode_attn_scratch_floats ( cap , s ) * sizeof (float)) || 2620! disjoint ( secondary , bytes , attn , uint64_t( 24 * HD ) * sizeof (float)) || 2621! disjoint ( secondary , bytes , q , uint64_t( 24 * HD ) * sizeof (float))) 2622return false; 2623launch_normal_qsa_vector ( 1 , q , pools , ids , steps , cap , s , scratch , attn , 2624n_q , stream , secondary ); 2625return true; 2626} 2627} 2628 2629namespace strata::kernels { 2630void qsa_query_float4_probe (int mode , const float * q , const QsaAttnPools & p , const int32_t * ids , const int32_t * steps ,int64_t cap , const QsaShapes & sh ,float * scratch ,float * attn ,int64_t T ,void * stream ,float * secondary ){ 2631if ( mode < 0 || mode > 1 ) throw std:: invalid_argument ( "B60 query float4 mode" ); 2632struct Restore {bool previous ;~ Restore (){ b60_qsa_float4_probe_mode = previous ;}} restore { b60_qsa_float4_probe_mode }; 2633b60_qsa_float4_probe_mode = mode == 1 ; 2634launch_normal_qsa_vector ( 1 , q , p , ids , steps , cap , sh , scratch , attn , T , stream , secondary ); 2635} 2636}