From bc1c346ae8afd90db846377dff59e4ed603473ad Mon Sep 17 00:00:00 2001 From: KerfuffleV2 Date: Thu, 23 Nov 2023 07:44:10 -0700 Subject: [PATCH] Fix off by one error in dump_kv_cache_view --- common/common.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/common/common.cpp b/common/common.cpp index c9b13db27..77f61dbf9 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1397,8 +1397,10 @@ void dump_kv_cache_view(const llama_kv_cache_view & view, int row_size) { printf("=== Dumping KV cache. total cells %d, max sequences per cell %d, populated cells %d, total tokens in cache %d, largest empty slot=%d @ %d", view.n_cells, view.n_max_seq, view.used_cells, view.token_count, view.max_contiguous_cells, view.max_contiguous_cells_idx); + llama_kv_cache_view_cell * c_curr = view.cells; struct llama_kv_cache_view_cell_sequence * cs_curr = view.cells_sequences; + for (int i = 0; i < view.n_cells; i++, c_curr++, cs_curr += view.n_max_seq) { if (i % row_size == 0) { printf("\n%5d: ", i); @@ -1407,19 +1409,22 @@ void dump_kv_cache_view(const llama_kv_cache_view & view, int row_size) { for (int j = 0; j < view.n_max_seq; j++) { if (cs_curr[j].seq_id >= 0) { seq_count++; } } - putchar(slot_chars[std::min(sizeof(slot_chars) - 1, size_t(seq_count))]); + putchar(slot_chars[std::min(sizeof(slot_chars) - 2, size_t(seq_count))]); } + printf("\n=== Done dumping\n"); } void dump_kv_cache_view_seqs(const llama_kv_cache_view & view, int row_size) { static const char slot_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + printf("=== Dumping KV cache. total cells %d, max sequences per cell %d, populated cells %d, total tokens in cache %d, largest empty slot=%d @ %d\n", view.n_cells, view.n_max_seq, view.used_cells, view.token_count, view.max_contiguous_cells, view.max_contiguous_cells_idx); std::unordered_map seqs; llama_kv_cache_view_cell * c_curr = view.cells; struct llama_kv_cache_view_cell_sequence * cs_curr = view.cells_sequences; + for (int i = 0; i < view.n_cells; i++, c_curr++, cs_curr += view.n_max_seq) { for (int j = 0; j < view.n_max_seq; j++) { if (cs_curr[j].seq_id < 0) { continue; } @@ -1430,6 +1435,7 @@ void dump_kv_cache_view_seqs(const llama_kv_cache_view & view, int row_size) { } if (seqs.size() + 1 >= sizeof(slot_chars)) { break; } } + printf("=== Sequence legend: "); for (const auto & it : seqs) { printf("%zu=%d, ", it.second, it.first); @@ -1452,5 +1458,6 @@ void dump_kv_cache_view_seqs(const llama_kv_cache_view & view, int row_size) { } putchar(' '); } + printf("\n=== Done dumping\n"); }