Skip to content

Add QK layernorm support for dot-product attention in MambaModel#4067

Open
Phlip79 wants to merge 9 commits intoNVIDIA:mainfrom
Phlip79:philip/add-qk-norm
Open

Add QK layernorm support for dot-product attention in MambaModel#4067
Phlip79 wants to merge 9 commits intoNVIDIA:mainfrom
Phlip79:philip/add-qk-norm

Conversation

@Phlip79
Copy link
Copy Markdown
Member

@Phlip79 Phlip79 commented Mar 31, 2026

What does this PR do ?

Converts 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.

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact the @mcore-oncall.

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

For MRs into `dev` branch The proposed review process for `dev` branch is under active discussion.

MRs are mergable after one approval by either eharper@nvidia.com or zijiey@nvidia.com.

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

copy-pr-bot bot commented Mar 31, 2026

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.

@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/claude review

Copy link
Copy Markdown
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Light review — the refactor from static specs to config-driven functions looks correct and consistent with the GPT layer specs pattern. One gap: the new QK-norm code paths have no test coverage (see inline comment).

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>
@Phlip79 Phlip79 marked this pull request as ready for review March 31, 2026 00:20
@Phlip79 Phlip79 requested review from a team as code owners March 31, 2026 00:20
@svcnvidia-nemo-ci svcnvidia-nemo-ci requested a review from a team March 31, 2026 00:20
@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/claude review

Copy link
Copy Markdown
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/ok to test 367b8a8

@svcnvidia-nemo-ci svcnvidia-nemo-ci added this to the Core 0.16 milestone Mar 31, 2026
@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/ok to test 1c46827

@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/ok to test b28a11f

@janEbert
Copy link
Copy Markdown
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. :)
Isn't this solvable by dynamically passing the arguments in the MambaStack or MambaModel constructors?

@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/claude review

Copy link
Copy Markdown
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/ok to test bdf2d12

@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/ok to test 488e448

@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/ok to test e9c20a4

TENorm uses __new__ to return a TE LayerNorm/RMSNorm instance, not a
TENorm instance, so isinstance(x, TENorm) always fails. Check for
not None instead.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Phlip79
Copy link
Copy Markdown
Member Author

Phlip79 commented Mar 31, 2026

/ok to test d16170f

Copy link
Copy Markdown
Contributor

@yuzhongw-nvidia yuzhongw-nvidia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants