okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6a74320ca
main
1// metal/tests/mma_parity.cpp - the simdgroup-matrix GEMV (mma.metal): parity vs double for every dense format, each 2// token of a window bitwise a one-token call, and its speed on the packs' shapes against the per-lane GEMV. 3// 4// mma_parity parity 5// mma_parity --bench speed, 1 / 3 / 8 tokens 6#include "mtl_rt.h" 7#include "ref_quant.h" 8 9#include <algorithm> 10#include <cmath> 11#include <cstdio> 12#include <cstring> 13#include <string> 14#include <vector> 15 16namespace { 17struct MmaArgs {uint32_t n_in ,n_out ,row_bytes ,x_stride ,y_stride ; }; 18struct GemvArgs {uint32_t n_in ,n_out ,n_tok ,row_bytes ,x_stride ,y_stride ; }; 19double median (std::vector < double > v ) { std::sort (v .begin (),v .end ());return v [v .size () /2 ]; } 20int xkind (int t ) { 21switch (t ) { 22case ref::Q2_0 :return 1 ; 23case ref::Q4_K :case ref::Q5_K :return 2 ; 24case ref::Q6_K :case ref::Q3_K :return 3 ; 25default :return 0 ; 26 } 27} 28}// namespace 29 30int main (int argc ,char ** argv ) { 31const bool bench = argc > 1 && std::string (argv [1 ])== "--bench" ; 32 mtl::Context cx ; 33const int lib = cx .compile ({"quant.metal" ,"units.metal" ,"gemv.metal" ,"mma.metal" }); 34 std::printf ("device %s\n" ,cx .device_name ().c_str ()); 35const int MAXT = 8 ; 36 mtl::Pipeline pprep = cx .pipeline (lib ,"xprep" ); 37const std::vector < int > types = {ref::BF16 , ref::Q8_0 , ref::Q4_0 , ref::Q5_0 , ref::IQ4_NL , ref::IQ4_XS , ref::Q2_0 , 38 ref::IQ3_S , ref::Q3_K , ref::Q4_K , ref::Q5_K , ref::Q6_K }; 39if (!bench ) { 40int fails = 0 ; 41for (int t :types ) 42for (int n_in : {2560 ,640 ,6144 }) { 43if (n_in % ref::block_elems (t )|| (n_in == 640 && ref::block_elems (t )== 256 ))continue ; 44const int n_out = 100 ; 45const size_t rb = ref::row_bytes (t ,n_in ); 46 mtl::Buffer W = cx .alloc (rb * n_out ),x = cx .alloc (4 * n_in * MAXT ),y = cx .alloc (4 * n_out * MAXT ), 47y1 = cx .alloc (4 * n_out * MAXT ); 48 ref::random_blocks (t ,W .as < uint8_t > (),n_out ,n_in ,41 + t ,0.02f ); 49 ref::Rng r (9 ); 50for (int i = 0 ;i < n_in * MAXT ;++ i )x .as < float > ()[i ]= r .sym (); 51 std::vector < float > wf ((size_t )n_in * n_out ); 52 ref::dequant (t ,W .ptr ,wf .data (),wf .size ()); 53for (int nt : {1 ,3 ,8 }) { 54 mtl::Pipeline p = cx .pipeline (lib ,"gemv_mma_" + std::to_string (t )+ "_2" , mtl::Constants ().i (4 ,nt )); 55MmaArgs a {(uint32_t )n_in , (uint32_t )n_out , (uint32_t )rb , (uint32_t )n_in , (uint32_t )n_out }; 56 mtl::Dispatch d ; 57d .p = & p ; 58d .args = {mtl::Arg ::B (W ), mtl::Arg ::B (x ), mtl::Arg ::B (y ), mtl::Arg ::V (a )}; 59d .grid [0 ]= (n_out + 15 ) /16 ; 60d .tg [0 ]= 64 ; 61cx .run ({d }); 62double se = 0 ,sr = 0 ; 63for (int tt = 0 ;tt < nt ;++ tt ) 64for (int o = 0 ;o < n_out ;++ o ) { 65double ref = 0 ; 66for (int k = 0 ;k < n_in ;++ k )ref += (double )wf [(size_t )o * n_in + k ]* x .as < float > ()[tt * n_in + k ]; 67const double e = y .as < float > ()[tt * n_out + o ]- ref ; 68se += e * e ; 69sr += ref * ref ; 70 } 71// the window property: token tt's column vs a one-token call on x shifted to row 0 72int notex = 0 ; 73if (nt > 1 ) { 74 mtl::Pipeline p1 = cx .pipeline (lib ,"gemv_mma_" + std::to_string (t )+ "_2" , mtl::Constants ().i (4 ,1 )); 75for (int tt = 0 ;tt < nt ;++ tt ) { 76 mtl::Dispatch d1 = d ; 77d1 .p = & p1 ; 78d1 .args = {mtl::Arg ::B (W ), mtl::Arg ::B (x ,4 * (size_t )tt * n_in ), mtl::Arg ::B (y1 ), mtl::Arg ::V (a )}; 79// rows past the window come from the following tokens' memory: padding only 80cx .run ({d1 }); 81notex += std::memcmp (y1 .ptr ,y .as < float > ()+ tt * n_out ,4 * n_out )!= 0 ; 82 } 83 } 84const double rel = std::sqrt (se /sr ); 85const bool ok = rel < 1e-5 && notex == 0 ; 86fails += !ok ; 87 std::printf ("mma parity %-7s n_in %5d ntok %d: rel L2 %.2e, window == one token: %s %s\n" , 88 ref::type_name (t ),n_in ,nt ,rel ,nt == 1 ?"-" :notex ?"NO" :"bitwise" ,ok ?"PASS" :"FAIL" ); 89 } 90cx .free (W ); 91cx .free (x ); 92cx .free (y ); 93cx .free (y1 ); 94 } 95 std::printf ("%s\n" ,fails ?"SOME FAILED" :"ALL PASS" ); 96return fails ?1 :0 ; 97 } 98struct Shape {const char * name ;int type ,n_in ,n_out ; }; 99const std::vector < Shape > shapes = {{"gdn qkv" , ref::IQ4_XS ,2560 ,10240 }, {"gdn qkv" , ref::Q3_K ,2560 ,10240 }, 100 {"gdn qkv" , ref::Q4_K ,2560 ,10240 }, {"gdn qkv" , ref::Q6_K ,2560 ,10240 }, 101 {"gdn out" , ref::Q5_K ,6144 ,2560 }, {"qsa q" , ref::Q2_0 ,2560 ,12288 }, 102 {"router" , ref::BF16 ,2560 ,512 }, {"lm head" , ref::Q5_K ,2560 ,248320 }}; 103 std::printf ("%-9s %-7s %5s | %9s %9s | %s\n" ,"shape" ,"type" ,"ntok" ,"lane us" ,"mma us" ,"mma GB/s (best SG)" ); 104for (const Shape & s :shapes ) { 105const size_t rb = ref::row_bytes (s .type ,s .n_in ),mb = rb * s .n_out ; 106const int copies = (int )std::max < size_t > (4 , std::min < size_t > (64 , (1ull <<30 ) /mb )); 107 mtl::Buffer W = cx .alloc (mb * copies ),x = cx .alloc (4 * s .n_in * MAXT ),y = cx .alloc (4 * (size_t )s .n_out * MAXT ), 108xp = cx .alloc (4 * (4 * s .n_in * MAXT + s .n_in /16 * MAXT )); 109 { 110 std::vector < uint8_t > tmp (mb ); 111 ref::random_blocks (s .type ,tmp .data (),s .n_out ,s .n_in ,3 ,0.02f ); 112for (int c = 0 ;c < copies ;++ c ) std::memcpy (W .as < uint8_t > ()+ (size_t )c * mb ,tmp .data (),mb ); 113 } 114 ref::Rng r (5 ); 115for (int i = 0 ;i < s .n_in * MAXT ;++ i )x .as < float > ()[i ]= r .sym (); 116 { 117 mtl::Dispatch d ; 118d .p = & pprep ; 119const uint32_t n = s .n_in ,nt = MAXT ; 120d .args = {mtl::Arg ::B (x ), mtl::Arg ::B (xp ), mtl::Arg ::V (n ), mtl::Arg ::V (nt )}; 121d .tg [0 ]= 256 ; 122d .grid [0 ]= (s .n_in /16 * MAXT + 255 ) /256 ; 123cx .run ({d }); 124 } 125const size_t plane = (size_t )4 * s .n_in * MAXT ; 126for (int nt : {1 ,3 ,8 }) { 127auto time = [& ](std::vector < mtl::Dispatch >& ds ) { 128cx .warm (ds ,0.1 ); 129 std::vector < double > t ; 130for (int i = 0 ;i < 5 ;++ i )t .push_back (cx .run (ds ) /ds .size ()); 131return median (t ); 132 }; 133// the per-lane GEMV with its usual tile 134const int L = s .n_in >=6144 ?32 : (s .n_out <=640 || s .type == ref::Q2_0 || s .type == ref::BF16 ) ?32 :16 ; 135const int S = s .n_in >=6144 ?8 :4 ,K = s .n_in >=6144 ?2 :1 ; 136 mtl::Pipeline pl = cx .pipeline (lib ,"gemv_" + std::to_string (s .type )+ "_" + std::to_string (L )+ "_1_" + 137 std::to_string (S )+ "_" + std::to_string (K ), mtl::Constants ().i (0 ,nt )); 138GemvArgs ga {(uint32_t )s .n_in , (uint32_t )s .n_out , (uint32_t )nt , (uint32_t )rb , (uint32_t )s .n_in , (uint32_t )s .n_out }; 139 std::vector < mtl::Dispatch > dl ; 140for (int k = 0 ;k < std::max (copies ,16 );++ k ) { 141 mtl::Dispatch d ; 142d .p = & pl ; 143d .args = {mtl::Arg ::B (W , (size_t )(k %copies )* mb ), mtl::Arg ::B (xp ,plane * xkind (s .type )), 144 mtl::Arg ::B (xp ,plane * 4 ), mtl::Arg ::B (y ), mtl::Arg ::V (ga )}; 145const int rpt = (S /K )* (32 /L ); 146d .grid [0 ]= (s .n_out + rpt - 1 ) /rpt ; 147d .tg [0 ]= S * 32 ; 148dl .push_back (d ); 149 } 150const double tl = time (dl ); 151double best = 1e9 ; 152int bsg = 0 ; 153for (int sgn : {1 ,2 ,4 }) { 154 mtl::Pipeline pm = cx .pipeline (lib ,"gemv_mma_" + std::to_string (s .type )+ "_" + std::to_string (sgn ), 155 mtl::Constants ().i (4 ,nt )); 156MmaArgs ma {(uint32_t )s .n_in , (uint32_t )s .n_out , (uint32_t )rb , (uint32_t )s .n_in , (uint32_t )s .n_out }; 157 std::vector < mtl::Dispatch > dm ; 158for (int k = 0 ;k < std::max (copies ,16 );++ k ) { 159 mtl::Dispatch d ; 160d .p = & pm ; 161d .args = {mtl::Arg ::B (W , (size_t )(k %copies )* mb ), mtl::Arg ::B (x ), mtl::Arg ::B (y ), mtl::Arg ::V (ma )}; 162d .grid [0 ]= (s .n_out + 8 * sgn - 1 ) / (8 * sgn ); 163d .tg [0 ]= 32 * sgn ; 164dm .push_back (d ); 165 } 166const double tm = time (dm ); 167if (tm < best ) {best = tm ;bsg = sgn ; } 168 } 169 std::printf ("%-9s %-7s %5d | %9.2f %9.2f | %6.1f (SG %d) lane %6.1f GB/s\n" ,s .name , ref::type_name (s .type ),nt , 170tl * 1e6 ,best * 1e6 ,mb /best /1e9 ,bsg ,mb /tl /1e9 ); 171 } 172cx .free (W ); 173cx .free (x ); 174cx .free (y ); 175cx .free (xp ); 176 } 177return 0 ; 178}