[NPU] Fix DSA top-k seed buffer shape for MTP IndexShare (#31005)

This commit is contained in:
Peng Xingchen
2026-07-17 12:05:57 +08:00
committed by GitHub
parent dfa6278370
commit 12af7e6c34
2 changed files with 10 additions and 1 deletions
@@ -44,6 +44,13 @@ logger = logging.getLogger(__name__)
FULL_ATTENTION_WINDOW = 2147483647 FULL_ATTENTION_WINDOW = 2147483647
def _expand_dsa_sparse_indices(topk_indices: torch.Tensor) -> torch.Tensor:
"""Expand [T, K] to [T, 1, K] for NPU sparse attention."""
if topk_indices.dim() == 2:
return topk_indices.unsqueeze(-2)
return topk_indices
def _reshape_kv_for_fia_nz( def _reshape_kv_for_fia_nz(
tensor: torch.Tensor, num_heads: int, head_dim: int, page_size: int tensor: torch.Tensor, num_heads: int, head_dim: int, page_size: int
) -> torch.Tensor: ) -> torch.Tensor:
@@ -1053,6 +1060,7 @@ class AscendAttnBackend(AttentionBackend):
actual_seq_lengths_kv, actual_seq_lengths_kv,
) )
else: else:
topk_indices = _expand_dsa_sparse_indices(topk_indices)
attn_out, _, _ = torch_npu.npu_sparse_flash_attention( attn_out, _, _ = torch_npu.npu_sparse_flash_attention(
query=q_nope, query=q_nope,
key=k_nope, key=k_nope,
@@ -2342,7 +2342,8 @@ class Indexer(MultiPlatformOp):
sparse_count=self.index_topk, sparse_count=self.index_topk,
sparse_mode=3, sparse_mode=3,
) )
return topk_indices[0] # Keep DSA top-k as [T, K]; NPU attention expands it when needed.
return topk_indices[0].squeeze(1)
def do_npu_cp_balance_indexer( def do_npu_cp_balance_indexer(
self, self,