Skip to content

Fix IllegalArgumentException (negative ArrayList capacity) on prompts longer than max-tokens#128

Open
mikepapadim wants to merge 1 commit into
beehive-lab:feat/mma_cudafrom
mikepapadim:fix/token-capacity
Open

Fix IllegalArgumentException (negative ArrayList capacity) on prompts longer than max-tokens#128
mikepapadim wants to merge 1 commit into
beehive-lab:feat/mma_cudafrom
mikepapadim:fix/token-capacity

Conversation

@mikepapadim

@mikepapadim mikepapadim commented Jul 11, 2026

Copy link
Copy Markdown
Member

Description

Fixes a crash when the prompt is longer than the generation token budget.

InferenceEngine.generateTokens* preallocates the output list with:

new ArrayList<>(Math.min(256, actualMaxTokens - promptTokens.size()));

When promptTokens.size() > actualMaxTokens (a long prompt with a small -n / --max-tokens), actualMaxTokens - promptTokens.size() is negative, so new ArrayList<>(negative) throws:

java.lang.IllegalArgumentException: Illegal Capacity: -883
    at org.beehive.gpullama3.inference.InferenceEngine.generateTokensGPULlama(InferenceEngine.java:289)

Fix: clamp the capacity at 0 with Math.max(0, ...) in both generation paths.

Repro

llama-tornado --gpu --cuda --model beehive-llama-3.2-1b-instruct-fp16.gguf \
  --prompt "<~900-token prompt>" -n 48 --instruct

Before: IllegalArgumentException: Illegal Capacity. After: generates normally (verified: correct output, no crash, on an RTX 4090 / CUDA backend). Short prompts unaffected (e.g. "What is 2+2?" → "2 + 2 = 4").

…reallocation 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.
Copilot AI review requested due to automatic review settings July 11, 2026 15:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR prevents a crash in GPU token generation when the prompt length exceeds the remaining token budget by ensuring the ArrayList preallocation capacity never becomes negative.

Changes:

  • Clamp ArrayList initial capacity to >= 0 in generateTokensGPULlama.
  • Clamp ArrayList initial capacity to >= 0 in generateTokensGPUQwen3.
  • Add explanatory comments describing why the clamp is needed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +378 to +381
// 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<Integer> generatedTokens = new ArrayList<>(Math.max(0, Math.min(256, actualMaxTokens - promptTokens.size()))); // Conservative estimate
Comment on lines +288 to +290
// 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").
Comment on lines +378 to +380
// 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").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants