save-load-state.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "build-info.h"
  2. #include "common.h"
  3. #include "llama.h"
  4. #include <vector>
  5. #include <cstdio>
  6. #include <chrono>
  7. int main(int argc, char ** argv) {
  8. gpt_params params;
  9. params.seed = 42;
  10. params.n_threads = 4;
  11. params.repeat_last_n = 64;
  12. params.prompt = "The quick brown fox";
  13. if (!gpt_params_parse(argc, argv, params)) {
  14. return 1;
  15. }
  16. print_build_info();
  17. if (params.n_predict < 0) {
  18. params.n_predict = 16;
  19. }
  20. auto lparams = llama_context_default_params();
  21. lparams.n_ctx = params.n_ctx;
  22. lparams.seed = params.seed;
  23. lparams.f16_kv = params.memory_f16;
  24. lparams.use_mmap = params.use_mmap;
  25. lparams.use_mlock = params.use_mlock;
  26. auto n_past = 0;
  27. auto last_n_tokens_data = std::vector<llama_token>(params.repeat_last_n, 0);
  28. // init
  29. auto * model = llama_load_model_from_file(params.model.c_str(), lparams);
  30. if (model == nullptr) {
  31. return 1;
  32. }
  33. auto * ctx = llama_new_context_with_model(model, lparams);
  34. if (ctx == nullptr) {
  35. llama_free_model(model);
  36. return 1;
  37. }
  38. auto tokens = llama_tokenize(ctx, params.prompt, true);
  39. auto n_prompt_tokens = tokens.size();
  40. if (n_prompt_tokens < 1) {
  41. fprintf(stderr, "%s : failed to tokenize prompt\n", __func__);
  42. llama_free(ctx);
  43. llama_free_model(model);
  44. return 1;
  45. }
  46. // evaluate prompt
  47. llama_decode(ctx, llama_batch_get_one(tokens.data(), n_prompt_tokens, n_past, 0), params.n_threads);
  48. last_n_tokens_data.insert(last_n_tokens_data.end(), tokens.data(), tokens.data() + n_prompt_tokens);
  49. n_past += n_prompt_tokens;
  50. const size_t state_size = llama_get_state_size(ctx);
  51. uint8_t * state_mem = new uint8_t[state_size];
  52. // Save state (rng, logits, embedding and kv_cache) to file
  53. {
  54. FILE *fp_write = fopen("dump_state.bin", "wb");
  55. llama_copy_state_data(ctx, state_mem); // could also copy directly to memory mapped file
  56. fwrite(state_mem, 1, state_size, fp_write);
  57. fclose(fp_write);
  58. }
  59. // save state (last tokens)
  60. const auto last_n_tokens_data_saved = std::vector<llama_token>(last_n_tokens_data);
  61. const auto n_past_saved = n_past;
  62. // first run
  63. printf("\n%s", params.prompt.c_str());
  64. for (auto i = 0; i < params.n_predict; i++) {
  65. auto * logits = llama_get_logits(ctx);
  66. auto n_vocab = llama_n_vocab(ctx);
  67. std::vector<llama_token_data> candidates;
  68. candidates.reserve(n_vocab);
  69. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  70. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  71. }
  72. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  73. auto next_token = llama_sample_token(ctx, &candidates_p);
  74. auto next_token_str = llama_token_to_piece(ctx, next_token);
  75. last_n_tokens_data.push_back(next_token);
  76. printf("%s", next_token_str.c_str());
  77. if (llama_decode(ctx, llama_batch_get_one(&next_token, 1, n_past, 0), params.n_threads)) {
  78. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  79. llama_free(ctx);
  80. llama_free_model(model);
  81. return 1;
  82. }
  83. n_past += 1;
  84. }
  85. printf("\n\n");
  86. // free old context
  87. llama_free(ctx);
  88. // make new context
  89. auto * ctx2 = llama_new_context_with_model(model, lparams);
  90. // Load state (rng, logits, embedding and kv_cache) from file
  91. {
  92. FILE *fp_read = fopen("dump_state.bin", "rb");
  93. if (state_size != llama_get_state_size(ctx2)) {
  94. fprintf(stderr, "\n%s : failed to validate state size\n", __func__);
  95. llama_free(ctx2);
  96. llama_free_model(model);
  97. return 1;
  98. }
  99. const size_t ret = fread(state_mem, 1, state_size, fp_read);
  100. if (ret != state_size) {
  101. fprintf(stderr, "\n%s : failed to read state\n", __func__);
  102. llama_free(ctx2);
  103. llama_free_model(model);
  104. return 1;
  105. }
  106. llama_set_state_data(ctx2, state_mem); // could also read directly from memory mapped file
  107. fclose(fp_read);
  108. }
  109. delete[] state_mem;
  110. // restore state (last tokens)
  111. last_n_tokens_data = last_n_tokens_data_saved;
  112. n_past = n_past_saved;
  113. // second run
  114. for (auto i = 0; i < params.n_predict; i++) {
  115. auto * logits = llama_get_logits(ctx2);
  116. auto n_vocab = llama_n_vocab(ctx2);
  117. std::vector<llama_token_data> candidates;
  118. candidates.reserve(n_vocab);
  119. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  120. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  121. }
  122. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  123. auto next_token = llama_sample_token(ctx2, &candidates_p);
  124. auto next_token_str = llama_token_to_piece(ctx2, next_token);
  125. last_n_tokens_data.push_back(next_token);
  126. printf("%s", next_token_str.c_str());
  127. if (llama_decode(ctx, llama_batch_get_one(&next_token, 1, n_past, 0), params.n_threads)) {
  128. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  129. llama_free(ctx2);
  130. llama_free_model(model);
  131. return 1;
  132. }
  133. n_past += 1;
  134. }
  135. printf("\n\n");
  136. llama_free(ctx2);
  137. llama_free_model(model);
  138. return 0;
  139. }