Skip to content

[2/2] Add TileLang fused DSA kernels support with THD and CP & Clean up - #5049

Merged
HollowMan6 merged 7 commits into
NVIDIA:mainfrom
HollowMan6:dsa_cp_thd_tilelang
Jul 24, 2026
Merged

HollowMan6 merged 7 commits into
NVIDIA:mainfrom
HollowMan6:dsa_cp_thd_tilelang

Conversation

@HollowMan6

@HollowMan6 HollowMan6 commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do ?

Closes #5703

This PR adds the TileLang implementation of the fused DSA operations.

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

Main changes

  • Adds TileLang kernels for DSA indexer forward and backward.
  • Adds TileLang kernels for absorbed sparse-MLA forward and backward.
  • Adds Python wrapper code under megatron/core/transformer/experimental_attention_variant/ops/.

Packed THD and CP behavior

The TileLang path uses the metadata and bounds produced by PR 1. When the current TileLang kernel path cannot safely handle a packed THD or CP case, DSA falls back to the unfused implementation instead of changing semantics. This keeps packed-sequence and context-parallel correctness owned by the common DSA path while allowing supported dense cases to use TileLang.

Contribution process

flowchart LR
    A[Pre-checks] --> B[PR Tests]
    subgraph Code Review/Approval
        C1[Expert Review] --> C2[Final Review]
    end
    B --> C1
    C2 --> D[Merge]
Loading

Pre-checks

  • I want this PR in a versioned release and have added the appropriate Milestone (e.g., Core 0.8)
  • 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

The following process is enforced via the CODEOWNERS file for changes into megatron/core. For changes outside of megatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.

For MRs into `main` branch

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!

(Step 1): Add PR label Expert Review

(Step 2): Collect the expert reviewers reviews

  1. Attach the Expert Review label when your PR is ready for review.
  2. GitHub auto-assigns expert reviewers based on your changes. They will get notified and pick up your PR soon.

⚠️ Only proceed to the next step once all reviewers have approved, merge-conflict are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

(Step 3): Final Review

  1. Add Final Review label
  2. GitHub auto-assigns final reviewers based on your changes. They will get notified and pick up your PR soon.

(Optional Step 4): Cherry-pick into release branch

If this PR also needs to be merged into core_r* release branches, after this PR has been merged, select Cherry-pick to open a new PR into the release branch.

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.

Merging your PR

Any member of core-adlr and core-nemo will be able to merge your PR.

@copy-pr-bot

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

@HollowMan6
HollowMan6 marked this pull request as ready for review June 2, 2026 04:32
Copilot AI review requested due to automatic review settings June 2, 2026 04:32
@HollowMan6
HollowMan6 requested review from a team as code owners June 2, 2026 04:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a TileLang-backed fused implementation for the experimental DSA attention variant (indexer + SparseMLA + optional streaming sparse-KL), along with a native PyTorch parity test.

Changes:

  • Introduces TileLang utility helpers (availability detection, no-op jit, and runtime guard).
  • Adds TileLang fused kernels for the sparse MLA forward/backward and indexer forward/backward, plus DSA hook glue.
  • Adds a unit test comparing TileLang-backed DSA against a native baseline for outputs and gradients.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/unit_tests/transformer/experimental_attention_variant/test_dsa_tilelang_native_parity.py Adds parity test for TileLang vs native DSA outputs/grads across configs.
megatron/core/transformer/experimental_attention_variant/ops/tilelang_utils.py Adds TileLang availability detection + no-op jit + explicit runtime requirement checks.
megatron/core/transformer/experimental_attention_variant/ops/tilelang_sparse_mla_fwd.py Adds TileLang sparse MLA forward kernel + caching + padding/bucketing wrapper.
megatron/core/transformer/experimental_attention_variant/ops/tilelang_sparse_mla_bwd.py Adds TileLang sparse MLA backward + preprocess/postprocess kernels + caching + padding/bucketing wrapper.
megatron/core/transformer/experimental_attention_variant/ops/tilelang_indexer_fwd.py Adds TileLang fused indexer forward kernel + logits cleaning + caching.
megatron/core/transformer/experimental_attention_variant/ops/tilelang_indexer_bwd.py Adds TileLang fused indexer backward kernel + caching + topk padding wrapper.
megatron/core/transformer/experimental_attention_variant/ops/tilelang_dsa.py Adds TileLang-backed DSA hooks (topk, SparseMLA, streaming sparse-KL) + scratch buffer cache.
megatron/core/transformer/experimental_attention_variant/ops/sparse_mla.py Wraps TileLang sparse MLA fwd/bwd in a torch.autograd.Function.
megatron/core/transformer/experimental_attention_variant/ops/indexer.py Wraps TileLang indexer fwd/bwd in an autograd Function and exposes lighting_indexer.
megatron/core/transformer/experimental_attention_variant/dsa_tilelang_kernels.py Exposes TileLang hook entrypoints for the DSA backend integration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread megatron/core/transformer/experimental_attention_variant/ops/tilelang_dsa.py Outdated
@HollowMan6
HollowMan6 requested a review from Copilot June 2, 2026 05:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 8 comments.

