okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9adf4e01a
main
1// sycl/src/kernels/dequant_bench.cpp - time the prompt path's expert dequantizers (iq_dequant_gu_f16 / iq_dequant_f16) 2// on random blocks and print a hash of the FP16 output, so two builds can be compared bit for bit. 3// dequant_bench [type|all] [experts] (gate/up 1280 x 2560 and down 2560 x 1280 per expert, the model's shape) 4#include "strata/kernels/iq_kernels.hpp" 5#include <sycl/sycl.hpp> 6#include <dpct/dpct.hpp> 7#include <chrono> 8#include <cstdint> 9#include <cstdio> 10#include <cstdlib> 11#include <cstring> 12#include <algorithm> 13#include <vector> 14 15namespace { 16struct Ty {int id ;const char * name ;int bytes_per_256 ; }; 17const Ty kTypes []= {{16 ,"IQ2_XXS" ,66 }, {17 ,"IQ2_XS" ,74 }, {22 ,"IQ2_S" ,82 }, {18 ,"IQ3_XXS" ,98 }, 18 {21 ,"IQ3_S" ,110 }, {20 ,"IQ4_NL" ,144 }, {42 ,"Q2_0" ,72 }, 19// Unsloth's files (MODEL-UPDATE): gate/up Q4_K / Q5_K / Q6_K / Q8_0, down Q5_1 / Q8_0 20 {12 ,"Q4_K" ,144 }, {13 ,"Q5_K" ,176 }, {14 ,"Q6_K" ,210 }, {7 ,"Q5_1" ,192 }, {8 ,"Q8_0" ,272 }, 21 {2 ,"Q4_0" ,144 }, {3 ,"Q4_1" ,160 }, {6 ,"Q5_0" ,176 }}; 22uint64_t fnv (const uint16_t * p ,size_t n ) { 23uint64_t h = 1469598103934665603ull ; 24for (size_t i = 0 ;i < n ;++ i ) {h ^=p [i ];h *=1099511628211ull ; } 25return h ; 26} 27}// namespace 28 29int main (int argc ,char ** argv ) { 30const int want = argc > 1 && std::strcmp (argv [1 ],"all" )!= 0 ? std::atoi (argv [1 ]) :-1 ; 31const int experts = argc > 2 ? std::atoi (argv [2 ]) :32 ; 32const int64_t n_ff = 1280 ,n_embd = 2560 ; 33 sycl::queue * s = & dpct::get_in_order_queue (); 34for (const Ty & t :kTypes ) { 35if (want >=0 && t .id != want )continue ; 36const size_t per_mat = (size_t )n_ff * n_embd /256 * t .bytes_per_256 ;// one of gate / up / down 37 std::vector < uint8_t > hb (per_mat ); 38uint32_t x = 12345u + (uint32_t )t .id ; 39for (auto & b :hb ) {x = x * 1664525u + 1013904223u ;b = (uint8_t ) (x >>24 ); } 40uint8_t * src = (uint8_t * ) sycl::malloc_device (per_mat * 3 * experts ,* s );// gate, up, down per expert 41for (int e = 0 ;e < experts * 3 ;++ e )s -> memcpy (src + (size_t )e * per_mat ,hb .data (),per_mat ); 42const size_t gu_out = (size_t )2 * n_ff * n_embd ,d_out = (size_t )n_embd * n_ff ; 43uint16_t * dst = (uint16_t * ) sycl::malloc_device ((gu_out + d_out )* 2 * experts ,* s ); 44s -> wait (); 45auto run = [& ]() { 46for (int e = 0 ;e < experts ;++ e ) { 47const uint8_t * b = src + (size_t )e * 3 * per_mat ; 48uint16_t * o = dst + (size_t )e * (gu_out + d_out ); 49 strata::kernels::iq_dequant_gu_f16 (t .id ,b ,b + per_mat ,n_ff ,n_embd ,o ,s ); 50 strata::kernels::iq_dequant_f16 (t .id ,b + 2 * per_mat ,n_embd * n_ff ,o + gu_out ,s ); 51 } 52s -> wait (); 53 }; 54for (int i = 0 ;i < 3 ;++ i )run (); 55const int reps = 10 ; 56 std::vector < double > ms ; 57for (int i = 0 ;i < reps ;++ i ) { 58const auto t0 = std::chrono::steady_clock::now (); 59run (); 60ms .push_back (std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count ()); 61 } 62 std::sort (ms .begin (),ms .end ()); 63const double med = ms [reps /2 ]; 64const double wr = (double ) (gu_out + d_out )* 2 * experts ,rd = (double )per_mat * 3 * experts ; 65 std::vector < uint16_t > ho ((gu_out + d_out )* 2 );// hash expert 0 and the last expert 66s -> memcpy (ho .data (),dst , (gu_out + d_out )* 2 ).wait (); 67const uint64_t h0 = fnv (ho .data (),gu_out + d_out ); 68s -> memcpy (ho .data (),dst + (size_t ) (experts - 1 )* (gu_out + d_out ), (gu_out + d_out )* 2 ).wait (); 69const uint64_t h1 = fnv (ho .data (),gu_out + d_out ); 70 std::printf ("%-8s %2d experts: median %7.2f ms write %6.1f GB/s (read %5.1f GB/s) hash %016llx %016llx\n" ,t .name , 71experts ,med ,wr /med /1e6 ,rd /med /1e6 , (unsigned long long )h0 , (unsigned long long )h1 ); 72 sycl::free (src ,* s ); 73 sycl::free (dst ,* s ); 74 } 75return 0 ; 76}