Fix jaxtyping shape mismatch for multimodal inputs in Gemma3nTransformer#624
Open
YADAV1825 wants to merge 1 commit into
Open
Fix jaxtyping shape mismatch for multimodal inputs in Gemma3nTransformer#624YADAV1825 wants to merge 1 commit into
YADAV1825 wants to merge 1 commit into
Conversation
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.
Resolves #620
Bug Description:
When running Gemma3n_E2B with multimodal inputs, the gm.text.Sampler properly expands the sequence length to account for the generated image placeholder tokens. However, the jaxtyping annotations in Gemma3nTransformer.call and _encode_and_get_inputs strictly enforced L and L_no_mm for the positions and attention_mask arguments. This caused a TypeCheckError during JAX tracing because the expanded tensors (e.g., length 512) did not match the raw token length (e.g., length 253).
The Fix:
Signature Update: Changed the type hints in call and _encode_and_get_inputs to use L_with_mm for positions and attention_mask to safely permit the expanded shapes generated by the sampler, while maintaining backward compatibility for text-only inputs.
Defensive Fallback: Added internal shape-checking inside _encode_and_get_inputs. If a user bypasses the sampler and manually passes raw unexpanded positions or attention_mask alongside an image, the function catches the shape mismatch and dynamically rebuilds them using _pos_utils.build_positions_from_mask to prevent downstream execution failures.
Testing:
Verified the fix locally by bypassing checkpoint auth and directly passing a dummy image through the sampler and model initialization to test the tensor shapes end-to-end.
Issue Reproduction:
Resolved Execution: