speculative.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #include "common.h"
  2. #include "llama.h"
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <string>
  6. #include <vector>
  7. #include <set>
  8. #define SPEC_VOCAB_MAX_SIZE_DIFFERENCE 100
  9. #define SPEC_VOCAB_CHECK_START_TOKEN_ID 5
  10. struct seq_draft {
  11. bool active = false;
  12. bool drafting = false;
  13. bool skip = false;
  14. int i_batch_dft = 0;
  15. std::vector<int> i_batch_tgt;
  16. std::vector<llama_token> tokens;
  17. std::vector<std::vector<llama_token_data>> dists;
  18. struct llama_sampling_context * ctx_sampling;
  19. };
  20. int main(int argc, char ** argv) {
  21. gpt_params params;
  22. if (gpt_params_parse(argc, argv, params) == false) {
  23. return 1;
  24. }
  25. if (params.model_draft.empty()) {
  26. fprintf(stderr, "%s: error: --model-draft is required\n", __func__);
  27. return 1;
  28. }
  29. // max number of parallel drafting sequences (i.e. tree branches)
  30. const int n_seq_dft = params.n_parallel;
  31. // probability threshold for splitting a draft branch (only for n_seq_dft > 1)
  32. const float p_split = params.p_split;
  33. if (params.seed == LLAMA_DEFAULT_SEED) {
  34. params.seed = time(NULL);
  35. }
  36. std::default_random_engine rng(params.seed);
  37. std::uniform_real_distribution<> u_dist;
  38. #ifndef LOG_DISABLE_LOGS
  39. log_set_target(log_filename_generator("speculative", "log"));
  40. LOG_TEE("Log start\n");
  41. log_dump_cmdline(argc, argv);
  42. #endif // LOG_DISABLE_LOGS
  43. // init llama.cpp
  44. llama_backend_init();
  45. llama_numa_init(params.numa);
  46. llama_model * model_tgt = NULL;
  47. llama_model * model_dft = NULL;
  48. llama_context * ctx_tgt = NULL;
  49. llama_context * ctx_dft = NULL;
  50. // load the target model
  51. params.logits_all = true;
  52. std::tie(model_tgt, ctx_tgt) = llama_init_from_gpt_params(params);
  53. // load the draft model
  54. params.model = params.model_draft;
  55. params.n_gpu_layers = params.n_gpu_layers_draft;
  56. if (params.n_threads_draft > 0) {
  57. params.n_threads = params.n_threads_draft;
  58. }
  59. params.n_threads_batch = params.n_threads_batch_draft;
  60. std::tie(model_dft, ctx_dft) = llama_init_from_gpt_params(params);
  61. {
  62. const int n_vocab_tgt = llama_n_vocab(model_tgt);
  63. const int n_vocab_dft = llama_n_vocab(model_dft);
  64. const int vocab_diff = n_vocab_tgt > n_vocab_dft
  65. ? n_vocab_tgt - n_vocab_dft
  66. : n_vocab_dft - n_vocab_tgt;
  67. if (vocab_diff > SPEC_VOCAB_MAX_SIZE_DIFFERENCE) {
  68. fprintf(stderr, "%s: error: draft model vocab must closely match target model to use speculation but ", __func__);
  69. fprintf(stderr, "target vocab size %d does not match draft vocab size %d - difference %d, max allowed %d\n",
  70. n_vocab_tgt, llama_n_vocab(model_dft), vocab_diff, SPEC_VOCAB_MAX_SIZE_DIFFERENCE);
  71. return 1;
  72. }
  73. for (int i = SPEC_VOCAB_CHECK_START_TOKEN_ID; i < std::min(n_vocab_tgt, n_vocab_dft); ++i) {
  74. const char * token_text_tgt = llama_token_get_text(model_tgt, i);
  75. const char * token_text_dft = llama_token_get_text(model_dft, i);
  76. if (std::strcmp(token_text_tgt, token_text_dft) != 0) {
  77. fprintf(stderr, "%s: error: draft model vocab must match target model to use speculation but ", __func__);
  78. fprintf(stderr, "token %d content differs - target '%s', draft '%s'\n", i,
  79. llama_token_to_piece(ctx_tgt, i).c_str(),
  80. llama_token_to_piece(ctx_dft, i).c_str());
  81. return 1;
  82. }
  83. }
  84. }
  85. // Tokenize the prompt
  86. const bool add_bos_tgt = llama_should_add_bos_token(model_tgt);
  87. LOG("add_bos tgt: %d\n", add_bos_tgt);
  88. const bool add_bos_dft = llama_should_add_bos_token(model_dft);
  89. LOG("add_bos dft: %d\n", add_bos_dft);
  90. if (add_bos_tgt != add_bos_dft) {
  91. fprintf(stderr, "%s: error: draft model add_bos must match target model to use speculation but ", __func__);
  92. fprintf(stderr, "add_bos_dft = %d while add_bos_tgt = %d\n", add_bos_dft, add_bos_tgt);
  93. return 1;
  94. }
  95. std::vector<llama_token> inp;
  96. inp = ::llama_tokenize(ctx_tgt, params.prompt, add_bos_tgt, true);
  97. const int max_context_size = llama_n_ctx(ctx_tgt);
  98. const int max_tokens_list_size = max_context_size - 4;
  99. if ((int) inp.size() > max_tokens_list_size) {
  100. fprintf(stderr, "%s: error: prompt too long (%d tokens, max %d)\n", __func__, (int) inp.size(), max_tokens_list_size);
  101. return 1;
  102. }
  103. fprintf(stderr, "\n\n");
  104. for (auto id : inp) {
  105. fprintf(stderr, "%s", llama_token_to_piece(ctx_tgt, id).c_str());
  106. }
  107. fflush(stderr);
  108. const int n_input = inp.size();
  109. const auto t_enc_start = ggml_time_us();
  110. // eval the prompt with both models
  111. llama_decode(ctx_tgt, llama_batch_get_one( inp.data(), n_input - 1, 0, 0));
  112. llama_decode(ctx_tgt, llama_batch_get_one(&inp.back(), 1, n_input - 1, 0));
  113. llama_decode(ctx_dft, llama_batch_get_one( inp.data(), n_input, 0, 0));
  114. const auto t_enc_end = ggml_time_us();
  115. // the 2 models should have the same vocab
  116. //GGML_ASSERT(n_vocab == llama_n_vocab(model_dft));
  117. // how many tokens to draft each time
  118. int n_draft = params.n_draft;
  119. int n_predict = 0;
  120. int n_drafted = 0;
  121. int n_accept = 0;
  122. int n_past_tgt = inp.size();
  123. int n_past_dft = inp.size();
  124. // used to determine end of generation
  125. bool has_eos = false;
  126. // target model sampling context
  127. struct llama_sampling_context * ctx_sampling = llama_sampling_init(params.sparams);
  128. // draft sequence data
  129. std::vector<seq_draft> drafts(n_seq_dft);
  130. params.sparams.grammar.clear(); // the draft samplers will copy the target sampler's grammar
  131. if (params.sparams.temp == 0) {
  132. params.sparams.temp = -1.0f; // force greedy sampling with probs for the draft model
  133. }
  134. for (int s = 0; s < n_seq_dft; ++s) {
  135. drafts[s].ctx_sampling = llama_sampling_init(params.sparams);
  136. }
  137. llama_batch batch_dft = llama_batch_init(params.n_ctx, 0, 1);
  138. llama_batch batch_tgt = llama_batch_init(params.n_ctx, 0, n_seq_dft);
  139. const auto t_dec_start = ggml_time_us();
  140. // sample from the last token of the prompt
  141. drafts[0].i_batch_tgt.resize(1);
  142. drafts[0].i_batch_tgt[0] = 0;
  143. while (true) {
  144. std::set<int> active_seqs = {};
  145. // print current draft sequences
  146. for (int s = 0; s < n_seq_dft; ++s) {
  147. if (!drafts[s].active) {
  148. continue;
  149. }
  150. active_seqs.insert(s);
  151. const auto & tokens = drafts[s].tokens;
  152. LOG("draft %d: %s\n", s, LOG_TOKENS_TOSTR_PRETTY(ctx_dft, tokens).c_str());
  153. }
  154. int i_dft = 0;
  155. int s_keep = 0;
  156. llama_token token_id;
  157. std::string token_str;
  158. // loop until we fail to accept a drafted token or we run out of drafted tokens
  159. while (true) {
  160. // check if the target token matches any of the drafts
  161. // for stochastic sampling, attempt to match the token with the drafted tokens
  162. {
  163. bool accept = false;
  164. if (params.sparams.temp > 0) {
  165. // stochastic verification
  166. llama_token_data_array dist_tgt = llama_sampling_probability_distribution(ctx_sampling, ctx_tgt, NULL, drafts[s_keep].i_batch_tgt[i_dft]);
  167. float p_tgt = 0, p_dft = 0;
  168. // GGML_ASSERT(dist_tgt.size() == dist_dft.size());
  169. while (active_seqs.size() > 0) {
  170. // randomly select a sequence to verify from active sequences
  171. std::uniform_int_distribution<u_int> u_int_dist(0, active_seqs.size() - 1);
  172. int s = *std::next(active_seqs.begin(), u_int_dist(rng));
  173. if (i_dft >= (int) drafts[s].tokens.size()) {
  174. drafts[s].active = false;
  175. active_seqs.erase(s);
  176. continue;
  177. }
  178. if (accept) {
  179. // if we already accepted a token, we can skip the rest
  180. if (drafts[s].tokens[i_dft] != drafts[s_keep].tokens[i_dft]) {
  181. drafts[s].active = false;
  182. active_seqs.erase(s);
  183. }
  184. continue;
  185. }
  186. LOG("verifying sequence #%d at pos #%d from %d active sequence(s)\n", s, i_dft, (int) active_seqs.size());
  187. float r = u_dist(rng);
  188. llama_token_data_array dist_dft = { drafts[s].dists[i_dft].data() , drafts[s].dists[i_dft].size(), true };
  189. // acquire the token probabilities assigned by the draft and target models
  190. for (size_t i = 0; i < dist_tgt.size; i++) {
  191. if (dist_tgt.data[i].id == drafts[s].tokens[i_dft]) {
  192. p_tgt = dist_tgt.data[i].p;
  193. }
  194. if (dist_dft.data[i].id == drafts[s].tokens[i_dft]) {
  195. p_dft = dist_dft.data[i].p;
  196. }
  197. if (p_tgt && p_dft) {
  198. break;
  199. }
  200. }
  201. LOG("r = %f, p_dft = %f, p_tgt = %f\n", r, p_dft, p_tgt);
  202. if (r <= p_tgt / p_dft) {
  203. s_keep = s;
  204. accept = true;
  205. token_id = drafts[s].tokens[i_dft];
  206. token_str = llama_token_to_piece(ctx_tgt, token_id);
  207. llama_sampling_accept(ctx_sampling, ctx_tgt, token_id, true);
  208. LOG("draft token %d of sequence %d (%d, '%s') accepted\n", i_dft, s, token_id, token_str.c_str());
  209. break;
  210. } else {
  211. LOG("draft token %d of sequence %d (%d, '%s') rejected\n", i_dft, s, drafts[s].tokens[i_dft], llama_token_to_piece(ctx_tgt, drafts[s].tokens[i_dft]).c_str());
  212. drafts[s].active = false;
  213. // calculate residual probability
  214. GGML_ASSERT(dist_tgt.sorted);
  215. GGML_ASSERT(dist_dft.sorted);
  216. float sum_probs = 0.0f;
  217. // sort dist by id
  218. std::sort(dist_tgt.data, dist_tgt.data + dist_tgt.size, [](const llama_token_data &a, const llama_token_data &b) {
  219. return a.id < b.id;
  220. });
  221. std::sort(dist_dft.data, dist_dft.data + dist_dft.size, [](const llama_token_data &a, const llama_token_data &b) {
  222. return a.id < b.id;
  223. });
  224. for (size_t i = 0; i < dist_tgt.size; i++) {
  225. dist_tgt.data[i].p = std::max(0.0f, dist_tgt.data[i].p - dist_dft.data[i].p);
  226. sum_probs += dist_tgt.data[i].p;
  227. }
  228. for (size_t i = 0; i < dist_tgt.size; i++) {
  229. dist_tgt.data[i].p /= sum_probs;
  230. }
  231. // sort dist_tgt by p desc
  232. std::sort(dist_tgt.data, dist_tgt.data + dist_tgt.size, [](const llama_token_data &a, const llama_token_data &b) {
  233. return a.p > b.p;
  234. });
  235. }
  236. active_seqs.erase(s);
  237. for(int i = 0; i < n_seq_dft; i++) {
  238. if (i == s) {
  239. continue;
  240. }
  241. if (drafts[i].tokens[i_dft] == drafts[s].tokens[i_dft]) {
  242. // synchronize active status for sequences with the same drafted token
  243. drafts[i].active = drafts[i].active && accept;
  244. if (!drafts[i].active) {
  245. active_seqs.erase(s);
  246. }
  247. }
  248. }
  249. }
  250. if (!accept) {
  251. // all drafted tokens were rejected
  252. // sample from the target model
  253. LOG("all drafted tokens were rejected, sampling from residual distribution\n");
  254. token_id = llama_sample_token(ctx_tgt, &dist_tgt);
  255. llama_sampling_accept(ctx_sampling, ctx_tgt, token_id, true);
  256. token_str = llama_token_to_piece(ctx_tgt, token_id);
  257. }
  258. } else {
  259. // greedy verification
  260. // sample from the target model
  261. LOG("sampling target: s_keep = %3d, i_dft = %3d, i_batch_tgt = %3d\n", s_keep, i_dft, drafts[s_keep].i_batch_tgt[i_dft]);
  262. token_id = llama_sampling_sample(ctx_sampling, ctx_tgt, NULL, drafts[s_keep].i_batch_tgt[i_dft]);
  263. llama_sampling_accept(ctx_sampling, ctx_tgt, token_id, true);
  264. //LOG("last: %s\n", LOG_TOKENS_TOSTR_PRETTY(ctx_tgt, ctx_sampling->prev).c_str());
  265. token_str = llama_token_to_piece(ctx_tgt, token_id);
  266. for (int s = 0; s < n_seq_dft; ++s) {
  267. if (!drafts[s].active) {
  268. continue;
  269. }
  270. if (i_dft < (int) drafts[s].tokens.size() && token_id == drafts[s].tokens[i_dft]) {
  271. LOG("the sampled target token matches the %dth drafted token of sequence %d (%d, '%s') - accepted\n", i_dft, s, token_id, token_str.c_str());
  272. s_keep = s;
  273. accept = true;
  274. } else {
  275. drafts[s].active = false;
  276. }
  277. }
  278. }
  279. if (token_id == llama_token_eos(model_tgt)) {
  280. has_eos = true;
  281. }
  282. ++n_predict;
  283. if (accept) {
  284. ++n_accept;
  285. ++n_past_tgt;
  286. ++n_past_dft;
  287. ++i_dft;
  288. if (params.use_color) {
  289. // Color token according to its origin sequence
  290. printf("\u001b[%dm%s\u001b[37m", (36 - s_keep % 6), token_str.c_str());
  291. } else {
  292. printf("%s", token_str.c_str());
  293. }
  294. fflush(stdout);
  295. continue;
  296. } else {
  297. printf("%s", token_str.c_str());
  298. fflush(stdout);
  299. break;
  300. }
  301. }
  302. }
  303. {
  304. LOG("the sampled target token (%d, '%s') did not match, or we ran out of drafted tokens\n", token_id, token_str.c_str());
  305. // TODO: simplify
  306. {
  307. LOG("keeping sequence %d, n_past_tgt = %d, n_past_dft = %d\n", s_keep, n_past_tgt, n_past_dft);
  308. llama_kv_cache_seq_keep(ctx_dft, s_keep);
  309. llama_kv_cache_seq_cp (ctx_dft, s_keep, 0, -1, -1);
  310. llama_kv_cache_seq_keep(ctx_dft, 0);
  311. llama_kv_cache_seq_rm (ctx_tgt, s_keep, n_past_tgt, -1);
  312. llama_kv_cache_seq_keep(ctx_tgt, s_keep);
  313. llama_kv_cache_seq_cp (ctx_tgt, s_keep, 0, -1, -1);
  314. llama_kv_cache_seq_keep(ctx_tgt, 0);
  315. }
  316. for (int s = 0; s < n_seq_dft; ++s) {
  317. drafts[s].active = false;
  318. drafts[s].tokens.clear();
  319. drafts[s].i_batch_tgt.clear();
  320. drafts[s].dists.clear();
  321. }
  322. // note: will be erased after the speculation phase
  323. drafts[0].tokens.push_back(token_id);
  324. drafts[0].dists.push_back(std::vector<llama_token_data>());
  325. drafts[0].i_batch_tgt.push_back(0);
  326. llama_batch_clear(batch_dft);
  327. llama_batch_add (batch_dft, token_id, n_past_dft, { 0 }, true);
  328. llama_kv_cache_seq_rm(ctx_dft, 0, n_past_dft, -1);
  329. // LOG("dft batch: %s\n", LOG_BATCH_TOSTR_PRETTY(ctx_dft, batch_dft).c_str());
  330. llama_decode(ctx_dft, batch_dft);
  331. ++n_past_dft;
  332. }
  333. if (n_predict > params.n_predict || has_eos) {
  334. break;
  335. }
  336. llama_sampling_cp(ctx_sampling, drafts[0].ctx_sampling);
  337. int n_seq_cur = 1;
  338. int n_past_cur = n_past_dft;
  339. for (int s = 0; s < n_seq_dft; ++s) {
  340. drafts[s].active = false;
  341. drafts[s].drafting = false;
  342. }
  343. drafts[0].active = true;
  344. drafts[0].drafting = true;
  345. drafts[0].i_batch_dft = 0;
  346. llama_batch_clear(batch_tgt);
  347. llama_batch_add (batch_tgt, drafts[0].tokens[0], n_past_tgt, { 0 }, true);
  348. // sample n_draft tokens from the draft model using tree-based sampling
  349. for (int i = 0; i < n_draft; ++i) {
  350. batch_dft.n_tokens = 0;
  351. for (int s = 0; s < n_seq_dft; ++s) {
  352. drafts[s].skip = false;
  353. }
  354. for (int s = 0; s < n_seq_dft; ++s) {
  355. if (!drafts[s].drafting || drafts[s].skip) {
  356. continue;
  357. }
  358. llama_sampling_sample(drafts[s].ctx_sampling, ctx_dft, NULL, drafts[s].i_batch_dft);
  359. const auto & cur_p = drafts[s].ctx_sampling->cur;
  360. for (int k = 0; k < std::min(n_seq_dft + 3, (int) cur_p.size()); ++k) {
  361. LOG(" - draft candidate %3d for seq %3d, pos %3d: %6d (%8.3f) '%s'\n",
  362. k, s, i, cur_p[k].id, cur_p[k].p, llama_token_to_piece(ctx_dft, cur_p[k].id).c_str());
  363. }
  364. std::vector<int> sa(1, s);
  365. // attempt to split the branch if the probability is high enough
  366. for (int f = 1; f < 8; ++f) {
  367. if (n_seq_cur < n_seq_dft && cur_p[f].p > p_split) {
  368. LOG("splitting seq %3d into %3d\n", s, n_seq_cur);
  369. llama_kv_cache_seq_rm(ctx_dft, n_seq_cur, -1, -1);
  370. llama_kv_cache_seq_cp(ctx_dft, s, n_seq_cur, -1, -1);
  371. // all previous tokens from this branch are now also part of the new branch
  372. for (int t = 0; t < batch_tgt.n_tokens; ++t) {
  373. for (int p = 0; p < batch_tgt.n_seq_id[t]; ++p) {
  374. if (batch_tgt.seq_id[t][p] == s) {
  375. batch_tgt.seq_id[t][batch_tgt.n_seq_id[t]] = n_seq_cur;
  376. batch_tgt.n_seq_id[t]++;
  377. break;
  378. }
  379. }
  380. }
  381. // copy the draft state
  382. drafts[n_seq_cur].active = true;
  383. drafts[n_seq_cur].drafting = true;
  384. drafts[n_seq_cur].skip = true;
  385. drafts[n_seq_cur].tokens = drafts[s].tokens;
  386. drafts[n_seq_cur].dists = drafts[s].dists;
  387. drafts[n_seq_cur].i_batch_dft = drafts[s].i_batch_dft;
  388. drafts[n_seq_cur].i_batch_tgt = drafts[s].i_batch_tgt;
  389. llama_sampling_cp(drafts[s].ctx_sampling, drafts[n_seq_cur].ctx_sampling);
  390. sa.push_back(n_seq_cur);
  391. n_seq_cur++;
  392. } else {
  393. break;
  394. }
  395. }
  396. // add drafted token for each sequence
  397. for (int is = 0; is < (int) sa.size(); ++is) {
  398. const llama_token id = cur_p[is].id;
  399. const int s = sa[is];
  400. llama_sampling_accept(drafts[s].ctx_sampling, ctx_dft, id, true);
  401. drafts[s].tokens.push_back(id);
  402. // save cur_p.data into drafts[s].dists
  403. drafts[s].dists.push_back(cur_p);
  404. // add unique drafted tokens to the target batch
  405. drafts[s].i_batch_tgt.push_back(batch_tgt.n_tokens);
  406. llama_batch_add(batch_tgt, id, n_past_tgt + i + 1, { s }, true);
  407. // add the token to the batch for batched decoding with the draft model
  408. drafts[s].i_batch_dft = batch_dft.n_tokens;
  409. llama_batch_add(batch_dft, id, n_past_cur, { s }, true);
  410. if (batch_tgt.n_tokens > n_draft) {
  411. drafts[s].drafting = false;
  412. }
  413. }
  414. }
  415. // no sequence is drafting anymore
  416. if (batch_dft.n_tokens == 0) {
  417. break;
  418. }
  419. // evaluate the drafted tokens on the draft model
  420. llama_decode(ctx_dft, batch_dft);
  421. ++n_past_cur;
  422. ++n_drafted;
  423. if (batch_tgt.n_tokens > n_draft) {
  424. break;
  425. }
  426. }
  427. // evaluate the target model on the drafted tokens
  428. {
  429. llama_kv_cache_seq_keep(ctx_tgt, 0);
  430. for (int s = 1; s < n_seq_dft; ++s) {
  431. llama_kv_cache_seq_cp(ctx_tgt, 0, s, -1, -1);
  432. }
  433. // LOG("target batch: %s\n", LOG_BATCH_TOSTR_PRETTY(ctx_tgt, batch_tgt).c_str());
  434. llama_decode(ctx_tgt, batch_tgt);
  435. ++n_past_tgt;
  436. }
  437. // the first token is always proposed by the target model before the speculation loop so we erase it here
  438. for (int s = 0; s < n_seq_dft; ++s) {
  439. if (!drafts[s].active) {
  440. continue;
  441. }
  442. drafts[s].tokens.erase(drafts[s].tokens.begin());
  443. drafts[s].dists.erase(drafts[s].dists.begin());
  444. }
  445. }
  446. auto t_dec_end = ggml_time_us();
  447. LOG_TEE("\n\n");
  448. 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));
  449. 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));
  450. LOG_TEE("\n");
  451. LOG_TEE("n_draft = %d\n", n_draft);
  452. LOG_TEE("n_predict = %d\n", n_predict);
  453. LOG_TEE("n_drafted = %d\n", n_drafted);
  454. LOG_TEE("n_accept = %d\n", n_accept);
  455. LOG_TEE("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
  456. LOG_TEE("\ndraft:\n");
  457. llama_print_timings(ctx_dft);
  458. LOG_TEE("\ntarget:\n");
  459. llama_print_timings(ctx_tgt);
  460. llama_sampling_free(ctx_sampling);
  461. for (int s = 0; s < n_seq_dft; ++s) {
  462. llama_sampling_free(drafts[s].ctx_sampling);
  463. }
  464. llama_batch_free(batch_dft);
  465. llama_free(ctx_tgt);
  466. llama_free_model(model_tgt);
  467. llama_free(ctx_dft);
  468. llama_free_model(model_dft);
  469. llama_backend_free();
  470. fprintf(stderr, "\n\n");
  471. return 0;
  472. }