feat: add KV prefix cache for MLLM text-only requests#259
Closed
janhilgard wants to merge 1 commit intowaybarrios:mainfrom
Closed
feat: add KV prefix cache for MLLM text-only requests#259janhilgard wants to merge 1 commit intowaybarrios:mainfrom
janhilgard wants to merge 1 commit intowaybarrios:mainfrom
Conversation
Add prefix caching support to the MLLM batch generator to reuse KV states between requests sharing the same prompt prefix (system prompt, conversation history). This significantly reduces prefill latency for repeated/similar prompts on MLLM-mode servers. Key changes: - Text-only requests (no images/videos) are eligible for prefix cache - Uses existing MemoryAwarePrefixCache with exact/prefix/LCP matching - Cache is stored on request completion (before batch.filter) - Multimodal requests are explicitly excluded from caching since identical input_ids can correspond to different images - Enabled by default; configurable via MLLMSchedulerConfig - Stats exposed via /v1/status endpoint (memory_aware_cache section) Measured speedups on Apple M3 Ultra (text-only requests): - Gemma 4 26B-A4B: 1.94x (497ms → 255ms) - Gemma 4 31B: 1.52x (1055ms → 691ms) - Qwen3.5-27B: 2.67x (871ms → 326ms) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
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.
Summary
input_idscan correspond to different imagesMemoryAwarePrefixCachewith exact, prefix, LCP, and supersequence matchingHow it works
is_text_onlybased on whether images are present_process_prompts(), text-only requests check the prefix cache before running the full VLM forward pass:/v1/statusendpoint (memory_aware_cachesection)Configuration
Enabled by default. New
MLLMSchedulerConfigfields:enable_prefix_cache: bool = True— toggle prefix cacheprefix_cache_memory_mb: Optional[int] = None— memory limit (auto-detect if None)Measured speedups (Apple M3 Ultra, text-only requests)
Test plan
/v1/statusshows correctmemory_aware_cachestats🤖 Generated with Claude Code