lookup-stats.cpp 5.7 KB

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