From 55d94b6cc4d067c029e6cf2bfacec5d13e2b061a Mon Sep 17 00:00:00 2001 From: mikepapadim Date: Sat, 11 Jul 2026 16:30:38 +0100 Subject: [PATCH] Fix IllegalArgumentException on long prompts: clamp generatedTokens preallocation at 0 When the prompt is longer than the token budget (actualMaxTokens), the preallocation capacity actualMaxTokens - promptTokens.size() is negative and new ArrayList<>(negative) throws IllegalArgumentException: Illegal Capacity. Clamp with Math.max(0, ...) in both generation paths. --- .../beehive/gpullama3/inference/InferenceEngine.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/beehive/gpullama3/inference/InferenceEngine.java b/src/main/java/org/beehive/gpullama3/inference/InferenceEngine.java index d653ba58..1ee56853 100644 --- a/src/main/java/org/beehive/gpullama3/inference/InferenceEngine.java +++ b/src/main/java/org/beehive/gpullama3/inference/InferenceEngine.java @@ -285,8 +285,10 @@ public static List generateTokensGPULlama(Model model, State state, int // Pre-validate the max tokens to avoid checking in the loop int actualMaxTokens = Math.min(maxTokens > 0 ? maxTokens : model.configuration().contextLength(), model.configuration().contextLength()); - // Preallocate with expected capacity to avoid resizing - List generatedTokens = new ArrayList<>(Math.min(256, actualMaxTokens - promptTokens.size())); // Conservative estimate + // Preallocate with expected capacity to avoid resizing. Clamp at 0: when the + // prompt is longer than the token budget (actualMaxTokens), the difference is + // negative and would throw IllegalArgumentException("Illegal Capacity"). + List generatedTokens = new ArrayList<>(Math.max(0, Math.min(256, actualMaxTokens - promptTokens.size()))); // Conservative estimate // === Token Generation Loop === int currentToken = state.latestToken; @@ -373,8 +375,10 @@ public static List generateTokensGPUQwen3(Model model, State state, int // Pre-validate the max tokens to avoid checking in the loop int actualMaxTokens = Math.min(maxTokens > 0 ? maxTokens : model.configuration().contextLength(), model.configuration().contextLength()); - // Preallocate with expected capacity to avoid resizing - List generatedTokens = new ArrayList<>(Math.min(256, actualMaxTokens - promptTokens.size())); // Conservative estimate + // Preallocate with expected capacity to avoid resizing. Clamp at 0: when the + // prompt is longer than the token budget (actualMaxTokens), the difference is + // negative and would throw IllegalArgumentException("Illegal Capacity"). + List generatedTokens = new ArrayList<>(Math.max(0, Math.min(256, actualMaxTokens - promptTokens.size()))); // Conservative estimate // Initialize token variables int currentToken = state.latestToken; // BOS?