Repository navigation
Conversation
Store prompt cache KV state in mmap'd files instead of RAM when --cache-disk <dir> is set. On UMA systems (Strix Halo, Apple Silicon) RAM and VRAM share the same pool, so the --cache-ram checkpoint pool directly competes with model weights and KV cache for memory. With --cache-disk, checkpoint state lives in the page cache backed by disk files, freeing the process's private RAM. Measured on Qwen3-14B (Q4_K_M, CPU): saving 3 checkpoints grows private memory by 10.1 MiB (RAM mode) vs 1.8 MiB (disk mode), an 82% reduction. Implementation: server_prompt_cache gains a cache_dir; alloc() creates a per-state mmap file (main+drft concatenated) and the KV state is written directly into the mapped region via llama_state_seq_get_data_ext, skipping the heap vector entirely. load() restores from the mmap region. POSIX and Windows (CreateFileMapping) paths included. Implements part of ggml-org#20697 (--cache-disk feature request).
|
Hi @pridesong, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
Please merge it, as this would be the only way to practically run e.g. Deepseek v4 flash on Strix Halo for agentic use without waiting an eternity for prompt processing almost every time, and please also with multimodality for Qwen3.5 122b for example. |
Thanks for the feedback — good to know the direction is useful for Strix Halo agentic workloads, that’s exactly the scenario it was built for. One status note: I had to close this PR temporarily to comply with the new-contributor limit (one open PR at a time) while another PR of mine is in review. This one will be reopened as soon as that resolves — nothing about the implementation has changed, the code and measurements above still stand. On multimodality for Qwen3.5-122B: that’s a model-architecture concern separate from the cache backend, but worth tracking in its own issue — the cache-disk path itself is backend-agnostic, so once it lands it applies to any model the server can run (recurrent or attention-based). |
|
Hi — I built a local implementation of --cache-disk on top of this PR, and I'd like to check with you before doing anything with it. The main thing it adds is loading the cache back after a server restart. Your Limitations section already notes that load() unlinks the file and that files are dropped on LRU eviction, so the state doesn't survive a restart. My implementation keeps a .meta sidecar next to each .bin and rebuilds the index at startup by scanning the cache directory, validating model / build / context size / KV cache type / RoPE / LoRA before accepting an entry; incomplete or mismatched files are removed. It also adds --cache-disk-max to bound the directory with LRU eviction, and it works without --cache-ram N != 0. I think this is complementary rather than a duplicate: your PR targets memory pressure on UMA systems, mine targets the cold prefill after a restart. On an agent workload where every new session shares a ~14,200-token system prompt, the first request after a restart went from 22.5 s to 0.45 s (14,199 tokens recomputed → 4). I also added unit tests following the repo's conventions — four cases in tools/server/tests/unit/, using ServerPreset.tinygemma3() so the SWA context-checkpoint path is actually exercised. They assert on timings.cache_n / timings.prompt_n rather than parsing logs. All four fail on b10644 and pass with the change. Would you like me to fold these into your PR, or should I open a separate one? AI usage disclosure: the implementation was generated with Codex and the tests were written with Claude; I reviewed and verified all of it, and this comment is my own. |
|
I've been playing around with this pr for a bit. I did have a deadlock at one point when loading cache from disk, and my local agent got a fix in for it. I haven't otherwise had other problems: The top screenshot reveals a severe Linux kernel livelock caused by the experimental --cache-disk mmap implementation: ────── 🚨 What is happening:
🛠️ The Solution:──────
|
Overview
Add
--cache-disk <dir>to store prompt cache (context checkpoint) KV state in mmap'd files instead of heap RAM. In disk mode the checkpoint state is written directly into a per-state file (ckpt_N.bin) throughllama_state_seq_get_data_ext, skipping thestd::vector<uint8_t>heap copy. Restore reads the mapping and unlinks the file; files are also unlinked on LRU eviction.The existing
--cache-rampool holds checkpoint state in heap RAM. On UMA systems (AMD Strix Halo, Apple Silicon) system RAM and VRAM share one pool, so that state competes with model weights and the KV cache for the same memory, as described in #20697.Additional information
Measured on Qwen3-14B Q4_K_M (CPU), private memory delta after saving checkpoints:
At 220k context with 64 checkpoints x ~150 MiB each, the RAM pool would hold ~9.4 GiB - most of the default
--cache-rambudget. Disk mode keeps that in page cache instead of the process heap.Implementation notes:
alloc()creates and mmaps one file per state (POSIXmmap/ WindowsCreateFileMapping), stores the pointerload()restores from the mapped region, then unmaps and deletes the fileprompt_save()writes into the mapped region in disk modeLimitations:
--cache-ram Nwith N != 0: the prompt cache subsystem (and its checkpoint bookkeeping) is only created whencache_ram_mib != 0.--cache-ram 0disables the whole subsystem. Making--cache-diskimplicitly enable it is a possible follow-up.Tested:
--cache-diskaccepted by the arg parser (via--help)Related: #24028. Closes #20697.
Requirements