okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
86bed1c92
main
1// src/kernels/cuda/fused_gdn.cu - see include/strata/kernels/fused_gdn.hpp. 2#define DPCT_PROFILING_ENABLED 3#include <sycl/sycl.hpp> 4#include "strata/kprof.hpp" 5#include "strata/core/geometry.hpp" 6#include <dpct/dpct.hpp> 7#include "strata/sycl_queue.hpp" 8#include "strata/kernels/fused_gdn.hpp" 9 10#include <cstdio> 11#include <cstdlib> 12 13namespace strata::kernels { 14namespace { 15 16constexpr int S = 128 ;// state size (rows = cols = 128) 17static_assert(S == strata::core::kCompiledGeometry .ssm_state_size ,"fused_gdn is compiled for core/geometry.hpp" ); 18constexpr int RG = 4 ;// row groups 19constexpr int RPG = S /RG ;// 32 rows per thread 20 21/* 22DPCT1110: The total declared local variable size in device function 23gdn_step_norm_kernel exceeds 128 bytes and may cause high register pressure. 24Consult with your hardware vendor to find the total register size available and 25adjust the code, or use smaller sub-group size to avoid high register pressure. 26*/ 27__dpct_inline__ void gdn_step_norm_kernel ( 28float * __restrict__state ,const float * __restrict__q , 29const float * __restrict__k ,const float * __restrict__v , 30const float * __restrict__gate ,const float * __restrict__beta , 31const float * __restrict__z ,const float * __restrict__gamma ,float eps , 32float * __restrict__y ,int h_k ,int h_v ) { 33auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 34auto & sk = * sycl::ext::oneapi::group_local_memory_for_overwrite < float [S ]> ( 35 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 36auto & sq = * sycl::ext::oneapi::group_local_memory_for_overwrite < float [S ]> ( 37 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 38auto & red_kv = 39* sycl::ext::oneapi::group_local_memory_for_overwrite < float [RG ][S ]> ( 40 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 41auto & red_o = 42* sycl::ext::oneapi::group_local_memory_for_overwrite < float [RG ][S ]> ( 43 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 44auto & wsum = * sycl::ext::oneapi::group_local_memory_for_overwrite < 45float [S * RG /32 ]> ( 46 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 47const int head = item_ct1 .get_group (2 ); 48const int col = item_ct1 .get_local_id (2 );// 0..127 49const int rg = item_ct1 .get_local_id (1 );// 0..3 50const int tid = rg * S + col ; 51const int qh = head %h_k ; 52if (tid < S ) {sk [tid ]= k [qh * S + tid ];sq [tid ]= q [qh * S + tid ]; } 53float s [RPG ]; 54float * base = state + ((size_t ) (rg * RPG )* h_v + head )* S + col ; 55const size_t row_stride = (size_t )h_v * S ; 56#pragma unroll 57for (int r = 0 ;r < RPG ;++ r )s [r ]= base [r * row_stride ]; 58/* 59DPCT1065: Consider replacing sycl::nd_item::barrier() with 60sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 61performance if there is no access to global memory. 62*/ 63item_ct1 .barrier (); 64const float g = sycl::native::exp (gate [head ]); 65float kv = 0.0f ; 66#pragma unroll 67for (int r = 0 ;r < RPG ;++ r )kv = sycl::fma (s [r ],sk [rg * RPG + r ],kv ); 68red_kv [rg ][col ]= kv ; 69/* 70DPCT1065: Consider replacing sycl::nd_item::barrier() with 71sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 72performance if there is no access to global memory. 73*/ 74item_ct1 .barrier (); 75const float kv_col = red_kv [0 ][col ]+ red_kv [1 ][col ]+ red_kv [2 ][col ]+ red_kv [3 ][col ]; 76const float delta = (v [head * S + col ]- g * kv_col )* beta [head ]; 77float o = 0.0f ; 78#pragma unroll 79for (int r = 0 ;r < RPG ;++ r ) { 80s [r ]= sycl::fma ((float )g ,s [r ],sk [rg * RPG + r ]* delta ); 81o = sycl::fma (s [r ],sq [rg * RPG + r ],o ); 82base [r * row_stride ]= s [r ]; 83 } 84red_o [rg ][col ]= o ; 85/* 86DPCT1065: Consider replacing sycl::nd_item::barrier() with 87sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 88performance if there is no access to global memory. 89*/ 90item_ct1 .barrier (); 91float oc = 0.0f ,sq_part = 0.0f ; 92if (rg == 0 ) { 93oc = (red_o [0 ][col ]+ red_o [1 ][col ]+ red_o [2 ][col ]+ red_o [3 ][col ])* 94 sycl::rsqrt ((float )S ); 95sq_part = oc * oc ; 96 } 97// RMS over the head's 128 outputs: warps of row group 0 are threads 0..127. 98/* 99DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature masked 100sub_group function which may not be supported by all compilers or runtimes. You 101may need to adjust the code. 102*/ 103#pragma unroll 104for (int o2 = 16 ;o2 > 0 ;o2 >>=1 )sq_part += 105 dpct::experimental::permute_sub_group_by_xor ( 1060xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 107sq_part ,o2 ); 108if ((tid & 31 )== 0 )wsum [tid >>5 ]= sq_part ; 109/* 110DPCT1065: Consider replacing sycl::nd_item::barrier() with 111sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 112performance if there is no access to global memory. 113*/ 114item_ct1 .barrier (); 115if (rg == 0 ) { 116const float ss = wsum [0 ]+ wsum [1 ]+ wsum [2 ]+ wsum [3 ]; 117const float scale = sycl::rsqrt (ss / (float )S + eps ); 118const float zz = z [head * S + col ]; 119y [head * S + col ]= 120oc * scale * gamma [col ]* (1.0f / (1.0f + sycl::native::exp (- zz ))); 121 } 122} 123 124__dpct_inline__ void gdn_conv_l2_kernel (float * __restrict__hist , 125const float * __restrict__qkv , 126const float * __restrict__w , 127float * __restrict__h ,int qk_heads , 128float eps ) { 129auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 130auto & part = 131* sycl::ext::oneapi::group_local_memory_for_overwrite < float [S /32 ]> ( 132 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 133const int c = item_ct1 .get_group (2 )* S + item_ct1 .get_local_id (2 ); 134const float v0 = hist [c * 3 ],v1 = hist [c * 3 + 1 ],v2 = hist [c * 3 + 2 ],x = qkv [c ]; 135float sum = v0 * w [c * 4 ]+ v1 * w [c * 4 + 1 ]+ v2 * w [c * 4 + 2 ]+ x * w [c * 4 + 3 ]; 136hist [c * 3 ]= v1 ; 137hist [c * 3 + 1 ]= v2 ; 138hist [c * 3 + 2 ]= x ; 139float y = sum / (1.0f + sycl::native::exp (- sum )); 140if ((int )item_ct1 .get_group (2 )< qk_heads ) { 141float sq = y * y ; 142/* 143DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature masked 144sub_group function which may not be supported by all compilers or runtimes. You 145may need to adjust the code. 146*/ 147#pragma unroll 148for (int o = 16 ;o > 0 ;o >>=1 )sq += 149 dpct::experimental::permute_sub_group_by_xor ( 1500xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 151sq ,o ); 152if ((item_ct1 .get_local_id (2 )& 31 )== 0 ) 153part [item_ct1 .get_local_id (2 ) >>5 ]= sq ; 154/* 155DPCT1118: SYCL group functions and algorithms must be encountered in 156converged control flow. You may need to adjust the code. 157*/ 158/* 159DPCT1065: Consider replacing sycl::nd_item::barrier() with 160sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 161better performance if there is no access to global memory. 162*/ 163item_ct1 .barrier (); 164const float ss = part [0 ]+ part [1 ]+ part [2 ]+ part [3 ]; 165y *= sycl::rsqrt (ss + eps ); 166 } 167h [c ]= y ; 168} 169 170__dpct_inline__ void 171gdn_ab_kernel (const float * __restrict__x ,const uint16_t * __restrict__wa , 172const uint16_t * __restrict__wb ,const float * __restrict__dt , 173const float * __restrict__ssm_a ,float * __restrict__gate , 174float * __restrict__beta ,int n ,int h_v ) { 175auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 176const int row = item_ct1 .get_group (2 )* 8 + (item_ct1 .get_local_id (2 ) >>5 ), 177lane = item_ct1 .get_local_id (2 )& 31 ; 178if (row >=2 * h_v )return ; 179const bool is_beta = row >=h_v ; 180const int r = is_beta ?row - h_v :row ; 181const sycl::uint4 * w4 = reinterpret_cast < const sycl::uint4 *> ( 182 (is_beta ?wb :wa )+ (size_t )r * n ); 183float acc = 0.0f ; 184for (int j = lane ;j < n /8 ;j += 32 ) { 185/* 186DPCT1098: The '*' expression is used instead of the __ldg call. 187These two expressions do not provide the exact same functionality. Check 188the generated code for potential precision and/or performance issues. 189*/ 190const sycl::uint4 wv = * (w4 + j ); 191const sycl::float4 xa = 192* reinterpret_cast < const sycl::float4 *> (x + j * 8 ); 193const sycl::float4 xb = 194* reinterpret_cast < const sycl::float4 *> (x + j * 8 + 4 ); 195acc = sycl::fma (sycl::bit_cast < float > (wv .x () <<16 ), (float )(xa .x ()), 196acc ); 197acc = sycl::fma (sycl::bit_cast < float > (wv .x ()& 0xffff0000u ), 198 (float )(xa .y ()),acc ); 199acc = sycl::fma (sycl::bit_cast < float > (wv .y () <<16 ), (float )(xa .z ()), 200acc ); 201acc = sycl::fma (sycl::bit_cast < float > (wv .y ()& 0xffff0000u ), 202 (float )(xa .w ()),acc ); 203acc = sycl::fma (sycl::bit_cast < float > (wv .z () <<16 ), (float )(xb .x ()), 204acc ); 205acc = sycl::fma (sycl::bit_cast < float > (wv .z ()& 0xffff0000u ), 206 (float )(xb .y ()),acc ); 207acc = sycl::fma (sycl::bit_cast < float > (wv .w () <<16 ), (float )(xb .z ()), 208acc ); 209acc = sycl::fma (sycl::bit_cast < float > (wv .w ()& 0xffff0000u ), 210 (float )(xb .w ()),acc ); 211 } 212/* 213DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature masked 214sub_group function which may not be supported by all compilers or runtimes. You 215may need to adjust the code. 216*/ 217#pragma unroll 218for (int o = 16 ;o > 0 ;o >>=1 )acc += 219 dpct::experimental::permute_sub_group_by_xor ( 2200xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 221acc ,o ); 222if (lane != 0 )return ; 223if (is_beta ) { 224beta [r ]= 1.0f / (1.0f + sycl::native::exp (- acc )); 225 }else { 226const float v = acc + dt [r ]; 227const float sp = v > 20.0f ?v : sycl::log1p (sycl::native::exp (v )); 228gate [r ]= sp * ssm_a [r ]; 229 } 230} 231 232}// namespace 233 234void fused_gdn_conv_l2 (float * history ,const float * qkv ,const float * conv_w ,float * h ,int channels ,int qk_heads , 235float eps ,void * stream ) { 236if (!history || !qkv || !conv_w || !h || channels %S != 0 || qk_heads < 0 || qk_heads > channels /S ) { 237 std::fprintf (stderr ,"fused_gdn_conv_l2: invalid arguments\n" ); 238 std::exit (1 ); 239 } 240 { 241auto exp_props = sycl::ext::oneapi::experimental::properties { 242 sycl::ext::oneapi::experimental::use_root_sync }; 243 244 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 245-> template parallel_for < dpct_kernel_name < class gdn_conv_l2_kernel_44cd0c >> ( 246 sycl::nd_range < 3 > (sycl::range (1 ,1 , (unsigned )(channels /S ))* 247 sycl::range (1 ,1 ,S ), 248 sycl::range (1 ,1 ,S )), 249exp_props , 250 [= ](sycl::nd_item < 3 > item_ct1 ) 251 [[sycl ::reqd_sub_group_size (32 )]] { 252gdn_conv_l2_kernel (history ,qkv ,conv_w ,h ,qk_heads , 253eps ); 254 }); 255 } 256/* 257DPCT1010: SYCL uses exceptions to report errors and does not use the 258error codes. The cudaGetLastError function call was replaced with 0. You 259need to rewrite this code. 260*/ 261const dpct::err0 e = 0 ; 262/* 263DPCT1009: SYCL reports errors using exceptions and does not use error 264codes. Please replace the "get_error_string_dummy(...)" with a real 265error-handling function. 266*/ 267} 268 269void fused_gdn_ab (const float * x ,const uint16_t * w_alpha ,const uint16_t * w_beta ,const float * dt ,const float * ssm_a , 270float * gate ,float * beta ,int n_embd ,int h_v ,void * stream ) { 271if (!x || !w_alpha || !w_beta || !dt || !ssm_a || !gate || !beta || n_embd %8 != 0 || h_v <=0 ) { 272 std::fprintf (stderr ,"fused_gdn_ab: invalid arguments\n" ); 273 std::exit (1 ); 274 } 275 { 276auto exp_props = sycl::ext::oneapi::experimental::properties { 277 sycl::ext::oneapi::experimental::use_root_sync }; 278 279 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 280-> template parallel_for < dpct_kernel_name < class gdn_ab_kernel_c211b9 >> ( 281 sycl::nd_range < 3 > ( 282 sycl::range (1 ,1 , (unsigned )((2 * h_v + 7 ) /8 ))* 283 sycl::range (1 ,1 ,256 ), 284 sycl::range (1 ,1 ,256 )), 285exp_props , 286 [= ](sycl::nd_item < 3 > item_ct1 ) 287 [[sycl ::reqd_sub_group_size (32 )]] { 288gdn_ab_kernel (x ,w_alpha ,w_beta ,dt ,ssm_a ,gate ,beta , 289n_embd ,h_v ); 290 }); 291 } 292/* 293DPCT1010: SYCL uses exceptions to report errors and does not use the 294error codes. The cudaGetLastError function call was replaced with 0. You 295need to rewrite this code. 296*/ 297const dpct::err0 e = 0 ; 298/* 299DPCT1009: SYCL reports errors using exceptions and does not use error 300codes. Please replace the "get_error_string_dummy(...)" with a real 301error-handling function. 302*/ 303} 304 305void fused_gdn_step_norm (float * state ,const float * q ,const float * k ,const float * v ,const float * gate , 306const float * beta ,const float * z ,const float * gamma ,float eps ,float * y ,int h_k ,int h_v , 307void * stream ) { 308if (!state || !q || !k || !v || !gate || !beta || !z || !gamma || !y || h_k <=0 || h_v <=0 || h_v %h_k ) { 309 std::fprintf (stderr ,"fused_gdn_step_norm: invalid arguments\n" ); 310 std::exit (1 ); 311 } 312/* 313DPCT1049: The work-group size passed to the SYCL kernel may exceed the 314limit. To get the device limit, query info::device::max_work_group_size. 315Adjust the work-group size if needed. 316*/ 317 { 318auto exp_props = sycl::ext::oneapi::experimental::properties { 319 sycl::ext::oneapi::experimental::use_root_sync }; 320 321 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 322-> template parallel_for < dpct_kernel_name < class gdn_step_norm_kernel_51bed3 >> ( 323 sycl::nd_range < 3 > (sycl::range (1 ,1 , (unsigned )h_v )* 324 sycl::range (1 ,RG ,S ), 325 sycl::range (1 ,RG ,S )), 326exp_props , 327 [= ](sycl::nd_item < 3 > item_ct1 ) 328 [[sycl ::reqd_sub_group_size (32 )]] { 329gdn_step_norm_kernel (state ,q ,k ,v ,gate ,beta ,z , 330gamma ,eps ,y ,h_k ,h_v ); 331 }); 332 } 333/* 334DPCT1010: SYCL uses exceptions to report errors and does not use the 335error codes. The cudaGetLastError function call was replaced with 0. You 336need to rewrite this code. 337*/ 338const dpct::err0 e = 0 ; 339} 340 341}// namespace strata::kernels