char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
41361c859
char/aliceai-t5-moe
1#include "arg.h" 2#include "common.h" 3#include "log.h" 4#include "llama.h" 5 6#include <cmath> 7#include <cstdio> 8#include <cstring> 9#include <string> 10#include <thread> 11#include <vector> 12 13static void print_usage (int /*argc*/ ,char ** argv ) { 14printf ("\nexample usage:\n" ); 15printf ("\n %s -m model.gguf [-ngl n_gpu_layers]\n" ,argv [0 ]); 16printf ("\n" ); 17} 18 19int main (int argc ,char ** argv ) { 20common_params params ; 21 22common_init (); 23 24if (!common_params_parse (argc ,argv ,params ,LLAMA_EXAMPLE_COMMON ,print_usage )) { 25return 1 ; 26 } 27 28// init LLM 29 30llama_backend_init (); 31llama_numa_init (params .numa ); 32 33// initialize the model 34 35llama_model_params model_params = common_model_params_to_llama (params ); 36 37llama_model * model = llama_model_load_from_file (params .model .path .c_str (),model_params ); 38 39if (model == NULL ) { 40LOG_ERR ("%s: error: unable to load model\n" ,__func__ ); 41return 1 ; 42 } 43 44const llama_vocab * vocab = llama_model_get_vocab (model ); 45 46// we need just a dummy token to evaluate 47 std::vector < llama_token > prompt_tokens (1 ,llama_vocab_bos (vocab )); 48 49llama_context_params ctx_params = llama_context_default_params (); 50ctx_params .n_ctx = 512 ; 51ctx_params .n_batch = 512 ; 52ctx_params .no_perf = false; 53 54llama_context * ctx = llama_init_from_model (model ,ctx_params ); 55if (ctx == NULL ) { 56fprintf (stderr ,"%s: error: failed to create the llama_context\n" ,__func__ ); 57return 1 ; 58 } 59 60llama_batch batch = llama_batch_get_one (prompt_tokens .data (),prompt_tokens .size ()); 61 62const int n_iters = 3 ; 63 64// warm-up 65llama_decode (ctx ,batch ); 66llama_memory_clear (llama_get_memory (ctx ), true); 67llama_synchronize (ctx ); 68 69for (int64_t t_pause_ms = 0 ;t_pause_ms <=4000 ;t_pause_ms += 800 ) { 70double t_sum_us = 0.0 ; 71double t_sum2_us = 0.0 ; 72 73for (int i = 0 ;i < n_iters ;i ++ ) { 74// this pause is important - it simulates "idle GPU" 75 std::this_thread::sleep_for (std::chrono::milliseconds (t_pause_ms )); 76 77const int64_t t_start_us = llama_time_us (); 78 79// this should take constant time 80llama_decode (ctx ,batch ); 81llama_synchronize (ctx ); 82 83const int64_t t_end_us = llama_time_us (); 84 85const double t_cur_us = t_end_us - t_start_us ; 86 87#if 1 88// print individual decode times 89printf (" - decode time: %8.2f ms\n" ,t_cur_us /1000 ); 90#endif 91 92t_sum_us += t_cur_us ; 93t_sum2_us += t_cur_us * t_cur_us ; 94 95llama_memory_clear (llama_get_memory (ctx ), true); 96llama_synchronize (ctx );// just in case 97 } 98 99const double t_avg_us = t_sum_us /n_iters ; 100const double t_dev_us = sqrt ((t_sum2_us / (n_iters - 1 ))- (t_avg_us * t_avg_us * n_iters ) / (n_iters - 1 )); 101 102printf ("iters: %4d, pause: %5d ms, avg decode time: %8.2f +/- %4.2f ms\n" ,n_iters , (int )t_pause_ms ,t_avg_us /1000 ,t_dev_us /1000 ); 103fflush (stdout ); 104 } 105 106llama_free (ctx ); 107llama_model_free (model ); 108 109return 0 ; 110}