| 2366 | } |
| 2367 | |
| 2368 | bool llama_context::state_load_file(const char * filepath, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) { |
| 2369 | llama_file file(filepath, "rb"); |
| 2370 | |
| 2371 | // sanity checks |
| 2372 | { |
| 2373 | const uint32_t magic = file.read_u32(); |
| 2374 | const uint32_t version = file.read_u32(); |
| 2375 | |
| 2376 | if (magic != LLAMA_SESSION_MAGIC || version != LLAMA_SESSION_VERSION) { |
| 2377 | LLAMA_LOG_ERROR("%s: unknown (magic, version) for session file: %08x, %08x\n", __func__, magic, version); |
| 2378 | return false; |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | // load the prompt |
| 2383 | { |
| 2384 | const uint32_t n_token_count = file.read_u32(); |
| 2385 | |
| 2386 | if (n_token_count > n_token_capacity) { |
| 2387 | LLAMA_LOG_ERROR("%s: token count in session file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity); |
| 2388 | return false; |
| 2389 | } |
| 2390 | |
| 2391 | file.read_raw(tokens_out, sizeof(llama_token) * n_token_count); |
| 2392 | *n_token_count_out = n_token_count; |
| 2393 | } |
| 2394 | |
| 2395 | // restore the context state |
| 2396 | { |
| 2397 | const size_t n_state_size_cur = file.size() - file.tell(); |
| 2398 | |
| 2399 | llama_io_read_file io( &file); |
| 2400 | const size_t n_read = state_read_data(io); |
| 2401 | |
| 2402 | if (n_read != n_state_size_cur) { |
| 2403 | LLAMA_LOG_ERROR("%s: did not read all of the session file data! size %zu, got %zu\n", __func__, n_state_size_cur, n_read); |
| 2404 | return false; |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | return true; |
| 2409 | } |
| 2410 | |
| 2411 | bool llama_context::state_save_file(const char * filepath, const llama_token * tokens, size_t n_token_count) { |
| 2412 | llama_file file(filepath, "wb"); |
no test coverage detected