lookup.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "common.h"
  2. #include "ggml.h"
  3. #include "llama.h"
  4. #include <cmath>
  5. #include <cstdint>
  6. #include <cstdio>
  7. #include <string>
  8. #include <vector>
  9. int main(int argc, char ** argv){
  10. gpt_params params;
  11. if (!gpt_params_parse(argc, argv, params)) {
  12. return 1;
  13. }
  14. // max/min n-grams size to search for in prompt
  15. const int ngram_max = 4;
  16. const int ngram_min = 1;
  17. // length of the candidate / draft sequence, if match is found
  18. const int n_draft = params.n_draft;
  19. const bool dump_kv_cache = params.dump_kv_cache;
  20. #ifndef LOG_DISABLE_LOGS
  21. log_set_target(log_filename_generator("lookup", "log"));
  22. LOG_TEE("Log start\n");
  23. log_dump_cmdline(argc, argv);
  24. #endif // LOG_DISABLE_LOGS
  25. // init llama.cpp
  26. llama_backend_init(params.numa);
  27. llama_model * model = NULL;
  28. llama_context * ctx = NULL;
  29. // load the model
  30. std::tie(model, ctx) = llama_init_from_gpt_params(params);
  31. // tokenize the prompt
  32. const bool add_bos = llama_should_add_bos_token(model);
  33. LOG("add_bos tgt: %d\n", add_bos);
  34. std::vector<llama_token> inp;
  35. inp = ::llama_tokenize(ctx, params.prompt, add_bos, true);
  36. const int max_context_size = llama_n_ctx(ctx);
  37. const int max_tokens_list_size = max_context_size - 4;
  38. if ((int) inp.size() > max_tokens_list_size) {
  39. fprintf(stderr, "%s: error: prompt too long (%d tokens, max %d)\n", __func__, (int) inp.size(), max_tokens_list_size);
  40. return 1;
  41. }
  42. fprintf(stderr, "\n\n");
  43. for (auto id : inp) {
  44. fprintf(stderr, "%s", llama_token_to_piece(ctx, id).c_str());
  45. }
  46. fflush(stderr);
  47. const int n_input = inp.size();
  48. const auto t_enc_start = ggml_time_us();
  49. llama_decode(ctx, llama_batch_get_one( inp.data(), n_input - 1, 0, 0));
  50. llama_decode(ctx, llama_batch_get_one(&inp.back(), 1, n_input - 1, 0));
  51. const auto t_enc_end = ggml_time_us();
  52. int n_predict = 0;
  53. int n_drafted = 0;
  54. int n_accept = 0;
  55. int64_t t_draft_us = 0;
  56. int n_past = inp.size();
  57. bool has_eos = false;
  58. struct llama_sampling_context * ctx_sampling = llama_sampling_init(params.sparams);
  59. std::vector<llama_token> draft;
  60. llama_batch batch_tgt = llama_batch_init(params.n_ctx, 0, 1);
  61. // debug
  62. struct llama_kv_cache_view kvc_view = llama_kv_cache_view_init(ctx, 1);
  63. const auto t_dec_start = ggml_time_us();
  64. while (true) {
  65. // debug
  66. if (dump_kv_cache) {
  67. llama_kv_cache_view_update(ctx, &kvc_view);
  68. dump_kv_cache_view_seqs(kvc_view, 40);
  69. }
  70. // print current draft sequence
  71. LOG("drafted %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx, draft).c_str());
  72. int i_dft = 0;
  73. while (true) {
  74. // sample from the target model
  75. llama_token id = llama_sampling_sample(ctx_sampling, ctx, NULL, i_dft);
  76. llama_sampling_accept(ctx_sampling, ctx, id, true);
  77. const std::string token_str = llama_token_to_piece(ctx, id);
  78. if (!params.use_color) {
  79. printf("%s", token_str.c_str());
  80. }
  81. if (id == llama_token_eos(model)) {
  82. has_eos = true;
  83. }
  84. ++n_predict;
  85. // check if the target token matches the draft
  86. if (i_dft < (int) draft.size() && id == draft[i_dft]) {
  87. LOG("the sampled target token matches the %dth drafted token (%d, '%s') - accepted\n", i_dft, id, token_str.c_str());
  88. ++n_accept;
  89. ++n_past;
  90. ++i_dft;
  91. inp.push_back(id);
  92. if (params.use_color) {
  93. // color accepted draft token
  94. printf("\033[34m%s\033[0m", token_str.c_str());
  95. fflush(stdout);
  96. }
  97. continue;
  98. }
  99. if (params.use_color) {
  100. printf("%s", token_str.c_str());
  101. }
  102. fflush(stdout);
  103. LOG("the sampled target token (%d, '%s') did not match, or we ran out of drafted tokens\n", id, token_str.c_str());
  104. draft.clear();
  105. draft.push_back(id);
  106. inp.push_back(id);
  107. break;
  108. }
  109. if ((params.n_predict > 0 && n_predict > params.n_predict) || has_eos) {
  110. break;
  111. }
  112. // KV cache management
  113. // clean the cache of draft tokens that weren't accepted
  114. llama_kv_cache_seq_rm(ctx, 0, n_past, -1);
  115. llama_batch_clear(batch_tgt);
  116. llama_batch_add(batch_tgt, draft[0], n_past, { 0 }, true);
  117. // generate n_pred tokens through prompt lookup
  118. auto prompt_lookup = [&]() -> void {
  119. const int inp_size = inp.size();
  120. for (int ngram_size = ngram_max ; ngram_size > ngram_min; --ngram_size){
  121. const llama_token * ngram = &inp[inp_size - ngram_size];
  122. for (int i = 0; i <= (int) inp_size - (ngram_size * 2); ++i) {
  123. bool match = true;
  124. for (int j = 0; j < ngram_size; ++j) {
  125. if (inp[i + j] != ngram[j]) {
  126. match = false;
  127. break;
  128. }
  129. }
  130. if (match) {
  131. const int startIdx = i + ngram_size;
  132. const int endIdx = startIdx + n_draft;
  133. if (endIdx < inp_size) {
  134. for (int j = startIdx; j < endIdx; ++j) {
  135. LOG(" - draft candidate %d: %d\n", j, inp[j]);
  136. draft.push_back(inp[j]);
  137. llama_batch_add(batch_tgt, inp[j], n_past + (j - startIdx) + 1, { 0 }, true);
  138. ++n_drafted;
  139. }
  140. return;
  141. }
  142. }
  143. }
  144. }
  145. return;
  146. };
  147. const int64_t t_start_draft_us = ggml_time_us();
  148. prompt_lookup();
  149. t_draft_us += ggml_time_us() - t_start_draft_us;
  150. llama_decode(ctx, batch_tgt);
  151. ++n_past;
  152. draft.erase(draft.begin());
  153. }
  154. auto t_dec_end = ggml_time_us();
  155. LOG_TEE("\n\n");
  156. LOG_TEE("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));
  157. LOG_TEE("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));
  158. LOG_TEE("\n");
  159. LOG_TEE("n_draft = %d\n", n_draft);
  160. LOG_TEE("n_predict = %d\n", n_predict);
  161. LOG_TEE("n_drafted = %d\n", n_drafted);
  162. LOG_TEE("t_draft = %.2f ms, %.2f us per token, %.2f tokens per second\n",
  163. t_draft_us*1e-3, 1.0f*t_draft_us/n_drafted, n_drafted/(1e-6*t_draft_us));
  164. LOG_TEE("n_accept = %d\n", n_accept);
  165. LOG_TEE("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
  166. LOG_TEE("\ntarget:\n");
  167. llama_print_timings(ctx);
  168. llama_sampling_free(ctx_sampling);
  169. llama_batch_free(batch_tgt);
  170. llama_free(ctx);
  171. llama_free_model(model);
  172. llama_backend_free();
  173. fprintf(stderr, "\n\n");
  174. return 0;
  175. }