save-load-state.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 n_past = 0;
  21. auto last_n_tokens_data = std::vector<llama_token>(params.repeat_last_n, 0);
  22. // init
  23. llama_model * model;
  24. llama_context * ctx;
  25. std::tie(model, ctx) = llama_init_from_gpt_params( params );
  26. if (model == nullptr) {
  27. return 1;
  28. }
  29. if (ctx == nullptr) {
  30. llama_free_model(model);
  31. return 1;
  32. }
  33. auto tokens = llama_tokenize(ctx, params.prompt, true);
  34. auto n_prompt_tokens = tokens.size();
  35. if (n_prompt_tokens < 1) {
  36. fprintf(stderr, "%s : failed to tokenize prompt\n", __func__);
  37. llama_free(ctx);
  38. llama_free_model(model);
  39. return 1;
  40. }
  41. // evaluate prompt
  42. llama_decode(ctx, llama_batch_get_one(tokens.data(), n_prompt_tokens, n_past, 0));
  43. last_n_tokens_data.insert(last_n_tokens_data.end(), tokens.data(), tokens.data() + n_prompt_tokens);
  44. n_past += n_prompt_tokens;
  45. const size_t state_size = llama_get_state_size(ctx);
  46. uint8_t * state_mem = new uint8_t[state_size];
  47. // Save state (rng, logits, embedding and kv_cache) to file
  48. {
  49. FILE *fp_write = fopen("dump_state.bin", "wb");
  50. llama_copy_state_data(ctx, state_mem); // could also copy directly to memory mapped file
  51. fwrite(state_mem, 1, state_size, fp_write);
  52. fclose(fp_write);
  53. }
  54. // save state (last tokens)
  55. const auto last_n_tokens_data_saved = std::vector<llama_token>(last_n_tokens_data);
  56. const auto n_past_saved = n_past;
  57. // first run
  58. printf("\n%s", params.prompt.c_str());
  59. for (auto i = 0; i < params.n_predict; i++) {
  60. auto * logits = llama_get_logits(ctx);
  61. auto n_vocab = llama_n_vocab(model);
  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. auto next_token = llama_sample_token(ctx, &candidates_p);
  69. auto next_token_str = llama_token_to_piece(ctx, next_token);
  70. last_n_tokens_data.push_back(next_token);
  71. printf("%s", next_token_str.c_str());
  72. if (llama_decode(ctx, llama_batch_get_one(&next_token, 1, n_past, 0))) {
  73. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  74. llama_free(ctx);
  75. llama_free_model(model);
  76. return 1;
  77. }
  78. n_past += 1;
  79. }
  80. printf("\n\n");
  81. // free old context
  82. llama_free(ctx);
  83. // make new context
  84. auto * ctx2 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
  85. // Load state (rng, logits, embedding and kv_cache) from file
  86. {
  87. FILE *fp_read = fopen("dump_state.bin", "rb");
  88. if (state_size != llama_get_state_size(ctx2)) {
  89. fprintf(stderr, "\n%s : failed to validate state size\n", __func__);
  90. llama_free(ctx2);
  91. llama_free_model(model);
  92. return 1;
  93. }
  94. const size_t ret = fread(state_mem, 1, state_size, fp_read);
  95. if (ret != state_size) {
  96. fprintf(stderr, "\n%s : failed to read state\n", __func__);
  97. llama_free(ctx2);
  98. llama_free_model(model);
  99. return 1;
  100. }
  101. llama_set_state_data(ctx2, state_mem); // could also read directly from memory mapped file
  102. fclose(fp_read);
  103. }
  104. delete[] state_mem;
  105. // restore state (last tokens)
  106. last_n_tokens_data = last_n_tokens_data_saved;
  107. n_past = n_past_saved;
  108. // second run
  109. for (auto i = 0; i < params.n_predict; i++) {
  110. auto * logits = llama_get_logits(ctx2);
  111. auto n_vocab = llama_n_vocab(model);
  112. std::vector<llama_token_data> candidates;
  113. candidates.reserve(n_vocab);
  114. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  115. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  116. }
  117. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  118. auto next_token = llama_sample_token(ctx2, &candidates_p);
  119. auto next_token_str = llama_token_to_piece(ctx2, next_token);
  120. last_n_tokens_data.push_back(next_token);
  121. printf("%s", next_token_str.c_str());
  122. if (llama_decode(ctx, llama_batch_get_one(&next_token, 1, n_past, 0))) {
  123. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  124. llama_free(ctx2);
  125. llama_free_model(model);
  126. return 1;
  127. }
  128. n_past += 1;
  129. }
  130. printf("\n\n");
  131. llama_free(ctx2);
  132. llama_free_model(model);
  133. return 0;
  134. }