okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1// sycl/src/kernels/q6k_align_bench.cpp - SYCL port: does Q6_K's 210-byte (2-byte aligned) block stride cost the wide 2// mmvq kernel its bandwidth? Same weights at stride 210 and repacked at 224 (16-byte aligned); outputs must match. 3#include "strata/kernels/native_mmvq.hpp" 4#include <sycl/sycl.hpp> 5#include <dpct/dpct.hpp> 6#include <chrono> 7#include <cmath> 8#include <cstdio> 9#include <cstring> 10#include <functional> 11#include <vector> 12int main () { 13 sycl::queue * s = & dpct::get_in_order_queue (); 14const int shapes [][2 ]= {{2560 ,10240 }, {2560 ,12288 }, {6144 ,2560 }, {2560 ,248320 }}; 15for (auto & sh :shapes ) { 16const int n_in = sh [0 ],n_out = sh [1 ]; 17const size_t nb = (size_t )n_out * (n_in /256 ); 18 std::vector < uint8_t > hw (nb * 210 ),hp (nb * 224 ,0 ); 19for (size_t i = 0 ;i < hw .size ();++ i )hw [i ]= (uint8_t ) (i * 2654435761u >>13 )& 0x3b ; 20for (size_t b = 0 ;b < nb ;++ b ) std::memcpy (& hp [b * 224 ],& hw [b * 210 ],210 ); 21void * w = sycl::malloc_device (hw .size (),* s ); 22void * wp = sycl::malloc_device (hp .size (),* s ); 23s -> memcpy (w ,hw .data (),hw .size ()).wait ();s -> memcpy (wp ,hp .data (),hp .size ()).wait (); 24for (int nc : {1 ,2 ,4 ,6 }) { 25 std::vector < float > hx ((size_t )nc * n_in ); 26for (size_t i = 0 ;i < hx .size ();++ i )hx [i ]= (float ) ((i * 7919 ) %1000 ) /500.f - 1.f ; 27float * x = sycl::malloc_device < float > (hx .size (),* s ); 28void * xq = sycl::malloc_device ((size_t )nc * (n_in /32 )* 36 ,* s ); 29float * y = sycl::malloc_device < float > ((size_t )nc * n_out ,* s ); 30s -> memcpy (x ,hx .data (),hx .size ()* 4 ).wait (); 31 strata::kernels::native_quantize_q8_1 (x ,xq ,n_in ,nc ,s ); 32auto time = [& ](const std::function < void ()>& run , std::vector < float >& out ) { 33const auto w0 = std::chrono::steady_clock::now (); 34while (std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- w0 ).count ()< 150 ) { 35for (int i = 0 ;i < 10 ;++ i )run (); 36s -> wait (); 37 } 38const int it = 200 ; 39const auto t0 = std::chrono::steady_clock::now (); 40for (int i = 0 ;i < it ;++ i )run (); 41s -> wait (); 42out .resize ((size_t )nc * n_out ); 43s -> memcpy (out .data (),y ,out .size ()* 4 ).wait (); 44return std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t0 ).count () /it ; 45 }; 46 std::vector < float > y0 ,y1 ; 47const double a = time ([& ] { strata::kernels::native_q6_k_mmvq (w ,xq ,y ,n_in ,n_out ,nc ,s ); },y0 ); 48const double b = time ([& ] { strata::kernels::native_q6_k_mmvq_stride224 (wp ,xq ,y ,n_in ,n_out ,nc ,s ); },y1 ); 49double num = 0 ,den = 0 ; 50for (size_t i = 0 ;i < y0 .size ();++ i ) {num += std::fabs ((double )y0 [i ]- y1 [i ]);den += std::fabs ((double )y0 [i ]); } 51const double gb = nb * 210.0 /1e3 ; 52 std::printf ("q6_k %5d x %6d cols %d: stride 210 %7.1f us %6.1f GB/s | stride 224 %7.1f us %6.1f GB/s (%.2fx) | rel diff %.1e\n" , 53n_in ,n_out ,nc ,a ,gb /a ,b ,gb /b ,a /b ,num / (den > 0 ?den :1 )); 54 sycl::free (x ,* s ); sycl::free (xq ,* s ); sycl::free (y ,* s ); 55 } 56 sycl::free (w ,* s ); sycl::free (wp ,* s ); 57 } 58return 0 ; 59}