lookup.cpp 8.4 KB

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