Draft
Conversation
Add bridge, provider, and VLM model wrapper for Google's Gemma 4
MoE architecture (gemma-4-26B-A4B).
Key components:
- gemma4_bridge.py: Weight mapping with fused router weights,
shared expert pre-norm fusion, and QKV/GatedMLP mappings
- gemma4_provider.py: Megatron-Core GPT provider with dual
local/global RoPE (proportional formula for global layers),
MoE expert routing, sliding window attention, and logit
soft-capping
- gemma4_vl_bridge.py: VLM bridge with vision tower + embedder
weight mappings (model.* prefix for raw safetensors keys)
- gemma4_vl_provider.py: VLM provider extending Gemma4 LLM
- modeling_gemma4_vl.py: VLM model combining HF vision tower
with Megatron language model
Validation (single-GPU, bf16):
- Text-only cosine similarity: 0.9998
- VLM cosine similarity: 0.9977 (causal-only, apples-to-apples)
- Same top-1 token prediction as HF
- Correct image understanding ("Red square" from test image)
Known limitations:
- Bidirectional attention for image tokens (mm_token_type_ids)
not yet implemented — uses causal-only masking
- Requires transformers >= 5.6.0.dev0 for Gemma4 support
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
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.
Summary
google/gemma-4-26B-A4B)New Files
gemma/gemma4_bridge.pygemma/gemma4_provider.pygemma_vl/gemma4_vl_bridge.pymodel.*prefixgemma_vl/gemma4_vl_provider.pygemma_vl/modeling_gemma4_vl.pyValidation Results (single-GPU, bf16)
Key Design Decisions
inv_freq = 1/(base^(arange/head_dim))withhead_dim=512(not the standarddim=128). Fixed viaGemma4RotaryEmbeddingoverride.mm_token_type_idsis provided, which accounts for the 0.9966→0.9977 cosine gap.vision_tower.forward()returnslast_hidden_state(already pooled + standardized), thenembed_visionprojects to language hidden dim — matches HF'sGemma4Model.get_image_features.Remaining Work
mm_token_type_ids-based attention mask to allow image tokens to attend to each other (improves VLM cosine from 0.9977 → matching HF bidirectional)tests/unit_tests/models/gemma4/transformers >= 5.6.0.dev0(Gemma4 not yet in stable release)Test plan
🤖 Generated with Claude Code