save-load-state.cpp 4.9 KB

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