main
py 45 lines 1.41 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 """Base class for attention-like layers."""
4
5 from abc import ABC, abstractmethod
6
7 import torch
8
9 from vllm.config import VllmConfig
10 from vllm.v1.attention.backend import AttentionBackend, AttentionImpl
11 from vllm.v1.kv_cache_interface import KVCacheSpec
12
13
14 class AttentionLayerBase(ABC):
15 """Base class for attention-like layers (Attention, Mamba, etc.)
16 that support the v1 engine.
17
18 This provides a common interface for getting attention backends
19 from different layer types.
20 """
21
22 impl: "AttentionImpl"
23 supports_dcp: bool = True
24 # Set by the speculator on its draft model's layers.
25 is_draft_layer: bool = False
26
27 def bind_kv_cache(self, kv_cache: torch.Tensor) -> None:
28 """Bind the allocated KV cache tensor to this layer.
29
30 The default stores the cache view as-is; subclasses (e.g. Mamba)
31 override this to unpack the raw buffer into per-state views.
32 """
33 self.kv_cache = kv_cache
34
35 @abstractmethod
36 def get_attn_backend(self) -> type[AttentionBackend]:
37 """Get the attention backend class for this layer."""
38 pass
39
40 @abstractmethod
41 def get_kv_cache_spec(self, vllm_config: VllmConfig) -> KVCacheSpec | None:
42 """Get the KV cache spec for this layer.
43 May be None if the layer does not need KV cache.
44 """
45 pass