1
0

simple.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_load_model_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. // tokenize the prompt
  83. // find the number of tokens in the prompt
  84. const int n_prompt = -llama_tokenize(model, prompt.c_str(), prompt.size(), NULL, 0, true, true);
  85. // allocate space for the tokens and tokenize the prompt
  86. std::vector<llama_token> prompt_tokens(n_prompt);
  87. if (llama_tokenize(model, prompt.c_str(), prompt.size(), prompt_tokens.data(), prompt_tokens.size(), true, true) < 0) {
  88. fprintf(stderr, "%s: error: failed to tokenize the prompt\n", __func__);
  89. return 1;
  90. }
  91. // initialize the context
  92. llama_context_params ctx_params = llama_context_default_params();
  93. // n_ctx is the context size
  94. ctx_params.n_ctx = n_prompt + n_predict - 1;
  95. // n_batch is the maximum number of tokens that can be processed in a single call to llama_decode
  96. ctx_params.n_batch = n_prompt;
  97. // enable performance counters
  98. ctx_params.no_perf = false;
  99. llama_context * ctx = llama_new_context_with_model(model, ctx_params);
  100. if (ctx == NULL) {
  101. fprintf(stderr , "%s: error: failed to create the llama_context\n" , __func__);
  102. return 1;
  103. }
  104. // initialize the sampler
  105. auto sparams = llama_sampler_chain_default_params();
  106. sparams.no_perf = false;
  107. llama_sampler * smpl = llama_sampler_chain_init(sparams);
  108. llama_sampler_chain_add(smpl, llama_sampler_init_greedy());
  109. // print the prompt token-by-token
  110. for (auto id : prompt_tokens) {
  111. char buf[128];
  112. int n = llama_token_to_piece(model, id, buf, sizeof(buf), 0, true);
  113. if (n < 0) {
  114. fprintf(stderr, "%s: error: failed to convert token to piece\n", __func__);
  115. return 1;
  116. }
  117. std::string s(buf, n);
  118. printf("%s", s.c_str());
  119. }
  120. // prepare a batch for the prompt
  121. llama_batch batch = llama_batch_get_one(prompt_tokens.data(), prompt_tokens.size());
  122. // main loop
  123. const auto t_main_start = ggml_time_us();
  124. int n_decode = 0;
  125. llama_token new_token_id;
  126. for (int n_pos = 0; n_pos + batch.n_tokens < n_prompt + n_predict; ) {
  127. // evaluate the current batch with the transformer model
  128. if (llama_decode(ctx, batch)) {
  129. fprintf(stderr, "%s : failed to eval, return code %d\n", __func__, 1);
  130. return 1;
  131. }
  132. n_pos += batch.n_tokens;
  133. // sample the next token
  134. {
  135. new_token_id = llama_sampler_sample(smpl, ctx, -1);
  136. // is it an end of generation?
  137. if (llama_token_is_eog(model, new_token_id)) {
  138. break;
  139. }
  140. char buf[128];
  141. int n = llama_token_to_piece(model, new_token_id, buf, sizeof(buf), 0, true);
  142. if (n < 0) {
  143. fprintf(stderr, "%s: error: failed to convert token to piece\n", __func__);
  144. return 1;
  145. }
  146. std::string s(buf, n);
  147. printf("%s", s.c_str());
  148. fflush(stdout);
  149. // prepare the next batch with the sampled token
  150. batch = llama_batch_get_one(&new_token_id, 1);
  151. n_decode += 1;
  152. }
  153. }
  154. printf("\n");
  155. const auto t_main_end = ggml_time_us();
  156. fprintf(stderr, "%s: decoded %d tokens in %.2f s, speed: %.2f t/s\n",
  157. __func__, n_decode, (t_main_end - t_main_start) / 1000000.0f, n_decode / ((t_main_end - t_main_start) / 1000000.0f));
  158. fprintf(stderr, "\n");
  159. llama_perf_sampler_print(smpl);
  160. llama_perf_context_print(ctx);
  161. fprintf(stderr, "\n");
  162. llama_sampler_free(smpl);
  163. llama_free(ctx);
  164. llama_free_model(model);
  165. return 0;
  166. }