okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3a76d5eaf
main
1// metal/tests/gdn_layer_bench.cpp - one Flash-Next GDN layer's prompt cost, piece by piece (30 Oct GDN-M phase 2). 2// Real shapes (N 2560, qkv 10240, z 6144, alpha/beta 48, out 2560 x 6144), a chunk of T rows (the engine's prompt chunk is 3// 2048): dq (Q6_K -> fp16 weights), the four input GEMMs separate and fused into one, tile choices, gdn_pre, the scan, gdn_post, 4// the out GEMM; each alone (GPU time) and the layer in order, plus the TFLOP/s of every GEMM. Random weights: a timing 5// of the byte and FLOP stream, not a numerical check. 6// gdn_layer_bench [--T 2048] [--reps 7] [--scan gdn_scanr_4] 7#include "mtl_rt.h" 8#include "ref_quant.h" 9 10#include <algorithm> 11#include <cstdio> 12#include <cstdlib> 13#include <cstring> 14#include <string> 15#include <vector> 16 17namespace { 18constexpr int N = 2560 ,HK = 16 ,HV = 48 ,CH = 10240 ,ZD = 6144 ,S = 128 ,TQ6K = 14 ; 19struct GemmArgs {int M ,N ,K ,pad ; }; 20struct DqArgs {uint32_t n_in ,n_out ,row_bytes ,perm ; }; 21struct GdnPArgs {uint32_t n_tok ,h_k ,h_v ,channels ;float eps ;uint32_t f32 ; }; 22double median (std::vector < double > v ) { std::sort (v .begin (),v .end ());return v [v .size () /2 ]; } 23}// namespace 24 25int main (int argc ,char ** argv ) { 26int T = 2048 ,reps = 7 ; 27 std::string scan_name = "gdn_scanr_4" ; 28for (int i = 1 ;i < argc ;++ i ) { 29 std::string a = argv [i ]; 30if (a == "--T" )T = std::atoi (argv [++ i ]); 31else if (a == "--reps" )reps = std::atoi (argv [++ i ]); 32else if (a == "--scan" )scan_name = argv [++ i ]; 33 } 34 mtl::Context cx ; 35const int lib = cx .compile ({"quant.metal" ,"units.metal" ,"mma.metal" ,"dense_mm.metal" }); 36const int lgdn = cx .compile ({"gdn.metal" ,"gdn_prompt.metal" },"#define NO_GGML_COMMON 1\n" ); 37 std::printf ("device %s, chunk T %d, scan %s\n" ,cx .device_name ().c_str (),T ,scan_name .c_str ()); 38 ref::Rng r (5 ); 39auto fill_half = [& ](mtl::Buffer & b ,size_t n ,float sc ) { 40for (size_t i = 0 ;i < n ;++ i )b .as < uint16_t > ()[i ]= ref::f32_to_f16 (r .sym ()* sc ); 41 }; 42const int rows [5 ]= {CH ,ZD ,48 ,48 ,N };// qkv, z, alpha, beta, out 43const int kin [5 ]= {N ,N ,N ,N ,ZD }; 44 mtl::Buffer mixh = cx .alloc (2 * (size_t )T * N ),ygh = cx .alloc (2 * (size_t )T * ZD ); 45fill_half (mixh , (size_t )T * N ,1.0f ); 46fill_half (ygh , (size_t )T * ZD ,1.0f ); 47 mtl::Buffer Wq [5 ],Wh [5 ],Y [5 ]; 48 mtl::Buffer qsrc [5 ]; 49for (int i = 0 ;i < 5 ;++ i ) { 50Wh [i ]= cx .alloc (2 * (size_t )rows [i ]* kin [i ]); 51fill_half (Wh [i ], (size_t )rows [i ]* kin [i ],0.03f ); 52Y [i ]= cx .alloc (4 * (size_t )T * rows [i ]); 53const size_t rb = ref::row_bytes (TQ6K ,kin [i ]); 54qsrc [i ]= cx .alloc (rb * rows [i ]); 55for (size_t j = 0 ;j < rb * rows [i ];++ j )qsrc [i ].as < uint8_t > ()[j ]= (uint8_t )(r .uni ()* 255.0f ); 56// keep the fp16 scale of each Q6_K block sane (the last two bytes of a 210 byte block) 57for (size_t j = 0 ;j + 210 <=rb * rows [i ];j += 210 ) {qsrc [i ].as < uint16_t > ()[(j + 208 ) /2 ]= ref::f32_to_f16 (0.01f ); } 58 } 59const int NF = CH + ZD + 48 + 48 ; 60 mtl::Buffer WF = cx .alloc (2 * (size_t )NF * N ),YF = cx .alloc (4 * (size_t )T * NF ); 61fill_half (WF , (size_t )NF * N ,0.03f ); 62 63auto timeit = [& ](const std::vector < mtl::Dispatch >& ds ) { 64cx .warm (ds ,0.3 ); 65 std::vector < double > t ; 66for (int i = 0 ;i < reps ;++ i )t .push_back (cx .run (ds )); 67return median (t )* 1e3 ; 68 }; 69auto dq_d = [& ](int i ) { 70DqArgs a {(uint32_t )kin [i ], (uint32_t )rows [i ], (uint32_t )ref::row_bytes (TQ6K ,kin [i ]),0 }; 71static mtl::Pipeline p = cx .pipeline (lib ,"dq_f16_14" ); 72 mtl::Dispatch d ;d .p = & p ; 73d .args = {mtl::Arg ::B (qsrc [i ]), mtl::Arg ::B (Wh [i ]), mtl::Arg ::V (a )}; 74d .grid [0 ]= (kin [i ] /32 + 63 ) /64 ;d .grid [1 ]= rows [i ];d .tg [0 ]= 64 ; 75return d ; 76 }; 77auto gemm_d = [& ](const mtl::Buffer & A ,const mtl::Buffer & B ,const mtl::Buffer & Yb ,int M ,int Nn ,int K ,int tm ,int tn ,int sgn = 4 ) { 78static std::vector < std::pair < std::string , mtl::Pipeline *>> cache ; 79const std::string fn = "gemm_f16_" + std::to_string (tm )+ "_" + std::to_string (tn )+ "_" + std::to_string (sgn ); 80 mtl::Pipeline * p = nullptr ; 81for (auto & c :cache )if (c .first == fn )p = c .second ; 82if (!p ) {p = new mtl::Pipeline (cx .pipeline (lib ,fn ));cache .push_back ({fn ,p }); } 83GemmArgs g {M ,Nn ,K ,0 }; 84 mtl::Dispatch d ;d .p = p ; 85d .args = {mtl::Arg ::B (A ), mtl::Arg ::B (B ), mtl::Arg ::B (Yb ), mtl::Arg ::V (g )}; 86d .grid [0 ]= (Nn + tn - 1 ) /tn ;d .grid [1 ]= (M + tm - 1 ) /tm ;d .tg [0 ]= 32 * sgn ; 87return d ; 88 }; 89auto tfl = [& ](int Nn ,int K ,double ms ) {return 2.0 * T * Nn * K / (ms * 1e-3 ) /1e12 ; }; 90 91 std::printf ("== dq (Q6_K -> fp16), ms: qkv %.3f z %.3f alpha %.3f beta %.3f out %.3f\n" ,timeit ({dq_d (0 )}),timeit ({dq_d (1 )}), 92timeit ({dq_d (2 )}),timeit ({dq_d (3 )}),timeit ({dq_d (4 )})); 93 std::printf ("== GEMMs (tile TMxTN, 4 simdgroups): ms and TFLOP/s\n" ); 94struct Sh {const char * nm ;int i ; }; 95struct Tl {int tm ,tn ,sg ; }; 96const std::vector < Tl > tiles = {{64 ,32 ,4 }, {64 ,64 ,4 }, {128 ,64 ,4 }, {64 ,128 ,4 }, {128 ,128 ,4 }, {128 ,128 ,8 }, {128 ,64 ,8 }, 97 {64 ,64 ,8 }, {256 ,64 ,8 }, {64 ,256 ,8 }, {128 ,32 ,4 }, {32 ,128 ,4 }}; 98for (Sh s : {Sh {"qkv" ,0 },Sh {"z" ,1 },Sh {"alpha" ,2 },Sh {"out" ,4 }}) { 99 std::printf ("%-6s N %5d K %4d:" ,s .nm ,rows [s .i ],kin [s .i ]); 100const mtl::Buffer & A = s .i == 4 ?ygh :mixh ; 101for (const Tl & tl :tiles ) { 102if (rows [s .i ] <=64 && tl .tn > 32 )continue ; 103const double ms = timeit ({gemm_d (A ,Wh [s .i ],Y [s .i ],T ,rows [s .i ],kin [s .i ],tl .tm ,tl .tn ,tl .sg )}); 104 std::printf (" %dx%d/%d %.3f (%.0f)" ,tl .tm ,tl .tn ,tl .sg ,ms ,tfl (rows [s .i ],kin [s .i ],ms )); 105 } 106 std::printf ("\n" ); 107 } 108 std::printf ("fused qkv+z+alpha+beta N %d:" ,NF ); 109double best_f = 1e9 ; 110for (const Tl & tl :tiles ) { 111const double ms = timeit ({gemm_d (mixh ,WF ,YF ,T ,NF ,N ,tl .tm ,tl .tn ,tl .sg )}); 112best_f = std::min (best_f ,ms ); 113 std::printf (" %dx%d/%d %.3f (%.0f)" ,tl .tm ,tl .tn ,tl .sg ,ms ,tfl (NF ,N ,ms )); 114 } 115 std::printf ("\n" ); 116// the four input GEMMs as the engine runs them, in order 117const double sep = timeit ({gemm_d (mixh ,Wh [0 ],Y [0 ],T ,CH ,N ,64 ,64 ),gemm_d (mixh ,Wh [1 ],Y [1 ],T ,ZD ,N ,64 ,64 ), 118gemm_d (mixh ,Wh [2 ],Y [2 ],T ,48 ,N ,64 ,32 ),gemm_d (mixh ,Wh [3 ],Y [3 ],T ,48 ,N ,64 ,32 )}); 119 std::printf ("== input projections: separate (engine tiles) %.3f ms, fused best %.3f ms\n" ,sep ,best_f ); 120 121// pre / scan / post 122 mtl::Buffer qkv = Y [0 ],z = Y [1 ],al = Y [2 ],be = Y [3 ],cw = cx .alloc (4 * CH * 4 ),dt = cx .alloc (4 * HV ),sa = cx .alloc (4 * HV ), 123gam = cx .alloc (4 * S ),st = cx .alloc (4 * (size_t )S * HV * S ),h0 = cx .alloc (4 * CH * 3 ),h1 = cx .alloc (4 * CH * 3 ), 124qn = cx .alloc (4 * (size_t )T * HK * S ),kn = cx .alloc (4 * (size_t )T * HK * S ),vc = cx .alloc (4 * (size_t )T * HV * S ), 125gb = cx .alloc (8 * (size_t )T * HV ),kq = cx .alloc (4 * (size_t )T * HK ),o = cx .alloc (4 * (size_t )T * HV * S ), 126yh = cx .alloc (2 * (size_t )T * HV * S ),y32 = cx .alloc (4 ); 127for (int i = 0 ;i < CH * 4 ;++ i )cw .as < float > ()[i ]= r .sym ()* 0.6f ; 128for (int i = 0 ;i < HV ;++ i ) {dt .as < float > ()[i ]= r .sym ();sa .as < float > ()[i ]= -0.2f - r .uni ()* 2.0f ; } 129for (int i = 0 ;i < S ;++ i )gam .as < float > ()[i ]= 0.5f + r .uni (); 130for (size_t i = 0 ;i < (size_t )T * CH ;++ i )qkv .as < float > ()[i ]= r .sym ()* 2.0f ; 131for (size_t i = 0 ;i < (size_t )T * HV ;++ i ) {al .as < float > ()[i ]= r .sym ()* 3.0f ;be .as < float > ()[i ]= r .sym ()* 2.0f ; } 132for (size_t i = 0 ;i < (size_t )T * ZD ;++ i )z .as < float > ()[i ]= r .sym ()* 2.0f ; 133 mtl::Pipeline pre = cx .pipeline (lgdn ,"gdn_pre" ),post = cx .pipeline (lgdn ,"gdn_post" ), 134scan = cx .pipeline (lgdn ,scan_name , mtl::Constants ().i (6 ,0 )); 135GdnPArgs ga {(uint32_t )T ,HK ,HV ,CH ,1e-6f ,0 }; 136 mtl::Dispatch dpre ;dpre .p = & pre ; 137dpre .args = {mtl::Arg ::B (qkv ), mtl::Arg ::B (h0 ), mtl::Arg ::B (cw ), mtl::Arg ::B (al ), mtl::Arg ::B (be ), mtl::Arg ::B (dt ), mtl::Arg ::B (sa ), 138 mtl::Arg ::B (qn ), mtl::Arg ::B (kn ), mtl::Arg ::B (vc ), mtl::Arg ::B (gb ), mtl::Arg ::B (kq ), mtl::Arg ::B (h1 ), mtl::Arg ::V (ga )}; 139dpre .grid [0 ]= HK + HV /2 ;dpre .grid [1 ]= T ;dpre .tg [0 ]= 256 ; 140 mtl::Dispatch dscan ;dscan .p = & scan ; 141dscan .args = {mtl::Arg ::B (st ), mtl::Arg ::B (qn ), mtl::Arg ::B (kn ), mtl::Arg ::B (vc ), mtl::Arg ::B (gb ), mtl::Arg ::B (kq ), mtl::Arg ::B (o ), 142 mtl::Arg ::V (ga )}; 143dscan .grid [0 ]= HV * 4 ;dscan .tg [0 ]= 128 ; 144 mtl::Dispatch dpost ;dpost .p = & post ; 145dpost .args = {mtl::Arg ::B (o ), mtl::Arg ::B (z ), mtl::Arg ::B (gam ), mtl::Arg ::B (yh ), mtl::Arg ::B (y32 ), mtl::Arg ::V (ga )}; 146dpost .grid [0 ]= HV ;dpost .grid [1 ]= T ;dpost .tg [0 ]= 128 ; 147const double tpre = timeit ({dpre }),tscan = timeit ({dscan }),tpost = timeit ({dpost }); 148 std::printf ("== gdn_pre %.3f, scan %.3f, gdn_post %.3f ms (stage %.3f)\n" ,tpre ,tscan ,tpost ,tpre + tscan + tpost ); 149double tF = 0 ; 150 {// the fused-stride kernels (function constant 7) against the separate ones: bit for bit 151for (int t = 0 ;t < T ;++ t ) { 152 std::memcpy (YF .as < float > ()+ (size_t )t * NF ,qkv .as < float > ()+ (size_t )t * CH ,4 * CH ); 153 std::memcpy (YF .as < float > ()+ (size_t )t * NF + CH ,z .as < float > ()+ (size_t )t * ZD ,4 * ZD ); 154 std::memcpy (YF .as < float > ()+ (size_t )t * NF + CH + ZD ,al .as < float > ()+ (size_t )t * 48 ,4 * 48 ); 155 std::memcpy (YF .as < float > ()+ (size_t )t * NF + CH + ZD + 48 ,be .as < float > ()+ (size_t )t * 48 ,4 * 48 ); 156 } 157 std::memset (h1 .ptr ,0 ,4 * CH * 3 ); 158cx .run ({dpre ,dscan ,dpost }); 159 std::vector < char > rqn ((char * )qn .ptr , (char * )qn .ptr + 4 * (size_t )T * HK * S ),rvc ((char * )vc .ptr , (char * )vc .ptr + 4 * (size_t )T * HV * S ), 160rgb ((char * )gb .ptr , (char * )gb .ptr + 8 * (size_t )T * HV ),ryh ((char * )yh .ptr , (char * )yh .ptr + 2 * (size_t )T * HV * S ), 161rh ((char * )h1 .ptr , (char * )h1 .ptr + 4 * CH * 3 ); 162 mtl::Pipeline preF = cx .pipeline (lgdn ,"gdn_pre" , mtl::Constants ().i (7 ,NF )),postF = cx .pipeline (lgdn ,"gdn_post" , mtl::Constants ().i (7 ,NF )); 163 mtl::Dispatch fpre = dpre ,fpost = dpost ; 164fpre .p = & preF ;fpost .p = & postF ; 165fpre .args [0 ]= mtl::Arg ::B (YF ,0 ); 166fpre .args [3 ]= mtl::Arg ::B (YF ,4 * (size_t )(CH + ZD )); 167fpre .args [4 ]= mtl::Arg ::B (YF ,4 * (size_t )(CH + ZD + 48 )); 168fpost .args [1 ]= mtl::Arg ::B (YF ,4 * (size_t )CH ); 169 std::memset (qn .ptr ,0 ,rqn .size ()); std::memset (vc .ptr ,0 ,rvc .size ()); std::memset (gb .ptr ,0 ,rgb .size ()); 170 std::memset (yh .ptr ,0 ,ryh .size ()); std::memset (h1 .ptr ,0 ,rh .size ()); 171cx .run ({fpre ,dscan ,fpost }); 172 std::printf ("fused-stride gdn_pre/gdn_post vs separate: qn %s, vc %s, gate/beta %s, history %s, fp16 y %s\n" , 173 std::memcmp (qn .ptr ,rqn .data (),rqn .size ())== 0 ?"equal" :"DIFFER" , std::memcmp (vc .ptr ,rvc .data (),rvc .size ())== 0 ?"equal" :"DIFFER" , 174 std::memcmp (gb .ptr ,rgb .data (),rgb .size ())== 0 ?"equal" :"DIFFER" , std::memcmp (h1 .ptr ,rh .data (),rh .size ())== 0 ?"equal" :"DIFFER" , 175 std::memcmp (yh .ptr ,ryh .data (),ryh .size ())== 0 ?"equal" :"DIFFER" ); 176tF = timeit ({fpre })+ timeit ({fpost }); 177 std::printf ("fused-stride pre + post %.3f ms (separate %.3f)\n" ,tF ,tpre + tpost ); 178 } 179const double tout = timeit ({gemm_d (ygh ,Wh [4 ],Y [4 ],T ,N ,ZD ,64 ,64 )}); 180double dqall = 0 ; 181for (int i = 0 ;i < 5 ;++ i )dqall += timeit ({dq_d (i )}); 182 std::printf ("== layer in engine order (GPU ms): dq %.3f + input GEMMs %.3f + pre/scan/post %.3f + out GEMM %.3f = %.3f (sum of parts)\n" , 183dqall ,sep ,tpre + tscan + tpost ,tout ,dqall + sep + tpre + tscan + tpost + tout ); 184 {// the layer as the engine now runs it on the M5 family: fused input GEMM, fused-stride pre/post, tiled out GEMM 185double tout_best = 1e9 ; 186for (auto [tm ,tn ] : std::vector < std::pair < int ,int >> {{64 ,64 }, {128 ,64 }, {64 ,128 }}) 187tout_best = std::min (tout_best ,timeit ({gemm_d (ygh ,Wh [4 ],Y [4 ],T ,N ,ZD ,tm ,tn )})); 188const double base_l = dqall + sep + tpre + tscan + tpost + tout ; 189const double new_l = dqall + best_f + tF + tscan + tout_best ; 190 std::printf ("== layer: before %.3f ms, after (fused input GEMM %.3f, fused-stride pre+post %.3f, scan %.3f, out GEMM %.3f) %.3f ms = x%.3f\n" , 191base_l ,best_f ,tF ,tscan ,tout_best ,new_l ,base_l /new_l ); 192 } 193return 0 ; 194}