Repository navigation
feat: opt-in VLM prompt-prefix cache sharing for multi-turn images - #184
Merged
Merged
Conversation
Completes #124 step c. Adds a default-off --enable-vlm-prefix-cache flag (env MLXCEL_ENABLE_VLM_PREFIX_CACHE) that lets multimodal chat requests adopt and donate KV prefixes for multi-turn same-image conversations. When enabled and the request carries image/audio (no video), the scheduler now expands the image placeholder tokens before probing the cache so the key and matched-prefix length are computed over the post-injection token stream, and restricts multimodal adoption to a whole-entry match so the prefilled suffix is guaranteed to be the newly-appended text turn: every image/audio token sits inside the matched prefix, and a different media payload lands in a different multimodal-digest bucket. On a hit the full-prompt embeddings are dropped and the text suffix runs through the token path while the bound MRoPE state covers the suffix positions. Video payloads are excluded because their frame bytes are not folded into the multimodal digest. The flag is plumbed from both ServeArgs paths through ServerStartupInput, ServerConfig, the model provider, and WorkerSchedulerConfig to BatchScheduler::with_vlm_prefix_cache. With the flag off (the default) every code path reduces to the previous behavior, so text-only and non-VLM requests are byte-identical and untouched. Real-model verification on Qwen2-VL-2B-Instruct-4bit (the MRoPE case): a multi-turn same-image conversation adopts the image prefix (108/124 tokens reused) and produces output byte-identical to a cold full prefill, on both short and 400+ character generations. New unit test pins the sharing gate's safety conditions (off by default, text-only rejected, video excluded). The vlm_prefix_sharing_gate_pins_safety_conditions and compose_prompt_cache_key_folds_request_multimodal_digest tests plus the existing 3049-test suite stay green.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…ablup#184) Completes lablup#124 step c. Adds default-off --enable-vlm-prefix-cache so multimodal chat requests can adopt and donate KV prefixes for multi-turn same-image conversations (whole-entry match, text suffix via the token path with MRoPE bound). Flag-off is byte-identical to the prior behavior. Verified on Qwen2-VL-2B: adopt output byte-identical to a cold prefill.
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
Completes #124 (step c). With the foundation merged (#182: SSM carve-out +
mm_digestplumbing), this wires the actual VLM (image/audio) prompt-prefix cache sharing behind a default-off flag.--enable-vlm-prefix-cache(envMLXCEL_ENABLE_VLM_PREFIX_CACHE) lets multimodal chat requests adopt and donate KV prefixes for multi-turn same-image conversations. When enabled and the request carries image/audio with no video, the scheduler:require_whole_entry), which guarantees the prefilled suffix is the newly-appended text turn: every image/audio token sits inside the matched prefix, and a different media payload lands in a different digest bucket;Video payloads are excluded because their frame bytes are not folded into the multimodal digest. The flag is plumbed from both
ServeArgspaths throughServerStartupInput,ServerConfig, the model provider, andWorkerSchedulerConfigtoBatchScheduler::with_vlm_prefix_cache.Safety: flag-off is byte-identical
With the flag off (the default),
vlm_prefix_sharing_allowedreturnsfalsefor every request, soprepared_earlystaysNone,ctx_ref/require_whole_entry/the donate-context gate all reduce to the pre-#124 behavior, and theprepare_request_vlm_embeddingsordering is unchanged. Text-only and non-VLM requests are untouched either way.Verification
Real-model parity (Qwen2-VL-2B-Instruct-4bit, the MRoPE case): a multi-turn same-image conversation adopts the image prefix (
prompt-cache hit: adopted 108/124 tokens) and produces output byte-identical to a cold full prefill, on both a short answer and a 400+ character generation. Text 2-turn adopt and the cold path were also confirmed working on the same server.Automated:
vlm_prefix_sharing_gate_pins_safety_conditionspins the gate (off by default, text-only rejected, video excluded, only opted-in image/audio eligible).compose_prompt_cache_key_folds_request_multimodal_digest(feat: hybrid-SSM cache carve-out tests and multimodal digest plumbing #182) covers the key.cargo fmt --all -- --check, coldcargo clippy --all-targets --features metal,accelerate -- -D warnings, and the fullmlxcellib suite (3049 passed, 0 failed) are green.Scope note
This first cut covers multi-turn same-image continuation via the whole-entry match. Cross-conversation system+image prefix sharing (partial match past the image tokens) needs a per-family placeholder-token boundary and is a clean follow-up; both stay behind this same flag.
Closes #124