simple.cpp 6.0 KB

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