Skip to content

[dev] feat(attention): add use_head_wise_attn_gate for Step-3.5-Flash#4841

Merged
shifangx merged 19 commits into
NVIDIA:devfrom
shifangx:shifang/head_wise_attn_gate
May 19, 2026
Merged

[dev] feat(attention): add use_head_wise_attn_gate for Step-3.5-Flash#4841
shifangx merged 19 commits into
NVIDIA:devfrom
shifangx:shifang/head_wise_attn_gate

Conversation

@shifangx

@shifangx shifangx commented May 18, 2026

Copy link
Copy Markdown
Contributor

What does this PR do ?

  • use_head_wise_attn_gate: bool -- enable a per-head scalar output gate
    (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.
  • Distinct from attention_output_gate, which fuses a full head_dim gate
    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.
  • Under tensor parallel, this requires that both num_query_groups and
    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.

⚠️ 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.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

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.

@shifangx shifangx requested review from a team as code owners May 18, 2026 02:36
@copy-pr-bot

copy-pr-bot Bot commented May 18, 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.

@svcnvidia-nemo-ci svcnvidia-nemo-ci marked this pull request as draft May 18, 2026 02:36
@github-actions

Copy link
Copy Markdown
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:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@shifangx shifangx changed the base branch from main to dev May 18, 2026 02:36
@shifangx shifangx changed the title [dev] feat(attention): support head wise attn gate for Step-3.5-Flash [dev] feat(attention): add use_head_wise_attn_gate for Step-3.5-Flash May 18, 2026
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.
@shifangx shifangx force-pushed the shifang/head_wise_attn_gate branch from 77c913e to 28fa192 Compare May 18, 2026 02:58
@shifangx shifangx removed request for a team May 18, 2026 02:59
@shifangx shifangx force-pushed the shifang/head_wise_attn_gate branch from dd94d20 to 22255f2 Compare May 18, 2026 22:29
@shifangx

Copy link
Copy Markdown
Contributor Author

/ok to test 22255f2

@shifangx shifangx force-pushed the shifang/head_wise_attn_gate branch from c3a23db to b89aeda Compare May 19, 2026 04:10
@shifangx

Copy link
Copy Markdown
Contributor Author

/ok to test b89aeda

@shifangx

Copy link
Copy Markdown
Contributor Author

/ok to test 2bfcbf4

@shifangx shifangx added this pull request to the merge queue May 19, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/26093684837

@Victarry Victarry removed this pull request from the merge queue due to a manual request May 19, 2026
@shifangx shifangx force-pushed the shifang/head_wise_attn_gate branch from 2bfcbf4 to 16ff6b1 Compare May 19, 2026 11:59
@shifangx

Copy link
Copy Markdown
Contributor Author

/ok to test 45eba11

@shifangx shifangx enabled auto-merge May 19, 2026 12:00
@shifangx shifangx force-pushed the shifang/head_wise_attn_gate branch from 45eba11 to 05f336c Compare May 19, 2026 12:02
@shifangx

Copy link
Copy Markdown
Contributor Author

/ok to test 05f336c

@shifangx

Copy link
Copy Markdown
Contributor Author

/ok to test f01a5d4

@shifangx shifangx force-pushed the shifang/head_wise_attn_gate branch from f01a5d4 to 522ae61 Compare May 19, 2026 12:06
@shifangx

Copy link
Copy Markdown
Contributor Author

/ok test 522ae61

@shifangx

Copy link
Copy Markdown
Contributor Author

/ok to test 684d7c5

@shifangx shifangx added this pull request to the merge queue May 19, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/26099801279

Merged via the queue into NVIDIA:dev with commit bd5c98f May 19, 2026
65 of 66 checks passed
@shifangx shifangx deleted the shifang/head_wise_attn_gate branch May 19, 2026 14:00
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.

3 participants