Skip to content

server : host-memory prompt caching - #16391

Merged
ggerganov merged 17 commits into
masterfrom
gg/prompt-cache-ext
Oct 9, 2025
Merged

server : host-memory prompt caching#16391
ggerganov merged 17 commits into
masterfrom
gg/prompt-cache-ext

Conversation

@ggerganov

@ggerganov ggerganov commented Oct 2, 2025

Copy link
Copy Markdown
Member

target #16440
rel #16117

Initial version of automatic memory offloading to host memory using an extended logic for minimizing the prompt reprocessing. The host-memory prompt cache acts as "extra slots" with which we can calculate prefix similarity and decide to hot-swap them into the llama_context if it would reduce the processing. The cache is stored in regular RAM.

The RAM size that is used for caching prompts has 2 limits:

  • Max size in bytes (controlled with new --cache-ram, -cram CLI arg)
  • Max number of cached tokens (by default, equal to --context-size)

The server logs provide detailed prompt cache information each time the cache is updated:

image
  • A small QoL improvement is that update_slots() now also logs the old and new prompt for each task around n_past (up to 10 tokens) so we can have a better understanding what caused the particular choice of the n_past value for the new task.

  • Setting LLAMA_SERVER_SLOTS_DEBUG=1 env will make the /slots endpoint output a more detailed output containing the prompt and the generated text of the current or last task. This is useful for debugging purposes.

Note: mtmd workarounds are starting to cause some headaches. For example server_tokens is not copyable which complicates the cache logic and makes the prompt caching feature incompatible with mtmd.

Usage

# use 8192 MiB of host RAM for caching prompts
llama-server ... --cache-ram 8192

# use as much host RAM is available (i.e. no limit)
llama-server ... -cram -1

# disable prompt caching in RAM
llama-server ... -cram 0

Server refactor

  • Replace server_slot members with a single server_task
  • Remove server_slot.n_predict
  • Remove prompt truncation logic (obsolete and not useful anymore)
  • slot.task is now const ptr to reflect that the task parameters should not change when it is passed to the slot
  • Bump default context checkpoints from 3 to 8

TODOs

  • Set memory limit for the host-memory cache from CLI
  • Clean-up implementation
  • Test with agentic workflows
  • Multi-slot tests
  • Fix progress report
@ggerganov
ggerganov force-pushed the gg/prompt-cache-ext branch 2 times, most recently from 0787f03 to 5c0cec4 Compare October 3, 2025 18:49
@tommarques56

This comment was marked as spam.

@ggerganov
ggerganov force-pushed the gg/prompt-cache-ext branch from 5c0cec4 to 1440ec5 Compare October 7, 2025 07:40
@ggerganov
ggerganov changed the base branch from master to gg/server-checkpoints-improve October 7, 2025 07:41
@github-actions github-actions Bot added the python python script changes label Oct 7, 2025
@ggerganov ggerganov mentioned this pull request Oct 7, 2025
3 tasks
@ggerganov
ggerganov force-pushed the gg/prompt-cache-ext branch from 9de8392 to cf7dd4b Compare October 7, 2025 15:09
@ggerganov

Copy link
Copy Markdown
Member Author

Looking for some feedback of how this new logic performs in different use cases. I've been testing it with the llama.vscode agent and it significantly improves the experience since we can now use a single server slot without trashing the prompt cache.

The current implementation should work with any model (dense, MoE, SWA, SSM, etc.). I think the default settings should be good for most use cases, though we'll probably add some options to adjust cache limits if needed.

Pay attention to these new messages in the logs:

image

Interested in testing agentic use cases, such as Claude Code and similar, where we have a single large context with various auxilary calls (keyword extraction, summarization, etc.) interleaved. The expectation is that prompt reprocessing should be significantly reduces in such cases.

Base automatically changed from gg/server-checkpoints-improve to master October 8, 2025 07:57
@ggerganov
ggerganov force-pushed the gg/prompt-cache-ext branch from 65e8991 to 264d2c3 Compare October 8, 2025 08:24
@ggerganov
ggerganov marked this pull request as ready for review October 8, 2025 12:53
@ggerganov
ggerganov requested a review from ngxson as a code owner October 8, 2025 12:53
MrLordCat referenced this pull request in MrLordCat/llama.cpp-rdna-lab Jul 16, 2026
* minor : code style

* server : fix prompt similarity calculation

* server : initial host-memory prompt caching

* cont

* server : refactor

* cont

* cont : make the server task of the slot const

* cont : minor [no ci]

* server : cache prompts and checkpoints only for completion tasks

* server : improve prompt caching logic

* cont : fix check for number of cached prompts [no ci]

* server : improve caching logic, add -cram CLI arg

* server : print prompt mismatch info

* cont : better naming [no ci]

* server : improve prompt cache loading logic

