1
0

save-load-state.cpp 5.2 KB

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