save-load-state.cpp 5.3 KB

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