fix(model): honor config epsilon in Post-LN row-parallel linear - #5712
Open
hokuyama0106 wants to merge 1 commit into
Open
fix(model): honor config epsilon in Post-LN row-parallel linear#5712hokuyama0106 wants to merge 1 commit into
hokuyama0106 wants to merge 1 commit into
Conversation
TENorm defaults to eps=1e-5 and never reads config.layernorm_epsilon, so the Post-LN that TERowParallelLinearLayerNorm attaches to row-parallel projection outputs ignored the model's RMSNorm epsilon. Gemma2, Gemma3 and Gemma4 set layernorm_epsilon=1e-6 (from HF rms_norm_eps), which every other norm in the layer honors, so their post_attention_layernorm and post_feedforward_layernorm ran at 1e-5 and diverged from HuggingFace. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Hiroki Okuyama <hokuyama@preferred.jp>
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.
What does this PR do ?
Pass
config.layernorm_epsilonto the Post-LN built insideTERowParallelLinearLayerNorm, so that Gemma2 / Gemma3 / Gemma4's post-attention and post-feedforward RMSNorms use the model's epsilon (1e-6) instead ofTENorm's1e-5default.TENorm.__new__(cls, config, hidden_size, eps=1e-5, has_residual=False)does not readconfig.layernorm_epsilon; the epsilon has to be passed explicitly, as MCore itself does for the final layernorm (transformer_block.py:eps=self.config.layernorm_epsilon) and as this repo already does for the Gemma3-VL projector norm (modeling_gemma3_vl.py:TENorm(config, config.input_size, eps=config.layernorm_epsilon)).Because
TERowParallelLinearLayerNormomitted it, the Post-LN modules were the only norms in the layer running at1e-5:input_layernorm(viaTELayerNormColumnParallelLinear)config.layernorm_epsilonpre_feedforward_layernorm(viaTELayerNormColumnParallelLinear)config.layernorm_epsilonfinal_layernorm(via MCoreTransformerBlock)config.layernorm_epsilonpost_attention_layernorm(linear_proj)1e-5config.layernorm_epsilonpost_feedforward_layernorm(linear_fc2)1e-5config.layernorm_epsilonAffected models (all users of
TERowParallelLinearLayerNorm):gemma2_provider.py:linear_proj,linear_fc2) —layernorm_epsilon = 1e-6→ behavior changes, now matches HFrms_norm_epsgemma3_provider.py:linear_proj,linear_fc2) —layernorm_epsilon = 1e-6→ behavior changes, now matches HFrms_norm_epsmodeling_gemma4.py:linear_projingemma4_block_spec) —layernorm_epsilon = 1e-6→ behavior changes, now matches HFrms_norm_epsrms_norm_eps = 1e-5, i.e. equal to the old hardcoded default, so no behavior changeThe forward-pass difference is small (
sqrt(mean(x^2) + eps)witheps1e-5 vs 1e-6), but it applies to every Post-LN in every layer and shows up as a systematic HF <-> Megatron parity gap for the Gemma family.Changelog
src/megatron/bridge/models/common/te_layers.py: build the Post-LN asTENorm(config, output_size, eps=config.layernorm_epsilon)tests/unit_tests/models/common/test_te_layers.py: addtest_post_layernorm_uses_config_epsilon, asserting the epsilon from the config reachesTENormGitHub Actions CI
Needs an NVIDIA developer to approve/trigger CI for this external contribution.
Before your PR is "Ready for review"
Pre checks:
Additional Information
TERowParallelLinearLayerNormingemma3_provider.py(Gemma3 Provider & Bridge #867) and moved unchanged intomodels/common/te_layers.pyin feat: Add EXAONE 4.0 model bridge (LG AI Research) #2532.