okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0a9381336
main
1// tests/quant/quant_bench_ggml.cpp - the layer's dot kernels and activation quantizers against ggml-cpu's, speed (links ggml-cpu: test only). 2// 3// For every dot type: R rows of n elements (64 rows of 8192: an L2-resident chunk; --rows 4096 streams from DRAM), one activation row; ours and ggml's alternate in interleaved rounds (ABAB...), each round 4// runs every row `reps` times; the report is the median of the per-round ratio ggml_time / our_time (> 1: ours is faster) and the number of rounds ours won. 5// Bit-exactness is the oracle's job (quant_parity); this only answers "is the row faster or slower than ggml's". 6// 7// usage: quant_bench_ggml [--rounds N] [--n ELEMENTS] [--rows R] [--type NAME] 8#include <algorithm> 9#include <chrono> 10#include <cstdio> 11#include <cstdlib> 12#include <cstring> 13#include <string> 14#include <vector> 15 16#include "ggml-cpu.h" 17#include "ggml.h" 18#include "strata/quant/act.hpp" 19#include "strata/quant/dot.hpp" 20#include "strata/quant/isa.hpp" 21#include "strata/quant/types.hpp" 22 23namespace sq= strata::quant; 24 25namespace { 26 27double now () {return std::chrono::duration < double > (std::chrono::steady_clock::now ().time_since_epoch ()).count (); } 28 29struct Rng { 30uint64_t s = 88172645463325252ull ; 31float gauss () { 32float a = 0 ; 33for (int i = 0 ;i < 4 ;++ i ) { 34s ^=s >>12 ;s ^=s <<25 ;s ^=s >>27 ; 35a += (float ) ((s * 0x2545F4914F6CDD1Dull ) >>40 ) /16777216.0f - 0.5f ; 36 } 37return a ; 38 } 39}; 40 41sq::Arith oracle_arith () { 42#if defined(__aarch64__ ) 43return sq::Arith ::Neon ; 44#else 45if (ggml_cpu_has_avx2 ()&& ggml_cpu_has_fma ())return sq::Arith ::Avx2 ; 46if (ggml_cpu_has_avx ())return sq::Arith ::Avx ; 47return sq::Arith ::Generic ; 48#endif 49} 50 51double median (std::vector < double > v ) { 52 std::sort (v .begin (),v .end ()); 53return v [v .size () /2 ]; 54} 55 56}// namespace 57 58int main (int argc ,char ** argv ) { 59int rounds = 11 ,n = 8192 ,R = 64 ; 60const char * only = nullptr ; 61for (int i = 1 ;i < argc ;++ i ) { 62if (!std::strcmp (argv [i ],"--rounds" )&& i + 1 < argc )rounds = std::atoi (argv [++ i ]); 63else if (!std::strcmp (argv [i ],"--n" )&& i + 1 < argc )n = std::atoi (argv [++ i ]); 64else if (!std::strcmp (argv [i ],"--rows" )&& i + 1 < argc )R = std::atoi (argv [++ i ]);// 4096+ rows stream from DRAM like the pool's experts 65else if (!std::strcmp (argv [i ],"--type" )&& i + 1 < argc )only = argv [++ i ]; 66 } 67ggml_cpu_init (); 68const sq::Arith ar = oracle_arith (); 69 std::printf ("quant_bench_ggml: contract %s, n=%d, %d rounds (ratio = ggml time / ours; > 1 means ours is faster)\n" , sq::arith_name (ar ),n ,rounds ); 70static const sq::Type kTypes []= {sq::Type ::Q2_0 , sq::Type ::Q4_0 , sq::Type ::Q4_1 , sq::Type ::Q5_0 , sq::Type ::Q5_1 , sq::Type ::Q8_0 , sq::Type ::IQ4_NL , 71 sq::Type ::MXFP4 , sq::Type ::NVFP4 , sq::Type ::Q2_K , sq::Type ::Q3_K , sq::Type ::Q4_K , sq::Type ::Q5_K , sq::Type ::Q6_K , 72 sq::Type ::IQ1_S , sq::Type ::IQ1_M , sq::Type ::IQ2_XXS , sq::Type ::IQ2_XS , sq::Type ::IQ2_S , sq::Type ::IQ3_XXS , 73 sq::Type ::IQ3_S , sq::Type ::IQ4_XS }; 74Rng rng ; 75for (sq::Type st :kTypes ) { 76const sq::TypeInfo * ti = sq::type_info (st ); 77if (only != nullptr && std::strcmp (only ,ti -> name )!= 0 )continue ; 78const ggml_type gt = (ggml_type ) (int )st ; 79const ggml_type_traits_cpu * tr = ggml_get_type_traits_cpu (gt ); 80const sq::DotKernel * k = sq::dot_kernel (st ,ar ); 81if (tr == nullptr || tr -> vec_dot == nullptr || k == nullptr )continue ; 82const ggml_type_traits_cpu * ta = ggml_get_type_traits_cpu (tr -> vec_dot_type ); 83const int unit = std::max < int > (ti -> blck , (int )ggml_blck_size (tr -> vec_dot_type )); 84const int nn = n /unit * unit ; 85 std::vector < float > f ((size_t )nn * R ); 86for (float & v :f )v = rng .gauss (); 87 std::vector < uint8_t > w (ggml_row_size (gt ,nn )* R ),act (ggml_row_size (tr -> vec_dot_type ,nn )); 88ggml_quantize_init (gt ); 89 std::vector < float > im ((size_t )nn ,1.f ); 90ggml_quantize_chunk (gt ,f .data (),w .data (),0 ,R ,nn ,ggml_quantize_requires_imatrix (gt ) ?im .data () :nullptr ); 91ta -> from_float (f .data (),act .data (),nn ); 92const size_t rb = ggml_row_size (gt ,nn ); 93// enough repetitions for ~20 ms per arm 94int reps = 1 ; 95float sink = 0.f ; 96for (;;reps *=2 ) { 97const double t0 = now (); 98for (int r = 0 ;r < reps ;++ r ) 99for (int row = 0 ;row < R ;++ row ) { 100float s ; 101tr -> vec_dot (nn ,& s ,0 ,w .data ()+ rb * row ,0 ,act .data (),0 ,1 ); 102sink += s ; 103 } 104if (now ()- t0 > 0.02 || reps >=4096 )break ; 105 } 106 std::vector < double > ratios ; 107double tg_all = 0 ,to_all = 0 ; 108int won = 0 ; 109for (int rd = 0 ;rd < rounds ;++ rd ) { 110double tg ,to ; 111auto run_ggml = [& ] { 112const double t0 = now (); 113for (int r = 0 ;r < reps ;++ r ) 114for (int row = 0 ;row < R ;++ row ) { 115float s ; 116tr -> vec_dot (nn ,& s ,0 ,w .data ()+ rb * row ,0 ,act .data (),0 ,1 ); 117sink += s ; 118 } 119return now ()- t0 ; 120 }; 121auto run_ours = [& ] { 122const double t0 = now (); 123for (int r = 0 ;r < reps ;++ r ) 124for (int row = 0 ;row < R ;++ row ) { 125float s ; 126k -> fn (nn ,& s ,w .data ()+ rb * row ,act .data ()); 127sink += s ; 128 } 129return now ()- t0 ; 130 }; 131if (rd & 1 ) {to = run_ours ();tg = run_ggml (); }else {tg = run_ggml ();to = run_ours (); } 132ratios .push_back (tg /to ); 133tg_all += tg ; 134to_all += to ; 135if (to < tg )++ won ; 136 } 137const double bytes = (double )rb * R * reps * rounds ; 138 std::printf (" %-8s %-18s median ratio %.3f ours faster in %2d/%d rounds ours %.1f GB/s ggml %.1f GB/s%s\n" ,ti -> name ,k -> name ,median (ratios ),won ,rounds , 139bytes /to_all /1e9 ,bytes /tg_all /1e9 ,sink == 12345.f ?" " :"" ); 140 } 141// activation quantizers 142for (sq::Type at : {sq::Type ::Q8_0 , sq::Type ::Q8_1 , sq::Type ::Q8_K }) { 143const sq::TypeInfo * ti = sq::type_info (at ); 144const ggml_type_traits_cpu * ta = ggml_get_type_traits_cpu ((ggml_type ) (int )at ); 145const sq::ActKernel * ak = sq::act_kernel (at ,ar ); 146if (ta == nullptr || ta -> from_float == nullptr || ak == nullptr )continue ; 147if (only != nullptr && std::strcmp (only ,ti -> name )!= 0 )continue ; 148const int nn = 4096 ; 149 std::vector < float > f ((size_t )nn ); 150for (float & v :f )v = rng .gauss (); 151 std::vector < uint8_t > o ((size_t )nn /ti -> blck * ti -> bytes + 64 ); 152const int reps = 2000 ; 153 std::vector < double > ratios ; 154int won = 0 ; 155for (int rd = 0 ;rd < rounds ;++ rd ) { 156double tg ,to ; 157auto g = [& ] {const double t0 = now ();for (int r = 0 ;r < reps ;++ r )ta -> from_float (f .data (),o .data (),nn );return now ()- t0 ; }; 158auto u = [& ] {const double t0 = now ();for (int r = 0 ;r < reps ;++ r )ak -> fn (f .data (),o .data (),nn );return now ()- t0 ; }; 159if (rd & 1 ) {to = u ();tg = g (); }else {tg = g ();to = u (); } 160ratios .push_back (tg /to ); 161if (to < tg )++ won ; 162 } 163 std::printf (" act %-5s %-18s median ratio %.3f ours faster in %2d/%d rounds\n" ,ti -> name ,ak -> name ,median (ratios ),won ,rounds ); 164 } 165return 0 ; 166}