Commit d293b7dad for llama.cpp
commit d293b7dad189d01344efb3784be2c6021788af67
Author: Md Raihanul Haque Rahi <52377279+RaihanulHaque@users.noreply.github.com>
Date: Fri Dec 4 17:51:27 2026 +0600
llama : return an error instead of aborting when a batch exceeds n_batch or n_ubatch (#30523)
* llama : return an error instead of aborting when a batch exceeds n_batch or n_ubatch
llama_decode documents -1 for an invalid batch, but a
batch larger than n_batch (or n_ubatch for non-causal) hit a
GGML_ASSERT and aborted the process. A causal context caps n_batch at n_ctx, so
a caller that asks for n_batch > n_ctx and submits a full batch crashes. This
was found in an application that embeds llama.cpp, where a small context made
an audio/image prompt abort the app instead of failing the call.
Return -1 with an error message instead, before any state is touched, and add
the batch_big case to test-llama-context: it submits n_max + 1 tokens, expects
an error and an empty memory, then decodes n_max tokens.
* llama : drop the batch_big test and shorten the n_batch error message (review)
diff --git a/src/llama-context.cpp b/src/llama-context.cpp
index 8de28ed38..54b30f8e2 100644
--- a/src/llama-context.cpp
+++ b/src/llama-context.cpp
@@ -2236,7 +2236,11 @@ int llama_context::decode(const llama_batch_ext & batch_inp) {
}
}
- GGML_ASSERT(n_tokens_all <= cparams.n_batch);
+ if (n_tokens_all > cparams.n_batch) {
+ LLAMA_LOG_ERROR("%s: n_tokens = %u exceeds n_batch = %u\n",
+ __func__, n_tokens_all, cparams.n_batch);
+ return -1;
+ }
if (llm_graph_pools_spans(cparams) && balloc->get_n_spans() > LLAMA_MAX_POOL_SPANS) {
LLAMA_LOG_ERROR("%s: too many spans to pool, n_spans = %u > %d\n", __func__, balloc->get_n_spans(), LLAMA_MAX_POOL_SPANS);
@@ -2250,7 +2254,11 @@ int llama_context::decode(const llama_batch_ext & batch_inp) {
return -1;
}
- GGML_ASSERT((cparams.causal_attn || cparams.n_ubatch >= n_tokens_all) && "non-causal attention requires n_ubatch >= n_tokens");
+ if (!cparams.causal_attn && n_tokens_all > cparams.n_ubatch) {
+ LLAMA_LOG_ERROR("%s: non-causal attention requires n_ubatch >= n_tokens (n_ubatch = %u, n_tokens = %u)\n",
+ __func__, cparams.n_ubatch, n_tokens_all);
+ return -1;
+ }
// TODO: this clear of the buffer can easily be forgotten - need something better
// sync first so any in-flight async copies into embd_seq complete before it is freed