lookup-stats.cpp 5.5 KB

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