Repository navigation
Conversation
`jitllm serve -m Llama-3.2-3B-Instruct-F16.gguf --gpu` failed with "Invalid number of elements (-536870912)": without --ctx-size the server asked for the model's 131,072-token window, whose key/value array (28 layers x 1024 x 131072 = 3.76e9 elements) exceeds what an int-indexed TornadoVM array holds, and State.fillKvFields computed the size in an int that wrapped negative. - KeyValueAddressing: one place for the arithmetic (largest context a private or pooled key/value array can address). - State.fillKvFields counts in long and refuses an overflow with the new [GPUL-MEM-004], naming the largest context that fits. - LocalModels.largestFittingContext (@experimental) / MemoryPreflight: the model's own window, unless the array limit or the configured memory budget is shorter (a search over the metadata-only preflight), returned as a ContextFit that says which limit applied. - serve: without --ctx-size it uses that and prints why. An explicit --ctx-size the cache cannot address fails with [GPUL-MEM-004] before any weight is read. Continuous batching uses the pooled bound. Verified on an Apple M4 Pro (Metal): the 3B now starts with 36,864 tokens and answers; --ctx-size 131072 gives the one-line error; Llama-3.2-1B keeps its full 131,072-token window.
The context fit asked Configuration.kvDim(), which Gemma 4 refuses to
answer because its layers differ in width, so `jitllm serve` with a
Gemma 4 model failed at once with UnsupportedOperationException ("Gemma4
has per-layer head dimensions") where main serves it.
Configuration.keyValueAddressingWidth() is kvDim() for a model whose
layers share one width, and the widest layer for Gemma 4, which errs
towards a shorter context rather than an array that overflows. The fit
and serve's explicit-length check use it. Gemma 4 E2B Q8_0 now serves at
119,808 tokens and answers.
This branch has not been deployed
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.
Summary
Without
--ctx-size,jitllm serveasked for the model's full window. On long-window models that crashed while sizing the key/value cache. This PR fixes the overflow and makesservechoose the longest context that fits: the model's own window, unless one key/value array can't address it or the GPU memory budget can't hold it. It prints which limit applied. An explicit--ctx-sizethat can't fit now fails at once with a coded error.Problem
On
mainthis fails during startup:Llama-3.2-3B's window is 131,072 tokens, so one sequence's key/value array has 28 layers × 1024 × 131,072 = 3.76e9 elements. That's more than an int-indexed TornadoVM array can hold, and
State.fillKvFieldscomputed the count in anint, which wrapped negative. A user had to know to pass a smaller--ctx-size, and the message didn't say which one.Changes
KeyValueAddressing(new,runtime.memory): one place for the arithmetic. It gives the number of elements in a private or pooled key/value array, and the longest context each can address.State.fillKvFields: counts inlong. A context that can't be addressed throws the newGPUL-MEM-004(KV_CACHE_NOT_ADDRESSABLE), which names the longest context that fits.LocalModels.largestFittingContext(Path, ModelOptions)(@Experimental), viaMemoryPreflight, works from metadata only and loads no weights. It returns aContextFitwith:MODEL,ADDRESSINGorMEMORY_BUDGET).The budget bound comes from a binary search, over whole key/value blocks, of the existing preflight. A long result is rounded down to a multiple of 1,024 tokens.
serve:--ctx-size, it uses that context and says why on stderr.--ctx-size, a context the cache can't address fails withGPUL-MEM-004before any weight is read.Configuration.keyValueAddressingWidth(): the key/value width the context is sized by. It iskvDim()where all layers share one width. Gemma 4's layers differ, and it refuseskvDim(), so it uses its widest layer. That errs towards a shorter context rather than an array that overflows.KeyValueAddressingTest: element counts, the 3B overflow, private and pooled bounds, and the error message.Testing
Unit tests:
./mvnw -Pjdk22plus testgives 985 tests, 0 failures (7 skipped).On an Apple M4 Pro (Metal, TornadoVM 8.2.0 SDK,
serve --gpu):main--ctx-sizeInvalid number of elements (-536870912)[server] context 36864 tokens; the model supports 131072, but the GPU memory budget (14GB, --gpu-memory) holds the weights and caches up to this window. Set --ctx-size to choose another.--ctx-size 131072[GPUL-MEM-004] a context of 131072 tokens needs a key/value cache larger than one TornadoVM array can address; the most this model can hold is 74896 tokens. Pass --ctx-size 74896 or less (or leave it out to use the largest that fits).--ctx-size[server] context 131072 tokens (the model's own window)and answers--ctx-sizeone key/value array ... holds at most 119824 tokens of this model) and answersTo check:
Not covered
--continuous-batching) end to end; its pooled bound is covered by the unit test.kvDim(),hiddenDim()andheadSize()), and a model it can't plan is taken to fit. A follow-up PR adds Gemma 4's memory plan.