Add QK layernorm support for dot-product attention in MambaModel#4067
Merged
Phlip79 merged 21 commits intoNVIDIA:mainfrom Apr 17, 2026
Merged
Add QK layernorm support for dot-product attention in MambaModel#4067Phlip79 merged 21 commits intoNVIDIA:mainfrom
Phlip79 merged 21 commits intoNVIDIA:mainfrom
Conversation
Convert static mamba_stack_spec and mamba_inference_stack_spec into config-driven functions (get_mamba_stack_spec, get_mamba_inference_stack_spec) that read qk_layernorm and qk_l2_norm from TransformerConfig, matching GPTModel's approach. Backward-compatible constants are preserved. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
Author
|
/claude review |
Tests cover: default (no config), qk_layernorm=True, qk_l2_norm=True, inference spec, backward-compatible constant, and a full forward pass with qk_layernorm enabled. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
Author
|
/claude review |
Member
Author
|
/ok to test 367b8a8 |
Member
Author
|
/ok to test 1c46827 |
Member
Author
|
/ok to test b28a11f |
5 tasks
Contributor
|
I think this is exactly what we want to avoid; as far as I understand, we do not want to start to make the spec dynamic in code. :) |
Member
Author
|
/claude review |
Member
Author
|
/ok to test bdf2d12 |
Member
Author
|
/ok to test 488e448 |
Replace the default_norm_cls intermediate with explicit branches on qk_l2_norm / qk_layernorm, matching the file's ValueError convention and validating spec/config consistency when both flags are disabled. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…add-qk-norm # Conflicts: # megatron/core/transformer/attention.py
Member
Author
|
/ok to test 861fe3b |
jaredcasper
approved these changes
Apr 16, 2026
The strict else-branch in SelfAttention requires config and spec to agree when any QK norm is active. This test was relying on spec-only enablement, which is now rejected. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Member
Author
|
/ok to test e16a762 |
Covers the two new behaviors introduced by this PR: config.qk_layernorm falling back to TENorm when the spec leaves q/k_layernorm as None, and ValueError when the spec sets a concrete norm but both config flags are disabled. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Member
Author
|
/ok to test 236c50f |
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Member
Author
|
/ok to test c8a313d |
Victarry
approved these changes
Apr 17, 2026
TENorm is a factory class (__new__ returns te.pytorch.LayerNorm), so isinstance against TENorm itself is always False. Check against the returned class instead. Also add the symmetric qk_l2_norm config-only fallback test to close the coverage gap. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Member
Author
|
/ok to test 1ca9061 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/24588820395 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/24589587870 |
Victarry
added a commit
to yanring/Megatron-LM
that referenced
this pull request
Apr 20, 2026
* origin/main: (286 commits) Rename MambaModel/MambaStack to HybridModel/HybridStack (NVIDIA#4099) Fix Megatron initialization with extra_args_provider (NVIDIA#4327) Fix RL to once again work with --skip-train (NVIDIA#4249) Add activation logging and tokens per expert logging (NVIDIA#3842) Make param_index_map always use unpacked (full numel) offsets (NVIDIA#4328) FA4 Inference (NVIDIA#4186) Fix RL reward due to stop token (NVIDIA#4096) cp: Fix UT timeout (NVIDIA#4310) (NVIDIA#4373) feat(ckpt): add --async-ckpt-use-cpu-shm argument (NVIDIA#4355) Update copy-pr-bot.yaml [skip ci] Docs: improve docstrings and comments in example training loop (NVIDIA#4041) Add QK layernorm support for dot-product attention in MambaModel (NVIDIA#4067) Fix bug with non-partial rollouts (NVIDIA#3964) [docs] ci: use parent-relative json_url for version picker (NVIDIA#4367) Add tables and histogram for RL staleness (NVIDIA#4097) Port DeepSeek Sparse Attention to `MambaModel` (NVIDIA#3553) docs: bump versions1.json to 0.17.0 (latest) (NVIDIA#4360) Fix potential coredump issue that occurs when saving a checkpoint (NVIDIA#1871) ci(gb200): add 1-node mr-github functional test variants (NVIDIA#4334) fix: wait for async P2P send before deallocating output tensor (NVIDIA#4047) ... # Conflicts: # megatron/core/transformer/cuda_graphs.py
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
SelfAttention.__init__so models with static specs (e.g. MambaModel) can enable QK layernorm via--qk-layernormwithout modifying their specsTENormorL2Norm); spec overrides ifq_layernorm/k_layernormare explicitly setq_layernorm/k_layernorm, which take precedenceTest plan
q_layernormandk_layernormareNoneqk_layernorm=True— verify norms are created viaTENormqk_l2_norm=True— verify norms areL2Norminstancesqk_layernorm=Trueproduces correct output shapeCloses MCORE-20.