[DSA] Wire DeepGEMM sparse MQA logits into the DeepSeek V4.1 indexer (#56254)

Signed-off-by: Jared Wen <w13431838023@gmail.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Jared Wen
2026-09-15 14:07:14 -07:00
committed by GitHub
co-authored by Claude Fable 5.1
parent f2aad6aa70
commit 1257512305
13 changed files with 1830 additions and 26 deletions
+6 -1
View File
@@ -1116,6 +1116,11 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
for request_slice, query_slice in chunk_specs
]
def _prefill_split_seq_lens(self, seq_lens_cpu: torch.Tensor) -> torch.Tensor:
"""Per-request KV lengths the prefill chunker budgets logits with;
subclasses whose logits rows are wider than the context override."""
return seq_lens_cpu
@staticmethod
def _split_indexer_prefill_chunks(
compressed_seq_lens_cpu: torch.Tensor,
@@ -1278,7 +1283,7 @@ class DeepseekV32IndexerMetadataBuilder(AttentionMetadataBuilder):
)
else:
chunk_specs = self._split_indexer_prefill_chunks(
compressed_seq_lens_cpu[num_decodes:],
self._prefill_split_seq_lens(compressed_seq_lens_cpu[num_decodes:]),
prefill_query_lens_cpu,
self.max_prefill_buffer_size,
max_logits_bytes,
@@ -0,0 +1,235 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""DeepSeek V4.1 indexer backend for the DeepGEMM sparse MQA-logits path.
Candidate-consuming indexer layers score only the candidate blocks published
by the candidate-source layer (``fp8_fp4_(paged_)sparse_mqa_logits``) instead
of dense logits over the whole context. This backend extends the dense
indexer metadata with the per-row scratch that path needs, so the layer op
allocates nothing per step, and lets all consumer layers of a step share one
candidate expansion and DeepGEMM schedule (they all read the same candidate
blocks). Opt in with ``AttentionConfig.indexer_sparse_logits``.
"""
from dataclasses import dataclass, fields
import torch
from vllm.config import VllmConfig
from vllm.logger import init_logger
from vllm.model_executor.kernels.attention.dsa.sparse_mqa_logits import (
check_deep_select_layout,
has_deep_select,
pick_sparse_block_kv,
)
from vllm.platforms import current_platform
from vllm.utils.deep_gemm import has_deep_gemm_sparse_mqa
from vllm.v1.attention.backend import CommonAttentionMetadata
from vllm.v1.attention.backends.mla.indexer import (
DeepseekV32IndexerMetadata,
DeepseekV32IndexerMetadataBuilder,
DeepseekV41IndexerBackend,
dsa_indexer_uses_fp4,
)
from vllm.v1.kv_cache_interface import AttentionSpec
logger = init_logger(__name__)
@dataclass
class SparseMQARowsMetadata:
"""Per-row state for one sparse-logits call: the decode rows, or one
prefill chunk. Buffers are builder-owned views sized to the rows.
``kernel_metadata`` (the DeepGEMM schedule) is filled by the first
consumer indexer layer of the step together with ``sparse_indices`` /
``end``; later consumer layers reuse all three.
"""
row_ks: torch.Tensor
"""[rows] int32 K-range start per row; zeros for paged decode."""
row_ke: torch.Tensor
"""[rows] int32 K-range end (prefill) or compressed context length."""
sparse_indices: torch.Tensor
"""[rows, num_sparse_blocks] int32 DeepGEMM sparse block ids."""
end: torch.Tensor
"""[rows] int32 valid sparse-column count per row."""
col_indices: torch.Tensor
"""[rows, topk] int32 scratch for the sparse-column top-k."""
block_table: torch.Tensor | None = None
"""Decode only: [rows, pages] per-row page table."""
row_indices: torch.Tensor | None = None
"""Decode only: [rows] row -> request index."""
kernel_metadata: torch.Tensor | None = None
@dataclass
class DeepseekV41SparseIndexerMetadata(DeepseekV32IndexerMetadata):
sparse_block_kv: int = 0
sparse_decode: SparseMQARowsMetadata | None = None
sparse_prefill: list[SparseMQARowsMetadata] | None = None
"""Parallel to ``prefill.chunks``."""
class DeepseekV41SparseIndexerBackend(DeepseekV41IndexerBackend):
@staticmethod
def get_name() -> str:
return "DEEPSEEK_V41_SPARSE_INDEXER"
@staticmethod
def get_builder_cls() -> type["DeepseekV41SparseIndexerMetadataBuilder"]:
return DeepseekV41SparseIndexerMetadataBuilder
class DeepseekV41SparseIndexerMetadataBuilder(DeepseekV32IndexerMetadataBuilder):
"""Dense indexer metadata plus the sparse-logits row scratch.
Every requirement of the sparse kernels is checked here, at engine start,
rather than falling back to the dense path per step.
"""
def __init__(
self,
kv_cache_spec: AttentionSpec,
layer_names: list[str],
vllm_config: VllmConfig,
device: torch.device,
**kwargs,
) -> None:
super().__init__(kv_cache_spec, layer_names, vllm_config, device, **kwargs)
hf_config = vllm_config.model_config.hf_text_config
candidate_block_size = getattr(hf_config, "candidate_block_size", 0)
candidate_topk_blocks = getattr(hf_config, "candidate_topk_blocks", 0)
topk_tokens = getattr(hf_config, "index_topk", 0)
prefix = "attention_config.indexer_sparse_logits"
if candidate_block_size <= 0 or candidate_topk_blocks <= 0:
raise ValueError(
f"{prefix} requires a two-level (candidate block) indexer model "
"such as DeepSeek-V4.1."
)
if not dsa_indexer_uses_fp4(vllm_config):
raise ValueError(
f"{prefix} requires the MXFP4 indexer cache "
"(attention_config.indexer_kv_dtype='mxfp4'): the sparse kernels "
"take UE8M0-packed granularity-32 scales."
)
if not (
current_platform.is_cuda()
and current_platform.is_device_capability_family(100)
):
raise ValueError(f"{prefix} requires an SM100-class GPU.")
if not has_deep_gemm_sparse_mqa():
raise ValueError(
f"{prefix} requires DeepGEMM >= 2.8 "
"(fp8_fp4_sparse_mqa_logits not found)."
)
if self.dcp_world_size > 1 or self.use_pcp:
raise NotImplementedError(
f"{prefix} is not supported with decode/prefill context parallel."
)
if not self.supports_varlen:
raise ValueError(
f"{prefix} requires the varlen paged MQA logits decode layout."
)
# The top-k runs on the kernels' bf16 logits with DeepSelect; there is
# no fp32 fallback, so the other top-k backends cannot be honored.
topk_backend = vllm_config.kernel_config.sparse_indexer_topk_backend
if not has_deep_select():
raise ValueError(
f"{prefix} requires the DeepSelect top-k extension "
"(vllm._deepselect_C) for this GPU."
)
if topk_backend not in ("auto", "deep_select"):
raise ValueError(
f"{prefix} uses DeepSelect for the top-k; "
f"kernel_config.sparse_indexer_topk_backend='{topk_backend}' "
"is not supported with it."
)
self.sparse_block_kv = pick_sparse_block_kv(candidate_block_size)
if kv_cache_spec.block_size % self.sparse_block_kv != 0:
raise ValueError(
f"{prefix}: indexer cache pages ({kv_cache_spec.block_size} "
f"tokens) must be a multiple of {self.sparse_block_kv} tokens."
)
self.num_sparse_blocks = candidate_topk_blocks * (
candidate_block_size // self.sparse_block_kv
)
num_sparse_cols = self.num_sparse_blocks * self.sparse_block_kv
check_deep_select_layout(num_sparse_cols, topk_tokens)
# Sparse logits are bf16 [rows, num_sparse_cols]; make the prefill
# chunker (which budgets fp32 [rows, seq_len]) size chunks as if every
# row were this wide.
self.min_split_seq_len = (num_sparse_cols * 2 + 3) // 4
# Rows are indexed by batch token (decode rows first, then prefill
# chunks by token range), like ``arange_buffer``.
max_rows = self.arange_buffer.shape[0]
int32 = dict(dtype=torch.int32, device=device)
self.sparse_indices_buffer = torch.zeros(
(max_rows, self.num_sparse_blocks), **int32
)
self.end_buffer = torch.zeros(max_rows, **int32)
self.col_indices_buffer = torch.zeros((max_rows, topk_tokens), **int32)
self.zero_row_ks = torch.zeros(max_rows, **int32)
logger.info_once(
"DeepSeek V4.1 indexer: candidate consumers score only the candidate "
"blocks with DeepGEMM sparse MQA logits (%d sparse blocks of %d "
"tokens per row).",
self.num_sparse_blocks,
self.sparse_block_kv,
)
def _prefill_split_seq_lens(self, seq_lens_cpu: torch.Tensor) -> torch.Tensor:
return seq_lens_cpu.clamp(min=self.min_split_seq_len)
def _rows(self, start: int, end: int, row_ks, row_ke, **kwargs):
return SparseMQARowsMetadata(
row_ks=row_ks,
row_ke=row_ke,
sparse_indices=self.sparse_indices_buffer[start:end],
end=self.end_buffer[start:end],
col_indices=self.col_indices_buffer[start:end],
**kwargs,
)
def build(
self,
common_prefix_len: int,
common_attn_metadata: CommonAttentionMetadata,
fast_build: bool = False,
) -> DeepseekV41SparseIndexerMetadata:
base = super().build(common_prefix_len, common_attn_metadata, fast_build)
metadata = DeepseekV41SparseIndexerMetadata(
**{f.name: getattr(base, f.name) for f in fields(base)},
sparse_block_kv=self.sparse_block_kv,
)
if base.decode is not None:
decode = base.decode
rows = decode.seq_lens.shape[0]
# The varlen layout flattens every query to one row, so the base
# decode tensors are already per row.
assert decode.seq_lens.shape[1] == 1 and not decode.requires_padding
assert decode.block_table.shape[0] == rows
metadata.sparse_decode = self._rows(
0,
rows,
row_ks=self.zero_row_ks[:rows],
row_ke=decode.seq_lens.view(-1),
block_table=decode.block_table,
row_indices=(
decode.indices
if decode.indices is not None
else self.arange_buffer[:rows]
),
)
if base.prefill is not None:
metadata.sparse_prefill = [
self._rows(
chunk.token_start,
chunk.token_end,
row_ks=chunk.cu_seqlen_ks,
row_ke=chunk.cu_seqlen_ke,
)
for chunk in base.prefill.chunks
]
return metadata
+9
View File
@@ -1608,6 +1608,15 @@ def _get_kv_cache_bytes_per_block(
]
if hot_page_sizes:
bytes_per_block = round_up(bytes_per_block, math.lcm(*hot_page_sizes))
stride_alignments = [
spec.block_stride_alignment
for group in kv_cache_groups
for layer_name in group.layer_names
if isinstance(spec := _get_per_layer_spec(group, layer_name), MLAAttentionSpec)
and spec.block_stride_alignment
]
if stride_alignments:
bytes_per_block = round_up(bytes_per_block, math.lcm(*stride_alignments))
return bytes_per_block
+10 -1
View File
@@ -653,6 +653,12 @@ class MLAAttentionSpec(FullAttentionSpec):
is_index_group_leader: bool = False
storage_block_size: int | None = None
"""Token width used to view storage when it differs from the kernel block."""
block_stride_alignment: int | None = None
"""Required alignment, in bytes, of the distance between consecutive
blocks of this cache. In block-major layouts that distance is the whole
block (all layers' pages), so the allocator rounds the block up to it.
DeepGEMM's paged sparse MQA-logits kernels address pages as
``base + page * stride`` and need it 512B-aligned."""
# Group capability enabled when any member flattens a non-causal query block
# into decode rows. Runtime metadata still selects causal vs. non-causal mode.
non_causal_multi_token_decode: bool = False
@@ -674,6 +680,7 @@ class MLAAttentionSpec(FullAttentionSpec):
cache_role_set = {spec.cache_role for spec in specs}
index_group_leader_set = {spec.is_index_group_leader for spec in specs}
storage_block_size_set = set(spec.storage_block_size for spec in specs)
block_stride_alignment_set = {spec.block_stride_alignment for spec in specs}
assert (
len(cache_dtype_str_set) == 1
and len(tokens_per_state_set) == 1
@@ -681,10 +688,11 @@ class MLAAttentionSpec(FullAttentionSpec):
and len(cache_role_set) == 1
and len(index_group_leader_set) == 1
and len(storage_block_size_set) == 1
and len(block_stride_alignment_set) == 1
), (
"All attention layers in the same KV cache group must use the same "
"quantization method, tokens per state, model version, cache role, "
"index-sharing role, and storage block size."
"index-sharing role, storage block size and block stride alignment."
)
merged_spec = cls(
block_size=specs[0].block_size,
@@ -701,6 +709,7 @@ class MLAAttentionSpec(FullAttentionSpec):
cache_role=cache_role_set.pop(),
is_index_group_leader=index_group_leader_set.pop(),
storage_block_size=storage_block_size_set.pop(),
block_stride_alignment=block_stride_alignment_set.pop(),
non_causal_multi_token_decode=any(
spec.non_causal_multi_token_decode for spec in specs
),