Skip to content

Fuse per-sequence AlltoAll into a unified one in GDN forward#4913

Open
xuantengh wants to merge 9 commits into
NVIDIA:mainfrom
xuantengh:xuantengh/gdn_a2a
Open

Fuse per-sequence AlltoAll into a unified one in GDN forward#4913
xuantengh wants to merge 9 commits into
NVIDIA:mainfrom
xuantengh:xuantengh/gdn_a2a

Conversation

@xuantengh
Copy link
Copy Markdown
Contributor

@xuantengh xuantengh commented May 21, 2026

What does this PR do ?

PR #2645 adds the THD support for GDN, but it requires per-sequence, per-section (qkv, alpha, beta, gate) AlltoAll converting activation between CP <-> HP. This PR fuses the per-sequence AlltoAll into a unified one, with local indexing mappings to reorder at the sequence dimension and feature dimension.

wandb link for running a Qwen3 Next proxy model with 16K sequence length and CP size = 4, the iteration time speedup is around 7% - 10%.

image

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 @NVIDIA/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.

@copy-pr-bot
Copy link
Copy Markdown

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

@xuantengh
Copy link
Copy Markdown
Contributor Author

/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

@xuantengh
Copy link
Copy Markdown
Contributor Author

/ok to test 8dcfd7d

@xuantengh
Copy link
Copy Markdown
Contributor Author

/ok to test 6d28256

@xuantengh
Copy link
Copy Markdown
Contributor Author

/claude review

Comment thread tests/unit_tests/ssm/test_gated_delta_net.py Outdated
@xuantengh
Copy link
Copy Markdown
Contributor Author

/claude review

Comment thread tests/unit_tests/ssm/test_gated_delta_net.py Outdated
Comment thread tests/unit_tests/ssm/test_gated_delta_net.py Outdated
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 core logic for fusing per-sequence AlltoAll into a single batched a2a + local permutations looks correct. The mamba_context_parallel.py cleanup is a clean replacement with existing utility functions. Test coverage in TestFusedThdAllToAll is solid — it validates both directions and a round-trip identity check across multiple cu_seqlens configurations and cp_sizes.

Only nit: unused import os and a commented-out os.environ.setdefault line in the test file (see inline comments).

@xuantengh xuantengh force-pushed the xuantengh/gdn_a2a branch from efb43a2 to de01425 Compare May 23, 2026 04:01
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Jun 1, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added Approved All necessary approvals have been made and removed Final Review PR is in the "final review" stage labels Jun 1, 2026
@xuantengh xuantengh enabled auto-merge June 2, 2026 01:40
@xuantengh xuantengh requested a review from a team as a code owner June 2, 2026 05:18
Comment thread megatron/core/ssm/gated_delta_net.py Outdated
Comment thread megatron/core/ssm/gated_delta_net.py
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.

return unpacked_x
def _build_thd_cp_a2a_perm(
cu_seqlens: torch.Tensor, cp_size: int, t_global: int
) -> Tuple[torch.Tensor, torch.Tensor]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: let's move toward new-style type specification using built-in tuple instead of typing.Tuple.


@lru_cache(maxsize=8)
def _build_head_perm_for_split_sections(
split_sections: Tuple[int], cp_size: int, device: torch.device
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same nit as before, but also, the type should be tuple[int, ...].

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

Labels

Approved All necessary approvals have been made complexity: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants