okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1// sycl/src/kernels/mmvq_bench.cpp - SYCL port: time the dense native_mmvq kernels on random weights. 2// mmvq_bench [n_in] [n_out] [ncols] (weights are random bytes: dequant cost is data-independent) 3#include "strata/kernels/native_mmvq.hpp" 4#include <sycl/sycl.hpp> 5#include <dpct/dpct.hpp> 6#include <algorithm> 7#include <chrono> 8#include <cstdio> 9#include <cstdlib> 10#include <cstring> 11#include <vector> 12#include <cmath> 13int main (int argc ,char ** argv ) { 14const int n_in = argc > 1 ? std::atoi (argv [1 ]) :2560 ,n_out = argc > 2 ? std::atoi (argv [2 ]) :2560 ,ncols = argc > 3 ? std::atoi (argv [3 ]) :4 ; 15 sycl::queue * s = & dpct::get_in_order_queue (); 16// Q6_K: 210 bytes per 256 weights 17const size_t wbytes = (size_t )n_out * (n_in /256 )* 210 ; 18 std::vector < uint8_t > hw (wbytes ); 19for (size_t i = 0 ;i < wbytes ;++ i )hw [i ]= (uint8_t ) (i * 2654435761u >>13 ); 20for (size_t b = 0 ;b < wbytes /210 ;++ b ) {hw [b * 210 + 208 ]= 0x00 ;hw [b * 210 + 209 ]= 0x3c ; }// d = 1.0 (finite) 21 std::vector < float > hx ((size_t )ncols * n_in ); 22for (size_t i = 0 ;i < hx .size ();++ i )hx [i ]= (float ) ((i * 7919 ) %1000 ) /500.f - 1.f ; 23void * w = sycl::malloc_device (wbytes ,* s ); 24float * x = sycl::malloc_device < float > (hx .size (),* s ); 25void * xq = sycl::malloc_device ((size_t )ncols * (n_in /32 )* 36 ,* s ); 26float * y = sycl::malloc_device < float > ((size_t )ncols * n_out ,* s ); 27s -> memcpy (w ,hw .data (),wbytes ).wait ();s -> memcpy (x ,hx .data (),hx .size ()* 4 ).wait (); 28 strata::kernels::native_quantize_q8_1 (x ,xq ,n_in ,ncols ,s ); 29 {// correctness: the 16-byte kernel against the shared one 30 std::vector < float > y0 ((size_t )ncols * n_out ),y1 (y0 .size ()); 31 strata::kernels::native_mmvq_set_q6k_wide (false); 32 strata::kernels::native_q6_k_mmvq (w ,xq ,y ,n_in ,n_out ,ncols ,s );s -> wait ();s -> memcpy (y0 .data (),y ,y0 .size ()* 4 ).wait (); 33 strata::kernels::native_mmvq_set_q6k_wide (true); 34 strata::kernels::native_q6_k_mmvq (w ,xq ,y ,n_in ,n_out ,ncols ,s );s -> wait ();s -> memcpy (y1 .data (),y ,y1 .size ()* 4 ).wait (); 35double num = 0 ,den = 0 ;for (size_t i = 0 ;i < y0 .size ();++ i ) {num += std::fabs ((double )y0 [i ]- y1 [i ]);den += std::fabs ((double )y0 [i ]); } 36 std::printf ("wide vs shared: rel %.3e over %zu outputs (first %g vs %g)\n" ,num / (den > 0 ?den :1 ),y0 .size (),y0 [0 ],y1 [0 ]); 37 strata::kernels::native_mmvq_set_q6k_wide (std::getenv ("STRATA_MMVQ_WIDE" )== nullptr || std::atoi (std::getenv ("STRATA_MMVQ_WIDE" ))!= 0 ); 38 } 39 {// warm up until the clocks are up (~300 ms of work), then time 40const auto w0 = std::chrono::steady_clock::now (); 41while (std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- w0 ).count ()< 300 ) { 42for (int i = 0 ;i < 20 ;++ i ) strata::kernels::native_q6_k_mmvq (w ,xq ,y ,n_in ,n_out ,ncols ,s ); 43s -> wait (); 44 } 45 } 46const int it = 400 ; 47const auto t0 = std::chrono::steady_clock::now (); 48for (int i = 0 ;i < it ;++ i ) strata::kernels::native_q6_k_mmvq (w ,xq ,y ,n_in ,n_out ,ncols ,s ); 49s -> wait (); 50const double us = std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t0 ).count () /it ; 51 std::vector < float > hy ((size_t )ncols * n_out ); 52s -> memcpy (hy .data (),y ,hy .size ()* 4 ).wait (); 53unsigned long long cs = 1469598103934665603ull ; 54for (float v :hy ) {unsigned u ; std::memcpy (& u ,& v ,4 );cs = (cs ^u )* 1099511628211ull ; } 55 std::printf ("q6_k mmvq %d x %d, %d cols: %.1f us (%.1f GB/s of weights) checksum %016llx\n" ,n_in ,n_out ,ncols ,us ,wbytes /us /1e3 ,cs ); 56// STRATA_BENCH_PLANAR=1 (#2340): the same matrix as STRATA_Q6_PLANAR planes, bitwise against the GGUF-layout result above; then a 57// gathered row subset (the MTP draft head's case) against the same rows of the full result. Exit 1 on any difference. 58const char * pe = std::getenv ("STRATA_BENCH_PLANAR" ); 59if (pe && std::atoi (pe )!= 0 ) { 60void * w2 = sycl::malloc_device (wbytes ,* s ); 61s -> memcpy (w2 ,hw .data (),wbytes ).wait (); 62void * wp = strata::kernels::native_q6_k_planar_adopt (w2 ,n_in ,n_out ,"bench matrix" ); 63if (!strata::kernels::native_q6_k_is_planar (wp )) { std::printf ("planar: adopt declined\n" );return 1 ; } 64float * y2 = sycl::malloc_device < float > ((size_t )ncols * n_out ,* s ); 65 strata::kernels::native_q6_k_mmvq (wp ,xq ,y2 ,n_in ,n_out ,ncols ,s );s -> wait (); 66 std::vector < float > hp ((size_t )ncols * n_out ); 67s -> memcpy (hp .data (),y2 ,hp .size ()* 4 ).wait (); 68const bool same = std::memcmp (hp .data (),hy .data (),hp .size ()* 4 )== 0 ; 69for (int i = 0 ;i < 20 ;++ i ) strata::kernels::native_q6_k_mmvq (wp ,xq ,y2 ,n_in ,n_out ,ncols ,s ); 70s -> wait (); 71const auto p0 = std::chrono::steady_clock::now (); 72for (int i = 0 ;i < it ;++ i ) strata::kernels::native_q6_k_mmvq (wp ,xq ,y2 ,n_in ,n_out ,ncols ,s ); 73s -> wait (); 74const double pus = std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- p0 ).count () /it ; 75 std::printf ("q6_k planar %d x %d, %d cols: %.1f us (%.1f GB/s of weights) bitwise %s\n" ,n_in ,n_out ,ncols ,pus ,wbytes /pus /1e3 , 76same ?"EQUAL" :"DIFFERENT" ); 77int rc = same ?0 :1 ; 78const int nsel = std::max (1 ,n_out /3 ); 79 std::vector < int32_t > rows (nsel ); 80for (int i = 0 ;i < nsel ;++ i )rows [i ]= (int32_t ) (((uint64_t )i * 2654435761ull + 12345 ) % (uint64_t )n_out ); 81int32_t * rd = sycl::malloc_device < int32_t > (nsel ,* s ); 82s -> memcpy (rd ,rows .data (),nsel * 4 ).wait (); 83void * wg = strata::kernels::native_q6_k_planar_gather (wp ,n_in ,rd ,nsel ,"bench subset" ); 84if (!wg ) { std::printf ("planar gather: declined\n" );return 1 ; } 85float * y3 = sycl::malloc_device < float > ((size_t )ncols * nsel ,* s ); 86 strata::kernels::native_q6_k_mmvq (wg ,xq ,y3 ,n_in ,nsel ,ncols ,s );s -> wait (); 87 std::vector < float > hg ((size_t )ncols * nsel ); 88s -> memcpy (hg .data (),y3 ,hg .size ()* 4 ).wait (); 89size_t bad = 0 ; 90for (int c = 0 ;c < ncols ;++ c ) 91for (int i = 0 ;i < nsel ;++ i ) 92if (std::memcmp (& hg [(size_t )c * nsel + i ],& hy [(size_t )c * n_out + rows [i ]],4 )!= 0 )++ bad ; 93 std::printf ("q6_k planar gather %d of %d rows, %d cols: %zu of %zu outputs differ\n" ,nsel ,n_out ,ncols ,bad ,hg .size ()); 94if (bad )rc = 1 ; 95return rc ; 96 } 97return 0 ; 98}