1
0

simple.cpp 5.2 KB

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