okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9c143c403
main
1// tests/quant/quant_bench.cpp - one-thread speed of every dot kernel of the layer (all contracts and ISAs this CPU can run), no ggml. 2// 3// usage: quant_bench [--type NAME] [--mb N] [--reps N] [--n N] [--arith avx2|avx|generic] 4// For each weight type and each runnable kernel: GB/s of weight bytes with the rows streamed from DRAM (a --mb MB set) and with them resident in L2 5// (256 KB). Interleaved A/B discipline: the kernels of one type run round-robin, `reps` rounds, the median of each is printed. 6#include <algorithm> 7#include <chrono> 8#include <cstdio> 9#include <cstdlib> 10#include <cstring> 11#include <string> 12#include <vector> 13 14#include "strata/quant/act.hpp" 15#include "strata/quant/dot.hpp" 16#include "strata/quant/isa.hpp" 17#include "strata/quant/types.hpp" 18 19namespace sq= strata::quant; 20 21namespace { 22uint64_t g_s = 88172645463325252ull ; 23uint32_t rnd () { 24g_s ^=g_s <<13 ; 25g_s ^=g_s >>7 ; 26g_s ^=g_s <<17 ; 27return (uint32_t ) (g_s >>16 ); 28} 29 30double run (const sq::DotKernel * k ,const std::vector < uint8_t >& w ,size_t rb ,int n ,size_t rows ,const void * act ,float * sink ) { 31using C = std::chrono::steady_clock ; 32const auto t0 = C ::now (); 33float acc = 0.f ; 34for (size_t r = 0 ;r < rows ;++ r ) { 35float s ; 36k -> fn (n ,& s ,w .data ()+ r * rb ,act ); 37acc += s ; 38 } 39* sink += acc ; 40return std::chrono::duration < double > (C ::now ()- t0 ).count (); 41} 42}// namespace 43 44int main (int argc ,char ** argv ) { 45const char * only = nullptr ; 46int mb = 256 ,reps = 7 ,n_el = 4096 ; 47 sq::Arith want = sq::default_arith (); 48for (int i = 1 ;i < argc ;++ i ) { 49if (!std::strcmp (argv [i ],"--type" )&& i + 1 < argc )only = argv [++ i ]; 50else if (!std::strcmp (argv [i ],"--mb" )&& i + 1 < argc )mb = std::atoi (argv [++ i ]); 51else if (!std::strcmp (argv [i ],"--reps" )&& i + 1 < argc )reps = std::atoi (argv [++ i ]); 52else if (!std::strcmp (argv [i ],"--n" )&& i + 1 < argc )n_el = std::atoi (argv [++ i ]); 53else if (!std::strcmp (argv [i ],"--arith" )&& i + 1 < argc ) { 54const std::string a = argv [++ i ]; 55want = a == "avx" ? sq::Arith ::Avx :a == "generic" ? sq::Arith ::Generic : sq::Arith ::Avx2 ; 56 } 57 } 58 std::printf ("quant_bench: contract %s, best isa %s, DRAM set %d MB, L2 set 256 KB, n=%d, median of %d\n" , sq::arith_name (want ), sq::isa_name (sq::best_isa ()), 59mb ,n_el ,reps ); 60float sink = 0.f ; 61for (int t = 0 ;t < 150 ;++ t ) { 62const sq::TypeInfo * ti = sq::type_info (t ); 63if (ti == nullptr || !ti -> has_dot || (only && std::strcmp (only ,ti -> name )))continue ; 64const sq::DotKernel * ks [16 ]; 65const int nk = sq::dot_kernels (ti -> type ,want ,ks ,16 ); 66 std::vector < const sq::DotKernel *> run_k ; 67for (int i = 0 ;i < nk ;++ i ) 68if (sq::isa_ok (ks [i ]-> isa ))run_k .push_back (ks [i ]); 69if (run_k .empty ())continue ; 70const sq::TypeInfo * ai = sq::type_info (ti -> act ); 71const int unit = std::max (ti -> blck ,ai -> blck ); 72const int n = n_el /unit * unit ; 73const size_t rb = sq::row_bytes (t ,n ); 74const size_t rows_dram = (size_t )mb * 1048576 /rb ,rows_l2 = std::max < size_t > (1 ,256 * 1024 /rb ); 75 std::vector < uint8_t > w (rows_dram * rb ); 76for (auto & b :w )b = (uint8_t )rnd (); 77// sane scales: first two bytes of each block as fp16 ~ 0.01 78for (size_t o = 0 ;o + ti -> bytes <=w .size ();o += ti -> bytes ) { 79if (ti -> type == sq::Type ::IQ1_M || ti -> type == sq::Type ::NVFP4 || ti -> type == sq::Type ::MXFP4 || ti -> type == sq::Type ::Q2_K || 80ti -> type == sq::Type ::Q3_K || ti -> type == sq::Type ::Q6_K )continue ;// scale not at the front: random bytes still give finite-enough sums 81w [o ]= 0x66 ,w [o + 1 ]= 0x24 ; 82 } 83 std::vector < float > x ((size_t )n ); 84for (auto & v :x )v = (float ) ((int ) (rnd () %2001 )- 1000 )* 1e-3f ; 85 std::vector < uint8_t > act ((size_t ) sq::row_bytes ((int )ti -> act ,n )); 86const sq::ActKernel * ak = sq::act_kernel (ti -> act ,want ); 87if (ak == nullptr )continue ; 88ak -> fn (x .data (),act .data (),n ); 89 std::vector < std::vector < double >> td (run_k .size ()),tl (run_k .size ()); 90for (int rep = 0 ;rep < reps ;++ rep ) 91for (size_t i = 0 ;i < run_k .size ();++ i ) { 92td [i ].push_back (run (run_k [i ],w ,rb ,n ,rows_dram ,act .data (),& sink )); 93double best = 1e9 ; 94for (int r2 = 0 ;r2 < 8 ;++ r2 )best = std::min (best ,run (run_k [i ],w ,rb ,n ,rows_l2 ,act .data (),& sink )); 95tl [i ].push_back (best ); 96 } 97for (size_t i = 0 ;i < run_k .size ();++ i ) { 98 std::sort (td [i ].begin (),td [i ].end ()); 99 std::sort (tl [i ].begin (),tl [i ].end ()); 100const double gd = (double )rows_dram * rb /td [i ][td [i ].size () /2 ] /1e9 ,gl = (double )rows_l2 * rb /tl [i ][tl [i ].size () /2 ] /1e9 ; 101 std::printf (" %-8s %-26s %-6s DRAM %6.2f GB/s L2 %6.2f GB/s\n" ,ti -> name ,run_k [i ]-> name , sq::isa_name (run_k [i ]-> isa ),gd ,gl ); 102 } 103 } 104if (sink == 12345.f ) std::printf ("\n" ); 105return 0 ; 106}