[main] feat(attention): add use_head_wise_attn_gate for Step-3.5-Flash#4848
Closed
shifangx wants to merge 1 commit into
Closed
[main] feat(attention): add use_head_wise_attn_gate for Step-3.5-Flash#4848shifangx wants to merge 1 commit into
shifangx wants to merge 1 commit into
Conversation
Adds an opt-in Step-3.5-Flash-style g_proj: num_attention_heads scalar gates fused as the trailing rows of linear_qkv, applied as a sigmoid multiplier per head before linear_proj. Mutually exclusive with the existing attention_output_gate (full head_dim gate). Implementation: - TransformerConfig: head_wise_attn_gate flag plus two init knobs (head_wise_attn_gate_init_weight_scale=0.1, head_wise_attn_gate_init_bias=2.0) chosen so sigmoid(gate)~=0.88 at init (near-identity, no half-scaling regression). - SelfAttention.get_query_key_value_tensors peels the trailing per-rank gate scalars off mixed_qkv before any reshape/AllGather; correctness rests on (a) ColumnParallelLinear's contiguous axis-0 split, (b) num_attention_heads % tp == 0, (c) global [QKV; Gate] layout preserved by a ShardedTensorFactory on save/load. - SelfAttention.sharded_state_dict installs that factory on linear_qkv.weight (and .bias) so dist-ckpt round-trips across TP reshape correctly chunk into [QKV_part, gate_part] and concat back. - Init-time gate-row surgery in SelfAttention.__init__: scales gate weight rows and fills gate bias rows so the gate starts near 1. - Config validation in __post_init__: rejects combination with attention_output_gate, num_attention_heads % tp != 0, num_query_groups < tp, and (under fp8/fp4) per-partition linear_qkv_out_dim misaligned to 16/32. Tests: - tests/unit_tests/transformer/test_head_wise_attn_gate.py covers config validation, init magnitudes, forward shape, TP=2 saturated-gate equivalence, and zero-gate halving. - tests/unit_tests/dist_checkpointing/test_head_wise_attn_gate_resharding.py asserts logits equality across TP=1<->TP=2 dist-ckpt reshard for both MHA and GQA, with and without QKV bias. Unverified paths (follow-ups): context parallelism, fp8 numerical parity, explicit backward-grad-to-gate, packed_seq_params.qkv_format == 'thd'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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. |
5 tasks
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 ?
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.
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.