1
0

save-load-state.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "common.h"
  2. #include "llama.h"
  3. #include <vector>
  4. #include <cstdio>
  5. int main(int argc, char ** argv) {
  6. gpt_params params;
  7. params.prompt = "The quick brown fox";
  8. params.sparams.seed = 1234;
  9. auto options = gpt_params_parser_init(params, LLAMA_EXAMPLE_COMMON);
  10. if (!gpt_params_parse(argc, argv, params, options)) {
  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. std::string result2;
  21. // init
  22. llama_init_result llama_init = llama_init_from_gpt_params(params);
  23. llama_model * model = llama_init.model;
  24. llama_context * ctx = llama_init.context;
  25. if (model == nullptr || ctx == nullptr) {
  26. fprintf(stderr, "%s : failed to init\n", __func__);
  27. return 1;
  28. }
  29. auto sparams = llama_sampler_chain_default_params();
  30. llama_sampler * smpl = llama_sampler_chain_init(sparams);
  31. llama_sampler_chain_add(smpl, llama_sampler_init_softmax());
  32. llama_sampler_chain_add(smpl, llama_sampler_init_dist(params.sparams.seed));
  33. // tokenize prompt
  34. auto tokens = llama_tokenize(ctx, params.prompt, true);
  35. // evaluate prompt
  36. llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size(), n_past, 0));
  37. n_past += tokens.size();
  38. // save state (rng, logits, embedding and kv_cache) to file
  39. {
  40. std::vector<uint8_t> state_mem(llama_state_get_size(ctx));
  41. const size_t written = llama_state_get_data(ctx, state_mem.data(), state_mem.size());
  42. FILE *fp_write = fopen("dump_state.bin", "wb");
  43. fwrite(state_mem.data(), 1, written, fp_write);
  44. fclose(fp_write);
  45. fprintf(stderr, "%s : serialized state into %zd out of a maximum of %zd bytes\n", __func__, written, state_mem.size());
  46. }
  47. // save state (last tokens)
  48. const auto n_past_saved = n_past;
  49. // first run
  50. printf("\nfirst run: %s", params.prompt.c_str());
  51. for (auto i = 0; i < params.n_predict; i++) {
  52. auto next_token = llama_sampler_sample(smpl, ctx, -1);
  53. auto next_token_str = llama_token_to_piece(ctx, next_token);
  54. printf("%s", next_token_str.c_str());
  55. result0 += next_token_str;
  56. if (llama_decode(ctx, llama_batch_get_one(&next_token, 1, n_past, 0))) {
  57. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  58. llama_free(ctx);
  59. llama_free_model(model);
  60. return 1;
  61. }
  62. n_past += 1;
  63. }
  64. printf("\n\n");
  65. // free old context
  66. llama_free(ctx);
  67. // make new context
  68. auto * ctx2 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
  69. llama_sampler * smpl2 = llama_sampler_chain_init(sparams);
  70. llama_sampler_chain_add(smpl2, llama_sampler_init_softmax());
  71. llama_sampler_chain_add(smpl2, llama_sampler_init_dist(params.sparams.seed));
  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;
  76. FILE * fp_read = fopen("dump_state.bin", "rb");
  77. fseek(fp_read, 0, SEEK_END);
  78. state_mem.resize(ftell(fp_read));
  79. fseek(fp_read, 0, SEEK_SET);
  80. const size_t read = fread(state_mem.data(), 1, state_mem.size(), fp_read);
  81. fclose(fp_read);
  82. if (read != llama_state_set_data(ctx2, state_mem.data(), state_mem.size())) {
  83. fprintf(stderr, "\n%s : failed to read state\n", __func__);
  84. llama_free(ctx2);
  85. llama_free_model(model);
  86. return 1;
  87. }
  88. fprintf(stderr, "%s : deserialized state from %zd out of a maximum of %zd bytes\n", __func__, read, state_mem.size());
  89. }
  90. // restore state (last tokens)
  91. n_past = n_past_saved;
  92. // second run
  93. for (auto i = 0; i < params.n_predict; i++) {
  94. auto next_token = llama_sampler_sample(smpl2, ctx2, -1);
  95. auto next_token_str = llama_token_to_piece(ctx2, next_token);
  96. printf("%s", next_token_str.c_str());
  97. result1 += next_token_str;
  98. if (llama_decode(ctx2, llama_batch_get_one(&next_token, 1, n_past, 0))) {
  99. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  100. llama_free(ctx2);
  101. llama_free_model(model);
  102. return 1;
  103. }
  104. n_past += 1;
  105. }
  106. printf("\n\n");
  107. llama_free(ctx2);
  108. if (result0 != result1) {
  109. fprintf(stderr, "\n%s : error : the 2 generations are different\n", __func__);
  110. return 1;
  111. }
  112. // make new context
  113. auto * ctx3 = llama_new_context_with_model(model, llama_context_params_from_gpt_params(params));
  114. llama_sampler * smpl3 = llama_sampler_chain_init(sparams);
  115. llama_sampler_chain_add(smpl3, llama_sampler_init_softmax());
  116. llama_sampler_chain_add(smpl3, llama_sampler_init_dist(params.sparams.seed));
  117. printf("\nsingle seq run: %s", params.prompt.c_str());
  118. // load state (rng, logits, embedding and kv_cache) from file
  119. {
  120. std::vector<uint8_t> state_mem;
  121. FILE * fp_read = fopen("dump_state.bin", "rb");
  122. fseek(fp_read, 0, SEEK_END);
  123. state_mem.resize(ftell(fp_read));
  124. fseek(fp_read, 0, SEEK_SET);
  125. const size_t read = fread(state_mem.data(), 1, state_mem.size(), fp_read);
  126. fclose(fp_read);
  127. if (read != llama_state_set_data(ctx3, state_mem.data(), state_mem.size())) {
  128. fprintf(stderr, "\n%s : failed to read state\n", __func__);
  129. llama_free(ctx3);
  130. llama_free_model(model);
  131. return 1;
  132. }
  133. fprintf(stderr, "%s : deserialized state from %zd out of a maximum of %zd bytes\n", __func__, read, state_mem.size());
  134. }
  135. // restore state (last tokens)
  136. n_past = n_past_saved;
  137. // save seq 0 and load into seq 1
  138. {
  139. // save kv of seq 0
  140. std::vector<uint8_t> seq_store(llama_state_seq_get_size(ctx3, 0));
  141. const size_t ncopy = llama_state_seq_get_data(ctx3, seq_store.data(), seq_store.size(), 0);
  142. if (ncopy != seq_store.size()) {
  143. fprintf(stderr, "\n%s : seq copy data length %zd does not match expected length %zd\n", __func__, ncopy, seq_store.size());
  144. llama_free(ctx3);
  145. llama_free_model(model);
  146. return 1;
  147. }
  148. fprintf(stderr, "%s : seq 0 copied, %zd bytes\n", __func__, ncopy);
  149. // erase whole kv
  150. llama_kv_cache_clear(ctx3);
  151. fprintf(stderr, "%s : kv cache cleared\n", __func__);
  152. // restore kv into seq 1
  153. const size_t nset = llama_state_seq_set_data(ctx3, seq_store.data(), seq_store.size(), 1);
  154. if (nset != seq_store.size()) {
  155. fprintf(stderr, "\n%s : seq set data length %zd does not match expected length %zd\n", __func__, nset, seq_store.size());
  156. llama_free(ctx3);
  157. llama_free_model(model);
  158. return 1;
  159. }
  160. fprintf(stderr, "%s : seq 1 restored, %zd bytes\n", __func__, nset);
  161. }
  162. // third run with seq 1 instead of 0
  163. for (auto i = 0; i < params.n_predict; i++) {
  164. auto next_token = llama_sampler_sample(smpl3, ctx3, -1);
  165. auto next_token_str = llama_token_to_piece(ctx3, next_token);
  166. printf("%s", next_token_str.c_str());
  167. result2 += next_token_str;
  168. if (llama_decode(ctx3, llama_batch_get_one(&next_token, 1, n_past, 1))) {
  169. fprintf(stderr, "\n%s : failed to evaluate\n", __func__);
  170. llama_free(ctx3);
  171. llama_free_model(model);
  172. return 1;
  173. }
  174. n_past += 1;
  175. }
  176. printf("\n");
  177. llama_sampler_free(smpl);
  178. llama_sampler_free(smpl2);
  179. llama_sampler_free(smpl3);
  180. llama_free(ctx3);
  181. llama_free_model(model);
  182. if (result0 != result2) {
  183. fprintf(stderr, "\n%s : error : the seq restore generation is different\n", __func__);
  184. return 1;
  185. }
  186. fprintf(stderr, "\n%s : success\n", __func__);
  187. return 0;
  188. }