char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
1d7ab2b94
char/aliceai-t5-moe
1#include "arg.h" 2#include "common.h" 3#include "log.h" 4#include "llama.h" 5 6#include <algorithm> 7#include <clocale> 8#include <cstdio> 9#include <string> 10#include <vector> 11 12static void print_usage (int ,char ** argv ) { 13LOG ("\nexample usage:\n" ); 14LOG ("\n %s -m model.gguf -c 2048 -b 2048 -ub 512 -npp 128,256,512 -ntg 128,256 -npl 1,2,4,8,16,32 [-pps]\n" ,argv [0 ]); 15LOG ("\n" ); 16} 17 18// satisfies -Wmissing-declarations 19int llama_batched_bench (int argc ,char ** argv ); 20 21int llama_batched_bench (int argc ,char ** argv ) { 22 std::setlocale (LC_NUMERIC ,"C" ); 23 24common_params params ; 25 26common_init (); 27 28if (!common_params_parse (argc ,argv ,params ,LLAMA_EXAMPLE_BENCH ,print_usage )) { 29return 1 ; 30 } 31 32int is_pp_shared = params .is_pp_shared ; 33int is_tg_separate = params .is_tg_separate ; 34 35 std::vector < int > n_pp = params .n_pp ; 36 std::vector < int > n_tg = params .n_tg ; 37 std::vector < int > n_pl = params .n_pl ; 38 39// init LLM 40 41llama_backend_init (); 42llama_numa_init (params .numa ); 43 44// initialize the model 45 46llama_model_params model_params = common_model_params_to_llama (params ); 47 48llama_model * model = llama_model_load_from_file (params .model .path .c_str (),model_params ); 49 50if (model == NULL ) { 51fprintf (stderr ,"%s: error: unable to load model\n" ,__func__ ); 52return 1 ; 53 } 54 55llama_context_params ctx_params = common_context_params_to_llama (params ); 56 57// ensure enough sequences are available 58ctx_params .n_seq_max = n_pl .empty () ?1 :* std::max_element (n_pl .begin (),n_pl .end ()); 59 60llama_context * ctx = llama_init_from_model (model ,ctx_params ); 61 62if (ctx == NULL ) { 63fprintf (stderr ,"%s: error: failed to create the llama_context\n" ,__func__ ); 64llama_model_free (model ); 65return 1 ; 66 } 67 68const llama_vocab * vocab = llama_model_get_vocab (model ); 69const int32_t n_vocab = llama_vocab_n_tokens (vocab ); 70 71const auto get_token_rand = [n_vocab ]()-> llama_token { 72return std::rand () %n_vocab ; 73 }; 74 75auto * mem = llama_get_memory (ctx ); 76 77const int32_t n_kv_max = llama_n_ctx (ctx ); 78 79llama_batch batch = llama_batch_init (n_kv_max ,0 ,1 ); 80 81// decode in batches of ctx_params.n_batch tokens 82auto decode_helper = [](llama_context * ctx ,llama_batch & batch ,int32_t n_batch ,bool synchronize ) { 83for (int32_t i = 0 ;i < batch .n_tokens ;i += n_batch ) { 84const int32_t n_tokens = std::min (n_batch ,batch .n_tokens - i ); 85 86llama_batch batch_view = { 87n_tokens , 88batch .token + i , 89nullptr , 90batch .pos + i , 91batch .n_seq_id + i , 92batch .seq_id + i , 93batch .logits + i , 94 }; 95 96const int ret = llama_decode (ctx ,batch_view ); 97if (ret != 0 ) { 98LOG_ERR ("failed to decode the batch, n_batch = %d, ret = %d\n" ,n_batch ,ret ); 99return false; 100 } 101 102if (synchronize ) { 103llama_synchronize (ctx ); 104 } 105 } 106 107return true; 108 }; 109 110// warm up 111 { 112for (int i = 0 ;i < 16 ;++ i ) { 113common_batch_add (batch ,get_token_rand (),i , {0 }, false); 114 } 115 116if (!decode_helper (ctx ,batch ,ctx_params .n_batch , true)) { 117LOG_ERR ("%s: llama_decode() failed\n" ,__func__ ); 118llama_free (ctx ); 119llama_model_free (model ); 120return 1 ; 121 } 122 } 123 124if (!params .batched_bench_output_jsonl ) { 125LOG ("\n" ); 126LOG ("%s: n_kv_max = %d, n_batch = %d, n_ubatch = %d, flash_attn = %d, is_pp_shared = %d, is_tg_separate = %d, n_gpu_layers = %d, n_threads = %u, n_threads_batch = %u\n" ,__func__ ,n_kv_max ,params .n_batch ,params .n_ubatch ,int (params .flash_attn_type ),is_pp_shared ,is_tg_separate ,params .n_gpu_layers ,ctx_params .n_threads ,ctx_params .n_threads_batch ); 127LOG ("\n" ); 128LOG ("|%6s | %6s | %4s | %6s | %8s | %8s | %8s | %8s | %8s | %8s |\n" ,"PP" ,"TG" ,"B" ,"N_KV" ,"T_PP s" ,"S_PP t/s" ,"T_TG s" ,"S_TG t/s" ,"T s" ,"S t/s" ); 129LOG ("|%6s-|-%6s-|-%4s-|-%6s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|-%8s-|\n" ,"------" ,"------" ,"----" ,"------" ,"--------" ,"--------" ,"--------" ,"--------" ,"--------" ,"--------" ); 130 } 131 132for (int i_pp = 0 ;i_pp < (int )n_pp .size ();++ i_pp ) { 133for (int i_tg = 0 ;i_tg < (int )n_tg .size ();++ i_tg ) { 134for (int i_pl = 0 ;i_pl < (int )n_pl .size ();++ i_pl ) { 135const int pp = n_pp [i_pp ]; 136const int tg = n_tg [i_tg ]; 137const int pl = n_pl [i_pl ]; 138 139const int n_ctx_req = is_pp_shared ? (params .kv_unified ?pp :pl * pp )+ pl * tg :pl * (pp + tg ); 140 141if (n_ctx_req > n_kv_max ) { 142continue ; 143 } 144 145common_batch_clear (batch ); 146 147for (int j = 0 ;j < (is_pp_shared ?1 :pl );++ j ) { 148for (int i = 0 ;i < pp ;++ i ) { 149common_batch_add (batch ,get_token_rand (),i , {j },i == pp - 1 ); 150 } 151 } 152 153llama_memory_clear (mem , false); 154 155const auto t_pp_start = ggml_time_us (); 156 157if (!decode_helper (ctx ,batch ,ctx_params .n_batch , false)) { 158LOG_ERR ("%s: llama_decode() failed\n" ,__func__ ); 159llama_free (ctx ); 160llama_model_free (model ); 161return 1 ; 162 } 163 164llama_synchronize (ctx ); 165 166const auto t_pp_end = ggml_time_us (); 167 168if (is_pp_shared ) { 169for (int32_t i = 1 ;i < pl ;++ i ) { 170llama_memory_seq_cp (mem ,0 ,i ,-1 ,-1 ); 171 } 172 173if (!params .kv_unified ) { 174// run one dummy token to apply the memory copy 175common_batch_clear (batch ); 176common_batch_add (batch ,get_token_rand (),pp + 0 , {0 }, true); 177if (!decode_helper (ctx ,batch ,ctx_params .n_batch , true)) { 178LOG_ERR ("%s: llama_decode() failed\n" ,__func__ ); 179llama_free (ctx ); 180llama_model_free (model ); 181return 1 ; 182 } 183llama_memory_seq_rm (mem ,0 ,pp ,-1 ); 184 } 185 } 186 187const auto t_tg_start = ggml_time_us (); 188 189if (is_tg_separate ) { 190// decode pattern: 191// 0 0 0 ... 1 1 1 ... 2 2 2 ... 3 3 3 ... 192for (int j = 0 ;j < pl ;++ j ) { 193for (int i = 0 ;i < tg ;++ i ) { 194common_batch_clear (batch ); 195 196common_batch_add (batch ,get_token_rand (),pp + i , {j }, true); 197 198if (!decode_helper (ctx ,batch ,ctx_params .n_batch , true)) { 199LOG_ERR ("%s: llama_decode() failed\n" ,__func__ ); 200llama_free (ctx ); 201llama_model_free (model ); 202return 1 ; 203 } 204 } 205 } 206 }else { 207// decode pattern: 208// 0123 0123 0123 ... 209for (int i = 0 ;i < tg ;++ i ) { 210common_batch_clear (batch ); 211 212for (int j = 0 ;j < pl ;++ j ) { 213common_batch_add (batch ,get_token_rand (),pp + i , {j }, true); 214 } 215 216if (!decode_helper (ctx ,batch ,ctx_params .n_batch , true)) { 217LOG_ERR ("%s: llama_decode() failed\n" ,__func__ ); 218llama_free (ctx ); 219llama_model_free (model ); 220return 1 ; 221 } 222 } 223 } 224 225const auto t_tg_end = ggml_time_us (); 226 227const int32_t n_kv = n_ctx_req ; 228 229const float t_pp = (t_pp_end - t_pp_start ) /1000000.0f ; 230const float t_tg = (t_tg_end - t_tg_start ) /1000000.0f ; 231const float t = t_pp + t_tg ; 232 233const float speed_pp = is_pp_shared ?pp /t_pp :pl * pp /t_pp ; 234const float speed_tg = pl * tg /t_tg ; 235const float speed = ((is_pp_shared ?pp :pl * pp )+ pl * tg ) /t ; 236 237if (params .batched_bench_output_jsonl ) { 238LOG ( 239"{\"n_kv_max\": %d, \"n_batch\": %d, \"n_ubatch\": %d, \"flash_attn\": %d, \"is_pp_shared\": %d, \"n_gpu_layers\": %d, \"n_threads\": %u, \"n_threads_batch\": %u, " 240"\"pp\": %d, \"tg\": %d, \"pl\": %d, \"n_kv\": %d, \"t_pp\": %f, \"speed_pp\": %f, \"t_tg\": %f, \"speed_tg\": %f, \"t\": %f, \"speed\": %f}\n" , 241n_kv_max ,params .n_batch ,params .n_ubatch ,int (params .flash_attn_type ),params .is_pp_shared ,params .n_gpu_layers ,ctx_params .n_threads ,ctx_params .n_threads_batch , 242pp ,tg ,pl ,n_kv ,t_pp ,speed_pp ,t_tg ,speed_tg ,t ,speed 243 ); 244 }else { 245LOG ("|%6d | %6d | %4d | %6d | %8.3f | %8.2f | %8.3f | %8.2f | %8.3f | %8.2f |\n" ,pp ,tg ,pl ,n_kv ,t_pp ,speed_pp ,t_tg ,speed_tg ,t ,speed ); 246 } 247 } 248 } 249 } 250 251LOG ("\n" ); 252llama_perf_context_print (ctx ); 253 254llama_batch_free (batch ); 255 256llama_free (ctx ); 257llama_model_free (model ); 258 259llama_backend_free (); 260 261return 0 ; 262}