interfaces.py 3.2 KB
Newer Older
1
2
import enum
from abc import ABC, abstractmethod
3
from typing import List
4
from typing import Sequence as GenericSequence
5
from typing import Tuple
6
7

from vllm.sequence import Sequence, SequenceGroup
8
from vllm.utils import Device
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


class AllocStatus(enum.Enum):
    """Result for BlockSpaceManager.can_allocate

    1. Ok: seq_group can be allocated now.
    2. Later: seq_group cannot be allocated.
      The capacity of allocator is larger than seq_group required.
    3. Never: seq_group can never be allocated.
      The seq_group is too large to allocated in GPU.
    """
    OK = enum.auto()
    LATER = enum.auto()
    NEVER = enum.auto()


class BlockSpaceManager(ABC):

    @staticmethod
    def get_block_space_manager_class(version: str):
        version = version.lower()

31
32
33
        if version == "selfattn":
            from vllm.core.block_manager import SelfAttnBlockSpaceManager
            return SelfAttnBlockSpaceManager
34

35
36
37
38
        if version == "placeholder":
            from vllm.core.placeholder_block_space_manager import (
                PlaceholderBlockSpaceManager)
            return PlaceholderBlockSpaceManager
39

40
41
42
        raise ValueError(f"Unknown version {version=}")

    @abstractmethod
43
44
45
    def can_allocate(self,
                     seq_group: SequenceGroup,
                     num_lookahead_slots: int = 0) -> AllocStatus:
46
47
48
49
50
51
52
        pass

    @abstractmethod
    def allocate(self, seq_group: SequenceGroup) -> None:
        pass

    @abstractmethod
53
54
    def can_append_slots(self, seq_group: SequenceGroup,
                         num_lookahead_slots: int) -> bool:
55
56
57
        pass

    @abstractmethod
58
    def append_slots(
59
60
        self,
        seq: Sequence,
61
        num_lookahead_slots: int,
62
    ) -> List[Tuple[int, int]]:
63
64
65
66
67
68
69
        pass

    @abstractmethod
    def fork(self, parent_seq: Sequence, child_seq: Sequence) -> None:
        pass

    @abstractmethod
70
    def can_swap_in(self, seq_group: SequenceGroup,
71
                    num_lookahead_slots: int) -> AllocStatus:
72
73
74
        pass

    @abstractmethod
75
    def swap_in(self, seq_group: SequenceGroup) -> List[Tuple[int, int]]:
76
77
78
79
80
81
82
        pass

    @abstractmethod
    def can_swap_out(self, seq_group: SequenceGroup) -> bool:
        pass

    @abstractmethod
83
    def swap_out(self, seq_group: SequenceGroup) -> List[Tuple[int, int]]:
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        pass

    @abstractmethod
    def free(self, seq: Sequence) -> None:
        pass

    @abstractmethod
    def get_block_table(self, seq: Sequence) -> List[int]:
        pass

    @abstractmethod
    def get_num_free_gpu_blocks(self) -> int:
        pass

    @abstractmethod
    def get_num_free_cpu_blocks(self) -> int:
        pass

    @abstractmethod
    def access_all_blocks_in_seq(
        self,
        seq: Sequence,
        access_time: float,
    ) -> None:
        pass

    @abstractmethod
111
112
    def get_common_computed_block_ids(
            self, seqs: List[Sequence]) -> GenericSequence[int]:
113
114
115
        pass

    @abstractmethod
116
117
    def mark_blocks_as_computed(self, seq_group: SequenceGroup,
                                token_chunk_size: int):
118
        pass
119
120
121
122
123

    @abstractmethod
    def get_prefix_cache_hit_rate(self, device: Device) -> float:
        """Prefix cache hit rate. -1 means not supported or disabled."""
        pass