* server : add option to debug the slot contents (#16482)

* server : add option to debug the slot contents

* Update tools/server/server.cpp

---------

Co-authored-by: Xuan-Son Nguyen <son@huggingface.co>

* server : add option to disable prompt cache

---------

Co-authored-by: Xuan-Son Nguyen <son@huggingface.co>
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Aug 2, 2026
…AtomicBot-ai#5)

4 parallele Subagents (Vulkan/AMD, CUDA/MoE, arXiv Papers, Multi-GPU/Batching).
51 Items gesammelt, dedupliziert, 5 Quick-Wins verifiziert.

Verifikation: 3/5 Quick-Wins bereits im Fork (ggml-org#15524=AtomicBot-ai#6, ggml-org#16391=--cram, ggml-org#19754= warmup).
PR ggml-org#25479 (Pascal MMVQ) bereits als AtomicBot-ai#3 ✅. PR ggml-org#22887 (4K per Iter) bereits im Fork.

9 neue ROADMAP-Items hinzugefügt:
- TheTom#89 Transfer Queue AMD RDNA3 (PR ggml-org#19976)
- TheTom#90 MUL_MAT_ID Non-Square Tile (Discussion ggml-org#22598)
- TheTom#91 Internal AllReduce Kernel (PR ggml-org#22299)
- TheTom#92 RateQuant KV Cache (arXiv:2605.06675)
- TheTom#93 InnerQ KV Cache (arXiv:2602.23200)
- TheTom#94 FineMoE Expert Offloading (arXiv:2502.05370)
- TheTom#95 Shared Expert Aux Stream (TensorRT-LLM)
- TheTom#96 Speculative Checkpointing (PR ggml-org#19493)
- TheTom#97 Backend-agnostic TP Meta Device (PR ggml-org#19378)

Vollständiger Report: docs/fork/2026-07-26_OPTIMIZATION_RESEARCH.md
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
* minor : code style

* server : fix prompt similarity calculation

* server : initial host-memory prompt caching

* cont

* server : refactor

* cont

* cont : make the server task of the slot const

* cont : minor [no ci]

* server : cache prompts and checkpoints only for completion tasks

* server : improve prompt caching logic

* cont : fix check for number of cached prompts [no ci]

* server : improve caching logic, add -cram CLI arg

* server : print prompt mismatch info

* cont : better naming [no ci]

* server : improve prompt cache loading logic

* server : add option to debug the slot contents (ggml-org#16482)

* server : add option to debug the slot contents

* Update tools/server/server.cpp

---------

Co-authored-by: Xuan-Son Nguyen <son@huggingface.co>

* server : add option to disable prompt cache

---------

Co-authored-by: Xuan-Son Nguyen <son@huggingface.co>
zcat-app added a commit to zcat-app/infochat that referenced this pull request Aug 24, 2026
llama-server's prompt-cache MiB limit is a FIXED 8192 MiB default — it
does not scale with ctx (the token limit does) — and no tracked key
covered it. Under prod's eval backlog the cache LRU-evicted the chat
turn's prefix between turns: an identical 11.7k-token prompt measured
9.9 s cold -> 0.97 s cached once prod's untracked overlay set
LLAMA_ARG_CACHE_RAM=16384
(.agents/memory-local/prod-state-post-upgrade-20260823.md). Every
default deployment silently shipped the churn-prone 8 GiB cache. The
key now sits on the tracked surface with a per-class sizing story; it
does NOT re-derive the serving ctx (M1-921) and never touches the
verified-rejected CACHE_REUSE. Analysis:
docs/plan/m1/tick-analysis/chat-context-budget-and-serving-defaults.md.

P11 --help probe on the pinned image ghcr.io/ggml-org/llama.cpp:server-
vulkan-b9776 (acceptance 2), verbatim:

  -cram, --cache-ram N                    set the maximum cache size in MiB (default: 8192, -1 - no limit, 0 -
                                          disable)[(more info)](ggml-org/llama.cpp#16391)
                                          (env: LLAMA_ARG_CACHE_RAM)

Implementation notes beyond the letter of the Approach (recorded in the
ticket's clarity_check, accepted by review):
- CPU branch clears a stale INFOCHAT_LLAMACPP_CACHE_MB on a GPU->CPU
  re-run (the M1-909 SPEC-decline-clear precedent): a leftover 16 GiB
  allocation target under the 7g cap is exactly P12's OOM path. Pinned
  by the seeded re-run drive gpuToCpuRerunClearsStaleCacheSecret added
  in review round 1's REWORK.
- GPU serving-class echo line extended with cache-ram=16384 so the
  printed class names every secret the branch writes.

Verify: reused r2 log (tree-identical); tick-test-M1-920-r2.log full
mvn verify BUILD SUCCESS, 384 provider tests / 0 failures / 0 errors,
zero port-race collisions (round-1 attempts r1/r1-rerun/r1-rerun2 red
solely on the documented rootless-docker port race, environmental).

Alternatives considered: raising the base compose default to 16384 was
rejected by the analysis (option E): a :-16384 render exceeds the
CPU-class 7g container cap; the byte-stable 8192 base plus the
GPU-class wizard write keeps every default deployment inside its cap.

Renames: none.

Reviewed-by: tick-reviewer round 1 REWORK (one low TEST-ADEQUACY item,
fixed), round 2 APPROVE (agent runs ses_fcc87e23cffe9LM9p1z1G10R7x,
ses_fcc6fbcc7ffe5v7g4WDW62PKvW)
@imnecho imnecho mentioned this pull request Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples python python script changes server