save-load-state.cpp 8.3 KB

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