simple.cpp 3.4 KB

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