Skip to content

server : allow cache reuse for text-only prompts with mmproj loaded - #4

Open
Piggidragon wants to merge 4 commits into
llama/devfrom
server/cache-reuse-mmproj-text-only
Open

Piggidragon wants to merge 4 commits into
llama/devfrom
server/cache-reuse-mmproj-text-only

Conversation

@Piggidragon

Copy link
Copy Markdown
Owner

Overview

Loading an --mmproj currently disables --cache-reuse for the whole server. The reuse gate keys on server_tokens::has_mtmd, which only means "an mmproj is loaded", not "this prompt contains media". So a text-only request against a multimodal model loses prompt cache reuse even though nothing about it needs media handling.

This changes the gate to look at real media chunks instead:

  • add server_tokens::has_media_chunks() (!map_idx_to_media.empty())
  • stop forcing params_base.n_cache_reuse = 0 when an mmproj is loaded
  • gate can_cache_reuse on !slot.prompt.tokens.has_media_chunks() && !input_tokens.has_media_chunks() so reuse only runs when both the cached prompt and the new prompt are text-only
  • relax server_tokens::set_token from GGML_ASSERT(!has_mtmd) to GGML_ASSERT(!has_mtmd || map_idx_to_media.empty()), since the reuse loop calls it and it now runs with an mmproj loaded
  • drop the now-unreachable GGML_ABORT("not supported by multimodal") and the has_mtmd assert inside the reuse block

Once an image or audio chunk is in either prompt, can_cache_reuse is false and behaviour is unchanged. Context shift and the global mmproj ctx_shift disable are left as-is.

Additional information

Addresses the cache-reuse part of ggml-org#21133 (capability flag has_mtmd conflated with "media present in this conversation"). Slot save/restore and checkpoints for text-only + mmproj are out of scope here.

Requirements

🤖 Generated with Claude Code

has_mtmd only means an mmproj is loaded, not that the current prompt
carries media. Loading an mmproj disabled cache reuse for the whole
server, so text-only requests lost prompt cache reuse too.

Gate cache reuse on real media chunks in the cached prompt and the new
prompt instead. It stays disabled once an image or audio chunk is
present, and works again as soon as both prompts are text-only.

Assisted-by: Claude Sonnet
Cache reuse is no longer disabled at startup for mmproj, so the per-slot
warning now fires on every request that carries media. Warn only when the
memory cannot shift, and log the expected media case at debug level.

Also simplify the set_token assert: a non-empty media map already implies
has_mtmd.

Assisted-by: Claude Opus 5
Review follow-ups on the cache reuse block:

- the shift is applied to the draft context too, so require both contexts
  to support it
- skip reuse while an alora is invoked, the loop moved n_past past the cap
- drop the context checkpoints after a shift, they no longer match the cache
- set_token asserts on the media placeholder instead of the media map, and
  takes a token index, which is what the only caller passes

Assisted-by: Claude Opus 5
One server instead of two. The shared chunk now starts on a newline, so it
tokenizes the same with and without the leading sentence.

Assisted-by: Claude Opus 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant