-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[2/2] Add TileLang fused DSA kernels support with THD and CP & Clean up #5049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff94b8e
Add TileLang fused DSA kernels
HollowMan6 7cecf32
Address review
HollowMan6 cab7852
clean up and consolidate
HollowMan6 4797cda
minor
HollowMan6 a18d138
Merge branch 'main' into dsa_cp_thd_tilelang
HollowMan6 603026b
Merge branch 'main' into dsa_cp_thd_tilelang
HollowMan6 ec53a7e
Merge branch 'main' into dsa_cp_thd_tilelang
HollowMan6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
142 changes: 142 additions & 0 deletions
142
megatron/core/transformer/experimental_attention_variant/dsa_tilelang_kernels.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
|
|
||
| """TileLang backend hooks for optional fused DeepSeek sparse attention kernels.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Optional, Tuple | ||
|
|
||
| import torch | ||
|
|
||
| from megatron.core.process_groups_config import ProcessGroupCollection | ||
| from megatron.core.transformer.experimental_attention_variant.ops import tilelang_dsa | ||
|
|
||
| if TYPE_CHECKING: | ||
| from megatron.core.packed_seq_params import PackedSeqParams | ||
| from megatron.core.transformer.transformer_config import TransformerConfig | ||
|
|
||
|
|
||
| def run_fused_qk_topk( | ||
| q: torch.Tensor, | ||
| k: torch.Tensor, | ||
| weights: torch.Tensor, | ||
| index_topk: int, | ||
| starts: torch.Tensor, | ||
| ends: torch.Tensor, | ||
| block_size: int, | ||
| use_relu: bool = True, | ||
| use_local_indexer_varlen: bool = False, | ||
| single_packed_thd_sequence: bool = False, | ||
| local_packed_cp_rank: int = 0, | ||
| local_packed_cp_query_start: int = 0, | ||
| local_packed_cp_query_len: Optional[int] = None, | ||
| packed_seq_params: Optional[PackedSeqParams] = None, | ||
| cp_size: int = 1, | ||
| ) -> Optional[Tuple[torch.Tensor, Optional[torch.Tensor]]]: | ||
| """Adapt TileLang's indices-only result to the shared backend hook contract.""" | ||
| topk_indices = tilelang_dsa.run_fused_qk_topk( | ||
| q, | ||
| k, | ||
| weights, | ||
| index_topk, | ||
| starts, | ||
| ends, | ||
| block_size, | ||
| use_relu, | ||
| use_local_indexer_varlen=use_local_indexer_varlen, | ||
| single_packed_thd_sequence=single_packed_thd_sequence, | ||
| local_packed_cp_rank=local_packed_cp_rank, | ||
| local_packed_cp_query_start=local_packed_cp_query_start, | ||
| local_packed_cp_query_len=local_packed_cp_query_len, | ||
| packed_seq_params=packed_seq_params, | ||
| cp_size=cp_size, | ||
| ) | ||
| if topk_indices is None: | ||
| return None | ||
| return topk_indices, None | ||
|
HollowMan6 marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def run_fused_qk_topk_with_loss( | ||
| q: torch.Tensor, | ||
| k: torch.Tensor, | ||
| weights: torch.Tensor, | ||
| index_topk: int, | ||
| starts: torch.Tensor, | ||
| ends: torch.Tensor, | ||
| block_size: int, | ||
| query: torch.Tensor, | ||
| key: torch.Tensor, | ||
| softmax_scale: float, | ||
| loss_coeff: float, | ||
| pg_collection: ProcessGroupCollection, | ||
| query_valid_rows: Optional[torch.Tensor] = None, | ||
| calculate_per_token_loss: bool = False, | ||
| use_relu: bool = True, | ||
| config: Optional["TransformerConfig"] = None, | ||
| use_local_indexer_varlen: bool = False, | ||
| single_packed_thd_sequence: bool = False, | ||
| local_packed_cp_rank: int = 0, | ||
| local_packed_cp_query_start: int = 0, | ||
| local_packed_cp_query_len: Optional[int] = None, | ||
| packed_seq_params: Optional[PackedSeqParams] = None, | ||
| cp_size: int = 1, | ||
| ) -> Optional[Tuple[torch.Tensor, Optional[torch.Tensor], torch.Tensor]]: | ||
| """Run fused TileLang indexer and sparse indexer loss.""" | ||
| del config | ||
| result = tilelang_dsa.run_fused_qk_topk_with_loss( | ||
| q=q, | ||
| k=k, | ||
| weights=weights, | ||
| index_topk=index_topk, | ||
| starts=starts, | ||
| ends=ends, | ||
| block_size=block_size, | ||
| query=query, | ||
| key=key, | ||
| softmax_scale=softmax_scale, | ||
| loss_coeff=loss_coeff, | ||
| pg_collection=pg_collection, | ||
| query_valid_rows=query_valid_rows, | ||
| calculate_per_token_loss=calculate_per_token_loss, | ||
| use_relu=use_relu, | ||
| use_local_indexer_varlen=use_local_indexer_varlen, | ||
| single_packed_thd_sequence=single_packed_thd_sequence, | ||
| local_packed_cp_rank=local_packed_cp_rank, | ||
| local_packed_cp_query_start=local_packed_cp_query_start, | ||
| local_packed_cp_query_len=local_packed_cp_query_len, | ||
| packed_seq_params=packed_seq_params, | ||
| cp_size=cp_size, | ||
| ) | ||
| if result is None: | ||
| return None | ||
| topk_indices, indexer_loss = result | ||
| return topk_indices, None, indexer_loss | ||
|
|
||
|
|
||
| def run_fused_absorbed_sparse_attention( | ||
| query: torch.Tensor, | ||
| key: torch.Tensor, | ||
| topk_indices: torch.Tensor, | ||
| softmax_scale: float, | ||
| v_channels: int, | ||
| topk_length: Optional[torch.Tensor] = None, | ||
| ) -> Optional[torch.Tensor]: | ||
| """Run fused TileLang SparseMLA for absorbed DSA sparse attention.""" | ||
| if topk_length is not None: | ||
| if topk_indices.ndim != 3 or topk_length.shape != topk_indices.shape[:-1]: | ||
| return None | ||
| positions = torch.arange(topk_indices.size(-1), device=topk_indices.device) | ||
| valid = positions < topk_length.to(dtype=torch.int64, device=topk_indices.device).unsqueeze( | ||
| -1 | ||
| ) | ||
| topk_indices = topk_indices.masked_fill(~valid, -1) | ||
| return tilelang_dsa.run_fused_absorbed_sparse_attention( | ||
| query, key, topk_indices, softmax_scale, v_channels | ||
| ) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "run_fused_absorbed_sparse_attention", | ||
| "run_fused_qk_topk", | ||
| "run_fused_qk_topk_with_loss", | ||
| ] | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a pure wrapper around
tilelang_dsa.run_fused_qk_topk?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, It forwards the backend inputs and converts TileLang’s indices-only return value to the common DSA backend contract (topk_indices, topk_length), with topk_length=None