simple.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "arg.h"
  2. #include "common.h"
  3. #include "log.h"
  4. #include "llama.h"
  5. #include <vector>
  6. static void print_usage(int, char ** argv) {
  7. LOG("\nexample usage:\n");
  8. LOG("\n %s -m model.gguf -p \"Hello my name is\" -n 32\n", argv[0]);
  9. LOG("\n");
  10. }
  11. int main(int argc, char ** argv) {
  12. gpt_params params;
  13. params.prompt = "Hello my name is";
  14. params.n_predict = 32;
  15. if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON, print_usage)) {
  16. return 1;
  17. }
  18. gpt_init();
  19. // total length of the sequence including the prompt
  20. const int n_predict = params.n_predict;
  21. // init LLM
  22. llama_backend_init();
  23. llama_numa_init(params.numa);
  24. // initialize the model
  25. llama_model_params model_params = llama_model_params_from_gpt_params(params);
  26. llama_model * model = llama_load_model_from_file(params.model.c_str(), model_params);
  27. if (model == NULL) {
  28. fprintf(stderr , "%s: error: unable to load model\n" , __func__);
  29. return 1;
  30. }
  31. // initialize the context
  32. llama_context_params ctx_params = llama_context_params_from_gpt_params(params);
  33. llama_context * ctx = llama_new_context_with_model(model, ctx_params);
  34. if (ctx == NULL) {
  35. fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
  36. return 1;
  37. }
  38. auto sparams = llama_sampler_chain_default_params();
  39. sparams.no_perf = false;
  40. llama_sampler * smpl = llama_sampler_chain_init(sparams);
  41. llama_sampler_chain_add(smpl, llama_sampler_init_greedy());
  42. // tokenize the prompt
  43. std::vector<llama_token> tokens_list;
  44. tokens_list = ::llama_tokenize(ctx, params.prompt, true);
  45. const int n_ctx = llama_n_ctx(ctx);
  46. const int n_kv_req = tokens_list.size() + (n_predict - tokens_list.size());
  47. LOG("\n");
  48. LOG_INF("%s: n_predict = %d, n_ctx = %d, n_kv_req = %d\n", __func__, n_predict, n_ctx, n_kv_req);
  49. // make sure the KV cache is big enough to hold all the prompt and generated tokens
  50. if (n_kv_req > n_ctx) {
  51. LOG_ERR("%s: error: n_kv_req > n_ctx, the required KV cache size is not big enough\n", __func__);
  52. LOG_ERR("%s: either reduce n_predict or increase n_ctx\n", __func__);
  53. return 1;
  54. }
  55. // print the prompt token-by-token
  56. LOG("\n");
  57. for (auto id : tokens_list) {
  58. LOG("%s", llama_token_to_piece(ctx, id).c_str());
  59. }
  60. // create a llama_batch with size 512
  61. // we use this object to submit token data for decoding
  62. llama_batch batch = llama_batch_init(512, 0, 1);
  63. // evaluate the initial prompt
  64. for (size_t i = 0; i < tokens_list.size(); i++) {
  65. llama_batch_add(batch, tokens_list[i], i, { 0 }, false);
  66. }
  67. // llama_decode will output logits only for the last token of the prompt
  68. batch.logits[batch.n_tokens - 1] = true;
  69. if (llama_decode(ctx, batch) != 0) {
  70. LOG("%s: llama_decode() failed\n", __func__);
  71. return 1;
  72. }
  73. // main loop
  74. int n_cur = batch.n_tokens;
  75. int n_decode = 0;
  76. const auto t_main_start = ggml_time_us();
  77. while (n_cur <= n_predict) {
  78. // sample the next token
  79. {
  80. const llama_token new_token_id = llama_sampler_sample(smpl, ctx, -1);
  81. // is it an end of generation?
  82. if (llama_token_is_eog(model, new_token_id) || n_cur == n_predict) {
  83. LOG("\n");
  84. break;
  85. }
  86. LOG("%s", llama_token_to_piece(ctx, new_token_id).c_str());
  87. fflush(stdout);
  88. // prepare the next batch
  89. llama_batch_clear(batch);
  90. // push this new token for next evaluation
  91. llama_batch_add(batch, new_token_id, n_cur, { 0 }, true);
  92. n_decode += 1;
  93. }
  94. n_cur += 1;
  95. // evaluate the current batch with the transformer model
  96. if (llama_decode(ctx, batch)) {
  97. LOG_ERR("%s : failed to eval, return code %d\n", __func__, 1);
  98. return 1;
  99. }
  100. }
  101. LOG("\n");
  102. const auto t_main_end = ggml_time_us();
  103. LOG_INF("%s: decoded %d tokens in %.2f s, speed: %.2f t/s\n",
  104. __func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
  105. LOG("\n");
  106. llama_perf_sampler_print(smpl);
  107. llama_perf_context_print(ctx);
  108. LOG("\n");
  109. llama_batch_free(batch);
  110. llama_sampler_free(smpl);
  111. llama_free(ctx);
  112. llama_free_model(model);
  113. llama_backend_free();
  114. return 0;
  115. }