Comment thread megatron/core/transformer/experimental_attention_variant/ops/tilelang_dsa.py Outdated
@HollowMan6
HollowMan6 requested a review from Copilot June 2, 2026 05:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 14807dae90

ℹ️ 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".

Comment thread megatron/core/transformer/experimental_attention_variant/ops/tilelang_dsa.py Outdated
Comment thread megatron/core/transformer/experimental_attention_variant/ops/tilelang_dsa.py Outdated
@HollowMan6
HollowMan6 requested a review from Copilot June 2, 2026 05:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Comment thread megatron/core/transformer/experimental_attention_variant/ops/tilelang_dsa.py Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 953f210933

ℹ️ 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".

@HollowMan6
HollowMan6 requested a review from Copilot June 2, 2026 06:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Comment thread megatron/core/transformer/experimental_attention_variant/ops/tilelang_dsa.py Outdated
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

ℹ️ 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".

@HollowMan6

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ffe3a16357

ℹ️ 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".

Comment thread megatron/core/transformer/experimental_attention_variant/ops/tilelang_dsa.py Outdated
@kvareddy
kvareddy requested a review from Mellonta June 2, 2026 15:21

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b4804238de

ℹ️ 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".

Comment thread megatron/core/transformer/experimental_attention_variant/ops/tilelang_dsa.py Outdated
@HollowMan6
HollowMan6 added this pull request to the merge queue Jul 23, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

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

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 23, 2026
@HollowMan6
HollowMan6 enabled auto-merge July 23, 2026 17:49
@HollowMan6
HollowMan6 added this pull request to the merge queue Jul 23, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

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

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

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

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

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

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 23, 2026
@HollowMan6
HollowMan6 enabled auto-merge July 23, 2026 23:04
@HollowMan6
HollowMan6 added this pull request to the merge queue Jul 24, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

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

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

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

Merged via the queue into NVIDIA:main with commit 4f65717 Jul 24, 2026
89 checks passed
@HollowMan6
HollowMan6 deleted the dsa_cp_thd_tilelang branch July 24, 2026 23:39
shjwudp pushed a commit to shjwudp/Megatron-LM that referenced this pull request Jul 30, 2026
…up (NVIDIA#5049)

Signed-off-by: Hollow Man <hollowman@opensuse.org>
terminator123 pushed a commit to 021ai/Megatron-LM that referenced this pull request Aug 3, 2026
…up (NVIDIA#5049)

Signed-off-by: Hollow Man <hollowman@opensuse.org>
buptzyb pushed a commit to buptzyb/Megatron-LM that referenced this pull request Aug 4, 2026
…up (NVIDIA#5049)

Signed-off-by: Hollow Man <hollowman@opensuse.org>
(cherry picked from commit 4f65717)
Signed-off-by: Robin Zhang <robinz@nvidia.com>
svcnvidia-nemo-ci pushed a commit to dimapihtar/Megatron-LM that referenced this pull request Aug 4, 2026
…up (NVIDIA#5049)

Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Dmytro Pykhtar <dpykhtar@nvidia.com>

This branch was previously deployed

2 inactive deployments
public — ec53a7e2 Deployed Jul 24, 2026 by copy-pr-bot[bot] via finalize / publish-docs #4489
test — ec53a7e2 Deployed Jul 24, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #17532
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: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Question about missing DSA fused backend modules: dsa_tilelang_kernels and dsa_cudnn_kernels

7 participants