okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1// metal/tests/fuse_parity.cpp - the decode latency fusions against the kernels they replace, BITWISE, for 1..4 2// tokens: 3// gemv_cat (gemv.metal) ssm_alpha + ssm_beta (BF16 2560 -> 48 + 48) in one dispatch == two gemv dispatches 4// router_route the router GEMV + moe_route in one dispatch == gemv + moe_route (logits, ids, w, plan; 5// twice in a row: the counter resets) 6// gr_norm_comb (gr2.metal) the MoE combine folded into the next gr_norm == moe_combine_sh + gr_norm (R', xn) 7#include "mtl_rt.h" 8#include "ref_quant.h" 9 10#include <cstdio> 11#include <cstring> 12#include <string> 13#include <vector> 14 15namespace { 16struct GemvArgs {uint32_t n_in ,n_out ,n_tok ,row_bytes ,x_stride ,y_stride ; }; 17struct MoeArgs { 18uint64_t gate_off ,up_off ,down_off ,gu_stride ,d_stride ; 19uint32_t gu_row ,d_row ,n_embd ,n_ff ,n_tok ,topk ,n_expert ,gx_gu ,gx_dn ,pad ; 20}; 21constexpr int P_INTS = 16 + 128 + 128 + 4 + 128 * 3 ,P_TOKENT = 16 + 128 + 128 + 4 + 128 * 2 ; 22struct GrArgs {uint32_t n_embd ,hc ,hc_lr ,n_tok ,apply ;float eps ; }; 23struct GlueArgs {uint32_t n ,n_tok ,n_embd ,topk ; }; 24}// namespace 25 26int main () { 27 mtl::Context cx ; 28const int lib = cx .compile ({"quant.metal" ,"units.metal" ,"moe.metal" ,"gemv.metal" ,"router.metal" ,"gr.metal" , 29"gr2.metal" ,"glue.metal" }); 30 std::printf ("device %s\n" ,cx .device_name ().c_str ()); 31int fails = 0 ; 32const int N = 2560 ,NO = 48 ,MAXT = 8 ; 33const size_t rb = ref::row_bytes (ref::BF16 ,N ); 34 mtl::Buffer Wa = cx .alloc (rb * NO ),Wb = cx .alloc (rb * NO ),xp = cx .alloc (4 * ((size_t )4 * MAXT * N + MAXT * N /16 )), 35y1 = cx .alloc (4 * MAXT * NO ),y2 = cx .alloc (4 * MAXT * NO ),z1 = cx .alloc (4 * MAXT * NO ),z2 = cx .alloc (4 * MAXT * NO ); 36 ref::random_blocks (ref::BF16 ,Wa .as < uint8_t > (),NO ,N ,1 ,0.05f ); 37 ref::random_blocks (ref::BF16 ,Wb .as < uint8_t > (),NO ,N ,2 ,0.05f ); 38 ref::Rng r (3 ); 39for (size_t i = 0 ;i < xp .bytes /4 ;++ i )xp .as < float > ()[i ]= r .sym (); 40for (int nt = 1 ;nt <=4 ;++ nt ) { 41 mtl::Pipeline pg = cx .pipeline (lib ,"gemv_30_32_1_4_1" , mtl::Constants ().i (0 ,nt )), 42pc = cx .pipeline (lib ,"gemvc_30_32_1_4" , mtl::Constants ().i (0 ,nt )); 43const size_t s16 = (size_t )4 * 4 * MAXT * N ; 44GemvArgs a {(uint32_t )N , (uint32_t )NO , (uint32_t )nt , (uint32_t )rb , (uint32_t )N , (uint32_t )NO }; 45auto g = [& ](const mtl::Buffer & W ,const mtl::Buffer & y ) { 46 mtl::Dispatch d ; 47d .p = & pg ; 48d .args = {mtl::Arg ::B (W ), mtl::Arg ::B (xp ), mtl::Arg ::B (xp ,s16 ), mtl::Arg ::B (y ), mtl::Arg ::V (a )}; 49d .grid [0 ]= NO /4 ; 50d .tg [0 ]= 128 ; 51return d ; 52 }; 53cx .run ({g (Wa ,y1 ),g (Wb ,y2 )}); 54GemvArgs ac = a ; 55ac .n_out = 2 * NO ; 56const uint32_t sp = NO ; 57 mtl::Dispatch d ; 58d .p = & pc ; 59d .args = {mtl::Arg ::B (Wa ), mtl::Arg ::B (xp ), mtl::Arg ::B (xp ,s16 ), mtl::Arg ::B (z1 ), mtl::Arg ::V (ac ), mtl::Arg ::B (Wb ), 60 mtl::Arg ::B (z2 ), mtl::Arg ::V (sp )}; 61d .grid [0 ]= 2 * NO /4 ; 62d .tg [0 ]= 128 ; 63cx .run ({d }); 64const bool ok = std::memcmp (y1 .ptr ,z1 .ptr ,4 * nt * NO )== 0 && std::memcmp (y2 .ptr ,z2 .ptr ,4 * nt * NO )== 0 ; 65fails += !ok ; 66 std::printf ("gemv_cat (alpha + beta) ntok %d: bitwise %s\n" ,nt ,ok ?"PASS" :"FAIL" ); 67 } 68// ---- router_route 69 { 70for (int rty : {ref::BF16 , ref::F32 }) 71for (const int NE : {512 ,256 }) {// (256: the expert-pruned Coder; the logits rows keep their 512-float stride) 72const size_t rbr = ref::row_bytes (rty ,N ); 73 mtl::Buffer Wr = cx .alloc (rbr * NE ),lg1 = cx .alloc (4 * MAXT * 512 ),lg2 = cx .alloc (4 * MAXT * 512 ), 74id1 = cx .alloc (4 * MAXT * 10 ),id2 = cx .alloc (4 * MAXT * 10 ),w1 = cx .alloc (4 * MAXT * 10 ), 75w2 = cx .alloc (4 * MAXT * 10 ),pl1 = cx .alloc (4 * P_INTS ),pl2 = cx .alloc (4 * P_INTS ),cnt = cx .alloc (16 ); 76 ref::random_blocks (rty ,Wr .as < uint8_t > (),NE ,N ,9 ,0.05f ); 77 mtl::Pipeline pr = cx .pipeline (lib ,"moe_route" ); 78for (int nt = 1 ;nt <=4 ;++ nt ) { 79 mtl::Pipeline pg = cx .pipeline (lib ,"gemv_" + std::to_string (rty )+ "_32_1_4_1" , mtl::Constants ().i (0 ,nt )), 80pf = cx .pipeline (lib ,"router_route_" + std::to_string (rty ), mtl::Constants ().i (0 ,nt )); 81const size_t s16 = (size_t )4 * 4 * MAXT * N ; 82GemvArgs ga {(uint32_t )N , (uint32_t )NE , (uint32_t )nt , (uint32_t )rbr , (uint32_t )N ,512u }; 83MoeArgs ma {}; 84ma .n_tok = nt ; 85ma .topk = 10 ; 86ma .n_expert = NE ; 87ma .gx_gu = 5 ; 88ma .gx_dn = 20 ; 89 mtl::Dispatch g ; 90g .p = & pg ; 91g .args = {mtl::Arg ::B (Wr ), mtl::Arg ::B (xp ), mtl::Arg ::B (xp ,s16 ), mtl::Arg ::B (lg1 ), mtl::Arg ::V (ga )}; 92g .grid [0 ]= NE /4 ; 93g .tg [0 ]= 128 ; 94 mtl::Dispatch rt ; 95rt .p = & pr ; 96rt .args = {mtl::Arg ::B (lg1 ), mtl::Arg ::B (id1 ), mtl::Arg ::B (w1 ), mtl::Arg ::B (pl1 ), mtl::Arg ::V (ma )}; 97rt .tg [0 ]= 512 ; 98 std::memset (pl1 .ptr ,0 ,pl1 .bytes ); 99cx .run ({g ,rt }); 100bool ok = true; 101for (int rep_ = 0 ;rep_ < 2 ;++ rep_ ) { 102 std::memset (pl2 .ptr ,0 ,pl2 .bytes ); 103 mtl::Dispatch f ; 104f .p = & pf ; 105f .args = {mtl::Arg ::B (Wr ), mtl::Arg ::B (xp ), mtl::Arg ::B (xp ,s16 ), mtl::Arg ::B (lg2 ), mtl::Arg ::V (ga ), mtl::Arg ::B (id2 ), 106 mtl::Arg ::B (w2 ), mtl::Arg ::B (pl2 ), mtl::Arg ::V (ma ), mtl::Arg ::B (cnt )}; 107f .grid [0 ]= NE /16 ; 108f .tg [0 ]= 512 ; 109cx .run ({f }); 110ok = ok && std::memcmp (lg1 .ptr ,lg2 .ptr ,4 * nt * 512 )== 0 && std::memcmp (id1 .ptr ,id2 .ptr ,4 * nt * 10 )== 0 && 111 std::memcmp (w1 .ptr ,w2 .ptr ,4 * nt * 10 )== 0 && std::memcmp (pl1 .ptr ,pl2 .ptr ,4 * P_INTS )== 0 && 112cnt .as < uint32_t > ()[0 ]== 0 ; 113 } 114fails += !ok ; 115 std::printf ("router_route %s ntok %d: logits, ids, w, plan bitwise (2 runs) %s\n" , ref::type_name (rty ),nt , 116ok ?"PASS" :"FAIL" ); 117 } 118 } 119 } 120// ---- gr_norm_comb 121 { 122const int HC = 4 ,TOPK = 10 ; 123 mtl::Buffer R0 = cx .alloc (4 * MAXT * HC * N ),R1 = cx .alloc (4 * MAXT * HC * N ),R2 = cx .alloc (4 * MAXT * HC * N ), 124ye = cx .alloc (4 * (size_t )MAXT * TOPK * N ),ysh = cx .alloc (4 * MAXT * N ),sgv = cx .alloc (4 * MAXT ), 125plan = cx .alloc (4 * P_INTS ),inj = cx .alloc (4 * MAXT * HC ),wn = cx .alloc (4 * HC * N ),bo = cx .alloc (4 * MAXT * N ), 126x1 = cx .alloc (4 * MAXT * HC * N ),x2 = cx .alloc (4 * MAXT * HC * N ); 127for (size_t i = 0 ;i < R0 .bytes /4 ;++ i )R0 .as < float > ()[i ]= r .sym (); 128for (size_t i = 0 ;i < ye .bytes /4 ;++ i )ye .as < float > ()[i ]= r .sym (); 129for (size_t i = 0 ;i < ysh .bytes /4 ;++ i )ysh .as < float > ()[i ]= r .sym (); 130for (int i = 0 ;i < MAXT ;++ i )sgv .as < float > ()[i ]= r .sym ()* 3 ; 131for (int i = 0 ;i < MAXT * HC ;++ i )inj .as < float > ()[i ]= r .sym ()* 3 ; 132for (int i = 0 ;i < HC * N ;++ i )wn .as < float > ()[i ]= 0.5f + r .uni (); 133for (int i = 0 ;i < MAXT * TOPK ;++ i )plan .as < int > ()[P_TOKENT + i ]= (i * 7 ) % (MAXT * TOPK ); 134 mtl::Pipeline pcm = cx .pipeline (lib ,"moe_combine_sh" ),pn = cx .pipeline (lib ,"gr_norm" ),pnc = cx .pipeline (lib ,"gr_norm_comb" ); 135for (int nt = 1 ;nt <=4 ;++ nt ) { 136 std::memcpy (R1 .ptr ,R0 .ptr ,R0 .bytes ); 137 std::memcpy (R2 .ptr ,R0 .ptr ,R0 .bytes ); 138GlueArgs ga {(uint32_t )N , (uint32_t )nt , (uint32_t )N ,TOPK }; 139GrArgs gr {(uint32_t )N ,HC ,320 , (uint32_t )nt ,1 ,1e-6f }; 140 mtl::Dispatch c ;c .p = & pcm ; 141c .args = {mtl::Arg ::B (ye ), mtl::Arg ::B (plan ), mtl::Arg ::B (ysh ), mtl::Arg ::B (sgv ), mtl::Arg ::B (bo ), mtl::Arg ::V (ga )}; 142c .grid [0 ]= (nt * N + 255 ) /256 ;c .tg [0 ]= 256 ; 143 mtl::Dispatch n1 ;n1 .p = & pn ; 144n1 .args = {mtl::Arg ::B (R1 ), mtl::Arg ::B (bo ), mtl::Arg ::B (inj ), mtl::Arg ::B (wn ), mtl::Arg ::B (x1 ), mtl::Arg ::V (gr )}; 145n1 .grid [0 ]= HC ;n1 .grid [1 ]= nt ;n1 .tg [0 ]= 256 ; 146cx .run ({c ,n1 }); 147const uint32_t tk = TOPK ; 148 mtl::Dispatch n2 ;n2 .p = & pnc ; 149n2 .args = {mtl::Arg ::B (R2 ), mtl::Arg ::B (ye ), mtl::Arg ::B (inj ), mtl::Arg ::B (wn ), mtl::Arg ::B (x2 ), mtl::Arg ::V (gr ), 150 mtl::Arg ::B (plan ), mtl::Arg ::B (ysh ), mtl::Arg ::B (sgv ), mtl::Arg ::V (tk )}; 151n2 .grid [0 ]= HC ;n2 .grid [1 ]= nt ;n2 .tg [0 ]= 256 ; 152cx .run ({n2 }); 153const bool ok = std::memcmp (R1 .ptr ,R2 .ptr ,4 * (size_t )nt * HC * N )== 0 && std::memcmp (x1 .ptr ,x2 .ptr ,4 * (size_t )nt * HC * N )== 0 ; 154fails += !ok ; 155 std::printf ("gr_norm_comb ntok %d: R', xn bitwise %s\n" ,nt ,ok ?"PASS" :"FAIL" ); 156 } 157 } 158 std::printf ("%s\n" ,fails ?"SOME FAILED" :"ALL PASS" ); 159return fails ?1 :0 ; 160}