lookup-stats.cpp 5.6 KB

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