| 1229 | } |
| 1230 | |
| 1231 | void llama_kv_cache_unified::state_write(llama_io_write_i & io, llama_seq_id seq_id) const { |
| 1232 | std::vector<std::pair<uint32_t, uint32_t>> cell_ranges; // ranges, from inclusive, to exclusive |
| 1233 | uint32_t cell_count = 0; |
| 1234 | |
| 1235 | // Count the number of cells with the specified seq_id |
| 1236 | // Find all the ranges of cells with this seq id (or all, when -1) |
| 1237 | uint32_t cell_range_begin = cells.size(); |
| 1238 | |
| 1239 | for (uint32_t i = 0; i < cells.size(); ++i) { |
| 1240 | if (!cells.is_empty(i) && (seq_id == -1 || cells.seq_has(i, seq_id))) { |
| 1241 | ++cell_count; |
| 1242 | if (cell_range_begin == cells.size()) { |
| 1243 | cell_range_begin = i; |
| 1244 | } |
| 1245 | } else { |
| 1246 | if (cell_range_begin != cells.size()) { |
| 1247 | cell_ranges.emplace_back(cell_range_begin, i); |
| 1248 | cell_range_begin = cells.size(); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | if (cell_range_begin != cells.size()) { |
| 1254 | cell_ranges.emplace_back(cell_range_begin, cells.size()); |
| 1255 | } |
| 1256 | |
| 1257 | // DEBUG CHECK: Sum of cell counts in ranges should equal the total cell count |
| 1258 | uint32_t cell_count_check = 0; |
| 1259 | for (const auto & range : cell_ranges) { |
| 1260 | cell_count_check += range.second - range.first; |
| 1261 | } |
| 1262 | GGML_ASSERT(cell_count == cell_count_check); |
| 1263 | |
| 1264 | io.write(&cell_count, sizeof(cell_count)); |
| 1265 | |
| 1266 | state_write_meta(io, cell_ranges, seq_id); |
| 1267 | state_write_data(io, cell_ranges); |
| 1268 | } |
| 1269 | |
| 1270 | void llama_kv_cache_unified::state_read(llama_io_read_i & io, llama_seq_id seq_id) { |
| 1271 | uint32_t cell_count; |
no test coverage detected