speculative.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #ifndef _GNU_SOURCE
  2. #define _GNU_SOURCE
  3. #endif
  4. #include "build-info.h"
  5. #include "common.h"
  6. #include "llama.h"
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <string>
  10. #include <vector>
  11. int main(int argc, char ** argv) {
  12. gpt_params params;
  13. if (gpt_params_parse(argc, argv, params) == false) {
  14. return 1;
  15. }
  16. if (params.model_draft.empty()) {
  17. fprintf(stderr, "%s: error: --model-draft is required\n", __func__);
  18. return 1;
  19. }
  20. #ifndef LOG_DISABLE_LOGS
  21. log_set_target(log_filename_generator("speculative", "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_tgt = NULL;
  28. llama_model * model_dft = NULL;
  29. llama_context * ctx_tgt = NULL;
  30. llama_context * ctx_dft = NULL;
  31. // load the target model
  32. params.perplexity = true; // HACK: enable logits_all = true
  33. std::tie(model_tgt, ctx_tgt) = llama_init_from_gpt_params(params);
  34. // load the draft model
  35. params.model = params.model_draft;
  36. std::tie(model_dft, ctx_dft) = llama_init_from_gpt_params(params);
  37. // tokenize the prompt
  38. std::vector<llama_token> inp;
  39. inp = ::llama_tokenize(ctx_tgt, params.prompt, true);
  40. const int max_context_size = llama_n_ctx(ctx_tgt);
  41. const int max_tokens_list_size = max_context_size - 4;
  42. if ((int) inp.size() > max_tokens_list_size) {
  43. fprintf(stderr, "%s: error: prompt too long (%d tokens, max %d)\n", __func__, (int) inp.size(), max_tokens_list_size);
  44. return 1;
  45. }
  46. fprintf(stderr, "\n\n");
  47. for (auto id : inp) {
  48. fprintf(stderr, "%s", llama_token_to_piece(ctx_tgt, id).c_str());
  49. }
  50. fflush(stderr);
  51. const int n_input = inp.size();
  52. const auto t_enc_start = ggml_time_us();
  53. // eval the prompt with both models
  54. llama_eval(ctx_tgt, inp.data(), int(inp.size() - 1), 0, params.n_threads);
  55. llama_eval(ctx_tgt, &inp.back(), 1, inp.size() - 1, params.n_threads);
  56. llama_eval(ctx_dft, inp.data(), int(inp.size()), 0, params.n_threads);
  57. const auto t_enc_end = ggml_time_us();
  58. // the 2 models should have the same vocab
  59. const int n_ctx = llama_n_ctx(ctx_tgt);
  60. const int n_vocab = llama_n_vocab(ctx_tgt);
  61. //GGML_ASSERT(n_vocab == llama_n_vocab(ctx_dft));
  62. // how many tokens to draft each time
  63. const int n_draft = params.n_draft;
  64. int n_predict = 0;
  65. int n_drafted = 0;
  66. int n_accept = 0;
  67. int n_past_tgt = inp.size();
  68. int n_past_dft = inp.size();
  69. std::vector<llama_token> drafted;
  70. std::vector<llama_token> last_tokens(n_ctx);
  71. std::fill(last_tokens.begin(), last_tokens.end(), 0);
  72. for (auto & id : inp) {
  73. last_tokens.erase(last_tokens.begin());
  74. last_tokens.push_back(id);
  75. }
  76. std::vector<llama_token_data> candidates;
  77. candidates.reserve(n_vocab);
  78. // used to determine end of generation
  79. bool has_eos = false;
  80. const auto t_dec_start = ggml_time_us();
  81. while (true) {
  82. LOG("drafted: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx_dft, drafted));
  83. // sample from the drafted tokens if any
  84. int i_dft = 0;
  85. while (true) {
  86. const llama_token id = llama_sample_token(ctx_tgt, NULL, NULL, params, last_tokens, candidates, i_dft);
  87. last_tokens.erase(last_tokens.begin());
  88. last_tokens.push_back(id);
  89. //LOG("last: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx_tgt, last_tokens));
  90. const std::string token_str = llama_token_to_piece(ctx_tgt, id);
  91. printf("%s", token_str.c_str());
  92. fflush(stdout);
  93. if (id == llama_token_eos(ctx_tgt)) {
  94. has_eos = true;
  95. }
  96. ++n_predict;
  97. if (i_dft < (int) drafted.size() && id == drafted[i_dft]) {
  98. LOG("drafted token %d accepted\n", id);
  99. ++n_accept;
  100. ++n_past_tgt;
  101. ++n_past_dft;
  102. ++i_dft;
  103. continue;
  104. }
  105. // the drafted token was rejected or we are out of drafted tokens
  106. llama_eval(ctx_dft, &id, 1, n_past_dft, params.n_threads);
  107. ++n_past_dft;
  108. drafted.clear();
  109. drafted.push_back(id);
  110. break;
  111. }
  112. if (n_predict > params.n_predict || has_eos) {
  113. break;
  114. }
  115. // sample n_draft tokens from the draft model picking the best token
  116. int n_past_cur = n_past_dft;
  117. for (int i = 0; i < n_draft; ++i) {
  118. float * logits = llama_get_logits(ctx_dft);
  119. candidates.clear();
  120. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  121. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  122. }
  123. llama_token_data_array cur_p = { candidates.data(), candidates.size(), false };
  124. // computes softmax and sorts the candidates
  125. llama_sample_softmax(ctx_dft, &cur_p);
  126. for (int i = 0; i < 3; ++i) {
  127. LOG(" - draft candidate %d: %d (%.3f)\n", i, cur_p.data[i].id, cur_p.data[i].p);
  128. }
  129. // too low probability, stop drafting
  130. if (cur_p.data[0].p < 2*cur_p.data[1].p) {
  131. break;
  132. }
  133. drafted.push_back(cur_p.data[0].id);
  134. ++n_drafted;
  135. if (i < n_draft - 1) {
  136. // evaluate the drafted token on the draft model
  137. llama_eval(ctx_dft, &drafted.back(), 1, n_past_cur, params.n_threads);
  138. ++n_past_cur;
  139. }
  140. }
  141. // evaluate the target model on the drafted tokens
  142. llama_eval(ctx_tgt, drafted.data(), drafted.size(), n_past_tgt, params.n_threads);
  143. ++n_past_tgt;
  144. drafted.erase(drafted.begin());
  145. }
  146. auto t_dec_end = ggml_time_us();
  147. LOG_TEE("\n\n");
  148. 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));
  149. 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));
  150. // TODO: make sure these numbers are computed correctly
  151. LOG_TEE("\n");
  152. LOG_TEE("n_draft = %d\n", n_draft);
  153. LOG_TEE("n_predict = %d\n", n_predict);
  154. LOG_TEE("n_drafted = %d\n", n_drafted);
  155. LOG_TEE("n_accept = %d\n", n_accept);
  156. LOG_TEE("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
  157. LOG_TEE("\ndraft:\n");
  158. llama_print_timings(ctx_dft);
  159. LOG_TEE("\ntarget:\n");
  160. llama_print_timings(ctx_tgt);
  161. llama_free(ctx_tgt);
  162. llama_free_model(model_tgt);
  163. llama_free(ctx_dft);
  164. llama_free_model(model_dft);
  165. llama_backend_free();
  166. fprintf(stderr, "\n\n");
  167. return 0;
  168. }