okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
86bed1c92
main
1// src/prefill/moe_mmq.cu - see include/strata/prefill/moe_mmq.hpp. llama.cpp's MMQ (ggml-cuda, MIT) is compiled 2// from the pinned llama.cpp checkout the build already takes ggml from; src/prefill/ggml_cuda_host.cu supplies the 3// few host symbols of ggml-cuda.cu it references. 4#define DPCT_PROFILING_ENABLED 5#include <sycl/sycl.hpp> 6#include "strata/kprof.hpp" 7#include <dpct/dpct.hpp> 8#include "strata/sycl_queue.hpp" 9#include "strata/prefill/moe_mmq.hpp" 10 11#include "common.cuh" 12#include "mmq.cuh" 13#include "quantize.cuh" 14 15#include <cstdio> 16#include <cstdlib> 17 18namespace strata::prefill::mmq { 19namespace { 20 21void ck (dpct::err0 e ,const char * what ) { 22} 23 24int64_t pad512 (int64_t n ) {return (n + 511 ) /512 * 512 ; } 25 26__dpct_inline__ void copy16_kernel (const sycl::uint4 * __restrict__a , 27int64_t na , 28const sycl::uint4 * __restrict__b , 29int64_t nb , sycl::uint4 * __restrict__ab_dst , 30const sycl::uint4 * __restrict__c , 31int64_t nc , 32 sycl::uint4 * __restrict__c_dst ) { 33auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 34const int64_t i = 35 (int64_t )item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 36item_ct1 .get_local_id (2 ); 37if (i < na )ab_dst [i ]= a [i ]; 38else if (i < na + nb )ab_dst [i ]= b [i - na ]; 39else if (i < na + nb + nc )c_dst [i - na - nb ]= c [i - na - nb ]; 40} 41__dpct_inline__ void copy1_kernel (const uint8_t * __restrict__a ,int64_t na , 42const uint8_t * __restrict__b ,int64_t nb , 43uint8_t * __restrict__ab_dst , 44const uint8_t * __restrict__c ,int64_t nc , 45uint8_t * __restrict__c_dst ) { 46auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 47const int64_t i = 48 (int64_t )item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 49item_ct1 .get_local_id (2 ); 50if (i < na )ab_dst [i ]= a [i ]; 51else if (i < na + nb )ab_dst [i ]= b [i - na ]; 52else if (i < na + nb + nc )c_dst [i - na - nb ]= c [i - na - nb ]; 53} 54 55// Strata blob: gate/up codes [1280][640 B], down codes [2560][160 B], gate/up scales [1280][40] f16, down scales 56// [2560][10] f16 (the layout of prefill/kernels.cu's blob_dequant_kernel). A GGUF Q2_0 block is {f16 d; 16 code 57// bytes} with the same 2-bit codes in the same order, so a block is a scale and a 16-byte run of codes. 58/* 59DPCT1110: The total declared local variable size in device function 60strata_q2_kernel exceeds 128 bytes and may cause high register pressure. Consult 61with your hardware vendor to find the total register size available and adjust 62the code, or use smaller sub-group size to avoid high register pressure. 63*/ 64__dpct_inline__ void strata_q2_kernel (const uint8_t * __restrict__blob , 65uint16_t * __restrict__gu , 66uint16_t * __restrict__dn ) { 67auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 68constexpr size_t O_D_CODES = (size_t )1280 * 640 , 69O_GU_SC = O_D_CODES + (size_t )2560 * 160 , 70O_D_SC = O_GU_SC + (size_t )1280 * 40 * 2 ; 71const int64_t i = 72 (int64_t )item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 73item_ct1 .get_local_id (2 );// one GGUF block 74const int64_t n_gu = 1280LL * 40 ,n_d = 2560LL * 10 ; 75const uint8_t * codes ; 76const uint16_t * scale ; 77uint16_t * out ; 78if (i < n_gu ) { 79const int64_t row = i /40 ,b = i %40 ; 80codes = blob + row * 640 + b * 16 ; 81scale = (const uint16_t * ) (blob + O_GU_SC )+ row * 40 + b ; 82out = gu + i * 9 ; 83 }else if (i < n_gu + n_d ) { 84const int64_t j = i - n_gu ,row = j /10 ,b = j %10 ; 85codes = blob + O_D_CODES + row * 160 + b * 16 ; 86scale = (const uint16_t * ) (blob + O_D_SC )+ row * 10 + b ; 87out = dn + j * 9 ; 88 }else { 89return ; 90 } 91const sycl::uint4 q = * (const sycl::uint4 * )codes ; 92const uint16_t * qh = (const uint16_t * )& q ; 93out [0 ]= * scale ; 94#pragma unroll 95for (int k = 0 ;k < 8 ;++ k )out [1 + k ]= qh [k ]; 96} 97 98__dpct_inline__ void swiglu_kernel (const float * __restrict__gu , 99float * __restrict__h ,int64_t rows , 100int64_t n_ff ,bool interleaved ) { 101auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 102const int64_t i = 103 (int64_t )item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 104item_ct1 .get_local_id (2 ); 105if (i >=rows * n_ff )return ; 106const int64_t r = i /n_ff ,k = i %n_ff ; 107const float * row = gu + r * 2 * n_ff ; 108const float g = interleaved ?row [2 * k ] :row [k ],u = interleaved ?row [2 * k + 1 ] :row [n_ff + k ]; 109h [i ]= g / (1.0f + sycl::native::exp (- g ))* u ; 110} 111 112__dpct_inline__ void iota_kernel (int32_t * dst ,int64_t n ) { 113auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 114const int64_t i = 115 (int64_t )item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 116item_ct1 .get_local_id (2 ); 117if (i < n )dst [i ]= (int32_t )i ; 118} 119 120unsigned blocks (int64_t n ) {return (unsigned ) ((n + 255 ) /256 ); } 121 122}// namespace 123 124bool built () {return true; } 125 126bool supported (int t ) { 127switch ((ggml_type )t ) { 128case GGML_TYPE_Q2_0 :case GGML_TYPE_IQ2_XXS :case GGML_TYPE_IQ2_XS :case GGML_TYPE_IQ2_S : 129case GGML_TYPE_IQ3_XXS :case GGML_TYPE_IQ3_S :case GGML_TYPE_IQ4_NL :case GGML_TYPE_IQ4_XS : 130return true; 131default : 132return false; 133 } 134} 135 136size_t matrix_bytes (int t ,int64_t rows ,int64_t cols ) { 137return (size_t )rows * (size_t ) (cols /ggml_blck_size ((ggml_type )t ))* ggml_type_size ((ggml_type )t ); 138} 139 140size_t q8_bytes (int64_t rows ,int64_t cols ) { 141return (size_t )rows * (size_t )pad512 (cols )* sizeof (block_q8_1_mmq ) / (4 * QK8_1 )+ 128 * sizeof (block_q8_1_mmq ); 142} 143 144void quantize (const float * x ,const int32_t * ids ,void * xq ,int t ,int64_t cols ,int64_t ld ,int64_t rows ,void * stream ) { 145if (rows <=0 )return ; 146quantize_mmq_q8_1_cuda (x ,ids ,xq , (ggml_type )t ,cols ,ld ,rows * ld ,rows * ld ,pad512 (cols ),rows ,1 ,1 , 147 (cudaStream_t )stream ); 148/* 149DPCT1010: SYCL uses exceptions to report errors and does not use the 150error codes. The cudaGetLastError function call was replaced with 0. You 151need to rewrite this code. 152*/ 153ck (0 ,"quantize" ); 154} 155 156Context ::Context () { 157int dev = 0 ; 158dev = dpct::get_current_device_id (); 159ctx_ = new ggml_backend_cuda_context (dev ); 160} 161Context ::~Context () {delete (ggml_backend_cuda_context * )ctx_ ; } 162 163void Context ::run (const Product & p ,void * stream ) { 164if (p .n <=0 || p .max_rows <=0 )return ; 165const ggml_type t = (ggml_type )p .type ; 166const int64_t qk = ggml_blck_size (t ),bpr = p .w_cols /qk ; 167const mmq_args a = {(const char * )p .w ,t , (const int * )p .xq ,p .ids ,p .bounds ,p .dst ,nullptr , 168p .w_cols ,p .w_rows ,p .total_rows ,bpr ,p .total_rows ,p .ld_dst , 169p .n ,p .n , (int64_t ) (p .expert_bytes /ggml_type_size (t )),0 ,0 , 1701 ,1 ,0 ,0 ,0 , 171p .max_rows ,p .max_rows }; 172auto & ctx = * (ggml_backend_cuda_context * )ctx_ ; 173const dpct::queue_ptr s = strata::q_of (stream ); 174switch (t ) { 175case GGML_TYPE_Q2_0 :mul_mat_q_case < GGML_TYPE_Q2_0 > (ctx ,a ,s );break ; 176case GGML_TYPE_IQ2_XXS :mul_mat_q_case < GGML_TYPE_IQ2_XXS > (ctx ,a ,s );break ; 177case GGML_TYPE_IQ2_XS :mul_mat_q_case < GGML_TYPE_IQ2_XS > (ctx ,a ,s );break ; 178case GGML_TYPE_IQ2_S :mul_mat_q_case < GGML_TYPE_IQ2_S > (ctx ,a ,s );break ; 179case GGML_TYPE_IQ3_XXS :mul_mat_q_case < GGML_TYPE_IQ3_XXS > (ctx ,a ,s );break ; 180case GGML_TYPE_IQ3_S :mul_mat_q_case < GGML_TYPE_IQ3_S > (ctx ,a ,s );break ; 181case GGML_TYPE_IQ4_NL :mul_mat_q_case < GGML_TYPE_IQ4_NL > (ctx ,a ,s );break ; 182case GGML_TYPE_IQ4_XS :mul_mat_q_case < GGML_TYPE_IQ4_XS > (ctx ,a ,s );break ; 183default : 184 std::fprintf (stderr ,"prefill mmq: type %d is not covered\n" , (int )t ); 185 std::exit (1 ); 186 } 187/* 188DPCT1010: SYCL uses exceptions to report errors and does not use the 189error codes. The cudaGetLastError function call was replaced with 0. You 190need to rewrite this code. 191*/ 192ck (0 ,"mul_mat_q" ); 193} 194 195void gather_native (const void * gate ,const void * up ,size_t gu_half_bytes ,const void * down ,size_t d_bytes , 196void * gu_dst ,void * d_dst ,void * stream ) { 197const dpct::queue_ptr s = strata::q_of (stream ); 198const bool a16 = ((uintptr_t )gate | (uintptr_t )up | (uintptr_t )down | (uintptr_t )gu_dst | (uintptr_t )d_dst | 199gu_half_bytes |d_bytes ) %16 == 0 ; 200if (a16 ) { 201const int64_t na = (int64_t )gu_half_bytes /16 ,nc = (int64_t )d_bytes /16 ; 202 { 203auto exp_props = sycl::ext::oneapi::experimental::properties { 204 sycl::ext::oneapi::experimental::use_root_sync }; 205 206 strata::kprof::kp (s ,__FILE__ ,__func__ ,__LINE__ )-> template parallel_for < dpct_kernel_name < class copy16_kernel_b12127 >> ( 207 sycl::nd_range < 3 > (sycl::range (1 ,1 ,blocks (2 * na + nc ))* 208 sycl::range (1 ,1 ,256 ), 209 sycl::range (1 ,1 ,256 )), 210exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 211copy16_kernel ( 212 (const sycl::uint4 * )gate ,na , (const sycl::uint4 * )up , 213na , (sycl::uint4 * )gu_dst , (const sycl::uint4 * )down , 214nc , (sycl::uint4 * )d_dst ); 215 }); 216 } 217 }else { 218const int64_t na = (int64_t )gu_half_bytes ,nc = (int64_t )d_bytes ; 219 { 220auto exp_props = sycl::ext::oneapi::experimental::properties { 221 sycl::ext::oneapi::experimental::use_root_sync }; 222 223 strata::kprof::kp (s ,__FILE__ ,__func__ ,__LINE__ )-> template parallel_for < dpct_kernel_name < class copy1_kernel_c557f3 >> ( 224 sycl::nd_range < 3 > (sycl::range (1 ,1 ,blocks (2 * na + nc ))* 225 sycl::range (1 ,1 ,256 ), 226 sycl::range (1 ,1 ,256 )), 227exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 228copy1_kernel ((const uint8_t * )gate ,na , (const uint8_t * )up , 229na , (uint8_t * )gu_dst , (const uint8_t * )down , 230nc , (uint8_t * )d_dst ); 231 }); 232 } 233 } 234/* 235DPCT1010: SYCL uses exceptions to report errors and does not use the 236error codes. The cudaGetLastError function call was replaced with 0. You 237need to rewrite this code. 238*/ 239ck (0 ,"gather_native" ); 240} 241 242void gather_strata_q2 (const uint8_t * blob ,void * gu_dst ,void * d_dst ,void * stream ) { 243 { 244auto exp_props = sycl::ext::oneapi::experimental::properties { 245 sycl::ext::oneapi::experimental::use_root_sync }; 246 247 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 248-> template parallel_for < dpct_kernel_name < class strata_q2_kernel_793a65 >> ( 249 sycl::nd_range < 3 > ( 250 sycl::range (1 ,1 ,blocks (1280LL * 40 + 2560LL * 10 ))* 251 sycl::range (1 ,1 ,256 ), 252 sycl::range (1 ,1 ,256 )), 253exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 254strata_q2_kernel (blob , (uint16_t * )gu_dst , 255 (uint16_t * )d_dst ); 256 }); 257 } 258/* 259DPCT1010: SYCL uses exceptions to report errors and does not use the 260error codes. The cudaGetLastError function call was replaced with 0. You 261need to rewrite this code. 262*/ 263ck (0 ,"gather_strata_q2" ); 264} 265 266void swiglu (const float * gu ,float * h ,int64_t rows ,int64_t n_ff ,bool interleaved ,void * stream ) { 267if (rows <=0 )return ; 268 { 269auto exp_props = sycl::ext::oneapi::experimental::properties { 270 sycl::ext::oneapi::experimental::use_root_sync }; 271 272 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 273-> template parallel_for < dpct_kernel_name < class swiglu_kernel_3f7a6f >> ( 274 sycl::nd_range < 3 > (sycl::range (1 ,1 ,blocks (rows * n_ff ))* 275 sycl::range (1 ,1 ,256 ), 276 sycl::range (1 ,1 ,256 )), 277exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 278swiglu_kernel (gu ,h ,rows ,n_ff ,interleaved ); 279 }); 280 } 281/* 282DPCT1010: SYCL uses exceptions to report errors and does not use the 283error codes. The cudaGetLastError function call was replaced with 0. You 284need to rewrite this code. 285*/ 286ck (0 ,"swiglu" ); 287} 288 289void iota (int32_t * dst ,int64_t n ,void * stream ) { 290if (n <=0 )return ; 291 { 292auto exp_props = sycl::ext::oneapi::experimental::properties { 293 sycl::ext::oneapi::experimental::use_root_sync }; 294 295 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 296-> template parallel_for < dpct_kernel_name < class iota_kernel_9245a0 >> ( 297 sycl::nd_range < 3 > (sycl::range (1 ,1 ,blocks (n ))* 298 sycl::range (1 ,1 ,256 ), 299 sycl::range (1 ,1 ,256 )), 300exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 301iota_kernel (dst ,n ); 302 }); 303 } 304/* 305DPCT1010: SYCL uses exceptions to report errors and does not use the 306error codes. The cudaGetLastError function call was replaced with 0. You 307need to rewrite this code. 308*/ 309ck (0 ,"iota" ); 310} 311 312// STRATA_MMQ_DIRECT (include/strata/prefill/moe_mmq.hpp): the pointer-table MMQ is CUDA only, so SYCL keeps the 313// gather - these say so, for a prefill.cpp migrated from a tree that calls them 314bool tab_built () {return false; } 315bool tab_fits (int ,int64_t ) {return false; } 316size_t overread_bytes (int ,int64_t ) {return 0 ; } 317int direct_mode () {return 0 ; } 318size_t direct_slot_guard () {return 0 ; } 319void prep_group (const GuardTab & ,size_t ,void * ,void * ,size_t ,void * ) {} 320 321}// namespace strata::prefill::mmq