Fix IllegalArgumentException (negative ArrayList capacity) on prompts longer than max-tokens#128
Open
mikepapadim wants to merge 1 commit into
Open
Conversation
…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.
Contributor
There was a problem hiding this comment.
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
ArrayListinitial capacity to>= 0ingenerateTokensGPULlama. - Clamp
ArrayListinitial capacity to>= 0ingenerateTokensGPUQwen3. - 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"). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes a crash when the prompt is longer than the generation token budget.
InferenceEngine.generateTokens*preallocates the output list with:When
promptTokens.size() > actualMaxTokens(a long prompt with a small-n/--max-tokens),actualMaxTokens - promptTokens.size()is negative, sonew ArrayList<>(negative)throws:Fix: clamp the capacity at 0 with
Math.max(0, ...)in both generation paths.Repro
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").