simple.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "build-info.h"
  2. #include "common.h"
  3. #include "llama.h"
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <string>
  7. #include <vector>
  8. int main(int argc, char ** argv) {
  9. gpt_params params;
  10. if (argc == 1 || argv[1][0] == '-') {
  11. printf("usage: %s MODEL_PATH [PROMPT]\n" , argv[0]);
  12. return 1 ;
  13. }
  14. if (argc >= 2) {
  15. params.model = argv[1];
  16. }
  17. if (argc >= 3) {
  18. params.prompt = argv[2];
  19. }
  20. if (params.prompt.empty()) {
  21. params.prompt = "Hello my name is";
  22. }
  23. // init LLM
  24. llama_backend_init(params.numa);
  25. llama_context_params ctx_params = llama_context_default_params();
  26. llama_model * model = llama_load_model_from_file(params.model.c_str(), ctx_params);
  27. if (model == NULL) {
  28. fprintf(stderr , "%s: error: unable to load model\n" , __func__);
  29. return 1;
  30. }
  31. llama_context * ctx = llama_new_context_with_model(model, ctx_params);
  32. // tokenize the prompt
  33. std::vector<llama_token> tokens_list;
  34. tokens_list = ::llama_tokenize(ctx, params.prompt, true);
  35. const int max_context_size = llama_n_ctx(ctx);
  36. const int max_tokens_list_size = max_context_size - 4;
  37. if ((int) tokens_list.size() > max_tokens_list_size) {
  38. fprintf(stderr, "%s: error: prompt too long (%d tokens, max %d)\n", __func__, (int) tokens_list.size(), max_tokens_list_size);
  39. return 1;
  40. }
  41. fprintf(stderr, "\n\n");
  42. for (auto id : tokens_list) {
  43. fprintf(stderr, "%s", llama_token_to_piece(ctx, id).c_str());
  44. }
  45. fflush(stderr);
  46. // main loop
  47. // The LLM keeps a contextual cache memory of previous token evaluation.
  48. // Usually, once this cache is full, it is required to recompute a compressed context based on previous
  49. // tokens (see "infinite text generation via context swapping" in the main example), but in this minimalist
  50. // example, we will just stop the loop once this cache is full or once an end of stream is detected.
  51. const int n_gen = std::min(32, max_context_size);
  52. while (llama_get_kv_cache_token_count(ctx) < n_gen) {
  53. // evaluate the transformer
  54. if (llama_eval(ctx, tokens_list.data(), int(tokens_list.size()), llama_get_kv_cache_token_count(ctx), params.n_threads)) {
  55. fprintf(stderr, "%s : failed to eval\n", __func__);
  56. return 1;
  57. }
  58. tokens_list.clear();
  59. // sample the next token
  60. llama_token new_token_id = 0;
  61. auto logits = llama_get_logits(ctx);
  62. auto n_vocab = llama_n_vocab(ctx);
  63. std::vector<llama_token_data> candidates;
  64. candidates.reserve(n_vocab);
  65. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  66. candidates.emplace_back(llama_token_data{ token_id, logits[token_id], 0.0f });
  67. }
  68. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  69. new_token_id = llama_sample_token_greedy(ctx , &candidates_p);
  70. // is it an end of stream ?
  71. if (new_token_id == llama_token_eos(ctx)) {
  72. fprintf(stderr, " [end of text]\n");
  73. break;
  74. }
  75. // print the new token :
  76. printf("%s", llama_token_to_piece(ctx, new_token_id).c_str());
  77. fflush(stdout);
  78. // push this new token for next evaluation
  79. tokens_list.push_back(new_token_id);
  80. }
  81. llama_free(ctx);
  82. llama_free_model(model);
  83. llama_backend_free();
  84. fprintf(stderr, "\n\n");
  85. return 0;
  86. }