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?