[Bugfix] DSV4 TP16 garbage output (#47493)

Signed-off-by: Jeff Ma <jeffjma@umich.edu>
Co-authored-by: Lucas Wilkinson <LucasWilkinson@users.noreply.github.com>
This commit is contained in:
Jeff (Junze) Ma
2026-07-07 21:04:33 -07:00
committed by GitHub
co-authored by Lucas Wilkinson
parent d9e57ea82e
commit 80eb01e93d
5 changed files with 61 additions and 6 deletions
@@ -45,6 +45,21 @@ def _get_flashinfer_dsv4_workspace(device: torch.device) -> torch.Tensor:
return workspace
def _packed_block_span(pool: torch.Tensor) -> int:
"""Per-block stride of ``pool`` in tokens (``stride(0)//stride(-2)``): ==
block_size for unpacked KV, larger when packed (#44577). Raises if not
token-aligned."""
block_stride = pool.stride(0)
token_stride = pool.stride(-2)
if block_stride % token_stride != 0:
raise NotImplementedError(
"FLASHINFER_MLA_SPARSE_DSV4 packed KV requires the per-block stride "
f"({block_stride}) to be a multiple of the per-token stride "
f"({token_stride}); this layout is not supported yet."
)
return block_stride // token_stride
class DeepseekV4FlashInferMLASparseBackend(DeepseekV4FlashMLABackend):
"""FlashInfer backend using the DSv4 sparse metadata/cache layout.
@@ -368,6 +383,8 @@ class DeepseekV4FlashInferMLAAttention(DeepseekV4Attention):
)
cached_sparse = swa_metadata.flashinfer_sparse_index_cache.get(cache_key, None)
if cached_sparse is None:
swa_block_span = _packed_block_span(swa_k_cache)
compressed_block_span = _packed_block_span(compressed_kv_cache)
sparse_indices, sparse_topk_lens = build_flashinfer_mixed_sparse_indices(
decode_swa_indices,
decode_compressed_indices,
@@ -385,6 +402,8 @@ class DeepseekV4FlashInferMLAAttention(DeepseekV4Attention):
top_k,
decode_compressed_indices_are_local=decode_compressed_indices_are_local,
decode_is_valid_token=decode_is_valid_token,
swa_block_span=swa_block_span,
compressed_block_span=compressed_block_span,
)
if cache_key != "c4a":
swa_metadata.flashinfer_sparse_index_cache[cache_key] = (