char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
f280b2698
char/aliceai-t5-moe
1#include "fa-vec.h" 2#include "ggml-backend.h" 3#include "ggml.h" 4 5#include <cstdio> 6#include <cstdlib> 7#include <cstring> 8 9struct tuner_def { 10const char * name ; 11bool (* run )(ggml_backend_t ,ggml_backend_dev_t ,const tuner_opts & ); 12}; 13 14static const tuner_def k_tuners []= { 15 {"fa-vec" ,tuner_fa_vec_run }, 16}; 17 18static void usage (const char * argv0 ) { 19printf ("usage: %s <tuner> [options]\n" ,argv0 ); 20printf ("\n" ); 21printf (" offline kernel tuner for the Metal backend: sweeps a kernel's config grid and\n" ); 22printf (" prints pasteable table rows for the machine it runs on. never a pass/fail test.\n" ); 23printf ("\n" ); 24printf (" tuners:\n" ); 25printf (" fa-vec flash-attn vec (Q,NE) for ggml-metal-tuning.cpp\n" ); 26printf ("\n" ); 27printf (" options:\n" ); 28printf (" -b <name> backend device (default: first Metal device)\n" ); 29printf (" --dtype <list> restrict KV dtypes, e.g. f16,q4_0 (default: all)\n" ); 30printf (" --dk <list> restrict head sizes, e.g. 128,192 (default: all)\n" ); 31printf (" --reps <n> timed reps per candidate, odd for an exact median (default: 7)\n" ); 32printf (" --seed <n> RNG seed; per-cell seeds mix it with the shape (default: 1234)\n" ); 33printf (" --no-cooldown do not pause/re-measure on thermal drift, only warn\n" ); 34printf (" --cool-drift <f> anchor drift that triggers a cooldown (default: 0.10)\n" ); 35printf (" --cool-eps <f> anchor tolerance to consider the GPU cool again (default: 0.03)\n" ); 36printf (" --cool-max-wait <s> give up cooling a cell after this many seconds (default: 120)\n" ); 37printf (" --cool-max-retry <n> re-measure rounds per cell before giving up (default: 2)\n" ); 38printf ("\n" ); 39printf (" the table goes to stdout, all diagnostics to stderr:\n" ); 40printf (" %s fa-vec > rows.txt 2> sweep.log\n" ,argv0 ); 41} 42 43int main (int argc ,char ** argv ) { 44const char * tuner = nullptr ; 45const char * bname = nullptr ; 46tuner_opts opts ; 47 48for (int i = 1 ;i < argc ;i ++ ) { 49const char * a = argv [i ]; 50if (strcmp (a ,"-h" )== 0 || strcmp (a ,"--help" )== 0 ) { 51usage (argv [0 ]); 52return 0 ; 53 }else if (strcmp (a ,"-b" )== 0 && i + 1 < argc ) { 54bname = argv [++ i ]; 55 }else if (strcmp (a ,"--dtype" )== 0 && i + 1 < argc ) { 56opts .dtype_filter = argv [++ i ]; 57 }else if (strcmp (a ,"--dk" )== 0 && i + 1 < argc ) { 58opts .dk_filter = argv [++ i ]; 59 }else if (strcmp (a ,"--reps" )== 0 && i + 1 < argc ) { 60opts .reps = atoi (argv [++ i ]); 61 }else if (strcmp (a ,"--seed" )== 0 && i + 1 < argc ) { 62opts .seed = (unsigned )strtoul (argv [++ i ],nullptr ,10 ); 63 }else if (strcmp (a ,"--no-cooldown" )== 0 ) { 64opts .cooldown = false; 65 }else if (strcmp (a ,"--cool-drift" )== 0 && i + 1 < argc ) { 66opts .cool_drift = atof (argv [++ i ]); 67 }else if (strcmp (a ,"--cool-eps" )== 0 && i + 1 < argc ) { 68opts .cool_eps = atof (argv [++ i ]); 69 }else if (strcmp (a ,"--cool-max-wait" )== 0 && i + 1 < argc ) { 70opts .cool_max_wait = atoi (argv [++ i ]); 71 }else if (strcmp (a ,"--cool-max-retry" )== 0 && i + 1 < argc ) { 72opts .cool_max_retry = atoi (argv [++ i ]); 73 }else if (a [0 ]!= '-' && tuner == nullptr ) { 74tuner = a ; 75 }else { 76fprintf (stderr ,"error: unrecognized or incomplete argument: %s\n\n" ,a ); 77usage (argv [0 ]); 78return 1 ; 79 } 80 } 81 82if (tuner == nullptr ) { 83usage (argv [0 ]); 84return 1 ; 85 } 86if (opts .reps < 1 ) { 87fprintf (stderr ,"error: --reps must be >= 1\n" ); 88return 1 ; 89 } 90 91const tuner_def * t = nullptr ; 92for (const auto & cand :k_tuners ) { 93if (strcmp (tuner ,cand .name )== 0 ) { 94t = & cand ; 95break ; 96 } 97 } 98if (t == nullptr ) { 99fprintf (stderr ,"error: unknown tuner: %s\n\n" ,tuner ); 100usage (argv [0 ]); 101return 1 ; 102 } 103 104ggml_backend_load_all (); 105 106ggml_backend_dev_t dev = nullptr ; 107for (size_t i = 0 ;i < ggml_backend_dev_count ();i ++ ) { 108ggml_backend_dev_t d = ggml_backend_dev_get (i ); 109if (bname ) { 110if (strcmp (ggml_backend_dev_name (d ),bname )== 0 ) { 111dev = d ; 112break ; 113 } 114 }else if (strncmp (ggml_backend_dev_name (d ),"MTL" ,3 )== 0 ) { 115dev = d ; 116break ; 117 } 118 } 119 120if (dev == nullptr ) { 121fprintf (stderr ,"error: no %s device found\n" ,bname ?bname :"Metal" ); 122return 1 ; 123 } 124 125ggml_backend_t backend = ggml_backend_dev_init (dev ,nullptr ); 126if (backend == nullptr ) { 127fprintf (stderr ,"error: failed to init backend %s\n" ,ggml_backend_dev_name (dev )); 128return 1 ; 129 } 130 131fprintf (stderr ,"device: %s (%s)\n" ,ggml_backend_dev_name (dev ),ggml_backend_dev_description (dev )); 132 133const bool ok = t -> run (backend ,dev ,opts ); 134 135ggml_backend_free (backend ); 136ggml_quantize_free (); 137 138return ok ?0 :1 ; 139}