1
0

simple.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include "llama.h"
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <vector>
  6. static void print_usage(int, char ** argv) {
  7. printf("\nexample usage:\n");
  8. printf("\n %s -m model.gguf [-n n_predict] [-ngl n_gpu_layers] [prompt]\n", argv[0]);
  9. printf("\n");
  10. }
  11. int main(int argc, char ** argv) {
  12. // path to the model gguf file
  13. std::string model_path;
  14. // prompt to generate text from
  15. std::string prompt = "Hello my name is";
  16. // number of layers to offload to the GPU
  17. int ngl = 99;
  18. // number of tokens to predict
  19. int n_predict = 32;
  20. // parse command line arguments
  21. {
  22. int i = 1;
  23. for (; i < argc; i++) {
  24. if (strcmp(argv[i], "-m") == 0) {
  25. if (i + 1 < argc) {
  26. model_path = argv[++i];
  27. } else {
  28. print_usage(argc, argv);
  29. return 1;
  30. }
  31. } else if (strcmp(argv[i], "-n") == 0) {
  32. if (i + 1 < argc) {
  33. try {
  34. n_predict = std::stoi(argv[++i]);
  35. } catch (...) {
  36. print_usage(argc, argv);
  37. return 1;
  38. }
  39. } else {
  40. print_usage(argc, argv);
  41. return 1;
  42. }
  43. } else if (strcmp(argv[i], "-ngl") == 0) {
  44. if (i + 1 < argc) {
  45. try {
  46. ngl = std::stoi(argv[++i]);
  47. } catch (...) {
  48. print_usage(argc, argv);
  49. return 1;
  50. }
  51. } else {
  52. print_usage(argc, argv);
  53. return 1;
  54. }
  55. } else {
  56. // prompt starts here
  57. break;
  58. }
  59. }
  60. if (model_path.empty()) {
  61. print_usage(argc, argv);
  62. return 1;
  63. }
  64. if (i < argc) {
  65. prompt = argv[i++];
  66. for (; i < argc; i++) {
  67. prompt += " ";
  68. prompt += argv[i];
  69. }
  70. }
  71. }
  72. // load dynamic backends
  73. ggml_backend_load_all();
  74. // initialize the model
  75. llama_model_params model_params = llama_model_default_params();
  76. model_params.n_gpu_layers = ngl;
  77. llama_model * model = llama_model_load_from_file(model_path.c_str(), model_params);
  78. if (model == NULL) {
  79. fprintf(stderr , "%s: error: unable to load model\n" , __func__);
  80. return 1;
  81. }
  82. const llama_vocab * vocab = llama_model_get_vocab(model);
  83. // tokenize the prompt
  84. // find the number of tokens in the prompt
  85. const int n_prompt = -llama_tokenize(vocab, prompt.c_str(), prompt.size(), NULL, 0, true, true);
  86. // allocate space for the tokens and tokenize the prompt
  87. std::vector<llama_token> prompt_tokens(n_prompt);
  88. if (llama_tokenize(vocab, prompt.c_str(), prompt.size(), prompt_tokens.data(), prompt_tokens.size(), true, true) < 0) {
  89. fprintf(stderr, "%s: error: failed to tokenize the prompt\n", __func__);
  90. return 1;
  91. }
  92. // initialize the context
  93. llama_context_params ctx_params = llama_context_default_params();
  94. // n_ctx is the context size
  95. ctx_params.n_ctx = n_prompt + n_predict - 1;
  96. // n_batch is the maximum number of tokens that can be processed in a single call to llama_decode
  97. ctx_params.n_batch = n_prompt;
  98. // enable performance counters
  99. ctx_params.no_perf = false;
  100. llama_context * ctx = llama_init_from_model(model, ctx_params);
  101. if (ctx == NULL) {
  102. fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
  103. return 1;
  104. }
  105. // initialize the sampler
  106. auto sparams = llama_sampler_chain_default_params();
  107. sparams.no_perf = false;
  108. llama_sampler * smpl = llama_sampler_chain_init(sparams);
  109. llama_sampler_chain_add(smpl, llama_sampler_init_greedy());
  110. // print the prompt token-by-token
  111. for (auto id : prompt_tokens) {
  112. char buf[128];
  113. int n = llama_token_to_piece(vocab, id, buf, sizeof(buf), 0, true);
  114. if (n < 0) {
  115. fprintf(stderr, "%s: error: failed to convert token to piece\n", __func__);
  116. return 1;
  117. }
  118. std::string s(buf, n);
  119. printf("%s", s.c_str());
  120. }
  121. // prepare a batch for the prompt
  122. llama_batch batch = llama_batch_get_one(prompt_tokens.data(), prompt_tokens.size());
  123. if (llama_model_has_encoder(model)) {
  124. if (llama_encode(ctx, batch)) {
  125. fprintf(stderr, "%s : failed to eval\n", __func__);
  126. return 1;
  127. }
  128. llama_token decoder_start_token_id = llama_model_decoder_start_token(model);
  129. if (decoder_start_token_id == LLAMA_TOKEN_NULL) {
  130. decoder_start_token_id = llama_vocab_bos(vocab);
  131. }
  132. batch = llama_batch_get_one(&decoder_start_token_id, 1);
  133. }
  134. // main loop
  135. const auto t_main_start = ggml_time_us();
  136. int n_decode = 0;
  137. llama_token new_token_id;
  138. for (int n_pos = 0; n_pos + batch.n_tokens < n_prompt + n_predict; ) {
  139. // evaluate the current batch with the transformer model
  140. if (llama_decode(ctx, batch)) {
  141. fprintf(stderr, "%s : failed to eval, return code %d\n", __func__, 1);
  142. return 1;
  143. }
  144. n_pos += batch.n_tokens;
  145. // sample the next token
  146. {
  147. new_token_id = llama_sampler_sample(smpl, ctx, -1);
  148. // is it an end of generation?
  149. if (llama_vocab_is_eog(vocab, new_token_id)) {
  150. break;
  151. }
  152. char buf[128];
  153. int n = llama_token_to_piece(vocab, new_token_id, buf, sizeof(buf), 0, true);
  154. if (n < 0) {
  155. fprintf(stderr, "%s: error: failed to convert token to piece\n", __func__);
  156. return 1;
  157. }
  158. std::string s(buf, n);
  159. printf("%s", s.c_str());
  160. fflush(stdout);
  161. // prepare the next batch with the sampled token
  162. batch = llama_batch_get_one(&new_token_id, 1);
  163. n_decode += 1;
  164. }
  165. }
  166. printf("\n");
  167. const auto t_main_end = ggml_time_us();
  168. fprintf(stderr, "%s: decoded %d tokens in %.2f s, speed: %.2f t/s\n",
  169. __func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
  170. fprintf(stderr, "\n");
  171. llama_perf_sampler_print(smpl);
  172. llama_perf_context_print(ctx);
  173. fprintf(stderr, "\n");
  174. llama_sampler_free(smpl);
  175. llama_free(ctx);
  176. llama_model_free(model);
  177. return 0;
  178. }