okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7f46672a4
main
1// metal/tests/gemv_parity.cpp - the dense GEMV (gemv.metal) per format: parity against double, and speed per tile 2// shape on the model's projection shapes. 3// 4// gemv_parity [--nin A,B,C] parity: every format, n_in 2560/640/10240 (or --nin), 1..8 tokens 5// gemv_parity --bench [--pack Q2_0|IQ3_XXS] [--ntok N] [--all] speed on the pack's shapes (best tile per shape) 6// gemv_parity --one TYPE NIN NOUT [--ntok N] sweep one shape 7// GEMV_F16=1 gemv_parity --one TYPE NIN NOUT [--ntok N] the fp16 tiles (STRATA_METAL_F16) against the fp32 gemv 8#include "gguf_lite.h" 9#include "mtl_rt.h" 10#include "ref_quant.h" 11 12#include <algorithm> 13#include <cmath> 14#include <cstdio> 15#include <cstdlib> 16#include <cstring> 17#include <string> 18#include <vector> 19 20namespace { 21 22struct GemvArgs {uint32_t n_in ,n_out ,n_tok ,row_bytes ,x_stride ,y_stride ; }; 23struct Cfg {int L ,R ,S ,K ; }; 24const std::vector < Cfg > kCfgs = {{32 ,1 ,4 ,1 }, {32 ,2 ,4 ,1 }, {16 ,1 ,4 ,1 }, {16 ,2 ,4 ,1 }, {8 ,1 ,4 ,1 }, 25 {8 ,2 ,4 ,1 }, {4 ,2 ,4 ,1 }, {4 ,1 ,4 ,1 }, {32 ,1 ,4 ,4 }, {32 ,2 ,4 ,2 }, 26 {32 ,1 ,8 ,8 }, {16 ,1 ,8 ,4 }, {32 ,1 ,8 ,2 }, {8 ,1 ,8 ,1 }, {1 ,1 ,4 ,1 }, 27 {1 ,2 ,4 ,1 }, {2 ,1 ,4 ,1 }, {2 ,2 ,4 ,1 }, {1 ,1 ,2 ,1 }, {4 ,4 ,4 ,1 }}; 28int rows_per_tg (Cfg c ) {return (c .S /c .K )* (32 /c .L )* c .R ; } 29int unit_of (int t ) { 30switch (t ) { 31case ref::Q2_0 :case ref::IQ3_S :case ref::IQ3_XXS :case ref::IQ2_XXS :case ref::IQ2_XS :case ref::IQ2_S : 32case ref::BF16 :case ref::F32 :return 16 ; 33default :return 32 ; 34 } 35} 36bool cfg_ok (Cfg c ,int type ,int n_in ) { 37const int U = n_in /unit_of (type ); 38return U %c .K == 0 && U /c .K >=c .L /2 ; 39} 40std::string kname (int t ,Cfg c ) { 41return "gemv_" + std::to_string (t )+ "_" + std::to_string (c .L )+ "_" + std::to_string (c .R )+ "_" + 42 std::to_string (c .S )+ "_" + std::to_string (c .K ); 43} 44double median (std::vector < double > v ) { std::sort (v .begin (),v .end ());return v [v .size () /2 ]; } 45// which prepared x a format reads (units.metal XK_*) 46int xkind (int t ) { 47switch (t ) { 48case ref::Q2_0 :return 1 ; 49case ref::Q4_K :case ref::Q5_K :return 2 ; 50case ref::Q6_K :case ref::Q3_K :return 3 ; 51default :return 0 ; 52 } 53} 54// x [MAXT][n] -> XP [4][MAXT][n] + S16 [MAXT][n/16] 55mtl::Dispatch xprep_dispatch (const mtl::Pipeline & p ,const mtl::Buffer & x ,const mtl::Buffer & xp ,uint32_t n ,uint32_t nt ) { 56 mtl::Dispatch d ; 57d .p = & p ; 58d .args = {mtl::Arg ::B (x ), mtl::Arg ::B (xp ), mtl::Arg ::V (n ), mtl::Arg ::V (nt )}; 59d .tg [0 ]= 256 ; 60d .grid [0 ]= (n /16 * nt + 255 ) /256 ; 61return d ; 62} 63 64int type_of (const std::string & s ) { 65for (int t : {ref::Q2_0 , ref::Q4_0 , ref::Q5_0 , ref::Q8_0 , ref::Q3_K , ref::Q4_K , ref::Q5_K , ref::Q6_K , ref::IQ4_NL , 66 ref::IQ4_XS , ref::IQ3_S , ref::BF16 }) 67if (s == ref::type_name (t ))return t ; 68 std::fprintf (stderr ,"unknown type %s\n" ,s .c_str ()); 69 std::exit (2 ); 70} 71 72struct Shape {const char * name ;int type ,n_in ,n_out ,per_token ; };// per_token: calls per decoded token 73 74}// namespace 75 76int main (int argc ,char ** argv ) { 77bool bench = false,all = false; 78double worst16 = -1 ;// GEMV_F16: the worst tile 79int ntok = 1 ; 80 std::string gguf_path ; 81 std::vector < int > nins = {2560 ,640 ,10240 };// --nin A,B,C: the parity run's row lengths (the model's: hidden, expert width, value dim, ...) 82 std::string pack = "Q2_0" ; 83 std::vector < Shape > one ; 84for (int i = 1 ;i < argc ;++ i ) { 85 std::string a = argv [i ]; 86if (a == "--bench" )bench = true; 87else if (a == "--all" )all = true; 88else if (a == "--ntok" )ntok = std::atoi (argv [++ i ]); 89else if (a == "--nin" ) { 90nins .clear (); 91for (const char * q = argv [++ i ];* q ;) {nins .push_back (std::atoi (q ));while (* q && * q != ',' )++ q ;if (* q )++ q ; } 92 } 93else if (a == "--pack" )pack = argv [++ i ]; 94else if (a == "--gguf" )gguf_path = argv [++ i ]; 95else if (a == "--one" ) { 96const int t = type_of (argv [i + 1 ]); 97one .push_back ({"one" ,t , std::atoi (argv [i + 2 ]), std::atoi (argv [i + 3 ]),1 }); 98i += 3 ; 99bench = true; 100all = true; 101 }else { std::fprintf (stderr ,"unknown argument %s\n" ,a .c_str ());return 2 ; } 102 } 103 mtl::Context cx ; 104const int lib = cx .compile ({"quant.metal" ,"units.metal" ,"gemv.metal" }, std::getenv ("GEMV_F16" ) ?"#define STRATA_METAL_F16 1\n" :"" ); 105 std::printf ("device %s\n" ,cx .device_name ().c_str ()); 106const int MAXT = 8 ; 107 mtl::Pipeline pprep = cx .pipeline (lib ,"xprep" ); 108 109if (!gguf_path .empty ()) { 110// real tensors: the first type-distinct ones of the file (experts: expert 0's matrix), 1 and 3 tokens 111 gguf::File f ; 112 std::string err ; 113if (!f .open (gguf_path ,err )) { std::fprintf (stderr ,"%s\n" ,err .c_str ());return 2 ; } 114 mtl::Buffer Wf = cx .wrap (f .map ,f .map_bytes ); 115 std::map < int ,const gguf::Tensor *> pick ; 116for (const auto & kv :f .tensors ) { 117const gguf::Tensor & t = kv .second ; 118if (t .shape .size ()< 2 || t .name .find ("token_embd" )!= std::string::npos || 119t .name .find ("per_layer" )!= std::string::npos ) 120continue ; 121const int ty = t .type ; 122const bool gemv_type = ty == ref::BF16 || ty == ref::Q8_0 || ty == ref::Q4_0 || ty == ref::Q5_0 || 123ty == ref::IQ4_NL || ty == ref::IQ4_XS || ty == ref::Q2_0 || ty == ref::IQ3_S || 124ty == ref::Q3_K || ty == ref::Q4_K || ty == ref::Q5_K || ty == ref::Q6_K ; 125if (gemv_type && !pick .count (ty ))pick [ty ]= & t ; 126 } 127int fails = 0 ; 128for (const auto & kv :pick ) { 129const gguf::Tensor & t = * kv .second ; 130const int ty = t .type ,n_in = (int )t .shape [0 ],n_out = std::min < int > ((int )t .shape [1 ],4096 ); 131const size_t rb = ref::row_bytes (ty ,n_in ); 132 mtl::Buffer x = cx .alloc (4 * n_in * MAXT ),y = cx .alloc (4 * (size_t )n_out * MAXT ), 133xp = cx .alloc (4 * (4 * n_in * MAXT + n_in /16 * MAXT )); 134 ref::Rng r (3 ); 135for (int i = 0 ;i < n_in * MAXT ;++ i )x .as < float > ()[i ]= r .sym (); 136cx .run ({xprep_dispatch (pprep ,x ,xp ,n_in ,MAXT )}); 137const size_t plane = (size_t )4 * n_in * MAXT ; 138 std::vector < float > wf ((size_t )n_in * n_out ); 139 ref::dequant (ty , (const uint8_t * )f .map + t .offset ,wf .data (),wf .size ()); 140Cfg c = n_in >=6144 ?Cfg {32 ,1 ,8 ,2 } :n_in <=640 ?Cfg {8 ,1 ,4 ,1 } :Cfg {16 ,1 ,4 ,1 }; 141if (!cfg_ok (c ,ty ,n_in ))c = Cfg {32 ,1 ,4 ,1 }; 142for (int nt : {1 ,3 }) { 143 mtl::Pipeline p = cx .pipeline (lib ,kname (ty ,c ), mtl::Constants ().i (0 ,nt )); 144GemvArgs a {(uint32_t )n_in , (uint32_t )n_out , (uint32_t )nt , (uint32_t )rb , (uint32_t )n_in , (uint32_t )n_out }; 145 mtl::Dispatch d ; 146d .p = & p ; 147d .args = {mtl::Arg ::B (Wf ,t .offset ), mtl::Arg ::B (xp ,plane * xkind (ty )), mtl::Arg ::B (xp ,plane * 4 ), 148 mtl::Arg ::B (y ), mtl::Arg ::V (a )}; 149d .grid [0 ]= (n_out + rows_per_tg (c )- 1 ) /rows_per_tg (c ); 150d .tg [0 ]= c .S * 32 ; 151cx .run ({d }); 152double se = 0 ,sr = 0 ; 153for (int tt = 0 ;tt < nt ;++ tt ) 154for (int o = 0 ;o < n_out ;++ o ) { 155double ref = 0 ; 156for (int k = 0 ;k < n_in ;++ k )ref += (double )wf [(size_t )o * n_in + k ]* x .as < float > ()[tt * n_in + k ]; 157const double e = y .as < float > ()[tt * n_out + o ]- ref ; 158se += e * e ; 159sr += ref * ref ; 160 } 161const double rel = std::sqrt (se /sr ); 162const bool ok = rel < 1e-5 && std::isfinite (rel ); 163fails += !ok ; 164 std::printf ("real %-7s %-34s %5d x %5d ntok %d: rel L2 %.2e %s\n" , ref::type_name (ty ),t .name .c_str (), 165n_out ,n_in ,nt ,rel ,ok ?"PASS" :"FAIL" ); 166 } 167 } 168 std::printf ("%s\n" ,fails ?"SOME FAILED" :"ALL PASS" ); 169return fails ?1 :0 ; 170 } 171if (!bench ) { 172int fails = 0 ; 173for (int t : {ref::F32 , ref::BF16 , ref::Q8_0 , ref::Q4_0 , ref::Q5_0 , ref::IQ4_NL , ref::IQ4_XS , ref::Q2_0 , ref::IQ3_S , 174 ref::Q3_K , ref::Q4_K , ref::Q5_K , ref::Q6_K }) { 175for (int n_in :nins ) { 176if (n_in % ref::block_elems (t ))continue ; 177const int n_out = 100 ;// not a multiple of the rows per threadgroup on purpose 178const size_t rb = ref::row_bytes (t ,n_in ); 179 mtl::Buffer W = cx .alloc (rb * n_out ),x = cx .alloc (4 * n_in * MAXT ),y = cx .alloc (4 * n_out * MAXT ); 180 mtl::Buffer xp = cx .alloc (4 * (4 * n_in * MAXT + n_in /16 * MAXT )); 181 ref::random_blocks (t ,W .as < uint8_t > (),n_out ,n_in ,31 + t + n_in ); 182 ref::Rng r (7 + n_in ); 183for (int i = 0 ;i < n_in * MAXT ;++ i )x .as < float > ()[i ]= r .sym (); 184cx .run ({xprep_dispatch (pprep ,x ,xp ,n_in ,MAXT )}); 185const size_t plane = (size_t )4 * n_in * MAXT ; 186 std::vector < float > wf ((size_t )n_in * n_out ); 187 ref::dequant (t ,W .ptr ,wf .data (),wf .size ()); 188for (int nt : {1 ,3 ,8 }) { 189double worst = 0 ; 190int not_exact = 0 ; 191 std::string worst_cfg ; 192for (Cfg c :kCfgs ) { 193if (!cfg_ok (c ,t ,n_in ))continue ; 194 mtl::Pipeline p = cx .pipeline (lib ,kname (t ,c ), mtl::Constants ().i (0 ,nt )); 195GemvArgs a {(uint32_t )n_in , (uint32_t )n_out , (uint32_t )nt , (uint32_t )rb , (uint32_t )n_in , 196 (uint32_t )n_out }; 197 std::memset (y .ptr ,0 ,y .bytes ); 198 mtl::Dispatch d ; 199d .p = & p ; 200d .args = {mtl::Arg ::B (W ), mtl::Arg ::B (xp ,plane * xkind (t )), mtl::Arg ::B (xp ,plane * 4 ), 201 mtl::Arg ::B (y ), mtl::Arg ::V (a )}; 202d .grid [0 ]= (n_out + rows_per_tg (c )- 1 ) /rows_per_tg (c ); 203d .tg [0 ]= c .S * 32 ; 204cx .run ({d }); 205double se = 0 ,sr = 0 ; 206for (int tt = 0 ;tt < nt ;++ tt ) 207for (int o = 0 ;o < n_out ;++ o ) { 208double ref = 0 ; 209for (int k = 0 ;k < n_in ;++ k )ref += (double )wf [(size_t )o * n_in + k ]* x .as < float > ()[tt * n_in + k ]; 210const double e = y .as < float > ()[tt * n_out + o ]- ref ; 211se += e * e ; 212sr += ref * ref ; 213 } 214const double rel = std::sqrt (se /sr ); 215if (rel > worst ) {worst = rel ;worst_cfg = kname (t ,c ); } 216if (nt > 1 ) { 217// every token of a window must be bitwise what a one-token call gives 218 std::vector < float > ywin ((float * )y .ptr , (float * )y .ptr + nt * n_out ); 219 mtl::Pipeline p1 = cx .pipeline (lib ,kname (t ,c ), mtl::Constants ().i (0 ,1 )); 220GemvArgs a1 = a ; 221a1 .n_tok = 1 ; 222for (int tt = 0 ;tt < nt ;++ tt ) { 223 mtl::Dispatch d1 = d ; 224d1 .p = & p1 ; 225d1 .args = {mtl::Arg ::B (W ), mtl::Arg ::B (xp ,plane * xkind (t )+ 4 * (size_t )tt * n_in ), 226 mtl::Arg ::B (xp ,plane * 4 + 4 * (size_t )tt * (n_in /16 )), mtl::Arg ::B (y ), 227 mtl::Arg ::V (a1 )}; 228cx .run ({d1 }); 229if (std::memcmp (y .ptr ,ywin .data ()+ tt * n_out ,4 * n_out )!= 0 )++ not_exact ; 230 } 231 } 232 } 233const bool ok = worst < 1e-5 && not_exact == 0 ; 234fails += !ok ; 235 std::printf ("parity %-7s n_in %5d ntok %d: worst rel L2 over %zu tiles %.2e (%s), window == one " 236"token: %s %s\n" , 237 ref::type_name (t ),n_in ,nt ,kCfgs .size (),worst ,worst_cfg .c_str (), 238nt == 1 ?"-" :not_exact ?"NO" :"bitwise" ,ok ?"PASS" :"FAIL" ); 239 } 240cx .free (W ); 241cx .free (x ); 242cx .free (y ); 243cx .free (xp ); 244 } 245 } 246 std::printf ("%s\n" ,fails ?"SOME FAILED" :"ALL PASS" ); 247return fails ?1 :0 ; 248 } 249 250// ---- bench 251 std::vector < Shape > shapes = one ; 252if (shapes .empty ()) { 253if (pack == "Q2_0" ) 254shapes = { 255 {"hc down (x4/layer)" , ref::BF16 ,10240 ,320 ,4 * 48 }, {"hc up (x4/layer)" , ref::BF16 ,320 ,10240 ,4 * 48 }, 256 {"router" , ref::BF16 ,2560 ,512 ,48 }, {"gdn qkv" , ref::IQ4_XS ,2560 ,10240 ,13 }, 257 {"gdn qkv" , ref::Q3_K ,2560 ,10240 ,18 }, {"gdn qkv" , ref::Q4_K ,2560 ,10240 ,4 }, 258 {"gdn gate" , ref::Q3_K ,2560 ,6144 ,25 }, {"gdn out" , ref::IQ4_XS ,6144 ,2560 ,17 }, 259 {"gdn out" , ref::Q4_K ,6144 ,2560 ,11 }, {"qsa q" , ref::Q3_K ,2560 ,12288 ,6 }, 260 {"qsa out" , ref::Q5_K ,6144 ,2560 ,4 }, {"qsa k" , ref::Q3_K ,2560 ,512 ,3 }, 261 {"shexp gate" , ref::Q2_0 ,2560 ,640 ,21 }, {"shexp down" , ref::Q4_0 ,640 ,2560 ,16 }, 262 {"shexp down" , ref::Q2_0 ,640 ,2560 ,16 }, {"indexer q" , ref::BF16 ,2560 ,512 ,12 }, 263 {"ssm alpha" , ref::BF16 ,2560 ,48 ,36 }, {"lm head" , ref::Q5_K ,2560 ,248320 ,1 }, 264 {"gdn qkv" , ref::Q6_K ,2560 ,10240 ,1 }, 265 }; 266else 267shapes = { 268 {"hc down (x4/layer)" , ref::BF16 ,10240 ,320 ,4 * 48 }, {"hc up (x4/layer)" , ref::BF16 ,320 ,10240 ,4 * 48 }, 269 {"gdn qkv" , ref::IQ4_XS ,2560 ,10240 ,11 }, {"gdn qkv" , ref::Q6_K ,2560 ,10240 ,14 }, 270 {"gdn qkv" , ref::Q4_K ,2560 ,10240 ,8 }, {"gdn out" , ref::Q6_K ,6144 ,2560 ,19 }, 271 {"gdn gate" , ref::IQ3_S ,2560 ,6144 ,10 }, {"gdn gate" , ref::IQ4_XS ,2560 ,6144 ,11 }, 272 {"qsa out" , ref::Q6_K ,6144 ,2560 ,10 }, {"qsa q" , ref::IQ4_XS ,2560 ,12288 ,4 }, 273 {"shexp down" , ref::IQ4_NL ,640 ,2560 ,40 }, {"shexp up" , ref::IQ3_S ,2560 ,640 ,27 }, 274 {"lm head" , ref::Q5_K ,2560 ,248320 ,1 }, 275 }; 276 } 277 std::vector < int > ntoks = {ntok }; 278 std::printf ("%-20s %-7s %6s %7s %5s %9s %9s %8s %s\n" ,"shape" ,"type" ,"n_in" ,"n_out" ,"ntok" ,"us" ,"GB/s" , 279"MB" ,"best tile (L,R,S,K)" ); 280for (const Shape & s :shapes ) { 281const size_t rb = ref::row_bytes (s .type ,s .n_in ),mb = rb * s .n_out ; 282const int copies = (int )std::max < size_t > (4 , std::min < size_t > (256 , (1ull <<30 ) /mb )); 283 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 ); 284 { 285 std::vector < uint8_t > tmp (mb ); 286 ref::random_blocks (s .type ,tmp .data (),s .n_out ,s .n_in ,99 ); 287for (int c = 0 ;c < copies ;++ c ) std::memcpy (W .as < uint8_t > ()+ (size_t )c * mb ,tmp .data (),mb ); 288 } 289 ref::Rng r (5 ); 290for (int i = 0 ;i < s .n_in * MAXT ;++ i )x .as < float > ()[i ]= r .sym (); 291 mtl::Buffer xp = cx .alloc (4 * (4 * s .n_in * MAXT + s .n_in /16 * MAXT )); 292cx .run ({xprep_dispatch (pprep ,x ,xp ,s .n_in ,MAXT )}); 293const size_t plane = (size_t )4 * s .n_in * MAXT ; 294// GEMV_F16=1: gemv16_* over the format's prepared plane converted to half; each tile's rel L2 against the fp32 gemv 295const bool f16 = std::getenv ("GEMV_F16" )!= nullptr ; 296 mtl::Buffer xh ,yr ; 297if (f16 ) {xh = cx .alloc (2 * (size_t )s .n_in * MAXT );yr = cx .alloc (4 * (size_t )s .n_out * MAXT ); } 298if (f16 )// the half plane from the raw x: the K-quants 2^-8 at odd positions, Q2_0 4^-(i % 4), the rest raw 299for (size_t i = 0 ;i < (size_t )s .n_in * MAXT ;++ i ) { 300const size_t k = i % (size_t )s .n_in ; 301const bool p2 = s .type == ref::Q3_K || s .type == ref::Q4_K || s .type == ref::Q5_K || s .type == ref::Q6_K ; 302const float f = p2 ? ((k & 1 ) ?1.0f /256 :1.0f ) :s .type == ref::Q2_0 ?1.0f /float (1 << (2 * (k & 3 ))) :1.0f ; 303xh .as < _Float16 > ()[i ]= (_Float16 )(xp .as < float > ()[i ]* f ); 304 } 305for (int nt :ntoks ) { 306if (f16 ) {// the fp32 reference of copy 0 with the default tile 307 mtl::Pipeline pr = cx .pipeline (lib ,kname (s .type ,Cfg {16 ,1 ,4 ,1 }), mtl::Constants ().i (0 ,nt ).i (9 ,0 )); 308GemvArgs a {(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 }; 309 mtl::Dispatch d ; 310d .p = & pr ; 311d .args = {mtl::Arg ::B (W ), mtl::Arg ::B (xp ,plane * xkind (s .type )), mtl::Arg ::B (xp ,plane * 4 ), mtl::Arg ::B (yr ), mtl::Arg ::V (a )}; 312d .grid [0 ]= (s .n_out + rows_per_tg (Cfg {16 ,1 ,4 ,1 })- 1 ) /rows_per_tg (Cfg {16 ,1 ,4 ,1 }); 313d .tg [0 ]= 128 ; 314cx .run ({d }); 315 } 316double best = 1e9 ; 317Cfg bc {}; 318const bool gx = std::getenv ("GEMV_X" )!= nullptr ;// gemvx_* (x staged in threadgroup memory) 319const uint32_t tgm = (uint32_t )(4 * nt * (s .n_in + s .n_in /16 )+ 15 ) /16 * 16 ; 320for (Cfg c :gx ? std::vector < Cfg > {{32 ,1 ,4 ,1 }, {32 ,2 ,4 ,1 }, {16 ,1 ,4 ,1 }, {16 ,2 ,4 ,1 }, {8 ,1 ,4 ,1 }, 321 {8 ,2 ,4 ,1 }, {4 ,2 ,4 ,1 }, {16 ,2 ,8 ,1 }, {8 ,2 ,8 ,1 }, {16 ,4 ,4 ,1 }, 322 {32 ,2 ,8 ,1 }, {2 ,2 ,4 ,1 }} 323 :kCfgs ) { 324if (!cfg_ok (c ,s .type ,s .n_in ))continue ; 325if (!all && !(c .K == 1 || s .n_in >=6144 ))continue ; 326if (gx && tgm > 32768 )continue ; 327if (f16 && !(c .K == 1 || (c .L == 32 && c .S == 8 && c .K == 2 )|| (c .L == 16 && c .S == 8 && c .K == 4 )))continue ; 328if (f16 && ((c .L == 32 && c .R == 1 && c .S == 4 && c .K == 4 )|| (c .L == 1 && c .R == 2 )|| (c .L == 1 && c .S == 2 )|| 329 (c .L == 4 && c .R == 4 )|| (c .L == 8 && c .S == 8 )))continue ; 330const std::string kn = gx ?"gemvx_" + std::to_string (s .type )+ "_" + std::to_string (c .L )+ "_" + std::to_string (c .R )+ 331"_" + std::to_string (c .S ) 332 :f16 ?"gemv16" + kname (s .type ,c ).substr (4 ) :kname (s .type ,c ); 333 mtl::Pipeline p = cx .pipeline (lib ,kn , mtl::Constants ().i (0 ,nt ).i (9 , std::getenv ("GEMV_PROBE" ) ? std::atoi (std::getenv ("GEMV_PROBE" )) :0 )); 334GemvArgs a {(uint32_t )s .n_in , (uint32_t )s .n_out , (uint32_t )nt , (uint32_t )rb , (uint32_t )s .n_in , 335 (uint32_t )s .n_out }; 336 std::vector < mtl::Dispatch > ds ; 337const int R = std::max (copies ,16 ); 338for (int k = 0 ;k < R ;++ k ) { 339 mtl::Dispatch d ; 340d .p = & p ; 341d .args = {mtl::Arg ::B (W , (size_t )(k %copies )* mb ),f16 ? mtl::Arg ::B (xh ) : mtl::Arg ::B (xp ,plane * xkind (s .type )), 342 mtl::Arg ::B (xp ,plane * 4 ), mtl::Arg ::B (y ), mtl::Arg ::V (a )}; 343d .grid [0 ]= (s .n_out + rows_per_tg (c )- 1 ) /rows_per_tg (c ); 344d .tg [0 ]= c .S * 32 ; 345if (gx )d .tg_mem = tgm ; 346ds .push_back (d ); 347 } 348cx .warm (ds ,0.05 ); 349 std::vector < double > t ; 350for (int i = 0 ;i < 5 ;++ i )t .push_back (cx .run (ds ) /R ); 351const double m = median (t ); 352double rel = 0 ; 353if (f16 ) {// ds[0] reads copy 0: its output against the fp32 reference 354cx .run ({ds [0 ]}); 355double se = 0 ,sr = 0 ; 356for (size_t i = 0 ;i < (size_t )s .n_out * nt ;++ i ) { 357const double g = y .as < float > ()[i ],r0 = yr .as < float > ()[i ]; 358se += (g - r0 )* (g - r0 ); 359sr += r0 * r0 ; 360 } 361rel = std::sqrt (se /sr ); 362worst16 = std::max (worst16 ,rel ); 363 } 364if (one .size ()) std::printf (" tile %2d,%d,%d,%d: %8.2f us %6.1f GB/s%s\n" ,c .L ,c .R ,c .S ,c .K ,m * 1e6 ,mb /m /1e9 , 365f16 ? (" rel L2 vs fp32 " + std::to_string (rel )).c_str () :"" ); 366if (m < best ) {best = m ;bc = c ; } 367 } 368 std::printf ("%-20s %-7s %6d %7d %5d %9.2f %9.1f %8.2f %d,%d,%d,%d\n" ,s .name , ref::type_name (s .type ),s .n_in , 369s .n_out ,nt ,best * 1e6 ,mb /best /1e9 ,mb /1e6 ,bc .L ,bc .R ,bc .S ,bc .K ); 370 } 371cx .free (W ); 372cx .free (x ); 373cx .free (y ); 374cx .free (xp ); 375cx .free (xh ); 376cx .free (yr ); 377 } 378if (worst16 >=0 ) {// GEMV_F16: half products of 8, float scales - ~1e-3 of the fp32 output 379 std::printf ("fp16: worst rel L2 vs fp32 %.2e %s\n" ,worst16 ,worst16 < 4e-3 ?"PASS" :"FAIL" ); 380return worst16 < 4e-3 ?0 :1 ; 381 } 382return 0 ; 383}