save-load-state.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "common.h"
  2. #include "llama.h"
  3. #include "build-info.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) == false) {
  14. return 1;
  15. }
  16. fprintf(stderr, "%s: build = %d (%s)\n", __func__, BUILD_NUMBER, BUILD_COMMIT);
  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.n_parts = params.n_parts;
  23. lparams.seed = params.seed;
  24. lparams.f16_kv = params.memory_f16;
  25. lparams.use_mmap = params.use_mmap;
  26. lparams.use_mlock = params.use_mlock;
  27. auto n_past = 0;
  28. auto last_n_tokens_data = std::vector<llama_token>(params.repeat_last_n, 0);
  29. // init
  30. auto ctx = llama_init_from_file(params.model.c_str(), lparams);
  31. auto tokens = std::vector<llama_token>(params.n_ctx);
  32. auto n_prompt_tokens = llama_tokenize(ctx, params.prompt.c_str(), tokens.data(), tokens.size(), true);
  33. if (n_prompt_tokens < 1) {
  34. fprintf(stderr, "%s : failed to tokenize prompt\n", __func__);
  35. return 1;
  36. }
  37. // evaluate prompt
  38. llama_eval(ctx, tokens.data(), n_prompt_tokens, n_past, params.n_threads);
  39. last_n_tokens_data.insert(last_n_tokens_data.end(), tokens.data(), tokens.data() + n_prompt_tokens);
  40. n_past += n_prompt_tokens;
  41. const size_t state_size = llama_get_state_size(ctx);
  42. uint8_t * state_mem = new uint8_t[state_size];
  43. // Save state (rng, logits, embedding and kv_cache) to file
  44. {
  45. FILE *fp_write = fopen("dump_state.bin", "wb");
  46. llama_copy_state_data(ctx, state_mem); // could also copy directly to memory mapped file
  47. fwrite(state_mem, 1, state_size, fp_write);
  48. fclose(fp_write);
  49. }
  50. // save state (last tokens)
  51. const auto last_n_tokens_data_saved = std::vector<llama_token>(last_n_tokens_data);
  52. const auto n_past_saved = n_past;
  53. // first run
  54. printf("\n%s", params.prompt.c_str());
  55. for (auto i = 0; i < params.n_predict; i++) {
  56. auto logits = llama_get_logits(ctx);
  57. auto n_vocab = llama_n_vocab(ctx);
  58. std::vector<llama_token_data> candidates;
  59. candidates.reserve(n_vocab);
  60. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  61. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  62. }
  63. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  64. auto next_token = llama_sample_token(ctx, &candidates_p);
  65. auto next_token_str = llama_token_to_str(ctx, next_token);
  66. last_n_tokens_data.push_back(next_token);
  67. printf("%s", next_token_str);
  68. if (llama_eval(ctx, &next_token, 1, n_past, params.n_threads)) {
  69. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  70. return 1;
  71. }
  72. n_past += 1;
  73. }
  74. printf("\n\n");
  75. // free old model
  76. llama_free(ctx);
  77. // load new model
  78. auto ctx2 = llama_init_from_file(params.model.c_str(), lparams);
  79. // Load state (rng, logits, embedding and kv_cache) from file
  80. {
  81. FILE *fp_read = fopen("dump_state.bin", "rb");
  82. if (state_size != llama_get_state_size(ctx2)) {
  83. fprintf(stderr, "\n%s : failed to validate state size\n", __func__);
  84. return 1;
  85. }
  86. const size_t ret = fread(state_mem, 1, state_size, fp_read);
  87. if (ret != state_size) {
  88. fprintf(stderr, "\n%s : failed to read state\n", __func__);
  89. return 1;
  90. }
  91. llama_set_state_data(ctx2, state_mem); // could also read directly from memory mapped file
  92. fclose(fp_read);
  93. }
  94. delete[] state_mem;
  95. // restore state (last tokens)
  96. last_n_tokens_data = last_n_tokens_data_saved;
  97. n_past = n_past_saved;
  98. // second run
  99. for (auto i = 0; i < params.n_predict; i++) {
  100. auto logits = llama_get_logits(ctx2);
  101. auto n_vocab = llama_n_vocab(ctx2);
  102. std::vector<llama_token_data> candidates;
  103. candidates.reserve(n_vocab);
  104. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  105. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  106. }
  107. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  108. auto next_token = llama_sample_token(ctx2, &candidates_p);
  109. auto next_token_str = llama_token_to_str(ctx2, next_token);
  110. last_n_tokens_data.push_back(next_token);
  111. printf("%s", next_token_str);
  112. if (llama_eval(ctx2, &next_token, 1, n_past, params.n_threads)) {
  113. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  114. return 1;
  115. }
  116. n_past += 1;
  117. }
  118. printf("\n\n");
  119. return 0;
  120. }