feat(gemma4): add Gemma4 dense and MoE support#2135
Open
EazyReal wants to merge 7 commits into
Open
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.
Summary
Adds Gemma4 model support for both dense and MoE variants in Slime:
google/gemma-4-31B-itandgoogle/gemma-4-26B-A4B-itThe implementation covers Gemma4-specific attention and conversion behavior, including dual RoPE, global/sliding attention, K=V global attention handling, layer scalar broadcast, router/expert parameters, QKV packing, and CP indexing.
Relation to #1855
#1855 is an earlier Gemma4 integration branch. This PR keeps the same upstream goal but uses a refreshed, focused branch for review:
Dependency
The large-model MoE proof also exercised the colocated raw weight sync edge case fixed in #2134. This PR intentionally keeps the Gemma4 model support separate from that bugfix; if maintainers run the 26B-A4B colocated recipe before #2134 lands, they should apply that fix as well.
Validation
Local validation on this Gemma4-only branch:
uv run --with pytest --with torch --with transformers --with safetensors --with numpy python -m pytest tests/gemma4 tests/utils/test_loss_mask_type_gemma4.py -q56 passed, 14 skippeduv run --with ruff ruff check slime_plugins/models/gemma4.py slime_plugins/models/gemma4_provider.py slime_plugins/mbridge/gemma4.py slime/backends/megatron_utils/megatron_to_hf/gemma4.py slime/utils/mask_utils.py tests/gemma4 tests/utils/test_loss_mask_type_gemma4.pyuv run --with black black --check slime_plugins/models/gemma4.py slime_plugins/models/gemma4_provider.py slime_plugins/mbridge/gemma4.py slime/backends/megatron_utils/megatron_to_hf/gemma4.py slime/utils/mask_utils.py tests/gemma4 tests/utils/test_loss_mask_type_gemma4.pybash -n scripts/run-gemma4-31B-gsm8k.sh scripts/run-gemma4-26B-A4B-gsm8k.shgit diff --checkStacked validation with #2134 included:
uv run --with pytest --with torch --with transformers --with safetensors --with numpy python -m pytest tests/gemma4 tests/utils/test_loss_mask_type_gemma4.py tests/test_empty_colocated_weight_bucket.py -q58 passed, 14 skippedSlime-native GSM8K proof runs:
google/gemma-4-31B-it, Megatron TP2 PP4 CP1, SGLang TP8: https://wandb.ai/augustinevmax-vmax/slime-gemma4-official-proof/runs/51l66o7ogoogle/gemma-4-26B-A4B-it, Megatron TP2 PP2 EP2 CP1, SGLang TP8: https://wandb.ai/augustinevmax-vmax/slime-gemma4-official-proof/runs/eeere4c3