okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
86bed1c92
main
1// src/kernels/cuda/s2_gemv.cu - P2.S2: the S2 GEMV, one thread per output row. 2// 3// Naive per the phase rule: dequantize on the fly, FP32 accumulation inside the row, no shared memory, no 4// vector loads, no __ldg hints. Phase 3 changes this file; the parity test is what says whether a change is 5// still right. 6#define DPCT_PROFILING_ENABLED 7#include <sycl/sycl.hpp> 8#include "strata/kprof.hpp" 9#include <dpct/dpct.hpp> 10#include "strata/kernels/s2_gemv.hpp" 11 12#include <cstdio> 13#include <cstdlib> 14 15namespace strata::kernels { 16namespace { 17 18constexpr int QK = 64 ; 19 20__dpct_inline__ void s2_gemv_kernel (const uint16_t * __restrict__x , 21const uint8_t * __restrict__codes , 22const float * __restrict__scales , 23float * __restrict__y ,long long n_in , 24long long n_out ) { 25auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 26const long long o = 27 (long long )item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 28item_ct1 .get_local_id (2 ); 29if (o >=n_out )return ; 30 31const long long nb = n_in /QK ; 32const uint8_t * c = codes + o * nb * (QK /4 ); 33const float * s = scales + o * nb ; 34 35float acc = 0.0f ; 36for (long long b = 0 ;b < nb ;++ b ) { 37const float d = s [b ]; 38const uint8_t * cb = c + b * (QK /4 ); 39const uint16_t * xb = x + b * QK ; 40for (int j = 0 ;j < QK ;++ j ) { 41// (code - 1) in the INTEGER domain, then the group scale, then the activation; the CPU reference 42// below is written in the same order so the two differ only by floating-point contraction. 43const int code = (cb [j >>2 ] >> ((j & 3 )* 2 ))& 0x03 ; 44acc += (float )(code - 1 )* d * 45 sycl::vec < sycl::half ,1 > ( 46 sycl::bit_cast < sycl::half ,unsigned short > (xb [j ])) 47 .convert < float , sycl::rounding_mode::automatic > ()[0 ]; 48 } 49 } 50y [o ]= acc ; 51} 52 53}// namespace 54 55void s2_gemv (const uint16_t * x ,const uint8_t * codes ,const float * scales , 56float * y ,int64_t n_in ,int64_t n_out )try { 57if (n_in <=0 || n_out <=0 )return ; 58if (n_in %QK != 0 ) { 59 std::fprintf (stderr ,"s2_gemv: n_in %lld is not a multiple of %d\n" , (long long )n_in ,QK ); 60 std::exit (1 ); 61 } 62const int threads = 128 ; 63const long long blocks = (n_out + threads - 1 ) /threads ; 64 { 65auto exp_props = sycl::ext::oneapi::experimental::properties { 66 sycl::ext::oneapi::experimental::use_root_sync }; 67 68 strata::kprof::kp (& dpct::get_in_order_queue (),__FILE__ ,__func__ ,__LINE__ ) 69-> template parallel_for < dpct_kernel_name < class s2_gemv_kernel_163eb8 >> ( 70 sycl::nd_range < 3 > (sycl::range (1 ,1 , (unsigned )blocks )* 71 sycl::range (1 ,1 ,threads ), 72 sycl::range (1 ,1 ,threads )), 73exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 74s2_gemv_kernel (x ,codes ,scales ,y ,n_in ,n_out ); 75 }); 76 } 77const dpct::err0 e = 78DPCT_CHECK_ERROR (dpct::get_current_device ().queues_wait_and_throw ()); 79} 80catch (sycl::exception const & exc ) { 81 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 82 <<", line:" <<__LINE__ << std::endl ; 83 std::exit (1 ); 84} 85 86}// namespace strata::kernels