Misc. bug: server: slot erase does not clear the --cache-ram prompt cache, so an earlier prompt is restored after erase
Name and Version
version: 0.9.0-dev (build 12110, commit cd959bae9) built with AppleClang 21.0.0.21000101 for Darwin arm64
Operating systems
Mac
Which llama.cpp modules do you know to be affected?
llama-server
Command line
llama-server -m qwen2.5-3b-instruct-q4_k_m.gguf --host 127.0.0.1 --port 8099 -np 1 -c 4096 -ngl 99 --seed 42 --slots --slot-save-path ./slots
Problem description & steps to reproduce
I expected POST /slots/0?action=erase to leave the server cold for the next request. That is what happens for P, erase, P. But if a different prompt used the slot in between, the next request gets the earlier prompt back from the --cache-ram prompt cache, even though the erase returned success.
Steps, fresh server each time, -np 1, default --cache-ram. Every request goes to /completion with cache_prompt: true, n_predict: 8, temperature: 0, seed: 42:
- Send prompt P (533 tokens).
cache_n0. - Send a different prompt Q (667 tokens).
cache_n0. curl -X POST "http://127.0.0.1:8099/slots/0?action=erase" -d ''returns{"id_slot":0,"n_erased":674}.- Send P again.
cache_n532,prompt_n1.
--cache-ram |
sequence | cache_n on the last request |
|---|---|---|
| default | P, P | 532 |
| default | P, erase, P | 0 |
| default | P, Q, erase, P | 532 |
| 0 | P, Q, erase, P | 0 |
/v1/completions shows the same: usage.prompt_tokens_details.cached_tokens is 532 in the P, Q, erase, P case. So the telemetry is right, it reports the restored tokens.
Reading the code, the erase task only calls prompt_clear() on the slot, and nothing touches server_prompt_cache. When Q took the slot, P was saved to the RAM cache, and after the erase the empty slot lets any cache entry win, so P is loaded back. I first saw this on b10434 (7e4c0a9), and it is the same on b12110. I have not tested -np above 1 or other backends.
The README says erase will "Erase the prompt cache of the specified slot", so I assumed it gave a cold start. I was using erase to reset between repeated runs while measuring how often greedy outputs repeat, and the restored state made runs depend on what the server saw before.
Two questions:
- Could the erase section of the README say that erase clears only the slot, that the
--cache-ramcache is kept, and that--cache-ram 0or a restart gives a cold server? It could also mention that slot actions need--slot-save-path. - Would a way to clear the RAM prompt cache be welcome, for example an option on erase or a separate endpoint? I can test a patch on my Mac.
Background, if useful: https://doi.org/10.1109/ACCESS.2026.3749922
First Bad Commit
Not a regression as far as I can tell. The --cache-ram prompt cache was added in #16391, and erase has not touched it since.
Relevant log output
Logs
Reproduced on master (3ba1ce0) using stories260K on linux. It also occurs with two slots (-np 2), 109 restored there too
Findings -
- P, Q, erase, P restores 109 of P’s 110 tokens;
- the same sequence with --cache-ram 0 restores 0.
Probable cause is that the erase handler only clears a single slot but doesn't touch the prompt cache. The RAM cache entries don't record which slot they came from, so the cache ends up being one pool shared by all slots
@ggerganov I'd like to try and take this, but before that I'd like to know what would be preferred
- Erase also empties the whole cache (would also end up clearing other slots' cached prompts)
- A new opt in parameter on erase
- A separate endpoint that would serve to clear the cache
Also plan on adding a regression test
Thanks for reproducing this on master and checking -np 2. I'd only tested with one slot.
I ran into this while using slot erase to reset server state between repeated runs in follow-up measurements for the cache-divergence study linked above. For that single-slot setup, either an opt-in erase parameter or a separate endpoint would work, as long as I can clear both the slot and the shared RAM prompt cache without restarting the server.
I'd keep plain erase scoped to the selected slot. Clearing the shared cache by default would also evict prompts that other slots could reuse.
I'm happy to test the PR on macOS with Metal. I can rerun P, Q, erase, P and check that cache_n is 0 after an explicit reset, while plain erase keeps its current behavior.
I reproduced this on Windows x64 using the official b12425 CPU build and stories260K.gguf. I started the server with -ngl 0 -np 1 --slots --slot-save-path .\slots -c 1024. The log showed that the actual slot context was 128 tokens.
I used cache_prompt: true, n_predict: 8, temperature: 0, and seed: 42 for each POST /completion request. To clear the slot, I sent POST /slots/0?action=erase.
I sent P, then Q, cleared the slot, and sent P again. Then I restarted the server with --cache-ram 0 and repeated the same steps. The erase request returned n_erased = 120 both times. When I sent P again, the server reused 73 prompt tokens with the default settings and zero with --cache-ram 0.
I think it makes sense to add a separate endpoint for clearing the shared RAM prompt cache. As far as I know, there isn’t an endpoint for that yet.
Hi @ps-abhi, have you started working on this yet? I’m interested in this issue too. If you’ve already started, could you share a quick update on how it’s going? Thanks!
I’ve implemented this locally and verified on Windows x64 that the new endpoint clears the shared RAM prompt cache without restarting the server.