lookup.cpp 8.1 KB

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