]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
server: fix non-bound n_discard value (ctx shifting) (#24786)
authorXuan-Son Nguyen <redacted>
Fri, 19 Jun 2026 08:53:44 +0000 (10:53 +0200)
committerGitHub <redacted>
Fri, 19 Jun 2026 08:53:44 +0000 (10:53 +0200)
* server: fix non-bound n_discard value

* Update tools/server/server-context.cpp

Co-authored-by: Georgi Gerganov <redacted>
---------

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

index ded622cfd680ea661001ddad33cdea960c9c88d3..a23b0405ce2227186f13252beb4035ee610bf50b 100644 (file)
@@ -2565,7 +2565,10 @@ private:
                 n_keep = std::min(slot.n_ctx - 4, n_keep);
 
                 const int n_left    = slot.prompt.n_tokens() - n_keep;
-                const int n_discard = slot.task->params.n_discard ? slot.task->params.n_discard : (n_left / 2);
+                int       n_discard = slot.task->params.n_discard ? slot.task->params.n_discard : (n_left / 2);
+
+                // ref: https://github.com/ggml-org/llama.cpp/pull/24786
+                n_discard = std::clamp(n_discard, 0, std::max(0, n_left - 1));
 
                 SLT_WRN(slot, "slot context shift, n_keep = %d, n_left = %d, n_discard = %d\n", n_keep, n_left, n_discard);