server: save only restorable slot states to the prompt cache (#30498)
* server: save only restorable slot states to the prompt cache The idle-slot save loop stores every idle slot whenever a task starts, while only completion tasks restore from the prompt cache. Rerank, embedding, decision and infill states fill the cache up to its limit without ever being read back, and slots already cached are offered again at every task start. Each slot now tracks with cache_pending whether its state is restorable and missing from the cache: a completion release or an explicit slot restore sets it, prompt_save consumes it, prompt_clear resets it. Both save paths share this rule, and the LRU path restores only for completion tasks. * server: test that decision slots skip the prompt cache Decision tasks never mark their slot as cacheable, so starting new tasks must not save their idle slots to the RAM prompt cache, with or without a unified KV cache. * server: restore any reusing task from the prompt cache Embedding and rerank tasks never reuse their state, so their slots are cleared on release like child slots and the cache only holds states that a later task can restore. Infill and decision tasks now restore from the cache too, which replaces the cache_pending flag. Decision models disable the prompt cache unless --cache-ram is given, since their states seldom repeat.
This commit is contained in:
1 parent
843e7dcc15
commit
2639a3b4e0
8 files changed
+73
-9
No files matched your search
@@ -594,7 +594,8 @@ struct server_slot {
|
||||
state = SLOT_STATE_IDLE;
|
||||
|
||||
// do not keep context of the child slots - the parent's context is enough
|
||||
if (task->is_child()) {
|
||||
// nor of the stateless slots - no task ever reuses it
|
||||
if (task->is_child() || task->is_stateless()) {
|
||||
prompt_clear();
|
||||
}
|
||||
|
||||
@@ -1489,6 +1490,13 @@ private:
|
||||
batch.init(ctx_tgt, std::max(n_batch, params_base.n_parallel), n_embd);
|
||||
}
|
||||
|
||||
// decision requests rarely repeat a state, the prompt cache is opt-in for decision models
|
||||
if (decision.type != COMMON_DECISION_TYPE_NONE && !params_base.cache_ram_explicit) {
|
||||
SRV_INF("%s", "decision model: prompt cache disabled by default, use `--cache-ram N` to enable it\n");
|
||||
params_base.cache_ram_mib = 0;
|
||||
params_base.cache_idle_slots = false;
|
||||
}
|
||||
|
||||
if (params_base.cache_ram_mib != 0) {
|
||||
if (params_base.cache_ram_mib < 0) {
|
||||
SRV_TRC("prompt cache is enabled, size limit: %s\n", "no limit");
|
||||
@@ -1782,8 +1790,8 @@ private:
|
||||
if (ret) {
|
||||
update_cache = update_cache && prompt_cache;
|
||||
|
||||
// cache prompts only for completion tasks
|
||||
update_cache = update_cache && task.type == SERVER_TASK_TYPE_COMPLETION;
|
||||
// cache prompts only for the tasks that reuse them
|
||||
update_cache = update_cache && !task.is_stateless();
|
||||
|
||||
if (update_cache) {
|
||||
SRV_TRC("%s", "updating prompt cache\n");
|
||||
@@ -3597,9 +3605,7 @@ private:
|
||||
return;
|
||||
}
|
||||
|
||||
const bool is_stateless_task = slot.task->type == SERVER_TASK_TYPE_EMBEDDING || slot.task->type == SERVER_TASK_TYPE_RERANK;
|
||||
|
||||
if (slot.task->params.cache_prompt && !is_stateless_task) {
|
||||
if (slot.task->params.cache_prompt && !slot.task->is_stateless()) {
|
||||
// reuse any previously computed tokens that are common with the new prompt
|
||||
n_past = slot.prompt.tokens.get_common_prefix(input_tokens);
|
||||
|
||||
|
||||
Reference in new issue
Block a user