Commit 40fe9164f for llama.cpp

commit 40fe9164f43b4e7b4bf2a261813eed768c367468
Author: Noah <99681487+NoahOksuz@users.noreply.github.com>
Date:   Tue Nov 24 13:13:59 2026 +0000

    server : retry prompt cache restore after clearing idle slots (#30997)

    With a unified KV cache, a RAM-cached prompt can fail to restore because another idle slot still holds the cells it needs. Save and clear the other idle slots and retry the restore once in update_prompt_cache(). Builds on #32828, which keeps the cache entry intact when a restore fails.

    Fixes #30961.

diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp
index 932d08bc7..2f927d74f 100644
--- a/tools/server/server-context.cpp
+++ b/tools/server/server-context.cpp
@@ -1824,6 +1824,29 @@ private:
         return res;
     }

+    // save idle slots (except id_slot_keep) to the prompt cache; with a unified KV cache, also clear them
+    void cache_idle_slots(int32_t id_slot_keep = -1) {
+        if (!params_base.cache_idle_slots) {
+            return;
+        }
+
+        for (auto & slot : slots) {
+            if (slot.id != id_slot_keep && !slot.is_processing()) {
+                SLT_TRC(slot, "%s", "saving idle slot to prompt cache\n");
+
+                if (slot.prompt_save(*prompt_cache)) {
+                    SLT_DBG(slot, "%s", "__TEST_TAG_CACHE_IDLE_SLOT__\n");
+                    prompt_cache->update();
+                }
+
+                if (params_base.kv_unified) {
+                    // [TAG_IDLE_SLOT_CLEAR]
+                    slot.prompt_clear();
+                }
+            }
+        }
+    }
+
     // call only when the task is sure to launch on the slot
     void update_prompt_cache(server_slot & slot, const server_task & task) {
         SRV_TRC("%s", "updating prompt cache\n");
@@ -1832,8 +1855,22 @@ private:

         slot.prompt_save(*prompt_cache);

-        if (!slot.prompt_load(*prompt_cache, task.tokens, get_task_lora(task))) {
+        const auto lora = get_task_lora(task);
+
+        if (!slot.prompt_load(*prompt_cache, task.tokens, lora)) {
             slot.prompt_clear();
+
+            // with a unified KV cache, the restore can fail because idle slots still hold the cells it needs
+            // the cache entry is kept on failure, so free those slots and retry once
+            if (params_base.kv_unified && params_base.cache_idle_slots) {
+                cache_idle_slots(slot.id);
+
+                SLT_INF(slot, "%s", "retrying prompt restore after clearing idle slots\n");
+
+                if (!slot.prompt_load(*prompt_cache, task.tokens, lora)) {
+                    slot.prompt_clear();
+                }
+            }
         }

         prompt_cache->update();
@@ -3022,23 +3059,7 @@ private:
                         break; // drop the task
                     }

-                    if (params_base.cache_idle_slots) {
-                        for (auto & slot : slots) {
-                            if (!slot.is_processing()) {
-                                SLT_TRC(slot, "%s", "saving idle slot to prompt cache\n");
-
-                                if (slot.prompt_save(*prompt_cache)) {
-                                    SLT_DBG(slot, "%s", "__TEST_TAG_CACHE_IDLE_SLOT__\n");
-                                    prompt_cache->update();
-                                }
-
-                                if (params_base.kv_unified) {
-                                    // [TAG_IDLE_SLOT_CLEAR]
-                                    slot.prompt_clear();
-                                }
-                            }
-                        }
-                    }
+                    cache_idle_slots();
                 } break;
             case SERVER_TASK_TYPE_CANCEL:
                 {
diff --git a/tools/server/tests/unit/test_kv_keep_only_active.py b/tools/server/tests/unit/test_kv_keep_only_active.py
index 44c05fab0..d8838f9e0 100644
--- a/tools/server/tests/unit/test_kv_keep_only_active.py
+++ b/tools/server/tests/unit/test_kv_keep_only_active.py
@@ -89,6 +89,44 @@ def test_clear_and_restore():
     assert "__TEST_TAG_CACHE_IDLE_SLOT__" not in log.drain()


+@pytest.mark.parametrize("n_slots,n_ctx,cache_ram,n_tokens_a,n_tokens_b", [
+    (2, 512, 100, 350, 300),
+    (3, 512, 100, 350, 300),
+    # Both prompts fit in KV, but their states do not fit together in RAM.
+    (2, 2048, 1, 1200, 700),
+])
+def test_restore_under_cache_pressure(n_slots, n_ctx, cache_ram, n_tokens_a, n_tokens_b):
+    global server
+    server.n_slots = n_slots
+    server.n_ctx = n_ctx
+    server.cache_ram = cache_ram
+    server.override_kv = f"llama.context_length=int:{n_ctx}"
+    server.start()
+
+    res = server.make_request("POST", "/tokenize", data={"content": "Hello world"})
+    assert res.status_code == 200
+    tokens = res.body["tokens"]
+    prompt_a = [tokens[0]] * n_tokens_a
+    prompt_b = [tokens[-1]] * n_tokens_b
+
+    for prompt, n_cached in [
+        (prompt_a, 0),
+        (prompt_a + [tokens[0]] * 10, n_tokens_a),
+        (prompt_b, 0),
+        (prompt_a + [tokens[0]] * 20, n_tokens_a + 10),
+        (prompt_b + [tokens[-1]] * 10, n_tokens_b),
+        (prompt_a + [tokens[0]] * 30, n_tokens_a + 20),
+    ]:
+        res = server.make_request("POST", "/completion", data={
+            "prompt": prompt,
+            "n_predict": 1,
+            "cache_prompt": True,
+        })
+        assert res.status_code == 200
+        assert res.body["timings"]["cache_n"] >= n_cached
+        assert res.body["timings"]["prompt_n"] <= len(prompt) - n_cached
+
+
 def test_disabled_with_flag():
     global server
     server.no_cache_idle_slots = True