char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
ca86fb222
char/aliceai-t5-moe
1#include "ggml.h" 2#include "llama.h" 3 4#ifdef NDEBUG 5#undef NDEBUG 6#endif 7 8#include <algorithm> 9#include <cmath> 10#include <cstdlib> 11#include <string> 12#include <vector> 13 14extern struct llama_sampler * llama_sampler_init_dry_testing (float dry_multiplier ,float dry_base ,int32_t dry_allowed_length ,int32_t dry_penalty_last_n ,const std::vector < std::vector < llama_token >>& seq_breakers ); 15 16static void dump (const llama_token_data_array * cur_p ) { 17for (size_t i = 0 ;i < cur_p -> size ;i ++ ) { 18printf ("%d: %f (%f)\n" ,cur_p -> data [i ].id ,cur_p -> data [i ].p ,cur_p -> data [i ].logit ); 19 } 20} 21 22#define DUMP (__cur_p ) do { printf("%s:%d (%s)\n", __FILE__, __LINE__, __func__); dump((__cur_p)); printf("-\n"); } while(0) 23 24struct sampler_tester { 25sampler_tester (size_t n_vocab ) { 26cur .reserve (n_vocab ); 27for (llama_token token_id = 0 ;token_id < (llama_token )n_vocab ;token_id ++ ) { 28const float logit = logf (token_id ); 29cur .emplace_back (llama_token_data {token_id ,logit ,0.0f }); 30 } 31 32cur_p = llama_token_data_array {cur .data (),cur .size (),-1 , false }; 33 } 34 35sampler_tester (const std::vector < float > & probs ,const std::vector < float > & probs_expected ) :probs_expected (probs_expected ) { 36cur .reserve (probs .size ()); 37for (llama_token token_id = 0 ;token_id < (llama_token )probs .size ();token_id ++ ) { 38const float logit = logf (probs [token_id ]); 39cur .emplace_back (llama_token_data {token_id ,logit ,probs [token_id ]}); 40 } 41 42cur_p = llama_token_data_array {cur .data (),cur .size (),-1 , false }; 43 } 44 45void apply (llama_sampler * sampler ) { 46llama_sampler_apply (sampler ,& cur_p ); 47llama_sampler_free (sampler ); 48 } 49 50void check () { 51GGML_ASSERT (cur_p .size == probs_expected .size ()); 52for (size_t i = 0 ;i < cur_p .size ;i ++ ) { 53GGML_ASSERT (fabs (cur_p .data [i ].p - probs_expected [i ])< 1e-5 ); 54 } 55 } 56 57llama_token_data_array cur_p ; 58 59private : 60const std::vector < float > probs_expected ; 61 62 std::vector < llama_token_data > cur ; 63}; 64 65static llama_token sample_dist (llama_sampler * sampler ,const std::vector < float > & logits ) { 66 std::vector < llama_token_data > cur ; 67for (llama_token token_id = 0 ;token_id < (llama_token )logits .size ();++ token_id ) { 68cur .push_back ({token_id ,logits [token_id ],0.0f }); 69 } 70 71llama_token_data_array cur_p = {cur .data (),cur .size (),-1 , false }; 72llama_sampler_apply (sampler ,& cur_p ); 73GGML_ASSERT (cur_p .selected >=0 ); 74GGML_ASSERT ((size_t )cur_p .selected < cur_p .size ); 75return cur_p .data [cur_p .selected ].id ; 76} 77 78static void test_dist_singleton_rng () { 79llama_sampler * singleton = llama_sampler_init_dist (4242 ); 80llama_sampler * control = llama_sampler_init_dist (4242 ); 81 82sample_dist (singleton , {0.0f }); 83sample_dist (control , {0.0f ,0.0f }); 84 85const std::vector < float > logits (256 ,0.0f ); 86for (int i = 0 ;i < 4 ;++ i ) { 87GGML_ASSERT (sample_dist (singleton ,logits )== sample_dist (control ,logits )); 88 } 89 90llama_sampler_free (singleton ); 91llama_sampler_free (control ); 92} 93 94static void test_temp (const std::vector < float > & probs ,const std::vector < float > & probs_expected ,float temp ) { 95sampler_tester tester (probs ,probs_expected ); 96 97DUMP (& tester .cur_p ); 98tester .apply (llama_sampler_init_temp (temp )); 99tester .apply (llama_sampler_init_dist (0 )); 100DUMP (& tester .cur_p ); 101 102tester .check (); 103} 104 105static void test_temp_ext (const std::vector < float > & probs ,const std::vector < float > & probs_expected ,float temp ,float delta ,float exponent ) { 106sampler_tester tester (probs ,probs_expected ); 107 108DUMP (& tester .cur_p ); 109tester .apply (llama_sampler_init_temp_ext (temp ,delta ,exponent )); 110tester .apply (llama_sampler_init_dist (0 )); 111DUMP (& tester .cur_p ); 112 113tester .check (); 114} 115 116static void test_top_k (const std::vector < float > & probs ,const std::vector < float > & probs_expected ,int k ) { 117sampler_tester tester (probs ,probs_expected ); 118 119DUMP (& tester .cur_p ); 120tester .apply (llama_sampler_init_top_k (k )); 121tester .apply (llama_sampler_init_dist (0 )); 122DUMP (& tester .cur_p ); 123 124tester .check (); 125} 126 127static void test_top_p (const std::vector < float > & probs ,const std::vector < float > & probs_expected ,float p ) { 128sampler_tester tester (probs ,probs_expected ); 129 130DUMP (& tester .cur_p ); 131tester .apply (llama_sampler_init_top_p (p ,0 )); 132tester .apply (llama_sampler_init_dist (0 )); 133DUMP (& tester .cur_p ); 134 135tester .check (); 136} 137 138static void test_min_p (const std::vector < float > & probs ,const std::vector < float > & probs_expected ,float p ) { 139sampler_tester tester (probs ,probs_expected ); 140 141DUMP (& tester .cur_p ); 142tester .apply (llama_sampler_init_min_p (p ,0 )); 143tester .apply (llama_sampler_init_dist (0 )); 144DUMP (& tester .cur_p ); 145 146tester .check (); 147} 148 149static void test_xtc (const std::vector < float > & probs ,const std::vector < float > & probs_expected ,float p ,float t ) { 150sampler_tester tester (probs ,probs_expected ); 151 152DUMP (& tester .cur_p ); 153tester .apply (llama_sampler_init_xtc (p ,t ,0 ,0 )); 154DUMP (& tester .cur_p ); 155 156tester .check (); 157} 158 159static void test_typical (const std::vector < float > & probs ,const std::vector < float > & probs_expected ,float p ) { 160sampler_tester tester (probs ,probs_expected ); 161 162DUMP (& tester .cur_p ); 163tester .apply (llama_sampler_init_typical (p ,0 )); 164DUMP (& tester .cur_p ); 165 166tester .check (); 167} 168 169static void test_penalties ( 170const std::vector < float > & probs ,const std::vector < llama_token > & last_tokens , 171const std::vector < float > & probs_expected ,float repeat_penalty ,float alpha_frequency ,float alpha_presence 172) { 173GGML_ASSERT (probs .size ()== probs_expected .size ()); 174 175sampler_tester tester (probs ,probs_expected ); 176 177auto * sampler = llama_sampler_init_penalties ((int32_t )probs .size (), (int32_t )last_tokens .size (),repeat_penalty ,alpha_frequency ,alpha_presence ); 178 179for (size_t i = 0 ;i < last_tokens .size ();i ++ ) { 180llama_sampler_accept (sampler ,last_tokens [i ]); 181 } 182 183DUMP (& tester .cur_p ); 184tester .apply (sampler ); 185tester .apply (llama_sampler_init_dist (0 )); 186DUMP (& tester .cur_p ); 187 188tester .check (); 189} 190 191static void test_dry ( 192const std::vector < float > & probs ,const std::vector < llama_token > & last_tokens , 193const std::vector < float > & expected_probs ,float dry_multiplier ,float dry_base , 194int dry_allowed_length ,int dry_penalty_last_n , 195const std::vector < std::vector < llama_token >> & seq_breakers 196) { 197GGML_ASSERT (probs .size ()== expected_probs .size ()); 198 199sampler_tester tester (probs ,expected_probs ); 200 201auto * sampler = llama_sampler_init_dry_testing (dry_multiplier ,dry_base ,dry_allowed_length ,dry_penalty_last_n ,seq_breakers ); 202 203for (size_t i = 0 ;i < last_tokens .size ();i ++ ) { 204llama_sampler_accept (sampler ,last_tokens [i ]); 205 } 206 207DUMP (& tester .cur_p ); 208tester .apply (sampler ); 209tester .apply (llama_sampler_init_dist (0 )); 210DUMP (& tester .cur_p ); 211tester .check (); 212} 213 214static void test_top_n_sigma (const std::vector < float > & probs ,const std::vector < float > & probs_expected ,int n ) { 215sampler_tester tester (probs ,probs_expected ); 216 217DUMP (& tester .cur_p ); 218tester .apply (llama_sampler_init_top_n_sigma (n )); 219tester .apply (llama_sampler_init_dist (0 )); 220DUMP (& tester .cur_p ); 221 222tester .check (); 223} 224 225static void test_sampler_queue (const size_t n_vocab ,const std::string & samplers_sequence ,const int top_k ,const float top_p ,const float min_p 226) { 227sampler_tester tester (n_vocab ); 228 229llama_token min_token_id = 0 ; 230const llama_token max_token_id = n_vocab - 1 ; 231 232for (auto s :samplers_sequence ) { 233switch (s ) { 234case 'k' :tester .apply (llama_sampler_init_top_k (top_k ));break ; 235case 'y' :GGML_ABORT ("typical test not implemented" ); 236case 'p' :tester .apply (llama_sampler_init_top_p (top_p ,1 ));break ; 237case 'm' :tester .apply (llama_sampler_init_min_p (min_p ,1 ));break ; 238case 't' :GGML_ABORT ("temperature test not implemented" ); 239default :GGML_ABORT ("Unknown sampler" ); 240 } 241 242tester .apply (llama_sampler_init_dist (0 )); 243 244auto & cur_p = tester .cur_p ; 245 246const int size = cur_p .size ; 247 248if (s == 'k' ) { 249const int expected_size = std::min (size ,top_k ); 250min_token_id = std::max (min_token_id , (llama_token )(n_vocab - top_k )); 251 252GGML_ASSERT (size == expected_size ); 253GGML_ASSERT (cur_p .data [0 ].id == max_token_id ); 254GGML_ASSERT (cur_p .data [expected_size - 1 ].id == min_token_id ); 255 }else if (s == 'p' ) { 256const int softmax_divisor = n_vocab * (n_vocab - 1 ) /2 - min_token_id * (min_token_id - 1 ) /2 ; 257const int softmax_numerator_target = ceilf (top_p * softmax_divisor ); 258 259min_token_id = n_vocab ; 260int expected_size = 0 ; 261int cumsum = 0 ; 262do {// do-while because always at least one token is sampled 263min_token_id -- ; 264expected_size ++ ; 265 266cumsum += min_token_id ; 267 }while (cumsum < softmax_numerator_target ); 268 269// token 0 has p == 0, need special consideration for cumsum because top_p immediately returns 270if (min_token_id == 1 ) { 271min_token_id -- ; 272expected_size += 1 ; 273 } 274 275GGML_ASSERT (size == expected_size ); 276GGML_ASSERT (!cur_p .sorted || cur_p .data [0 ].id == max_token_id ); 277GGML_ASSERT (!cur_p .sorted || cur_p .data [expected_size - 1 ].id == min_token_id ); 278 }else if (s == 'm' ) { 279int expected_size = ceilf ((1.0f - min_p )* n_vocab ); 280expected_size = std::max (expected_size ,1 ); 281expected_size = std::min (expected_size ,size ); 282 283min_token_id = floorf (min_p * n_vocab ); 284min_token_id = std::max (min_token_id ,1 ); 285min_token_id = std::max (min_token_id , (llama_token )(n_vocab - size )); 286min_token_id = std::min (min_token_id , (llama_token )(n_vocab - 1 )); 287 288GGML_ASSERT (size == expected_size ); 289GGML_ASSERT (!cur_p .sorted || cur_p .data [0 ].id == max_token_id ); 290GGML_ASSERT (!cur_p .sorted || cur_p .data [expected_size - 1 ].id == min_token_id ); 291 }else { 292GGML_ABORT ("fatal error" ); 293 } 294 } 295 296printf ("Sampler queue %3s OK with n_vocab=%05zu top_k=%5d top_p=%f min_p=%f\n" , 297samplers_sequence .c_str (),n_vocab ,top_k ,top_p ,min_p ); 298} 299 300static void bench (llama_sampler * cnstr ,const char * cnstr_name ,const std::vector < llama_token_data > & data ,int n_iter ) { 301 std::vector < llama_token_data > cur (data .size ()); 302 std::copy (data .begin (),data .end (),cur .begin ()); 303llama_token_data_array cur_p = {cur .data (),cur .size (),-1 , false }; 304llama_sampler_apply (cnstr ,& cur_p ); 305llama_sampler_reset (cnstr ); 306const int64_t t_start = ggml_time_us (); 307for (int i = 0 ;i < n_iter ;i ++ ) { 308 std::copy (data .begin (),data .end (),cur .begin ()); 309llama_token_data_array cur_p = {cur .data (),cur .size (),-1 , false }; 310llama_sampler_apply (cnstr ,& cur_p ); 311llama_sampler_reset (cnstr ); 312 } 313const int64_t t_end = ggml_time_us (); 314llama_sampler_free (cnstr ); 315printf ("%-43s: %8.3f us/iter\n" ,cnstr_name , (t_end - t_start ) / (float )n_iter ); 316} 317 318#define BENCH (__cnstr ,__data ,__n_iter ) bench((__cnstr), #__cnstr, (__data), (__n_iter)) 319 320static void test_perf () { 321const int n_vocab = 1 <<17 ; 322 323 std::vector < llama_token_data > data ; 324 325data .reserve (n_vocab ); 326for (int i = 0 ;i < n_vocab ;i ++ ) { 327const float logit = 2.0f * ((double )(rand ())/RAND_MAX - 0.5 ); 328data .emplace_back (llama_token_data {i ,logit ,0.0f }); 329 } 330 331BENCH (llama_sampler_init_top_k (40 ),data ,32 ); 332BENCH (llama_sampler_init_top_p (0.8f ,1 ),data ,32 ); 333BENCH (llama_sampler_init_min_p (0.2f ,1 ),data ,32 ); 334BENCH (llama_sampler_init_typical (0.5f ,1 ),data ,32 ); 335BENCH (llama_sampler_init_xtc (1.0f ,0.1f ,1 ,1 ),data ,32 ); 336} 337 338int main (void ) { 339ggml_time_init (); 340 341test_dist_singleton_rng (); 342 343test_temp ({0.1f ,0.2f ,0.3f ,0.4f }, {0.1f ,0.2f ,0.3f ,0.4f },1.0f ); 344test_temp ({0.1f ,0.2f ,0.3f ,0.4f }, {0.0f ,0.0f ,0.0f ,1.0f },0.0f ); 345 346test_temp_ext ({0.1f ,0.2f ,0.3f ,0.4f }, {0.1f ,0.2f ,0.3f ,0.4f },1.0f ,0.0f ,1.0f ); 347test_temp_ext ({0.1f ,0.2f ,0.3f ,0.4f }, {0.0f ,0.0f ,0.0f ,1.0f },0.0f ,0.0f ,1.0f ); 348 349test_top_k ({0.1f ,0.2f ,0.3f ,0.4f }, {1.0f },1 ); 350test_top_k ({0.1f ,0.2f ,0.3f ,0.4f }, {0.44444f ,0.33333f ,0.22222f },3 ); 351test_top_k ({0.1f ,0.2f ,0.3f ,0.4f }, {0.4f ,0.3f ,0.2f ,0.1f },4 ); 352test_top_k ({0.1f ,0.2f ,0.3f ,0.4f }, {0.1f ,0.2f ,0.3f ,0.4f },0 ); 353 354test_top_p ({0.1f ,0.2f ,0.3f ,0.4f }, {1.0f },0 ); 355test_top_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.571429f ,0.428571f },0.7f ); 356test_top_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.44444f ,0.33333f ,0.22222f },0.8f ); 357test_top_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.1f ,0.2f ,0.3f ,0.4f },1.0f ); 358 359test_min_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.1f /1.0f ,0.2f /1.0f ,0.3f /1.0f ,0.4f /1.0f },0.00f ); 360test_min_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.1f /1.0f ,0.2f /1.0f ,0.3f /1.0f ,0.4f /1.0f },0.24f ); 361test_min_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.2f /0.9f ,0.3f /0.9f ,0.4f /0.9f },0.26f ); 362test_min_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.2f /0.9f ,0.3f /0.9f ,0.4f /0.9f },0.49f ); 363test_min_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.3f /0.7f ,0.4f /0.7f },0.51f ); 364test_min_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.3f /0.7f ,0.4f /0.7f },0.74f ); 365test_min_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.4f /0.4f },0.76f ); 366test_min_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.4f /0.4f },1.00f ); 367test_min_p ({0.1f ,0.2f ,0.3f ,0.4f }, {0.4f /0.4f },1.05f ); 368 369printf ("XTC should:\n" ); 370test_xtc ({0.4f ,0.3f ,0.2f ,0.1f }, {0.1f },0.99f ,0.09f ); 371test_xtc ({0.4f ,0.3f ,0.2f ,0.1f }, {0.2f ,0.1f },0.99f ,0.19f ); 372test_xtc ({0.4f ,0.3f ,0.2f ,0.1f }, {0.3f ,0.2f ,0.1f },0.99f ,0.29f ); 373 374printf ("XTC should not:\n" ); 375test_xtc ({0.4f ,0.3f ,0.2f ,0.1f }, {0.4f ,0.3f ,0.2f ,0.1f },0.99f ,0.39f ); 376 377test_typical ({0.97f ,0.01f ,0.01f ,0.01f }, {0.97f },0.5f ); 378test_typical ({0.4f ,0.2f ,0.2f ,0.2f }, {0.2f ,0.2f ,0.2f },0.5f ); 379 380test_penalties ({0.2f ,0.2f ,0.2f ,0.2f ,0.2f }, {0 }, {0 ,0.25f ,0.25f ,0.25f ,0.25f },50.0f ,0.0f ,0.0f ); 381test_penalties ({0.2f ,0.2f ,0.2f ,0.2f ,0.2f }, {0 ,1 ,2 }, {0 ,0 ,0 ,0.5f ,0.5f },50.0f ,0.0f ,0.0f ); 382test_penalties ({0.2f ,0.2f ,0.2f ,0.2f ,0.2f }, {0 ,1 ,2 ,0 ,0 }, {0 ,0 ,0 ,0.5f ,0.5f },50.0f ,0.0f ,0.0f ); 383 384test_penalties ({0.2f ,0.2f ,0.2f ,0.2f ,0.2f }, {0 }, {0.000011f ,0.249997f ,0.249997f ,0.249997f ,0.249997f },1.0f ,5.0f ,5.0f ); 385test_penalties ({0.2f ,0.2f ,0.2f ,0.2f ,0.2f }, {0 ,1 ,2 }, {0.000023f ,0.000023f ,0.000023f ,0.499966f ,0.499966f },1.0f ,5.0f ,5.0f ); 386test_penalties ({0.2f ,0.2f ,0.2f ,0.2f ,0.2f }, {0 ,1 ,2 ,0 ,0 }, {0.000000f ,0.000023f ,0.000023f ,0.499977f ,0.499977f },1.0f ,5.0f ,5.0f ); 387 388 389test_dry ({0.25f ,0.25f ,0.25f ,0.25f }, {0 ,1 }, {0.25f ,0.25f ,0.25f ,0.25f },1.0f ,1.1f ,2 ,4 , {}); 390test_dry ({0.25f ,0.25f ,0.25f ,0.25f }, {0 ,1 ,2 ,0 ,1 }, {0.296923f ,0.296923f ,0.109232f ,0.296923f },1.0f ,1.1f ,2 ,5 , {}); 391test_dry ({0.2f ,0.2f ,0.2f ,0.2f ,0.2f }, {0 ,1 ,3 ,4 ,0 ,1 }, {0.2f ,0.2f ,0.2f ,0.2f ,0.2f },1.0f ,1.1f ,2 ,6 , {{3 }}); 392test_dry ({0.2f ,0.2f ,0.2f ,0.2f ,0.2f }, {0 ,1 ,2 ,0 ,1 }, {0.241818f ,0.241818f ,0.032727f ,0.241818f ,0.241818f },2.0f ,1.1f ,2 ,5 , {}); 393test_dry ({0.2f ,0.2f ,0.2f ,0.2f ,0.2f }, {0 ,1 ,2 ,3 ,4 ,0 ,1 }, {0.2f ,0.2f ,0.2f ,0.2f ,0.2f },1.0f ,1.1f ,4 ,7 , {}); 394 395test_top_n_sigma ({0.1f ,0.2f ,0.3f ,0.4f }, {0.0f ,0.0f ,0.428571f ,0.571429f },1.00f ); 396test_top_n_sigma ({0.1f ,0.2f ,0.3f ,0.4f }, {0.1f ,0.2f ,0.3f ,0.4f },0.00f );// top_n_sigma == 0 now represents a no-op rather than greedy decoding as of PR#13345 397test_top_n_sigma ({0.1f ,0.2f ,0.3f ,0.4f }, {0.1f ,0.2f ,0.3f ,0.4f },3.00f ); 398 399test_sampler_queue (10000 ,"k" ,10000 ,1.0f ,1.0f ); 400test_sampler_queue (10000 ,"k" ,1 ,1.0f ,1.0f ); 401test_sampler_queue (10000 ,"p" ,10000 ,1.0f ,1.0f ); 402test_sampler_queue (10000 ,"p" ,10000 ,0.0f ,1.0f ); 403test_sampler_queue (10000 ,"m" ,10000 ,1.0f ,1.0f ); 404test_sampler_queue (10000 ,"m" ,10000 ,1.0f ,1e-12 ); 405 406test_sampler_queue (10000 ,"k" ,100 ,1.0000f ,1.0f ); 407test_sampler_queue (10000 ,"p" ,10000 ,0.0003f ,1.0f ); 408test_sampler_queue (10000 ,"p" ,10000 ,0.8000f ,1.0f ); 409test_sampler_queue (10000 ,"m" ,10000 ,1.0000f ,9997.9f /9999.0f ); 410test_sampler_queue (10000 ,"m" ,10000 ,1.0000f ,0.1f ); 411 412test_sampler_queue (10000 ,"kp" ,100 ,0.8f ,0.1f ); 413test_sampler_queue (10000 ,"km" ,100 ,0.8f ,0.1f ); 414test_sampler_queue (10000 ,"pk" ,100 ,0.8f ,0.1f ); 415test_sampler_queue (10000 ,"pm" ,100 ,0.8f ,0.1f ); 416test_sampler_queue (10000 ,"mk" ,100 ,0.8f ,0.1f ); 417test_sampler_queue (10000 ,"mp" ,100 ,0.8f ,9997.9f /9999.0f ); 418test_sampler_queue (10000 ,"mp" ,100 ,0.8f ,0.1f ); 419 420test_sampler_queue (10000 ,"kpm" ,100 ,0.8f ,0.1f ); 421test_sampler_queue (10000 ,"kmp" ,100 ,0.8f ,0.1f ); 422test_sampler_queue (10000 ,"pkm" ,100 ,0.8f ,0.1f ); 423test_sampler_queue (10000 ,"pmk" ,100 ,0.8f ,0.1f ); 424test_sampler_queue (10000 ,"mkp" ,100 ,0.8f ,0.1f ); 425test_sampler_queue (10000 ,"mpk" ,100 ,0.8f ,0.1f ); 426 427printf ("OK\n" ); 428 429test_perf (); 430 431return 0 ; 432}