Skip to content

llama_dsv4: write only used rows in state#25325

Open
am17an wants to merge 2 commits into
ggml-org:masterfrom
am17an:dsv4-cache
Open

llama_dsv4: write only used rows in state#25325
am17an wants to merge 2 commits into
ggml-org:masterfrom
am17an:dsv4-cache

Conversation

@am17an

@am17an am17an commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Overview

Currently the dsv4 cache stores the entire kv-cache as state, this is not optimal. This PR only stores the used rows in the cache.

Additional information

Requirements

@am17an am17an requested a review from ggerganov as a code owner July 5, 2026 16:26
Comment on lines +1297 to +1304
const llama_pos pos_max = seq_id >= 0 ? kv_raw->seq_pos_max(seq_id) : -1;

const uint32_t n_rows_csa = seq_id >= 0 ?
dsv4_state_n_used_k_rows(pos_max, DSV4_CSA_RATIO, kv_csa->get_size()) : kv_csa->get_size();
const uint32_t n_rows_hca = seq_id >= 0 ?
dsv4_state_n_used_k_rows(pos_max, DSV4_HCA_RATIO, kv_hca->get_size()) : kv_hca->get_size();
const uint32_t n_rows_lid = seq_id >= 0 ?
dsv4_state_n_used_k_rows(pos_max, DSV4_CSA_RATIO, kv_lid->get_size()) : kv_lid->get_size();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, the token positions and the KV cells/rows should not be conflated. That's why for the llama_kv_cache we utilize the information from the llama_kv_cells to determine which cells should be stored for the state.

At the very least, need to add TODOs that this logic will not work for multi-modal cases where the positions will no longer map correctly to the cache cells.

@am17an am17an requested a review from CISC as a code owner July 6, 2026 13:23
@ggerganov

Copy link
Copy Markdown
Member

@am17an I guess it would be better to first confirm and resolve the checkpoint problem reported here #25402 and after that merge this change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants