lookup.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "ggml.h"
  2. #include "llama.h"
  3. #include "common.h"
  4. #include "ngram-cache.h"
  5. #include <cmath>
  6. #include <cstdint>
  7. #include <cstdio>
  8. #include <fstream>
  9. #include <string>
  10. #include <vector>
  11. #include <unordered_map>
  12. int main(int argc, char ** argv){
  13. gpt_params params;
  14. if (!gpt_params_parse(argc, argv, params)) {
  15. return 1;
  16. }
  17. // max. number of additional tokens to draft if match is found
  18. const int n_draft = params.n_draft;
  19. const bool dump_kv_cache = params.dump_kv_cache;
  20. #ifndef LOG_DISABLE_LOGS
  21. log_set_target(log_filename_generator("lookup", "log"));
  22. LOG_TEE("Log start\n");
  23. log_dump_cmdline(argc, argv);
  24. #endif // LOG_DISABLE_LOGS
  25. // init llama.cpp
  26. llama_backend_init();
  27. llama_numa_init(params.numa);
  28. llama_model * model = NULL;
  29. llama_context * ctx = NULL;
  30. // load the model
  31. std::tie(model, ctx) = llama_init_from_gpt_params(params);
  32. llama_set_rng_seed(ctx, params.seed);
  33. GGML_ASSERT(llama_n_vocab(model) < (1 << 16));
  34. // tokenize the prompt
  35. const bool add_bos = llama_should_add_bos_token(model);
  36. LOG("add_bos tgt: %d\n", add_bos);
  37. std::vector<llama_token> inp;
  38. inp = ::llama_tokenize(ctx, params.prompt, add_bos, true);
  39. llama_ngram_cache ngram_cache_context;
  40. llama_ngram_cache ngram_cache_dynamic;
  41. llama_ngram_cache ngram_cache_static;
  42. int64_t t_draft_flat_us = 0;
  43. int64_t t_draft_us = 0;
  44. {
  45. // Fill up context ngram cache with tokens from user input:
  46. const int64_t t_start_draft_us = ggml_time_us();
  47. llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, inp.size(), false);
  48. if (!params.lookup_cache_static.empty()) {
  49. try {
  50. ngram_cache_static = llama_ngram_cache_load(params.lookup_cache_static);
  51. } catch (std::ifstream::failure const &) {
  52. fprintf(stderr, "error: failed to open static lookup cache: %s", params.lookup_cache_static.c_str());
  53. exit(1);
  54. }
  55. }
  56. if (!params.lookup_cache_dynamic.empty()) {
  57. try {
  58. ngram_cache_dynamic = llama_ngram_cache_load(params.lookup_cache_dynamic);
  59. } catch (std::ifstream::failure const &) {} // if the file does not exist it will simply be created at the end of the program
  60. }
  61. t_draft_flat_us += ggml_time_us() - t_start_draft_us;
  62. }
  63. const int max_context_size = llama_n_ctx(ctx);
  64. const int max_tokens_list_size = max_context_size - 4;
  65. if ((int) inp.size() > max_tokens_list_size) {
  66. fprintf(stderr, "%s: error: prompt too long (%d tokens, max %d)\n", __func__, (int) inp.size(), max_tokens_list_size);
  67. return 1;
  68. }
  69. fprintf(stderr, "\n\n");
  70. for (auto id : inp) {
  71. fprintf(stderr, "%s", llama_token_to_piece(ctx, id).c_str());
  72. }
  73. fflush(stderr);
  74. const int n_input = inp.size();
  75. const auto t_enc_start = ggml_time_us();
  76. llama_decode(ctx, llama_batch_get_one( inp.data(), n_input - 1, 0, 0));
  77. llama_decode(ctx, llama_batch_get_one(&inp.back(), 1, n_input - 1, 0));
  78. const auto t_enc_end = ggml_time_us();
  79. int n_predict = 0;
  80. int n_drafted = 0;
  81. int n_accept = 0;
  82. int n_past = inp.size();
  83. bool has_eos = false;
  84. struct llama_sampling_context * ctx_sampling = llama_sampling_init(params.sparams);
  85. std::vector<llama_token> draft;
  86. llama_batch batch_tgt = llama_batch_init(params.n_ctx, 0, 1);
  87. // debug
  88. struct llama_kv_cache_view kvc_view = llama_kv_cache_view_init(ctx, 1);
  89. const auto t_dec_start = ggml_time_us();
  90. while (true) {
  91. // debug
  92. if (dump_kv_cache) {
  93. llama_kv_cache_view_update(ctx, &kvc_view);
  94. dump_kv_cache_view_seqs(kvc_view, 40);
  95. }
  96. // print current draft sequence
  97. LOG("drafted %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, draft).c_str());
  98. int i_dft = 0;
  99. while (true) {
  100. // sample from the target model
  101. llama_token id = llama_sampling_sample(ctx_sampling, ctx, NULL, i_dft);
  102. llama_sampling_accept(ctx_sampling, ctx, id, true);
  103. const std::string token_str = llama_token_to_piece(ctx, id);
  104. if (!params.use_color) {
  105. printf("%s", token_str.c_str());
  106. }
  107. if (id == llama_token_eos(model)) {
  108. has_eos = true;
  109. }
  110. ++n_predict;
  111. // check if the target token matches the draft
  112. if (i_dft < (int) draft.size() && id == draft[i_dft]) {
  113. LOG("the sampled target token matches the %dth drafted token (%d, '%s') - accepted\n", i_dft, id, token_str.c_str());
  114. ++n_accept;
  115. ++n_past;
  116. ++i_dft;
  117. inp.push_back(id);
  118. {
  119. // Update context ngram cache with the newly accepted token:
  120. const int64_t t_start_draft_us = ggml_time_us();
  121. llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
  122. t_draft_us += ggml_time_us() - t_start_draft_us;
  123. }
  124. if (params.use_color) {
  125. // color accepted draft token
  126. printf("\033[34m%s\033[0m", token_str.c_str());
  127. fflush(stdout);
  128. }
  129. continue;
  130. }
  131. if (params.use_color) {
  132. printf("%s", token_str.c_str());
  133. }
  134. fflush(stdout);
  135. LOG("the sampled target token (%d, '%s') did not match, or we ran out of drafted tokens\n", id, token_str.c_str());
  136. draft.clear();
  137. draft.push_back(id);
  138. inp.push_back(id);
  139. {
  140. // Update context ngram cache with the newly accepted token:
  141. const int64_t t_start_draft_us = ggml_time_us();
  142. llama_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
  143. t_draft_us += ggml_time_us() - t_start_draft_us;
  144. }
  145. break;
  146. }
  147. if ((params.n_predict > 0 && n_predict > params.n_predict) || has_eos) {
  148. break;
  149. }
  150. // KV cache management
  151. // clean the cache of draft tokens that weren't accepted
  152. llama_kv_cache_seq_rm(ctx, 0, n_past, -1);
  153. llama_batch_clear(batch_tgt);
  154. llama_batch_add(batch_tgt, draft[0], n_past, { 0 }, true);
  155. // Draft already contains a single token sampled from the model:
  156. GGML_ASSERT(draft.size() == 1);
  157. GGML_ASSERT(draft[0] == inp.back());
  158. const int64_t t_start_draft_us = ggml_time_us();
  159. llama_ngram_cache_draft(inp, draft, n_draft, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, ngram_cache_context, ngram_cache_dynamic, ngram_cache_static);
  160. for (size_t i = 1; i < draft.size(); ++i) {
  161. llama_batch_add(batch_tgt, draft[i], n_past + i, { 0 }, true);
  162. }
  163. t_draft_us += ggml_time_us() - t_start_draft_us;
  164. n_drafted += draft.size() - 1;
  165. llama_decode(ctx, batch_tgt);
  166. ++n_past;
  167. draft.erase(draft.begin());
  168. }
  169. auto t_dec_end = ggml_time_us();
  170. // Update dynamic ngram cache with context ngram cache and save it to disk:
  171. llama_ngram_cache_merge(ngram_cache_dynamic, ngram_cache_context);
  172. llama_ngram_cache_save(ngram_cache_dynamic, params.lookup_cache_dynamic);
  173. LOG_TEE("\n\n");
  174. LOG_TEE("encoded %4d tokens in %8.3f seconds, speed: %8.3f t/s\n", n_input, (t_enc_end - t_enc_start) / 1e6f, inp.size() / ((t_enc_end - t_enc_start) / 1e6f));
  175. LOG_TEE("decoded %4d tokens in %8.3f seconds, speed: %8.3f t/s\n", n_predict, (t_dec_end - t_dec_start) / 1e6f, n_predict / ((t_dec_end - t_dec_start) / 1e6f));
  176. LOG_TEE("\n");
  177. LOG_TEE("n_draft = %d\n", n_draft);
  178. LOG_TEE("n_predict = %d\n", n_predict);
  179. LOG_TEE("n_drafted = %d\n", n_drafted);
  180. LOG_TEE("t_draft_flat = %.2f ms\n", t_draft_flat_us*1e-3);
  181. LOG_TEE("t_draft = %.2f ms, %.2f us per token, %.2f tokens per second\n",
  182. t_draft_us*1e-3, 1.0f*t_draft_us/n_drafted, n_drafted/(1e-6*t_draft_us));
  183. LOG_TEE("n_accept = %d\n", n_accept);
  184. LOG_TEE("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
  185. LOG_TEE("\ntarget:\n");
  186. llama_print_timings(ctx);
  187. llama_sampling_free(ctx_sampling);
  188. llama_batch_free(batch_tgt);
  189. llama_free(ctx);
  190. llama_free_model(model);
  191. llama_backend_free();
  192. fprintf(stderr, "\n\n");
  193. return 0;
  194. }