[Mamba] Add FlashInfer ReplaySSM backend (#52506)
Signed-off-by: Andrii Skliar <askliar@nvidia.com> Signed-off-by: Andrii Skliar <andreyws96@gmail.com> Co-authored-by: Andrii Skliar <askliar@nvidia.com>
This commit is contained in:
co-authored by
Andrii Skliar
parent
25efcfa788
commit
adebc41b7e
@@ -8,6 +8,7 @@ from typing import Any, ClassVar, TypeVar
|
||||
import torch
|
||||
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.config.mamba import MambaBackendEnum
|
||||
from vllm.utils.math_utils import cdiv
|
||||
from vllm.utils.torch_utils import async_tensor_h2d
|
||||
from vllm.v1.attention.backend import (
|
||||
@@ -74,12 +75,15 @@ class BaseMambaAttentionMetadata:
|
||||
nums_dict: dict | None = None
|
||||
batch_ptr: torch.Tensor | None = None
|
||||
token_chunk_offset_ptr: torch.Tensor | None = None
|
||||
# ReplaySSM standard decode: per-row ring cursor and flush flag, plus the
|
||||
# per-step (decode_rows, ngroups, replayssm_buffer_len) fp32 scratch for the
|
||||
# precomputed k^T q products. All None when use_replayssm is disabled.
|
||||
# ReplaySSM standard decode — Triton: per-row ring cursor and flush flag,
|
||||
# plus (decode_rows, ngroups, replayssm_buffer_len) fp32 scratch for
|
||||
# precomputed B·C products. All None when use_replayssm is disabled or
|
||||
# the FlashInfer ReplaySSM backend is selected.
|
||||
write_pos_d: torch.Tensor | None = None
|
||||
is_flush_d: torch.Tensor | None = None
|
||||
bc_pre_scratch: torch.Tensor | None = None
|
||||
# ReplaySSM — FlashInfer checkpointing_ssu two-kernel scratch.
|
||||
replayssm_scratch: tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None = None
|
||||
|
||||
|
||||
class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
@@ -107,6 +111,10 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
self.use_spec_decode = self.num_spec_tokens > 0
|
||||
self.use_replayssm = vllm_config.cache_config.use_replayssm
|
||||
self.replayssm_buffer_len = vllm_config.cache_config.replayssm_buffer_len
|
||||
self.use_flashinfer_replayssm = (
|
||||
self.use_replayssm
|
||||
and vllm_config.mamba_config.backend == MambaBackendEnum.FLASHINFER
|
||||
)
|
||||
|
||||
scheduler_config = vllm_config.scheduler_config
|
||||
self.decode_cudagraph_max_bs: int = scheduler_config.max_num_seqs
|
||||
@@ -166,9 +174,12 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
dtype=torch.int32,
|
||||
device=device,
|
||||
)
|
||||
# ReplaySSM standard-decode CUDA-graph buffers: per-row ring cursor,
|
||||
# flush flag, and the k^T q precompute scratch.
|
||||
if self.use_replayssm:
|
||||
self.decode_bc_pre_scratch: torch.Tensor | None = None
|
||||
self.decode_replayssm_scratch: (
|
||||
tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None
|
||||
) = None
|
||||
# ReplaySSM CUDA-graph buffers for the selected backend.
|
||||
if self.use_replayssm and not self.use_flashinfer_replayssm:
|
||||
self.decode_write_pos_d: torch.Tensor = torch.empty(
|
||||
(self.decode_cudagraph_max_bs,),
|
||||
dtype=torch.int32,
|
||||
@@ -185,7 +196,7 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
bc_scratch_bs = max(
|
||||
self.decode_cudagraph_max_bs, scheduler_config.max_num_seqs
|
||||
)
|
||||
self.decode_bc_pre_scratch: torch.Tensor = torch.empty(
|
||||
self.decode_bc_pre_scratch = torch.empty(
|
||||
(
|
||||
bc_scratch_bs,
|
||||
bc_ngroups,
|
||||
@@ -194,8 +205,20 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
dtype=torch.float32,
|
||||
device=device,
|
||||
)
|
||||
else:
|
||||
self.decode_bc_pre_scratch = None
|
||||
elif self.use_flashinfer_replayssm:
|
||||
from flashinfer.mamba.checkpointing_ssu import (
|
||||
allocate_checkpointing_ssu_scratch,
|
||||
)
|
||||
|
||||
nheads = kv_cache_spec.shapes[2][0]
|
||||
self.decode_replayssm_scratch = allocate_checkpointing_ssu_scratch(
|
||||
batch_size=scheduler_config.max_num_seqs,
|
||||
num_heads=nheads,
|
||||
num_predicted_tokens=1,
|
||||
max_window=self.replayssm_buffer_len,
|
||||
dtype=vllm_config.model_config.dtype,
|
||||
device=device,
|
||||
)
|
||||
|
||||
self._init_reorder_batch_threshold(1, self.use_spec_decode)
|
||||
if self.use_spec_decode:
|
||||
@@ -500,6 +523,7 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
nums_dict, batch_ptr, token_chunk_offset_ptr = None, None, None
|
||||
write_pos_d = None
|
||||
is_flush_d = None
|
||||
replayssm_scratch = None
|
||||
|
||||
if self.vllm_config.cache_config.mamba_cache_mode == "all":
|
||||
num_computed_tokens = common_attn_metadata.compute_num_computed_tokens()
|
||||
@@ -586,7 +610,7 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
num_reqs - num_prefills : num_reqs
|
||||
]
|
||||
|
||||
if self.use_replayssm and num_decodes > 0:
|
||||
if self.use_replayssm and not self.use_flashinfer_replayssm and num_decodes > 0:
|
||||
decode_base_cpu = common_attn_metadata.replayssm_decode_base_cpu
|
||||
num_computed_tokens_cpu = common_attn_metadata._num_computed_tokens_cpu
|
||||
if decode_base_cpu is None or num_computed_tokens_cpu is None:
|
||||
@@ -651,6 +675,15 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
device=common_attn_metadata.query_start_loc.device,
|
||||
)
|
||||
|
||||
if self.use_flashinfer_replayssm and num_decodes > 0:
|
||||
assert self.decode_replayssm_scratch is not None
|
||||
cb_scaled, cumAdt_vec, cb_old = self.decode_replayssm_scratch
|
||||
replayssm_scratch = (
|
||||
cb_scaled[:num_decodes],
|
||||
cumAdt_vec[:num_decodes],
|
||||
cb_old[:num_decodes],
|
||||
)
|
||||
|
||||
bc_pre_scratch = None
|
||||
if (
|
||||
self.use_replayssm
|
||||
@@ -671,6 +704,7 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
write_pos_d=write_pos_d,
|
||||
is_flush_d=is_flush_d,
|
||||
bc_pre_scratch=bc_pre_scratch,
|
||||
replayssm_scratch=replayssm_scratch,
|
||||
num_accepted_tokens=num_accepted_tokens,
|
||||
query_start_loc_d=query_start_loc_d,
|
||||
block_idx_last_scheduled_token=block_idx_last_scheduled_token,
|
||||
@@ -708,6 +742,7 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
write_pos_d = metadata.write_pos_d
|
||||
is_flush_d = metadata.is_flush_d
|
||||
bc_pre_scratch = metadata.bc_pre_scratch
|
||||
replayssm_scratch = metadata.replayssm_scratch
|
||||
if (
|
||||
metadata.num_prefills == 0
|
||||
and metadata.num_decodes <= self.decode_cudagraph_max_bs
|
||||
@@ -769,7 +804,7 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
)
|
||||
block_idx_last_scheduled_token_prev_step[metadata.num_decodes :] = 0
|
||||
|
||||
if self.use_replayssm:
|
||||
if self.use_replayssm and not self.use_flashinfer_replayssm:
|
||||
assert write_pos_d is not None
|
||||
assert is_flush_d is not None
|
||||
self.decode_write_pos_d[: metadata.num_decodes].copy_(
|
||||
@@ -788,6 +823,14 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
|
||||
if self.decode_bc_pre_scratch is not None:
|
||||
bc_pre_scratch = self.decode_bc_pre_scratch[:padded_bs]
|
||||
elif self.use_flashinfer_replayssm:
|
||||
assert self.decode_replayssm_scratch is not None
|
||||
cb_scaled, cumAdt_vec, cb_old = self.decode_replayssm_scratch
|
||||
replayssm_scratch = (
|
||||
cb_scaled[:padded_bs],
|
||||
cumAdt_vec[:padded_bs],
|
||||
cb_old[:padded_bs],
|
||||
)
|
||||
|
||||
return replace(
|
||||
metadata,
|
||||
@@ -797,6 +840,7 @@ class BaseMambaAttentionMetadataBuilder(AttentionMetadataBuilder[M], abc.ABC):
|
||||
write_pos_d=write_pos_d,
|
||||
is_flush_d=is_flush_d,
|
||||
bc_pre_scratch=bc_pre_scratch,
|
||||
replayssm_scratch=replayssm_scratch,
|
||||
block_idx_last_scheduled_token=block_idx_last_scheduled_token,
|
||||
block_idx_last_computed_token=block_idx_last_computed_token,
|
||||
block_idx_last_scheduled_token_prev_step=(
|
||||
|
||||
@@ -229,7 +229,13 @@ def init_kv_cache(
|
||||
in ("longcat_flash", "longcat_flash_ngram")
|
||||
else 1
|
||||
)
|
||||
bind_kv_cache(kv_caches, forward_context, runner_kv_caches, num_attn_module)
|
||||
bind_kv_cache(
|
||||
kv_caches,
|
||||
forward_context,
|
||||
runner_kv_caches,
|
||||
num_attn_module,
|
||||
kv_cache_groups=kv_cache_config.kv_cache_groups,
|
||||
)
|
||||
return kv_caches
|
||||
|
||||
|
||||
|
||||
@@ -633,7 +633,9 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
cls=self.pcp_manager_cls,
|
||||
)
|
||||
initialize_mamba_ssu_backend(
|
||||
self.vllm_config.mamba_config, self.kv_cache_config
|
||||
self.vllm_config.mamba_config,
|
||||
self.kv_cache_config,
|
||||
use_replayssm=self.vllm_config.cache_config.use_replayssm,
|
||||
)
|
||||
if self.adaptive_verification is not None:
|
||||
self.compilation_config.cudagraph_mode = CUDAGraphMode.FULL_AND_PIECEWISE
|
||||
@@ -707,6 +709,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
context_len: int = 0,
|
||||
skip_eplb: bool = False,
|
||||
is_profile: bool = False,
|
||||
valid_dummy_state_slots: bool = False,
|
||||
**kwargs,
|
||||
) -> tuple[torch.Tensor | None, torch.Tensor | None]:
|
||||
if skip_attn and not is_profile:
|
||||
@@ -764,6 +767,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
skip_attn_for_dummy_run=skip_attn,
|
||||
is_profile=is_profile,
|
||||
context_len=context_len,
|
||||
valid_dummy_state_slots=valid_dummy_state_slots,
|
||||
)
|
||||
self.kv_connector.set_disabled(False)
|
||||
|
||||
@@ -1390,9 +1394,18 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
return block_tables, slot_mappings
|
||||
|
||||
def prepare_dummy_attn(
|
||||
self, input_batch: InputBatch
|
||||
self, input_batch: InputBatch, valid_state_slots: bool = False
|
||||
) -> tuple[tuple[torch.Tensor, ...], torch.Tensor]:
|
||||
block_tables = self.block_tables.get_dummy_block_tables(input_batch.num_reqs)
|
||||
if valid_state_slots:
|
||||
state_slots = torch.arange(
|
||||
1,
|
||||
input_batch.num_reqs + 1,
|
||||
dtype=torch.int32,
|
||||
device=self.device,
|
||||
)
|
||||
for block_table in block_tables:
|
||||
block_table[:, 0].copy_(state_slots)
|
||||
slot_mappings = pcp.maybe_get_pcp_dummy_slot_mappings(
|
||||
self.pcp_manager, self.block_tables, input_batch.num_tokens
|
||||
)
|
||||
@@ -1525,6 +1538,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
skip_attn_for_dummy_run: bool = False,
|
||||
is_profile: bool = False,
|
||||
context_len: int = 0,
|
||||
valid_dummy_state_slots: bool = False,
|
||||
) -> ModelRunnerOutput | IntermediateTensors | None:
|
||||
if not dummy_run:
|
||||
# Update the request states.
|
||||
@@ -1624,7 +1638,9 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
max_query_len=batch_desc.max_query_len,
|
||||
)
|
||||
if not skip_attn_for_dummy_run:
|
||||
block_tables, slot_mappings = self.prepare_dummy_attn(input_batch)
|
||||
block_tables, slot_mappings = self.prepare_dummy_attn(
|
||||
input_batch, valid_dummy_state_slots
|
||||
)
|
||||
if context_len:
|
||||
set_dummy_context(
|
||||
input_batch,
|
||||
|
||||
@@ -7434,6 +7434,7 @@ class GPUModelRunner(
|
||||
self.compilation_config.static_forward_context,
|
||||
self.kv_caches,
|
||||
num_attn_module,
|
||||
kv_cache_groups=kv_cache_config.kv_cache_groups,
|
||||
)
|
||||
return kv_caches
|
||||
|
||||
@@ -7485,7 +7486,9 @@ class GPUModelRunner(
|
||||
self.maybe_add_kv_sharing_layers_to_kv_cache_groups(kv_cache_config)
|
||||
self.initialize_attn_backend(kv_cache_config, is_profiling=is_profiling)
|
||||
initialize_mamba_ssu_backend(
|
||||
self.vllm_config.mamba_config, self.kv_cache_config
|
||||
self.vllm_config.mamba_config,
|
||||
self.kv_cache_config,
|
||||
use_replayssm=self.vllm_config.cache_config.use_replayssm,
|
||||
)
|
||||
# The kernel block size for all KV cache groups. For example, if
|
||||
# kv_cache_manager uses block_size 256 for a given group, but the attention
|
||||
|
||||
@@ -13,6 +13,7 @@ import torch
|
||||
from vllm.config import CacheConfig, VllmConfig
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.attention import Attention
|
||||
from vllm.model_executor.layers.mamba.mamba_mixer2 import share_replayssm_ring_trackers
|
||||
from vllm.model_executor.layers.utils import warmup_rocm_skinny_gemm_workspaces
|
||||
from vllm.model_executor.models.interfaces import MultiModalEmbeddings
|
||||
from vllm.model_executor.models.utils import extract_layer_index
|
||||
@@ -575,6 +576,7 @@ def bind_kv_cache(
|
||||
forward_context: dict[str, Attention],
|
||||
runner_kv_caches: list[torch.Tensor],
|
||||
num_attn_module: int = 1,
|
||||
kv_cache_groups: Sequence[KVCacheGroupSpec] | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Bind the allocated KV cache to both ModelRunner and forward context so
|
||||
@@ -600,6 +602,7 @@ def bind_kv_cache(
|
||||
for layer_name in kv_caches:
|
||||
index2name[extract_layer_index(layer_name, num_attn_module)].append(layer_name)
|
||||
|
||||
ordered_layer_names: list[str] = []
|
||||
for layer_index in sorted(index2name.keys()):
|
||||
layer_names = index2name[layer_index]
|
||||
if len(layer_names) > 1:
|
||||
@@ -613,6 +616,7 @@ def bind_kv_cache(
|
||||
current_platform.check_runner_kv_caches_multi_layer()
|
||||
for layer_name in layer_names:
|
||||
runner_kv_caches.append(kv_caches[layer_name])
|
||||
ordered_layer_names.append(layer_name)
|
||||
|
||||
# Bind kv_caches to forward context. Each layer's bind_kv_cache unpacks
|
||||
# its raw allocation into the per-layer view(s) it needs (e.g. Mamba
|
||||
@@ -621,6 +625,8 @@ def bind_kv_cache(
|
||||
for layer_name, kv_cache in kv_caches.items():
|
||||
forward_context[layer_name].bind_kv_cache(kv_cache)
|
||||
|
||||
share_replayssm_ring_trackers(ordered_layer_names, forward_context, kv_cache_groups)
|
||||
|
||||
|
||||
def clear_layer_kv_caches(layers: Iterable[Any]) -> None:
|
||||
"""Detach the KV/state cache tensors installed by bind_kv_cache().
|
||||
|
||||
Reference in New Issue
Block a user