Add QK layernorm support for dot-product attention in MambaModel - #4067
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>
|
/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>
|
/claude review |
|
/ok to test 367b8a8 |
|
/ok to test 1c46827 |
|
/ok to test b28a11f |
|
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. :) |
|
/claude review |
|
/ok to test bdf2d12 |
|
/ok to test 488e448 |
|
/ok to test 861fe3b |
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>
|
/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>
|
/ok to test 236c50f |
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
/ok to test c8a313d |
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>
|
/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 |
* 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
…DIA#4067) Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Yuzhong Wang <yuzhongw@nvidia.com>
…DIA#4067) Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Yuzhong Wang <yuzhongw@nvidia.com> Signed-off-by: yhgalaxy <yhgalaxy@outlook.com>
…DIA#4067) Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Yuzhong Wang <yuzhongw@nvidia.com> Signed-off-by: Jon Barker <jbarker@aws-cmh-slurm-1-vscode-02.cm.cluster>
…DIA#4067) Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Yuzhong Wang <yuzhongw@nvidia.com>
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.