| 1695 | } |
| 1696 | |
| 1697 | void llama_kv_cache::state_read(llama_io_read_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) { |
| 1698 | GGML_UNUSED(flags); |
| 1699 | |
| 1700 | GGML_ASSERT(seq_id == -1 || (seq_id >= 0 && (size_t) seq_id < seq_to_stream.size())); |
| 1701 | |
| 1702 | uint32_t n_stream_cur; |
| 1703 | io.read_to(&n_stream_cur, sizeof(n_stream_cur)); |
| 1704 | if (n_stream_cur != n_stream) { |
| 1705 | throw std::runtime_error("n_stream mismatch"); |
| 1706 | } |
| 1707 | |
| 1708 | for (uint32_t s = 0; s < n_stream; ++s) { |
| 1709 | uint32_t cell_count; |
| 1710 | io.read_to(&cell_count, sizeof(cell_count)); |
| 1711 | |
| 1712 | if (cell_count == 0) { |
| 1713 | continue; |
| 1714 | } |
| 1715 | |
| 1716 | const uint32_t strm = seq_id == -1 ? s : seq_to_stream[seq_id]; |
| 1717 | |
| 1718 | slot_info sinfo; |
| 1719 | |
| 1720 | bool res = true; |
| 1721 | res = res && state_read_meta(io, strm, cell_count, sinfo, seq_id); |
| 1722 | res = res && state_read_data(io, strm, cell_count, sinfo); |
| 1723 | |
| 1724 | if (!res) { |
| 1725 | if (seq_id == -1) { |
| 1726 | clear(true); |
| 1727 | } else { |
| 1728 | seq_rm(seq_id, -1, -1); |
| 1729 | } |
| 1730 | throw std::runtime_error("failed to restore kv cache"); |
| 1731 | } |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | void llama_kv_cache::state_write_meta(llama_io_write_i & io, const cell_ranges_t & cr, llama_seq_id seq_id) const { |
| 1736 | const auto & cells = v_cells[cr.strm]; |
no test coverage detected