save-load-state.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "common.h"
  2. #include "llama.h"
  3. #include <vector>
  4. #include <cstdio>
  5. #include <chrono>
  6. int main(int argc, char ** argv) {
  7. gpt_params params;
  8. params.prompt = "The quick brown fox";
  9. if (!gpt_params_parse(argc, argv, params)) {
  10. return 1;
  11. }
  12. print_build_info();
  13. if (params.n_predict < 0) {
  14. params.n_predict = 16;
  15. }
  16. auto n_past = 0;
  17. std::string result0;
  18. std::string result1;
  19. std::string result2;
  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_state_get_size(ctx));
  36. const size_t written = llama_state_get_data(ctx, state_mem.data());
  37. FILE *fp_write = fopen("dump_state.bin", "wb");
  38. fwrite(state_mem.data(), 1, written, fp_write);
  39. fclose(fp_write);
  40. fprintf(stderr, "%s : serialized state into %zd out of a maximum of %zd bytes\n", __func__, written, state_mem.size());
  41. }
  42. // save state (last tokens)
  43. const auto n_past_saved = n_past;
  44. // first run
  45. printf("\nfirst run: %s", params.prompt.c_str());
  46. for (auto i = 0; i < params.n_predict; i++) {
  47. auto * logits = llama_get_logits(ctx);
  48. auto n_vocab = llama_n_vocab(model);
  49. std::vector<llama_token_data> candidates;
  50. candidates.reserve(n_vocab);
  51. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  52. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  53. }
  54. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  55. auto next_token = llama_sample_token(ctx, &candidates_p);
  56. auto next_token_str = llama_token_to_piece(ctx, next_token);
  57. printf("%s", next_token_str.c_str());
  58. result0 += next_token_str;
  59. if (llama_decode(ctx, llama_batch_get_one(&next_token, 1, n_past, 0))) {
  60. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  61. llama_free(ctx);
  62. llama_free_model(model);
  63. return 1;
  64. }
  65. n_past += 1;
  66. }
  67. printf("\n\n");
  68. // free old context
  69. llama_free(ctx);
  70. // make new context
  71. auto * ctx2 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
  72. printf("\nsecond run: %s", params.prompt.c_str());
  73. // load state (rng, logits, embedding and kv_cache) from file
  74. {
  75. std::vector<uint8_t> state_mem(llama_state_get_size(ctx2));
  76. FILE * fp_read = fopen("dump_state.bin", "rb");
  77. const size_t read = fread(state_mem.data(), 1, state_mem.size(), fp_read);
  78. fclose(fp_read);
  79. if (read != llama_state_set_data(ctx2, state_mem.data())) {
  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. fprintf(stderr, "%s : deserialized state from %zd out of a maximum of %zd bytes\n", __func__, read, state_mem.size());
  86. }
  87. // restore state (last tokens)
  88. n_past = n_past_saved;
  89. // second run
  90. for (auto i = 0; i < params.n_predict; i++) {
  91. auto * logits = llama_get_logits(ctx2);
  92. auto n_vocab = llama_n_vocab(model);
  93. std::vector<llama_token_data> candidates;
  94. candidates.reserve(n_vocab);
  95. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  96. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  97. }
  98. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  99. auto next_token = llama_sample_token(ctx2, &candidates_p);
  100. auto next_token_str = llama_token_to_piece(ctx2, next_token);
  101. printf("%s", next_token_str.c_str());
  102. result1 += next_token_str;
  103. if (llama_decode(ctx2, llama_batch_get_one(&next_token, 1, n_past, 0))) {
  104. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  105. llama_free(ctx2);
  106. llama_free_model(model);
  107. return 1;
  108. }
  109. n_past += 1;
  110. }
  111. printf("\n\n");
  112. llama_free(ctx2);
  113. if (result0 != result1) {
  114. fprintf(stderr, "\n%s : error : the 2 generations are different\n", __func__);
  115. return 1;
  116. }
  117. // make new context
  118. auto* ctx3 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
  119. printf("\nsingle seq run: %s", params.prompt.c_str());
  120. // load state (rng, logits, embedding and kv_cache) from file
  121. {
  122. std::vector<uint8_t> state_mem(llama_state_get_size(ctx3));
  123. FILE * fp_read = fopen("dump_state.bin", "rb");
  124. const size_t read = fread(state_mem.data(), 1, state_mem.size(), fp_read);
  125. fclose(fp_read);
  126. if (read != llama_state_set_data(ctx3, state_mem.data())) {
  127. fprintf(stderr, "\n%s : failed to read state\n", __func__);
  128. llama_free(ctx3);
  129. llama_free_model(model);
  130. return 1;
  131. }
  132. fprintf(stderr, "%s : deserialized state from %zd out of a maximum of %zd bytes\n", __func__, read, state_mem.size());
  133. }
  134. // restore state (last tokens)
  135. n_past = n_past_saved;
  136. // save seq 0 and load into seq 1
  137. {
  138. // save kv of seq 0
  139. std::vector<uint8_t> seq_store(llama_state_seq_get_size(ctx3, 0));
  140. const size_t ncopy = llama_state_seq_get_data(ctx3, seq_store.data(), 0);
  141. if (ncopy != seq_store.size()) {
  142. fprintf(stderr, "\n%s : seq copy data length %zd does not match expected length %zd\n", __func__, ncopy, seq_store.size());
  143. llama_free(ctx3);
  144. llama_free_model(model);
  145. return 1;
  146. }
  147. fprintf(stderr, "%s : seq 0 copied, %zd bytes\n", __func__, ncopy);
  148. // erase whole kv
  149. llama_kv_cache_clear(ctx3);
  150. fprintf(stderr, "%s : kv cache cleared\n", __func__);
  151. // restore kv into seq 1
  152. const size_t nset = llama_state_seq_set_data(ctx3, seq_store.data(), 1);
  153. if (nset != seq_store.size()) {
  154. fprintf(stderr, "\n%s : seq set data length %zd does not match expected length %zd\n", __func__, nset, seq_store.size());
  155. llama_free(ctx3);
  156. llama_free_model(model);
  157. return 1;
  158. }
  159. fprintf(stderr, "%s : seq 1 restored, %zd bytes\n", __func__, nset);
  160. }
  161. // third run with seq 1 instead of 0
  162. for (auto i = 0; i < params.n_predict; i++) {
  163. auto * logits = llama_get_logits(ctx3);
  164. auto n_vocab = llama_n_vocab(model);
  165. std::vector<llama_token_data> candidates;
  166. candidates.reserve(n_vocab);
  167. for (llama_token token_id = 0; token_id < n_vocab; token_id++) {
  168. candidates.emplace_back(llama_token_data{token_id, logits[token_id], 0.0f});
  169. }
  170. llama_token_data_array candidates_p = { candidates.data(), candidates.size(), false };
  171. auto next_token = llama_sample_token(ctx3, &candidates_p);
  172. auto next_token_str = llama_token_to_piece(ctx3, next_token);
  173. printf("%s", next_token_str.c_str());
  174. result2 += next_token_str;
  175. if (llama_decode(ctx3, llama_batch_get_one(&next_token, 1, n_past, 1))) {
  176. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  177. llama_free(ctx3);
  178. llama_free_model(model);
  179. return 1;
  180. }
  181. n_past += 1;
  182. }
  183. printf("\n");
  184. llama_free(ctx3);
  185. llama_free_model(model);
  186. if (result0 != result2) {
  187. fprintf(stderr, "\n%s : error : the seq restore generation is different\n", __func__);
  188. return 1;
  189. }
  190. fprintf(stderr, "\n%s : success\n", __func__);
  191. return 0;
  192. }