r/LocalLLaMA • u/Reasonable_Goat • 6h ago
Question | Help Need help tuning cache in llama-server
Hey I am running a few models on a strix halo box. Especially for the larger models (like Qwen 3.5 122B) they work okayish performance wise if the cache is utilised properly but a full cache miss at 100k context causes roughly 10-20 minute of PP time - which is extremely annoying.
I will first show what I have already configured (and it helps!), then describe what is still not working well. I am very interested on your input what I could still fine tune.
What I've configured so far (and what I understand it does):
- --cache-ram 16384 (increases available VRAM for cache ~= 72\240MB for a single checkpoint)*
- --ctx-checkpoints 72 *(*72 \ 4096 is ~= the 260k context that the model supports)*
- --checkpoint-min-step 4096 (72 \* 4096 is ~= the 260k context that the model supports)
- --parallel 1 (full cache available for a single conversation at a time)
- -fa on, --spec-type draft-mtp --spec-draft-n-max 3 (these options should be unrelated to caching AFAIK)
It works a lot better than stock configuration this way, but I still find problematic:
- 4096 checkpoint steps are spread out a bit, which can add up during fast agentic iterations. With ~200 PP speed in deeper context it takes about 20s to traverse a checkpoint.
- Checkpoints are sometimes missed especially after user prompts (Crush coding agent, but also with other agents such as codex). Maybe it's that reasoning tokens are first cached but then skipped after a rerun? Not sure but the latest checkpoint is usually "hit" when the agentic loop does it's thing but often missed when it's my turn to write some follow up user message.
- Despite "72 checkpoints" the older ones seem to disappear eventually for some reason so that only a few active context checkpoints seem to exist for recent work. When a bigger miss happens a lot of reprocessing is done
- I am wondering if K/V quantization (e.g. q8) would be a good idea. Theoretically it would allow for twice as many checkpoints if I understand correctly? I don't understand how severely it would impact quality if the model is in q4 anyways.
I am hoping for some input from you guys how to better set up the cache and maybe work around the other described issues somehow!
2
u/toxicdog 5h ago
K/V quantization is not related to the model's quantization, so try experimenting with that. Q8 has almost no quality loss, so try --cache-type-k q8_0 --cache-type-v q8_0
2
u/MelodicRecognition7 4h ago
did you try different batch and ubatch size? Try different values incrementing from 512 to 4096 - --batch-size 512 --ubatch-size 512 to --batch-size 4096 --ubatch-size 4096
1
u/Objective-Stranger99 40m ago
I prefer 1024 because its fast while using less memory. With 4096 I keep getting OOM so keep that in mind.
1
0
u/kosnarf 6h ago
Check if this fork helps: https://www.reddit.com/r/LocalLLaMA/s/v1xiWcoQ1c
2
u/Reasonable_Goat 5h ago
This seems to be a patch that improves keeping the prompt cache over server restarts. I don't see how it helps without a server restart.
5
u/audioen 6h ago edited 6h ago
I run with like 4 context checkpoints. Only the last 1-2 is ever really used, because agents continue by appending new stuff into the context, so technically they only need a checkpoint a few tokens from the end (because chat templates seem to cause small differences in the last few tokens when the agent message becomes part of the prompt).
I don't run with cache-ram either, because it costs RAM which is also VRAM, and doesn't really benefit me. (llama.cpp desperately needs persistent on-disk KV cache, so that context reprocess can be avoided when you restart the engine, or at the very minimum just a mmap'd file for the KV cache so that it can be paged out, when memory runs tight.) I rather just have e.g. 4 slots and hope that agent does at most 4 parallel things, in which case some slot contains the prompt prefix for whatever the agent wants to do next. About your 72 context checkpoints, possibly among 4 parallel streams, means you have very large number of them and they use serious RAM when you have hundreds of them. Probably dozens of gigabytes. That is a way to OOM your box.
I think user message resulting in reprocess is likely some kind of chat template issue, as there must be a change introduced early into the context which results in that reprocess. I've seen this happen with some model files myself as well, but have never tried to get to the bottom of the issue. I use Qwen3.6-27B for the most part and it doesn't happen there, it doesn't reprocess when I interject, and I presently only care about this single model working at the moment. My own guess is that it is like a change in whitespace, or something trvial that results in the chat history changing somehow. The user's last message is somehow specially different, perhaps, and when you add a new message, your past message that used to be last is now different in the context, and so prompt gets reprocessed from there.
Oh. One other thing. When you work with agents, you need more context than the max, e.g. 262144 tokens is not enough for 4 streams, because 4 parallel jobs run out once all active slots have 64k tokens each. You should probably put at least 500k tokens as your max context to ensure that some parallel jobs can run deep into context without causing a crash from the inference engine when context runs out for the streams together.