[dev] feat(attention): add use_head_wise_attn_gate for Step-3.5-Flash#4841
Merged
Conversation
Contributor
|
This PR has been automatically converted to draft because all PRs must start as drafts. When you are ready for review, click Ready for Review to begin the review process. This will:
See the contribution guide for more details. |
Adds the Step-3.5-Flash per-head scalar attention gate (a.k.a. g_proj). When use_head_wise_attn_gate is True, the gate is fused into linear_qkv as the trailing num_attention_heads rows: each rank slices off its num_attention_heads_per_partition gate scalars, runs them through sigmoid, and uses them to scale each attention head's output before linear_proj. Distinct from attention_output_gate, which fuses a full head_dim gate. Includes unit tests covering linear_qkv sizing, gate buffer lifecycle, end-to-end forward shapes, the zero-gate -> 0.5x output identity, and low-level tensor-math correctness of the reshape and sigmoid path.
77c913e to
28fa192
Compare
dd94d20 to
22255f2
Compare
Contributor
Author
|
/ok to test 22255f2 |
c3a23db to
b89aeda
Compare
Contributor
Author
|
/ok to test b89aeda |
Contributor
Author
|
/ok to test 2bfcbf4 |
Victarry
approved these changes
May 19, 2026
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/26093684837 |
2bfcbf4 to
16ff6b1
Compare
Contributor
Author
|
/ok to test 45eba11 |
45eba11 to
05f336c
Compare
Contributor
Author
|
/ok to test 05f336c |
Contributor
Author
|
/ok to test f01a5d4 |
…stead of inheriting QKV's init.
f01a5d4 to
522ae61
Compare
Contributor
Author
|
/ok test 522ae61 |
Contributor
Author
|
/ok to test 684d7c5 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/26099801279 |
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 ?
(a.k.a. g_proj). The gate is fused into linear_qkv as the trailing
num_attention_heads rows; in the forward pass each rank slices off its
num_attention_heads_per_partition gate scalars, applies sigmoid, and
scales each attention head's output before linear_proj.
into linear_qkv. The per-head gate adds only num_attention_heads extra
output rows on linear_qkv (vs. num_attention_heads * head_dim), so the
parameter and FLOP overhead is negligible.
num_attention_heads be divisible by the TP world size so the QKV and
gate sub-ranges stay aligned per rank.
The new feature defaults to False and has no effect on existing models.
for main branch: #4848
Related: per-layer RoPE for Step-3.5-Flash is tracked separately in
#4473.
Issue tracking
For PRs from open-source community contributors:
Linked issue:
Contribution process
Pre-checks
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"
.github/CODEOWNERS.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, theFinal Reviewlabel 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
Approvedlabel 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.comorzijiey@nvidia.com.