server: persist LRU prompt cache to disk across restarts - #1405
Closed
javruben wants to merge 1 commit into
Closed
Conversation
…1178) Add `--prompt-cache-dir` to `mlx_lm.server`. On startup the server restores any cached prompts whose model key matches the currently-loaded model; on `atexit` and `SIGTERM` it re-saves the in-memory LRU. Layout under the directory is one safetensors file per cached prompt (written via the existing `save_prompt_cache` helper) plus a `manifest.json` carrying the LRU order, model keys, tokens, and cache_type. Entries whose model is not currently loaded are skipped on load — the cache survives model changes safely. The on-disk format is forward-compatible via `LRUPromptCache.MANIFEST_VERSION`. Closes the resumable long-context gap requested in ml-explore#1178. Signed-off-by: Mujo <ruben.d@allelo.eco>
Member
|
Thanks for the PR but at the moment the number of PRs is way beyond our capacity to review so I'm closing the non-essential ones so we can actually work on this repo. |
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.
Problem
mlx_lm.serveralready keeps an in-memory LRU prompt cache (LRUPromptCacheinmlx_lm/models/cache.py), but the cache is lost on every server restart. For long shared prompts (system prompts, RAG contexts, multi-turn chats), this means re-prefilling thousands of tokens from scratch after every restart, even though the cached KV state is reusable.Change
Adds
--prompt-cache-dirtomlx_lm.server. When set:atexitandSIGTERM, the in-memory LRU is re-saved to disk.save_prompt_cachehelper) plus amanifest.jsoncarrying the LRU order, model keys, tokens, and cache type.LRUPromptCache.MANIFEST_VERSION.Files:
mlx_lm/server.py(+81),mlx_lm/models/cache.py(+107),mlx_lm/SERVER.md(+15),tests/test_prompt_cache.py(+73),tests/test_server.py(+59). Net +334 LOC.Default behaviour is unchanged: omit
--prompt-cache-dirand the server behaves exactly as before.Mechanism (asserted)
The on-disk layout is inspired by LuminaNAO's LSCKPT2 sidecar (
codeberg.org/LuminaNAO/llama-hdd.cpp), which persists KV cache alongside the model file with a fixed magic header that lets the engine verify the sidecar before restoring. Here we apply the same idea at the Python server layer usingsafetensorsfiles keyed by model identity, so a corrupted or model-mismatched sidecar is simply skipped rather than misapplied.Evidence
Measured on a 5-QA replay over a held prompt with the same model + flags, restart between cold and warm:
(Numbers from a side-by-side reference implementation; for
mlx_lm.serverthe per-prompt sidecar avoids the prefill on warm-start by the same mechanism.)Test
python -m pytest tests/test_prompt_cache.py tests/test_server.pyNew tests exercise round-trip save/load, model-mismatch skip, and the SIGTERM persist path.
Notes
--prompt-cache-diris passed.--prompt-cache-filewith mlx_lm.server #1178.