okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/tests/qsa_prompt_parity.cpp - qsa_attn_mm (qsa_prompt.metal, the matrix units, fp16 operands) against qsa_attn 2// (fp32, itself checked by qsa_parity), both followed by qsa_merge: the attention output of T query tokens on random 3// int8 caches, within the budget (all cells, causal) and past it (2051 selected cells per token); then the speed. 4// qsa_prompt_parity [--bench] 5#include "coop_probe.h" 6#include "mtl_rt.h" 7#include "ref_quant.h" 8#include "engine/geo.h" 9 10#include <algorithm> 11#include <cmath> 12#include <cstdio> 13#include <cstdlib> 14#include <set> 15#include <string> 16#include <vector> 17 18namespace { 19constexpr int QH = strata_metal::geo::QH ,QKV = strata_metal::geo::QKV ,QD = strata_metal::geo::QD ,QB = strata_metal::geo::BUD ,NCH = strata_metal::geo::QCHUNKS ; 20struct QsaArgs {uint32_t pos0 ,pos_base ,max_cells ,n_tok ;float eps ; }; 21double median (std::vector < double > v ) { std::sort (v .begin (),v .end ());return v [v .size () /2 ]; } 22}// namespace 23 24int main (int argc ,char ** argv ) { 25const bool bench = argc > 1 && std::string (argv [1 ])== "--bench" ; 26 std::setvbuf (stdout ,nullptr ,_IOLBF ,0 ); 27const bool noflash = std::getenv ("QSA_NOFLASH" )!= nullptr ; 28 mtl::Context cx ; 29const int lib = cx .compile ({"qsa.metal" ,"qsa_prompt.metal" }, strata_metal::geo::prelude ()+ "#define NO_GGML_COMMON 1\n" ); 30 std::printf ("device %s\n" ,cx .device_name ().c_str ()); 31const int T = bench ?2048 :96 ,MAXC = 40000 ; 32 mtl::Buffer kq = cx .alloc ((size_t )MAXC * QKV * QD ),vq = cx .alloc ((size_t )MAXC * QKV * QD ),ksc = cx .alloc (2 * (size_t )MAXC * QKV * 4 ), 33vsc = cx .alloc (2 * (size_t )MAXC * QKV * 4 ),qcur = cx .alloc (4 * (size_t )T * QH * QD ),qh = cx .alloc (2 * (size_t )T * QH * QD ), 34qfull = cx .alloc (4 * (size_t )T * QH * 2 * QD ),ids = cx .alloc (4 * (size_t )T * QB ), 35part = cx .alloc (4 * (size_t )T * NCH * QH * (QD + 2 )),xa = cx .alloc (4 * ((size_t )4 * T * QH * QD + (size_t )T * QH * QD /16 )), 36xb = cx .alloc (4 * ((size_t )4 * T * QH * QD + (size_t )T * QH * QD /16 )); 37 ref::Rng r (5 ); 38for (size_t i = 0 ;i < kq .bytes ;++ i ) {kq .as < int8_t > ()[i ]= (int8_t )((int )(r .next () %255 )- 127 );vq .as < int8_t > ()[i ]= (int8_t )((int )(r .next () %255 )- 127 ); } 39for (size_t i = 0 ;i < ksc .bytes /2 ;++ i ) {ksc .as < uint16_t > ()[i ]= ref::f32_to_f16 (0.004f + 0.004f * r .uni ());vsc .as < uint16_t > ()[i ]= ref::f32_to_f16 (0.004f + 0.004f * r .uni ()); } 40for (size_t i = 0 ;i < (size_t )T * QH * QD ;++ i )qcur .as < float > ()[i ]= r .sym ()* 3.0f ; 41for (size_t i = 0 ;i < (size_t )T * QH * 2 * QD ;++ i )qfull .as < float > ()[i ]= r .sym (); 42 mtl::Pipeline pa = cx .pipeline (lib ,"qsa_attn" ),pm = cx .pipeline (lib ,"qsa_merge" ),pmm = cx .pipeline (lib ,"qsa_attn_mm" , mtl::Constants ().b (7 ,coop_k4 (cx )).i (8 , std::getenv ("QSA_STOP" ) ? std::atoi (std::getenv ("QSA_STOP" )) :0 )), 43pq = cx .pipeline (lib ,"qsa_q_f16" ),pf = cx .pipeline (lib ,"qsa_flash" , mtl::Constants ().b (7 ,coop_k4 (cx ))); 44 mtl::Buffer xf = cx .alloc (2 * (size_t )T * QH * QD ); 45 std::printf ("pipelines: qsa_attn_mm max threads %d, tg mem %d; qsa_flash max threads %d, tg mem %d\n" ,pmm .max_threads , 46pmm .static_tg_mem ,pf .max_threads ,pf .static_tg_mem ); 47int fails = 0 ; 48for (uint32_t pos0 : {0u ,30000u }) { 49// ids: within the budget the identity; past it 2051 distinct ascending cells < n_kv 50for (int t = 0 ;t < T ;++ t ) { 51const uint32_t n_kv = pos0 + t + 1 ; 52int * o = ids .as < int > ()+ (size_t )t * QB ; 53if (n_kv <= (uint32_t )QB ) {for (uint32_t c = 0 ;c < n_kv ;++ c )o [c ]= (int )c ;continue ; } 54 std::set < int > s ; 55while ((int )s .size ()< QB )s .insert ((int )(r .next () %n_kv )); 56int j = 0 ; 57for (int c :s )o [j ++ ]= c ; 58 } 59QsaArgs a {pos0 ,0 , (uint32_t )MAXC , (uint32_t )T ,1e-6f }; 60auto D = [& ](const mtl::Pipeline & p , std::vector < mtl::Arg > args ,uint32_t gx ,uint32_t gy ,uint32_t gz ,uint32_t tg ) { 61 mtl::Dispatch d ;d .p = & p ;d .args = std::move (args );d .grid [0 ]= gx ;d .grid [1 ]= gy ;d .grid [2 ]= gz ;d .tg [0 ]= tg ;return d ; 62 }; 63const uint32_t nq = T * QH * QD ; 64 std::vector < mtl::Dispatch > ref_ds = { 65D (pa , {mtl::Arg ::B (qcur ), mtl::Arg ::B (kq ), mtl::Arg ::B (vq ), mtl::Arg ::B (ksc ), mtl::Arg ::B (vsc ), mtl::Arg ::B (ids ), mtl::Arg ::B (part ), mtl::Arg ::V (a )},NCH ,QKV ,T ,256 ), 66D (pm , {mtl::Arg ::B (part ), mtl::Arg ::B (qfull ), mtl::Arg ::B (xa ), mtl::Arg ::V (a )},QH ,T ,1 ,256 )}; 67 std::vector < mtl::Dispatch > mm_ds = { 68D (pq , {mtl::Arg ::B (qcur ), mtl::Arg ::B (qh ), mtl::Arg ::V (nq )}, (nq + 255 ) /256 ,1 ,1 ,256 ), 69D (pmm , {mtl::Arg ::B (qh ), mtl::Arg ::B (kq ), mtl::Arg ::B (vq ), mtl::Arg ::B (ksc ), mtl::Arg ::B (vsc ), mtl::Arg ::B (ids ), mtl::Arg ::B (part ), mtl::Arg ::V (a )},NCH ,QKV ,T ,32 ), 70D (pm , {mtl::Arg ::B (part ), mtl::Arg ::B (qfull ), mtl::Arg ::B (xb ), mtl::Arg ::V (a )},QH ,T ,1 ,256 )}; 71const mtl::Dispatch fl = D (pf , {mtl::Arg ::B (qh ), mtl::Arg ::B (kq ), mtl::Arg ::B (vq ), mtl::Arg ::B (ksc ), mtl::Arg ::B (vsc ), 72 mtl::Arg ::B (ids ), mtl::Arg ::B (qfull ), mtl::Arg ::B (xf ), mtl::Arg ::V (a )},QKV ,T ,1 ,128 ); 73if (bench ) { 74auto timeit = [& ](const std::vector < mtl::Dispatch >& ds ) { 75cx .warm (ds ,0.3 ); 76 std::vector < double > t ; 77for (int i = 0 ;i < 5 ;++ i )t .push_back (cx .run (ds )); 78return median (t ); 79 }; 80 std::printf ("pos0 %5u, T %d: qsa_attn %.2f ms, qsa_attn_mm %.2f ms, merge %.2f ms; qsa_flash (no merge) %.2f ms\n" ,pos0 , 81T ,timeit ({ref_ds [0 ]})* 1e3 ,timeit ({mm_ds [1 ]})* 1e3 ,timeit ({ref_ds [1 ]})* 1e3 ,timeit ({fl })* 1e3 ); 82continue ; 83 } 84cx .run (ref_ds ); 85 std::fill (part .as < float > (),part .as < float > ()+ part .bytes /4 , std::nanf ("" ));// a kernel that writes nothing shows 86cx .run (mm_ds ); 87if (std::getenv ("QSA_DEBUG" )) { 88// how much of the partials the matrix-unit kernel wrote, and token 5's first chunk (m, l, acc[0..3]) 89size_t written = 0 ,nonfinite = 0 ; 90const float * pp = part .as < float > (); 91for (size_t i = 0 ;i < (size_t )T * NCH * QH * (QD + 2 );++ i ) { 92if (!std::isnan (pp [i ]))++ written ; 93if (!std::isfinite (pp [i ])&& !std::isnan (pp [i ]))++ nonfinite ; 94 } 95const float * q = pp + ((size_t )5 * NCH * QH )* (QD + 2 ); 96 std::printf ("debug pos0 %u: %zu of %zu partial floats written (%zu inf); t5 h0: m %g l %g acc %g %g %g %g\n" ,pos0 , 97written , (size_t )T * NCH * QH * (QD + 2 ),nonfinite ,q [QD ],q [QD + 1 ],q [0 ],q [1 ],q [2 ],q [3 ]); 98 } 99double se = 0 ,sr = 0 ; 100for (size_t i = 0 ;i < (size_t )T * QH * QD ;++ i ) { 101const double e = xb .as < float > ()[i ]- xa .as < float > ()[i ]; 102se += e * e ; 103sr += (double )xa .as < float > ()[i ]* xa .as < float > ()[i ]; 104 } 105const double rel = std::sqrt (se /sr ); 106const bool ok = rel < 3e-3 ; 107fails += !ok ; 108 std::printf ("pos0 %5u, T %d: qsa_attn_mm vs qsa_attn (after merge) rel L2 %.2e %s\n" ,pos0 ,T ,rel ,ok ?"PASS" :"FAIL" ); 109if (noflash )continue ; 110cx .run ({fl }); 111double sf = 0 ; 112for (size_t i = 0 ;i < (size_t )T * QH * QD ;++ i ) { 113const double e = ref::f16_to_f32 (xf .as < uint16_t > ()[i ])- xa .as < float > ()[i ]; 114sf += e * e ; 115 } 116const double relf = std::sqrt (sf /sr ); 117const bool okf = relf < 3e-3 ; 118fails += !okf ; 119 std::printf ("pos0 %5u, T %d: qsa_flash (fp16 out) vs qsa_attn + merge rel L2 %.2e %s\n" ,pos0 ,T ,relf ,okf ?"PASS" :"FAIL" ); 120 } 121// qsa_score_mm vs qsa_score at pos0 30000: the scores of every block <= n_bid 122 { 123const uint32_t pos0 = 30000 ,MB = MAXC /4 + 2 ,nb = (pos0 + T ) /4 + 1 ; 124 mtl::Buffer qidx = cx .alloc (4 * (size_t )T * 4 * 128 ),pooled = cx .alloc (4 * (size_t )MB * 128 ),dead = cx .alloc (4 * 128 ), 125sa = cx .alloc (4 * (size_t )T * MB ),sb = cx .alloc (4 * (size_t )T * MB ); 126for (size_t i = 0 ;i < (size_t )T * 4 * 128 ;++ i )qidx .as < float > ()[i ]= r .sym (); 127for (size_t i = 0 ;i < (size_t )MB * 128 ;++ i )pooled .as < float > ()[i ]= r .sym (); 128for (int i = 0 ;i < 128 ;++ i )dead .as < float > ()[i ]= r .sym (); 129QsaArgs a {pos0 ,0 , (uint32_t )MAXC , (uint32_t )T ,1e-6f }; 130 mtl::Pipeline ps = cx .pipeline (lib ,"qsa_score" ),psm = cx .pipeline (lib ,"qsa_score_mm" ); 131 mtl::Dispatch d1 ;d1 .p = & ps ;d1 .args = {mtl::Arg ::B (qidx ), mtl::Arg ::B (pooled ), mtl::Arg ::B (dead ), mtl::Arg ::B (sa ), mtl::Arg ::V (a )}; 132d1 .grid [0 ]= (nb + 7 ) /8 ;d1 .grid [1 ]= T ;d1 .tg [0 ]= 256 ; 133 mtl::Dispatch d2 ;d2 .p = & psm ;d2 .args = {mtl::Arg ::B (qidx ), mtl::Arg ::B (pooled ), mtl::Arg ::B (dead ), mtl::Arg ::B (sb ), mtl::Arg ::V (a )}; 134d2 .grid [0 ]= (nb + 63 ) /64 ;d2 .grid [1 ]= (T + 15 ) /16 ;d2 .tg [0 ]= 128 ; 135if (bench ) { 136auto timeit = [& ](const mtl::Dispatch & d ) { 137cx .warm ({d },0.3 ); 138 std::vector < double > t ; 139for (int i = 0 ;i < 5 ;++ i )t .push_back (cx .run ({d })); 140return median (t ); 141 }; 142 std::printf ("score pos0 %u, T %d: qsa_score %.2f ms, qsa_score_mm %.2f ms\n" ,pos0 ,T ,timeit (d1 )* 1e3 ,timeit (d2 )* 1e3 ); 143 }else { 144cx .run ({d1 }); 145cx .run ({d2 }); 146double worst = 0 ; 147for (int t = 0 ;t < T ;++ t ) { 148const uint32_t nbid = (pos0 + t + 1 ) /4 ; 149for (uint32_t b = 0 ;b <=nbid ;++ b ) { 150const double x = sa .as < float > ()[(size_t )t * MB + b ],y = sb .as < float > ()[(size_t )t * MB + b ]; 151worst = std::max (worst , std::fabs (x - y ) / (std::fabs (x )+ 1.0 ));// scores are O(10) 152 } 153 } 154const bool ok = worst < 1e-5 ; 155fails += !ok ; 156 std::printf ("score pos0 %u, T %d: qsa_score_mm vs qsa_score worst rel %.2e %s\n" ,pos0 ,T ,worst ,ok ?"PASS" :"FAIL" ); 157 } 158 } 159if (!bench ) std::printf ("%s\n" ,fails ?"SOME FAILED" :"ALL PASS" ); 160return fails ?1 :0 ; 161}