lookup-stats.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "arg.h"
  2. #include "common.h"
  3. #include "log.h"
  4. #include "ngram-cache.h"
  5. #include "llama.h"
  6. #include "ggml.h"
  7. #include <cstdint>
  8. #include <cstdio>
  9. #include <cinttypes>
  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. const int n_draft = params.n_draft;
  20. // init llama.cpp
  21. llama_backend_init();
  22. llama_numa_init(params.numa);
  23. // load the model
  24. common_init_result llama_init = common_init_from_params(params);
  25. llama_model * model = llama_init.model;
  26. llama_context * ctx = llama_init.context;
  27. // tokenize the prompt
  28. std::vector<llama_token> inp;
  29. inp = common_tokenize(ctx, params.prompt, true, true);
  30. common_ngram_cache ngram_cache_context;
  31. common_ngram_cache ngram_cache_dynamic;
  32. common_ngram_cache ngram_cache_static;
  33. int64_t t_draft_flat_us = 0;
  34. int64_t t_draft_us = 0;
  35. {
  36. const int64_t t_start_draft_us = ggml_time_us();
  37. if (!params.lookup_cache_static.empty()) {
  38. try {
  39. ngram_cache_static = common_ngram_cache_load(params.lookup_cache_static);
  40. } catch (std::ifstream::failure const &) {
  41. LOG_ERR("failed to open static lookup cache: %s", params.lookup_cache_static.c_str());
  42. exit(1);
  43. }
  44. }
  45. if (!params.lookup_cache_dynamic.empty()) {
  46. try {
  47. ngram_cache_dynamic = common_ngram_cache_load(params.lookup_cache_dynamic);
  48. } catch (std::ifstream::failure const &) {} // if the file does not exist it will simply be created at the end of the program
  49. }
  50. t_draft_flat_us += ggml_time_us() - t_start_draft_us;
  51. }
  52. const int n_input = inp.size();
  53. const int n_ctx = llama_n_ctx(ctx);
  54. int n_drafted = 0;
  55. int n_accept = 0;
  56. const int64_t t_start_ms = ggml_time_ms();
  57. // Iterate over input tokens in chunks of size n_ctx.
  58. // Each chunk is treated as if a sequential generation but with pre-determined tokens to ensure reproducibility.
  59. for (int i_start = 0; i_start + n_ctx < n_input; i_start += n_ctx) {
  60. const std::vector<llama_token> inp_slice(inp.begin() + i_start, inp.begin() + i_start + n_ctx);
  61. std::vector<llama_token> pseudo_output;
  62. pseudo_output.push_back(inp_slice[0]);
  63. while ((int) pseudo_output.size() < n_ctx) {
  64. // Simulate drafting and decoding from draft:
  65. std::vector<llama_token> draft;
  66. draft.push_back(pseudo_output.back());
  67. {
  68. const int64_t t_start_draft_us = ggml_time_us();
  69. common_ngram_cache_draft(pseudo_output, draft, n_draft, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, ngram_cache_context, ngram_cache_dynamic, ngram_cache_static);
  70. t_draft_us += ggml_time_us() - t_start_draft_us;
  71. }
  72. n_drafted += draft.size() - 1;
  73. for (size_t j = 1; j < draft.size() && (int) pseudo_output.size() < n_ctx; ++j) {
  74. const llama_token ground_truth = inp_slice[pseudo_output.size()];
  75. const llama_token drafted = draft[j];
  76. if (ground_truth != drafted) {
  77. break;
  78. }
  79. ++n_accept;
  80. pseudo_output.push_back(ground_truth);
  81. {
  82. const int64_t t_start_draft_us = ggml_time_us();
  83. common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, pseudo_output, 1, false);
  84. t_draft_us += ggml_time_us() - t_start_draft_us;
  85. }
  86. }
  87. // After each simulated batch decoding simulate the sampling of a single token:
  88. if ((int) pseudo_output.size() < n_ctx) {
  89. pseudo_output.push_back(inp_slice[pseudo_output.size()]);
  90. {
  91. const int64_t t_start_draft_us = ggml_time_us();
  92. common_ngram_cache_update(ngram_cache_context, LLAMA_NGRAM_MIN, LLAMA_NGRAM_MAX, pseudo_output, 1, false);
  93. t_draft_us += ggml_time_us() - t_start_draft_us;
  94. }
  95. }
  96. draft.erase(draft.begin());
  97. }
  98. if (i_start > 0 && i_start / 100000 != (i_start - n_ctx) / 100000) {
  99. const int64_t t_now_ms = ggml_time_ms();
  100. const int64_t eta_ms = (n_input - i_start) * (t_now_ms - t_start_ms) / i_start;
  101. const int64_t eta_min = eta_ms / (60*1000);
  102. const int64_t eta_s = (eta_ms - 60*1000*eta_min) / 1000;
  103. LOG_INF("lookup-stats: %d/%d done, ETA: %02" PRId64 ":%02" PRId64 "\n", i_start, n_input, eta_min, eta_s);
  104. }
  105. // After each chunk, update the dynamic ngram cache with the context ngram cache:
  106. common_ngram_cache_merge(ngram_cache_dynamic, ngram_cache_context);
  107. ngram_cache_context.clear();
  108. }
  109. LOG("\n");
  110. LOG_INF("\n");
  111. LOG_INF("n_draft = %d\n", n_draft);
  112. LOG_INF("n_predict = %d\n", n_input - n_input % n_ctx);
  113. LOG_INF("n_drafted = %d\n", n_drafted);
  114. LOG_INF("t_draft_flat = %.2f ms\n", t_draft_flat_us*1e-3);
  115. LOG_INF("t_draft = %.2f ms, %.2f us per token, %.2f tokens per second\n",
  116. t_draft_us*1e-3, 1.0f*t_draft_us/n_drafted, n_drafted/(1e-6*t_draft_us));
  117. LOG_INF("n_accept = %d\n", n_accept);
  118. LOG_INF("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
  119. llama_free(ctx);
  120. llama_free_model(model);
  121. llama_backend_free();
  122. LOG("\n\n");
  123. return 0;
  124. }