Repository navigation
Sharing prompt prefixes across sessions in llama-server #30664
rochaudhari
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Problem
Agent clients start every session with the same long prefix: the tool schemas and the system prompt. For Hermes, this prefix is 14.1K tokens. llama-server can only reuse the cache of the slot a request lands in. This causes three problems.
--cache-ramrestores a copy of the prefix, but loading the entry removes it from the cache, so the next session cannot use it.Proposal
I propose three changes to llama-server. All three are opt-in. The core llama.cpp library does not change.
--slot-attach). A new session takes the shared prefix from another slot withseq_cp, which shares the KV cells instead of copying them. Hybrid models attach at one of the other slot's checkpoints.--cache-prefix). The server saves the state at the first user message as a separate entry in the RAM prompt cache. The entry is copied when loaded, so it stays for later sessions. The 25% rule does not apply to it, and when the cache is full, the least recently used entry is removed first.Attach needs a unified KV cache and turns off with context shift. All three features turn themselves off on sliding-window models and DeepSeek-V4, and for requests that use speculative decoding, LoRA or images. The server refuses
--cache-reusewhile attach is on.amosWeiskopf's comment in #21961 discusses the same core idea as attach: running slots share a prefix's KV cells through
seq_cp. That work was tested on attention-only models. My proposal adds four things on top of it:Results
Setup:
NVIDIA A40 (46 GB), CUDA 12.8, Hermes's tools and system prompt (~14K-token prefix), q8_0 KV, 4 slots, 3 sessions, MTP off.
With one shared prefix and no history, I grew Hermes's prompt with more project context and measured the 3rd session's time to first token and the decode speed-up:
Without sharing, the wait grows faster than the prefix does. With attach, the wait stays about the same as the prefix grows.
Questions
seq_cpsharing between running slots, plus divergence checkpoints for hybrid models. It could use a single flag instead of three.--cache-reusefor now? Attach also turns off with context shift, because both move shared cells. The alternative is to add copy-on-write for shared cells to the library first.The prototype works on current master. I am happy to open a PR in whatever scope you prefer, including a smaller one.
All reactions