okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9adf4e01a
main
1// sycl/src/kernels/dense_bench.cpp - the decode window's dense GEMVs (native_mmvq) at the model's shapes and formats: 2// NMAT different matrices per shape captured in one graph, so the weights stream from DRAM as in the engine (a window 3// reads every layer's matrix once); us per call, GB/s of weights, and an FNV hash of the outputs so two builds or two 4// STRATA_MMVQ_* settings can be compared bit for bit. 5// 6// dense_bench [type ...] (default: 14 13 12 23 = Q6_K Q5_K Q4_K IQ4_XS) 7#define DPCT_PROFILING_ENABLED 8#include <sycl/sycl.hpp> 9#include <dpct/dpct.hpp> 10#include "strata/kernels/native_mmvq.hpp" 11 12#include <algorithm> 13#include <cstdint> 14#include <cstdio> 15#include <cstdlib> 16#include <cstring> 17#include <random> 18#include <vector> 19 20namespace k= strata::kernels; 21 22namespace { 23const char * name_of (int t ) { 24switch (t ) {case 14 :return "Q6_K" ;case 13 :return "Q5_K" ;case 12 :return "Q4_K" ;case 23 :return "IQ4_XS" ; 25case 8 :return "Q8_0" ;default :return "?" ; } 26} 27// random blocks with a finite, small fp16 scale (at the format's offset) so the outputs are finite 28void fill (int t , std::vector < uint8_t >& w , std::mt19937 & rng ) { 29 std::uniform_int_distribution < int > b (0 ,255 ); 30for (auto & v :w )v = (uint8_t )b (rng ); 31const size_t bs = t == 14 ?210 :t == 13 ?176 :t == 12 ?144 :t == 23 ?136 :34 ; 32const size_t off = t == 14 ?208 :0 ; 33for (size_t o = 0 ;o + bs <=w .size ();o += bs ) { 34const uint16_t h = (uint16_t ) (0x1400 | (b (rng )& 0x3ff ));// ~2^-10 35 std::memcpy (& w [o + off ],& h ,2 ); 36if (t == 12 || t == 13 ) std::memcpy (& w [o + 2 ],& h ,2 );// dmin 37 } 38} 39uint64_t fnv (const float * p ,size_t n ) { 40uint64_t h = 1469598103934665603ull ; 41const uint32_t * u = (const uint32_t * )p ; 42for (size_t i = 0 ;i < n ;++ i ) {h ^=u [i ];h *=1099511628211ull ; } 43return h ; 44} 45}// namespace 46 47int main (int argc ,char ** argv ) { 48 std::vector < int > types ; 49for (int i = 1 ;i < argc ;++ i )types .push_back (std::atoi (argv [i ])); 50if (types .empty ())types = {14 ,13 ,12 ,23 }; 51 dpct::queue_ptr s = dpct::get_current_device ().create_queue (true); 52 std::mt19937 rng (7 ); 53const struct {int n_in ,n_out ;const char * what ; }shapes []= { 54 {2560 ,10240 ,"GDN qkv" }, {2560 ,6144 ,"GDN z" }, {6144 ,2560 ,"out-proj" }, {2560 ,12288 ,"QSA q" }, {2560 ,512 ,"k/v" }}; 55constexpr int NMAT = 24 ; 56 std::printf ("dense_bench: %d matrices per shape in one graph; us per call (median of 30), GB/s of weights, hash\n" ,NMAT ); 57for (const int t :types ) { 58for (const auto & sh :shapes ) { 59const size_t wb = k::native_mmvq_weight_bytes (t ,sh .n_in ,sh .n_out ),slot = (wb + 255 )& ~(size_t )255 ; 60uint8_t * w = (uint8_t * ) sycl::malloc_device (slot * NMAT ,* s ); 61 std::vector < uint8_t > hw (wb ); 62for (int m = 0 ;m < NMAT ;++ m ) {fill (t ,hw ,rng );s -> memcpy (w + slot * m ,hw .data (),wb ).wait (); } 63 std::vector < float > hx ((size_t )4 * sh .n_in ); 64 std::normal_distribution < float > nd (0.f ,1.f ); 65for (auto & v :hx )v = nd (rng ); 66float * x = sycl::malloc_device < float > (hx .size (),* s ); 67s -> memcpy (x ,hx .data (),hx .size ()* 4 ).wait (); 68void * xq = sycl::malloc_device (k::native_q8_1_bytes (sh .n_in ,4 )+ 256 ,* s ); 69float * y = sycl::malloc_device < float > ((size_t )4 * sh .n_out * NMAT ,* s ); 70for (int nc = 1 ;nc <=4 ;++ nc ) { 71 k::native_quantize_q8_1 (x ,xq ,sh .n_in ,nc ,s ); 72s -> wait (); 73 dpct::experimental::command_graph_ptr g ; 74 dpct::experimental::begin_recording (s ); 75for (int m = 0 ;m < NMAT ;++ m ) 76 k::native_mmvq (t ,w + slot * m ,xq ,y + (size_t )4 * sh .n_out * m ,sh .n_in ,sh .n_out ,nc ,s ); 77 dpct::experimental::end_recording (s ,& g ); 78auto * ge = new sycl::ext::oneapi::experimental::command_graph < 79 sycl::ext::oneapi::experimental::graph_state::executable > (g -> finalize ()); 80delete g ; 81 std::vector < double > us ; 82for (int i = 0 ;i < 40 ;++ i ) { 83 sycl::event e0 = s -> ext_oneapi_submit_barrier (); 84s -> ext_oneapi_graph (* ge ); 85 sycl::event e1 = s -> ext_oneapi_submit_barrier (); 86e1 .wait (); 87const double ns = (double ) (e1 .get_profiling_info < sycl::info::event_profiling::command_end > ()- 88e0 .get_profiling_info < sycl::info::event_profiling::command_start > ()); 89if (i >=10 )us .push_back (ns /1e3 /NMAT ); 90 } 91delete ge ; 92 std::sort (us .begin (),us .end ()); 93 std::vector < float > hy ((size_t )4 * sh .n_out * NMAT ); 94s -> memcpy (hy .data (),y ,hy .size ()* 4 ).wait (); 95uint64_t h = 1469598103934665603ull ; 96for (int m = 0 ;m < NMAT ;++ m )h ^=fnv (& hy [(size_t )4 * sh .n_out * m ], (size_t )nc * sh .n_out )+ m ; 97 std::printf ("%-6s %-8s %5d x %5d %d cols: %8.2f us %6.1f GB/s hash %016llx\n" ,name_of (t ),sh .what , 98sh .n_in ,sh .n_out ,nc ,us [us .size () /2 ],wb /us [us .size () /2 ] /1e3 , (unsigned long long )h ); 99 } 100 sycl::free (w ,* s ); sycl::free (x ,* s ); sycl::free (xq ,* s ); sycl::free (y ,* s ); 101 } 102 } 103 dpct::get_current_device ().destroy_queue (s ); 104return 0 ; 105}