Files

245 lines
8.7 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import itertools
from dataclasses import dataclass, replace
from typing import Any
import torch
from vllm.config import VllmConfig, get_current_vllm_config_or_none
from vllm.model_executor.layers.mamba.checkpoint import (
MambaPrefillCheckpointBuilder,
MambaPrefillCheckpointMetadata,
)
from vllm.utils.torch_utils import async_tensor_h2d
from vllm.v1.attention.backend import (
AttentionBackend,
CommonAttentionMetadata,
max_decode_query_len,
)
from vllm.v1.attention.backends.mamba_attn import (
BaseMambaAttentionMetadata,
BaseMambaAttentionMetadataBuilder,
)
from vllm.v1.kv_cache_interface import KVCacheSpec, MambaSpec
def compute_varlen_chunk_metadata(
query_start_loc: torch.Tensor,
chunk_size: int,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""Build chunk-aligned, variable-length metadata used by Mamba2 SSD kernels.
Given per-sequence cumulative token starts `query_start_loc` of shape [B+1]
and a physical `chunk_size`, returns three tensors on the same device:
- cu_chunk_seqlens: (nchunks+1,) int32 exclusive prefix-sum of
logical-chunk lengths (each logical chunk never crosses a sequence or
physical-chunk boundary).
- last_chunk_indices: (B,) int32 index of the last logical chunk
for each sequence (=-1 for empty sequences).
- seq_idx_chunks: (nchunks,) int32 sequence index for each logical
chunk in order.
This is intentionally lightweight and CPU-side; it mirrors the metadata
produced by the V1 Mamba2 meta-data builder and is exported so tests
(and other callers) can avoid duplicating the logic.
"""
assert query_start_loc.ndim == 1, "query_start_loc must be 1-D [B+1]"
assert int(query_start_loc[0].item()) == 0, "query_start_loc[0] must be 0"
device = query_start_loc.device
qsl64 = query_start_loc.to(torch.int64)
starts = qsl64[:-1].tolist()
ends = qsl64[1:].tolist()
total = int(qsl64[-1].item())
chunk_lens: list[int] = []
seq_idx_chunks: list[int] = []
last_chunk_indices: list[int] = [-1] * len(starts)
for b, (s, e) in enumerate(zip(starts, ends)):
if e <= s:
# empty sequence
continue
pos = s
while pos < e:
# split at both sequence boundaries and physical chunk boundaries
room = chunk_size - (pos % chunk_size)
take = min(room, e - pos)
chunk_lens.append(int(take))
seq_idx_chunks.append(b)
last_chunk_indices[b] = len(chunk_lens) - 1
pos += take
# Exclusive prefix sum over logical-chunk lengths
if chunk_lens:
cu_chunk_seqlens_list = [0] + list(itertools.accumulate(chunk_lens))
# Final boundary must equal total tokens (check on host to avoid a sync)
assert cu_chunk_seqlens_list[-1] == total
else:
cu_chunk_seqlens_list = [0]
cu_chunk_seqlens = async_tensor_h2d(
cu_chunk_seqlens_list, dtype=torch.int32, device=device
)
# last_chunk_indices is empty when there are no sequences (len(starts) == 0).
last_chunk_indices_t = async_tensor_h2d(
last_chunk_indices, dtype=torch.int32, device=device
)
seq_idx_chunks_t = async_tensor_h2d(
seq_idx_chunks, dtype=torch.int32, device=device
)
return cu_chunk_seqlens, last_chunk_indices_t, seq_idx_chunks_t
class Mamba2AttentionBackend(AttentionBackend):
@staticmethod
def get_name() -> str:
return "MAMBA2_ATTN"
@staticmethod
def get_builder_cls() -> type["Mamba2AttentionMetadataBuilder"]:
return Mamba2AttentionMetadataBuilder
@classmethod
def is_ssm(cls) -> bool:
return True
@classmethod
def supports_device_cpu_query_lens_mismatch(cls) -> bool:
# Decode kernels take device offsets; ReplaySSM plans off CPU counts.
vllm_config = get_current_vllm_config_or_none()
if vllm_config is None:
return True
return not vllm_config.cache_config.use_replayssm
@dataclass
class Mamba2AttentionMetadata(BaseMambaAttentionMetadata):
prep_initial_states: bool = False
chunk_size: int = 0
# Chunk-related metadata (only for prefill)
seq_idx_p: torch.Tensor | None = None
# Internal prefill checkpoints, one entry per prefill row. The chunk
# index selects the varlen_states row holding the checkpoint state.
checkpoint_chunk_idx: torch.Tensor | None = None
checkpoint_meta: MambaPrefillCheckpointMetadata | None = None
class Mamba2AttentionMetadataBuilder(
BaseMambaAttentionMetadataBuilder[Mamba2AttentionMetadata]
):
metadata_cls = Mamba2AttentionMetadata
@classmethod
def get_varlen_cudagraph_max_query_len(
cls,
vllm_config: VllmConfig,
kv_cache_spec: KVCacheSpec,
) -> int | None:
return max_decode_query_len(vllm_config)
def __init__(
self,
kv_cache_spec: MambaSpec,
layer_names: list[str],
vllm_config: VllmConfig,
device: torch.device,
):
super().__init__(kv_cache_spec, layer_names, vllm_config, device)
chunk_size = vllm_config.model_config.get_mamba_chunk_size()
assert chunk_size is not None, (
"chunk_size needs to be set in the model config for Mamba2 models"
)
self.chunk_size: int = chunk_size
self.checkpoint_builder = MambaPrefillCheckpointBuilder(
vllm_config, kv_cache_spec
)
def build(
self,
common_prefix_len: int,
common_attn_metadata: CommonAttentionMetadata,
fast_build: bool = False,
**kwargs: Any,
) -> Mamba2AttentionMetadata:
common = self._compute_common_metadata(
common_attn_metadata,
num_accepted_tokens=kwargs.get("num_accepted_tokens"),
num_decode_draft_tokens_cpu=kwargs.get("num_decode_draft_tokens_cpu"),
)
seq_idx_p = None
cu_chunk_seqlen_p = None
last_chunk_indices_p = None
checkpoint_chunk_idx = None
checkpoint_meta = None
prep_initial_states = False
# Compute seq_idx for prefill only
if common.num_prefills > 0:
prep_initial_states = False
if common.has_initial_states_p is not None:
# Same condition as `has_initial_states_p`, but derived from CPU
# data so it needs no D2H. `seq_lens_cpu_upper_bound` is precise
# for prefill rows, which is all this slice covers.
num_computed_tokens_p_cpu, _ = self._prefill_cpu_metadata(
common_attn_metadata,
common.num_reqs,
common.num_prefills,
common.num_decode_tokens,
)
prep_initial_states = bool((num_computed_tokens_p_cpu > 0).any())
checkpoint_offsets_p = None
first = common.num_reqs - common.num_prefills
checkpoint = self.checkpoint_builder.build(
common_attn_metadata,
list(range(first, common.num_reqs)),
)
if checkpoint is not None:
# The host offsets place a chunk boundary on the checkpoint.
checkpoint_offsets_p = checkpoint.offsets
checkpoint_meta = checkpoint
(
cu_chunk_seqlen_p,
seq_idx_p,
last_chunk_indices_p,
checkpoint_chunk_idx,
) = self._build_chunk_metadata_tensors(
self.chunk_size,
common,
common_attn_metadata,
checkpoint_offsets_p,
)
return replace(
common,
prep_initial_states=prep_initial_states,
chunk_size=self.chunk_size,
seq_idx_p=seq_idx_p,
cu_chunk_seqlen_p=cu_chunk_seqlen_p,
last_chunk_indices_p=last_chunk_indices_p,
checkpoint_chunk_idx=checkpoint_chunk_idx,
checkpoint_meta=checkpoint_meta,
)
def update_block_table(
self,
metadata: Mamba2AttentionMetadata,
blk_table: torch.Tensor,
slot_mapping: torch.Tensor,
) -> Mamba2AttentionMetadata:
new_metadata = super().update_block_table(metadata, blk_table, slot_mapping)
if metadata.checkpoint_meta is None:
return new_metadata
# Checkpoint destinations are block-table entries, so each group
# re-gathers its own rather than writing into the source group's.
return replace(
new_metadata,
checkpoint_meta=metadata.checkpoint_meta.regather_state_indices(blk_table),
)