Fix GPU Hang in Gemma4 and add metrics#40
Merged
Merged
Conversation
added 2 commits
May 12, 2026 13:10
…tter and implement MTP acceptance rate metric extraction
There was a problem hiding this comment.
Pull request overview
This PR aims to (1) prevent a GPU hang in Gemma4’s sparse masked-embedder logit projection by removing a per-row scatter loop, and (2) propagate speculative/MTP draft-token metrics into the generation completion info so callers can observe acceptance rates.
Changes:
- Adds
acceptedDraftTokens/totalDraftTokenstracking to the common token-iterator interface and includes them inGenerateCompletionInfoemitted at the end of async generation. - Updates
SpeculativeTokenIteratorto accumulate accepted/total draft-token counts per speculation round. - Replaces a CPU-style per-row scatter loop in
Gemma4Text.maskedEmbedderLogitswith a vectorized advanced-index assignment.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Libraries/MLXLMCommon/Evaluate.swift | Adds draft-token metrics to iterators and propagates them into GenerateCompletionInfo for async generation. |
| Libraries/MLXLLM/Models/Gemma4Text.swift | Vectorizes candidate-logit scattering into the output vocab tensor to address a GPU hang. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1120
to
+1121
| let rowIndices = MLXArray(0 ..< Int32(B * S)).reshaped([B * S, 1]) | ||
| output2D[rowIndices, scatterIdx2D] = selectedLogits2D |
solderzzc
commented
May 12, 2026
solderzzc
left a comment
Member
Author
There was a problem hiding this comment.
Great catch regarding Int32(B * S)! Using MLXArray.arange(B * S).asType(.int32) correctly bypasses the Swift 32-bit width overflow trap while keeping the MLX pipeline safe. Applied in the latest commit.
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.
Pushes the final GPU hang fix and MTP metric propagation that was missed before the main branch merge.