+5








e126687a9a
Signed-off-by: huanghaoyan.hhy <huanghaoyan.hhy@alibaba-inc.com> Signed-off-by: andyluo7 <andy.luo@amd.com> Signed-off-by: lishunyang12 <lishunyang03@gmail.com> Signed-off-by: Harry Huang <huanghaoyan.hhy@alibaba-inc.com> Signed-off-by: Thien Tran <gau.nernst@yahoo.com.sg> Signed-off-by: Isotr0py <Isotr0py@outlook.com> Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com> Signed-off-by: Yifan Qiao <yifanqiao@inferact.ai> Signed-off-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: KungYork <firelikest@163.com> Co-authored-by: Thien Tran <gau.nernst@yahoo.com.sg> Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai> Co-authored-by: andyluo7 <43718156+andyluo7@users.noreply.github.com> Co-authored-by: Isotr0py <mozf@inferact.ai> Co-authored-by: andyluo7 <andy.luo@amd.com> Co-authored-by: OpenAI Codex <noreply@openai.com> Co-authored-by: lishunyang12 <lishunyang03@gmail.com> Co-authored-by: Vadim Gimpelson <156319763+vadiklyutiy@users.noreply.github.com> Co-authored-by: Isotr0py <Isotr0py@outlook.com> Co-authored-by: Lucas Wilkinson <lwilkins@redhat.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: OpenAI Codex <codex@openai.com>
91 lines
3.4 KiB
Python
91 lines
3.4 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
from abc import abstractmethod
|
|
from collections.abc import Iterable
|
|
from math import prod
|
|
|
|
import torch
|
|
|
|
from vllm.config import VllmConfig
|
|
from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase
|
|
from vllm.utils.torch_utils import get_dtype_size
|
|
from vllm.v1.attention.backend import AttentionBackend
|
|
from vllm.v1.attention.backends.registry import MambaAttentionBackendEnum
|
|
from vllm.v1.attention.selector import get_mamba_attn_backend
|
|
from vllm.v1.kv_cache_interface import KVCacheSpec, MambaSpec
|
|
|
|
|
|
class MambaBase(AttentionLayerBase):
|
|
"""
|
|
Base class for Mamba-like layers which support the v1 engine.
|
|
Inherit from this class if you implement a custom layer.
|
|
"""
|
|
|
|
# Contains the KV cache (mamba state) for the layer
|
|
# in the shape specified by `self.get_state_shape`.
|
|
kv_cache: tuple[torch.Tensor, ...]
|
|
supports_dcp: bool = False
|
|
|
|
def bind_kv_cache(self, kv_cache: torch.Tensor) -> None:
|
|
"""Unpack a raw ``[B, 1, 1, C]`` int8 page view into per-state views.
|
|
|
|
Each block's ``C`` bytes hold the layer's states (e.g. conv, ssm)
|
|
packed contiguously; slice them out and reinterpret per dtype/shape.
|
|
"""
|
|
pages = kv_cache.squeeze(dim=(1, 2))
|
|
states: list[torch.Tensor] = []
|
|
offset = 0
|
|
for shape, dtype in zip(self.get_state_shape(), self.get_state_dtype()):
|
|
nbytes = prod(shape) * get_dtype_size(dtype)
|
|
state = pages[:, offset : offset + nbytes].view(dtype)
|
|
states.append(state.view(-1, *shape))
|
|
offset += nbytes
|
|
self.kv_cache = tuple(states)
|
|
|
|
@abstractmethod
|
|
def get_state_shape(self) -> Iterable[tuple[int, ...]]:
|
|
"""
|
|
Defines the shape of the state.
|
|
For mamba layers this is usually a (conv_state, ssm_state) tuple.
|
|
In this case, returns (conv_state_shape, ssm_state_shape).
|
|
"""
|
|
pass
|
|
|
|
@property
|
|
@abstractmethod
|
|
def mamba_type(self) -> MambaAttentionBackendEnum:
|
|
pass
|
|
|
|
@property
|
|
def is_kv_cache_tp_replicated(self) -> bool:
|
|
return False
|
|
|
|
@abstractmethod
|
|
def get_state_dtype(self) -> tuple[torch.dtype, ...]:
|
|
pass
|
|
|
|
def get_kv_cache_spec(self, vllm_config: VllmConfig) -> KVCacheSpec | None:
|
|
mamba_block_size = vllm_config.cache_config.mamba_block_size
|
|
assert mamba_block_size is not None
|
|
page_size_padded = vllm_config.cache_config.mamba_page_size_padded
|
|
return MambaSpec(
|
|
shapes=tuple(self.get_state_shape()),
|
|
dtypes=self.get_state_dtype(),
|
|
block_size=mamba_block_size,
|
|
page_size_padded=page_size_padded,
|
|
mamba_type=self.mamba_type,
|
|
tp_replicated=self.is_kv_cache_tp_replicated,
|
|
mamba_cache_mode=vllm_config.cache_config.mamba_cache_mode,
|
|
# RecoverSSM verifies the whole window off one checkpoint, so it
|
|
# never writes the baseline's per-draft-token state slots.
|
|
num_speculative_blocks=(
|
|
0
|
|
if vllm_config.cache_config.use_kda_recoverssm
|
|
else vllm_config.num_speculative_tokens
|
|
),
|
|
)
|
|
|
|
def get_attn_backend(self) -> type[AttentionBackend]:
|
|
"""Get the attention backend class for this Mamba layer."""
|
|
return get_mamba_attn_backend(self.mamba_type)
|