fix(gqa): replace float division with integer division in GQA reshape#668
Open
Sumu004 wants to merge 1 commit into
Open
fix(gqa): replace float division with integer division in GQA reshape#668Sumu004 wants to merge 1 commit into
Sumu004 wants to merge 1 commit into
Conversation
In grouped query attention (GQA), the number of query heads per KV head was computed as int(kg / self.num_kv_heads) using float division + int(). When kg is not exactly divisible by num_kv_heads, float division produces a non-integer result that int() silently truncates. This yields an incorrect reshape dimension with no error, causing silent shape corruption or an unexpected crash in non-standard head configurations. Replaced all 8 occurrences across 4 files with integer division (//), which is semantically correct for integer tensor dimensions and makes the intent explicit. Affected files: - gemma/gm/nn/_modules.py (lines 244, 288) - gemma/gm/nn/gemma4/_modules.py (lines 326, 365) - gemma/gm/nn/gemma3n/_modules.py (lines 342, 387) - gemma/research/t5gemma/modules.py (lines 240, 272) Fixes: google-deepmind#641
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.
Problem
In the GQA (Grouped Query Attention) reshape operations, the number of query heads per KV head group is computed as:
This is semantically wrong for two reasons:
Silent truncation: When
kg % num_kv_heads != 0, float division produces a non-integer (e.g.7/4 = 1.75), andint()silently truncates it to1. The reshape proceeds with a wrong dimension — either silently corrupting the tensor shape or crashing with a confusing size-mismatch error.Semantic mismatch: Tensor dimensions are integers by definition. Using float division here obscures the intent and masks invalid configurations.
Standard Gemma model configs (where
num_query_headsis always a multiple ofnum_kv_heads) happen to produce exact divisions, so this bug is latent in production — but it surfaces immediately with custom or experimental head configurations.Fix
Replace all 8 occurrences of
int(kg / self.num_kv_heads)withkg // self.num_kv_heads:Affected files
gemma/gm/nn/_modules.pygemma/gm/nn/gemma4/_modules.pygemma/gm/nn/gemma3n/_modules.pygemma/research/t5gemma/modules.pyVerification
Confirmed all 8 occurrences replaced, zero remaining instances of
int(kg / self.num_kv_heads)in the codebase.Fixes #641