1
0

passkey.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include "arg.h"
  2. #include "common.h"
  3. #include "log.h"
  4. #include "llama.h"
  5. #include <cmath>
  6. #include <cstdio>
  7. #include <string>
  8. #include <vector>
  9. #include <algorithm>
  10. static void print_usage(int, char ** argv) {
  11. LOG("\nexample usage:\n");
  12. LOG("\n %s -m model.gguf --junk 250 --pos 90 --keep 32 --grp-attn-n 2 [--seed 1234]\n", argv[0]);
  13. LOG("\n");
  14. }
  15. int main(int argc, char ** argv) {
  16. common_params params;
  17. params.n_junk = 250;
  18. params.n_keep = 32;
  19. params.i_pos = -1;
  20. if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_PASSKEY, print_usage)) {
  21. return 1;
  22. }
  23. common_init();
  24. int n_junk = params.n_junk;
  25. int n_keep = params.n_keep;
  26. int n_grp = params.grp_attn_n;
  27. int i_pos = params.i_pos;
  28. if (i_pos == -1) {
  29. i_pos = rand() % n_junk;
  30. }
  31. const std::string prompt_prefix = "There is an important info hidden inside a lot of irrelevant text. Find it and memorize them. I will quiz you about the important information there.";
  32. const std::string prompt_suffix = " What is the pass key? The pass key is";
  33. // generate junk text
  34. params.prompt = prompt_prefix;
  35. const int passkey = rand() % 50000 + 1;
  36. for (int i = 0; i < n_junk; i++) {
  37. if (i % n_junk == i_pos) {
  38. params.prompt += " The pass key is " + std::to_string(passkey) + ". Remember it. " + std::to_string(passkey) + " is the pass key.";
  39. }
  40. params.prompt += " The grass is green. The sky is blue. The sun is yellow. Here we go. There and back again.";
  41. }
  42. params.prompt += prompt_suffix;
  43. // init LLM
  44. llama_backend_init();
  45. llama_numa_init(params.numa);
  46. // initialize the model
  47. llama_model_params model_params = common_model_params_to_llama(params);
  48. llama_model * model = llama_model_load_from_file(params.model.path.c_str(), model_params);
  49. if (model == NULL) {
  50. LOG_ERR("%s: unable to load model\n" , __func__);
  51. return 1;
  52. }
  53. const llama_vocab * vocab = llama_model_get_vocab(model);
  54. // initialize the context
  55. llama_context_params ctx_params = common_context_params_to_llama(params);
  56. ctx_params.n_ctx = llama_model_n_ctx_train(model)*n_grp + n_keep;
  57. GGML_ASSERT(ctx_params.n_batch % n_grp == 0 && "n_batch must be divisible by n_grp");
  58. llama_context * ctx = llama_init_from_model(model, ctx_params);
  59. if (ctx == NULL) {
  60. LOG_ERR("%s: failed to create the llama_context\n" , __func__);
  61. return 1;
  62. }
  63. auto sparams = llama_sampler_chain_default_params();
  64. llama_sampler * smpl = llama_sampler_chain_init(sparams);
  65. llama_sampler_chain_add(smpl, llama_sampler_init_greedy());
  66. // tokenize the prompt
  67. std::vector<llama_token> tokens_list;
  68. tokens_list = common_tokenize(ctx, params.prompt, true);
  69. // tokenize the prefix and use it as a sink
  70. const int n_tokens_prefix = common_tokenize(ctx, prompt_prefix, true).size();
  71. const int n_tokens_all = tokens_list.size();
  72. // we leave a margin of 16 tokens for the generated text - it should contain just the passkey
  73. const int n_predict = 16;
  74. // total length of the sequences including the prompt
  75. const int n_len = n_tokens_all + n_predict;
  76. const int n_ctx = llama_n_ctx(ctx) - n_keep;
  77. const int n_kv_req = llama_n_ctx(ctx);
  78. const int n_batch = ctx_params.n_batch;
  79. const int n_batch_grp = ctx_params.n_batch/n_grp;
  80. LOG_INF("\n%s: n_len = %d, n_ctx = %d, n_kv_req = %d, n_grp = %d, n_batch = %d, n_junk = %d, i_pos = %d\n", __func__, n_len, n_ctx, n_kv_req, n_grp, n_batch, n_junk, i_pos);
  81. // print the prompt token-by-token
  82. LOG_INF("\n");
  83. LOG_INF("prefix tokens: %d\n", n_tokens_prefix);
  84. LOG_INF("prompt tokens: %d\n", n_tokens_all);
  85. //LOG_INF("prompt: %s\n", params.prompt.c_str());
  86. llama_batch batch = llama_batch_init(params.n_batch, 0, 1);
  87. int n_past = 0;
  88. auto * mem = llama_get_memory(ctx);
  89. // fill the KV cache
  90. for (int i = 0; i < n_ctx; i += n_batch) {
  91. if (i > 0 && n_grp > 1) {
  92. // if SelfExtend is enabled, we compress the position from the last batch by a factor of n_grp
  93. const int ib = i/n_batch - 1;
  94. const int bd = n_batch_grp*(n_grp - 1);
  95. llama_memory_seq_add(mem, 0, n_past - n_batch, n_past, ib*bd);
  96. llama_memory_seq_div(mem, 0, n_past - n_batch + ib*bd, n_past + ib*bd, n_grp);
  97. n_past = llama_memory_seq_pos_max(mem, 0) + 1;
  98. }
  99. common_batch_clear(batch);
  100. for (int j = 0; j < n_batch && i + j < n_tokens_all; j++) {
  101. common_batch_add(batch, tokens_list[i + j], n_past++, { 0 }, false);
  102. }
  103. if (i + n_batch >= n_tokens_all) {
  104. batch.logits[batch.n_tokens - 1] = true;
  105. }
  106. if (llama_decode(ctx, batch) != 0) {
  107. LOG_INF("%s: llama_decode() failed\n", __func__);
  108. return 1;
  109. }
  110. LOG_INF("%s: processed: [%6d, %6d)\n", __func__, i, std::min(i + n_batch, n_tokens_all));
  111. if (i + n_batch >= n_tokens_all) {
  112. break;
  113. }
  114. }
  115. for (int i = n_ctx; i < n_tokens_all; i += n_batch) {
  116. const int n_discard = n_batch;
  117. LOG_INF("%s: shifting KV cache with %d\n", __func__, n_discard);
  118. llama_memory_seq_rm (mem, 0, n_keep , n_keep + n_discard);
  119. llama_memory_seq_add(mem, 0, n_keep + n_discard, n_ctx, -n_discard);
  120. n_past = llama_memory_seq_pos_max(mem, 0) + 1;
  121. common_batch_clear(batch);
  122. for (int j = 0; j < n_batch && i + j < n_tokens_all; j++) {
  123. common_batch_add(batch, tokens_list[i + j], n_past++, { 0 }, false);
  124. }
  125. if (i + n_batch >= n_tokens_all) {
  126. batch.logits[batch.n_tokens - 1] = true;
  127. }
  128. if (llama_decode(ctx, batch) != 0) {
  129. LOG_ERR("%s: llama_decode() failed\n", __func__);
  130. return 1;
  131. }
  132. LOG_INF("%s: processed: [%6d, %6d)\n", __func__, i, std::min(i + n_batch, n_tokens_all));
  133. }
  134. {
  135. const int n_discard = n_past - n_ctx + n_predict;
  136. if (n_discard > 0) {
  137. LOG_INF("%s: shifting KV cache with %d to free space for the answer\n", __func__, n_discard);
  138. llama_memory_seq_rm (mem, 0, n_keep , n_keep + n_discard);
  139. llama_memory_seq_add(mem, 0, n_keep + n_discard, n_ctx, -n_discard);
  140. n_past = llama_memory_seq_pos_max(mem, 0) + 1;
  141. }
  142. }
  143. LOG_INF("\n");
  144. LOG_INF("%s: passkey = %d, inserted at position %d / %d (token pos: ~%d)\n", __func__, passkey, i_pos, n_junk, (i_pos * n_tokens_all) / n_junk);
  145. LOG_INF("\n");
  146. // main loop
  147. int n_cur = n_tokens_all;
  148. int n_decode = 0;
  149. LOG_INF("%s", prompt_suffix.c_str());
  150. const auto t_main_start = ggml_time_us();
  151. while (n_cur <= n_len) {
  152. // sample the next token
  153. {
  154. const llama_token new_token_id = llama_sampler_sample(smpl, ctx, batch.n_tokens - 1);
  155. // is it an end of generation?
  156. if (llama_vocab_is_eog(vocab, new_token_id) || n_cur == n_len) {
  157. LOG("\n");
  158. break;
  159. }
  160. LOG("%s", common_token_to_piece(ctx, new_token_id).c_str());
  161. n_decode += 1;
  162. // prepare the next batch
  163. common_batch_clear(batch);
  164. // push this new token for next evaluation
  165. common_batch_add(batch, new_token_id, n_past++, { 0 }, true);
  166. }
  167. n_cur += 1;
  168. // evaluate the current batch with the transformer model
  169. if (llama_decode(ctx, batch)) {
  170. LOG_ERR("%s : failed to eval, return code %d\n", __func__, 1);
  171. return 1;
  172. }
  173. }
  174. LOG("\n");
  175. const auto t_main_end = ggml_time_us();
  176. LOG_INF("%s: decoded %d tokens in %.2f s, speed: %.2f t/s\n",
  177. __func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
  178. LOG("\n");
  179. llama_perf_context_print(ctx);
  180. LOG("\n");
  181. llama_sampler_free(smpl);
  182. llama_batch_free(batch);
  183. llama_free(ctx);
  184. llama_model_free(model);
  185. llama_backend_free();
  186. return 0;
  187. }