Skip to content

Commit 7ce56f6

Browse files
committed
feat: Add specific exception messages for llama_decode failure codes
1 parent e10e36e commit 7ce56f6

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

llama_cpp/_internals.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,19 @@ def encode(self, batch: LlamaBatch):
436436
raise RuntimeError(f"llama_encode returned {return_code}")
437437

438438
def decode(self, batch: LlamaBatch):
439-
return_code = llama_cpp.llama_decode(
440-
self.ctx,
441-
batch.batch,
442-
)
443-
if return_code != 0:
444-
raise RuntimeError(f"llama_decode returned {return_code}")
439+
return_code = llama_cpp.llama_decode(self.ctx, batch.batch)
440+
441+
if return_code == 0:
442+
return
443+
444+
error_map = {
445+
1: "No KV slot available: try reducing batch size or increasing context window",
446+
2: "Decoding aborted",
447+
-1: "Invalid input batch",
448+
}
449+
450+
msg = error_map.get(return_code, "Fatal internal error")
451+
raise RuntimeError(f"llama_decode failed (code {return_code}): {msg}")
445452

446453
def set_n_threads(self, n_threads: int, n_threads_batch: int):
447454
llama_cpp.llama_set_n_threads(self.ctx, n_threads, n_threads_batch)

0 commit comments

Comments
 (0)