server: unify text and vision KV prefix reuse - #977
Open
HariboApfel wants to merge 3 commits into
Open
Conversation
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.
Goal
Make KV-cache reuse modality-agnostic: restore the longest prefix that produced identical model inputs—text or images—and compute only the new or changed suffix.
Motivation
For ds4 on Apple M-series hardware, practical agent serving lives or dies by cache reuse. OMP system and tool prompts can exceed 17,000 tokens and take tens of seconds to prefill, whereas restoring a compatible KV checkpoint takes only tens of milliseconds. Initial prefill is a much larger bottleneck here than on the NVIDIA serving hardware typically used for these models, so cache reuse needs to be a first-class execution path rather than a best-effort optimization.
The previous split between text and vision lookup paths caused that optimization to disappear exactly when a conversation gained an image. The underlying KV tensors are not inherently text or vision specific; reuse is correct whenever the retained token prefix and all conditioning inputs before the cache frontier are identical.
That gives predictable behavior:
Images are represented in cache metadata as conditioning spans, identified by token location, row count, and a SHA-256 fingerprint of the actual encoder output. This preserves correctness without forcing a full prefill at modality boundaries.
In the OMP validation run, this reused 18,073 of 18,435 tokens for the first image and 18,807 of 19,079 tokens when adding a second image.
Summary
Dependency
Stacked on #961 at commit d28b29e, which adds exact multimodal checkpoint persistence. Until #961 lands, this PR intentionally includes that prerequisite commit.
Test plan
make -j4 ds4-server ds4_testDS4_LOCK_FILE=/tmp/ds4-codex-vision-kv-test.lock ./ds4_test --serverGenerated with Codex.