Tutorial: KV cache reuse with llama-server #13606
Replies: 6 comments 16 replies
|
This is great! I have one concern however - the Warning The |
If you provide repro steps about this problem, we can take a look and fix it. |
|
can we cache context in http client?the tutorial for cache prompt |
|
My issue with this feature is that if you have 10 slots and 11 cyclic different prompts, it will never reuse cache if I understood how it works correctly. |
|
I am sitting here since five hours, absolutely boggling at how broken and brain damaged this api is. It's the most stunningly bad thing i've seen since i don't know when. The room has literally gone spinny on me. You know how people use llm's right? You know a FIFO to llama-cli or a unix pipe would be a zero-config solution to how 99% of people want to interface with a llm... right? What explains this? Did you all grow up in the OOP 1990s? Is that the cause of the spiritual and mental damage, this writhing abortion on the cold concrete of the dark hall of the urban tenement? |
|
Checking my logs, it seems that when using llama-server with prompt caching enabled, the current slot selection logic picks a sufficient match based on LCP similarity rather than the optimal one (i.e., the prompt with the largest reusable prefix). This leads to significant unnecessary recomputation, especially for very long prompts (~40k–50k tokens), even when better matches exist in the cache. Could you please confirm if this is correct? What happens when all requests use the same slot? Will one request invalidate or overwrite the cache of other requests, forcing recomputation? What happens when two clients use different slots, but both requests share a 50k-token prompt prefix? If slots do not share cache, would it be better to use a single slot instead? Agent workflows typically involve multiple sessions that start with the same initial prompt prefix, after which they diverge through multi-turn conversation messages appended at the end. Agents that support branching conversations from any point would require the ability to reuse and continue from the cache of the latest checkpoint, as well as from previous checkpoints. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This tutorial demonstrates how to use the slots management feature in
llama-serverto optimize repeated prompt processing through KV cache reuse.Server Setup
Start the server with the desired number of slots:
Key parameters:
-c 1024 -np 2: Creates 2 processing slots with(1024/2) = 512context tokens eachUpon startup, the logs should indicate the initialization of two slots:
By default,
llama-serverattempts to assign slot to a new request based on prompt similarity. The-spsparameter controls this behavior. A value of 0.5 (the default) means a slot is considered a match if at least 50% of the prompt context matches.Automatic slot selection can be disabled by setting
-sps 0.0. The slot to use can be explicitly specified in the request using the"id_slot"parameter in the request body.Prompt caching
Basic Request Example
Here's a sample curl request:
The parameter
cache_prompt: true(default) instructs the server to cache the prompt. While redundant in this case, it's good practice to be explicit.Server Logs & Slot Usage:
When the request is processed, the server logs will show the details about the slot used, context tokens matched and any KV cache eviction. Execution logs for the first query confirm full prompt processing:
This indicates that slot 0 was used, 0 tokens were matched (
n_past - n_tokens, or1 - progressas fraction), the slot's KV cache was fully cleared before processing the prompt.Subsequent identical requests reuse the cached context on slot 0 (with the default value of 0.5 for
-spsarg):This should result in significantly faster processing times as the KV cache is reused.
Manual Slot Assignment
Force a specific slot by including
id_slotin the request:{ "id_slot": 1, "messages": [...] }In this scenario, the prompt is processed from scratch in slot 1.
Partial Context Reuse
If you submit a slightly modified request (e.g. keeping the system prompt same with a different user query in the above example request), the server will leverage the existing cache in slot 1 for the common parts of the prompt and only process the new tokens.
Slot Persistence
This feature depends on
--slotsargument for the server, which enables the/slotsAPI endpoint.Warning
As per the server docs, this endpoint may change in the future and could pose a security risk on production systems. It is advised to consider this only if necessary and in secure or air-gapped setups.
Key parameters:
--slots: Enable the/slotsAPI endpoint--slot-save-path: Directory for persistent slot storageSave slot 0's KV cache
Output:
{"id_slot":0,"filename":"slot0.bin","n_saved":72,"n_written":7669224,"timings":{"save_ms":1.401}}Restore to slot 1
Output:
{"id_slot":1,"filename":"slot0.bin","n_restored":72,"n_read":7669224,"timings":{"restore_ms":0.739}}Now slot 1 will contain the pre-computed KV cache, leading to faster response times. Subsequent requests will use the restored cache until the context changes significantly or the cache is explicitly cleared. You’ll see log output similar to that of a reused slot.
Implementation Considerations
Use Cases
Optimization Guidelines
Slot Management:
Performance:
Notes
-spsparameter doesn’t always behave predictably, with inconsistent and unexpected slot switching observed. Further investigation is needed.Would welcome discussion from devs/maintainers about best practices and potential improvements.
All reactions