Gemma 3: chunked prompt prefill, skip lm_head on prompt positions#346
Open
beshkenadze wants to merge 1 commit into
Open
Gemma 3: chunked prompt prefill, skip lm_head on prompt positions#346beshkenadze wants to merge 1 commit into
beshkenadze wants to merge 1 commit into
Conversation
Gemma3TextModel.prepare previously returned the whole prompt to the TokenIterator, which then ran the 262k-vocabulary lm_head over every prompt position just to produce the first token. Instead, prefill all but the last token through the inner model (which has no lm_head), updating only the KV cache, and hand just the final token to the iterator. The chunk size honours an explicit GenerateParameters.prefillStepSize and otherwise defaults to 128, tuned for this path on Apple Silicon for the best asyncEval CPU/GPU pipelining. To make that default reachable, prefillStepSize becomes optional (nil = let the model choose); other models keep their 512 default via `windowSize ?? 512`. Measured on translategemma-4b-it-4bit (Apple Silicon, 577-token prompt, greedy): prompt prefill 177 -> 463 tok/s (2.6x), 3253 -> 1246 ms. Greedy output is byte-identical to before, including prompts longer than the 1024 sliding window where the rotating KV cache rotates.
5faffc8 to
c4aa0c4
Compare
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.
What
Speeds up prompt prefill (time-to-first-token) for all Gemma 3 text models by skipping the large
lm_headon prompt positions and chunking the prefill for better CPU/GPU overlap.Why
Gemma3TextModel.preparereturned the whole prompt to theTokenIterator, which then ran the 262k-vocabularylm_headover every prompt position just to produce the first token. For Gemma 3's unusually large vocab this is a real TTFT tax that grows with prompt length, plus a large transient-logits memory spike.How
Gemma3Model(nolm_head), updating only the KV cache; the discarded hidden states are never evaluated thanks to lazy eval. Only the final token goes through the full model to prime the iterator.asyncEvalper chunk so chunk N+1's graph builds while the GPU evaluates chunk N. Chunk size honours an explicitGenerateParameters.prefillStepSizeand otherwise defaults to 128, empirically tuned for this path on Apple Silicon. To make that default reachable,prefillStepSizebecomes optional (nil= let the model choose); other models keep their 512 default viawindowSize ?? 512— no behaviour change for them.Results
mlx-community/translategemma-4b-it-4bit(Gemma 3 text), Apple Silicon, 577-token prompt, greedy, median of 3:2.6× faster prefill. Greedy output is byte-identical to before, including prompts longer than the 1024 sliding window where the rotating KV cache rotates.
Tuning (chunk-size sweep)
128 wins: larger chunks lose
asyncEvalpipelining; smaller ones drown in per-launch overhead.Scope / risk
GenerateParameters.prefillStepSizechangesInt→Int?(constructing with an explicit value is unchanged; only the default becomes "model decides").