Skip to content

feat: opt-in VLM prompt-prefix cache sharing for multi-turn images - #184

Merged
inureyes merged 1 commit into
mainfrom
feature/issue-124-vlm-prefix-cache
Jun 8, 2026
Merged

inureyes merged 1 commit into
mainfrom
feature/issue-124-vlm-prefix-cache

Conversation

@inureyes

@inureyes inureyes commented Jun 8, 2026

Copy link
Copy Markdown
Member

Summary

Completes #124 (step c). With the foundation merged (#182: SSM carve-out + mm_digest plumbing), this wires the actual VLM (image/audio) prompt-prefix cache sharing behind a default-off flag.

--enable-vlm-prefix-cache (env MLXCEL_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:

  1. expands the image/audio placeholder tokens before probing the cache, so the key (via the request's multimodal digest) and the matched-prefix length are computed over the post-injection token stream the KV is built over;
  2. restricts multimodal adoption to a whole-entry match (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;
  3. on a hit, drops the full-prompt embeddings and runs the text suffix through the token path, with the bound MRoPE / per-layer state covering 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.

Safety: flag-off is byte-identical

With the flag off (the default), vlm_prefix_sharing_allowed returns false for every request, so prepared_early stays None, ctx_ref/require_whole_entry/the donate-context gate all reduce to the pre-#124 behavior, and the prepare_request_vlm_embeddings ordering 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_conditions pins 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, cold cargo clippy --all-targets --features metal,accelerate -- -D warnings, and the full mlxcel lib 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

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.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:models Model architectures, weights, loading, metadata labels Jun 8, 2026
@inureyes
inureyes merged commit dcae0ac into main Jun 8, 2026
4 checks passed
@inureyes
inureyes deleted the feature/issue-124-vlm-prefix-cache branch June 8, 2026 06:46
@inureyes inureyes self-assigned this Jun 10, 2026
@inureyes inureyes added this to the 0.2 milestone Jun 21, 2026
@inureyes inureyes added the status:done Completed label Sep 6, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:models Model architectures, weights, loading, metadata status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hybrid SSM exclusion and multimodal adopt policy under the unified cache

1 participant