From 25812762c9eb3d01a38fb7dba0fd9d7cde50b53b Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Wed, 24 Apr 2024 14:54:02 +0100 Subject: [PATCH] Added checks in `Decode` to skip doing anything if the batch is empty. --- LLama/Native/SafeLLamaContextHandle.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/LLama/Native/SafeLLamaContextHandle.cs b/LLama/Native/SafeLLamaContextHandle.cs index c9e959a09..51568769d 100644 --- a/LLama/Native/SafeLLamaContextHandle.cs +++ b/LLama/Native/SafeLLamaContextHandle.cs @@ -368,6 +368,9 @@ public uint TokenToSpan(LLamaToken token, Span dest) /// public DecodeResult Decode(LLamaBatch batch) { + if (batch.TokenCount == 0) + return DecodeResult.Ok; + lock (GlobalInferenceLock) using (batch.ToNativeBatch(out var nb)) return (DecodeResult)llama_decode(this, nb); @@ -383,6 +386,9 @@ public DecodeResult Decode(LLamaBatch batch) /// A tuple, containing the decode result and the number of tokens that have not been decoded yet. internal (DecodeResult, int) Decode(List tokens, LLamaSeqId id, LLamaBatch batch, ref int n_past) { + if (tokens.Count == 0) + return (DecodeResult.Ok, 0); + var batchSize = checked((int)BatchSize); // Evaluate the prompt, in chunks smaller than the max batch size