| 10498 | } |
| 10499 | |
| 10500 | static bool llama_load_session_file_internal(struct llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) { |
| 10501 | llama_file file(path_session, "rb"); |
| 10502 | |
| 10503 | // sanity checks |
| 10504 | { |
| 10505 | const uint32_t magic = file.read_u32(); |
| 10506 | const uint32_t version = file.read_u32(); |
| 10507 | |
| 10508 | if (magic != LLAMA_SESSION_MAGIC || version != LLAMA_SESSION_VERSION) { |
| 10509 | LLAMA_LOG_ERROR("%s : unknown (magic, version) for session file: %08x, %08x\n", __func__, magic, version); |
| 10510 | return false; |
| 10511 | } |
| 10512 | |
| 10513 | llama_hparams session_hparams; |
| 10514 | file.read_raw(&session_hparams, sizeof(llama_hparams)); |
| 10515 | |
| 10516 | if (session_hparams != ctx->model.hparams) { |
| 10517 | LLAMA_LOG_INFO("%s : model hparams didn't match from session file!\n", __func__); |
| 10518 | return false; |
| 10519 | } |
| 10520 | } |
| 10521 | |
| 10522 | // load the prompt |
| 10523 | { |
| 10524 | const uint32_t n_token_count = file.read_u32(); |
| 10525 | |
| 10526 | if (n_token_count > n_token_capacity) { |
| 10527 | LLAMA_LOG_ERROR("%s : token count in session file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity); |
| 10528 | return false; |
| 10529 | } |
| 10530 | |
| 10531 | file.read_raw(tokens_out, sizeof(llama_token) * n_token_count); |
| 10532 | *n_token_count_out = n_token_count; |
| 10533 | } |
| 10534 | |
| 10535 | // restore the context state |
| 10536 | { |
| 10537 | const size_t n_state_size_cur = file.size - file.tell(); |
| 10538 | const size_t n_state_size_max = llama_get_state_size(ctx); |
| 10539 | |
| 10540 | if (n_state_size_cur > n_state_size_max) { |
| 10541 | LLAMA_LOG_ERROR("%s : the state size in session file is too big! max %zu, got %zu\n", __func__, n_state_size_max, n_state_size_cur); |
| 10542 | return false; |
| 10543 | } |
| 10544 | |
| 10545 | std::vector<uint8_t> state_data(n_state_size_max); |
| 10546 | file.read_raw(state_data.data(), n_state_size_cur); |
| 10547 | |
| 10548 | llama_set_state_data(ctx, state_data.data()); |
| 10549 | } |
| 10550 | |
| 10551 | return true; |
| 10552 | } |
| 10553 | |
| 10554 | bool llama_load_session_file(struct llama_context * ctx, const char * path_session, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) { |
| 10555 | try { |
no test coverage detected