]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
server: avoid unnecessary checkpoint restore when new tokens are present (#24110)
authorYongyue Sun <redacted>
Thu, 4 Jun 2026 13:09:01 +0000 (21:09 +0800)
committerGitHub <redacted>
Thu, 4 Jun 2026 13:09:01 +0000 (16:09 +0300)
* server: avoid unnecessary checkpoint restore when new tokens are present

The pos_min_thold calculation unconditionally subtracts 1 to ensure at
least one token is evaluated for logits when no new tokens exist.
However, when the request contains new tokens beyond the cached prefix,
this -1 is overly conservative and may trigger an unnecessary checkpoint
restore.

Conditionally apply the -1 only when n_past >= task.n_tokens() (no new
tokens), avoiding redundant KV state restoration when there is actual
work to do.

* cont : add ref

---------

Co-authored-by: Georgi Gerganov <redacted>
tools/server/server-context.cpp

index 28b1158c7769a8895b973c0760214469be0bf3a6..28f738c3feb20b23a67ece6b6a8cdab0068a0d4b 100644 (file)
@@ -2782,8 +2782,11 @@ private:
 
                             llama_pos pos_next = slot.prompt.tokens.pos_next(n_past);
 
+                            // ref: https://github.com/ggml-org/llama.cpp/pull/24110
+                            const bool has_new_tokens = (n_past < slot.task->n_tokens());
+
                             // the largest pos_min required for a checkpoint to be useful
-                            const auto pos_min_thold = std::max(0, pos_next - n_swa - 1);
+                            const auto pos_min_thold = std::max(0, pos_next - n_swa - (has_new_tokens ? 0 : 1));
 
                             if (n_past > 0 && n_past <= slot.prompt.n_tokens()) {
                                 const auto pos_min = llama_memory_seq_pos_min(llama_get_memory(ctx_tgt), slot.id);