char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
14e733e36
char/aliceai-t5-moe
1#include "arg.h" 2#include "common.h" 3#include "log.h" 4#include "ngram-cache.h" 5#include "llama.h" 6#include "ggml.h" 7 8#include <cinttypes> 9#include <clocale> 10#include <cstdint> 11#include <cstdio> 12#include <fstream> 13#include <string> 14#include <vector> 15 16int main (int argc ,char ** argv ){ 17 std::setlocale (LC_NUMERIC ,"C" ); 18 19common_params params ; 20 21common_init (); 22 23if (!common_params_parse (argc ,argv ,params ,LLAMA_EXAMPLE_LOOKUP )) { 24return 1 ; 25 } 26 27const int n_draft = params .speculative .draft .n_max ; 28 29// init llama.cpp 30llama_backend_init (); 31llama_numa_init (params .numa ); 32 33// load the model 34auto llama_init = common_init_from_params (params ); 35 36llama_context * ctx = llama_init -> context (); 37 38// tokenize the prompt 39 std::vector < llama_token > inp ; 40inp = common_tokenize (ctx ,params .prompt , true, true); 41 42common_ngram_cache ngram_cache_context ; 43common_ngram_cache ngram_cache_dynamic ; 44common_ngram_cache ngram_cache_static ; 45 46int64_t t_draft_flat_us = 0 ; 47int64_t t_draft_us = 0 ; 48 49 { 50const int64_t t_start_draft_us = ggml_time_us (); 51 52if (!params .speculative .ngram_cache .lookup_cache_static .empty ()) { 53try { 54ngram_cache_static = common_ngram_cache_load (params .speculative .ngram_cache .lookup_cache_static ); 55 }catch (std::ifstream::failure const & ) { 56LOG_ERR ("failed to open static lookup cache: %s" ,params .speculative .ngram_cache .lookup_cache_static .c_str ()); 57exit (1 ); 58 } 59 } 60 61if (!params .speculative .ngram_cache .lookup_cache_dynamic .empty ()) { 62try { 63ngram_cache_dynamic = common_ngram_cache_load (params .speculative .ngram_cache .lookup_cache_dynamic ); 64 }catch (std::ifstream::failure const & ) {}// if the file does not exist it will simply be created at the end of the program 65 } 66 67t_draft_flat_us += ggml_time_us ()- t_start_draft_us ; 68 } 69 70const int n_input = inp .size (); 71const int n_ctx = llama_n_ctx (ctx ); 72 73int n_drafted = 0 ; 74int n_accept = 0 ; 75 76const int64_t t_start_ms = ggml_time_ms (); 77 78// Iterate over input tokens in chunks of size n_ctx. 79// Each chunk is treated as if a sequential generation but with pre-determined tokens to ensure reproducibility. 80for (int i_start = 0 ;i_start + n_ctx < n_input ;i_start += n_ctx ) { 81const std::vector < llama_token > inp_slice (inp .begin ()+ i_start ,inp .begin ()+ i_start + n_ctx ); 82 std::vector < llama_token > pseudo_output ; 83pseudo_output .push_back (inp_slice [0 ]); 84 85while ((int )pseudo_output .size ()< n_ctx ) { 86// Simulate drafting and decoding from draft: 87 std::vector < llama_token > draft ; 88draft .push_back (pseudo_output .back ()); 89 90 { 91const int64_t t_start_draft_us = ggml_time_us (); 92common_ngram_cache_draft (pseudo_output ,draft ,n_draft ,LLAMA_NGRAM_MIN ,LLAMA_NGRAM_MAX ,ngram_cache_context ,ngram_cache_dynamic ,ngram_cache_static ); 93t_draft_us += ggml_time_us ()- t_start_draft_us ; 94 } 95 96n_drafted += draft .size ()- 1 ; 97 98for (size_t j = 1 ;j < draft .size ()&& (int )pseudo_output .size ()< n_ctx ;++ j ) { 99const llama_token ground_truth = inp_slice [pseudo_output .size ()]; 100const llama_token drafted = draft [j ]; 101 102if (ground_truth != drafted ) { 103break ; 104 } 105 106++ n_accept ; 107pseudo_output .push_back (ground_truth ); 108 109 { 110const int64_t t_start_draft_us = ggml_time_us (); 111common_ngram_cache_update (ngram_cache_context ,LLAMA_NGRAM_MIN ,LLAMA_NGRAM_MAX ,pseudo_output ,1 , false); 112t_draft_us += ggml_time_us ()- t_start_draft_us ; 113 } 114 } 115 116// After each simulated batch decoding simulate the sampling of a single token: 117if ((int )pseudo_output .size ()< n_ctx ) { 118pseudo_output .push_back (inp_slice [pseudo_output .size ()]); 119 { 120const int64_t t_start_draft_us = ggml_time_us (); 121common_ngram_cache_update (ngram_cache_context ,LLAMA_NGRAM_MIN ,LLAMA_NGRAM_MAX ,pseudo_output ,1 , false); 122t_draft_us += ggml_time_us ()- t_start_draft_us ; 123 } 124 } 125 126draft .erase (draft .begin ()); 127 128 } 129if (i_start > 0 && i_start /100000 != (i_start - n_ctx ) /100000 ) { 130const int64_t t_now_ms = ggml_time_ms (); 131const int64_t eta_ms = (n_input - i_start )* (t_now_ms - t_start_ms ) /i_start ; 132const int64_t eta_min = eta_ms / (60 * 1000 ); 133const int64_t eta_s = (eta_ms - 60 * 1000 * eta_min ) /1000 ; 134 135LOG_INF ("lookup-stats: %d/%d done, ETA: %02" PRId64 ":%02" PRId64 "\n" ,i_start ,n_input ,eta_min ,eta_s ); 136 } 137 138// After each chunk, update the dynamic ngram cache with the context ngram cache: 139common_ngram_cache_merge (ngram_cache_dynamic ,ngram_cache_context ); 140ngram_cache_context .clear (); 141 } 142 143LOG ("\n" ); 144 145LOG_INF ("\n" ); 146LOG_INF ("n_draft = %d\n" ,n_draft ); 147LOG_INF ("n_predict = %d\n" ,n_input - n_input %n_ctx ); 148LOG_INF ("n_drafted = %d\n" ,n_drafted ); 149LOG_INF ("t_draft_flat = %.2f ms\n" ,t_draft_flat_us * 1e-3 ); 150LOG_INF ("t_draft = %.2f ms, %.2f us per token, %.2f tokens per second\n" , 151t_draft_us * 1e-3 ,1.0f * t_draft_us /n_drafted ,n_drafted /(1e-6 * t_draft_us )); 152LOG_INF ("n_accept = %d\n" ,n_accept ); 153LOG_INF ("accept = %.3f%%\n" ,100.0f * n_accept /n_drafted ); 154 155llama_backend_free (); 156 157LOG ("\n\n" ); 158 159return 0 ; 160}