lookup.cpp 8.1 KB

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