vllm.core.block_manager
A block manager that manages token blocks.
SelfAttnBlockSpaceManager
¶
Bases: BlockSpaceManager
BlockSpaceManager which manages the allocation of KV cache.
It owns responsibility for allocation, swapping, allocating memory for autoregressively-generated tokens, and other advanced features such as prefix caching, forking/copy-on-write, and sliding-window memory allocation.
This class implements the design described in https://github.com/vllm-project/vllm/pull/3492.
Lookahead slots The block manager has the notion of a "lookahead slot". These are slots in the KV cache that are allocated for a sequence. Unlike the other allocated slots, the content of these slots is undefined -- the worker may use the memory allocations in any way.
In practice, a worker could use these lookahead slots to run multiple
forward passes for a single scheduler invocation. Each successive
forward pass would write KV activations to the corresponding lookahead
slot. This allows low inter-token latency use-cases, where the overhead
of continuous batching scheduling is amortized over >1 generated tokens.
Speculative decoding uses lookahead slots to store KV activations of
proposal tokens.
See https://github.com/vllm-project/vllm/pull/3250 for more information
on lookahead scheduling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block_size
|
int
|
The size of each memory block. |
required |
num_gpu_blocks
|
int
|
The number of memory blocks allocated on GPU. |
required |
num_cpu_blocks
|
int
|
The number of memory blocks allocated on CPU. |
required |
watermark
|
float
|
The threshold used for memory swapping. Defaults to 0.01. |
0.01
|
sliding_window
|
Optional[int]
|
The size of the sliding window. Defaults to None. |
None
|
enable_caching
|
bool
|
Flag indicating whether caching is enabled. Defaults to False. |
False
|
Source code in vllm/core/block_manager.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
|
_computed_blocks_tracker
instance-attribute
¶
_computed_blocks_tracker = ComputedBlocksTracker(
block_allocator, block_size, enable_caching
)
_last_access_blocks_tracker
instance-attribute
¶
_last_access_blocks_tracker = LastAccessBlocksTracker(
block_allocator
)
block_allocator
instance-attribute
¶
block_allocator = create(
allocator_type="prefix_caching"
if enable_caching
else "naive",
num_gpu_blocks=num_gpu_blocks,
num_cpu_blocks=num_cpu_blocks,
block_size=block_size,
)
__init__
¶
__init__(
block_size: int,
num_gpu_blocks: int,
num_cpu_blocks: int,
watermark: float = 0.01,
sliding_window: Optional[int] = None,
enable_caching: bool = False,
) -> None
Source code in vllm/core/block_manager.py
_allocate_sequence
¶
_allocate_sequence(seq: Sequence) -> BlockTable
Source code in vllm/core/block_manager.py
_can_swap
¶
_can_swap(
seq_group: SequenceGroup,
device: Device,
status: SequenceStatus,
num_lookahead_slots: int = 0,
) -> AllocStatus
Returns the AllocStatus for swapping in/out the given sequence_group on to the 'device'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence_group
|
SequenceGroup
|
The sequence group to swap in/out. |
required |
device
|
Device
|
device to swap the 'seq_group' on. |
required |
status
|
SequenceStatus
|
The status of sequence which is needed for action. RUNNING for swap out and SWAPPED for swap in |
required |
num_lookahead_slots
|
int
|
Number of lookahead slots used in speculative decoding, default to 0. |
0
|
Returns:
Name | Type | Description |
---|---|---|
AllocStatus |
AllocStatus
|
The AllocStatus for swapping in/out the given sequence_group on to the 'device'. |
Source code in vllm/core/block_manager.py
access_all_blocks_in_seq
¶
Source code in vllm/core/block_manager.py
allocate
¶
allocate(seq_group: SequenceGroup) -> None
Source code in vllm/core/block_manager.py
append_slots
¶
Source code in vllm/core/block_manager.py
can_allocate
¶
can_allocate(
seq_group: SequenceGroup, num_lookahead_slots: int = 0
) -> AllocStatus
Source code in vllm/core/block_manager.py
can_append_slots
¶
can_append_slots(
seq_group: SequenceGroup, num_lookahead_slots: int
) -> bool
Determine if there is enough space in the GPU KV cache to continue generation of the specified sequence group.
We use a worst-case heuristic: assume each touched block will require a new allocation (either via CoW or new block). We can append slots if the number of touched blocks is less than the number of free blocks.
"Lookahead slots" are slots that are allocated in addition to the slots for known tokens. The contents of the lookahead slots are not defined. This is used by speculative decoding when speculating future tokens.
Source code in vllm/core/block_manager.py
can_swap_in
¶
can_swap_in(
seq_group: SequenceGroup, num_lookahead_slots: int
) -> AllocStatus
Returns the AllocStatus for the given sequence_group with num_lookahead_slots.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence_group
|
SequenceGroup
|
The sequence group to swap in. |
required |
num_lookahead_slots
|
int
|
Number of lookahead slots used in speculative decoding, default to 0. |
required |
Returns:
Name | Type | Description |
---|---|---|
AllocStatus |
AllocStatus
|
The AllocStatus for the given sequence group. |
Source code in vllm/core/block_manager.py
can_swap_out
¶
can_swap_out(seq_group: SequenceGroup) -> bool
Returns whether we can swap out the given sequence_group with num_lookahead_slots.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq_group
|
SequenceGroup
|
The sequence group to swap out. |
required |
num_lookahead_slots
|
int
|
Number of lookahead slots used in speculative decoding, default to 0. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
Whether it's possible to swap out current sequence group. |
Source code in vllm/core/block_manager.py
fork
¶
Source code in vllm/core/block_manager.py
free
¶
free(seq: Sequence) -> None
Source code in vllm/core/block_manager.py
get_block_table
¶
get_common_computed_block_ids
¶
Determine which blocks for which we skip prefill.
With prefix caching we can skip prefill for previously-generated blocks. Currently, the attention implementation only supports skipping cached blocks if they are a contiguous prefix of cached blocks.
This method determines which blocks can be safely skipped for all sequences in the sequence group.
Source code in vllm/core/block_manager.py
get_cross_block_table
¶
get_cross_block_table(
seq_group: SequenceGroup,
) -> List[int]
Source code in vllm/core/block_manager.py
get_num_cached_tokens
¶
Get the number of tokens in blocks that are already computed and cached in the block manager for the sequence.
get_prefix_cache_hit_rate
¶
mark_blocks_as_computed
¶
mark_blocks_as_computed(
seq_group: SequenceGroup, token_chunk_size: int
)
Source code in vllm/core/block_manager.py
remove_seq_from_computed_blocks_tracker
¶
remove_seq_from_computed_blocks_tracker(
seq: Sequence,
) -> None
reset_prefix_cache
¶
swap_in
¶
swap_in(seq_group: SequenceGroup) -> List[Tuple[int, int]]
Returns the block id mapping (from CPU to GPU) generated by swapping in the given seq_group with num_lookahead_slots.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq_group
|
SequenceGroup
|
The sequence group to swap in. |
required |
Returns:
Type | Description |
---|---|
List[Tuple[int, int]]
|
List[Tuple[int, int]]: The mapping of swapping block from CPU to GPU. |
Source code in vllm/core/block_manager.py
swap_out
¶
swap_out(seq_group: SequenceGroup) -> List[Tuple[int, int]]
Returns the block id mapping (from GPU to CPU) generated by swapping out the given sequence_group with num_lookahead_slots.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence_group
|
SequenceGroup
|
The sequence group to swap out. |
required |
Returns:
Type | Description |
---|---|
List[Tuple[int, int]]
|
List[Tuple[int, int]]: The mapping of swapping block from GPU to CPU. |