| 1863 | } |
| 1864 | |
| 1865 | bool llama_kv_cache::state_read_meta(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, slot_info & sinfo, llama_seq_id dest_seq_id) { |
| 1866 | auto & cells = v_cells[strm]; |
| 1867 | auto & head = v_heads[strm]; |
| 1868 | |
| 1869 | if (dest_seq_id != -1) { |
| 1870 | // single sequence |
| 1871 | seq_rm(dest_seq_id, -1, -1); |
| 1872 | |
| 1873 | llama_batch_allocr balloc(hparams.n_pos_per_embd()); |
| 1874 | |
| 1875 | llama_ubatch ubatch = balloc.ubatch_reserve(cell_count, 1); |
| 1876 | |
| 1877 | ubatch.seq_id_unq[0] = dest_seq_id; |
| 1878 | |
| 1879 | for (uint32_t i = 0; i < cell_count; ++i) { |
| 1880 | llama_pos pos; |
| 1881 | uint32_t n_seq_id; |
| 1882 | |
| 1883 | io.read_to(&pos, sizeof(pos)); |
| 1884 | io.read_to(&n_seq_id, sizeof(n_seq_id)); |
| 1885 | |
| 1886 | if (n_seq_id != 1) { |
| 1887 | LLAMA_LOG_ERROR("%s: invalid seq_id-agnostic kv cell\n", __func__); |
| 1888 | return false; |
| 1889 | } |
| 1890 | |
| 1891 | // read the sequence id, but directly discard it - we will use dest_seq_id instead |
| 1892 | { |
| 1893 | llama_seq_id seq_id; |
| 1894 | io.read_to(&seq_id, sizeof(seq_id)); |
| 1895 | } |
| 1896 | |
| 1897 | ubatch.pos[i] = pos; |
| 1898 | ubatch.n_seq_id[i] = n_seq_id; |
| 1899 | ubatch.seq_id[i] = &dest_seq_id; |
| 1900 | } |
| 1901 | |
| 1902 | sinfo = find_slot(ubatch, false); |
| 1903 | if (sinfo.empty()) { |
| 1904 | LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__); |
| 1905 | return false; |
| 1906 | } |
| 1907 | |
| 1908 | // TODO: we cannot yet restore llama_kv_cell_ext as the apply_ubatch() does not support it yet |
| 1909 | // see: https://github.com/ggml-org/llama.cpp/pull/16825#issuecomment-3460868350 |
| 1910 | apply_ubatch(sinfo, ubatch); |
| 1911 | |
| 1912 | LLAMA_LOG_DEBUG("%s: cell_count = %d, dest_seq_id = %d\n", __func__, cell_count, dest_seq_id); |
| 1913 | |
| 1914 | // DEBUG CHECK: verify that all cells were allocated and have correct seq_id and pos values |
| 1915 | GGML_ASSERT(sinfo.n_stream() == 1); |
| 1916 | GGML_ASSERT(sinfo.idxs[0].size() == cell_count); |
| 1917 | for (uint32_t i = 0; i < cell_count; ++i) { |
| 1918 | const uint32_t idx = sinfo.idxs[0][i]; |
| 1919 | GGML_ASSERT(cells.pos_get(idx) == ubatch.pos[i]); |
| 1920 | GGML_ASSERT(cells.seq_has(idx, dest_seq_id)); |
| 1921 | } |
| 1922 | } else { |
nothing calls this directly
no test coverage detected