interfaces.py 8.02 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
from abc import ABC, abstractmethod
5
from typing import Dict, FrozenSet, List, Optional, Protocol, Tuple
6
7
8

from vllm.utils import Device

9
10
BlockId = int

11
12
13
14
15
16
17

class Block(ABC):

    @abstractmethod
    def append_token_ids(self, token_ids: List[int]) -> None:
        pass

18
19
    @property
    @abstractmethod
20
21
22
    def block_id(self) -> Optional[int]:
        pass

23
24
25
26
27
28
    @block_id.setter
    @abstractmethod
    def block_id(self, value: Optional[int]) -> None:
        """NOTE: Do not use this API outside Block."""
        self._block_id = value

29
30
    @property
    @abstractmethod
31
32
33
    def token_ids(self) -> List[int]:
        pass

34
35
36
37
38
39
40
    @property
    @abstractmethod
    def num_tokens_total(self) -> int:
        """The number of tokens till the current block (inclusive)
        """
        pass

41
42
    @property
    @abstractmethod
43
44
45
    def num_empty_slots(self) -> int:
        pass

46
47
    @property
    @abstractmethod
48
49
50
    def is_full(self) -> bool:
        pass

51
52
    @property
    @abstractmethod
53
54
55
    def prev_block(self) -> Optional["Block"]:
        pass

56
57
58
59
60
    @property
    @abstractmethod
    def extra_hash(self) -> Optional[int]:
        return None

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    @property
    @abstractmethod
    def computed(self) -> bool:
        raise NotImplementedError

    @computed.setter
    @abstractmethod
    def computed(self, value) -> bool:
        """Should be only used by PrefixCacingAllocator"""
        raise NotImplementedError

    @property
    @abstractmethod
    def last_accessed(self) -> float:
        raise NotImplementedError

    @last_accessed.setter
    @abstractmethod
    def last_accessed(self, last_accessed_ts: float):
        raise NotImplementedError

82
83
84
85
86
87
88
89
90
91
    class Factory(Protocol):

        @abstractmethod
        def __call__(
            self,
            prev_block: Optional["Block"],
            token_ids: List[int],
            block_size: int,
            allocator: "BlockAllocator",
            block_id: Optional[int] = None,
92
93
            computed: bool = False,
            extra_hash: Optional[int] = None,
94
95
96
        ) -> "Block":
            pass

97
98
99
100
101
102
103
104
105
106
107
    @property
    @abstractmethod
    def content_hash(self) -> Optional[int]:
        """Return the content-based hash of the current block, or None if it is
        not yet defined or not supported.

        For the content-based hash to be defined, the current block must be
        full.
        """
        return None

108
109
110
111

class BlockAllocator(ABC):

    @abstractmethod
112
113
    def allocate_mutable_block(self, prev_block: Optional[Block],
                               extra_hash: Optional[int]) -> Block:
114
115
116
        pass

    @abstractmethod
117
    def allocate_immutable_block(self, prev_block: Optional[Block],
118
119
                                 token_ids: List[int],
                                 extra_hash: Optional[int]) -> Block:
120
121
122
        pass

    @abstractmethod
123
124
125
    def allocate_immutable_blocks(self, prev_block: Optional[Block],
                                  block_token_ids: List[List[int]],
                                  extra_hash: Optional[int]) -> List[Block]:
126
127
128
129
130
131
132
133
134
135
        pass

    @abstractmethod
    def free(self, block: Block) -> None:
        pass

    @abstractmethod
    def fork(self, last_block: Block) -> List[Block]:
        pass

136
137
138
139
    @abstractmethod
    def get_num_total_blocks(self) -> int:
        pass

140
    @abstractmethod
141
    def get_num_free_blocks(self) -> int:
142
143
        pass

144
145
146
147
148
149
150
151
152
153
154
155
    @abstractmethod
    def get_physical_block_id(self, absolute_id: int) -> int:
        pass

    @abstractmethod
    def swap_out(self, blocks: List[Block]) -> None:
        pass

    @abstractmethod
    def swap_in(self, blocks: List[Block]) -> None:
        pass

156
157
158
    @property
    @abstractmethod
    def all_block_ids(self) -> FrozenSet[int]:
159
160
161
        pass

    @abstractmethod
162
    def clear_copy_on_writes(self) -> List[Tuple[int, int]]:
163
164
        pass

165
    @abstractmethod
166
167
    def mark_blocks_as_accessed(self, block_ids: List[int],
                                now: float) -> None:
168
169
        pass

170
    @abstractmethod
171
    def mark_blocks_as_computed(self, block_ids: List[int]) -> None:
172
173
174
175
        pass

    @abstractmethod
    def get_common_computed_block_ids(
176
            self, computed_seq_block_ids: List[List[int]]) -> List[int]:
177
178
        pass

179
    @abstractmethod
180
    def cow_block_if_not_appendable(self, block: Block) -> BlockId:
181
182
183
184
185
186
187
188
        """NOTE: This should not be used besides Block"""
        pass

    @abstractmethod
    def promote_to_immutable_block(self, block: Block) -> BlockId:
        """NOTE: This should not be used besides Block"""
        pass

189
    @abstractmethod
190
    def get_num_full_blocks_touched(self, blocks: List[Block]) -> int:
191
192
        pass

193
194
195
196
197
    @abstractmethod
    def get_prefix_cache_hit_rate(self) -> float:
        """Prefix cache hit rate. -1 means not supported or disabled."""
        pass

198
199
200
201
202
    @abstractmethod
    def reset_prefix_cache(self) -> bool:
        """Reset prefix cache."""
        pass

203
204
205
    class NoFreeBlocksError(ValueError):
        pass

206
207
208
209
210
211
212
    @abstractmethod
    def find_cached_blocks_prefix(
        self,
        block_hashes: List[int],
    ) -> List[int]:
        pass

213

214
class DeviceAwareBlockAllocator(ABC):
215
216

    @abstractmethod
217
218
219
220
    def allocate_mutable_block(self,
                               prev_block: Optional[Block],
                               device: Device,
                               extra_hash: Optional[int] = None) -> Block:
221
222
223
        pass

    @abstractmethod
224
225
    def allocate_immutable_block(self,
                                 prev_block: Optional[Block],
226
                                 token_ids: List[int],
227
228
                                 device: Device,
                                 extra_hash: Optional[int] = None) -> Block:
229
230
231
        pass

    @abstractmethod
232
233
234
235
236
    def allocate_immutable_blocks(
        self,
        prev_block: Optional[Block],
        block_token_ids: List[List[int]],
        device: Device,
237
        extra_hash: Optional[int] = None,
238
    ) -> List[Block]:
239
240
241
        pass

    @abstractmethod
242
    def get_num_free_blocks(self, device: Device) -> int:
243
244
245
        pass

    @abstractmethod
246
    def get_num_total_blocks(self, device: Device) -> int:
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
        pass

    @abstractmethod
    def free(self, block: Block) -> None:
        pass

    @abstractmethod
    def fork(self, last_block: Block) -> List[Block]:
        pass

    @property
    @abstractmethod
    def all_block_ids(self) -> FrozenSet[int]:
        pass

    @abstractmethod
263
    def clear_copy_on_writes(self) -> List[Tuple[int, int]]:
264
265
266
267
268
269
270
271
272
273
274
275
276
        pass

    @abstractmethod
    def mark_blocks_as_accessed(self, block_ids: List[int],
                                now: float) -> None:
        pass

    @abstractmethod
    def mark_blocks_as_computed(self, block_ids: List[int]) -> None:
        pass

    @abstractmethod
    def get_common_computed_block_ids(
277
            self, computed_seq_block_ids: List[List[int]]) -> List[int]:
278
        pass
279

280
    @abstractmethod
281
282
    def get_num_full_blocks_touched(self, blocks: List[Block],
                                    device: Device) -> int:
283
284
285
        pass

    @abstractmethod
286
287
    def swap(self, blocks: List[Block], src_device: Device,
             dst_device: Device) -> Dict[int, int]:
288
289
290
291
292
293
        pass

    @abstractmethod
    def get_physical_block_id(self, device: Device, absolute_id: int) -> int:
        pass

294
295
296
297
298
299
300
301
    @abstractmethod
    def allocate_or_get_null_block(self) -> Block:
        """
        Null blocks are used as a placeholders for KV cache blocks that have
        been dropped due to sliding window.
        There is at most one null block per allocator.
        """
        pass
302
303
304
305
306

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

308
    @abstractmethod
309
    def reset_prefix_cache(self, device: Optional[Device] = None) -> bool:
310
311
312
        """Reset prefix cache."""
        pass

313
314
315
316
317
318
319
    @abstractmethod
    def find_cached_blocks_prefix(
        self,
        block_hashes: List[int],
        device: Device = Device.GPU,
    ) -> List[int]:
        pass