Skip to content

fix(model): honor config epsilon in Post-LN row-parallel linear - #5712

Open
hokuyama0106 wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
hokuyama0106:fix/post-layernorm-eps
Open

fix(model): honor config epsilon in Post-LN row-parallel linear#5712
hokuyama0106 wants to merge 1 commit into
NVIDIA-NeMo:mainfrom
hokuyama0106:fix/post-layernorm-eps

Conversation

@hokuyama0106

Copy link
Copy Markdown

What does this PR do ?

Pass config.layernorm_epsilon to the Post-LN built inside TERowParallelLinearLayerNorm, so that Gemma2 / Gemma3 / Gemma4's post-attention and post-feedforward RMSNorms use the model's epsilon (1e-6) instead of TENorm's 1e-5 default.

TENorm.__new__(cls, config, hidden_size, eps=1e-5, has_residual=False) does not read config.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 TERowParallelLinearLayerNorm omitted it, the Post-LN modules were the only norms in the layer running at 1e-5:

Norm epsilon before epsilon after
input_layernorm (via TELayerNormColumnParallelLinear) config.layernorm_epsilon unchanged
pre_feedforward_layernorm (via TELayerNormColumnParallelLinear) config.layernorm_epsilon unchanged
final_layernorm (via MCore TransformerBlock) config.layernorm_epsilon unchanged
post_attention_layernorm (linear_proj) 1e-5 config.layernorm_epsilon
post_feedforward_layernorm (linear_fc2) 1e-5 config.layernorm_epsilon

Affected models (all users of TERowParallelLinearLayerNorm):

  • Gemma2 (gemma2_provider.py: linear_proj, linear_fc2) — layernorm_epsilon = 1e-6 → behavior changes, now matches HF rms_norm_eps
  • Gemma3 (gemma3_provider.py: linear_proj, linear_fc2) — layernorm_epsilon = 1e-6 → behavior changes, now matches HF rms_norm_eps
  • Gemma4 (modeling_gemma4.py: linear_proj in gemma4_block_spec) — layernorm_epsilon = 1e-6 → behavior changes, now matches HF rms_norm_eps
  • EXAONE 4.0 / 4.5rms_norm_eps = 1e-5, i.e. equal to the old hardcoded default, so no behavior change

The forward-pass difference is small (sqrt(mean(x^2) + eps) with eps 1e-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 as TENorm(config, output_size, eps=config.layernorm_epsilon)
  • tests/unit_tests/models/common/test_te_layers.py: add test_post_layernorm_uses_config_epsilon, asserting the epsilon from the config reaches TENorm

GitHub Actions CI

Needs an NVIDIA developer to approve/trigger CI for this external contribution.

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you add or update any necessary documentation?
  • Does the PR affect components that are optional to install? (Ex: Numba, Pynini, Apex etc)
    • Reviewer: Does the PR have correct import guards for all optional libraries?

Additional Information

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>
@copy-pr-bot

copy-pr-bot Bot commented Aug 20, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33 yaoyu-33 added area:model Model implementations and HF bridge logic bug Something isn't working needs-more-tests Requires additional L0 and L1 test coverage before merge needs-review PR is ready for code review and waiting on a reviewer labels Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:model Model implementations and HF bridge logic bug Something isn't working community-request needs-more-tests Requires additional L0 and L1 test coverage before merge needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants