Skip to content

kv-cache : refactor the update/defrag mechanism - #13988

Merged
ggerganov merged 4 commits into
masterfrom
gg/kv-cache-refactor-update
Jun 4, 2025
Merged

ggerganov merged 4 commits into
masterfrom
gg/kv-cache-refactor-update

Conversation

@ggerganov

@ggerganov ggerganov commented Jun 3, 2025 •

Copy link
Copy Markdown
Member

cont #13746 (comment)

Overview

  • Remove virtual llama_kv_cache::update()
  • Remove virtual llama_kv_cache::defrag_sched()
  • Add virtual llama_kv_cache::init_update()
  • llama_kv_cache_unified::defrag_prepare() is now const

The logic for shifting and defragmenting the KV cache is now implemented using a memory state (i.e. llama_memory_state) for consistency with the decoding states that were introduced in #13746. The idea is that calling init_update() will check if any updates have to be performed without mutating the KV cache (a.k.a. the memory). We can then apply the created memory update state to perform the necessary updates:

const auto kv_state = kv_self->init_update(this, optimize);
if (kv_state->get_status() == LLAMA_MEMORY_STATUS_NO_UPDATE) {
// no updates need to be performed
return false;
}
if (!kv_state->apply()) {
LLAMA_LOG_ERROR("%s: failed to apply memory update\n", __func__);
}
}

This change generalizes the concept of updating the memory module. So far we have been doing KV cache shifts and defrags, but in the future we can do additional operations through this mechanism.

We also start to avoid the explicit "defrag" term as it is too specific for the unified KV cache. Instead, the init_update() method takes a bool optimize flag that can mean different things depending on the underlying memory implementation.

Next PRs

@ggerganov
ggerganov requested a review from slaren June 4, 2025 07:33
@ggerganov
ggerganov force-pushed the gg/kv-cache-refactor-update branch from ddc998b to 503dda2 Compare June 4, 2025 07:34
@ggerganov
ggerganov merged commit 3e63a58 into master Jun 4, 2025
@ggerganov
ggerganov deleted the gg/kv-cache-refactor-update branch June 4, 2025 15:58
furyhawk pushed a commit to furyhawk/llama.cpp that referenced this pull request Jun 6, 2025
* kv-cache : refactor update mechanism

ggml-ci

* memory : improve status handling

* defrag : reset head + add comments

ggml-ci

* cont : minor fixes

ggml-ci
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* kv-cache : refactor update mechanism

ggml-ci

* memory : improve status handling

* defrag : reset head + add comments

ggml-ci

* cont : minor fixes

ggml-ci
my-other-github-account pushed a commit to my-other-github-account/llama.cpp that referenced this pull request May 15, 2026
* kv-cache : refactor update mechanism

ggml-ci

* memory : improve status handling

* defrag : reset head + add comments

ggml-ci

* cont : minor fixes

ggml-ci
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* kv-cache : refactor update mechanism

ggml-ci

* memory : improve status handling

* defrag : reset head + add comments

ggml-ci

* cont : minor fixes

ggml-ci
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* kv-cache : refactor update mechanism

ggml-ci

* memory : improve status handling

* defrag : reset head + add comments

ggml-ci

* cont : minor fixes

ggml-ci
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
* kv-cache : refactor update mechanism

ggml-ci

* memory : improve status handling

* defrag : reset head + add comments

ggml-ci

* cont : minor fixes

ggml-ci
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
* kv-cache : refactor update mechanism

ggml-ci

* memory : improve status handling

* defrag : reset head + add comments

ggml-ci

* cont : minor fixes

ggml-ci
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
* kv-cache : refactor update mechanism

ggml-ci

* memory : improve status handling

* defrag : reset head + add comments

ggml-ci

* cont : minor fixes

ggml-ci
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