lookup.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // init llama.cpp
  22. llama_backend_init();
  23. llama_numa_init(params.numa);
  24. // load the model
  25. common_init_result llama_init = common_init_from_params(params);
  26. llama_model * model = llama_init.model.get();
  27. llama_context * ctx = llama_init.context.get();
  28. const llama_vocab * vocab = llama_model_get_vocab(model);
  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));
  70. llama_decode(ctx, llama_batch_get_one(&inp.back(), 1));
  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.sampling);
  78. std::vector<llama_token> draft;
  79. llama_batch batch_tgt = llama_batch_init(params.n_ctx, 0, 1);
  80. const auto t_dec_start = ggml_time_us();
  81. while (true) {
  82. // print current draft sequence
  83. LOG_DBG("drafted %s\n", string_from(ctx, draft).c_str());
  84. int i_dft = 0;
  85. while (true) {
  86. // sample from the target model
  87. llama_token id = common_sampler_sample(smpl, ctx, i_dft);
  88. common_sampler_accept(smpl, id, true);
  89. const std::string token_str = common_token_to_piece(ctx, id);
  90. if (!params.use_color) {
  91. LOG("%s", token_str.c_str());
  92. }
  93. if (llama_vocab_is_eog(vocab, id)) {
  94. has_eos = true;
  95. }
  96. ++n_predict;
  97. // check if the target token matches the draft
  98. if (i_dft < (int) draft.size() && id == draft[i_dft]) {
  99. LOG_DBG("the sampled target token matches the %dth drafted token (%d, '%s') - accepted\n", i_dft, id, token_str.c_str());
  100. ++n_accept;
  101. ++n_past;
  102. ++i_dft;
  103. inp.push_back(id);
  104. {
  105. // Update context ngram cache with the newly accepted token:
  106. const int64_t t_start_draft_us = ggml_time_us();
  107. common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
  108. t_draft_us += ggml_time_us() - t_start_draft_us;
  109. }
  110. if (params.use_color) {
  111. // color accepted draft token
  112. LOG("\033[34m%s\033[0m", token_str.c_str());
  113. fflush(stdout);
  114. }
  115. continue;
  116. }
  117. if (params.use_color) {
  118. LOG("%s", token_str.c_str());
  119. }
  120. fflush(stdout);
  121. LOG_DBG("the sampled target token (%d, '%s') did not match, or we ran out of drafted tokens\n", id, token_str.c_str());
  122. draft.clear();
  123. draft.push_back(id);
  124. inp.push_back(id);
  125. {
  126. // Update context ngram cache with the newly accepted token:
  127. const int64_t t_start_draft_us = ggml_time_us();
  128. common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, inp, 1, false);
  129. t_draft_us += ggml_time_us() - t_start_draft_us;
  130. }
  131. break;
  132. }
  133. if ((params.n_predict > 0 && n_predict > params.n_predict) || has_eos) {
  134. break;
  135. }
  136. // KV cache management
  137. // clean the cache of draft tokens that weren't accepted
  138. llama_memory_seq_rm(llama_get_memory(ctx), 0, n_past, -1);
  139. common_batch_clear(batch_tgt);
  140. common_batch_add(batch_tgt, draft[0], n_past, { 0 }, true);
  141. // Draft already contains a single token sampled from the model:
  142. GGML_ASSERT(draft.size() == 1);
  143. GGML_ASSERT(draft[0] == inp.back());
  144. const int64_t t_start_draft_us = ggml_time_us();
  145. common_ngram_cache_draft(inp, draft, n_draft, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, ngram_cache_context, ngram_cache_dynamic, ngram_cache_static);
  146. for (size_t i = 1; i < draft.size(); ++i) {
  147. common_batch_add(batch_tgt, draft[i], n_past + i, { 0 }, true);
  148. }
  149. t_draft_us += ggml_time_us() - t_start_draft_us;
  150. n_drafted += draft.size() - 1;
  151. llama_decode(ctx, batch_tgt);
  152. ++n_past;
  153. draft.erase(draft.begin());
  154. }
  155. auto t_dec_end = ggml_time_us();
  156. // Update dynamic ngram cache with context ngram cache and save it to disk:
  157. common_ngram_cache_merge(ngram_cache_dynamic, ngram_cache_context);
  158. common_ngram_cache_save(ngram_cache_dynamic, params.lookup_cache_dynamic);
  159. LOG("\n\n");
  160. 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));
  161. 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));
  162. LOG_INF("\n");
  163. LOG_INF("n_draft = %d\n", n_draft);
  164. LOG_INF("n_predict = %d\n", n_predict);
  165. LOG_INF("n_drafted = %d\n", n_drafted);
  166. LOG_INF("t_draft_flat = %.2f ms\n", t_draft_flat_us*1e-3);
  167. LOG_INF("t_draft = %.2f ms, %.2f us per token, %.2f tokens per second\n",
  168. t_draft_us*1e-3, 1.0f*t_draft_us/n_drafted, n_drafted/(1e-6*t_draft_us));
  169. LOG_INF("n_accept = %d\n", n_accept);
  170. LOG_INF("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
  171. LOG_INF("\ntarget:\n\n");
  172. common_perf_print(ctx, smpl);
  173. common_sampler_free(smpl);
  174. llama_batch_free(batch_tgt);
  175. llama_backend_free();
  176. LOG("\n\n");
  177. return 0;
  178. }