simple.cpp 4.9 KB

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