Fix Gemma 3 mixed-batch token type IDs - #9786
Open
GoGiants1 wants to merge 1 commit into
Open
Conversation
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
GoGiants1
marked this pull request as ready for review
July 22, 2026 08:40
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
tastelikefeet
approved these changes
Jul 30, 2026
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.
PR type
PR information
Gemma3VisionTemplate._encodepreviously emittedtoken_type_idsonly for rows containing images.BaseTemplate.data_collatorgathers this field only from rows where it is present, so a mixed image/text batch could have different batch dimensions:Gemma 3's eager causal-mask implementation indexes
token_type_idsusing the full input batch index, which then accesses a row that does not exist and triggers a CUDA device-side assertion.This PR initializes
token_type_idsto all zeros for every row. Zero is the existing text-token type, so text-only semantics are unchanged. Image rows still overwrite the initialized values with1at image-token positions. A lightweight regression test covers the text-only encoding path.Minimal reproduction
The following script creates one image row and one text-only row, keeps them in the same batch, and runs one eager-attention training step. It requires access to
google/gemma-3-4b-ptand one CUDA GPU.Before this change, the model forward fails while constructing the eager causal mask. The relevant excerpt from the observed multi-GPU training failure is:
Experiment results
After the change:
Validation commands: