llama_dsv4: write only used rows in state#25325
Open
am17an wants to merge 2 commits into
Open
Conversation
ggerganov
reviewed
Jul 6, 2026
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(); |
Member
There was a problem hiding this comment.
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.
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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