okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a8a51b280
main
1#include <sycl/ext/intel/experimental/grf_size_properties.hpp> 2// src/kernels/cuda/verify_kernels.cu - see include/strata/kernels/verify_kernels.hpp. 3// 4// The per-token arithmetic of every kernel here is transcribed from its single-token original (fused_gdn.cu, 5// elementwise.cu) with the same operation order, so a verify window reproduces plain decode bit for bit. 6#define DPCT_PROFILING_ENABLED 7#include <sycl/sycl.hpp> 8#include "strata/kprof.hpp" 9#include <sycl/ext/intel/experimental/grf_size_properties.hpp> 10#include "strata/core/geometry.hpp" 11#include <dpct/dpct.hpp> 12#include "strata/sycl_queue.hpp" 13#include "strata/sycl_doorbell.hpp" 14#include "strata/kernels/verify_kernels.hpp" 15#include "strata/kernels/resident_plan_mirror.hpp" 16#include "strata/kernels/dp4a.hpp" 17 18#include <algorithm> 19#include <cstdio> 20#include <cstdlib> 21#include <cmath> 22#include <atomic> 23#include <chrono> 24#include <memory> 25#include <mutex> 26#include <stdexcept> 27#include <thread> 28#include <vector> 29 30#ifndef STRATA_PLAN_LOCAL 31#define STRATA_PLAN_LOCAL 1 // 0: the original one-thread plan kernel (A/B) 32#endif 33namespace strata::kernels { 34namespace { 35 36constexpr int S = 128 ;// GDN state size 37static_assert(S == strata::core::kCompiledGeometry .ssm_state_size ,"verify_kernels is compiled for core/geometry.hpp" ); 38constexpr int RG = 4 ; 39constexpr int RPG = S /RG ; 40 41void check (const char * what ) { 42/* 43DPCT1010: SYCL uses exceptions to report errors and does not use the 44error codes. The cudaGetLastError function call was replaced with 0. You 45need to rewrite this code. 46*/ 47const dpct::err0 e = 0 ; 48/* 49DPCT1009: SYCL reports errors using exceptions and does not use error 50codes. Please replace the "get_error_string_dummy(...)" with a real 51error-handling function. 52*/ 53} 54 55// `commit` (one token): the history then keeps it - [hist1, hist2, x_0], each thread its channel's, after reading them 56__dpct_inline__ void gdn_conv_l2_multi_kernel (float * hist , 57const float * __restrict__qkv , 58const float * __restrict__w , 59float * __restrict__h ,int C , 60int qk_heads ,float eps , 61int t_begin ,bool commit ) { 62auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 63auto & part = 64* sycl::ext::oneapi::group_local_memory_for_overwrite < float [S /32 ]> ( 65 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 66const int t = t_begin + item_ct1 .get_group (1 ); 67const int c = item_ct1 .get_group (2 )* S + item_ct1 .get_local_id (2 ); 68// the window of token t: [hist0, hist1, hist2, x_0, ..., x_t], its last four entries 69float win [3 ]; 70#pragma unroll 71for (int j = 0 ;j < 3 ;++ j ) { 72const int src = t + j ;// index into [hist(3) | x...] 73win [j ]= src < 3 ?hist [c * 3 + src ] :qkv [(size_t ) (src - 3 )* C + c ]; 74 } 75const float v0 = win [0 ],v1 = win [1 ],v2 = win [2 ],x = qkv [(size_t )t * C + c ]; 76if (commit ) { 77hist [c * 3 ]= v1 ; 78hist [c * 3 + 1 ]= v2 ; 79hist [c * 3 + 2 ]= x ; 80 } 81float sum = v0 * w [c * 4 ]+ v1 * w [c * 4 + 1 ]+ v2 * w [c * 4 + 2 ]+ x * w [c * 4 + 3 ]; 82float y = sum / (1.0f + sycl::native::exp (- sum )); 83if ((int )item_ct1 .get_group (2 )< qk_heads ) { 84float sq = y * y ; 85/* 86DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 87masked sub_group function which may not be supported by all compilers or 88runtimes. You may need to adjust the code. 89*/ 90#pragma unroll 91for (int o = 16 ;o > 0 ;o >>=1 )sq += 92 dpct::experimental::permute_sub_group_by_xor ( 930xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 94sq ,o ); 95if ((item_ct1 .get_local_id (2 )& 31 )== 0 ) 96part [item_ct1 .get_local_id (2 ) >>5 ]= sq ; 97/* 98DPCT1118: SYCL group functions and algorithms must be encountered in 99converged control flow. You may need to adjust the code. 100*/ 101/* 102DPCT1065: Consider replacing sycl::nd_item::barrier() with 103sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 104better performance if there is no access to global memory. 105*/ 106item_ct1 .barrier (); 107const float ss = part [0 ]+ part [1 ]+ part [2 ]+ part [3 ]; 108y *= sycl::rsqrt (ss + eps ); 109 } 110h [(size_t )t * C + c ]= y ; 111} 112 113__dpct_inline__ void 114gdn_conv_commit_kernel (float * __restrict__hist ,const float * __restrict__qkv , 115int C ,const int32_t * __restrict__n_keep ) { 116auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 117const int c = item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 118item_ct1 .get_local_id (2 ); 119if (c >=C )return ; 120const int n = * n_keep ; 121if (n <=0 )return ; 122float seq [3 ]; 123#pragma unroll 124for (int j = 0 ;j < 3 ;++ j ) { 125const int src = n + j ;// the last three of [hist(3) | x_0..x_{n-1}] 126seq [j ]= src < 3 ?hist [c * 3 + src ] :qkv [(size_t ) (src - 3 )* C + c ]; 127 } 128hist [c * 3 ]= seq [0 ]; 129hist [c * 3 + 1 ]= seq [1 ]; 130hist [c * 3 + 2 ]= seq [2 ]; 131} 132 133template < int MAX_T = kVerifyMaxT ,bool EXACT_T = false> 134/* 135DPCT1110: The total declared local variable size in device function 136gdn_ab_multi_kernel exceeds 128 bytes and may cause high register pressure. 137Consult with your hardware vendor to find the total register size available and 138adjust the code, or use smaller sub-group size to avoid high register pressure. 139*/ 140__dpct_inline__ void gdn_ab_multi_kernel ( 141const float * __restrict__x ,const uint16_t * __restrict__wa , 142const uint16_t * __restrict__wb ,const float * __restrict__dt , 143const float * __restrict__ssm_a ,float * __restrict__gate , 144float * __restrict__beta ,int n ,int h_v ,int T ) { 145auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 146const int row = item_ct1 .get_group (2 )* 2 + (item_ct1 .get_local_id (2 ) >>5 ), 147lane = item_ct1 .get_local_id (2 )& 31 ; 148if (row >=2 * h_v )return ; 149const bool is_beta = row >=h_v ; 150const int r = is_beta ?row - h_v :row ; 151const sycl::uint4 * w4 = reinterpret_cast < const sycl::uint4 *> ( 152 (is_beta ?wb :wa )+ (size_t )r * n ); 153float acc [MAX_T ]; 154#pragma unroll 155for (int t = 0 ;t < MAX_T ;++ t )acc [t ]= 0.0f ; 156for (int j = lane ;j < n /8 ;j += 32 ) { 157/* 158DPCT1098: The '*' expression is used instead of the __ldg call. 159These two expressions do not provide the exact same functionality. Check 160the generated code for potential precision and/or performance issues. 161*/ 162const sycl::uint4 wv = * (w4 + j ); 163const float w0 = sycl::bit_cast < float > (wv .x () <<16 ), 164w1 = sycl::bit_cast < float > (wv .x ()& 0xffff0000u ); 165const float w2 = sycl::bit_cast < float > (wv .y () <<16 ), 166w3 = sycl::bit_cast < float > (wv .y ()& 0xffff0000u ); 167const float w4f = sycl::bit_cast < float > (wv .z () <<16 ), 168w5 = sycl::bit_cast < float > (wv .z ()& 0xffff0000u ); 169const float w6 = sycl::bit_cast < float > (wv .w () <<16 ), 170w7 = sycl::bit_cast < float > (wv .w ()& 0xffff0000u ); 171#pragma unroll 172for (int t = 0 ;t < MAX_T ;++ t ) { 173if (!EXACT_T && t >=T )break ; 174const float * xt = x + (size_t )t * n ; 175/* 176DPCT1098: The '*' expression is used instead of the __ldg call. 177These two expressions do not provide the exact same functionality. 178Check the generated code for potential precision and/or performance 179issues. 180*/ 181const sycl::float4 xa = * reinterpret_cast < const sycl::float4 *> (xt + j * 8 ); 182/* 183DPCT1098: The '*' expression is used instead of the __ldg call. 184These two expressions do not provide the exact same functionality. 185Check the generated code for potential precision and/or performance 186issues. 187*/ 188const sycl::float4 xb = * reinterpret_cast < const sycl::float4 *> (xt + j * 8 + 4 ); 189float a = acc [t ]; 190a = sycl::fma ((float )w0 , (float )(xa .x ()),a ); 191a = sycl::fma ((float )w1 , (float )(xa .y ()),a ); 192a = sycl::fma ((float )w2 , (float )(xa .z ()),a ); 193a = sycl::fma ((float )w3 , (float )(xa .w ()),a ); 194a = sycl::fma ((float )w4f , (float )(xb .x ()),a ); 195a = sycl::fma ((float )w5 , (float )(xb .y ()),a ); 196a = sycl::fma ((float )w6 , (float )(xb .z ()),a ); 197a = sycl::fma ((float )w7 , (float )(xb .w ()),a ); 198acc [t ]= a ; 199 } 200 } 201#pragma unroll 202for (int t = 0 ;t < MAX_T ;++ t ) { 203if (!EXACT_T && t >=T )break ; 204float a = acc [t ]; 205#pragma unroll 206/* 207DPCT1108: '__shfl_xor_sync' was migrated with the experimental 208feature masked sub_group function which may not be supported by all 209compilers or runtimes. You may need to adjust the code. 210*/ 211for (int o = 16 ;o > 0 ;o >>=1 )a += 212 dpct::experimental::permute_sub_group_by_xor ( 2130xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 214a ,o ); 215if (lane != 0 )continue ; 216if (is_beta ) { 217beta [(size_t )t * h_v + r ]= 1.0f / (1.0f + sycl::native::exp (- a )); 218 }else { 219const float v = a + dt [r ]; 220const float sp = v > 20.0f ?v : sycl::log1p (sycl::native::exp (v )); 221gate [(size_t )t * h_v + r ]= sp * ssm_a [r ]; 222 } 223 } 224} 225 226// State-only commit kernel: each head's 128 independent state columns are split across 4 blocks of 32 columns 227// (48 * 4 = 192 blocks of 128 threads across all SMs, vs 48 blocks of 512 threads), with no sq/o/norm/z/y work. 228/* 229DPCT1110: The total declared local variable size in device function 230gdn_step_commit_kernel exceeds 128 bytes and may cause high register pressure. 231Consult with your hardware vendor to find the total register size available and 232adjust the code, or use smaller sub-group size to avoid high register pressure. 233*/ 234__dpct_inline__ void gdn_step_commit_kernel ( 235float * __restrict__state ,const float * __restrict__hbuf ,int C , 236const float * __restrict__gate ,const float * __restrict__beta ,int h_k , 237int h_v ,const int32_t * __restrict__n_keep ) { 238auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 239const int n = * n_keep ; 240if (n <=0 )return ; 241auto & sk = * sycl::ext::oneapi::group_local_memory_for_overwrite < float [S ]> ( 242 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 243auto & red = 244* sycl::ext::oneapi::group_local_memory_for_overwrite < float [RG ][32 ]> ( 245 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 246const int head = item_ct1 .get_group (2 ); 247const int col_local = item_ct1 .get_local_id (2 ); 248const int col = item_ct1 .get_group (1 )* 32 + col_local ; 249const int rg = item_ct1 .get_local_id (1 ); 250const int tid = rg * 32 + col_local ;// 0 .. 127 251const int qh = head %h_k ; 252const int qk = S * h_k ; 253float s [RPG ]; 254float * base = state + ((size_t ) (rg * RPG )* h_v + head )* S + col ; 255const size_t row_stride = (size_t )h_v * S ; 256#pragma unroll 257for (int r = 0 ;r < RPG ;++ r )s [r ]= base [r * row_stride ]; 258for (int t = 0 ;t < n ;++ t ) { 259const float * ht = hbuf + (size_t )t * C ; 260/* 261DPCT1118: SYCL group functions and algorithms must be encountered in 262converged control flow. You may need to adjust the code. 263*/ 264/* 265DPCT1065: Consider replacing sycl::nd_item::barrier() with 266sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 267better performance if there is no access to global memory. 268*/ 269item_ct1 .barrier (); 270sk [tid ]= ht [qk + qh * S + tid ]; 271/* 272DPCT1118: SYCL group functions and algorithms must be encountered in 273converged control flow. You may need to adjust the code. 274*/ 275/* 276DPCT1065: Consider replacing sycl::nd_item::barrier() with 277sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 278better performance if there is no access to global memory. 279*/ 280item_ct1 .barrier (); 281const float g = sycl::native::exp (gate [(size_t )t * h_v + head ]); 282float kv = 0.0f ; 283#pragma unroll 284for (int r = 0 ;r < RPG ;++ r ) 285kv = sycl::fma (s [r ],sk [rg * RPG + r ],kv ); 286red [rg ][col_local ]= kv ; 287/* 288DPCT1118: SYCL group functions and algorithms must be encountered in 289converged control flow. You may need to adjust the code. 290*/ 291/* 292DPCT1065: Consider replacing sycl::nd_item::barrier() with 293sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 294better performance if there is no access to global memory. 295*/ 296item_ct1 .barrier (); 297const float kv_col = red [0 ][col_local ]+ red [1 ][col_local ]+ red [2 ][col_local ]+ red [3 ][col_local ]; 298const float delta = (ht [2 * qk + head * S + col ]- g * kv_col )* beta [(size_t )t * h_v + head ]; 299#pragma unroll 300for (int r = 0 ;r < RPG ;++ r ) 301s [r ]= sycl::fma ((float )g ,s [r ],sk [rg * RPG + r ]* delta ); 302 } 303#pragma unroll 304for (int r = 0 ;r < RPG ;++ r )base [r * row_stride ]= s [r ]; 305} 306 307// S26 (STRATA_QFUSE=1): the q8_1 image of a 32-column block, written by the warp that holds those columns with 308// native_quantize_q8_1_kernel's quantizer (the same XOR-tree max and sum, d = amax / 127, roundf(x / d), ds = (d, sum)). 309struct GdnQ81 { sycl::half2 ds ;int8_t qs [32 ]; }; 310__dpct_inline__ void gdn_q8_1_store (GdnQ81 * __restrict__xq ,size_t idx , 311float xi ) { 312// no contraction: xi is a product here, and fma(a, b, shfl) would round the first sum unlike the separate 313// quantizer, which reads xi from memory (S26 harness: ds.sum differed by 1 ulp without this) 314#pragma clang fp contract(off) 315float amax = sycl::fabs (xi ),sum = xi ; 316#pragma unroll 317/* 318DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 319masked sub_group function which may not be supported by all compilers or 320runtimes. You may need to adjust the code. 321*/ 322for (int o = 16 ;o > 0 ;o >>=1 )amax = sycl::fmax ( 323amax , dpct::experimental::permute_sub_group_by_xor ( 3240xffffffffu , 325 sycl::ext::oneapi::this_work_item::get_sub_group (),amax ,o )); 326#pragma unroll 327/* 328DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 329masked sub_group function which may not be supported by all compilers or 330runtimes. You may need to adjust the code. 331*/ 332for (int o = 16 ;o > 0 ;o >>=1 )sum += 333 dpct::experimental::permute_sub_group_by_xor ( 3340xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 335sum ,o ); 336const float d = amax /127.0f ; 337const int8_t q = amax == 0.0f ?0 : sycl::round (xi /d ); 338xq [idx /32 ].qs [idx %32 ]= q ; 339if (idx %32 == 0 )xq [idx /32 ].ds = sycl::half2 (d ,sum ); 340} 341 342template < bool ALL_OUT ,bool Q ,bool QK_REG = false> 343/* 344DPCT1110: The total declared local variable size in device function 345gdn_step_norm_multi_kernel exceeds 128 bytes and may cause high register 346pressure. Consult with your hardware vendor to find the total register size 347available and adjust the code, or use smaller sub-group size to avoid high 348register pressure. 349*/ 350__dpct_inline__ void gdn_step_norm_multi_kernel ( 351float * __restrict__state ,const float * __restrict__hbuf ,int C , 352const float * __restrict__gate ,const float * __restrict__beta , 353const float * __restrict__z ,const float * __restrict__gamma ,float eps , 354float * __restrict__y ,int h_k ,int h_v ,int T , 355const int32_t * __restrict__n_keep ,int t_out_begin , 356GdnQ81 * __restrict__xq ,GdnGroups grp = {}) { 357auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 358if ( grp . n > 0 ) { // group get_group(1) - its slot's state and its rows of the window's buffers 359const int gi = item_ct1 . get_group ( 1 ), f = grp . first [ gi ]; 360state = grp . state [ gi ]; 361hbuf += (size_t) f * C ; 362gate += (size_t) f * h_v ; 363beta += (size_t) f * h_v ; 364z += (size_t) f * S * h_v ; 365y += (size_t) f * S * h_v ; 366T = grp . ntok [ gi ]; 367n_keep = grp . keep [ gi ]; 368} 369auto & sk = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ 2 ][ S ] > ( 370sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 371auto & sq = 372* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ 2 ][ S ] > ( 373sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 374auto & red_kv = 375* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ RG ][ S ] > ( 376sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 377auto & red_o = 378* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ RG ][ S ] > ( 379sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 380auto & wsum = 381* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ S / 32 ] > ( 382sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 383const int head = item_ct1 . get_group ( 2 ); 384const int col = item_ct1 . get_local_id ( 2 ); 385const int rg = item_ct1 . get_local_id ( 1 ); 386const int qh = head % h_k ; 387const int qk = S * h_k ; // q at [0, qk), k at [qk, 2qk), v at [2qk, ...) 388const int value_dim = S * h_v ; 389const int n = ALL_OUT ? T : ( n_keep ? * n_keep : T ); 390if (! QK_REG && n > 0 ) { 391if ( rg == 0 ) sk [ 0 ][ col ] = hbuf [ qk + qh * S + col ]; 392else if ( rg == 1 && ( ALL_OUT || 0 >= t_out_begin )) sq [ 0 ][ col ] = hbuf [ qh * S + col ]; 393} 394const float gam = ( rg == 0 ) ? gamma [ col ] : 0.0f ; 395float s [ RPG ]; 396float * base = state + ((size_t) ( rg * RPG ) * h_v + head ) * S + col ; 397const size_t row_stride = (size_t) h_v * S ; 398#pragma unroll 399/* 400DPCT1098: The '*' expression is used instead of the __ldg call. These 401two expressions do not provide the exact same functionality. Check the 402generated code for potential precision and/or performance issues. 403*/ 404for (int r = 0 ; r < RPG ; ++ r ) s [ r ] = ALL_OUT ? base [ r * row_stride ] 405: base [ r * row_stride ]; 406for (int t = 0 ; t < n ; ++ t ) { 407const int cur = t & 1 , nxt = ( t + 1 ) & 1 ; 408const float * ht = hbuf + (size_t) t * C ; 409const bool need_out = ALL_OUT || ( t >= t_out_begin ); 410/* 411DPCT1118: SYCL group functions and algorithms must be encountered in 412converged control flow. You may need to adjust the code. 413*/ 414/* 415DPCT1065: Consider replacing sycl::nd_item::barrier() with 416sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 417better performance if there is no access to global memory. 418*/ 419item_ct1 . barrier (); 420float k_own = 0.0f , q_own = 0.0f ; 421auto sg = item_ct1 . get_sub_group (); 422if constexpr ( QK_REG ) { 423const int i = rg * RPG + sg . get_local_linear_id (); 424k_own = ht [ qk + qh * S + i ]; 425if ( need_out ) q_own = ht [ qh * S + i ]; 426} 427if (! QK_REG && t + 1 < n ) { 428const float * ht_next = ht + C ; 429const bool need_next = ALL_OUT || ( t + 1 >= t_out_begin ); 430if ( rg == 0 ) sk [ nxt ][ col ] = ht_next [ qk + qh * S + col ]; 431else if ( rg == 1 && need_next ) sq [ nxt ][ col ] = ht_next [ qh * S + col ]; 432} 433const float g = sycl::native:: exp ( gate [(size_t) t * h_v + head ]); 434float kv = 0.0f ; 435#pragma unroll 436for (int r = 0 ; r < RPG ; ++ r ) 437kv = sycl:: fma ( s [ r ], QK_REG ? sycl:: select_from_group ( sg , k_own , r ) : sk [ cur ][ rg * RPG + r ], kv ); 438red_kv [ rg ][ col ] = kv ; 439/* 440DPCT1118: SYCL group functions and algorithms must be encountered in 441converged control flow. You may need to adjust the code. 442*/ 443/* 444DPCT1065: Consider replacing sycl::nd_item::barrier() with 445sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 446better performance if there is no access to global memory. 447*/ 448item_ct1 . barrier (); 449const float kv_col = red_kv [ 0 ][ col ] + red_kv [ 1 ][ col ] + red_kv [ 2 ][ col ] + red_kv [ 3 ][ col ]; 450const float delta = ( ht [ 2 * qk + head * S + col ] - g * kv_col ) * beta [(size_t) t * h_v + head ]; 451if (! need_out ) { 452#pragma unroll 453for (int r = 0 ; r < RPG ; ++ r ) 454s [ r ] = sycl:: fma ((float) g , s [ r ], 455( QK_REG ? sycl:: select_from_group ( sg , k_own , r ) : sk [ cur ][ rg * RPG + r ]) * delta ); 456continue ; 457} 458float o = 0.0f ; 459#pragma unroll 460for (int r = 0 ; r < RPG ; ++ r ) { 461s [ r ] = sycl:: fma ((float) g , s [ r ], 462( QK_REG ? sycl:: select_from_group ( sg , k_own , r ) : sk [ cur ][ rg * RPG + r ]) * delta ); 463o = sycl:: fma ( s [ r ], QK_REG ? sycl:: select_from_group ( sg , q_own , r ) : sq [ cur ][ rg * RPG + r ], o ); 464} 465red_o [ rg ][ col ] = o ; 466/* 467DPCT1118: SYCL group functions and algorithms must be encountered in 468converged control flow. You may need to adjust the code. 469*/ 470/* 471DPCT1065: Consider replacing sycl::nd_item::barrier() with 472sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 473better performance if there is no access to global memory. 474*/ 475item_ct1 . barrier (); 476float oc = 0.0f , sq_part = 0.0f ; 477if ( rg == 0 ) { 478oc = ( red_o [ 0 ][ col ] + red_o [ 1 ][ col ] + red_o [ 2 ][ col ] + 479red_o [ 3 ][ col ]) * 480sycl:: rsqrt ((float) S ); 481sq_part = oc * oc ; 482} 483// every thread takes the shuffles, as the single-token kernel does: inside the `rg == 0` branch hipcc on 484// gfx1151 gave another rounding for 3% of the outputs (gdn_parity section 5 on Aurora) 485/* 486DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 487masked sub_group function which may not be supported by all compilers or 488runtimes. You may need to adjust the code. 489*/ 490#pragma unroll 491for (int o2 = 16 ; o2 > 0 ; o2 >>= 1 ) sq_part += 492dpct::experimental:: permute_sub_group_by_xor ( 4930xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), 494sq_part , o2 ); 495if ( rg == 0 && ( col & 31 ) == 0 ) wsum [ col >> 5 ] = sq_part ; 496/* 497DPCT1118: SYCL group functions and algorithms must be encountered in 498converged control flow. You may need to adjust the code. 499*/ 500/* 501DPCT1065: Consider replacing sycl::nd_item::barrier() with 502sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 503better performance if there is no access to global memory. 504*/ 505item_ct1 . barrier (); 506if ( rg == 0 ) { 507const float ss = wsum [ 0 ] + wsum [ 1 ] + wsum [ 2 ] + wsum [ 3 ]; 508const float scale = sycl:: rsqrt ( ss / (float) S + eps ); 509const float zz = z [(size_t) t * value_dim + head * S + col ]; 510const float yv = 511oc * scale * gam * ( 1.0f / ( 1.0f + sycl::native:: exp ( - zz ))); 512y [(size_t) t * value_dim + head * S + col ] = yv ; 513if constexpr ( Q ) gdn_q8_1_store ( xq , (size_t) ( t - t_out_begin ) * value_dim + head * S + col , yv ); 514} 515} 516if (! ALL_OUT && n_keep != nullptr && n > 0 ) { 517#pragma unroll 518for (int r = 0 ; r < RPG ; ++ r ) base [ r * row_stride ] = s [ r ]; 519} 520} 521 522// S25 (STRATA_GDN_SPLIT=1): the same recurrence with each head's 128 state columns over 4 blocks of 32 columns 523// (128 threads: the same (column, row group) threads, each with the same 32 state rows), so 4x the blocks of 524// gdn_step_norm_multi_kernel - which ran one 512-thread block per head (32 blocks) and read the 2 MB state of a layer 525// at ~60 GB/s. Every column's arithmetic is the original's; the output norm, which needs all 128 columns of a head, 526// moves to gdn_out_norm_kernel: this kernel leaves the unnormalized output `oc` in y and the second kernel sums the 527// squares by the same warps (columns 32w..32w+31, the same butterfly) in the same order. 528// S26: OUT = false (the commit, whose outputs nobody reads) drops the output reduction and its two barriers; the 529// norm kernel below has the old kernel's code shape (S x RG threads, rg 0 holding oc), which makes it bitwise equal - 530// the 128-thread version differed from the old kernel by 1 ulp in ~7% of the outputs. 531constexpr int GS_COLS = 32 ; 532template < bool OUT > 533/* 534DPCT1110: The total declared local variable size in device function 535gdn_step_split_kernel exceeds 128 bytes and may cause high register pressure. 536Consult with your hardware vendor to find the total register size available and 537adjust the code, or use smaller sub-group size to avoid high register pressure. 538*/ 539__dpct_inline__ void 540gdn_step_split_kernel (float * __restrict__ state , const float * __restrict__ hbuf , 541int C , const float * __restrict__ gate , 542const float * __restrict__ beta , float * __restrict__ y , 543int h_k , int h_v , int T , 544const int32_t * __restrict__ n_keep , int t_out_begin ) { 545auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 546auto & sk = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ S ] > ( 547sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 548auto & sq = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ S ] > ( 549sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 550auto & red = * sycl::ext::oneapi:: group_local_memory_for_overwrite < 551float[ RG ][ GS_COLS ] > ( 552sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 553const int head = item_ct1 . get_group ( 2 ); 554const int c0 = item_ct1 . get_group ( 1 ) * GS_COLS ; 555const int lc = item_ct1 . get_local_id ( 2 ); // local column 556const int col = c0 + lc ; 557const int rg = item_ct1 . get_local_id ( 1 ); 558const int tid = rg * GS_COLS + lc ; 559const int qh = head % h_k ; 560const int qk = S * h_k ; 561const int value_dim = S * h_v ; 562const int n = n_keep ? * n_keep : T ; 563float s [ RPG ]; 564float * base = state + ((size_t) ( rg * RPG ) * h_v + head ) * S + col ; 565const size_t row_stride = (size_t) h_v * S ; 566#pragma unroll 567for (int r = 0 ; r < RPG ; ++ r ) s [ r ] = base [ r * row_stride ]; 568for (int t = 0 ; t < n ; ++ t ) { 569const float * ht = hbuf + (size_t) t * C ; 570/* 571DPCT1118: SYCL group functions and algorithms must be encountered in 572converged control flow. You may need to adjust the code. 573*/ 574/* 575DPCT1065: Consider replacing sycl::nd_item::barrier() with 576sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 577better performance if there is no access to global memory. 578*/ 579item_ct1 . barrier (); 580if ( tid < S ) { sk [ tid ] = ht [ qk + qh * S + tid ]; sq [ tid ] = ht [ qh * S + tid ]; } 581/* 582DPCT1118: SYCL group functions and algorithms must be encountered in 583converged control flow. You may need to adjust the code. 584*/ 585/* 586DPCT1065: Consider replacing sycl::nd_item::barrier() with 587sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 588better performance if there is no access to global memory. 589*/ 590item_ct1 . barrier (); 591const float g = sycl::native:: exp ( gate [(size_t) t * h_v + head ]); 592float kv = 0.0f ; 593#pragma unroll 594for (int r = 0 ; r < RPG ; ++ r ) 595kv = sycl:: fma ( s [ r ], sk [ rg * RPG + r ], kv ); 596red [ rg ][ lc ] = kv ; 597/* 598DPCT1118: SYCL group functions and algorithms must be encountered in 599converged control flow. You may need to adjust the code. 600*/ 601/* 602DPCT1065: Consider replacing sycl::nd_item::barrier() with 603sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 604better performance if there is no access to global memory. 605*/ 606item_ct1 . barrier (); 607const float kv_col = red [ 0 ][ lc ] + red [ 1 ][ lc ] + red [ 2 ][ lc ] + red [ 3 ][ lc ]; 608const float delta = ( ht [ 2 * qk + head * S + col ] - g * kv_col ) * beta [(size_t) t * h_v + head ]; 609float o = 0.0f ; 610#pragma unroll 611for (int r = 0 ; r < RPG ; ++ r ) { 612s [ r ] = sycl:: fma ((float) g , s [ r ], sk [ rg * RPG + r ] * delta ); 613o = sycl:: fma ( s [ r ], sq [ rg * RPG + r ], o ); 614} 615if (! OUT ) continue ; 616/* 617DPCT1118: SYCL group functions and algorithms must be encountered in 618converged control flow. You may need to adjust the code. 619*/ 620/* 621DPCT1065: Consider replacing sycl::nd_item::barrier() with 622sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 623better performance if there is no access to global memory. 624*/ 625item_ct1 . barrier (); 626red [ rg ][ lc ] = o ; 627/* 628DPCT1118: SYCL group functions and algorithms must be encountered in 629converged control flow. You may need to adjust the code. 630*/ 631/* 632DPCT1065: Consider replacing sycl::nd_item::barrier() with 633sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 634better performance if there is no access to global memory. 635*/ 636item_ct1 . barrier (); 637if ( rg == 0 && t >= t_out_begin ) 638y [(size_t) t * value_dim + head * S + col ] = 639( red [ 0 ][ lc ] + red [ 1 ][ lc ] + red [ 2 ][ lc ] + red [ 3 ][ lc ]) * 640sycl:: rsqrt ((float) S ); 641} 642if ( n_keep != nullptr && n > 0 ) { 643#pragma unroll 644for (int r = 0 ; r < RPG ; ++ r ) base [ r * row_stride ] = s [ r ]; 645} 646} 647 648 649template < bool OUT > 650/* 651DPCT1110: The total declared local variable size in device function 652gdn_step_split_kernel exceeds 128 bytes and may cause high register pressure. 653Consult with your hardware vendor to find the total register size available and 654adjust the code, or use smaller sub-group size to avoid high register pressure. 655*/ 656__dpct_inline__ void 657b60_gdn_split_local_kernel (float * __restrict__ state , const float * __restrict__ hbuf , 658int C , const float * __restrict__ gate , 659const float * __restrict__ beta , float * __restrict__ y , 660int h_k , int h_v , int T , 661const int32_t * __restrict__ n_keep , int t_out_begin ) { 662auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 663auto & sk = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ S ] > ( 664sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 665auto & sq = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ S ] > ( 666sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 667auto & red = * sycl::ext::oneapi:: group_local_memory_for_overwrite < 668float[ RG ][ GS_COLS ] > ( 669sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 670const int head = item_ct1 . get_group ( 2 ); 671const int c0 = item_ct1 . get_group ( 1 ) * GS_COLS ; 672const int lc = item_ct1 . get_local_id ( 2 ); // local column 673const int col = c0 + lc ; 674const int rg = item_ct1 . get_local_id ( 1 ); 675const int tid = rg * GS_COLS + lc ; 676const int qh = head % h_k ; 677const int qk = S * h_k ; 678const int value_dim = S * h_v ; 679const int n = n_keep ? * n_keep : T ; 680float s [ RPG ]; 681float * base = state + ((size_t) ( rg * RPG ) * h_v + head ) * S + col ; 682const size_t row_stride = (size_t) h_v * S ; 683#pragma unroll 684for (int r = 0 ; r < RPG ; ++ r ) s [ r ] = base [ r * row_stride ]; 685for (int t = 0 ; t < n ; ++ t ) { 686const float * ht = hbuf + (size_t) t * C ; 687/* 688DPCT1118: SYCL group functions and algorithms must be encountered in 689converged control flow. You may need to adjust the code. 690*/ 691/* 692DPCT1065: Consider replacing sycl::nd_item::barrier() with 693sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 694better performance if there is no access to global memory. 695*/ 696item_ct1 . barrier (sycl::access::fence_space:: local_space ); 697if ( tid < S ) { sk [ tid ] = ht [ qk + qh * S + tid ]; sq [ tid ] = ht [ qh * S + tid ]; } 698/* 699DPCT1118: SYCL group functions and algorithms must be encountered in 700converged control flow. You may need to adjust the code. 701*/ 702/* 703DPCT1065: Consider replacing sycl::nd_item::barrier() with 704sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 705better performance if there is no access to global memory. 706*/ 707item_ct1 . barrier (sycl::access::fence_space:: local_space ); 708const float g = sycl::native:: exp ( gate [(size_t) t * h_v + head ]); 709float kv = 0.0f ; 710#pragma unroll 711for (int r = 0 ; r < RPG ; ++ r ) 712kv = sycl:: fma ( s [ r ], sk [ rg * RPG + r ], kv ); 713red [ rg ][ lc ] = kv ; 714/* 715DPCT1118: SYCL group functions and algorithms must be encountered in 716converged control flow. You may need to adjust the code. 717*/ 718/* 719DPCT1065: Consider replacing sycl::nd_item::barrier() with 720sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 721better performance if there is no access to global memory. 722*/ 723item_ct1 . barrier (sycl::access::fence_space:: local_space ); 724const float kv_col = red [ 0 ][ lc ] + red [ 1 ][ lc ] + red [ 2 ][ lc ] + red [ 3 ][ lc ]; 725const float delta = ( ht [ 2 * qk + head * S + col ] - g * kv_col ) * beta [(size_t) t * h_v + head ]; 726float o = 0.0f ; 727#pragma unroll 728for (int r = 0 ; r < RPG ; ++ r ) { 729s [ r ] = sycl:: fma ((float) g , s [ r ], sk [ rg * RPG + r ] * delta ); 730o = sycl:: fma ( s [ r ], sq [ rg * RPG + r ], o ); 731} 732if (! OUT ) continue ; 733/* 734DPCT1118: SYCL group functions and algorithms must be encountered in 735converged control flow. You may need to adjust the code. 736*/ 737/* 738DPCT1065: Consider replacing sycl::nd_item::barrier() with 739sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 740better performance if there is no access to global memory. 741*/ 742item_ct1 . barrier (sycl::access::fence_space:: local_space ); 743red [ rg ][ lc ] = o ; 744/* 745DPCT1118: SYCL group functions and algorithms must be encountered in 746converged control flow. You may need to adjust the code. 747*/ 748/* 749DPCT1065: Consider replacing sycl::nd_item::barrier() with 750sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 751better performance if there is no access to global memory. 752*/ 753item_ct1 . barrier (sycl::access::fence_space:: local_space ); 754if ( rg == 0 && t >= t_out_begin ) 755y [(size_t) t * value_dim + head * S + col ] = 756( red [ 0 ][ lc ] + red [ 1 ][ lc ] + red [ 2 ][ lc ] + red [ 3 ][ lc ]) * 757sycl:: rsqrt ((float) S ); 758} 759if ( n_keep != nullptr && n > 0 ) { 760#pragma unroll 761for (int r = 0 ; r < RPG ; ++ r ) base [ r * row_stride ] = s [ r ]; 762} 763} 764 765 766// S26: the state-only replay (the commit) with its per-token inputs prefetched - gate, beta and the v column of every 767// row up front, the next row's k / q while the current one computes. The arithmetic and its order are 768// gdn_step_split_kernel<false>'s (gfx1151 harness: state bitwise equal; 44.5 -> 39.1 us at 1 row, 47.4 -> 40.5 at 2, 769// 53.8 -> 42.9 at 4, h_v 48). With outputs the prefetch lost (register pressure), so the window keeps the plain split. 770template < int MAXT > 771/* 772DPCT1110: The total declared local variable size in device function 773gdn_state_replay_kernel exceeds 128 bytes and may cause high register pressure. 774Consult with your hardware vendor to find the total register size available and 775adjust the code, or use smaller sub-group size to avoid high register pressure. 776*/ 777__dpct_inline__ void gdn_state_replay_kernel ( 778float * __restrict__ state , const float * __restrict__ hbuf , int C , 779const float * __restrict__ gate , const float * __restrict__ beta , int h_k , 780int h_v , int T , const int32_t * __restrict__ n_keep ) { 781auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 782auto & sk = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ S ] > ( 783sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 784auto & sq = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ S ] > ( 785sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 786auto & red = * sycl::ext::oneapi:: group_local_memory_for_overwrite < 787float[ RG ][ GS_COLS ] > ( 788sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 789const int head = item_ct1 . get_group ( 2 ); 790const int c0 = item_ct1 . get_group ( 1 ) * GS_COLS ; 791const int lc = item_ct1 . get_local_id ( 2 ); 792const int col = c0 + lc ; 793const int rg = item_ct1 . get_local_id ( 1 ); 794const int tid = rg * GS_COLS + lc ; 795const int qh = head % h_k ; 796const int qk = S * h_k ; 797const int n = n_keep ? * n_keep : T ; 798float gv [ MAXT ], bv [ MAXT ], vv [ MAXT ]; 799#pragma unroll 800for (int t = 0 ; t < MAXT ; ++ t ) { 801if ( t < n ) { 802gv [ t ] = gate [(size_t) t * h_v + head ]; 803bv [ t ] = beta [(size_t) t * h_v + head ]; 804vv [ t ] = hbuf [(size_t) t * C + 2 * qk + head * S + col ]; 805} 806} 807float kn = 0.0f , qn = 0.0f ; 808if ( n > 0 ) { kn = hbuf [ qk + qh * S + tid ]; qn = hbuf [ qh * S + tid ]; } 809float s [ RPG ]; 810float * base = state + ((size_t) ( rg * RPG ) * h_v + head ) * S + col ; 811const size_t row_stride = (size_t) h_v * S ; 812#pragma unroll 813for (int r = 0 ; r < RPG ; ++ r ) s [ r ] = base [ r * row_stride ]; 814#pragma unroll 815for (int t = 0 ; t < MAXT ; ++ t ) { 816if ( t >= n ) break ; 817/* 818DPCT1118: SYCL group functions and algorithms must be encountered in 819converged control flow. You may need to adjust the code. 820*/ 821/* 822DPCT1065: Consider replacing sycl::nd_item::barrier() with 823sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 824better performance if there is no access to global memory. 825*/ 826item_ct1 . barrier (); 827sk [ tid ] = kn ; sq [ tid ] = qn ; 828/* 829DPCT1118: SYCL group functions and algorithms must be encountered in 830converged control flow. You may need to adjust the code. 831*/ 832/* 833DPCT1065: Consider replacing sycl::nd_item::barrier() with 834sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 835better performance if there is no access to global memory. 836*/ 837item_ct1 . barrier (); 838if ( t + 1 < n ) { 839const float * hn = hbuf + (size_t) ( t + 1 ) * C ; 840kn = hn [ qk + qh * S + tid ]; qn = hn [ qh * S + tid ]; 841} 842const float g = sycl::native:: exp ( gv [ t ]); 843float kv = 0.0f ; 844#pragma unroll 845for (int r = 0 ; r < RPG ; ++ r ) 846kv = sycl:: fma ( s [ r ], sk [ rg * RPG + r ], kv ); 847red [ rg ][ lc ] = kv ; 848/* 849DPCT1118: SYCL group functions and algorithms must be encountered in 850converged control flow. You may need to adjust the code. 851*/ 852/* 853DPCT1065: Consider replacing sycl::nd_item::barrier() with 854sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 855better performance if there is no access to global memory. 856*/ 857item_ct1 . barrier (); 858const float kv_col = red [ 0 ][ lc ] + red [ 1 ][ lc ] + red [ 2 ][ lc ] + red [ 3 ][ lc ]; 859const float delta = ( vv [ t ] - g * kv_col ) * bv [ t ]; 860float o = 0.0f ; // unused: kept because the measured code had it (without it: 51 us instead of 39 at 1 row) 861#pragma unroll 862for (int r = 0 ; r < RPG ; ++ r ) { 863s [ r ] = sycl:: fma ((float) g , s [ r ], sk [ rg * RPG + r ] * delta ); 864o = sycl:: fma ( s [ r ], sq [ rg * RPG + r ], o ); 865} 866(void) o ; 867} 868if ( n_keep != nullptr && n > 0 ) { 869#pragma unroll 870for (int r = 0 ; r < RPG ; ++ r ) base [ r * row_stride ] = s [ r ]; 871} 872} 873// y = oc * rsqrt(mean(oc^2) + eps) * gamma * sigmoid(z) per head and token: gdn_step_norm_multi_kernel's tail with its 874// code shape (S x RG threads; rg 0 holds oc, the other row groups contribute 0 to wsum), reading oc from y 875template < bool Q > 876__dpct_inline__ void gdn_out_norm_kernel ( 877const float * __restrict__ z , const float * __restrict__ gamma , float eps , 878float * __restrict__ y , int h_v , int T , const int32_t * __restrict__ n_keep , 879int t_out_begin , GdnQ81 * __restrict__ xq ) { 880auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 881auto & wsum = 882* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ S * RG / 32 ] > ( 883sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 884const int head = item_ct1 . get_group ( 2 ), 885t = item_ct1 . get_group ( 1 ) + t_out_begin , 886col = item_ct1 . get_local_id ( 2 ), rg = item_ct1 . get_local_id ( 1 ); 887const int tid = rg * S + col ; 888const int n = n_keep ? * n_keep : T ; 889if ( t >= n ) return ; 890const int value_dim = S * h_v ; 891float oc = 0.0f , sq_part = 0.0f ; 892if ( rg == 0 ) { 893oc = y [(size_t) t * value_dim + head * S + col ]; 894sq_part = oc * oc ; 895} 896/* 897DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 898masked sub_group function which may not be supported by all compilers or 899runtimes. You may need to adjust the code. 900*/ 901#pragma unroll 902for (int o2 = 16 ; o2 > 0 ; o2 >>= 1 ) sq_part += 903dpct::experimental:: permute_sub_group_by_xor ( 9040xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), 905sq_part , o2 ); 906if (( tid & 31 ) == 0 ) wsum [ tid >> 5 ] = sq_part ; 907/* 908DPCT1065: Consider replacing sycl::nd_item::barrier() with 909sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 910performance if there is no access to global memory. 911*/ 912item_ct1 . barrier (); 913if ( rg == 0 ) { 914const float ss = wsum [ 0 ] + wsum [ 1 ] + wsum [ 2 ] + wsum [ 3 ]; 915const float scale = sycl:: rsqrt ( ss / (float) S + eps ); 916const float zz = z [(size_t) t * value_dim + head * S + col ]; 917if constexpr ( Q ) { 918const float yv = oc * scale * gamma [ col ] * 919( 1.0f / ( 1.0f + sycl::native:: exp ( - zz ))); 920y [(size_t) t * value_dim + head * S + col ] = yv ; 921gdn_q8_1_store ( xq , (size_t) ( t - t_out_begin ) * value_dim + head * S + col , yv ); 922} else { 923y [(size_t) t * value_dim + head * S + col ] = 924oc * scale * gamma [ col ] * 925( 1.0f / ( 1.0f + sycl::native:: exp ( - zz ))); 926} 927} 928} 929 930// Commit has no output consumer. Keep the state update order. 931__dpct_inline__ void gdn_commit_state_multi_kernel ( 932float * __restrict__ state , const float * __restrict__ hbuf , int C , 933const float * __restrict__ gate , const float * __restrict__ beta , 934int h_k , int h_v , const int32_t * __restrict__ n_keep ) { 935auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 936auto & sk = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ S ] > ( 937sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 938auto & red = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ RG ][ S ] > ( 939sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 940const int head = item_ct1 . get_group ( 2 ), col = item_ct1 . get_local_id ( 2 ); 941const int rg = item_ct1 . get_local_id ( 1 ), tid = rg * S + col ; 942const int qh = head % h_k , qk = S * h_k ; 943const int n = * n_keep ; 944float s [ RPG ]; 945float * base = state + ((size_t)( rg * RPG ) * h_v + head ) * S + col ; 946const size_t row_stride = (size_t) h_v * S ; 947#pragma unroll 948for (int r = 0 ; r < RPG ; ++ r ) s [ r ] = base [ r * row_stride ]; 949for (int t = 0 ; t < n ; ++ t ) { 950const float * ht = hbuf + (size_t) t * C ; 951item_ct1 . barrier (); 952if ( tid < S ) sk [ tid ] = ht [ qk + qh * S + tid ]; 953item_ct1 . barrier (); 954const float g = sycl::native:: exp ( gate [(size_t) t * h_v + head ]); 955float kv = 0.0f ; 956#pragma unroll 957for (int r = 0 ; r < RPG ; ++ r ) kv = sycl:: fma ( s [ r ], sk [ rg * RPG + r ], kv ); 958red [ rg ][ col ] = kv ; 959item_ct1 . barrier (); 960const float kv_col = red [ 0 ][ col ] + red [ 1 ][ col ] + red [ 2 ][ col ] + red [ 3 ][ col ]; 961const float delta = ( ht [ 2 * qk + head * S + col ] - g * kv_col ) * beta [(size_t) t * h_v + head ]; 962#pragma unroll 963for (int r = 0 ; r < RPG ; ++ r ) 964s [ r ] = sycl:: fma ((float) g , s [ r ], sk [ rg * RPG + r ] * delta ); 965} 966if ( n > 0 ) { 967#pragma unroll 968for (int r = 0 ; r < RPG ; ++ r ) base [ r * row_stride ] = s [ r ]; 969} 970} 971 972__dpct_inline__ void gdn_step_norm_qkreg_kernel ( 973float * __restrict__ state , const float * __restrict__ hbuf , int C , 974const float * __restrict__ gate , const float * __restrict__ beta , 975const float * __restrict__ z , const float * __restrict__ gamma , float eps , 976float * __restrict__ y , int h_k , int h_v , int T , 977const int32_t * __restrict__ n_keep , int t_out_begin ) { 978auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 979auto & red = 980* sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ RG ][ S ] > ( 981sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 982auto & wsum = * sycl::ext::oneapi:: group_local_memory_for_overwrite < 983float[ S * RG / 32 ] > ( 984sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 985const int head = item_ct1 . get_group ( 2 ); 986const int col = item_ct1 . get_local_id ( 2 ); 987const int rg = item_ct1 . get_local_id ( 1 ); 988const int tid = rg * S + col ; 989auto sg = item_ct1 . get_sub_group (); 990const int lane = sg . get_local_linear_id (); 991const int qh = head % h_k ; 992const int qk = S * h_k ; // q at [0, qk), k at [qk, 2qk), v at [2qk, ...) 993const int value_dim = S * h_v ; 994const int n = n_keep ? * n_keep : T ; 995float s [ RPG ]; 996float * base = state + ((size_t) ( rg * RPG ) * h_v + head ) * S + col ; 997const size_t row_stride = (size_t) h_v * S ; 998#pragma unroll 999for (int r = 0 ; r < RPG ; ++ r ) s [ r ] = base [ r * row_stride ]; 1000for (int t = 0 ; t < n ; ++ t ) { 1001const float * ht = hbuf + (size_t) t * C ; 1002/* 1003DPCT1118: SYCL group functions and algorithms must be encountered in 1004converged control flow. You may need to adjust the code. 1005*/ 1006/* 1007DPCT1065: Consider replacing sycl::nd_item::barrier() with 1008sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 1009better performance if there is no access to global memory. 1010*/ 1011item_ct1 . barrier (); // the previous token is done with sk/sq/red/wsum 1012const float k_own = ht [ qk + qh * S + rg * RPG + lane ]; 1013const float q_own = ht [ qh * S + rg * RPG + lane ]; 1014const float g = sycl::native:: exp ( gate [(size_t) t * h_v + head ]); 1015float kv = 0.0f ; 1016#pragma unroll 1017for (int r = 0 ; r < RPG ; ++ r ) 1018kv = sycl:: fma ( s [ r ], sycl:: select_from_group ( sg , k_own , r ), kv ); 1019red [ rg ][ col ] = kv ; 1020/* 1021DPCT1118: SYCL group functions and algorithms must be encountered in 1022converged control flow. You may need to adjust the code. 1023*/ 1024/* 1025DPCT1065: Consider replacing sycl::nd_item::barrier() with 1026sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 1027better performance if there is no access to global memory. 1028*/ 1029item_ct1 . barrier (); 1030const float kv_col = red [ 0 ][ col ] + red [ 1 ][ col ] + red [ 2 ][ col ] + red [ 3 ][ col ]; 1031const float delta = ( ht [ 2 * qk + head * S + col ] - g * kv_col ) * beta [(size_t) t * h_v + head ]; 1032float o = 0.0f ; 1033#pragma unroll 1034for (int r = 0 ; r < RPG ; ++ r ) { 1035s [ r ] = sycl:: fma ((float) g , s [ r ], sycl:: select_from_group ( sg , k_own , r ) * delta ); 1036o = sycl:: fma ( s [ r ], sycl:: select_from_group ( sg , q_own , r ), o ); 1037} 1038/* 1039DPCT1118: SYCL group functions and algorithms must be encountered in 1040converged control flow. You may need to adjust the code. 1041*/ 1042/* 1043DPCT1065: Consider replacing sycl::nd_item::barrier() with 1044sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 1045better performance if there is no access to global memory. 1046*/ 1047item_ct1 . barrier (); 1048red [ rg ][ col ] = o ; 1049/* 1050DPCT1118: SYCL group functions and algorithms must be encountered in 1051converged control flow. You may need to adjust the code. 1052*/ 1053/* 1054DPCT1065: Consider replacing sycl::nd_item::barrier() with 1055sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 1056better performance if there is no access to global memory. 1057*/ 1058item_ct1 . barrier (); 1059float oc = 0.0f , sq_part = 0.0f ; 1060if ( rg == 0 ) { 1061oc = ( red [ 0 ][ col ] + red [ 1 ][ col ] + red [ 2 ][ col ] + red [ 3 ][ col ]) * 1062sycl:: rsqrt ((float) S ); 1063sq_part = oc * oc ; 1064} 1065if ( t < t_out_begin ) continue ; // a replayed token: its state update is needed, its output is not 1066/* 1067DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 1068masked sub_group function which may not be supported by all compilers or 1069runtimes. You may need to adjust the code. 1070*/ 1071#pragma unroll 1072for (int o2 = 16 ; o2 > 0 ; o2 >>= 1 ) sq_part += 1073dpct::experimental:: permute_sub_group_by_xor ( 10740xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), 1075sq_part , o2 ); 1076if (( tid & 31 ) == 0 ) wsum [ tid >> 5 ] = sq_part ; 1077/* 1078DPCT1118: SYCL group functions and algorithms must be encountered in 1079converged control flow. You may need to adjust the code. 1080*/ 1081/* 1082DPCT1065: Consider replacing sycl::nd_item::barrier() with 1083sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 1084better performance if there is no access to global memory. 1085*/ 1086item_ct1 . barrier (); 1087if ( rg == 0 ) { 1088const float ss = wsum [ 0 ] + wsum [ 1 ] + wsum [ 2 ] + wsum [ 3 ]; 1089const float scale = sycl:: rsqrt ( ss / (float) S + eps ); 1090const float zz = z [(size_t) t * value_dim + head * S + col ]; 1091y [(size_t) t * value_dim + head * S + col ] = 1092oc * scale * gamma [ col ] * 1093( 1.0f / ( 1.0f + sycl::native:: exp ( - zz ))); 1094} 1095} 1096if ( n_keep != nullptr && n > 0 ) { 1097#pragma unroll 1098for (int r = 0 ; r < RPG ; ++ r ) base [ r * row_stride ] = s [ r ]; 1099} 1100} 1101 1102__dpct_inline__ void embedding_gather_dev_kernel ( 1103const uint8_t * __restrict__ codes , const float * __restrict__ scales , 1104const float * __restrict__ offsets , const int32_t * __restrict__ tokens , 1105int64_t n , int code_bits , int code_bias , int group_elems , 1106unsigned long long row_codes , unsigned long long row_groups , 1107float * __restrict__ out ) { 1108auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1109const int t = item_ct1 . get_group ( 1 ); 1110const int64_t i = 1111(int64_t) item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 1112item_ct1 . get_local_id ( 2 ); 1113if ( i >= n ) return ; 1114const unsigned long long token = (unsigned long long) tokens [ t ]; 1115const uint8_t * c = codes + token * row_codes ; 1116const float * sc = scales + token * row_groups ; 1117const float * of = offsets ? offsets + token * row_groups : nullptr ; 1118const int per_byte = 8 / code_bits ; 1119const unsigned mask = ( 1u << code_bits ) - 1u ; 1120const int code = ( c [ i / per_byte ] >> (( i % per_byte ) * code_bits )) & mask ; 1121const int64_t group = i / group_elems ; 1122/* 1123DPCT1013: The rounding mode could not be specified and the generated 1124code may have different accuracy than the original code. Verify the 1125correctness. SYCL math built-in function rounding mode is aligned with 1126OpenCL C 1.2 standard. 1127*/ 1128const float product = (float)( code + code_bias ) * sc [ group ]; 1129/* 1130DPCT1013: The rounding mode could not be specified and the generated 1131code may have different accuracy than the original code. Verify the 1132correctness. SYCL math built-in function rounding mode is aligned with 1133OpenCL C 1.2 standard. 1134*/ 1135out [(size_t) t * n + i ] = product + ( of ? of [ group ] : 0.0f ); 1136} 1137 1138__dpct_inline__ void broadcast_streams_kernel ( const float * __restrict__ x , 1139float * __restrict__ R , int64_t n , 1140int hc ) { 1141auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1142const int t = item_ct1 . get_group ( 1 ); 1143const int64_t i = 1144(int64_t) item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 1145item_ct1 . get_local_id ( 2 ); 1146if ( i >= n * hc ) return ; 1147R [(size_t) t * n * hc + i ] = x [(size_t) t * n + i % n ]; 1148} 1149 1150__dpct_inline__ void copy_indexed_kernel (float * __restrict__ dst , 1151const float * __restrict__ src , 1152int64_t stride , 1153const int32_t * __restrict__ index , 1154int64_t n ) { 1155auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1156const int idx = * index ; 1157if ( idx < 0 ) return ; 1158#pragma unroll 1159for (int64_t i = 1160(int64_t) item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 1161item_ct1 . get_local_id ( 2 ); 1162i < n ; i += (int64_t) item_ct1 . get_group_range ( 2 ) * 1163item_ct1 . get_local_range ( 2 )) 1164dst [ i ] = src [(size_t) idx * stride + i ]; 1165} 1166 1167__dpct_inline__ void 1168fetch_blobs_kernel ( const unsigned long long * __restrict__ src , 1169const int32_t * __restrict__ n , sycl::uint4 * __restrict__ dst , 1170long long per ) { 1171auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1172const long long total = (long long) * n * per ; 1173for (long long i = 1174(long long) item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 1175item_ct1 . get_local_id ( 2 ); 1176i < total ; i += (long long) item_ct1 . get_group_range ( 2 ) * 1177item_ct1 . get_local_range ( 2 )) { 1178const long long k = i / per , off = i - k * per ; 1179dst [ i ] = (( const sycl::uint4 * ) src [ k ])[ off ]; 1180} 1181} 1182 1183__dpct_inline__ void rebase_ptrs_kernel (unsigned long long * ptr , 1184const int32_t * n , 1185unsigned long long base , 1186long long bytes ) { 1187const int k = 1188sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (). get_local_id ( 2 ); 1189if ( k < * n ) ptr [ k ] = base + (unsigned long long) k * (unsigned long long) bytes ; 1190} 1191 1192__dpct_inline__ void add_streams_broadcast_kernel ( const float * __restrict__ h , 1193const float * __restrict__ e , 1194float * __restrict__ R , 1195int64_t n , int hc ) { 1196auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1197const int t = item_ct1 . get_group ( 1 ); 1198const int64_t i = 1199(int64_t) item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 1200item_ct1 . get_local_id ( 2 ); 1201if ( i >= n * hc ) return ; 1202R [(size_t) t * n * hc + i ] = h [(size_t) t * n * hc + i ] + e [(size_t) t * n + i % n ]; 1203} 1204 1205__dpct_inline__ void ident_hits_kernel ( const int32_t * __restrict__ ids , int n , 1206int32_t * __restrict__ slot , 1207int32_t * __restrict__ dst , 1208int32_t * __restrict__ count ) { 1209const int i = 1210sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (). get_local_id ( 2 ); 1211if ( i < n ) { slot [ i ] = ids [ i ]; dst [ i ] = i ; } 1212if ( i == 0 ) * count = n ; 1213} 1214 1215// E = the widest element the row size divides into (16, 4 or 1 bytes): a Q6_K head row of 2560 values is 2100 bytes 1216template < typename E > 1217__dpct_inline__ void gather_rows_kernel ( const E * __restrict__ src , 1218long long row_e , 1219const int32_t * __restrict__ ids , 1220long long n , E * __restrict__ dst ) { 1221auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1222const long long total = n * row_e ; 1223for (long long i = 1224(long long) item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 1225item_ct1 . get_local_id ( 2 ); 1226i < total ; i += (long long) item_ct1 . get_group_range ( 2 ) * 1227item_ct1 . get_local_range ( 2 )) { 1228const long long r = i / row_e , o = i - r * row_e ; 1229dst [ i ] = src [(long long) ids [ r ] * row_e + o ]; 1230} 1231} 1232 1233__dpct_inline__ void map_ids_kernel (int32_t * ids , 1234const int32_t * __restrict__ table , int n ) { 1235const int i = 1236sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (). get_local_id ( 2 ); 1237if ( i < n ) ids [ i ] = table [ ids [ i ]]; 1238} 1239 1240__dpct_inline__ void row_top_prob_kernel ( const float * __restrict__ logits , 1241int n_vocab , 1242const int32_t * __restrict__ ids , 1243float * __restrict__ probs ) { 1244auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1245auto & part = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ 32 ] > ( 1246sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 1247const int t = item_ct1 . get_group ( 2 ); 1248const float * l = logits + (size_t) t * n_vocab ; 1249const float m = l [ ids [ t ]]; 1250float s = 0.0f ; 1251#pragma unroll 1252for (int i = item_ct1 . get_local_id ( 2 ); i < n_vocab ; 1253i += item_ct1 . get_local_range ( 2 )) s += sycl::native:: exp ( l [ i ] - m ); 1254/* 1255DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 1256masked sub_group function which may not be supported by all compilers or 1257runtimes. You may need to adjust the code. 1258*/ 1259#pragma unroll 1260for (int o = 16 ; o > 0 ; o >>= 1 ) s += 1261dpct::experimental:: permute_sub_group_by_xor ( 12620xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), s , 1263o ); 1264if (( item_ct1 . get_local_id ( 2 ) & 31 ) == 0 ) 1265part [ item_ct1 . get_local_id ( 2 ) >> 5 ] = s ; 1266item_ct1 . barrier (sycl::access::fence_space:: local_space ); 1267if ( item_ct1 . get_local_id ( 2 ) == 0 ) { 1268float tot = 0.0f ; 1269#pragma unroll 1270for (int w = 0 ; w < (int)( item_ct1 . get_local_range ( 2 ) >> 5 ); ++ w ) tot += 1271part [ w ]; 1272probs [ t ] = 1.0f / tot ; 1273} 1274} 1275 1276__dpct_inline__ void 1277mtp_select_kernel ( const float * __restrict__ R_src , int64_t stride , 1278const int32_t * __restrict__ ids , 1279const int32_t * __restrict__ row_dev , 1280float * __restrict__ R_dst , int32_t * __restrict__ tok_dst , 1281int32_t * out , int j , const float * probs , float * out_p ) { 1282auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1283const int row = * row_dev ; 1284#pragma unroll 1285for (int64_t i = 1286(int64_t) item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 1287item_ct1 . get_local_id ( 2 ); 1288i < stride ; i += (int64_t) item_ct1 . get_group_range ( 2 ) * 1289item_ct1 . get_local_range ( 2 )) 1290R_dst [ i ] = R_src [(size_t) row * stride + i ]; 1291item_ct1 . barrier (sycl::access::fence_space:: local_space ); 1292if ( item_ct1 . get_group ( 2 ) == 0 && item_ct1 . get_local_id ( 2 ) == 0 ) { 1293const int32_t tok = ids [ row ]; 1294* tok_dst = tok ; 1295if ( probs != nullptr && out_p != nullptr ) (( volatile float * ) out_p )[ j ] = probs [ row ]; 1296/* 1297DPCT1078: Consider replacing memory_order::acq_rel with 1298memory_order::seq_cst for correctness if strong memory order 1299restrictions are needed. 1300*/ 1301sycl:: atomic_fence (sycl::memory_order:: acq_rel , 1302sycl::memory_scope:: system ); 1303if ( out != nullptr ) (( volatile int32_t * ) out )[ j ] = tok ; 1304} 1305} 1306 1307__dpct_inline__ void dense_steps_kernel ( const int32_t * __restrict__ cells , 1308int n , int32_t * __restrict__ steps ) { 1309const int i = 1310sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (). get_local_id ( 2 ); 1311if ( i >= n ) return ; 1312const int c = cells [ i ]; 1313steps [ i * 4 + 0 ] = c ; 1314steps [ i * 4 + 1 ] = c + 1 ; 1315steps [ i * 4 + 2 ] = ( c + 1 ) / 4 ; 1316steps [ i * 4 + 3 ] = c + 1 ; 1317} 1318 1319// --pipeline-windows: the drafter's chain, teacher forced (see force_token) 1320__dpct_inline__ void force_token_kernel (int32_t * tok , const int32_t * force , 1321int j ) { 1322const int32_t f = (( const volatile int32_t * ) force )[ j ]; 1323if ( f >= 0 ) * tok = f ; 1324} 1325 1326} // namespace 1327 1328void fetch_blobs ( const unsigned long long * src , const int32_t * n , uint8_t * dst , int64_t blob_bytes , int cap , void * stream ) { 1329if ( cap <= 0 ) return ; 1330if ( blob_bytes % 16 != 0 ) { std:: fprintf ( stderr , "fetch_blobs: blob size must be a multiple of 16\n" ); std:: exit ( 1 ); } 1331{ 1332auto exp_props = sycl::ext::oneapi::experimental::properties{ 1333sycl::ext::oneapi::experimental:: use_root_sync }; 1334 1335strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1336-> submit ([ & ](sycl::handler & cgh ) { 1337auto blob_bytes_ct3 = (long long)( blob_bytes / 16 ); 1338 1339cgh . parallel_for < 1340dpct_kernel_name < class fetch_blobs_kernel_14b6f5 >> ( 1341sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 48 * 8 ) * 1342sycl:: range ( 1 , 1 , 256 ), 1343sycl:: range ( 1 , 1 , 256 )), 1344exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1345fetch_blobs_kernel ( src , n , (sycl::uint4 * ) dst , 1346blob_bytes_ct3 ); 1347}); 1348}); 1349} 1350check ( "fetch_blobs" ); 1351} 1352 1353void rebase_ptrs (unsigned long long * ptr , const int32_t * n , uint8_t * base , int64_t blob_bytes , void * stream ) { 1354{ 1355auto exp_props = sycl::ext::oneapi::experimental::properties{ 1356sycl::ext::oneapi::experimental:: use_root_sync }; 1357 1358strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1359-> template parallel_for < dpct_kernel_name < class rebase_ptrs_kernel_436f27 >> ( 1360sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 128 ), 1361sycl:: range ( 1 , 1 , 128 )), 1362exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1363rebase_ptrs_kernel ( ptr , n , (unsigned long long) base , 1364(long long) blob_bytes ); 1365}); 1366} 1367check ( "rebase_ptrs" ); 1368} 1369 1370void add_streams_broadcast ( const float * h , const float * e , float * R , int64_t n_embd , int hc , int n_tok , void * stream ) { 1371{ 1372auto exp_props = sycl::ext::oneapi::experimental::properties{ 1373sycl::ext::oneapi::experimental:: use_root_sync }; 1374 1375strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1376-> template parallel_for < 1377dpct_kernel_name < class add_streams_broadcast_kernel_7de8f5 >> ( 1378sycl:: nd_range < 3 > ( 1379sycl:: range ( 1 , (unsigned) n_tok , 1380(unsigned)(( n_embd * hc + 255 ) / 256 )) * 1381sycl:: range ( 1 , 1 , 256 ), 1382sycl:: range ( 1 , 1 , 256 )), 1383exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1384add_streams_broadcast_kernel ( h , e , R , n_embd , hc ); 1385}); 1386} 1387check ( "add_streams_broadcast" ); 1388} 1389 1390void ident_hits ( const int32_t * ids , int n , int32_t * slot , int32_t * dst , int32_t * count , void * stream ) { 1391if ( n < 1 || n > 1024 ) { std:: fprintf ( stderr , "ident_hits: n out of range\n" ); std:: exit ( 1 ); } 1392/* 1393DPCT1049: The work-group size passed to the SYCL kernel may exceed the 1394limit. To get the device limit, query info::device::max_work_group_size. 1395Adjust the work-group size if needed. 1396*/ 1397{ 1398auto exp_props = sycl::ext::oneapi::experimental::properties{ 1399sycl::ext::oneapi::experimental:: use_root_sync }; 1400 1401strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1402-> template parallel_for < dpct_kernel_name < class ident_hits_kernel_287489 >> ( 1403sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 1024 ), 1404sycl:: range ( 1 , 1 , 1024 )), 1405exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1406ident_hits_kernel ( ids , n , slot , dst , count ); 1407}); 1408} 1409check ( "ident_hits" ); 1410} 1411 1412namespace { 1413__dpct_inline__ void 1414copy_row_to_first_kernel ( const int32_t * __restrict__ row_dev , float * a , 1415int64_t a_n , float * b , int64_t b_n , float * c , 1416int64_t c_n ) { 1417auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1418const int64_t row = * row_dev ; 1419if ( row == 0 ) return ; 1420const int64_t i0 = (int64_t) item_ct1 . get_group ( 2 ) * 1421item_ct1 . get_local_range ( 2 ) + 1422item_ct1 . get_local_id ( 2 ), 1423st = (int64_t) item_ct1 . get_group_range ( 2 ) * 1424item_ct1 . get_local_range ( 2 ); 1425#pragma unroll 1426for (int64_t i = i0 ; i < a_n ; i += st ) a [ i ] = a [ row * a_n + i ]; 1427#pragma unroll 1428for (int64_t i = i0 ; i < b_n ; i += st ) b [ i ] = b [ row * b_n + i ]; 1429#pragma unroll 1430for (int64_t i = i0 ; i < c_n ; i += st ) c [ i ] = c [ row * c_n + i ]; 1431} 1432} // namespace 1433 1434void copy_row_to_first ( const int32_t * row_dev , float * a , int64_t a_n , float * b , int64_t b_n , float * c , int64_t c_n , 1435void * stream ) { 1436{ 1437auto exp_props = sycl::ext::oneapi::experimental::properties{ 1438sycl::ext::oneapi::experimental:: use_root_sync }; 1439 1440strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1441-> template parallel_for < 1442dpct_kernel_name < class copy_row_to_first_kernel_993785 >> ( 1443sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 16 ) * 1444sycl:: range ( 1 , 1 , 256 ), 1445sycl:: range ( 1 , 1 , 256 )), 1446exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1447copy_row_to_first_kernel ( row_dev , a , a_n , b , b_n , c , c_n ); 1448}); 1449} 1450check ( "copy_row_to_first" ); 1451} 1452 1453void mtp_select ( const float * R_src , int64_t R_stride , const int32_t * ids , const int32_t * row_dev , float * R_dst , 1454int32_t * tok_dst , int32_t * out , int j , void * stream , const float * probs , float * out_p ) { 1455{ 1456auto exp_props = sycl::ext::oneapi::experimental::properties{ 1457sycl::ext::oneapi::experimental:: use_root_sync }; 1458 1459strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1460-> template parallel_for < dpct_kernel_name < class mtp_select_kernel_4611fe >> ( 1461sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 256 ), 1462sycl:: range ( 1 , 1 , 256 )), 1463exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1464mtp_select_kernel ( R_src , R_stride , ids , row_dev , R_dst , 1465tok_dst , out , j , probs , out_p ); 1466}); 1467} 1468check ( "mtp_select" ); 1469} 1470 1471void force_token (int32_t * tok , const int32_t * force , int j , void * stream ) { 1472{ 1473auto exp_props = sycl::ext::oneapi::experimental::properties{ 1474sycl::ext::oneapi::experimental:: use_root_sync }; 1475 1476strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1477-> template parallel_for < dpct_kernel_name < class force_token_kernel_a9fa72 >> ( 1478sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 1 ), sycl:: range ( 1 , 1 , 1 )), 1479exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1480force_token_kernel ( tok , force , j ); 1481}); 1482} 1483check ( "force_token" ); 1484} 1485 1486void gather_rows ( const uint8_t * src , int64_t row_bytes , const int32_t * ids , int64_t n , uint8_t * dst , void * stream ) { 1487dpct::queue_ptr s = strata:: q_of ( stream ); 1488if ( row_bytes % 16 == 0 ) 1489{ 1490auto exp_props = sycl::ext::oneapi::experimental::properties{ 1491sycl::ext::oneapi::experimental:: use_root_sync }; 1492 1493strata::kprof:: kp ( s , __FILE__ , __func__ , __LINE__ ) -> submit ([ & ](sycl::handler & cgh ) { 1494long long row_bytes_ct1 = row_bytes / 16 ; 1495 1496cgh . parallel_for < 1497dpct_kernel_name < class gather_rows_kernel_b3ae5a, sycl::uint4 >> ( 1498sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 48 * 8 ) * 1499sycl:: range ( 1 , 1 , 256 ), 1500sycl:: range ( 1 , 1 , 256 )), 1501[ = ](sycl::nd_item < 3 > item_ct1 ) { 1502gather_rows_kernel (( const sycl::uint4 * ) src , row_bytes_ct1 , 1503ids , n , (sycl::uint4 * ) dst ); 1504}); 1505}); 1506} else if ( row_bytes % 4 == 0 ) 1507{ 1508auto exp_props = sycl::ext::oneapi::experimental::properties{ 1509sycl::ext::oneapi::experimental:: use_root_sync }; 1510 1511strata::kprof:: kp ( s , __FILE__ , __func__ , __LINE__ ) -> submit ([ & ](sycl::handler & cgh ) { 1512long long row_bytes_ct1 = row_bytes / 4 ; 1513 1514cgh . parallel_for < 1515dpct_kernel_name < class gather_rows_kernel_f256ac, uint32_t >> ( 1516sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 48 * 8 ) * 1517sycl:: range ( 1 , 1 , 256 ), 1518sycl:: range ( 1 , 1 , 256 )), 1519[ = ](sycl::nd_item < 3 > item_ct1 ) { 1520gather_rows_kernel (( const uint32_t * ) src , row_bytes_ct1 , 1521ids , n , (uint32_t * ) dst ); 1522}); 1523}); 1524} else { 1525auto exp_props = sycl::ext::oneapi::experimental::properties{ 1526sycl::ext::oneapi::experimental:: use_root_sync }; 1527 1528strata::kprof:: kp ( s , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < 1529dpct_kernel_name < class gather_rows_kernel_c5b171, uint8_t >> ( 1530sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 48 * 8 ) * 1531sycl:: range ( 1 , 1 , 256 ), 1532sycl:: range ( 1 , 1 , 256 )), 1533[ = ](sycl::nd_item < 3 > item_ct1 ) { 1534gather_rows_kernel ( src , row_bytes , ids , n , dst ); 1535}); 1536} 1537check ( "gather_rows" ); 1538} 1539 1540void map_ids (int32_t * ids , const int32_t * table , int n , void * stream ) { 1541{ 1542auto exp_props = sycl::ext::oneapi::experimental::properties{ 1543sycl::ext::oneapi::experimental:: use_root_sync }; 1544 1545strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1546-> template parallel_for < dpct_kernel_name < class map_ids_kernel_43a113 >> ( 1547sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 64 ), sycl:: range ( 1 , 1 , 64 )), 1548exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1549map_ids_kernel ( ids , table , n ); 1550}); 1551} 1552check ( "map_ids" ); 1553} 1554 1555void row_top_prob ( const float * logits , int n_rows , int n_vocab , const int32_t * ids , float * probs , void * stream ) { 1556/* 1557DPCT1049: The work-group size passed to the SYCL kernel may exceed the 1558limit. To get the device limit, query info::device::max_work_group_size. 1559Adjust the work-group size if needed. 1560*/ 1561{ 1562auto exp_props = sycl::ext::oneapi::experimental::properties{ 1563sycl::ext::oneapi::experimental:: use_root_sync }; 1564 1565strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1566-> template parallel_for < dpct_kernel_name < class row_top_prob_kernel_6adbe2 >> ( 1567sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , n_rows ) * 1568sycl:: range ( 1 , 1 , 1024 ), 1569sycl:: range ( 1 , 1 , 1024 )), 1570exp_props , 1571[ = ](sycl::nd_item < 3 > item_ct1 ) 1572[[ sycl :: reqd_sub_group_size ( 32 )]] { 1573row_top_prob_kernel ( logits , n_vocab , ids , probs ); 1574}); 1575} 1576check ( "row_top_prob" ); 1577} 1578 1579#if !defined( __HIPCC__ ) // warp-32 kernels: an AMD card keeps the one-block row_top_prob and sampler_greedy_kernel 1580namespace { 1581 1582constexpr int kArgMaxBlocks = 128 , kArgThreads = 256 , kTopBlocks = 8 ; 1583 1584// the larger value, on equality the lower index: sampler_greedy_kernel's order 1585__dpct_inline__ void arg_take (float ov , int oi , float & bv , int & bi ) { 1586if ( ov > bv || ( ov == bv && oi < bi )) { bv = ov ; bi = oi ; } 1587} 1588 1589// a block's (value, index) pick, in thread 0; `none` is the index of no candidate 1590__dpct_inline__ void arg_block (float & bv , int & bi , int none ) { 1591/* 1592DPCT1115: The sycl::ext::oneapi::group_local_memory_for_overwrite is 1593used to allocate group-local memory at the none kernel functor scope of a 1594work-group data parallel kernel. You may need to adjust the code. 1595*/ 1596auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1597auto & sv = * sycl::ext::oneapi:: group_local_memory_for_overwrite < float[ 32 ] > ( 1598sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 1599/* 1600DPCT1115: The sycl::ext::oneapi::group_local_memory_for_overwrite is 1601used to allocate group-local memory at the none kernel functor scope of a 1602work-group data parallel kernel. You may need to adjust the code. 1603*/ 1604auto & si = * sycl::ext::oneapi:: group_local_memory_for_overwrite < int[ 32 ] > ( 1605sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 1606for (int off = 16 ; off > 0 ; off >>= 1 ) { 1607/* 1608DPCT1108: '__shfl_down_sync' was migrated with the experimental 1609feature masked sub_group function which may not be supported by all 1610compilers or runtimes. You may need to adjust the code. 1611*/ 1612/* 1613DPCT1121: Make sure that the "bv" which is used in the SYCL group 1614function/algorithm is initialized. 1615*/ 1616const float ov = dpct::experimental:: shift_sub_group_left ( 16170xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), bv , 1618off ); 1619/* 1620DPCT1108: '__shfl_down_sync' was migrated with the experimental 1621feature masked sub_group function which may not be supported by all 1622compilers or runtimes. You may need to adjust the code. 1623*/ 1624/* 1625DPCT1121: Make sure that the "bi" which is used in the SYCL group 1626function/algorithm is initialized. 1627*/ 1628const int oi = dpct::experimental:: shift_sub_group_left ( 16290xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), bi , 1630off ); 1631arg_take ( ov , oi , bv , bi ); 1632} 1633const int warp = (int)( item_ct1 . get_local_id ( 2 ) >> 5 ), 1634lane = (int)( item_ct1 . get_local_id ( 2 ) & 31 ); 1635if ( lane == 0 ) { sv [ warp ] = bv ; si [ warp ] = bi ; } 1636/* 1637DPCT1065: Consider replacing sycl::nd_item::barrier() with 1638sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1639performance if there is no access to global memory. 1640*/ 1641item_ct1 . barrier (); 1642if ( warp == 0 ) { 1643const int nw = (int)( item_ct1 . get_local_range ( 2 ) >> 5 ); 1644bv = lane < nw ? sv [ lane ] : sycl:: bit_cast < float, int > ( 0xff800000 ); 1645bi = lane < nw ? si [ lane ] : none ; 1646for (int off = 16 ; off > 0 ; off >>= 1 ) { 1647/* 1648DPCT1108: '__shfl_down_sync' was migrated with the experimental 1649feature masked sub_group function which may not be supported by all 1650compilers or runtimes. You may need to adjust the code. 1651*/ 1652/* 1653DPCT1121: Make sure that the "bv" which is used in the SYCL 1654group function/algorithm is initialized. 1655*/ 1656const float ov = dpct::experimental:: shift_sub_group_left ( 16570xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), 1658bv , off ); 1659/* 1660DPCT1108: '__shfl_down_sync' was migrated with the experimental 1661feature masked sub_group function which may not be supported by all 1662compilers or runtimes. You may need to adjust the code. 1663*/ 1664/* 1665DPCT1121: Make sure that the "bi" which is used in the SYCL 1666group function/algorithm is initialized. 1667*/ 1668const int oi = dpct::experimental:: shift_sub_group_left ( 16690xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), 1670bi , off ); 1671arg_take ( ov , oi , bv , bi ); 1672} 1673} 1674} 1675 1676// A row's scratch, whatever the launch's row count (one scratch serves windows of every size): its counter, then 1677// the blocks' values and indices. 1678struct ArgRow { 1679unsigned counter ; 1680unsigned pad [ 3 ]; 1681float v [ kArgMaxBlocks ]; 1682int i [ kArgMaxBlocks ]; 1683}; 1684 1685// grid (blocks, rows): block b scans [b * per_block, +per_block) of its row 1686__dpct_inline__ void argmax_rows_kernel ( const float * __restrict__ logits , int n , 1687int per_block , 1688ArgRow * __restrict__ rows , 1689int32_t * out ) { 1690auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1691const int row = (int) item_ct1 . get_group ( 1 ), b = (int) item_ct1 . get_group ( 2 ), 1692nb = (int) item_ct1 . get_group_range ( 2 ); 1693unsigned * counter = & rows [ row ]. counter ; 1694float * pv = rows [ row ]. v ; 1695int * pi = rows [ row ]. i ; 1696const float * l = logits + (size_t) row * n ; 1697const int lo = b * per_block , hi = sycl:: min ( n , lo + per_block ); 1698float bv = sycl:: bit_cast < float, int > ( 0xff800000 ); // -inf 1699int bi = n ; 1700for (int v = lo + (int) item_ct1 . get_local_id ( 2 ); v < hi ; 1701v += (int) item_ct1 . get_local_range ( 2 )) { 1702const float s = l [ v ]; 1703if ( s > bv ) { bv = s ; bi = v ; } 1704} 1705arg_block ( bv , bi , n ); 1706auto & last = * sycl::ext::oneapi:: group_local_memory_for_overwrite < bool > ( 1707sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 1708if ( item_ct1 . get_local_id ( 2 ) == 0 ) { 1709pv [ b ] = bv ; 1710pi [ b ] = bi ; 1711/* 1712DPCT1078: Consider replacing memory_order::acq_rel with 1713memory_order::seq_cst for correctness if strong memory order 1714restrictions are needed. 1715*/ 1716sycl:: atomic_fence (sycl::memory_order:: acq_rel , 1717sycl::memory_scope:: device ); 1718last = 1719dpct:: atomic_fetch_add < sycl::access::address_space::generic_space > ( 1720counter , 1u ) == (unsigned)( nb - 1 ); 1721} 1722/* 1723DPCT1065: Consider replacing sycl::nd_item::barrier() with 1724sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1725performance if there is no access to global memory. 1726*/ 1727item_ct1 . barrier (); 1728if (! last ) return ; 1729/* 1730DPCT1078: Consider replacing memory_order::acq_rel with 1731memory_order::seq_cst for correctness if strong memory order restrictions 1732are needed. 1733*/ 1734sycl:: atomic_fence (sycl::memory_order:: acq_rel , sycl::memory_scope:: device ); 1735bv = sycl:: bit_cast < float, int > ( 0xff800000 ); 1736bi = n ; 1737if ((int) item_ct1 . get_local_id ( 2 ) < nb ) { 1738bv = (( volatile const float * ) pv )[ item_ct1 . get_local_id ( 2 )]; 1739bi = (( volatile const int * ) pi )[ item_ct1 . get_local_id ( 2 )]; 1740} 1741arg_block ( bv , bi , n ); 1742if ( item_ct1 . get_local_id ( 2 ) == 0 ) { 1743out [ row ] = bi < n ? bi : 0 ; // no value above -inf: 0, as the one-block kernel 1744* counter = 0 ; 1745} 1746} 1747 1748// a row's top-probability scratch: its counter, then its 32 warps' sums 1749struct TopRow { 1750unsigned counter ; 1751unsigned pad [ 3 ]; 1752float parts [ 32 ]; 1753}; 1754 1755// grid (kTopBlocks, rows), 1024 / kTopBlocks threads: thread j of block b is row_top_prob's thread b * blockDim + j 1756__dpct_inline__ void row_top_prob_split_kernel ( const float * __restrict__ logits , 1757int n_vocab , 1758const int32_t * __restrict__ ids , 1759float * __restrict__ probs , 1760TopRow * __restrict__ rows ) { 1761auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1762const int row = (int) item_ct1 . get_group ( 1 ); 1763float * parts = rows [ row ]. parts ; 1764const float * l = logits + (size_t) row * n_vocab ; 1765const float m = l [ ids [ row ]]; 1766const int vt = (int)( item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 1767item_ct1 . get_local_id ( 2 )); 1768float s = 0.0f ; 1769#pragma unroll 1770for (int i = vt ; i < n_vocab ; i += 1024 ) s += sycl::native:: exp ( l [ i ] - m ); 1771/* 1772DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 1773masked sub_group function which may not be supported by all compilers or 1774runtimes. You may need to adjust the code. 1775*/ 1776#pragma unroll 1777for (int o = 16 ; o > 0 ; o >>= 1 ) s += 1778dpct::experimental:: permute_sub_group_by_xor ( 17790xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), s , 1780o ); 1781if (( item_ct1 . get_local_id ( 2 ) & 31 ) == 0 ) parts [ vt >> 5 ] = s ; 1782/* 1783DPCT1078: Consider replacing memory_order::acq_rel with 1784memory_order::seq_cst for correctness if strong memory order restrictions 1785are needed. 1786*/ 1787sycl:: atomic_fence (sycl::memory_order:: acq_rel , sycl::memory_scope:: device ); 1788/* 1789DPCT1065: Consider replacing sycl::nd_item::barrier() with 1790sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1791performance if there is no access to global memory. 1792*/ 1793item_ct1 . barrier (); 1794auto & last = * sycl::ext::oneapi:: group_local_memory_for_overwrite < bool > ( 1795sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 1796if ( item_ct1 . get_local_id ( 2 ) == 0 ) last = 1797dpct:: atomic_fetch_add < sycl::access::address_space::generic_space > ( 1798& rows [ row ]. counter , 1u ) == 1799(unsigned)( item_ct1 . get_group_range ( 2 ) - 1 ); 1800/* 1801DPCT1065: Consider replacing sycl::nd_item::barrier() with 1802sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 1803performance if there is no access to global memory. 1804*/ 1805item_ct1 . barrier (); 1806if (! last || item_ct1 . get_local_id ( 2 ) != 0 ) return ; 1807/* 1808DPCT1078: Consider replacing memory_order::acq_rel with 1809memory_order::seq_cst for correctness if strong memory order restrictions 1810are needed. 1811*/ 1812sycl:: atomic_fence (sycl::memory_order:: acq_rel , sycl::memory_scope:: device ); 1813float tot = 0.0f ; 1814#pragma unroll 1815for (int w = 0 ; w < 32 ; ++ w ) tot += (( volatile const float * ) parts )[ w ]; 1816probs [ row ] = 1.0f / tot ; 1817rows [ row ]. counter = 0 ; 1818} 1819 1820} // namespace 1821 1822uint64_t argmax_rows_scratch_bytes (int n_rows ) { return (uint64_t) n_rows * sizeof ( ArgRow ); } 1823 1824void argmax_rows ( const float * logits , int n_rows , int n , void * scratch , int32_t * out , void * stream ) { 1825if ( n_rows <= 0 ) return ; 1826const int nb = std:: min ( kArgMaxBlocks , std:: max ( 1 , ( n + 4095 ) / 4096 )); 1827const int per_block = ( n + nb - 1 ) / nb ; 1828{ 1829auto exp_props = sycl::ext::oneapi::experimental::properties{ 1830sycl::ext::oneapi::experimental:: use_root_sync }; 1831 1832strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1833-> template parallel_for < dpct_kernel_name < class argmax_rows_kernel_d5bcf6 >> ( 1834sycl:: nd_range < 3 > ( 1835sycl:: range ( 1 , (unsigned) n_rows , (unsigned) nb ) * 1836sycl:: range ( 1 , 1 , kArgThreads ), 1837sycl:: range ( 1 , 1 , kArgThreads )), 1838exp_props , 1839[ = ](sycl::nd_item < 3 > item_ct1 ) 1840[[ sycl :: reqd_sub_group_size ( 32 )]] { 1841argmax_rows_kernel ( logits , n , per_block , 1842(ArgRow * ) scratch , out ); 1843}); 1844} 1845check ( "argmax_rows" ); 1846} 1847 1848uint64_t row_top_prob_scratch_bytes (int n_rows ) { return (uint64_t) n_rows * sizeof ( TopRow ); } 1849 1850void row_top_prob_split ( const float * logits , int n_rows , int n_vocab , const int32_t * ids , float * probs , void * scratch , 1851void * stream ) { 1852if ( n_rows <= 0 ) return ; 1853{ 1854auto exp_props = sycl::ext::oneapi::experimental::properties{ 1855sycl::ext::oneapi::experimental:: use_root_sync }; 1856 1857strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1858-> template parallel_for < 1859dpct_kernel_name < class row_top_prob_split_kernel_9495b0 >> ( 1860sycl:: nd_range < 3 > (sycl:: range ( 1 , (unsigned) n_rows , kTopBlocks ) * 1861sycl:: range ( 1 , 1 , 1024 / kTopBlocks ), 1862sycl:: range ( 1 , 1 , 1024 / kTopBlocks )), 1863exp_props , 1864[ = ](sycl::nd_item < 3 > item_ct1 ) 1865[[ sycl :: reqd_sub_group_size ( 32 )]] { 1866row_top_prob_split_kernel ( logits , n_vocab , ids , probs , 1867(TopRow * ) scratch ); 1868}); 1869} 1870check ( "row_top_prob_split" ); 1871} 1872#else 1873uint64_t argmax_rows_scratch_bytes (int) { return 0 ; } 1874void argmax_rows ( const float * , int, int, void * , int32_t * , void * ) { 1875std:: fprintf ( stderr , "argmax_rows: not built for this backend\n" ); 1876std:: exit ( 1 ); 1877} 1878uint64_t row_top_prob_scratch_bytes (int) { return 0 ; } 1879void row_top_prob_split ( const float * logits , int n_rows , int n_vocab , const int32_t * ids , float * probs , void * , 1880void * stream ) { 1881row_top_prob ( logits , n_rows , n_vocab , ids , probs , stream ); 1882} 1883#endif 1884 1885bool multi_block_head_ops () { 1886#if defined( __HIPCC__ ) 1887return false; 1888#else 1889static const bool on = [] { 1890const char * v = std:: getenv ( "STRATA_MULTI_BLOCK_ARGMAX" ); 1891return v == nullptr || std:: atoi ( v ) != 0 ; 1892}(); 1893return on ; 1894#endif 1895} 1896 1897bool argmax_rows_wanted () try { 1898#if defined( __HIPCC__ ) 1899return false; 1900#else 1901// sm_80 to sm_89: sample_tokens' greedy pick there is the one-block kernel (sm_90+ has the cluster kernel) 1902static const bool want = [] { 1903int dev = 0 , major = 0 ; 1904if ( DPCT_CHECK_ERROR ( dev = dpct:: get_current_device_id ()) != 0 || 1905DPCT_CHECK_ERROR ( 1906major = dpct:: get_device ( dev ). get_major_version ()) != 0 ) 1907return false; 1908return major == 8 ; 1909}(); 1910return want && multi_block_head_ops (); 1911#endif 1912} 1913catch (sycl::exception const & exc ) { 1914std:: cerr << exc . what () << "Exception caught at file:" << __FILE__ 1915<< ", line:" << __LINE__ << std:: endl ; 1916std:: exit ( 1 ); 1917} 1918 1919namespace { 1920__dpct_inline__ void window_ids_kernel (int32_t * steps , int window , int32_t * ids , 1921long long stride ) { 1922auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 1923const int q = item_ct1 . get_group ( 1 ); 1924int32_t * st = steps + q * 4 ; 1925const int n_kv = st [ 1 ]; 1926const int start = n_kv > window ? n_kv - window : 0 ; 1927const int width = n_kv - start ; 1928#pragma unroll 1929for (int j = item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 1930item_ct1 . get_local_id ( 2 ); 1931j < width ; 1932j += item_ct1 . get_group_range ( 2 ) * item_ct1 . get_local_range ( 2 )) 1933ids [ q * stride + j ] = start + j ; 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 (); 1940if ( item_ct1 . get_group ( 2 ) == 0 && item_ct1 . get_local_id ( 2 ) == 0 ) st [ 3 ] = 1941width ; 1942} 1943} // namespace 1944 1945void window_ids (int32_t * steps , int n , int window , int32_t * ids , int64_t ids_stride , void * stream ) { 1946{ 1947auto exp_props = sycl::ext::oneapi::experimental::properties{ 1948sycl::ext::oneapi::experimental:: use_root_sync }; 1949 1950strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1951-> template parallel_for < dpct_kernel_name < class window_ids_kernel_581536 >> ( 1952sycl:: nd_range < 3 > (sycl:: range ( 1 , (unsigned) n , 8 ) * 1953sycl:: range ( 1 , 1 , 256 ), 1954sycl:: range ( 1 , 1 , 256 )), 1955exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1956window_ids_kernel ( steps , window , ids , 1957(long long) ids_stride ); 1958}); 1959} 1960check ( "window_ids" ); 1961} 1962 1963void dense_steps ( const int32_t * cells , int n , int32_t * steps , void * stream ) { 1964{ 1965auto exp_props = sycl::ext::oneapi::experimental::properties{ 1966sycl::ext::oneapi::experimental:: use_root_sync }; 1967 1968strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1969-> template parallel_for < dpct_kernel_name < class dense_steps_kernel_fd7245 >> ( 1970sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 64 ), sycl:: range ( 1 , 1 , 64 )), 1971exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 1972dense_steps_kernel ( cells , n , steps ); 1973}); 1974} 1975check ( "dense_steps" ); 1976} 1977 1978void gdn_conv_l2_multi ( const float * history , const float * qkv , const float * conv_w , float * h , int channels , 1979int qk_heads , float eps , int n_tok , void * stream , int t_begin , bool commit ) { 1980if (! history || ! qkv || ! conv_w || ! h || channels % S != 0 || n_tok < 1 || n_tok > kVerifyMaxT || 1981( commit && ( n_tok != 1 || t_begin != 0 ))) { 1982std:: fprintf ( stderr , "gdn_conv_l2_multi: invalid arguments\n" ); 1983std:: exit ( 1 ); 1984} 1985{ 1986auto exp_props = sycl::ext::oneapi::experimental::properties{ 1987sycl::ext::oneapi::experimental:: use_root_sync }; 1988 1989strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 1990-> template parallel_for < 1991dpct_kernel_name < class gdn_conv_l2_multi_kernel_b3f737 >> ( 1992sycl:: nd_range < 3 > ( 1993sycl:: range ( 1 , (unsigned) n_tok , (unsigned)( channels / S )) * 1994sycl:: range ( 1 , 1 , S ), 1995sycl:: range ( 1 , 1 , S )), 1996exp_props , 1997[ = ](sycl::nd_item < 3 > item_ct1 ) 1998[[ sycl :: reqd_sub_group_size ( 32 )]] { 1999gdn_conv_l2_multi_kernel ( 2000const_cast < float *> ( history ), qkv , conv_w , h , 2001channels , qk_heads , eps , t_begin , commit ); 2002}); 2003} 2004check ( "gdn_conv_l2_multi" ); 2005} 2006 2007void gdn_conv_commit (float * history , const float * qkv , int channels , const int32_t * n_keep , void * stream ) { 2008{ 2009auto exp_props = sycl::ext::oneapi::experimental::properties{ 2010sycl::ext::oneapi::experimental:: use_root_sync }; 2011 2012strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2013-> template parallel_for < 2014dpct_kernel_name < class gdn_conv_commit_kernel_dc1d7f >> ( 2015sycl:: nd_range < 3 > ( 2016sycl:: range ( 1 , 1 , (unsigned)(( channels + 255 ) / 256 )) * 2017sycl:: range ( 1 , 1 , 256 ), 2018sycl:: range ( 1 , 1 , 256 )), 2019exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 2020gdn_conv_commit_kernel ( history , qkv , channels , n_keep ); 2021}); 2022} 2023check ( "gdn_conv_commit" ); 2024} 2025 2026void gdn_ab_multi ( const float * x , const uint16_t * w_alpha , const uint16_t * w_beta , const float * dt , const float * ssm_a , 2027float * gate , float * beta , int n_embd , int h_v , int n_tok , void * stream ) { 2028if ( n_embd % 8 != 0 || n_tok < 1 || n_tok > kVerifyMaxT ) { 2029std:: fprintf ( stderr , "gdn_ab_multi: invalid arguments\n" ); 2030std:: exit ( 1 ); 2031} 2032const unsigned blocks = (unsigned) (( 2 * h_v + 1 ) / 2 ); 2033dpct::queue_ptr st = strata:: q_of ( stream ); 2034switch ( n_tok ) { 2035case 1 : { 2036auto exp_props = sycl::ext::oneapi::experimental::properties{ 2037sycl::ext::oneapi::experimental:: use_root_sync }; 2038 2039strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < 2040dpct_kernel_name < class gdn_ab_multi_kernel_34dcce, 2041dpct_kernel_scalar < 1 > , dpct_kernel_scalar < true >>> ( 2042sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , blocks ) * sycl:: range ( 1 , 1 , 64 ), 2043sycl:: range ( 1 , 1 , 64 )), 2044exp_props , 2045[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2046gdn_ab_multi_kernel < 1 , true > ( x , w_alpha , w_beta , dt , ssm_a , 2047gate , beta , n_embd , h_v , n_tok ); 2048}); 2049} break ; 2050case 2 : { 2051auto exp_props = sycl::ext::oneapi::experimental::properties{ 2052sycl::ext::oneapi::experimental:: use_root_sync }; 2053 2054strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < 2055dpct_kernel_name < class gdn_ab_multi_kernel_34dcce, 2056dpct_kernel_scalar < 2 > , dpct_kernel_scalar < true >>> ( 2057sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , blocks ) * sycl:: range ( 1 , 1 , 64 ), 2058sycl:: range ( 1 , 1 , 64 )), 2059exp_props , 2060[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2061gdn_ab_multi_kernel < 2 , true > ( x , w_alpha , w_beta , dt , ssm_a , 2062gate , beta , n_embd , h_v , n_tok ); 2063}); 2064} break ; 2065case 3 : { 2066auto exp_props = sycl::ext::oneapi::experimental::properties{ 2067sycl::ext::oneapi::experimental:: use_root_sync }; 2068 2069strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < 2070dpct_kernel_name < class gdn_ab_multi_kernel_34dcce, 2071dpct_kernel_scalar < 3 > , dpct_kernel_scalar < true >>> ( 2072sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , blocks ) * sycl:: range ( 1 , 1 , 64 ), 2073sycl:: range ( 1 , 1 , 64 )), 2074exp_props , 2075[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2076gdn_ab_multi_kernel < 3 , true > ( x , w_alpha , w_beta , dt , ssm_a , 2077gate , beta , n_embd , h_v , n_tok ); 2078}); 2079} break ; 2080case 4 : { 2081auto exp_props = sycl::ext::oneapi::experimental::properties{ 2082sycl::ext::oneapi::experimental:: use_root_sync }; 2083 2084strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < 2085dpct_kernel_name < class gdn_ab_multi_kernel_34dcce, 2086dpct_kernel_scalar < 4 > , dpct_kernel_scalar < true >>> ( 2087sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , blocks ) * sycl:: range ( 1 , 1 , 64 ), 2088sycl:: range ( 1 , 1 , 64 )), 2089exp_props , 2090[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2091gdn_ab_multi_kernel < 4 , true > ( x , w_alpha , w_beta , dt , ssm_a , 2092gate , beta , n_embd , h_v , n_tok ); 2093}); 2094} break ; 2095case 5 : { 2096auto exp_props = sycl::ext::oneapi::experimental::properties{ 2097sycl::ext::oneapi::experimental:: use_root_sync }; 2098 2099strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < 2100dpct_kernel_name < class gdn_ab_multi_kernel_34dcce, 2101dpct_kernel_scalar < 5 > , dpct_kernel_scalar < true >>> ( 2102sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , blocks ) * sycl:: range ( 1 , 1 , 64 ), 2103sycl:: range ( 1 , 1 , 64 )), 2104exp_props , 2105[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2106gdn_ab_multi_kernel < 5 , true > ( x , w_alpha , w_beta , dt , ssm_a , 2107gate , beta , n_embd , h_v , n_tok ); 2108}); 2109} break ; 2110case 6 : { 2111auto exp_props = sycl::ext::oneapi::experimental::properties{ 2112sycl::ext::oneapi::experimental:: use_root_sync }; 2113 2114strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < 2115dpct_kernel_name < class gdn_ab_multi_kernel_34dcce, 2116dpct_kernel_scalar < 6 > , dpct_kernel_scalar < true >>> ( 2117sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , blocks ) * sycl:: range ( 1 , 1 , 64 ), 2118sycl:: range ( 1 , 1 , 64 )), 2119exp_props , 2120[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2121gdn_ab_multi_kernel < 6 , true > ( x , w_alpha , w_beta , dt , ssm_a , 2122gate , beta , n_embd , h_v , n_tok ); 2123}); 2124} break ; 2125default : { 2126auto exp_props = sycl::ext::oneapi::experimental::properties{ 2127sycl::ext::oneapi::experimental:: use_root_sync }; 2128 2129strata::kprof:: kp ( st , __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class gdn_ab_multi_kernel_34dcce, 2130dpct_kernel_scalar < kVerifyMaxT > , 2131dpct_kernel_scalar < false >>> ( 2132sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , blocks ) * sycl:: range ( 1 , 1 , 64 ), 2133sycl:: range ( 1 , 1 , 64 )), 2134exp_props , 2135[ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2136gdn_ab_multi_kernel < kVerifyMaxT, false > ( x , w_alpha , w_beta , dt , 2137ssm_a , gate , beta , 2138n_embd , h_v , n_tok ); 2139}); 2140} break ; 2141} 2142check ( "gdn_ab_multi" ); 2143} 2144 2145void gdn_step_norm_groups ( const GdnGroups & groups , const float * h , int conv_channels , const float * gate , const float * beta , 2146const float * z , const float * gamma , float eps , float * y , int h_k , int h_v , bool commit , 2147void * stream ) { 2148if ( groups . n < 1 || groups . n > 8 || ! h || ! gate || ! beta || ! z || ! gamma || ! y || h_k <= 0 || h_v % h_k ) { 2149std:: fprintf ( stderr , "gdn_step_norm_groups: invalid arguments\n" ); 2150std:: exit ( 1 ); 2151} 2152for (int i = 0 ; i < groups . n ; ++ i ) 2153if (! groups . state [ i ] || groups . ntok [ i ] < 1 || groups . ntok [ i ] > kVerifyMaxT || ( commit && ! groups . keep [ i ])) { 2154std:: fprintf ( stderr , "gdn_step_norm_groups: invalid group %d\n" , i ); 2155std:: exit ( 1 ); 2156} 2157const dpct::dim3 g1 ((unsigned) h_v , (unsigned) groups . n ), b1 (S, RG); 2158const GdnGroups grp = groups ; 2159auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental:: use_root_sync }; 2160// the kernel gdn_step_norm_multi picks for these calls: a window's rows (all outputs, no keep) or a commit of 2161// one-row groups (keep, t_out_begin 0) 2162if ( commit ) 2163strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class gdn_step_norm_groups_kernel_b1, dpct_kernel_scalar < false >>> ( 2164sycl:: nd_range < 3 > ( g1 * b1 , b1 ), exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2165gdn_step_norm_multi_kernel < false, false > ( nullptr , h , conv_channels , gate , beta , z , gamma , eps , y , h_k , 2166h_v , 0 , nullptr , 0 , nullptr , grp ); 2167}); 2168else 2169strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) -> template parallel_for < dpct_kernel_name < class gdn_step_norm_groups_kernel_b2, dpct_kernel_scalar < true >>> ( 2170sycl:: nd_range < 3 > ( g1 * b1 , b1 ), exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2171gdn_step_norm_multi_kernel < true, false > ( nullptr , h , conv_channels , gate , beta , z , gamma , eps , y , h_k , 2172h_v , 0 , nullptr , 0 , nullptr , grp ); 2173}); 2174} 2175 2176void launch_b60_gdn_readonly_split_local (float * state , const float * h ,int C , 2177const float * gate , const float * beta , const float * z , const float * gamma ,float * y , 2178int HK ,int HV ,int T ,void * stream ); 2179void gdn_commit_state_multi (float * state , const float * h , int C , const float * gate , const float * beta , 2180int h_k , int h_v , int n_tok , const int32_t * n_keep , void * stream ) { 2181if (! state || ! h || ! gate || ! beta || ! n_keep || h_k <= 0 || h_v % h_k || 2182n_tok < 1 || n_tok > kVerifyMaxT ) 2183throw std:: invalid_argument ( "GDN state-only commit arguments" ); 2184auto properties = sycl::ext::oneapi::experimental::properties{ 2185sycl::ext::oneapi::experimental:: use_root_sync }; 2186strata:: q_of ( stream ) -> parallel_for < dpct_kernel_name < class gdn_commit_state_multi_kernel_name >> ( 2187sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 ,(unsigned) h_v ) * sycl:: range ( 1 , RG , S ), sycl:: range ( 1 , RG , S )), 2188properties , [ = ](sycl::nd_item < 3 > ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2189gdn_commit_state_multi_kernel ( state , h , C , gate , beta , h_k , h_v , n_keep ); 2190}); 2191check ( "gdn_commit_state_multi" ); 2192} 2193 2194void gdn_step_norm_qkreg (float * state , const float * h , int conv_channels , const float * gate , const float * beta , 2195const float * z , const float * gamma , float eps , float * y , int h_k , int h_v , int n_tok , 2196const int32_t * n_keep , void * stream , int t_out_begin , void * xq_out ); 2197void gdn_step_norm_multi (float * state , const float * h , int conv_channels , const float * gate , const float * beta , 2198const float * z , const float * gamma , float eps , float * y , int h_k , int h_v , int n_tok , 2199const int32_t * n_keep , void * stream , int t_out_begin , void * xq_out ) { 2200GdnQ81 * const xq = static_cast < GdnQ81 *> ( xq_out ); // S26: non-null = also the outputs' q8_1 (STRATA_QFUSE) 2201if (! state || ! h || ! gate || ! beta || ! z || ! gamma || ! y || h_k <= 0 || h_v % h_k || n_tok < 1 || 2202n_tok > kVerifyMaxT ) { 2203std:: fprintf ( stderr , "gdn_step_norm_multi: invalid arguments\n" ); 2204std:: exit ( 1 ); 2205} 2206static const bool b60_decode_pool = [] { 2207const char * v = std:: getenv ( "STRATA_PRIVATE_B60_NEXT_POOL" ); return v && v [ 0 ] == '1' ; 2208}(); 2209if ( b60_decode_pool && n_keep == nullptr && t_out_begin == 0 && ! xq && 2210conv_channels == 10240 && h_k == 16 && h_v == 48 && eps == 1e-6f && ( n_tok == 2 || n_tok == 4 )) { 2211const unsigned device = dpct:: get_device_id (strata:: q_of ( stream ) -> get_device ()); 2212static std::atomic < uint64_t > reported { 0 }; 2213if ( device < 8 ) { 2214const uint64_t bit = uint64_t( 1 )<<( device * 8 + unsigned ( n_tok )); 2215if (!( reported . fetch_or ( bit ,std:: memory_order_relaxed ) & bit )) 2216std:: fprintf ( stderr , "[B60_GDN_SPLIT_LOCAL] capture device=%u T=%d\n" , device , n_tok ); 2217} 2218launch_b60_gdn_readonly_split_local ( state , h , conv_channels , gate , beta , z , gamma , y , h_k , h_v , n_tok , stream ); 2219check ( "gdn_step_norm_multi" ); 2220return ; 2221} 2222static const bool qk_reg = strata:: bm_default_on ( "STRATA_GDN_QK_REG" , strata:: q_of ( stream ) -> get_device (), false); 2223if ( qk_reg && ! xq && ! n_keep && t_out_begin == 0 && 2224h_k == 16 && h_v == 48 && conv_channels == 10240 && 2225strata:: intel_gpu_gen (strata:: q_of ( stream ) -> get_device ()) == strata::IntelGpuGen:: Battlemage ) { 2226static const bool note = [] { 2227std:: fprintf ( stderr , "strata: GDN QK register broadcast enabled (GRF256)\n" ); 2228return true; 2229}(); 2230(void) note ; 2231auto props = sycl::ext::oneapi::experimental::properties{ 2232sycl::ext::oneapi::experimental:: use_root_sync , 2233sycl::ext::intel::experimental:: grf_size < 256 > }; 2234strata:: q_of ( stream ) -> parallel_for < dpct_kernel_name < class gdn_qk_register >> ( 2235sycl:: nd_range < 3 > (sycl:: range ( 1 , RG , (unsigned) h_v * S ), sycl:: range ( 1 , RG , S )), 2236props , [ = ](sycl::nd_item < 3 > ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 2237gdn_step_norm_multi_kernel < true, false, true > ( 2238state , h , conv_channels , gate , beta , z , gamma , eps , y , 2239h_k , h_v , n_tok , nullptr , 0 , nullptr ); 2240}); 2241check ( "gdn QK register" ); 2242return ; 2243} 2244static const bool qk_grf = [] { const char * e = std:: getenv ( "STRATA_SYCL_GDN_QK_GRF256" ); return e && e [ 0 ] == '1' ; }(); 2245if ( qk_grf && xq_out == nullptr && n_keep == nullptr && h_k == 16 && h_v == 48 && conv_channels == 10240 ) { 2246gdn_step_norm_qkreg ( state , h , conv_channels , gate , beta , z , gamma , eps , y , 2247h_k , h_v , n_tok , n_keep , stream , t_out_begin , xq_out ); 2248return ; 2249} 2250static const bool split = [] { const char * v = std:: getenv ( "STRATA_GDN_SPLIT" ); return v && v [ 0 ] == '1' ; }(); 2251// S26: the split runs where it is faster (gfx1151 harness, h_v 48: the commit 8-19% per call; a window of 2+ rows 2252// 3-13%; one row: no gain, the old kernel stays) - bitwise equal in every case 2253if ( split && ( t_out_begin >= n_tok || n_tok - t_out_begin >= 2 )) { 2254const dpct::dim3 grid ((unsigned) h_v , S / GS_COLS ), block (GS_COLS, RG); 2255if ( t_out_begin >= n_tok ) { // no outputs (the commit): the state only 2256/* 2257DPCT1049: The work-group size passed to the SYCL kernel may 2258exceed the limit. To get the device limit, query 2259info::device::max_work_group_size. Adjust the work-group size if 2260needed. 2261*/ 2262auto exp_props = sycl::ext::oneapi::experimental::properties{ 2263sycl::ext::oneapi::experimental:: use_root_sync }; 2264 2265strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2266-> template parallel_for < 2267dpct_kernel_name < class gdn_state_replay_kernel_e4d4c4, 2268dpct_kernel_scalar < kVerifyMaxT >>> ( 2269sycl:: nd_range < 3 > ( grid * block , block ), exp_props , 2270[ = ](sycl::nd_item < 3 > item_ct1 ) { 2271gdn_state_replay_kernel < kVerifyMaxT > ( 2272state , h , conv_channels , gate , beta , h_k , h_v , 2273n_tok , n_keep ); 2274}); 2275} else { 2276/* 2277DPCT1049: The work-group size passed to the SYCL kernel may 2278exceed the limit. To get the device limit, query 2279info::device::max_work_group_size. Adjust the work-group size if 2280needed. 2281*/ 2282{ 2283auto exp_props = sycl::ext::oneapi::experimental::properties{ 2284sycl::ext::oneapi::experimental:: use_root_sync }; 2285 2286strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2287-> template parallel_for < 2288dpct_kernel_name < class gdn_step_split_kernel_83c676, 2289dpct_kernel_scalar < true >>> ( 2290sycl:: nd_range < 3 > ( grid * block , block ), exp_props , 2291[ = ](sycl::nd_item < 3 > item_ct1 ) { 2292gdn_step_split_kernel < true > ( 2293state , h , conv_channels , gate , beta , y , h_k , 2294h_v , n_tok , n_keep , t_out_begin ); 2295}); 2296} 2297const dpct::dim3 ng ((unsigned) h_v , (unsigned)( n_tok - t_out_begin )); 2298/* 2299DPCT1049: The work-group size passed to the SYCL kernel may 2300exceed the limit. To get the device limit, query 2301info::device::max_work_group_size. Adjust the work-group size if 2302needed. 2303*/ 2304if ( xq ) { 2305auto exp_props = sycl::ext::oneapi::experimental::properties{ 2306sycl::ext::oneapi::experimental:: use_root_sync }; 2307 2308strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2309-> template parallel_for < 2310dpct_kernel_name < class gdn_out_norm_kernel_a650eb, 2311dpct_kernel_scalar < true >>> ( 2312sycl:: nd_range < 3 > ( ng * sycl:: range ( 1 , RG , S ), 2313sycl:: range ( 1 , RG , S )), 2314exp_props , 2315[ = ](sycl::nd_item < 3 > item_ct1 ) 2316[[ sycl :: reqd_sub_group_size ( 32 )]] { 2317gdn_out_norm_kernel < true > ( z , gamma , eps , y , h_v , 2318n_tok , n_keep , 2319t_out_begin , xq ); 2320}); 2321} 2322/* 2323DPCT1049: The work-group size passed to the SYCL kernel may 2324exceed the limit. To get the device limit, query 2325info::device::max_work_group_size. Adjust the work-group size if 2326needed. 2327*/ 2328else { 2329auto exp_props = sycl::ext::oneapi::experimental::properties{ 2330sycl::ext::oneapi::experimental:: use_root_sync }; 2331 2332strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2333-> template parallel_for < 2334dpct_kernel_name < class gdn_out_norm_kernel_f6dfab, 2335dpct_kernel_scalar < false >>> ( 2336sycl:: nd_range < 3 > ( ng * sycl:: range ( 1 , RG , S ), 2337sycl:: range ( 1 , RG , S )), 2338exp_props , 2339[ = ](sycl::nd_item < 3 > item_ct1 ) 2340[[ sycl :: reqd_sub_group_size ( 32 )]] { 2341gdn_out_norm_kernel < false > ( 2342z , gamma , eps , y , h_v , n_tok , n_keep , 2343t_out_begin , nullptr ); 2344}); 2345} 2346} 2347check ( "gdn_step_norm_multi (split)" ); 2348return ; 2349} 2350static const bool commit_split = [] { 2351const char * e = std:: getenv ( "STRATA_GDN_COMMIT_SPLIT" ); 2352return ! e || e [ 0 ] != '0' ; 2353}(); 2354if ( commit_split && n_keep != nullptr && t_out_begin >= n_tok ) { 2355{ 2356auto exp_props = sycl::ext::oneapi::experimental::properties{ 2357sycl::ext::oneapi::experimental:: use_root_sync }; 2358 2359strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2360-> template parallel_for < 2361dpct_kernel_name < class gdn_step_commit_kernel_5d9da7 >> ( 2362sycl:: nd_range < 3 > (sycl:: range ( 1 , 4u , (unsigned) h_v ) * 2363sycl:: range ( 1 , RG , 32 ), 2364sycl:: range ( 1 , RG , 32 )), 2365exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 2366gdn_step_commit_kernel ( state , h , conv_channels , gate , 2367beta , h_k , h_v , n_keep ); 2368}); 2369} 2370check ( "gdn_step_commit" ); 2371return ; 2372} 2373const dpct::dim3 g1 ((unsigned) h_v ), b1 (S, RG); 2374const bool all_out = n_keep == nullptr && t_out_begin <= 0 ; 2375if ( xq && t_out_begin < n_tok ) { 2376/* 2377DPCT1049: The work-group size passed to the SYCL kernel may exceed 2378the limit. To get the device limit, query 2379info::device::max_work_group_size. Adjust the work-group size if needed. 2380*/ 2381if ( all_out ) { 2382auto exp_props = sycl::ext::oneapi::experimental::properties{ 2383sycl::ext::oneapi::experimental:: use_root_sync }; 2384 2385strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2386-> template parallel_for < dpct_kernel_name < 2387class gdn_step_norm_multi_kernel_42ae26, 2388dpct_kernel_scalar < true > , dpct_kernel_scalar < true >>> ( 2389sycl:: nd_range < 3 > ( g1 * b1 , b1 ), exp_props , 2390[ = ](sycl::nd_item < 3 > item_ct1 ) 2391[[ sycl :: reqd_sub_group_size ( 32 )]] { 2392gdn_step_norm_multi_kernel < true, true > ( 2393state , h , conv_channels , gate , beta , z , gamma , 2394eps , y , h_k , h_v , n_tok , nullptr , 0 , xq ); 2395}); 2396} 2397/* 2398DPCT1049: The work-group size passed to the SYCL kernel may exceed 2399the limit. To get the device limit, query 2400info::device::max_work_group_size. Adjust the work-group size if needed. 2401*/ 2402else { 2403auto exp_props = sycl::ext::oneapi::experimental::properties{ 2404sycl::ext::oneapi::experimental:: use_root_sync }; 2405 2406strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2407-> template parallel_for < dpct_kernel_name < 2408class gdn_step_norm_multi_kernel_42ae26, 2409dpct_kernel_scalar < false > , dpct_kernel_scalar < true >>> ( 2410sycl:: nd_range < 3 > ( g1 * b1 , b1 ), exp_props , 2411[ = ](sycl::nd_item < 3 > item_ct1 ) 2412[[ sycl :: reqd_sub_group_size ( 32 )]] { 2413gdn_step_norm_multi_kernel < false, true > ( 2414state , h , conv_channels , gate , beta , z , gamma , 2415eps , y , h_k , h_v , n_tok , n_keep , t_out_begin , 2416xq ); 2417}); 2418} 2419} else if ( all_out ) { 2420/* 2421DPCT1049: The work-group size passed to the SYCL kernel may exceed 2422the limit. To get the device limit, query 2423info::device::max_work_group_size. Adjust the work-group size if needed. 2424*/ 2425auto exp_props = sycl::ext::oneapi::experimental::properties{ 2426sycl::ext::oneapi::experimental:: use_root_sync }; 2427 2428strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2429-> template parallel_for < dpct_kernel_name < 2430class gdn_step_norm_multi_kernel_42ae26, 2431dpct_kernel_scalar < true > , dpct_kernel_scalar < false >>> ( 2432sycl:: nd_range < 3 > ( g1 * b1 , b1 ), exp_props , 2433[ = ](sycl::nd_item < 3 > item_ct1 ) 2434[[ sycl :: reqd_sub_group_size ( 32 )]] { 2435gdn_step_norm_multi_kernel < true, false > ( 2436state , h , conv_channels , gate , beta , z , gamma , eps , 2437y , h_k , h_v , n_tok , nullptr , 0 , nullptr ); 2438}); 2439} else { 2440/* 2441DPCT1049: The work-group size passed to the SYCL kernel may exceed 2442the limit. To get the device limit, query 2443info::device::max_work_group_size. Adjust the work-group size if needed. 2444*/ 2445auto exp_props = sycl::ext::oneapi::experimental::properties{ 2446sycl::ext::oneapi::experimental:: use_root_sync }; 2447 2448strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2449-> template parallel_for < dpct_kernel_name < 2450class gdn_step_norm_multi_kernel_42ae26, 2451dpct_kernel_scalar < false > , dpct_kernel_scalar < false >>> ( 2452sycl:: nd_range < 3 > ( g1 * b1 , b1 ), exp_props , 2453[ = ](sycl::nd_item < 3 > item_ct1 ) 2454[[ sycl :: reqd_sub_group_size ( 32 )]] { 2455gdn_step_norm_multi_kernel < false, false > ( 2456state , h , conv_channels , gate , beta , z , gamma , eps , 2457y , h_k , h_v , n_tok , n_keep , t_out_begin , nullptr ); 2458}); 2459} 2460check ( "gdn_step_norm_multi" ); 2461} 2462 2463namespace { 2464__dpct_inline__ void wait_flag_ge_kernel ( const volatile uint32_t * flag , 2465uint32_t value , uint32_t spin_max ) { 2466for (uint32_t spin = 0 ; spin < spin_max && strata:: sys_load ( flag ) < value ; ++ spin ) strata_spin_pause (); 2467/* 2468DPCT1078: Consider replacing memory_order::acq_rel with 2469memory_order::seq_cst for correctness if strong memory order restrictions 2470are needed. 2471*/ 2472sycl:: atomic_fence (sycl::memory_order:: acq_rel , sycl::memory_scope:: system ); 2473} 2474} // namespace 2475 2476namespace { 2477// one thread per window entry: up to kVerifyMaxT tokens x 10 routed experts (80), so 128 (#646 had 64: a window of 2478// 7+ tokens lost its last entries) 2479constexpr int kResidentPlanMax = 128 ; 2480static_assert( kVerifyMaxT * 10 <= kResidentPlanMax , "resident_plan: one thread per entry" ); 2481// the parallel scan keeps one partial sum per warp in s_wsum[4] and packs (entries << 16 | groups) in an int 2482static_assert( kResidentPlanMax <= 128 && kResidentPlanMax % 32 == 0 , "resident_plan: at most 4 warps" ); 2483static_assert( kResidentPlanMax < 32768 , "resident_plan: the packed entry count must stay below 2^15" ); 2484/* 2485DPCT1110: The total declared local variable size in device function 2486resident_plan_kernel exceeds 128 bytes and may cause high register pressure. 2487Consult with your hardware vendor to find the total register size available and 2488adjust the code, or use smaller sub-group size to avoid high register pressure. 2489*/ 2490__dpct_inline__ void 2491resident_plan_kernel ( const int32_t * __restrict__ ids , int n , int k , 2492const int32_t * __restrict__ res , int n_expert , 2493const uint8_t * cache_base , 2494const unsigned long long * slot_off , long long blob , 2495int32_t * __restrict__ pl , long long capx , uint32_t * skip , 2496uint32_t ring , const unsigned long long * __restrict__ mir , 2497volatile uint32_t * plan_err , int32_t * usage ) { 2498auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 2499auto & s_ids = * sycl::ext::oneapi:: group_local_memory_for_overwrite < 2500int32_t[ kResidentPlanMax ] > ( 2501sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 2502auto & s_excl = * sycl::ext::oneapi:: group_local_memory_for_overwrite < 2503int32_t[ kResidentPlanMax ] > ( 2504sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 2505auto & s_wsum = 2506* sycl::ext::oneapi:: group_local_memory_for_overwrite < int32_t[ 4 ] > ( 2507sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 2508auto & s_bad = * sycl::ext::oneapi:: group_local_memory_for_overwrite < int > ( 2509sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 2510const int tid = item_ct1 . get_local_id ( 2 ); 2511if ( tid == 0 ) s_bad = 0 ; 2512item_ct1 . barrier (sycl::access::fence_space:: local_space ); 2513 2514int32_t eid = -1 ; 2515int32_t slot = -1 ; 2516unsigned long long maddr = 0 ; // SYCL port: a host-mirrored expert (not in VRAM): its pinned mirror, read over PCIe 2517if ( tid < n ) { 2518eid = ids [ tid ]; 2519s_ids [ tid ] = eid ; 2520slot = ( eid >= 0 && eid < n_expert ) ? res [ eid ] : -1 ; 2521if ( slot < 0 && eid >= 0 && eid < n_expert && mir != nullptr ) maddr = mir [ eid ]; 2522if ( usage != nullptr && eid >= 0 && eid < n_expert ) // the adaptive tier's routing counts (device side) 2523sycl:: atomic_ref < int32_t, sycl::memory_order::relaxed, sycl::memory_scope::device, 2524sycl::access::address_space::global_space > ( usage [ eid ]). fetch_add ( 1 ); 2525if ( slot < 0 && maddr == 0 ) 2526dpct:: atomic_fetch_or < sycl::access::address_space::generic_space > ( 2527& s_bad , 1 ); 2528} 2529item_ct1 . barrier (sycl::access::fence_space:: local_space ); 2530if ( s_bad ) { 2531if ( tid == 0 && skip != nullptr ) * skip = 0 ; 2532if ( tid == 0 && skip == nullptr ) { // #871: the all-resident graph has no host plan to fall back on 2533pl [ 0 ] = 0 ; pl [ 1 ] = 0 ; pl [ 2 ] = 0 ; // an empty plan: no expert runs on a stale pointer 2534/* 2535DPCT1078: Consider replacing memory_order::acq_rel with 2536memory_order::seq_cst for correctness if strong memory order 2537restrictions are needed. 2538*/ 2539if ( plan_err != nullptr ) { 2540* plan_err = 1 ; 2541sycl:: atomic_fence (sycl::memory_order:: acq_rel , 2542sycl::memory_scope:: system ); 2543} 2544} 2545return ; 2546} 2547 2548int first_j = tid ; 2549int rank_in_group = 0 ; 2550int count_same = 0 ; 2551if ( tid < n ) { 2552#pragma unroll 2553for (int j = 0 ; j < n ; ++ j ) { 2554if ( s_ids [ j ] == eid ) { 2555if ( j < first_j ) first_j = j ; 2556if ( j < tid ) ++ rank_in_group ; 2557++ count_same ; 2558} 2559} 2560} 2561const bool is_first = ( tid < n && first_j == tid && ( slot >= 0 || maddr != 0 )); 2562const int my_cnt = is_first ? count_same : 0 ; 2563const int my_pack = ( my_cnt << 16 ) | ( is_first ? 1 : 0 ); 2564const int lane = tid & 31 ; 2565const int warp = tid >> 5 ; 2566const int n_warps = item_ct1 . get_local_range ( 2 ) / 32 ; 2567int pref = my_pack ; 2568#pragma unroll 2569for (int d = 1 ; d < 32 ; d <<= 1 ) { 2570/* 2571DPCT1108: '__shfl_up_sync' was migrated with the experimental 2572feature masked sub_group function which may not be supported by all 2573compilers or runtimes. You may need to adjust the code. 2574*/ 2575const int up = dpct::experimental:: shift_sub_group_right ( 25760xffffffffu , sycl::ext::oneapi::this_work_item:: get_sub_group (), 2577pref , d ); 2578if ( lane >= d ) pref += up ; 2579} 2580s_excl [ tid ] = pref - my_pack ; 2581if ( lane == 31 ) { 2582s_wsum [ warp ] = pref ; 2583} 2584/* 2585DPCT1065: Consider replacing sycl::nd_item::barrier() with 2586sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 2587performance if there is no access to global memory. 2588*/ 2589item_ct1 . barrier (); 2590 2591int32_t * counts = pl ; 2592int32_t * start = pl + 4 ; 2593int32_t * dst = start + capx + 1 ; 2594int32_t * tok = dst + capx ; 2595const long long ptr_off = (( 4 + ( capx + 1 ) + 2 * capx ) + 1 ) & ~ 1ll ; 2596unsigned long long * ptr = (unsigned long long * ) ( pl + ptr_off ); 2597int32_t * start2 = pl + ptr_off + 4 * capx ; 2598 2599if ( is_first ) { 2600int tot = s_excl [ tid ]; 2601#pragma unroll 2602for (int w = 0 ; w < n_warps ; ++ w ) { 2603if ( w < warp ) tot += s_wsum [ w ]; 2604} 2605const int grp_idx = tot & 0xffff ; 2606const int ent_start = tot >> 16 ; 2607ptr [ grp_idx ] = slot >= 0 ? (unsigned long long) ( cache_base + ( slot_off ? (size_t) slot_off [ slot ] : (size_t) slot * (size_t) blob )) 2608: maddr ; 2609start [ grp_idx ] = ent_start ; 2610} 2611if ( tid < n ) { 2612const int fj_warp = first_j >> 5 ; 2613int fj_tot = s_excl [ first_j ]; 2614#pragma unroll 2615for (int w = 0 ; w < n_warps ; ++ w ) { 2616if ( w < fj_warp ) fj_tot += s_wsum [ w ]; 2617} 2618const int out_idx = ( fj_tot >> 16 ) + rank_in_group ; 2619dst [ out_idx ] = tid ; 2620tok [ out_idx ] = tid / k ; 2621} 2622if ( tid == 0 ) { 2623int groups = 0 ; 2624for (int w = 0 ; w < n_warps ; ++ w ) groups += s_wsum [ w ]; 2625groups &= 0xffff ; 2626start [ groups ] = n ; 2627start2 [ 0 ] = n ; 2628counts [ 0 ] = groups ; 2629counts [ 1 ] = n ; 2630counts [ 2 ] = 0 ; 2631if ( skip != nullptr ) { 2632/* 2633DPCT1078: Consider replacing memory_order::acq_rel with 2634memory_order::seq_cst for correctness if strong memory order 2635restrictions are needed. 2636*/ 2637sycl:: atomic_fence (sycl::memory_order:: acq_rel , 2638sycl::memory_scope:: device ); 2639strata:: sys_store ( skip , ring ); 2640} 2641} 2642} 2643// The same plan in one block of 128 threads (n <= 128): thread i owns entry i. Groups are the distinct experts in 2644// order of first occurrence; group g's entries are its occurrences in increasing i - exactly the loop above 2645// (S26: the one-thread loop took ~44 us per call on gfx1151, 48 per window). 2646void resident_plan_par_kernel ( const int32_t * __restrict__ ids , int n , int k , const int32_t * __restrict__ res , 2647int n_expert , const uint8_t * cache_base , const unsigned long long * slot_off , 2648long long blob , int32_t * __restrict__ pl , long long capx , uint32_t * skip , 2649uint32_t ring ) { 2650auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 2651auto & s_id = * sycl::ext::oneapi:: group_local_memory_for_overwrite < int32_t[ 128 ] > ( 2652sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 2653auto & s_first = 2654* sycl::ext::oneapi:: group_local_memory_for_overwrite < int[ 128 ] > ( 2655sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 2656auto & s_size = 2657* sycl::ext::oneapi:: group_local_memory_for_overwrite < int[ 128 ] > ( 2658sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 2659auto & s_bad = * sycl::ext::oneapi:: group_local_memory_for_overwrite < int > ( 2660sycl::ext::oneapi::this_work_item:: get_work_group < 3 > ()); 2661const int i = item_ct1 . get_local_id ( 2 ); 2662if ( i == 0 ) s_bad = 0 ; 2663item_ct1 . barrier (sycl::access::fence_space:: local_space ); 2664int32_t e = -1 ; 2665if ( i < n ) { 2666e = ids [ i ]; 2667s_id [ i ] = e ; 2668if ( e < 0 || e >= n_expert || res [ e ] < 0 ) s_bad = 1 ; 2669} 2670item_ct1 . barrier (sycl::access::fence_space:: local_space ); 2671if ( s_bad ) { if ( i == 0 ) * skip = 0 ; return ; } 2672int first = i , rank = 0 , size = 0 ; 2673if ( i < n ) { 2674for (int j = 0 ; j < n ; ++ j ) { 2675const bool same = s_id [ j ] == e ; 2676if ( same && j < first ) first = j ; 2677if ( same && j < i ) ++ rank ; 2678size += same ; 2679} 2680} 2681if ( i < n ) { s_first [ i ] = first ; s_size [ i ] = size ; } 2682/* 2683DPCT1065: Consider replacing sycl::nd_item::barrier() with 2684sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 2685performance if there is no access to global memory. 2686*/ 2687item_ct1 . barrier (); 2688int32_t * counts = pl ; 2689int32_t * start = pl + 4 ; 2690int32_t * dst = start + capx + 1 ; 2691int32_t * tok = dst + capx ; 2692const long long ptr_off = (( 4 + ( capx + 1 ) + 2 * capx ) + 1 ) & ~ 1ll ; 2693unsigned long long * ptr = (unsigned long long * ) ( pl + ptr_off ); 2694int32_t * start2 = pl + ptr_off + 4 * capx ; 2695if ( i < n ) { 2696// group of entry i = the number of first occurrences before `first`; its start = their sizes' sum 2697int group = 0 , gstart = 0 ; 2698#pragma unroll 2699for (int j = 0 ; j < first ; ++ j ) 2700if ( s_first [ j ] == j ) { ++ group ; gstart += s_size [ j ]; } 2701if ( i == first ) { 2702const int32_t slot = res [ e ]; 2703ptr [ group ] = (unsigned long long) ( cache_base + ( slot_off ? (size_t) slot_off [ slot ] : (size_t) slot * (size_t) blob )); 2704start [ group ] = gstart ; 2705} 2706dst [ gstart + rank ] = i ; 2707tok [ gstart + rank ] = i / k ; 2708if ( i == 0 ) { 2709int groups = 0 ; 2710#pragma unroll 2711for (int j = 0 ; j < n ; ++ j ) groups += s_first [ j ] == j ; 2712start [ groups ] = n ; 2713start2 [ 0 ] = n ; 2714counts [ 0 ] = groups ; 2715counts [ 1 ] = n ; 2716counts [ 2 ] = 0 ; 2717} 2718} 2719/* 2720DPCT1078: Consider replacing memory_order::acq_rel with 2721memory_order::seq_cst for correctness if strong memory order restrictions 2722are needed. 2723*/ 2724sycl:: atomic_fence (sycl::memory_order:: acq_rel , sycl::memory_scope:: device ); 2725/* 2726DPCT1065: Consider replacing sycl::nd_item::barrier() with 2727sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 2728performance if there is no access to global memory. 2729*/ 2730item_ct1 . barrier (); 2731if ( i == 0 ) * skip = ring ; 2732} 2733__dpct_inline__ void wait_flag_ge_or_kernel ( const volatile uint32_t * flag , 2734uint32_t value , 2735const volatile uint32_t * skip , uint32_t spin_max ) { 2736if (strata:: sys_load ( skip ) == value ) return ; 2737for (uint32_t spin = 0 ; spin < spin_max && strata:: sys_load ( flag ) < value ; ++ spin ) strata_spin_pause (); 2738/* 2739DPCT1078: Consider replacing memory_order::acq_rel with 2740memory_order::seq_cst for correctness if strong memory order restrictions 2741are needed. 2742*/ 2743sycl:: atomic_fence (sycl::memory_order:: acq_rel , sycl::memory_scope:: system ); 2744} 2745__dpct_inline__ void copy_i32_unless_kernel (int32_t * __restrict__ dst , 2746const volatile int32_t * src , int n , 2747const uint32_t * skip , 2748uint32_t value ) { 2749auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 2750if (strata:: sys_load ( skip ) == value ) return ; 2751#pragma unroll 2752for (int i = item_ct1 . get_local_id ( 2 ); i < n ; 2753i += item_ct1 . get_local_range ( 2 )) dst [ i ] = src [ i ]; 2754} 2755/* 2756DPCT1052: SYCL does not support the member access for a volatile qualified 2757vector type. The volatile qualifier was removed. You may need to rewrite the 2758code. 2759*/ 2760__dpct_inline__ void copy_or_zero_kernel (sycl::float4 * __restrict__ dst , 2761const sycl::float4 * src , long long n4 , 2762const uint32_t * skip , uint32_t value , bool pair ) { 2763auto item_ct1 = sycl::ext::oneapi::this_work_item:: get_nd_item < 3 > (); 2764const bool zero = * skip == value ; 2765#pragma unroll 2766for (long long i = 2767(long long) item_ct1 . get_group ( 2 ) * item_ct1 . get_local_range ( 2 ) + 2768item_ct1 . get_local_id ( 2 ); 2769i < n4 ; i += (long long) item_ct1 . get_group_range ( 2 ) * 2770item_ct1 . get_local_range ( 2 )) 2771dst [ i ] = zero ? sycl:: float4 ( 0.f , 0.f , 0.f , 0.f ) 2772: ( pair ? strata:: load_mapped_float4 ( src + i ) : const_cast < const sycl::float4 *> ( src )[ i ]); 2773} 2774} // namespace 2775 2776namespace { 2777const int32_t * g_mirror_res = nullptr ; 2778const unsigned long long * g_mirror_table = nullptr ; 2779int32_t * g_usage = nullptr ; 2780long long g_mirror_entries = 0 ; // 0: unchecked 2781// #1980: the layer slice `res_layer` belongs to the table the mirror was set for (a later stage passes its own) 2782bool in_mirror_table ( const int32_t * res_layer ) { 2783if ( g_mirror_res == nullptr || res_layer < g_mirror_res ) return false; 2784return g_mirror_entries <= 0 || res_layer - g_mirror_res < g_mirror_entries ; 2785} 2786} 2787void resident_plan_set_mirror ( const int32_t * d_res , const unsigned long long * mirror_table , int32_t * usage , long long entries ) { 2788g_mirror_entries = entries ; 2789g_usage = usage ; 2790g_mirror_res = d_res ; 2791g_mirror_table = mirror_table ; 2792} 2793void resident_plan ( const int32_t * ids , int n_entries , int k , const int32_t * res_layer , int n_expert , 2794const uint8_t * cache_base , const unsigned long long * slot_off , long long blob , int32_t * plan , 2795long long capx , uint32_t * skip , uint32_t ring , void * stream , uint32_t * plan_err ) { 2796const unsigned long long * mir = nullptr ; // SYCL port: the layer's slice of the host-mirror table, if any 2797int32_t * use = nullptr ; // the layer's slice of the device routing counts, if on 2798if ( g_mirror_table != nullptr && in_mirror_table ( res_layer )) mir = g_mirror_table + ( res_layer - g_mirror_res ); 2799if ( g_usage != nullptr && in_mirror_table ( res_layer )) use = g_usage + ( res_layer - g_mirror_res ); 2800static const bool compact = strata:: bm_default_on ( "STRATA_RESIDENT_PLAN_COMPACT" , strata:: q_of ( stream ) -> get_device (), false); 2801const int threads = compact ? std:: max ( 32 , (( n_entries + 31 ) / 32 ) * 32 ) : kResidentPlanMax ; 2802{ 2803auto exp_props = sycl::ext::oneapi::experimental::properties{ 2804sycl::ext::oneapi::experimental:: use_root_sync }; 2805 2806strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2807-> template parallel_for < dpct_kernel_name < class resident_plan_kernel_db6b7a >> ( 2808sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , threads ), 2809sycl:: range ( 1 , 1 , threads )), 2810exp_props , 2811[ = ](sycl::nd_item < 3 > item_ct1 ) 2812[[ sycl :: reqd_sub_group_size ( 32 )]] { 2813resident_plan_kernel ( 2814ids , n_entries , k , res_layer , n_expert , cache_base , 2815slot_off , blob , plan , capx , skip , ring , mir , plan_err , use ); 2816}); 2817} 2818check ( "resident_plan" ); 2819} 2820void wait_flag_ge_or ( const uint32_t * flag , uint32_t value , const uint32_t * skip , void * stream ) { 2821const uint32_t spin_max = strata:: spin_max ( * strata:: q_of ( stream )); 2822{ 2823auto exp_props = sycl::ext::oneapi::experimental::properties{ 2824sycl::ext::oneapi::experimental:: use_root_sync }; 2825 2826strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2827-> template parallel_for < 2828dpct_kernel_name < class wait_flag_ge_or_kernel_2b2de3 >> ( 2829sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 1 ), sycl:: range ( 1 , 1 , 1 )), 2830exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 2831wait_flag_ge_or_kernel ( flag , value , skip , spin_max ); 2832}); 2833} 2834check ( "wait_flag_ge_or" ); 2835} 2836void copy_i32_from_mapped_unless (int32_t * dst , const int32_t * src , long long n , const uint32_t * skip , uint32_t value , 2837void * stream ) { 2838if ( n <= 0 ) return ; 2839{ 2840auto exp_props = sycl::ext::oneapi::experimental::properties{ 2841sycl::ext::oneapi::experimental:: use_root_sync }; 2842 2843strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2844-> template parallel_for < 2845dpct_kernel_name < class copy_i32_unless_kernel_d9d761 >> ( 2846sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 128 ), 2847sycl:: range ( 1 , 1 , 128 )), 2848exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 2849copy_i32_unless_kernel ( dst , ( const volatile int32_t * ) src , 2850(int) n , skip , value ); 2851}); 2852} 2853check ( "copy_i32_from_mapped_unless" ); 2854} 2855void copy_or_zero_from_mapped (float * dst , const float * src , long long n , const uint32_t * skip , uint32_t value , 2856void * stream ) { 2857if ( n <= 0 ) return ; 2858const long long n4 = n / 4 ; 2859const bool pair = strata:: a770_fast (); 2860const int blocks = (int) (( n4 + 255 ) / 256 < 64 ? ( n4 + 255 ) / 256 : 64 ); 2861{ 2862auto exp_props = sycl::ext::oneapi::experimental::properties{ 2863sycl::ext::oneapi::experimental:: use_root_sync }; 2864 2865strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2866-> template parallel_for < dpct_kernel_name < class copy_or_zero_kernel_5ef081 >> ( 2867sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , blocks ) * 2868sycl:: range ( 1 , 1 , 256 ), 2869sycl:: range ( 1 , 1 , 256 )), 2870exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 2871copy_or_zero_kernel ((sycl::float4 * ) dst , 2872( const sycl::float4 * ) src , n4 , 2873skip , value , pair ); 2874}); 2875} 2876check ( "copy_or_zero_from_mapped" ); 2877} 2878 2879void wait_flag_ge ( const uint32_t * flag , uint32_t value , void * stream ) { 2880const uint32_t spin_max = strata:: spin_max ( * strata:: q_of ( stream )); 2881{ 2882auto exp_props = sycl::ext::oneapi::experimental::properties{ 2883sycl::ext::oneapi::experimental:: use_root_sync }; 2884 2885strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2886-> template parallel_for < dpct_kernel_name < class wait_flag_ge_kernel_d7debf >> ( 2887sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , 1 ), sycl:: range ( 1 , 1 , 1 )), 2888exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 2889wait_flag_ge_kernel ( flag , value , spin_max ); 2890}); 2891} 2892check ( "wait_flag_ge" ); 2893} 2894 2895void embedding_gather_dev ( const uint8_t * codes , const float * scales , const float * offsets , const int32_t * tokens , 2896int n_tok , int64_t n , int code_bits , int code_bias , int group_elems , uint64_t row_codes , 2897uint64_t row_groups , float * out , void * stream ) { 2898{ 2899auto exp_props = sycl::ext::oneapi::experimental::properties{ 2900sycl::ext::oneapi::experimental:: use_root_sync }; 2901 2902strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2903-> template parallel_for < 2904dpct_kernel_name < class embedding_gather_dev_kernel_600016 >> ( 2905sycl:: nd_range < 3 > (sycl:: range ( 1 , (unsigned) n_tok , 2906(unsigned)(( n + 255 ) / 256 )) * 2907sycl:: range ( 1 , 1 , 256 ), 2908sycl:: range ( 1 , 1 , 256 )), 2909exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 2910embedding_gather_dev_kernel ( 2911codes , scales , offsets , tokens , n , code_bits , code_bias , 2912group_elems , row_codes , row_groups , out ); 2913}); 2914} 2915check ( "embedding_gather_dev" ); 2916} 2917 2918void broadcast_streams ( const float * x , float * R , int64_t n_embd , int hc , int n_tok , void * stream ) { 2919{ 2920auto exp_props = sycl::ext::oneapi::experimental::properties{ 2921sycl::ext::oneapi::experimental:: use_root_sync }; 2922 2923strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2924-> template parallel_for < 2925dpct_kernel_name < class broadcast_streams_kernel_aa4f6f >> ( 2926sycl:: nd_range < 3 > ( 2927sycl:: range ( 1 , (unsigned) n_tok , 2928(unsigned)(( n_embd * hc + 255 ) / 256 )) * 2929sycl:: range ( 1 , 1 , 256 ), 2930sycl:: range ( 1 , 1 , 256 )), 2931exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 2932broadcast_streams_kernel ( x , R , n_embd , hc ); 2933}); 2934} 2935check ( "broadcast_streams" ); 2936} 2937 2938void copy_indexed (float * dst , const float * src , int64_t stride , const int32_t * index , int64_t n , void * stream ) { 2939const unsigned blocks = (unsigned) (( n + 255 ) / 256 < 64 ? ( n + 255 ) / 256 : 64 ); 2940{ 2941auto exp_props = sycl::ext::oneapi::experimental::properties{ 2942sycl::ext::oneapi::experimental:: use_root_sync }; 2943 2944strata::kprof:: kp (strata:: q_of ( stream ), __FILE__ , __func__ , __LINE__ ) 2945-> template parallel_for < dpct_kernel_name < class copy_indexed_kernel_b12e23 >> ( 2946sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , blocks ) * 2947sycl:: range ( 1 , 1 , 256 ), 2948sycl:: range ( 1 , 1 , 256 )), 2949exp_props , [ = ](sycl::nd_item < 3 > item_ct1 ) { 2950copy_indexed_kernel ( dst , src , stride , index , n ); 2951}); 2952} 2953check ( "copy_indexed" ); 2954} 2955 2956// Opt-in GPU stage timestamps. DG2 has no comparable device-wide clock, so 2957// read a live host USM clock uncached (the fork's gpu_stamp implementation). 2958// The writer is joined before runtime teardown and exists only in profile mode. 2959namespace { 2960uint64_t profile_now_ns () { 2961return std::chrono:: duration_cast < std::chrono::nanoseconds > ( 2962std::chrono::steady_clock:: now (). time_since_epoch ()). count (); 2963} 2964struct ProfileHostClock { 2965sycl::context context ; 2966uint64_t * word ; 2967std::atomic < bool > stop {false}; 2968std::thread writer ; 2969explicit ProfileHostClock ( const sycl::queue & q ) : context ( q . get_context ()), 2970word (sycl:: malloc_host < uint64_t > ( 8 , context )) { 2971if (! word ) throw std:: runtime_error ( "gpu_stamp: host clock allocation failed" ); 2972__atomic_store_n ( word , profile_now_ns (), __ATOMIC_RELEASE ); 2973writer = std:: thread ([ this ] { 2974while (! stop . load (std:: memory_order_relaxed )) { 2975__atomic_store_n ( word , profile_now_ns (), __ATOMIC_RELEASE ); 2976for (int i = 0 ; i < 8 ; ++ i ) __builtin_ia32_pause (); 2977} 2978}); 2979} 2980~ ProfileHostClock () { 2981stop . store (true, std:: memory_order_relaxed ); 2982if ( writer . joinable ()) writer . join (); 2983// Keep the word alive until context teardown, as in the fork: a graph 2984// may still retain its pointer when process-level statics are destroyed. 2985} 2986}; 2987uint64_t * profile_clock_word ( const sycl::queue & q ) { 2988static std::mutex mu ; 2989static std::vector < std::unique_ptr < ProfileHostClock >> clocks ; 2990std::lock_guard < std::mutex > lock (mu); 2991for ( const auto & clock : clocks ) if ( clock -> context == q . get_context ()) return clock -> word ; 2992clocks . push_back (std:: make_unique < ProfileHostClock > ( q )); 2993return clocks . back () -> word ; 2994} 2995} 2996 2997void gpu_stamp (unsigned long long * buf , int i , void * stream ) { 2998auto & q = * strata:: q_of ( stream ); 2999uint64_t * word = profile_clock_word ( q ); 3000strata::kprof:: kp ( & q , __FILE__ , __func__ , __LINE__ ) -> single_task ([ = ] { 3001sycl:: atomic_fence (sycl::memory_order:: acquire , sycl::memory_scope:: system ); 3002buf [ i ] = strata:: sys_load_mapped ( word ); 3003}); 3004} 3005 3006class B60GdnReadonlySplitLocal; 3007class B60GdnReadonlySplitLocalNorm; 3008void launch_b60_gdn_readonly_split_local (float * state , const float * h ,int C , 3009const float * gate , const float * beta , const float * z , const float * gamma ,float * y , 3010int HK ,int HV ,int T ,void * stream ){ 3011const auto props = sycl::ext::oneapi::experimental::properties{ 3012sycl::ext::oneapi::experimental:: use_root_sync }; 3013auto * q = strata:: q_of ( stream ); 3014q -> parallel_for < B60GdnReadonlySplitLocal > ( 3015sycl:: nd_range < 3 > (sycl:: range < 3 > ( 1 , 4 * RG ,size_t( HV ) * GS_COLS ),sycl:: range < 3 > ( 1 , RG , GS_COLS )), 3016props ,[ = ](sycl::nd_item < 3 > ) { 3017b60_gdn_split_local_kernel < true > ( state , h , C , gate , beta , y , HK , HV , T , nullptr , 0 ); 3018}); 3019q -> parallel_for < B60GdnReadonlySplitLocalNorm > ( 3020sycl:: nd_range < 3 > (sycl:: range < 3 > ( 1 , T * RG ,size_t( HV ) * S ),sycl:: range < 3 > ( 1 , RG , S )), 3021props ,[ = ](sycl::nd_item < 3 > ) [[ sycl :: reqd_sub_group_size ( 32 )]] { 3022gdn_out_norm_kernel < false > ( z , gamma , 1e-6f , y , HV , T , nullptr , 0 , nullptr ); 3023}); 3024} 3025 3026void gdn_step_norm_qkreg (float * state , const float * h , int conv_channels , const float * gate , const float * beta , 3027const float * z , const float * gamma , float eps , float * y , int h_k , int h_v , int n_tok , 3028const int32_t * n_keep , void * stream , int t_out_begin , void * xq_out ) { 3029if ( xq_out != nullptr ) throw std:: invalid_argument ( "SYCL gdn_step_norm_qkreg: fused q8_1 output is not implemented" ); 3030if (! state || ! h || ! gate || ! beta || ! z || ! gamma || ! y || h_k <= 0 || h_v % h_k || n_tok < 1 || 3031n_tok > kVerifyMaxT ) { 3032std:: fprintf ( stderr , "gdn_step_norm_qkreg: invalid arguments\n" ); 3033std:: exit ( 1 ); 3034} 3035/* 3036DPCT1049: The work-group size passed to the SYCL kernel may exceed the 3037limit. To get the device limit, query info::device::max_work_group_size. 3038Adjust the work-group size if needed. 3039*/ 3040{ 3041auto exp_props = sycl::ext::oneapi::experimental::properties{ 3042sycl::ext::oneapi::experimental:: use_root_sync ,sycl::ext::intel::experimental:: grf_size < 256 > }; 3043 3044strata:: q_of ( stream ) 3045-> parallel_for < 3046dpct_kernel_name < class gdn_step_norm_qkreg_kernel_42ae26 >> ( 3047sycl:: nd_range < 3 > (sycl:: range ( 1 , 1 , (unsigned) h_v ) * 3048sycl:: range ( 1 , RG , S ), 3049sycl:: range ( 1 , RG , S )), 3050exp_props , 3051[ = ](sycl::nd_item < 3 > item_ct1 ) 3052[[ sycl :: reqd_sub_group_size ( 32 )]] { 3053gdn_step_norm_qkreg_kernel ( 3054state , h , conv_channels , gate , beta , z , gamma , eps , 3055y , h_k , h_v , n_tok , n_keep , t_out_begin ); 3056}); 3057} 3058check ( "gdn_step_norm_qkreg" ); 3059} 3060 3061} // namespace strata::kernels