okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
523b78e21
main
1// sycl/src/kernels/cuda/dflash2.dp.cpp - the small kernels of the DFlash2 drafter on SYCL (Intel Arc). Hand port of src/kernels/cuda/dflash2.cu for the 2// Q4_0 drafter pack; the interface is include/strata/kernels/dflash2.hpp (a `stream` is a sycl::queue*, null = the default in-order queue). 3// 4// Differences from the CUDA file, all on purpose: 5// * Q4_0 pack only (df2_gemm throws for a Q8 matrix, df2_q8_tile is absent). 6// * DF-03, active columns: the GEMM is instantiated per column count NC = 1..8 (acc[R][NC]); the arithmetic of a column does not depend on NC (same steps, 7// same fma order, same shuffle tree), so the first n columns of an 8-column launch are bitwise the n-column launch. Alchemist has 128 GRFs a thread: 8// at 32 lanes a float is 4 GRFs, so acc[8][8] cannot live in registers; fewer columns is the difference between spilling and not. 9// * conv_rms_quant takes 256 threads (a work-group is at most 512 on Alchemist), the sum of the squares reduces over 8 sub-group partials, not 32. 10// * attention: no register staging of the next K/V tile (loads go to local memory directly). 11// * math: sycl::exp / rsqrt / rint (precise) where CUDA uses __expf / rsqrtf. 12// Everything is asynchronous on the queue, allocates nothing and can be recorded into a sycl graph. 13#define DPCT_PROFILING_ENABLED 14#include <sycl/sycl.hpp> 15#include <dpct/dpct.hpp> 16#include "strata/sycl_queue.hpp" 17#include "strata/kernels/dflash2.hpp" 18#include "strata/kernels/dp4a.hpp" 19 20#include <cfloat> 21#include <cmath> 22#include <cstdio> 23#include <cstdlib> 24#include <stdexcept> 25#include <string> 26 27namespace strata::kernels { 28namespace { 29 30constexpr int HD = kDf2Hd ; 31#ifndef DF2_ATTN_CHUNK_AUTO 32#define DF2_ATTN_CHUNK_AUTO 64 33#endif 34#ifndef DF2_ATTN_AUTO 35#define DF2_ATTN_AUTO 2 36#endif 37int g_gemm_variant = 0 ;// bench only (df2_gemm_variant) 38int g_attn_chunk = 0 ;// bench only (df2_attention_chunk) 39int g_attn_variant = 0 ;// 0: STRATA_DF2_ATTN or the default; 1: the CUDA file kernel (128 work-items); 2: 256 work-items, O[2][8] 40 41#define DF2_SG32 [[sycl::reqd_sub_group_size(32)]] 42#define DF2_SG16 [[sycl::reqd_sub_group_size(16)]] 43 44using sycl::nd_item ; 45inline float bf16f (uint16_t h ) {return sycl::bit_cast < float > ((uint32_t )h <<16 ); } 46inline float xorf (const sycl::sub_group & sg ,float v ,int o ) {return sycl::permute_group_by_xor (sg ,v , (unsigned )o ); } 47inline int xori (const sycl::sub_group & sg ,int v ,int o ) {return sycl::permute_group_by_xor (sg ,v , (unsigned )o ); } 48inline void bar (const nd_item < 1 >& it ) { sycl::group_barrier (it .get_group ()); } 49inline void bar (const nd_item < 2 >& it ) { sycl::group_barrier (it .get_group ()); } 50 51// one 32-block of one row to the planar activation layout (a sub-group: lane = element) 52inline void quant_block (const sycl::sub_group & sg ,float v ,int blk ,int col ,int K ,int8_t * xq ,float * xd ) { 53const int lane = (int )sg .get_local_linear_id (); 54float amax = sycl::fabs (v ); 55for (int o = 16 ;o > 0 ;o >>=1 )amax = sycl::fmax (amax ,xorf (sg ,amax ,o )); 56const float d = amax /127.0f ; 57const float inv = amax > 0.0f ?127.0f /amax :0.0f ; 58const int q = (int ) sycl::rint (v * inv ); 59xq [(size_t )col * K + blk * 32 + lane ]= (int8_t )q ; 60if (lane == 0 )xd [(size_t )col * (K /32 )+ blk ]= d ; 61} 62 63// ---- the grouped dynamic 2-tap convolution over the block's rows 64void conv_launch (sycl::queue * q ,const float * x ,const float * dyn ,int dyn_stride ,const float * base ,const float * resid ,float * out ,int rows ,int H ,int group ) { 65const int n = rows * H ; 66q -> parallel_for (sycl::nd_range < 1 > ((size_t ) ((n + 255 ) /256 )* 256 ,256 ), [= ](nd_item < 1 > it ) { 67const int i = (int )it .get_global_id (0 ); 68if (i >=n )return ; 69const int b = i /H ,c = i %H ; 70const int groups = H /group ; 71float acc = 0.0f ; 72#pragma unroll 73for (int o = 0 ;o < 2 ;++ o ) { 74if (b - o < 0 )continue ; 75const float k = base [o * H + c ]+ dyn [(size_t )b * dyn_stride + o * groups + c /group ]; 76acc = sycl::fma (k ,x [(size_t ) (b - o )* H + c ],acc ); 77 } 78out [i ]= resid ?resid [i ]+ acc :acc ; 79 }); 80} 81 82inline void rope_cs (double inv ,int32_t pos ,float & c ,float & s ) { 83double ang = (double )pos * inv ; 84const double two_pi = 6.283185307179586476925286766559 ; 85ang -= two_pi * sycl::rint (ang /two_pi );// [-pi, pi] 86s = sycl::sin ((float )ang ); 87c = sycl::cos ((float )ang ); 88} 89 90// ---- norm + rope of one (row, head), 128 work-items: out = rope(rmsnorm(in) * gamma) 91inline float head_norm (const nd_item < 2 >& it ,const sycl::local_accessor < float ,1 >& red ,float x ,float eps ) { 92const sycl::sub_group sg = it .get_sub_group (); 93const int d = (int )it .get_local_id (1 ); 94float ss = x * x ; 95for (int o = 16 ;o > 0 ;o >>=1 )ss += xorf (sg ,ss ,o ); 96if ((d & 31 )== 0 )red [d >>5 ]= ss ; 97bar (it ); 98const float tot = red [0 ]+ red [1 ]+ red [2 ]+ red [3 ]; 99return tot ; 100} 101 102}// namespace 103 104void df2_conv (const float * x ,const float * dyn ,int dyn_stride ,const float * base ,const float * resid ,float * out ,int rows ,int H ,int group ,void * stream ) { 105conv_launch (strata::q_of (stream ),x ,dyn ,dyn_stride ,base ,resid ,out ,rows ,H ,group ); 106} 107 108void df2_rope_inv (float theta ,double * inv ,void * stream ) { 109 strata::q_of (stream )-> parallel_for (sycl::nd_range < 1 > (64 ,64 ), [= ](nd_item < 1 > it ) { 110const int i = (int )it .get_local_id (0 ); 111inv [i ]= sycl::pow ((double )theta ,- (double ) (2 * i ) / (double )HD ); 112 }); 113} 114 115void df2_rope_table (const int32_t * pos ,int rows ,float theta ,float * table ,void * stream ) { 116 strata::q_of (stream )-> parallel_for (sycl::nd_range < 1 > ((size_t )rows * 64 ,64 ), [= ](nd_item < 1 > it ) { 117const int r = (int )it .get_group (0 ),i = (int )it .get_local_id (0 ); 118const double inv = sycl::pow ((double )theta ,- (double ) (2 * i ) / (double )HD ); 119float c ,s ; 120rope_cs (inv ,pos [r ],c ,s ); 121table [(r * 2 + 0 )* 64 + i ]= c ; 122table [(r * 2 + 1 )* 64 + i ]= s ; 123 }); 124} 125 126void df2_rope_table_inv (const int32_t * pos ,int rows ,const double * inv_tab ,float * table ,void * stream ) { 127 strata::q_of (stream )-> parallel_for (sycl::nd_range < 1 > ((size_t )rows * 64 ,64 ), [= ](nd_item < 1 > it ) { 128const int r = (int )it .get_group (0 ),i = (int )it .get_local_id (0 ); 129float c ,s ; 130rope_cs (inv_tab [i ],pos [r ],c ,s ); 131table [(r * 2 + 0 )* 64 + i ]= c ; 132table [(r * 2 + 1 )* 64 + i ]= s ; 133 }); 134} 135 136void df2_rope_table_cpu (const int32_t * pos ,int rows ,float theta ,float * table ) { 137const double two_pi = 6.283185307179586476925286766559 ; 138for (int i = 0 ;i < 64 ;++ i ) { 139const double inv = std::pow ((double )theta ,- (double ) (2 * i ) / (double )HD ); 140for (int r = 0 ;r < rows ;++ r ) { 141double ang = (double )pos [r ]* inv ; 142ang -= two_pi * std::nearbyint (ang /two_pi );// [-pi, pi] 143table [(r * 2 + 0 )* 64 + i ]= (float ) std::cos ((float )ang ); 144table [(r * 2 + 1 )* 64 + i ]= (float ) std::sin ((float )ang ); 145 } 146 } 147} 148 149void df2_norm_rope (const float * in ,int in_stride ,const float * gamma ,const int32_t * pos ,const float * rope_table ,float * out ,int slot_mod ,int rows ,int heads , 150float eps ,void * stream ) { 151 sycl::queue * q = strata::q_of (stream ); 152q -> submit ([& ](sycl::handler & h ) { 153 sycl::local_accessor < float ,1 > red (4 ,h ),y (HD ,h ); 154h .parallel_for (sycl::nd_range < 2 > ({(size_t )heads , (size_t )rows * HD }, {1 ,HD }), [= ](nd_item < 2 > it )DF2_SG32 { 155const int hh = (int )it .get_group (0 ),r = (int )it .get_group (1 ),d = (int )it .get_local_id (1 ); 156const float x = in [(size_t )r * in_stride + hh * HD + d ]; 157const float tot = head_norm (it ,red ,x ,eps ); 158const float v = x * sycl::rsqrt (tot / (float )HD + eps )* gamma [d ]; 159float res = v ; 160if (rope_table != nullptr ) { 161y [d ]= v ; 162bar (it ); 163const int i = d & 63 ; 164const float c = rope_table [(r * 2 + 0 )* 64 + i ],s = rope_table [(r * 2 + 1 )* 64 + i ]; 165res = d < 64 ?v * c - y [d + 64 ]* s :v * c + y [d - 64 ]* s ; 166 } 167const int slot = slot_mod > 0 ? (int ) (pos [r ] %slot_mod ) :r ; 168out [slot_mod > 0 ? ((size_t )hh * slot_mod + slot )* HD + d : ((size_t )slot * heads + hh )* HD + d ]= res ; 169 }); 170 }); 171} 172 173void df2_copy_rows (const float * in ,float * out ,const int32_t * pos ,int slot_mod ,int rows ,int cols ,void * stream ) { 174const int n = rows * cols ; 175 strata::q_of (stream )-> parallel_for (sycl::nd_range < 1 > ((size_t ) ((n + 255 ) /256 )* 256 ,256 ), [= ](nd_item < 1 > it ) { 176const int i = (int )it .get_global_id (0 ); 177if (i >=n )return ; 178const int r = i /cols ,c = i %cols ; 179const int slot = slot_mod > 0 ? (int ) (pos [r ] %slot_mod ) :r ; 180out [slot_mod > 0 ? ((size_t ) (c /HD )* slot_mod + slot )* HD + c %HD : (size_t )slot * cols + c ]= in [i ]; 181 }); 182} 183 184// ---- attention. grid (8 K/V heads, kDf2MaxChunks), 128 work-items. The 32 queries (4 heads x 8 rows) of a K/V head share each key tile; 185// S = Q K^T in 4 x 4 register tiles (64 work-items), the online softmax per query, O += P V in 4 x 8 register tiles (128 work-items). 186namespace { 187constexpr int AS4 = 33 ;// local row stride of the Q / K / V tiles in float4 (132 floats: conflict-free for the 4 x 4 tiles) 188constexpr int PS4 = 9 ;// stride of the transposed probabilities P[key][query] in float4 (36 floats) 189} 190 191void df2_attention (const float * q ,const float * kring ,const float * vring ,const float * kblk ,const float * vblk ,const int32_t * anchor_pos ,int ring_slots , 192int window ,float * part ,void * xq ,float * xd ,void * stream ,const int64_t * tag ,const int32_t * epoch_ptr ) { 193 sycl::queue * qu = strata::q_of (stream ); 194static const int env_v = [] {const char * e = std::getenv ("STRATA_DF2_ATTN" );return e ? std::atoi (e ) :0 ; }(); 195const int av = g_attn_variant != 0 ?g_attn_variant :env_v != 0 ?env_v :DF2_ATTN_AUTO ; 196static const int env_ck = [] {const char * e = std::getenv ("STRATA_DF2_ATTN_CHUNK" );return e ? std::atoi (e ) :0 ; }(); 197const int CK = g_attn_chunk != 0 ?g_attn_chunk :env_ck == 128 ?128 :DF2_ATTN_CHUNK_AUTO ;// keys per work-group: 64 (33 chunks) or 128 (17) 198const int nch = CK == 128 ?kDf2MaxChunks :kDf2PartChunks ; 199if (av == 2 ) { 200qu -> submit ([& ](sycl::handler & h ) { 201 sycl::local_accessor < sycl::float4 ,1 > Qs (32 * AS4 ,h ),Ks (32 * AS4 ,h ),Ps (32 * PS4 ,h ); 202 sycl::local_accessor < float ,1 > Sc (32 * 33 ,h ),m_s (32 ,h ),l_s (32 ,h ),corr_s (32 ,h ); 203 sycl::local_accessor < unsigned char ,1 > kvalid (32 ,h ); 204h .parallel_for (sycl::nd_range < 2 > ({(size_t )nch , (size_t )kDf2Kv * 256 }, {1 ,256 }), [= ](nd_item < 2 > it )DF2_SG16 { 205const sycl::sub_group sg = it .get_sub_group (); 206const int chunk = (int )it .get_group (0 ),kvh = (int )it .get_group (1 ),tid = (int )it .get_local_id (1 ); 207float * Psf = reinterpret_cast < float *> (& Ps [0 ]); 208const int s = * anchor_pos ; 209const int lo = s - (window - 1 )> 0 ?s - (window - 1 ) :0 ; 210const int nctx = s - lo ; 211const int nkeys = nctx + kDf2Rows ; 212const int k_begin = chunk * CK ; 213const int64_t ep = tag != nullptr ? ((int64_t )* epoch_ptr <<32 ) :0 ; 214float * const pbase = part + (((size_t )chunk * kDf2Kv + kvh )* 32 )* 130 ; 215if (k_begin >=nkeys ) { 216for (int i = tid ;i < 32 * 130 ;i += 256 )pbase [i ]= (i %130 == 0 ) ?- INFINITY :0.0f ; 217return ; 218 } 219const float scale = sycl::rsqrt ((float )HD ); 220#pragma unroll 221for (int u = 0 ;u < 4 ;++ u ) {// Q: 32 queries x 128, float4 222const int i = tid + u * 256 ; 223const int a = i / (HD /4 ),d4 = i % (HD /4 ); 224const int row = a >>2 ,g = a & 3 ; 225 sycl::float4 v = * reinterpret_cast < const sycl::float4 *> (q + ((size_t )row * 32 + kvh * kDf2Gqa + g )* HD + d4 * 4 ); 226Qs [a * AS4 + d4 ]= v * scale ; 227 } 228if (tid < 32 ) {m_s [tid ]= - INFINITY ;l_s [tid ]= 0.0f ; } 229float O [2 ][8 ]; 230#pragma unroll 231for (int i = 0 ;i < 2 ;++ i ) 232#pragma unroll 233for (int j = 0 ;j < 8 ;++ j )O [i ][j ]= 0.0f ; 234const int qg_pv = tid >>4 ,dg_pv = tid & 15 ;// PV: queries qg*2, qg*2+1, dims dg*8 .. +7 235const int qg = tid >>4 ,kg = tid & 15 ;// S: queries qg*2 .. +1, keys kg*2 .. +1 236const int k_end = sycl::min (k_begin + CK ,nkeys ); 237auto load_tile = [& ](int k0 ,const float * ring ,const float * blk ) { 238#pragma unroll 239for (int u = 0 ;u < 4 ;++ u ) { 240const int i = tid + u * 256 ,t = i / (HD /4 ),d4 = i % (HD /4 ),k = k0 + t ; 241 sycl::float4 v (0.f ,0.f ,0.f ,0.f ); 242if (k < nctx )v = * reinterpret_cast < const sycl::float4 *> (ring + ((size_t )kvh * ring_slots + (lo + k ) %ring_slots )* HD + d4 * 4 ); 243else if (k < nkeys )v = * reinterpret_cast < const sycl::float4 *> (blk + ((size_t ) (k - nctx )* kDf2Kv + kvh )* HD + d4 * 4 ); 244Ks [t * AS4 + d4 ]= v ; 245 } 246 }; 247for (int k0 = k_begin ;k0 < k_end ;k0 += 32 ) { 248bar (it ); 249load_tile (k0 ,kring ,kblk ); 250if (tid < 32 ) { 251const int k = k0 + tid ; 252kvalid [tid ]= (tag == nullptr || k >=nctx || k >=nkeys ) ?1 : (tag [(lo + k ) %ring_slots ]== (ep | (int64_t ) (lo + k )) ?1 :0 ); 253 } 254bar (it ); 255 {// S tile: 2 queries x 2 keys per work-item 256float acc [2 ][2 ]; 257#pragma unroll 258for (int i = 0 ;i < 2 ;++ i ) 259#pragma unroll 260for (int j = 0 ;j < 2 ;++ j )acc [i ][j ]= 0.0f ; 261#pragma unroll 4 262for (int d4 = 0 ;d4 < HD /4 ;++ d4 ) { 263 sycl::float4 qv [2 ],kv [2 ]; 264#pragma unroll 265for (int i = 0 ;i < 2 ;++ i )qv [i ]= Qs [(qg * 2 + i )* AS4 + d4 ]; 266#pragma unroll 267for (int j = 0 ;j < 2 ;++ j )kv [j ]= Ks [(kg * 2 + j )* AS4 + d4 ]; 268#pragma unroll 269for (int i = 0 ;i < 2 ;++ i ) 270#pragma unroll 271for (int j = 0 ;j < 2 ;++ j )acc [i ][j ]+= qv [i ].x ()* kv [j ].x ()+ qv [i ].y ()* kv [j ].y ()+ qv [i ].z ()* kv [j ].z ()+ qv [i ].w ()* kv [j ].w (); 272 } 273#pragma unroll 274for (int i = 0 ;i < 2 ;++ i ) 275#pragma unroll 276for (int j = 0 ;j < 2 ;++ j ) { 277const int qi = qg * 2 + i ,t = kg * 2 + j ,k = k0 + t ,row = qi >>2 ; 278bool ok = k < nkeys ; 279if (ok && k < nctx )ok = (lo + k )> (s + row - window )&& kvalid [t ]; 280Sc [qi * 33 + t ]= ok ?acc [i ][j ] :- INFINITY ; 281 } 282 } 283bar (it ); 284 {// online softmax: query = tid / 8, 4 keys each, 8 lanes per query 285const int qi = tid >>3 ,sub = tid & 7 ; 286float v [4 ],tmax = - INFINITY ; 287#pragma unroll 288for (int u = 0 ;u < 4 ;++ u ) {v [u ]= Sc [qi * 33 + sub * 4 + u ];tmax = sycl::fmax (tmax ,v [u ]); } 289tmax = sycl::fmax (tmax ,xorf (sg ,tmax ,1 )); 290tmax = sycl::fmax (tmax ,xorf (sg ,tmax ,2 )); 291tmax = sycl::fmax (tmax ,xorf (sg ,tmax ,4 )); 292const float m_old = m_s [qi ]; 293const float mn = sycl::fmax (m_old ,tmax ); 294const float corr = (m_old == - INFINITY ) ?0.0f : sycl::exp (m_old - mn ); 295float psum = 0.0f ; 296#pragma unroll 297for (int u = 0 ;u < 4 ;++ u ) { 298const float p = v [u ]== - INFINITY ?0.0f : sycl::exp (v [u ]- mn ); 299Psf [(sub * 4 + u )* (PS4 * 4 )+ qi ]= p ; 300psum += p ; 301 } 302psum += xorf (sg ,psum ,1 ); 303psum += xorf (sg ,psum ,2 ); 304psum += xorf (sg ,psum ,4 ); 305if (sub == 0 ) {l_s [qi ]= l_s [qi ]* corr + psum ;m_s [qi ]= mn ;corr_s [qi ]= corr ; } 306 } 307bar (it ); 308load_tile (k0 ,vring ,vblk ); 309bar (it ); 310 { 311const float cr [2 ]= {corr_s [qg_pv * 2 ],corr_s [qg_pv * 2 + 1 ]}; 312#pragma unroll 313for (int i = 0 ;i < 2 ;++ i ) 314#pragma unroll 315for (int j = 0 ;j < 8 ;++ j )O [i ][j ] *=cr [i ]; 316#pragma unroll 4 317for (int t = 0 ;t < 32 ;++ t ) { 318const float p0 = Psf [t * (PS4 * 4 )+ qg_pv * 2 ],p1 = Psf [t * (PS4 * 4 )+ qg_pv * 2 + 1 ]; 319const sycl::float4 va = Ks [t * AS4 + dg_pv * 2 ],vb = Ks [t * AS4 + dg_pv * 2 + 1 ]; 320const float vv [8 ]= {va .x (),va .y (),va .z (),va .w (),vb .x (),vb .y (),vb .z (),vb .w ()}; 321#pragma unroll 322for (int j = 0 ;j < 8 ;++ j ) {O [0 ][j ]= sycl::fma (p0 ,vv [j ],O [0 ][j ]);O [1 ][j ]= sycl::fma (p1 ,vv [j ],O [1 ][j ]); } 323 } 324 } 325 } 326bar (it ); 327if (tid < 32 ) {pbase [tid * 130 ]= m_s [tid ];pbase [tid * 130 + 1 ]= l_s [tid ]; } 328#pragma unroll 329for (int i = 0 ;i < 2 ;++ i ) { 330float * o = pbase + (size_t ) (qg_pv * 2 + i )* 130 + 2 + dg_pv * 8 ; 331#pragma unroll 332for (int j = 0 ;j < 8 ;++ j )o [j ]= O [i ][j ]; 333 } 334 }); 335 }); 336 }else 337qu -> submit ([& ](sycl::handler & h ) { 338 sycl::local_accessor < sycl::float4 ,1 > Qs (32 * AS4 ,h ),Ks (32 * AS4 ,h ),Ps (32 * PS4 ,h ); 339 sycl::local_accessor < float ,1 > Sc (32 * 33 ,h ),m_s (32 ,h ),l_s (32 ,h ),corr_s (32 ,h ); 340 sycl::local_accessor < unsigned char ,1 > kvalid (32 ,h ); 341h .parallel_for (sycl::nd_range < 2 > ({(size_t )nch , (size_t )kDf2Kv * 128 }, {1 ,128 }), [= ](nd_item < 2 > it )DF2_SG16 { 342const sycl::sub_group sg = it .get_sub_group (); 343const int chunk = (int )it .get_group (0 ),kvh = (int )it .get_group (1 ),tid = (int )it .get_local_id (1 ); 344float * Psf = reinterpret_cast < float *> (& Ps [0 ]); 345const int s = * anchor_pos ; 346const int lo = s - (window - 1 )> 0 ?s - (window - 1 ) :0 ; 347const int nctx = s - lo ; 348const int nkeys = nctx + kDf2Rows ; 349const int k_begin = chunk * CK ; 350const int64_t ep = tag != nullptr ? ((int64_t )* epoch_ptr <<32 ) :0 ; 351float * const pbase = part + (((size_t )chunk * kDf2Kv + kvh )* 32 )* 130 ; 352if (k_begin >=nkeys ) { 353for (int i = tid ;i < 32 * 130 ;i += 128 )pbase [i ]= (i %130 == 0 ) ?- INFINITY :0.0f ; 354return ; 355 } 356const float scale = sycl::rsqrt ((float )HD ); 357for (int i = tid ;i < 32 * HD /4 ;i += 128 ) {// Q: 32 queries x 128, float4 358const int a = i / (HD /4 ),d4 = i % (HD /4 ); 359const int row = a >>2 ,g = a & 3 ; 360 sycl::float4 v = * reinterpret_cast < const sycl::float4 *> (q + ((size_t )row * 32 + kvh * kDf2Gqa + g )* HD + d4 * 4 ); 361Qs [a * AS4 + d4 ]= v * scale ; 362 } 363if (tid < 32 ) {m_s [tid ]= - INFINITY ;l_s [tid ]= 0.0f ; } 364float O [4 ][8 ]; 365#pragma unroll 366for (int i = 0 ;i < 4 ;++ i ) 367#pragma unroll 368for (int j = 0 ;j < 8 ;++ j )O [i ][j ]= 0.0f ; 369const int qg_pv = tid >>4 ,dg_pv = tid & 15 ;// PV: queries qg*4 .. +3, dims dg*8 .. +7 370const int k_end = sycl::min (k_begin + CK ,nkeys ); 371// a tile is 32 keys x 128 dims = 1024 float4: eight per work-item 372auto load_tile = [& ](int k0 ,const float * ring ,const float * blk ) { 373#pragma unroll 374for (int u = 0 ;u < 8 ;++ u ) { 375const int i = tid + u * 128 ,t = i / (HD /4 ),d4 = i % (HD /4 ),k = k0 + t ; 376 sycl::float4 v (0.f ,0.f ,0.f ,0.f ); 377if (k < nctx )v = * reinterpret_cast < const sycl::float4 *> (ring + ((size_t )kvh * ring_slots + (lo + k ) %ring_slots )* HD + d4 * 4 ); 378else if (k < nkeys )v = * reinterpret_cast < const sycl::float4 *> (blk + ((size_t ) (k - nctx )* kDf2Kv + kvh )* HD + d4 * 4 ); 379Ks [t * AS4 + d4 ]= v ; 380 } 381 }; 382for (int k0 = k_begin ;k0 < k_end ;k0 += 32 ) { 383bar (it );// Ks free (the last tile's V reads done); Qs / m_s written on the first pass 384load_tile (k0 ,kring ,kblk ); 385if (tid < 32 ) { 386const int k = k0 + tid ; 387kvalid [tid ]= (tag == nullptr || k >=nctx || k >=nkeys ) ?1 : (tag [(lo + k ) %ring_slots ]== (ep | (int64_t ) (lo + k )) ?1 :0 ); 388 } 389bar (it ); 390if (tid < 64 ) {// S tile: 4 queries x 4 keys per work-item 391const int qg = tid >>3 ,kg = tid & 7 ; 392float acc [4 ][4 ]; 393#pragma unroll 394for (int i = 0 ;i < 4 ;++ i ) 395#pragma unroll 396for (int j = 0 ;j < 4 ;++ j )acc [i ][j ]= 0.0f ; 397#pragma unroll 4 398for (int d4 = 0 ;d4 < HD /4 ;++ d4 ) { 399 sycl::float4 qv [4 ],kv [4 ]; 400#pragma unroll 401for (int i = 0 ;i < 4 ;++ i )qv [i ]= Qs [(qg * 4 + i )* AS4 + d4 ]; 402#pragma unroll 403for (int j = 0 ;j < 4 ;++ j )kv [j ]= Ks [(kg * 4 + j )* AS4 + d4 ]; 404#pragma unroll 405for (int i = 0 ;i < 4 ;++ i ) 406#pragma unroll 407for (int j = 0 ;j < 4 ;++ j )acc [i ][j ]+= qv [i ].x ()* kv [j ].x ()+ qv [i ].y ()* kv [j ].y ()+ qv [i ].z ()* kv [j ].z ()+ qv [i ].w ()* kv [j ].w (); 408 } 409#pragma unroll 410for (int i = 0 ;i < 4 ;++ i ) 411#pragma unroll 412for (int j = 0 ;j < 4 ;++ j ) { 413const int qi = qg * 4 + i ,t = kg * 4 + j ,k = k0 + t ,row = qi >>2 ; 414bool ok = k < nkeys ; 415if (ok && k < nctx )ok = (lo + k )> (s + row - window )&& kvalid [t ];// s + row - j < window, and the slot holds that row 416Sc [qi * 33 + t ]= ok ?acc [i ][j ] :- INFINITY ; 417 } 418 } 419bar (it ); 420 {// online softmax: query = tid / 4, 8 keys each, 4 lanes per query 421const int qi = tid >>2 ,sub = tid & 3 ; 422float v [8 ],tmax = - INFINITY ; 423#pragma unroll 424for (int u = 0 ;u < 8 ;++ u ) {v [u ]= Sc [qi * 33 + sub * 8 + u ];tmax = sycl::fmax (tmax ,v [u ]); } 425tmax = sycl::fmax (tmax ,xorf (sg ,tmax ,1 )); 426tmax = sycl::fmax (tmax ,xorf (sg ,tmax ,2 )); 427const float m_old = m_s [qi ]; 428const float mn = sycl::fmax (m_old ,tmax ); 429const float corr = (m_old == - INFINITY ) ?0.0f : sycl::exp (m_old - mn ); 430float psum = 0.0f ; 431#pragma unroll 432for (int u = 0 ;u < 8 ;++ u ) { 433const float p = v [u ]== - INFINITY ?0.0f : sycl::exp (v [u ]- mn ); 434Psf [(sub * 8 + u )* (PS4 * 4 )+ qi ]= p ; 435psum += p ; 436 } 437psum += xorf (sg ,psum ,1 ); 438psum += xorf (sg ,psum ,2 ); 439if (sub == 0 ) {l_s [qi ]= l_s [qi ]* corr + psum ;m_s [qi ]= mn ;corr_s [qi ]= corr ; } 440 } 441bar (it );// Ps, corr_s complete; every read of Ks (the S tile) done 442load_tile (k0 ,vring ,vblk ); 443bar (it ); 444 { 445const float cr [4 ]= {corr_s [qg_pv * 4 ],corr_s [qg_pv * 4 + 1 ],corr_s [qg_pv * 4 + 2 ],corr_s [qg_pv * 4 + 3 ]}; 446#pragma unroll 447for (int i = 0 ;i < 4 ;++ i ) 448#pragma unroll 449for (int j = 0 ;j < 8 ;++ j )O [i ][j ] *=cr [i ]; 450#pragma unroll 4 451for (int t = 0 ;t < 32 ;++ t ) { 452const sycl::float4 p4 = Ps [t * PS4 + qg_pv ]; 453const sycl::float4 va = Ks [t * AS4 + dg_pv * 2 ],vb = Ks [t * AS4 + dg_pv * 2 + 1 ]; 454const float pv [4 ]= {p4 .x (),p4 .y (),p4 .z (),p4 .w ()}; 455const float vv [8 ]= {va .x (),va .y (),va .z (),va .w (),vb .x (),vb .y (),vb .z (),vb .w ()}; 456#pragma unroll 457for (int i = 0 ;i < 4 ;++ i ) 458#pragma unroll 459for (int j = 0 ;j < 8 ;++ j )O [i ][j ]= sycl::fma (pv [i ],vv [j ],O [i ][j ]); 460 } 461 } 462 } 463bar (it ); 464if (tid < 32 ) {pbase [tid * 130 ]= m_s [tid ];pbase [tid * 130 + 1 ]= l_s [tid ]; } 465#pragma unroll 466for (int i = 0 ;i < 4 ;++ i ) { 467float * o = pbase + (size_t ) (qg_pv * 4 + i )* 130 + 2 + dg_pv * 8 ; 468#pragma unroll 469for (int j = 0 ;j < 8 ;++ j )o [j ]= O [i ][j ]; 470 } 471 }); 472 }); 473qu -> parallel_for (sycl::nd_range < 1 > ((size_t )kDf2Kv * 32 * HD ,HD ), [= ](nd_item < 1 > it )DF2_SG32 { 474const sycl::sub_group sg = it .get_sub_group (); 475const int a = (int )it .get_group (0 );// kvh * 32 + qi 476const int kvh = a >>5 ,qi = a & 31 ,d = (int )it .get_local_id (0 ); 477float M = - INFINITY ; 478for (int c = 0 ;c < nch ;++ c )M = sycl::fmax (M ,part [(((size_t )c * kDf2Kv + kvh )* 32 + qi )* 130 ]); 479float L = 0.0f ,o = 0.0f ; 480for (int c = 0 ;c < nch ;++ c ) { 481const float * p = part + (((size_t )c * kDf2Kv + kvh )* 32 + qi )* 130 ; 482const float mc = p [0 ]; 483const float w = (mc == - INFINITY ) ?0.0f : sycl::exp (mc - M ); 484L += p [1 ]* w ; 485o = sycl::fma (p [2 + d ],w ,o ); 486 } 487const int row = qi >>2 ,g = qi & 3 ; 488// the output row `row`, head kvh * 4 + g, dims d: quant block (kvh * 4 + g) * 4 + d / 32 of the 4096-wide row 489quant_block (sg ,L > 0.0f ?o /L :0.0f , (kvh * kDf2Gqa + g )* (HD /32 )+ (d >>5 ),row ,32 * HD ,static_cast < int8_t *> (xq ),xd ); 490 }); 491} 492 493// ---- Q4_0 GEMM for up to 8 activation columns (the drafter's matrices); NC = the columns that are computed (DF-03) 494// 495// Weights are TILED (df2_q4_tile makes them from ggml Q4_0 blocks): a group of 8 rows is one unit; per 512 values of the reduction axis (a "step", 16 496// Q4_0 blocks) the group stores the 8 rows' nibbles one after the other (2 KB), a lane (block * 2 + half) takes 8 bytes = 16 values as two words. Word 0 497// holds values 0-3 in its four low nibbles and 4-7 in its four high nibbles, word 1 holds 8-11 and 12-15, so (w & 0x0F0F0F0F) and ((w >> 4) & 0x0F0F0F0F) 498// are the int8 words dp4a wants, as unsigned q = 0..15 (value = (q - 8) * d). sum((q - 8) x) = sum(q x) - 8 sum(x). The fp16 block scales follow the 499// nibble plane (n_out * n_in / 2 bytes) in the same tile order. Activations: xq (int8 [col][n_in]) and xd (float [col][n_in / 32]). 500namespace { 501struct Df2Seg {const int8_t * q ;const sycl::half * d ;float * y ;int n_out ;int row0 ; }; 502struct Df2Segs {Df2Seg s [3 ];int n ; }; 503 504inline Df2Seg df2_pick (const Df2Segs & segs ,int row_g ) { 505Df2Seg sg = segs .s [0 ]; 506if (segs .n > 1 && row_g >=segs .s [1 ].row0 )sg = segs .s [1 ]; 507if (segs .n > 2 && row_g >=segs .s [2 ].row0 )sg = segs .s [2 ]; 508return sg ; 509} 510 511template < int WARPS ,int R ,int UNR ,int NC > 512void gemm4_launch (sycl::queue * q ,const Df2Segs & segs ,const int8_t * xq ,const float * xd ,int n_in ,int rows ) { 513constexpr int SUBS = 8 /R ; 514const size_t groups = (size_t ) (rows /8 )* SUBS /WARPS ; 515q -> parallel_for (sycl::nd_range < 1 > (groups * WARPS * 32 ,WARPS * 32 ), [= ](nd_item < 1 > it )DF2_SG32 { 516const sycl::sub_group sg = it .get_sub_group (); 517const int warp = (int ) (it .get_local_id (0 ) >>5 ),lane = (int )sg .get_local_linear_id (); 518const int wi = (int )it .get_group (0 )* WARPS + warp ; 519const int row_g = (wi /SUBS )* 8 ,sub = wi %SUBS ; 520const Df2Seg sgm = df2_pick (segs ,row_g ); 521const int row0 = row_g - sgm .row0 ; 522const int nb = n_in /32 ,steps = nb /16 ; 523const sycl::uint2 * wq = reinterpret_cast < const sycl::uint2 *> (sgm .q + (size_t )row0 * (n_in /2 ))+ lane + sub * R * 32 ; 524const sycl::half * wd = sgm .d + (size_t )row0 * nb + (lane >>1 )+ sub * R * 16 ; 525const sycl::int4 * xv = reinterpret_cast < const sycl::int4 *> (xq )+ lane ; 526float acc [R ][NC ]; 527#pragma unroll 528for (int r = 0 ;r < R ;++ r ) 529#pragma unroll 530for (int c = 0 ;c < NC ;++ c )acc [r ][c ]= 0.0f ; 531#pragma unroll UNR 532for (int st = 0 ;st < steps ;++ st ) { 533int w [R ][4 ]; 534float dw [R ]; 535#pragma unroll 536for (int r = 0 ;r < R ;++ r ) { 537const sycl::uint2 raw = wq [(size_t )st * 256 + r * 32 ]; 538// bytewise (q - 8) for q = 0..15: ((q | 0x80) - 8) ^ 0x80, no borrow across bytes; sum((q - 8) x) is the integer the CUDA kernel gets as sum(q x) - 8 sum(x) 539w [r ][0 ]= (int ) ((((raw .x ()& 0x0F0F0F0Fu ) |0x80808080u )- 0x08080808u ) ^0x80808080u ); 540w [r ][1 ]= (int ) (((((raw .x () >>4 )& 0x0F0F0F0Fu ) |0x80808080u )- 0x08080808u ) ^0x80808080u ); 541w [r ][2 ]= (int ) ((((raw .y ()& 0x0F0F0F0Fu ) |0x80808080u )- 0x08080808u ) ^0x80808080u ); 542w [r ][3 ]= (int ) (((((raw .y () >>4 )& 0x0F0F0F0Fu ) |0x80808080u )- 0x08080808u ) ^0x80808080u ); 543dw [r ]= (float )wd [(size_t )st * 128 + r * 16 ]; 544 } 545float xs [NC ]; 546#pragma unroll 547for (int c = 0 ;c < NC ;++ c )xs [c ]= xd [(size_t )c * nb + st * 16 + (lane >>1 )]; 548#pragma unroll 549for (int c = 0 ;c < NC ;++ c ) { 550const sycl::int4 x = xv [(size_t )c * (n_in /16 )+ st * 32 ]; 551#pragma unroll 552for (int r = 0 ;r < R ;++ r ) { 553int isum = STRATA_DP4A (w [r ][0 ],x .x (),0 ); 554isum = STRATA_DP4A (w [r ][1 ],x .y (),isum ); 555isum = STRATA_DP4A (w [r ][2 ],x .z (),isum ); 556isum = STRATA_DP4A (w [r ][3 ],x .w (),isum ); 557acc [r ][c ]= sycl::fma (dw [r ]* xs [c ], (float )isum ,acc [r ][c ]); 558 } 559 } 560 } 561#pragma unroll 562for (int r = 0 ;r < R ;++ r ) 563#pragma unroll 564for (int c = 0 ;c < NC ;++ c ) { 565float v = acc [r ][c ]; 566v += xorf (sg ,v ,16 ); 567v += xorf (sg ,v ,8 ); 568v += xorf (sg ,v ,4 ); 569v += xorf (sg ,v ,2 ); 570v += xorf (sg ,v ,1 ); 571if (lane == 0 )sgm .y [(size_t )c * sgm .n_out + row0 + sub * R + r ]= v ; 572 } 573 }); 574} 575 576template < int WARPS ,int R ,int UNR > 577void gemm4_nc (sycl::queue * q ,const Df2Segs & segs ,const int8_t * xq ,const float * xd ,int n_in ,int rows ,int ncols ) { 578switch (ncols ) { 579case 1 :gemm4_launch < WARPS ,R ,UNR ,1 > (q ,segs ,xq ,xd ,n_in ,rows );break ; 580case 2 :gemm4_launch < WARPS ,R ,UNR ,2 > (q ,segs ,xq ,xd ,n_in ,rows );break ; 581case 3 :gemm4_launch < WARPS ,R ,UNR ,3 > (q ,segs ,xq ,xd ,n_in ,rows );break ; 582case 4 :gemm4_launch < WARPS ,R ,UNR ,4 > (q ,segs ,xq ,xd ,n_in ,rows );break ; 583case 5 :gemm4_launch < WARPS ,R ,UNR ,5 > (q ,segs ,xq ,xd ,n_in ,rows );break ; 584case 6 :gemm4_launch < WARPS ,R ,UNR ,6 > (q ,segs ,xq ,xd ,n_in ,rows );break ; 585case 7 :gemm4_launch < WARPS ,R ,UNR ,7 > (q ,segs ,xq ,xd ,n_in ,rows );break ; 586default :gemm4_launch < WARPS ,R ,UNR ,8 > (q ,segs ,xq ,xd ,n_in ,rows );break ; 587 } 588} 589 590 591// The same GEMM on sub-groups of 16 (SIMD16: a variable takes 2 GRFs, not 4, so R rows a lane fit without spilling): the 32 lanes of the CUDA warp are two 592// sub-groups, `half` 0 for lanes 0-15 and `half` 1 for lanes 16-31 of the same row group, each lane doing the work of its virtual lane l = half * 16 + l16 exactly 593// (same steps, same fma order). The xor-16 stage of the shuffle tree is acc[l] + acc[l + 16], an add that commutes, so half 1 hands its sums over through local 594// memory and half 0 finishes the tree (8, 4, 2, 1): bitwise the result of gemm4_launch. 595template < int R ,int NC ,int PAIRS ,int UNR > 596void gemm4p_launch (sycl::queue * q ,const Df2Segs & segs ,const int8_t * xq ,const float * xd ,int n_in ,int rows ) { 597constexpr int SUBS = 8 /R ; 598const size_t pairs = (size_t ) (rows /8 )* SUBS ; 599q -> submit ([& ](sycl::handler & hd ) { 600 sycl::local_accessor < float ,1 > slm ((size_t )PAIRS * R * NC * 16 ,hd ); 601hd .parallel_for (sycl::nd_range < 1 > ((pairs /PAIRS )* PAIRS * 32 ,PAIRS * 32 ), [= ](nd_item < 1 > it )DF2_SG16 { 602const sycl::sub_group sg = it .get_sub_group (); 603const int l16 = (int )sg .get_local_linear_id (); 604const int sgi = (int ) (it .get_local_id (0 ) >>4 ); 605const int pair = sgi >>1 ,half = sgi & 1 ; 606const int lane = half * 16 + l16 ; 607const size_t gp = it .get_group (0 )* PAIRS + pair ; 608const int row_g = (int ) (gp /SUBS )* 8 ,sub = (int ) (gp %SUBS ); 609const Df2Seg sgm = df2_pick (segs ,row_g ); 610const int row0 = row_g - sgm .row0 ; 611const int nb = n_in /32 ,steps = nb /16 ; 612const sycl::uint2 * wq = reinterpret_cast < const sycl::uint2 *> (sgm .q + (size_t )row0 * (n_in /2 ))+ lane + sub * R * 32 ; 613const sycl::half * wd = sgm .d + (size_t )row0 * nb + (lane >>1 )+ sub * R * 16 ; 614const sycl::int4 * xv = reinterpret_cast < const sycl::int4 *> (xq )+ lane ; 615float acc [R ][NC ]; 616#pragma unroll 617for (int r = 0 ;r < R ;++ r ) 618#pragma unroll 619for (int c = 0 ;c < NC ;++ c )acc [r ][c ]= 0.0f ; 620#pragma unroll UNR 621for (int st = 0 ;st < steps ;++ st ) { 622int w [R ][4 ]; 623float dw [R ]; 624#pragma unroll 625for (int r = 0 ;r < R ;++ r ) { 626const sycl::uint2 raw = wq [(size_t )st * 256 + r * 32 ]; 627w [r ][0 ]= (int ) ((((raw .x ()& 0x0F0F0F0Fu ) |0x80808080u )- 0x08080808u ) ^0x80808080u ); 628w [r ][1 ]= (int ) (((((raw .x () >>4 )& 0x0F0F0F0Fu ) |0x80808080u )- 0x08080808u ) ^0x80808080u ); 629w [r ][2 ]= (int ) ((((raw .y ()& 0x0F0F0F0Fu ) |0x80808080u )- 0x08080808u ) ^0x80808080u ); 630w [r ][3 ]= (int ) (((((raw .y () >>4 )& 0x0F0F0F0Fu ) |0x80808080u )- 0x08080808u ) ^0x80808080u ); 631dw [r ]= (float )wd [(size_t )st * 128 + r * 16 ]; 632 } 633float xs [NC ]; 634#pragma unroll 635for (int c = 0 ;c < NC ;++ c )xs [c ]= xd [(size_t )c * nb + st * 16 + (lane >>1 )]; 636#pragma unroll 637for (int c = 0 ;c < NC ;++ c ) { 638const sycl::int4 x = xv [(size_t )c * (n_in /16 )+ st * 32 ]; 639#pragma unroll 640for (int r = 0 ;r < R ;++ r ) { 641int isum = STRATA_DP4A (w [r ][0 ],x .x (),0 ); 642isum = STRATA_DP4A (w [r ][1 ],x .y (),isum ); 643isum = STRATA_DP4A (w [r ][2 ],x .z (),isum ); 644isum = STRATA_DP4A (w [r ][3 ],x .w (),isum ); 645acc [r ][c ]= sycl::fma (dw [r ]* xs [c ], (float )isum ,acc [r ][c ]); 646 } 647 } 648 } 649if (half == 1 ) { 650#pragma unroll 651for (int r = 0 ;r < R ;++ r ) 652#pragma unroll 653for (int c = 0 ;c < NC ;++ c )slm [((pair * R + r )* NC + c )* 16 + l16 ]= acc [r ][c ]; 654 } 655 sycl::group_barrier (it .get_group ()); 656if (half == 0 ) { 657#pragma unroll 658for (int r = 0 ;r < R ;++ r ) 659#pragma unroll 660for (int c = 0 ;c < NC ;++ c ) { 661float v = acc [r ][c ]+ slm [((pair * R + r )* NC + c )* 16 + l16 ];// the xor-16 stage 662v += xorf (sg ,v ,8 ); 663v += xorf (sg ,v ,4 ); 664v += xorf (sg ,v ,2 ); 665v += xorf (sg ,v ,1 ); 666if (l16 == 0 )sgm .y [(size_t )c * sgm .n_out + row0 + sub * R + r ]= v ; 667 } 668 } 669 }); 670 }); 671} 672 673template < int R ,int PAIRS ,int UNR > 674void gemm4p_nc (sycl::queue * q ,const Df2Segs & segs ,const int8_t * xq ,const float * xd ,int n_in ,int rows ,int ncols ) { 675switch (ncols ) { 676case 1 :gemm4p_launch < R ,1 ,PAIRS ,UNR > (q ,segs ,xq ,xd ,n_in ,rows );break ; 677case 2 :gemm4p_launch < R ,2 ,PAIRS ,UNR > (q ,segs ,xq ,xd ,n_in ,rows );break ; 678case 3 :gemm4p_launch < R ,3 ,PAIRS ,UNR > (q ,segs ,xq ,xd ,n_in ,rows );break ; 679case 4 :gemm4p_launch < R ,4 ,PAIRS ,UNR > (q ,segs ,xq ,xd ,n_in ,rows );break ; 680case 5 :gemm4p_launch < R ,5 ,PAIRS ,UNR > (q ,segs ,xq ,xd ,n_in ,rows );break ; 681case 6 :gemm4p_launch < R ,6 ,PAIRS ,UNR > (q ,segs ,xq ,xd ,n_in ,rows );break ; 682case 7 :gemm4p_launch < R ,7 ,PAIRS ,UNR > (q ,segs ,xq ,xd ,n_in ,rows );break ; 683default :gemm4p_launch < R ,8 ,PAIRS ,UNR > (q ,segs ,xq ,xd ,n_in ,rows );break ; 684 } 685} 686 687// ---- planar quantization / norm kernels 688void quant_rows_launch (sycl::queue * q ,const float * x ,int K ,int ncols ,int8_t * xq ,float * xd ) { 689q -> parallel_for (sycl::nd_range < 2 > ({(size_t )ncols , (size_t ) ((K /32 + 7 ) /8 )* 256 }, {1 ,256 }), [= ](nd_item < 2 > it )DF2_SG32 { 690const sycl::sub_group sg = it .get_sub_group (); 691const int col = (int )it .get_group (0 ),blk = (int )it .get_group (1 )* 8 + (int ) (it .get_local_id (1 ) >>5 ),lane = (int )sg .get_local_linear_id (); 692if (blk * 32 >=K )return ; 693quant_block (sg ,x [(size_t )col * K + blk * 32 + lane ],blk ,col ,K ,xq ,xd ); 694 }); 695} 696}// namespace 697 698#ifndef DF2_Q4_AUTO 699#define DF2_Q4_AUTO (rows ) ((rows) <= 1024 ? 40 : 41) // SIMD16 pair kernels (bitwise the same results): 2 rows a lane for the big matrices, 1 for the 640-row ones 700#endif 701 702void df2_gemm (const Df2Mat * mats ,int n ,const void * xq ,const float * xd ,int n_in ,int ncols ,void * stream ) { 703if (n < 1 || n > 3 || n_in %512 != 0 || ncols < 1 || ncols > 8 )throw std::invalid_argument ("df2_gemm: unsupported shape" ); 704Df2Segs segs ; 705int rows = 0 ; 706for (int i = 0 ;i < n ;++ i ) { 707if (mats [i ].n_out %16 != 0 )throw std::invalid_argument ("df2_gemm: n_out must be a multiple of 16" ); 708if (!mats [i ].q4 )throw std::invalid_argument ("df2_gemm: the SYCL drafter runs the Q4_0 pack only" ); 709const int8_t * qw = static_cast < const int8_t *> (mats [i ].w ); 710segs .s [i ]= Df2Seg {qw ,reinterpret_cast < const sycl::half *> (qw + (size_t )mats [i ].n_out * n_in /2 ),mats [i ].y ,mats [i ].n_out ,rows }; 711rows += mats [i ].n_out ; 712 } 713segs .n = n ; 714const int8_t * xqp = static_cast < const int8_t *> (xq ); 715 sycl::queue * q = strata::q_of (stream ); 716static const int env_v = [] {const char * e = std::getenv ("STRATA_DF2_GEMM4" );return e ? std::atoi (e ) :0 ; }(); 717const int v = g_gemm_variant != 0 ?g_gemm_variant :env_v != 0 ?env_v :DF2_Q4_AUTO (rows ); 718switch (v ) {// the ids are the CUDA file's (W warps a block, R rows a warp) 719case 10 :gemm4_nc < 4 ,4 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 720case 11 :gemm4_nc < 4 ,2 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 721case 12 :gemm4_nc < 4 ,1 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 722case 24 :gemm4_nc < 1 ,2 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 723case 25 :gemm4_nc < 1 ,4 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 724case 28 :gemm4_nc < 1 ,1 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 725case 34 :gemm4_nc < 2 ,1 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 726case 17 :gemm4_nc < 2 ,2 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 727case 40 :gemm4p_nc < 1 ,2 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ;// sub-groups of 16, two per row group (R rows a lane, PAIRS pairs a work-group) 728case 41 :gemm4p_nc < 2 ,2 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 729case 42 :gemm4p_nc < 4 ,2 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 730case 43 :gemm4p_nc < 8 ,2 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ; 731default :gemm4_nc < 1 ,1 ,2 > (q ,segs ,xqp ,xd ,n_in ,rows ,ncols );break ;// 28 732 } 733} 734 735void df2_gemm_variant (int v ) {g_gemm_variant = v ; } 736void df2_attention_variant (int v ) {g_attn_variant = v ; } 737void df2_attention_chunk (int ck ) {g_attn_chunk = ck ; } 738 739void df2_quant_rows (const float * x ,int K ,int ncols ,void * xq ,float * xd ,void * stream ) { 740quant_rows_launch (strata::q_of (stream ),x ,K ,ncols ,static_cast < int8_t *> (xq ),xd ); 741} 742 743// RMS norm of row `col` (N values) then the planar quantization of the normalized values; also writes them when xn != null. One group of 256 per row. 744void df2_rms_quant (const float * h ,const float * gamma ,float * xn ,int rows ,int N ,float eps ,void * xq ,float * xd ,void * stream ) { 745 sycl::queue * q = strata::q_of (stream ); 746int8_t * xqi = static_cast < int8_t *> (xq ); 747q -> submit ([& ](sycl::handler & hd ) { 748 sycl::local_accessor < float ,1 > red (8 ,hd ); 749hd .parallel_for (sycl::nd_range < 1 > ((size_t )rows * 256 ,256 ), [= ](nd_item < 1 > it )DF2_SG32 { 750const sycl::sub_group sg = it .get_sub_group (); 751const int col = (int )it .get_group (0 ),tid = (int )it .get_local_id (0 ),warp = tid >>5 ,lane = tid & 31 ; 752float ss = 0.0f ; 753for (int i = tid ;i < N ;i += 256 ) {const float v = h [(size_t )col * N + i ];ss += v * v ; } 754for (int o = 16 ;o > 0 ;o >>=1 )ss += xorf (sg ,ss ,o ); 755if (lane == 0 )red [warp ]= ss ; 756bar (it ); 757float tot = 0.0f ; 758#pragma unroll 759for (int w = 0 ;w < 8 ;++ w )tot += red [w ]; 760const float scale = sycl::rsqrt (tot / (float )N + eps ); 761for (int blk = warp ;blk * 32 < N ;blk += 8 ) { 762const int i = blk * 32 + lane ; 763const float v = h [(size_t )col * N + i ]* scale * gamma [i ]; 764if (xn )xn [(size_t )col * N + i ]= v ; 765quant_block (sg ,v ,blk ,col ,N ,xqi ,xd ); 766 } 767 }); 768 }); 769} 770 771// conv (as df2_conv, no residual) then the planar quantization: grid (ceil(H / 32 / 8), rows), 256 work-items 772void df2_conv_quant (const float * x ,const float * dyn ,int dyn_stride ,const float * base ,int rows ,int H ,int group ,void * xq ,float * xd ,void * stream ) { 773int8_t * xqi = static_cast < int8_t *> (xq ); 774 strata::q_of (stream )-> parallel_for (sycl::nd_range < 2 > ({(size_t )rows , (size_t ) ((H /32 + 7 ) /8 )* 256 }, {1 ,256 }), [= ](nd_item < 2 > it )DF2_SG32 { 775const sycl::sub_group sg = it .get_sub_group (); 776const int b = (int )it .get_group (0 ),blk = (int )it .get_group (1 )* 8 + (int ) (it .get_local_id (1 ) >>5 ),lane = (int )sg .get_local_linear_id (); 777if (blk * 32 >=H )return ; 778const int c = blk * 32 + lane ,groups = H /group ; 779float acc = 0.0f ; 780#pragma unroll 781for (int o = 0 ;o < 2 ;++ o ) { 782if (b - o < 0 )continue ; 783const float k = base [o * H + c ]+ dyn [(size_t )b * dyn_stride + o * groups + c /group ]; 784acc = sycl::fma (k ,x [(size_t ) (b - o )* H + c ],acc ); 785 } 786quant_block (sg ,acc ,blk ,b ,H ,xqi ,xd ); 787 }); 788} 789 790// h += conv(x) (the finish convolution with its residual), then RMS norm of the new h with `gamma` -> xn (optional) and the planar quantization. 791// One group of 256 per row: a sub-group owns whole 32-blocks (80 of them for 2560, ten each), so the quantization needs no exchange. 792void df2_conv_rms_quant (const float * x ,const float * dyn ,int dyn_stride ,const float * base ,float * h ,const float * gamma ,float * xn ,int rows ,int H ,int group , 793float eps ,void * xq ,float * xd ,void * stream ) { 794constexpr int MAXJ = 12 ;// H <= 3072 -> 96 blocks -> 12 per sub-group 795if (H > 3072 )throw std::invalid_argument ("df2_conv_rms_quant: H too large" ); 796 sycl::queue * q = strata::q_of (stream ); 797int8_t * xqi = static_cast < int8_t *> (xq ); 798q -> submit ([& ](sycl::handler & hd ) { 799 sycl::local_accessor < float ,1 > red (8 ,hd ); 800hd .parallel_for (sycl::nd_range < 1 > ((size_t )rows * 256 ,256 ), [= ](nd_item < 1 > it )DF2_SG32 { 801const sycl::sub_group sg = it .get_sub_group (); 802const int b = (int )it .get_group (0 ),tid = (int )it .get_local_id (0 ),warp = tid >>5 ,lane = tid & 31 ; 803const int groups = H /group ,nb = H /32 ; 804float v [MAXJ ]; 805float ss = 0.0f ; 806#pragma unroll 807for (int j = 0 ;j < MAXJ ;++ j ) { 808const int blk = warp + j * 8 ; 809v [j ]= 0.0f ; 810if (blk < nb ) { 811const int c = blk * 32 + lane ; 812float acc = 0.0f ; 813#pragma unroll 814for (int o = 0 ;o < 2 ;++ o ) { 815if (b - o < 0 )continue ; 816const float k = base [o * H + c ]+ dyn [(size_t )b * dyn_stride + o * groups + c /group ]; 817acc = sycl::fma (k ,x [(size_t ) (b - o )* H + c ],acc ); 818 } 819v [j ]= h [(size_t )b * H + c ]+ acc ; 820h [(size_t )b * H + c ]= v [j ]; 821ss += v [j ]* v [j ]; 822 } 823 } 824for (int o = 16 ;o > 0 ;o >>=1 )ss += xorf (sg ,ss ,o ); 825if (lane == 0 )red [warp ]= ss ; 826bar (it ); 827float tot = 0.0f ; 828#pragma unroll 829for (int w = 0 ;w < 8 ;++ w )tot += red [w ]; 830const float scale = sycl::rsqrt (tot / (float )H + eps ); 831#pragma unroll 832for (int j = 0 ;j < MAXJ ;++ j ) { 833const int blk = warp + j * 8 ; 834if (blk < nb ) { 835const int c = blk * 32 + lane ; 836const float y = v [j ]* scale * gamma [c ]; 837if (xn )xn [(size_t )b * H + c ]= y ; 838quant_block (sg ,y ,blk ,b ,H ,xqi ,xd ); 839 } 840 } 841 }); 842 }); 843} 844 845// q and k of the block: per (row, head) RMS norm and rope; group y < q_heads is a q head (-> qo, row-major rows), the rest k heads (-> ko) 846void df2_norm_rope_qk (const float * q ,const float * k ,const float * gq ,const float * gk ,const float * table ,float * qo ,float * ko ,int rows ,int q_heads ,int k_heads , 847float eps ,void * stream ) { 848 sycl::queue * qu = strata::q_of (stream ); 849qu -> submit ([& ](sycl::handler & hd ) { 850 sycl::local_accessor < float ,1 > red (4 ,hd ),y (HD ,hd ); 851hd .parallel_for (sycl::nd_range < 2 > ({(size_t ) (q_heads + k_heads ), (size_t )rows * HD }, {1 ,HD }), [= ](nd_item < 2 > it )DF2_SG32 { 852const int hh = (int )it .get_group (0 ),r = (int )it .get_group (1 ),d = (int )it .get_local_id (1 ); 853const bool isq = hh < q_heads ; 854const int h = isq ?hh :hh - q_heads ,heads = isq ?q_heads :k_heads ; 855const float * in = isq ?q :k ; 856const float * gamma = isq ?gq :gk ; 857float * out = isq ?qo :ko ; 858const float x = in [((size_t )r * heads + h )* HD + d ]; 859const float tot = head_norm (it ,red ,x ,eps ); 860const float v = x * sycl::rsqrt (tot / (float )HD + eps )* gamma [d ]; 861y [d ]= v ; 862bar (it ); 863const int i = d & 63 ; 864const float c = table [(r * 2 + 0 )* 64 + i ],sn = table [(r * 2 + 1 )* 64 + i ]; 865out [((size_t )r * heads + h )* HD + d ]= d < 64 ?v * c - y [d + 64 ]* sn :v * c + y [d - 64 ]* sn ; 866 }); 867 }); 868} 869 870// the context update's ring write: K norm + rope of (row, head) and the V copy, both into the head-major ring slot pos % slots 871void df2_kv_ring (const float * kin ,const float * vin ,const float * gk ,const int32_t * pos ,const float * table ,float * kring ,float * vring ,int slots ,int rows ,int heads , 872float eps ,void * stream ,int64_t * tag ,const int32_t * epoch_ptr ) { 873 sycl::queue * qu = strata::q_of (stream ); 874qu -> submit ([& ](sycl::handler & hd ) { 875 sycl::local_accessor < float ,1 > red (4 ,hd ),y (HD ,hd ); 876hd .parallel_for (sycl::nd_range < 2 > ({(size_t )heads , (size_t )rows * HD }, {1 ,HD }), [= ](nd_item < 2 > it )DF2_SG32 { 877const int h = (int )it .get_group (0 ),r = (int )it .get_group (1 ),d = (int )it .get_local_id (1 ); 878const float x = kin [((size_t )r * heads + h )* HD + d ]; 879const float tot = head_norm (it ,red ,x ,eps ); 880const float v = x * sycl::rsqrt (tot / (float )HD + eps )* gk [d ]; 881y [d ]= v ; 882bar (it ); 883const int i = d & 63 ; 884const float c = table [(r * 2 + 0 )* 64 + i ],sn = table [(r * 2 + 1 )* 64 + i ]; 885const int slot = (int ) (pos [r ] %slots ); 886const size_t at = ((size_t )h * slots + slot )* HD + d ; 887kring [at ]= d < 64 ?v * c - y [d + 64 ]* sn :v * c + y [d - 64 ]* sn ; 888vring [at ]= vin [((size_t )r * heads + h )* HD + d ]; 889if (tag != nullptr && h == 0 && d == 0 )tag [slot ]= ((int64_t )* epoch_ptr <<32 ) | (int64_t )pos [r ]; 890 }); 891 }); 892} 893 894// silu(gate) * up then the planar quantization: grid (ceil(I / 32 / 8), rows), 256 work-items 895void df2_swiglu_quant (const float * gate ,const float * up ,int rows ,int I ,void * xq ,float * xd ,void * stream ) { 896int8_t * xqi = static_cast < int8_t *> (xq ); 897 strata::q_of (stream )-> parallel_for (sycl::nd_range < 2 > ({(size_t )rows , (size_t ) ((I /32 + 7 ) /8 )* 256 }, {1 ,256 }), [= ](nd_item < 2 > it )DF2_SG32 { 898const sycl::sub_group sg = it .get_sub_group (); 899const int b = (int )it .get_group (0 ),blk = (int )it .get_group (1 )* 8 + (int ) (it .get_local_id (1 ) >>5 ),lane = (int )sg .get_local_linear_id (); 900if (blk * 32 >=I )return ; 901const size_t i = (size_t )b * I + blk * 32 + lane ; 902const float g = gate [i ]; 903quant_block (sg ,g / (1.0f + sycl::exp (- g ))* up [i ],blk ,b ,I ,xqi ,xd ); 904 }); 905} 906 907// ggml Q4_0 blocks (18 bytes: fp16 d, 16 bytes with value i in the low nibble of byte i and value i + 16 in the high nibble) of a [n_out][n_in] matrix -> the 908// nibble plane (n_out * n_in / 2 bytes, the layout above) and the tiled d plane; one work-item per block 909void df2_q4_tile (const void * blocks_ ,void * qs_ ,void * dpl_ ,int n_in ,int64_t count ,void * stream ) { 910const uint8_t * blocks = static_cast < const uint8_t *> (blocks_ ); 911uint8_t * qs = static_cast < uint8_t *> (qs_ ); 912uint16_t * dpl = static_cast < uint16_t *> (dpl_ ); 913 strata::q_of (stream )-> parallel_for (sycl::nd_range < 1 > ((size_t ) ((count + 255 ) /256 )* 256 ,256 ), [= ](nd_item < 1 > it ) { 914const int64_t b = (int64_t )it .get_global_id (0 ); 915if (b >=count )return ; 916const int nb = n_in /32 ; 917const int64_t row = b /nb ; 918const int blk = (int ) (b %nb ); 919const int64_t g = row /8 ; 920const int r = (int ) (row %8 ),st = blk /16 ,bi = blk %16 ; 921const uint8_t * src = blocks + b * 18 ; 922dpl [(g * 8 * nb )+ (int64_t )st * 128 + r * 16 + bi ]= (uint16_t )src [0 ] | ((uint16_t )src [1 ] <<8 ); 923#pragma unroll 924for (int half = 0 ;half < 2 ;++ half ) {// values half * 16 .. +15 of the block 925uint32_t wd [2 ]; 926#pragma unroll 927for (int w = 0 ;w < 2 ;++ w ) {// word w holds values w * 8 .. w * 8 + 7 of the half: byte p = (value w*8+p, value w*8+4+p) as (low, high) nibble 928uint32_t v = 0 ; 929#pragma unroll 930for (int p = 0 ;p < 4 ;++ p ) { 931const uint8_t b_lo = src [2 + w * 8 + p ],b_hi = src [2 + w * 8 + 4 + p ]; 932const uint32_t q_lo = half == 0 ? (b_lo & 15u ) : (b_lo >>4 ),q_hi = half == 0 ? (b_hi & 15u ) : (b_hi >>4 ); 933v |= (q_lo | (q_hi <<4 )) << (p * 8 ); 934 } 935wd [w ]= v ; 936 } 937uint8_t * o = qs + g * 8 * (n_in /2 )+ (int64_t )st * 2048 + r * 256 + (bi * 2 + half )* 8 ; 938reinterpret_cast < sycl::uint2 *> (o )[0 ]= sycl::uint2 (wd [0 ],wd [1 ]); 939 } 940 }); 941} 942 943void df2_q8_tile (const void * ,void * ,void * ,int ,int64_t ,void * ) { 944throw std::invalid_argument ("df2_q8_tile: the SYCL drafter runs the Q4_0 pack only" ); 945} 946 947// ---- top-16 948namespace { 949constexpr int TK_CHUNK_MAX = 4096 ; 950inline bool better (float va ,int ia ,float vb ,int ib ) {return va > vb || (va == vb && ia < ib ); } 951} 952 953void df2_topk (const float * logits ,int n ,const int32_t * idx_map ,int rows ,uint32_t * scratch ,float * val ,int32_t * tok ,void * stream ) { 954if ((n + kDf2TopBlocks - 1 ) /kDf2TopBlocks > TK_CHUNK_MAX )throw std::invalid_argument ("df2_topk: too many logits per row" ); 955float * cval = reinterpret_cast < float *> (scratch ); 956int32_t * cidx = reinterpret_cast < int32_t *> (scratch + (size_t )rows * kDf2TopBlocks * kDf2TopK ); 957 sycl::queue * q = strata::q_of (stream ); 958q -> submit ([& ](sycl::handler & hd ) { 959 sycl::local_accessor < float ,1 > vals (TK_CHUNK_MAX ,hd ),wv (8 ,hd ); 960 sycl::local_accessor < int ,1 > wi (8 ,hd ); 961hd .parallel_for (sycl::nd_range < 2 > ({(size_t )rows , (size_t )kDf2TopBlocks * 256 }, {1 ,256 }), [= ](nd_item < 2 > it )DF2_SG32 { 962const sycl::sub_group sg = it .get_sub_group (); 963const int row = (int )it .get_group (0 ),blk = (int )it .get_group (1 ),tid = (int )it .get_local_id (1 ); 964const int cs = (n + kDf2TopBlocks - 1 ) /kDf2TopBlocks ; 965const int c0 = blk * cs ,c1 = sycl::min (c0 + cs ,n ); 966for (int i = tid ;i < cs ;i += 256 )vals [i ]= (c0 + i < c1 ) ?logits [(size_t )row * n + c0 + i ] :- INFINITY ; 967bar (it ); 968for (int k = 0 ;k < kDf2TopK ;++ k ) { 969float bv = - INFINITY ; 970int bi = 0x7fffffff ; 971for (int i = tid ;i < cs ;i += 256 ) 972if (better (vals [i ],i ,bv ,bi )) {bv = vals [i ];bi = i ; } 973for (int o = 16 ;o > 0 ;o >>=1 ) { 974const float ov = xorf (sg ,bv ,o ); 975const int oi = xori (sg ,bi ,o ); 976if (better (ov ,oi ,bv ,bi )) {bv = ov ;bi = oi ; } 977 } 978if ((tid & 31 )== 0 ) {wv [tid >>5 ]= bv ;wi [tid >>5 ]= bi ; } 979bar (it ); 980if (tid == 0 ) { 981float v = wv [0 ]; 982int ix = wi [0 ]; 983for (int w = 1 ;w < 8 ;++ w ) 984if (better (wv [w ],wi [w ],v ,ix )) {v = wv [w ];ix = wi [w ]; } 985const size_t o = ((size_t )row * kDf2TopBlocks + blk )* kDf2TopK + k ; 986cval [o ]= v ; 987cidx [o ]= v == - INFINITY ?-1 :c0 + ix ; 988if (v != - INFINITY )vals [ix ]= - INFINITY ; 989 } 990bar (it ); 991 } 992 }); 993 }); 994q -> submit ([& ](sycl::handler & hd ) { 995constexpr int NC = kDf2TopBlocks * kDf2TopK ; 996 sycl::local_accessor < float ,1 > sv (NC ,hd ),wv (8 ,hd ); 997 sycl::local_accessor < int ,1 > si (NC ,hd ),wi (8 ,hd ),wp (8 ,hd ); 998hd .parallel_for (sycl::nd_range < 1 > ((size_t )rows * 256 ,256 ), [= ](nd_item < 1 > it )DF2_SG32 { 999const sycl::sub_group sg = it .get_sub_group (); 1000const int row = (int )it .get_group (0 ),tid = (int )it .get_local_id (0 ); 1001for (int i = tid ;i < NC ;i += 256 ) {sv [i ]= cval [(size_t )row * NC + i ];si [i ]= cidx [(size_t )row * NC + i ]; } 1002bar (it ); 1003for (int k = 0 ;k < kDf2TopK ;++ k ) { 1004float bv = - INFINITY ; 1005int bi = 0x7fffffff ,bp = -1 ; 1006for (int i = tid ;i < NC ;i += 256 ) 1007if (si [i ] >=0 && better (sv [i ],si [i ],bv ,bi )) {bv = sv [i ];bi = si [i ];bp = i ; } 1008for (int o = 16 ;o > 0 ;o >>=1 ) { 1009const float ov = xorf (sg ,bv ,o ); 1010const int oi = xori (sg ,bi ,o ); 1011const int op = xori (sg ,bp ,o ); 1012if (better (ov ,oi ,bv ,bi )) {bv = ov ;bi = oi ;bp = op ; } 1013 } 1014if ((tid & 31 )== 0 ) {wv [tid >>5 ]= bv ;wi [tid >>5 ]= bi ;wp [tid >>5 ]= bp ; } 1015bar (it ); 1016if (tid == 0 ) { 1017float v = wv [0 ]; 1018int ix = wi [0 ],p = wp [0 ]; 1019for (int w = 1 ;w < 8 ;++ w ) 1020if (better (wv [w ],wi [w ],v ,ix )) {v = wv [w ];ix = wi [w ];p = wp [w ]; } 1021if (p >=0 )si [p ]= -1 ; 1022val [row * kDf2TopK + k ]= v ; 1023tok [row * kDf2TopK + k ]= p >=0 ? (idx_map ?idx_map [ix ] :ix ) :0 ; 1024 } 1025bar (it ); 1026 } 1027 }); 1028 }); 1029} 1030 1031// ---- the selector's walk: one group of 256 work-items 1032void df2_select (const float * hp ,const float * unary ,const int32_t * tok ,const uint16_t * pred_cb ,const uint16_t * succ_cb ,int rank ,int rows , 1033const int32_t * anchor_tok ,int32_t * out_tok ,float * out_prob ,void * stream ,bool cb_q4 ) { 1034if (cb_q4 )throw std::invalid_argument ("df2_select: Q4_0 selector codebooks are not on SYCL (the loader keeps the bf16 pack)" ); 1035if (rank != 256 )throw std::invalid_argument ("df2_select: the selector rank must be 256" ); 1036 sycl::queue * q = strata::q_of (stream ); 1037q -> submit ([& ](sycl::handler & hd ) { 1038 sycl::local_accessor < float ,1 > vp (256 ,hd ),sc (kDf2TopK ,hd ); 1039 sycl::local_accessor < int ,1 > prev (1 ,hd ); 1040hd .parallel_for (sycl::nd_range < 1 > (256 ,256 ), [= ](nd_item < 1 > it )DF2_SG32 { 1041const sycl::sub_group sg = it .get_sub_group (); 1042const int tid = (int )it .get_local_id (0 ),w = tid >>5 ,lane = tid & 31 ; 1043if (tid == 0 )prev [0 ]= * anchor_tok ; 1044bar (it ); 1045for (int i = 0 ;i < rows ;++ i ) { 1046vp [tid ]= bf16f (pred_cb [(size_t )prev [0 ]* rank + tid ])* hp [(size_t )i * rank + tid ]; 1047bar (it ); 1048for (int j = w * 2 ;j < w * 2 + 2 ;++ j ) { 1049const int t = tok [i * kDf2TopK + j ]; 1050const sycl::uint4 raw = * reinterpret_cast < const sycl::uint4 *> (succ_cb + (size_t )t * rank + lane * 8 ); 1051const uint32_t u [4 ]= {raw .x (),raw .y (),raw .z (),raw .w ()}; 1052float acc = 0.0f ; 1053#pragma unroll 1054for (int e = 0 ;e < 4 ;++ e ) { 1055acc = sycl::fma (sycl::bit_cast < float > (u [e ] <<16 ),vp [lane * 8 + e * 2 ],acc ); 1056acc = sycl::fma (sycl::bit_cast < float > (u [e ]& 0xffff0000u ),vp [lane * 8 + e * 2 + 1 ],acc ); 1057 } 1058for (int o = 16 ;o > 0 ;o >>=1 )acc += xorf (sg ,acc ,o ); 1059if (lane == 0 )sc [j ]= unary [i * kDf2TopK + j ]+ acc ; 1060 } 1061bar (it ); 1062if (tid == 0 ) { 1063int bj = 0 ; 1064float bv = sc [0 ]; 1065for (int j = 1 ;j < kDf2TopK ;++ j ) 1066if (sc [j ]> bv ) {bv = sc [j ];bj = j ; } 1067float z = 0.0f ; 1068for (int j = 0 ;j < kDf2TopK ;++ j )z += sycl::exp (sc [j ]- bv ); 1069out_tok [i ]= tok [i * kDf2TopK + bj ]; 1070out_prob [i ]= 1.0f /z ; 1071prev [0 ]= out_tok [i ]; 1072 } 1073bar (it ); 1074 } 1075 }); 1076 }); 1077} 1078 1079void df2_tap_rows (const float * R ,float * out ,int64_t out_stride ,int64_t n_embd ,int hc ,int rows ,void * stream ) { 1080 strata::q_of (stream )-> parallel_for (sycl::nd_range < 2 > ({(size_t )rows , (size_t ) ((n_embd + 255 ) /256 )* 256 }, {1 ,256 }), [= ](nd_item < 2 > it ) { 1081const int64_t i = (int64_t )it .get_group (1 )* 256 + (int64_t )it .get_local_id (1 ); 1082const int r = (int )it .get_group (0 ); 1083if (i >=n_embd )return ; 1084float s = 0.0f ; 1085for (int c = 0 ;c < hc ;++ c )s += R [((size_t )r * hc + c )* n_embd + i ]; 1086out [(size_t )r * out_stride + i ]= s / (float )hc ; 1087 }); 1088} 1089 1090void df2_tap_gr (const float * R ,int64_t R_stride ,const float * bo ,int64_t bo_stride ,const float * inject ,int64_t inj_stride ,float * out ,int64_t out_stride ,int rows , 1091int64_t n_embd ,int hc ,void * stream ) { 1092 strata::q_of (stream )-> parallel_for (sycl::nd_range < 2 > ({(size_t )rows , (size_t ) ((n_embd + 255 ) /256 )* 256 }, {1 ,256 }), [= ](nd_item < 2 > it ) { 1093const int64_t i = (int64_t )it .get_group (1 )* 256 + (int64_t )it .get_local_id (1 ); 1094const int r = (int )it .get_group (0 ); 1095if (i >=n_embd )return ; 1096float wsum = 0.0f ,rs = 0.0f ; 1097for (int c = 0 ;c < hc ;++ c ) { 1098const float x = inject [(size_t )r * inj_stride + c ] / (float )hc ; 1099wsum += 2.0f / (1.0f + sycl::exp (- x )); 1100rs += R [(size_t )r * R_stride + (size_t )c * n_embd + i ]; 1101 } 1102out [(size_t )r * out_stride + i ]= (rs + bo [(size_t )r * bo_stride + i ]* wsum ) / (float )hc ; 1103 }); 1104} 1105 1106}// namespace strata::kernels