| 1629 | } |
| 1630 | |
| 1631 | bool llama_context::state_load_file(const char * filepath, llama_token * tokens_out, size_t n_token_capacity, size_t * n_token_count_out) { |
| 1632 | llama_file file(filepath, "rb"); |
| 1633 | |
| 1634 | // sanity checks |
| 1635 | { |
| 1636 | const uint32_t magic = file.read_u32(); |
| 1637 | const uint32_t version = file.read_u32(); |
| 1638 | |
| 1639 | if (magic != LLAMA_SESSION_MAGIC || version != LLAMA_SESSION_VERSION) { |
| 1640 | LLAMA_LOG_ERROR("%s: unknown (magic, version) for session file: %08x, %08x\n", __func__, magic, version); |
| 1641 | return false; |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | // load the prompt |
| 1646 | { |
| 1647 | const uint32_t n_token_count = file.read_u32(); |
| 1648 | |
| 1649 | if (n_token_count > n_token_capacity) { |
| 1650 | LLAMA_LOG_ERROR("%s: token count in session file exceeded capacity! %u > %zu\n", __func__, n_token_count, n_token_capacity); |
| 1651 | return false; |
| 1652 | } |
| 1653 | |
| 1654 | file.read_raw(tokens_out, sizeof(llama_token) * n_token_count); |
| 1655 | *n_token_count_out = n_token_count; |
| 1656 | } |
| 1657 | |
| 1658 | // restore the context state |
| 1659 | { |
| 1660 | const size_t n_state_size_cur = file.size() - file.tell(); |
| 1661 | |
| 1662 | llama_io_read_file io( &file); |
| 1663 | const size_t n_read = state_read_data(io); |
| 1664 | |
| 1665 | if (n_read != n_state_size_cur) { |
| 1666 | 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); |
| 1667 | return false; |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | return true; |
| 1672 | } |
| 1673 | |
| 1674 | bool llama_context::state_save_file(const char * filepath, const llama_token * tokens, size_t n_token_count) { |
| 1675 | llama_file file(filepath, "wb"); |
no test coverage detected