okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/tests/gr_parity.cpp - the hyper-connection read (gr.metal) against a double-precision CPU reference 2// (fused_gr.hpp's formulas), and its speed. 3// 4// gr_parity [--ntok N] parity for N = 1, 3, 8 (or N), with and without the folded write 5// gr_parity --bench [--ntok N] per-kernel GPU time, 8 weight sets rotated (no cache reuse across calls) 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 <cstring> 15#include <string> 16#include <vector> 17 18namespace { 19constexpr int N = strata_metal::geo::N ,HC = strata_metal::geo::HC ,LR = strata_metal::geo::LR ,NIN = HC * N ;// the shape this build is compiled for (engine/geo.h) 20struct GrArgs {uint32_t n_embd ,hc ,hc_lr ,n_tok ,apply ;float eps ; }; 21double median (std::vector < double > v ) { std::sort (v .begin (),v .end ());return v [v .size () /2 ]; } 22struct Err { 23double se = 0 ,sr = 0 ,mx = 0 ,mr = 0 ; 24void add (double g ,double r ) {se += (g - r )* (g - r );sr += r * r ;mx = std::max (mx , std::fabs (g - r ));mr = std::max (mr , std::fabs (r )); } 25double l2 ()const {return sr > 0 ? std::sqrt (se /sr ) : std::sqrt (se ); } 26}; 27}// namespace 28 29int main (int argc ,char ** argv ) { 30bool bench = false; 31 std::vector < int > ntoks = {1 ,3 ,8 }; 32for (int i = 1 ;i < argc ;++ i ) { 33 std::string a = argv [i ]; 34if (a == "--bench" )bench = true; 35else if (a == "--ntok" )ntoks = {std::atoi (argv [++ i ])}; 36 } 37 mtl::Context cx ; 38const int lib = cx .compile ({"quant.metal" ,"units.metal" ,"gr.metal" }, strata_metal::geo::prelude ()); 39 std::printf ("device %s\n" ,cx .device_name ().c_str ()); 40const int SETS = bench ?48 :1 ; 41const bool q8 = std::getenv ("GR_Q8" )!= nullptr ;// the Q8_0 form of the HC matrices (gr_down_q8_*, gr_up_q8) 42const int wty = q8 ? ref::Q8_0 : ref::BF16 ; 43// GR_INJ_F32 (with GR_Q8): the inject rows as F32 (the Unsloth files), read by gr_down_q8f_* 44const bool injf = q8 && std::getenv ("GR_INJ_F32" )!= nullptr ; 45const size_t wd_bytes = (size_t )LR * NIN * 2 ,wi_bytes = (size_t )HC * NIN * 4 ,wu_bytes = (size_t )NIN * LR * 2 ; 46const size_t set_bytes = wd_bytes + wi_bytes + wu_bytes + NIN * 4 ; 47 mtl::Buffer W = cx .alloc (set_bytes * SETS ); 48 ref::Rng rng (3 ); 49for (int s = 0 ;s < SETS ;++ s ) { 50uint8_t * b = W .as < uint8_t > ()+ s * set_bytes ; 51 ref::random_blocks (ref::BF16 ,b ,LR ,NIN ,11 + s ,0.02f ); 52 ref::random_blocks (ref::BF16 ,b + wd_bytes ,HC ,NIN ,22 + s ,0.02f ); 53 ref::random_blocks (ref::BF16 ,b + wd_bytes + wi_bytes ,NIN ,LR ,33 + s ,0.1f ); 54float * wn = (float * )(b + wd_bytes + wi_bytes + wu_bytes ); 55for (int i = 0 ;i < NIN ;++ i )wn [i ]= 1.0f + 0.1f * rng .sym (); 56if (q8 ) { 57// GR_Q8: the three matrices as Q8_0 (quantized from the BF16 values), each at its BF16 offset (it fits) 58const size_t offs [3 ]= {0 ,wd_bytes ,wd_bytes + wi_bytes }; 59const size_t ns [3 ]= {(size_t )LR * NIN , (size_t )HC * NIN , (size_t )NIN * LR }; 60for (int m = 0 ;m < 3 ;++ m ) { 61 std::vector < float > f (ns [m ]); 62 ref::dequant (ref::BF16 ,b + offs [m ],f .data (),ns [m ]); 63if (m == 1 && injf ) std::memcpy (b + offs [m ],f .data (),4 * ns [m ]); 64else ref::quantize_q8_0 (f .data (),ns [m ],b + offs [m ]); 65 } 66 } 67 } 68const int MAXT = 8 ; 69// GR_DOWN=SG_KS selects gr_down's variant (default 8_8: 8 simdgroups split one row) 70const std::string grd = std::getenv ("GR_DOWN" ) ? std::getenv ("GR_DOWN" ) :"8_8" ; 71int grd_sg = 8 ,grd_ks = 8 ; 72 std::sscanf (grd .c_str (),"%d_%d" ,& grd_sg ,& grd_ks ); 73const int grd_rs = grd_sg /grd_ks ; 74 mtl::Buffer R = cx .alloc (4 * MAXT * NIN ),R0 = cx .alloc (4 * MAXT * NIN ),bo = cx .alloc (4 * MAXT * N ), 75inj = cx .alloc (4 * MAXT * HC ),xn = cx .alloc (4 * MAXT * NIN ),lo = cx .alloc (4 * MAXT * LR ), 76injo = cx .alloc (4 * MAXT * HC ),xp = cx .alloc (4 * (4 * MAXT * N + MAXT * N /16 )), 77xr = cx .alloc (4 * (4 * MAXT * N + MAXT * N /16 )); 78for (int i = 0 ;i < MAXT * NIN ;++ i )R0 .as < float > ()[i ]= rng .sym ()* 2.0f ; 79for (int i = 0 ;i < MAXT * N ;++ i )bo .as < float > ()[i ]= rng .sym (); 80for (int i = 0 ;i < MAXT * HC ;++ i )inj .as < float > ()[i ]= rng .sym ()* 3.0f ; 81 mtl::Pipeline pprep = cx .pipeline (lib ,"xprep" ); 82int fails = 0 ; 83for (int nt :ntoks ) { 84 mtl::Pipeline pn = cx .pipeline (lib ,"gr_norm" ), 85pd = cx .pipeline (lib , std::string (injf ?"gr_down_q8f_" :q8 ?"gr_down_q8_" :"gr_down_" )+ grd , mtl::Constants ().i (1 ,nt )), 86pu = cx .pipeline (lib ,q8 ?"gr_up_q8" :"gr_up" , mtl::Constants ().i (1 ,nt )); 87for (int apply = 0 ;apply < 2 ;++ apply ) { 88GrArgs a {N ,HC ,LR , (uint32_t )nt , (uint32_t )apply ,1e-6f }; 89 std::memcpy (R .ptr ,R0 .ptr ,R .bytes ); 90auto enc = [& ](int set , std::vector < mtl::Dispatch >& ds ,int which ) { 91const size_t o = (size_t )set * set_bytes ; 92 mtl::Dispatch d ; 93if (which & 1 ) { 94d .p = & pn ; 95d .args = {mtl::Arg ::B (R ), mtl::Arg ::B (bo ), mtl::Arg ::B (inj ), mtl::Arg ::B (W ,o + wd_bytes + wi_bytes + wu_bytes ), 96 mtl::Arg ::B (xn ), mtl::Arg ::V (a )}; 97d .grid [0 ]= HC ; 98d .grid [1 ]= nt ; 99d .tg [0 ]= 256 ; 100ds .push_back (d ); 101 } 102if (which & 2 ) { 103 mtl::Dispatch e ; 104e .p = & pd ; 105e .args = {mtl::Arg ::B (W ,o ), mtl::Arg ::B (W ,o + wd_bytes ), mtl::Arg ::B (xn ), mtl::Arg ::B (lo ), 106 mtl::Arg ::B (injo ), mtl::Arg ::V (a )}; 107e .grid [0 ]= (LR + HC + grd_rs - 1 ) /grd_rs ;// RS = SG / KS rows per threadgroup 108e .tg [0 ]= 32 * grd_sg ; 109ds .push_back (e ); 110 } 111if (which & 4 ) { 112 mtl::Dispatch f ; 113f .p = & pu ; 114f .args = {mtl::Arg ::B (W ,o + wd_bytes + wi_bytes ), mtl::Arg ::B (lo ), mtl::Arg ::B (xn ), mtl::Arg ::B (xp ), 115 mtl::Arg ::V (a )}; 116f .grid [0 ]= N /16 ; 117f .tg [0 ]= 256 ; 118ds .push_back (f ); 119 } 120 }; 121if (!bench ) { 122 std::vector < mtl::Dispatch > ds ; 123enc (0 ,ds ,7 ); 124cx .run (ds ); 125// reference 126const uint8_t * b = W .as < uint8_t > (); 127 std::vector < float > wd ((size_t )LR * NIN ),wi ((size_t )HC * NIN ),wu ((size_t )NIN * LR ); 128 ref::dequant (wty ,b ,wd .data (),wd .size ()); 129 ref::dequant (injf ? ref::F32 :wty ,b + wd_bytes ,wi .data (),wi .size ()); 130 ref::dequant (wty ,b + wd_bytes + wi_bytes ,wu .data (),wu .size ()); 131const float * wn = (const float * )(b + wd_bytes + wi_bytes + wu_bytes ); 132Err eR ,elo ,einj ,emix ; 133for (int t = 0 ;t < nt ;++ t ) { 134 std::vector < double > r (NIN ),x (NIN ),l (LR ); 135for (int c = 0 ;c < HC ;++ c ) { 136const double g = apply ?2.0 / (1.0 + std::exp (- (double )inj .as < float > ()[t * HC + c ] /HC )) :0.0 ; 137double ss = 0 ; 138for (int d = 0 ;d < N ;++ d ) { 139double v = R0 .as < float > ()[t * NIN + c * N + d ]; 140if (apply )v += bo .as < float > ()[t * N + d ]* g ; 141r [c * N + d ]= v ; 142ss += v * v ; 143 } 144const double rs = 1.0 / std::sqrt (ss /N + 1e-6 ); 145for (int d = 0 ;d < N ;++ d )x [c * N + d ]= r [c * N + d ]* wn [c * N + d ]* rs ; 146 } 147for (int i = 0 ;i < NIN ;++ i )eR .add (R .as < float > ()[t * NIN + i ],r [i ]); 148for (int k = 0 ;k < LR ;++ k ) { 149double s = 0 ; 150for (int i = 0 ;i < NIN ;++ i )s += (double )wd [(size_t )k * NIN + i ]* x [i ]; 151const double z = s /HC ; 152l [k ]= z / (1.0 + std::exp (- z )); 153elo .add (lo .as < float > ()[t * LR + k ],l [k ]); 154 } 155for (int c = 0 ;c < HC ;++ c ) { 156double s = 0 ; 157for (int i = 0 ;i < NIN ;++ i )s += (double )wi [(size_t )c * NIN + i ]* x [i ]; 158einj .add (injo .as < float > ()[t * HC + c ],s ); 159 } 160for (int d = 0 ;d < N ;++ d ) { 161double m = 0 ; 162for (int c = 0 ;c < HC ;++ c ) { 163double s = 0 ; 164// the up projection reads the GPU's lo, so the check isolates gr_up 165for (int k = 0 ;k < LR ;++ k )s += (double )wu [(size_t )(c * N + d )* LR + k ]* lo .as < float > ()[t * LR + k ]; 166m += x [c * N + d ] / (1.0 + std::exp (- s )); 167 } 168emix .add (xp .as < float > ()[t * N + d ],m /HC ); 169 } 170 } 171// the prepared planes must be bitwise xprep's of the raw plane 172 { 173 mtl::Dispatch d ; 174d .p = & pprep ; 175const uint32_t n = N ,ntt = nt ; 176d .args = {mtl::Arg ::B (xp ), mtl::Arg ::B (xr ), mtl::Arg ::V (n ), mtl::Arg ::V (ntt )}; 177d .tg [0 ]= 256 ; 178d .grid [0 ]= (N /16 * nt + 255 ) /256 ; 179cx .run ({d }); 180 } 181const size_t elems = (size_t )4 * nt * N + (size_t )nt * N /16 ; 182const bool planes = std::memcmp (xp .ptr ,xr .ptr ,4 * elems )== 0 ; 183const bool ok = eR .l2 ()< 1e-6 && elo .l2 ()< 1e-5 && einj .l2 ()< 1e-5 && emix .l2 ()< 1e-5 && planes ; 184fails += !ok ; 185 std::printf ("parity ntok %d apply %d: R rel L2 %.2e, lo %.2e, inject %.2e, mixed %.2e, prepared planes " 186"== xprep: %s %s\n" ,nt ,apply ,eR .l2 (),elo .l2 (),einj .l2 (),emix .l2 (), 187planes ?"bitwise" :"NO" ,ok ?"PASS" :"FAIL" ); 188 }else { 189for (int which : {1 ,2 ,4 ,7 }) { 190 std::vector < mtl::Dispatch > ds ; 191for (int r = 0 ;r < 96 ;++ r )enc (r %SETS ,ds ,which ); 192cx .warm (ds ,0.1 ); 193 std::vector < double > ts ; 194for (int i = 0 ;i < 7 ;++ i )ts .push_back (cx .run (ds ) /96 ); 195const double m = median (ts ); 196const double bytes = which == 1 ?2.0 * nt * NIN * 4 + NIN * 4 + nt * NIN * 4 197 :which == 2 ?wd_bytes + wi_bytes 198 :which == 4 ?wu_bytes 199 :wd_bytes + wi_bytes + wu_bytes ; 200 std::printf ("bench ntok %d apply %d %-8s %7.2f us %6.1f GB/s (%.2f MB)\n" ,nt ,apply , 201which == 1 ?"norm" :which == 2 ?"down" :which == 4 ?"up" :"all 3" ,m * 1e6 , 202bytes /m /1e9 ,bytes /1e6 ); 203 } 204 } 205 } 206 } 207if (!bench ) std::printf ("%s\n" ,fails ?"SOME FAILED" :"ALL PASS" ); 208return fails ?1 :0 ; 209}