[2/2] Wiring cuDNN fused DSA kernels support with THD, CP and IndexShare (GLM5.2) - #5099
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds optional fused cuDNN/FlashMLA kernels for DSA attention, including config-level dependency validation and a native-vs-fused parity test to validate correctness.
Changes:
- Add fused DSA kernel wrappers (FlashMLA forward + cuDNN DSA indexer/attention backward + KL-loss paths).
- Add
apply_dsa_kernel_fusionconfig flag with runtime dependency/SM checks. - Add GPU parity tests comparing native PyTorch DSA vs the module implementation across backends and loss variants.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tests/unit_tests/transformer/experimental_attention_variant/test_dsa_cudnn_native_parity.py | Adds parity test and native reference implementation for DSA. |
| megatron/core/transformer/transformer_config.py | Adds apply_dsa_kernel_fusion flag and validates fused-kernel dependencies in config init. |
| megatron/core/transformer/experimental_attention_variant/dsa_cudnn_kernels.py | Introduces fused DSA kernels wrapper and custom autograd for fused indexer+sparse-attn+loss. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
25166f0 to
8327e85
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8327e85bfa
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
8327e85 to
d3592e0
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d3592e090f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
40d58ac to
c08880b
Compare
91bada7 to
253fbd2
Compare
60e98be to
196a151
Compare
Signed-off-by: Hollow Man <hollowman@opensuse.org>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: db277983ba
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 35efdad661
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Signed-off-by: Hollow Man <hollowman@opensuse.org>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 318fc1caf3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Signed-off-by: Hollow Man <hollowman@opensuse.org>
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Signed-off-by: Hollow Man <hollowman@opensuse.org>
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28962390611 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28965756617 |
Signed-off-by: Hollow Man <hollowman@opensuse.org>
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28978107273 |
|
@HollowMan6 Is there a cuDNN-accelerated operator implementation available for the DSA indexer when CP > 1? |
|
@xiaoxi-wangfj The
Would you let me know more about your use case? |
@HollowMan6 The relevant configuration is: --context-parallel-size 2 In DSAttention.forward, the inputs are: attn_mask_type=AttnMaskType.causal For SEQ_LEN=8192 and CP=2, one rank gets bounds like: varlen_starts = [0, 0, 0, ...] Inside _indexer_topk_bshd, the relevant flags are: return_scores = False So this is not the dense-loss case, and it is not the packed-THD CP path. It falls into the generic non-packed varlen CP path: dsa_kernels.run_fused_qk_topk_with_loss I also noticed that cuDNN frontend 1.26.0-rc adds q_causal_offsets to the DSA indexer forward API. This seems to match my case, Is q_causal_offsets intended to support this non-packed CP causal case and avoid the _compute_indexer_scores_chunk_with_global_rows fallback? If so, is there a plan to wire this API into the Megatron-LM DSA path? |
@HollowMan6 |
|
Thank you for your info, if you are not using packing, then it will fall back to torch implementation with BSHD and CP>1. I didn't really target BSHD involving CP in this PR, as this PR mainly targets THD-packing (as suggested in the PR title). Also, since typically we can have highly variance length between different sequence, then THD should be more memory friendly and compute efficient, since you are already trying to use CP, I would recommend THD as well to maximize the memory saving here. For the |
What does this PR do ?
This PR adds the wiring code for cuDNN fused DSA operations and wires it into the backend-neutral hooks introduced by PR 1. It contains the cuDNN-specific wiring code only
Issue tracking
For PRs from open-source community contributors:
Linked issue:
Contribution process
Pre-checks
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"
.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.