okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
86bed1c92
main
1// src/kernels/cuda/cvec.cu - see include/strata/kernels/cvec.hpp. 2#define DPCT_PROFILING_ENABLED 3#include <sycl/sycl.hpp> 4#include "strata/kprof.hpp" 5#include <dpct/dpct.hpp> 6#include "strata/sycl_queue.hpp" 7#include "strata/kernels/cvec.hpp" 8 9#include <stdexcept> 10 11namespace strata::kernels { 12namespace { 13 14constexpr int THREADS = 256 ; 15constexpr int MAXK = 16 ;// n_embd up to 4096, held in registers between the dot and the update 16 17Cvec g_cvec ;// the description; its device pointers are the uploading device's 18bool g_on_host = false; 19// the tables on every device that holds them (a layer split applies the vector on several) 20constexpr int kDevices = 64 ; 21struct DevTables {float * dir = nullptr ;float * s = nullptr ;int * on = nullptr ; }; 22DevTables g_dev [kDevices ]; 23std::vector < float > g_dir_host ,g_s_host ; 24int cur_device ()try { 25int d = 0 ; 26if (DPCT_CHECK_ERROR (d = dpct::get_current_device_id ())!= 0 || d < 0 || 27d >=kDevices )d = 0 ; 28return d ; 29} 30catch (sycl::exception const & exc ) { 31 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 32 <<", line:" <<__LINE__ << std::endl ; 33 std::exit (1 ); 34} 35bool upload_here (std::string & err )try { 36DevTables & t = g_dev [cur_device ()]; 37if (t .dir != nullptr )return true; 38const int flag = g_on_host ?1 :0 ; 39if (DPCT_CHECK_ERROR (t .dir = sycl::malloc_device < float > ( 40g_dir_host .size (), dpct::get_in_order_queue ()))!= 410 || 42DPCT_CHECK_ERROR (t .s = sycl::malloc_device < float > ( 43g_s_host .size (), dpct::get_in_order_queue ()))!= 440 || 45DPCT_CHECK_ERROR (t .on = sycl::malloc_device < int > ( 461 , dpct::get_in_order_queue ()))!= 0 || 47/* 48DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 49assuming in the original code the source host memory is pageable memory. 50If the memory is not pageable, call wait() on event return by memcpy API 51to ensure synchronization behavior. 52*/ 53DPCT_CHECK_ERROR (dpct::get_in_order_queue ().memcpy ( 54t .dir ,g_dir_host .data (),g_dir_host .size ()* sizeof (float )).wait ())!= 550 || 56/* 57DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 58assuming in the original code the source host memory is pageable memory. 59If the memory is not pageable, call wait() on event return by memcpy API 60to ensure synchronization behavior. 61*/ 62DPCT_CHECK_ERROR (dpct::get_in_order_queue ().memcpy ( 63t .s ,g_s_host .data (),g_s_host .size ()* sizeof (float )).wait ())!= 0 || 64DPCT_CHECK_ERROR (dpct::get_in_order_queue () 65 .memcpy (t .on ,& flag ,sizeof (int )) 66 .wait ())!= 0 ) { 67err = "control vector: device allocation failed" ; 68t = DevTables {}; 69return false; 70 } 71return true; 72} 73catch (sycl::exception const & exc ) { 74 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 75 <<", line:" <<__LINE__ << std::endl ; 76 std::exit (1 ); 77} 78 79// the fused hyper-connection read's gate (fused_gr.cu), so a write done here is bitwise the one it would have folded 80__dpct_inline__ float sigmoidf_ (float x ) { 81return 1.0f / (1.0f + sycl::native::exp (- x )); 82} 83 84// one block per (stream, token): the pending write, then h . v over the stream, then the update 85/* 86DPCT1110: The total declared local variable size in device function 87cvec_kernel exceeds 128 bytes and may cause high register pressure. Consult with 88your hardware vendor to find the total register size available and adjust the 89code, or use smaller sub-group size to avoid high register pressure. 90*/ 91__dpct_inline__ void 92cvec_kernel (float * __restrict__R ,const float * __restrict__dir , 93const float * __restrict__s_l ,const int * __restrict__on ,int mode , 94int64_t layer ,int n ,int hc ,int64_t r_ld , 95const float * __restrict__bo ,int64_t bo_ld , 96const float * __restrict__inj ,int64_t inj_ld ,int write ) { 97auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 98const int c = item_ct1 .get_group (2 ); 99const int64_t t = item_ct1 .get_group (1 ); 100float * r = R + t * r_ld + (int64_t )c * n ; 101const float s = s_l [layer ]; 102const bool steer = * on != 0 && s != 0.0f ;// uniform over the block 103if (!steer && !write )return ; 104const float * v = dir + layer * n ; 105const float w = write ?2.0f * sigmoidf_ (inj [t * inj_ld + c ] / (float )hc ) :0.0f ; 106const float * b = write ?bo + t * bo_ld :nullptr ; 107float x [MAXK ]; 108float dot = 0.0f ; 109#pragma unroll 110for (int k = 0 ;k < MAXK ;++ k ) { 111const int d = item_ct1 .get_local_id (2 )+ k * THREADS ; 112if (d < n ) { 113float xv = r [d ]; 114if (write )xv = sycl::fma ((float )(b [d ]), (float )w ,xv ); 115x [k ]= xv ; 116if (steer && mode == 0 )dot = sycl::fma (xv , (float )(v [d ]),dot ); 117 } 118 } 119if (steer && mode == 0 ) { 120auto & part = * sycl::ext::oneapi::group_local_memory_for_overwrite < 121float [THREADS /32 ]> ( 122 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 123#pragma unroll 124/* 125DPCT1108: '__shfl_xor_sync' was migrated with the experimental 126feature masked sub_group function which may not be supported by all 127compilers or runtimes. You may need to adjust the code. 128*/ 129for (int o = 16 ;o > 0 ;o >>=1 )dot += 130 dpct::experimental::permute_sub_group_by_xor ( 1310xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 132dot ,o ); 133if ((item_ct1 .get_local_id (2 )& 31 )== 0 ) 134part [item_ct1 .get_local_id (2 ) >>5 ]= dot ; 135/* 136DPCT1118: SYCL group functions and algorithms must be encountered in 137converged control flow. You may need to adjust the code. 138*/ 139/* 140DPCT1065: Consider replacing sycl::nd_item::barrier() with 141sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 142better performance if there is no access to global memory. 143*/ 144item_ct1 .barrier (); 145if (item_ct1 .get_local_id (2 )< 32 ) { 146float p = item_ct1 .get_local_id (2 )< THREADS /32 147 ?part [item_ct1 .get_local_id (2 )] 148 :0.0f ; 149#pragma unroll 150/* 151DPCT1108: '__shfl_xor_sync' was migrated with the experimental 152feature masked sub_group function which may not be supported by all 153compilers or runtimes. You may need to adjust the code. 154*/ 155for (int o = 16 ;o > 0 ;o >>=1 )p += 156 dpct::experimental::permute_sub_group_by_xor ( 1570xffffffffu , 158 sycl::ext::oneapi::this_work_item::get_sub_group (),p ,o ); 159if (item_ct1 .get_local_id (2 )== 0 )part [0 ]= p ; 160 } 161/* 162DPCT1118: SYCL group functions and algorithms must be encountered in 163converged control flow. You may need to adjust the code. 164*/ 165/* 166DPCT1065: Consider replacing sycl::nd_item::barrier() with 167sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 168better performance if there is no access to global memory. 169*/ 170item_ct1 .barrier (); 171dot = part [0 ]* s ;// s (h . v) 172 } 173#pragma unroll 174for (int k = 0 ;k < MAXK ;++ k ) { 175const int d = item_ct1 .get_local_id (2 )+ k * THREADS ; 176if (d < n ) { 177float xv = x [k ]; 178if (steer )xv = 179mode == 0 ? sycl::fma (- dot , (float )(v [d ]),xv ) :xv + v [d ]; 180r [d ]= xv ; 181 } 182 } 183} 184 185}// namespace 186 187const Cvec & cvec () {return g_cvec ; } 188 189bool cvec_upload (const std::vector < float >& dir ,const std::vector < float >& s ,int mode ,int first ,int last , 190int64_t n_embd ,int64_t hc , std::string & err ) { 191if (n_embd < 1 || n_embd > (int64_t )THREADS * MAXK ) {err = "control vector: unsupported n_embd" ;return false; } 192if (s .empty ()|| dir .size ()!= s .size ()* (size_t )n_embd ) {err = "control vector: bad table sizes" ;return false; } 193int prev = 0 ;// a new vector replaces the old one on every device 194prev = dpct::get_current_device_id (); 195for (int d = 0 ;d < kDevices ;++ d ) { 196if (g_dev [d ].dir == nullptr )continue ; 197/* 198DPCT1093: The "d" device may be not the one intended for use. 199Adjust the selected device if needed. 200*/ 201 dpct::select_device (d ); 202 dpct::get_current_device ().queues_wait_and_throw (); 203 sycl::free (g_dev [d ].dir , dpct::get_in_order_queue ()); 204 sycl::free (g_dev [d ].s , dpct::get_in_order_queue ()); 205 sycl::free (g_dev [d ].on , dpct::get_in_order_queue ()); 206g_dev [d ]= DevTables {}; 207 } 208/* 209DPCT1093: The "prev" device may be not the one intended for use. Adjust 210the selected device if needed. 211*/ 212 dpct::select_device (prev ); 213g_dir_host = dir ; 214g_s_host = s ; 215g_on_host = true; 216if (!upload_here (err ))return false; 217const DevTables & t = g_dev [cur_device ()]; 218g_cvec .dir = t .dir ; 219g_cvec .s = t .s ; 220g_cvec .on = t .on ; 221g_cvec .mode = mode ; 222g_cvec .first = first ; 223g_cvec .last = last ; 224g_cvec .n_embd = n_embd ; 225g_cvec .hc = hc ; 226g_cvec .steered .assign (s .size (), false); 227for (size_t l = 0 ;l < s .size ();++ l )g_cvec .steered [l ]= s [l ]!= 0.0f ; 228return true; 229} 230 231bool cvec_replicate (std::string & err ) {return !g_cvec .loaded ()|| upload_here (err ); } 232 233void cvec_set_enabled (bool on ) { 234if (!g_cvec .loaded ()|| on == g_on_host )return ; 235int prev = 0 ; 236prev = dpct::get_current_device_id (); 237const int v = on ?1 :0 ; 238for (int d = 0 ;d < kDevices ;++ d ) { 239if (g_dev [d ].on == nullptr )continue ; 240/* 241DPCT1093: The "d" device may be not the one intended for use. 242Adjust the selected device if needed. 243*/ 244 dpct::select_device (d ); 245 dpct::get_current_device () 246 .queues_wait_and_throw ();// nothing in flight may still read the 247// flag 248 dpct::get_in_order_queue ().memcpy (g_dev [d ].on ,& v ,sizeof (int )).wait (); 249 } 250/* 251DPCT1093: The "prev" device may be not the one intended for use. Adjust 252the selected device if needed. 253*/ 254 dpct::select_device (prev ); 255g_on_host = on ; 256} 257 258bool cvec_enabled () {return g_cvec .loaded ()&& g_on_host ; } 259 260bool cvec_tables (const float ** dir ,const float ** s ,const int ** on ) { 261if (!g_cvec .loaded ())return false; 262const DevTables & t = g_dev [cur_device ()]; 263if (t .dir == nullptr )return false; 264* dir = t .dir ;* s = t .s ;* on = t .on ; 265return true; 266} 267 268void cvec_apply (float * R ,int64_t layer ,int64_t T ,int64_t r_ld , 269const float * bo ,int64_t bo_ld ,const float * inj , 270int64_t inj_ld ,bool write ,void * stream )try { 271if (!g_cvec .loaded ()|| T < 1 )return ; 272const DevTables & t = g_dev [cur_device ()]; 273if (t .dir == nullptr )throw std::runtime_error ("cvec_apply: the control vector is not on this device (cvec_replicate)" ); 274const dpct::dim3 grid ((unsigned )g_cvec .hc , (unsigned )T ); 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-> submit ([& ](sycl::handler & cgh ) { 281auto g_cvec_mode_ct4 = g_cvec .mode ; 282auto g_cvec_n_embd_ct6 = (int )g_cvec .n_embd ; 283auto g_cvec_hc_ct7 = (int )g_cvec .hc ; 284 285cgh .parallel_for < dpct_kernel_name < class cvec_kernel_a17328 >> ( 286 sycl::nd_range < 3 > (grid * sycl::range (1 ,1 ,THREADS ), 287 sycl::range (1 ,1 ,THREADS )), 288exp_props , 289 [= ](sycl::nd_item < 3 > item_ct1 ) 290 [[sycl ::reqd_sub_group_size (32 )]] { 291cvec_kernel (R ,t .dir ,t .s ,t .on ,g_cvec_mode_ct4 , 292layer ,g_cvec_n_embd_ct6 ,g_cvec_hc_ct7 , 293r_ld ,bo ,bo_ld ,inj ,inj_ld , 294write ?1 :0 ); 295 }); 296 }); 297 } 298/* 299DPCT1010: SYCL uses exceptions to report errors and does not use the 300error codes. The cudaPeekAtLastError function call was replaced with 0. You 301need to rewrite this code. 302*/ 303if (0 != 0 )throw std::runtime_error ("cvec_apply: launch failed" ); 304} 305catch (sycl::exception const & exc ) { 306 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 307 <<", line:" <<__LINE__ << std::endl ; 308 std::exit (1 ); 309} 310 311}// namespace strata::kernels