1
0

save-load-state.cpp 4.6 KB

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