fix(gemma3): use GeGLU activation instead of SwiGLU#1825
Open
leofan-lab wants to merge 2 commits intoTHUDM:mainfrom
Open
fix(gemma3): use GeGLU activation instead of SwiGLU#1825leofan-lab wants to merge 2 commits intoTHUDM:mainfrom
leofan-lab wants to merge 2 commits intoTHUDM:mainfrom
Conversation
The upstream mbridge base config hardcodes F.silu (SwiGLU) for all models, but the entire Gemma family (1/2/3/4) uses gelu_pytorch_tanh (GeGLU). This causes incorrect MLP outputs when running Gemma3 through Megatron's native MLP path, as the activation function doesn't match what the model was pretrained with. Evidence from Gemma4 parity testing: - Before fix: 19.4% loss gap vs HuggingFace, cos=0.81 per-layer MLP output - After fix: 0.29% loss gap, cos=0.9999 per-layer, all 60 layers match Sources confirming GeGLU for Gemma: - Google blog: developers.googleblog.com/en/gemma-explained-new-in-gemma-2/ - HF config: hidden_activation='gelu_pytorch_tanh' for Gemma 1/2/3/4 - New megatron-bridge (v0.3.0) already uses fast_gelu for Gemma3
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
The upstream mbridge base config (
_build_base_config) hardcodesactivation_func=F.siluwithgated_linear_unit=True, which gives SwiGLU. However, the entire Gemma family (1/2/3/4) uses GeGLU (gelu_pytorch_tanh+ GLU).This causes incorrect MLP outputs when running Gemma3 through Megatron's native MLP path.
Evidence
Discovered during Gemma4 parity testing against HuggingFace:
Sources confirming GeGLU for Gemma:
hidden_activation='gelu_pytorch_tanh'for Gemma 1/2/3/4megatron-bridgev0.3.0 (the successor to mbridge) already usesfast_gelufor Gemma3Fix
Adds
slime_plugins/mbridge/gemma3.pythat overrides_build_configto usefunctools.partial(F.gelu, approximate="tanh")instead ofF.silu.