Skip to content

server: add --cache-disk for mmap-backed prompt cache state (UMA offloading) - #26408

Closed
pridesong wants to merge 1 commit into
ggml-org:masterfrom
pridesong:feat/cache-disk-mmap
Closed

pridesong wants to merge 1 commit into
ggml-org:masterfrom
pridesong:feat/cache-disk-mmap

Conversation

@pridesong

@pridesong pridesong commented Aug 1, 2026 •

Copy link
Copy Markdown

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) through llama_state_seq_get_data_ext, skipping the std::vector<uint8_t> heap copy. Restore reads the mapping and unlinks the file; files are also unlinked on LRU eviction.

The existing --cache-ram pool 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:

  • RAM mode: +10.1 MiB
  • disk mode: +1.8 MiB (two checkpoint files, 2.34 + 2.03 MiB)

At 220k context with 64 checkpoints x ~150 MiB each, the RAM pool would hold ~9.4 GiB - most of the default --cache-ram budget. Disk mode keeps that in page cache instead of the process heap.

Implementation notes:

  • alloc() creates and mmaps one file per state (POSIX mmap / Windows CreateFileMapping), stores the pointer
  • load() restores from the mapped region, then unmaps and deletes the file
  • prompt_save() writes into the mapped region in disk mode
  • No fsync on purpose; a checkpoint interrupted by a crash may be stale and is discarded on load

Limitations:

  • One file per checkpoint state. Log-structured compaction (checkpoint as cursor) is a follow-up.
  • Windows mapping handles are not stored per state; cleanup relies on process exit. Known issue, planned fix.
  • Disk mode currently requires --cache-ram N with N != 0: the prompt cache subsystem (and its checkpoint bookkeeping) is only created when cache_ram_mib != 0. --cache-ram 0 disables the whole subsystem. Making --cache-disk implicitly enable it is a possible follow-up.
  • Restore latency was not benchmarked on target hardware (UMA/Vulkan); the NVMe floor for ~150 MiB is roughly 20-30 ms.

Tested:

  • --cache-disk accepted by the arg parser (via --help)
  • stories15M and Qwen3-14B: checkpoint files created on disk, cache accounting correct (2 prompts, sizes match state)
  • A/B private memory comparison above

Related: #24028. Closes #20697.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes - used for design review and test scripting. All changes were reviewed and verified by me before submission.

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).
@ggml-gh-bot

ggml-gh-bot Bot commented Aug 1, 2026

Copy link
Copy Markdown

Hi @pridesong, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

  • AI-generated content: While code is allowed to be generated by AI, please write the PR description and commit messages on your own without the help of AI.


Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@builtproper

Copy link
Copy Markdown

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.

@pridesong

Copy link
Copy Markdown
Author

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).

@yitizi

yitizi commented Aug 31, 2026 •

Copy link
Copy Markdown

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.

@colinrgodsey

colinrgodsey commented Aug 31, 2026 •

Copy link
Copy Markdown

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:

456928 root  99.9% llama-server
460369 root  96.4% kworker/u129:2+flush-259:0

──────

🚨 What is happening:

  1. The Livelock:
    • When saving the 22,000-token checkpoint (~1.5–2 GiB), the --cache-disk patch creates a sparse file and maps it using MAP_SHARED.
    • It then runs llama_state_seq_get_data_ext, which tells the AMD ROCm GPU driver (hipMemcpyDtoH) to DMA directly from GPU VRAM into that uncommitted
    MAP_SHARED file-backed memory region.
  2. The Contention with flush-259:0:
    • The Linux kernel's block layer (kworker +flush-259:0) immediately detects unwritten dirty pages and locks the filesystem extents to flush them to disk.
    • At the same time, the ROCm driver tries to pin and DMA into those same locked pages.

🛠️ The Solution:

  • Both threads enter a 100% CPU lock contention loop (accumulating 21+ minutes of CPU time) where GPU DMA and the filesystem flush worker block each other
  indefinitely.

──────
Writing GPU state directly into a file-backed MAP_SHARED mmap is unsafe for GPU DMA on Linux.
Instead, the safe and fast architecture is:

  1. DMA to Host RAM: Copy the checkpoint state from GPU VRAM into a standard host buffer (takes ~15 ms via PCIe/UMA).
  2. Sequential File Write: Stream the buffer to the .bin checkpoint file on NVMe using standard fwrite()/write() in a single contiguous write (takes ~300 ms on
    an NVMe).
  3. Restore: When loading a checkpoint from disk, stream it back into GPU memory.
    ──────
    Would you like me to update tools/server/server-task.cpp and tools/server/server-context.cpp to use clean sequential disk streaming so --cache-disk saves in
    0.3s without any kworker livelocks?

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.

Feature Request: Disk-based context checkpoint offloading (--cache-disk)

4 participants