vllm.core.block.block_table
BlockTable
¶
A class to manage blocks for a specific sequence.
The BlockTable maps a sequence of tokens to a list of blocks, where each block represents a contiguous memory allocation for a portion of the sequence. The blocks are managed by a DeviceAwareBlockAllocator, which is responsible for allocating and freeing memory for the blocks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block_size
|
int
|
The maximum number of tokens that can be stored in a single block. |
required |
block_allocator
|
DeviceAwareBlockAllocator
|
The block allocator used to manage memory for the blocks. |
required |
_blocks
|
Optional[List[Block]]
|
An optional list of existing blocks to initialize the BlockTable with. If not provided, an empty BlockTable is created. |
None
|
max_block_sliding_window
|
Optional[int]
|
The number of blocks to keep around for each sequence. If None, all blocks are kept (eg., when sliding window is not used). It should at least fit the sliding window size of the model. |
None
|
Attributes:
Name | Type | Description |
---|---|---|
_block_size |
int
|
The maximum number of tokens that can be stored in a single block. |
_allocator |
DeviceAwareBlockAllocator
|
The block allocator used to manage memory for the blocks. |
_blocks |
Optional[List[Block]]
|
The list of blocks managed by this BlockTable. |
_num_full_slots |
int
|
The number of tokens currently stored in the blocks. |
Source code in vllm/core/block/block_table.py
12 13 14 15 16 17 18 19 20 21 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 |
|
num_full_slots
property
¶
num_full_slots: int
Returns the total number of tokens currently stored in the BlockTable.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The total number of tokens currently stored in the BlockTable. |
physical_block_ids
property
¶
Returns a list of physical block indices for the blocks in the BlockTable.
This property returns a list of integers, where each integer represents
the physical block index of a corresponding block in the _blocks
list.
The physical block index is a unique identifier for the memory location
occupied by the block.
Returns:
Type | Description |
---|---|
List[int]
|
List[int]: A list of physical block indices for the blocks in the BlockTable. |
__init__
¶
__init__(
block_size: int,
block_allocator: DeviceAwareBlockAllocator,
_blocks: Optional[List[Block]] = None,
max_block_sliding_window: Optional[int] = None,
)
Source code in vllm/core/block/block_table.py
_allocate_blocks_for_token_ids
¶
_allocate_blocks_for_token_ids(
prev_block: Optional[Block],
token_ids: List[int],
device: Device,
extra_hash: Optional[int] = None,
) -> List[Block]
Source code in vllm/core/block/block_table.py
_chunk_token_blocks_for_append
¶
Split the token ids into block-sized chunks so they can be easily appended to blocks. The first such "token block" may have less token ids than the block size, since the last allocated block may be partially full.
If no token ids are provided, then no chunks are returned.
Source code in vllm/core/block/block_table.py
_get_all_token_ids
¶
Source code in vllm/core/block/block_table.py
allocate
¶
Allocates memory blocks for storing the given sequence of token IDs.
This method allocates the required number of blocks to store the given sequence of token IDs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_ids
|
List[int]
|
The sequence of token IDs to be stored. |
required |
device
|
Device
|
The device on which the blocks should be allocated. Defaults to Device.GPU. |
GPU
|
extra_hash
|
Optional[int]
|
The hash value of additional factors, such as adapters, that influence the block hash in the prefixcaching block. |
None
|
Source code in vllm/core/block/block_table.py
append_token_ids
¶
append_token_ids(
token_ids: List[int],
num_lookahead_slots: int = 0,
num_computed_slots: Optional[int] = None,
extra_hash: Optional[int] = None,
) -> None
Appends a sequence of token IDs to the existing blocks in the BlockTable.
This method appends the given sequence of token IDs to the existing
blocks in the BlockTable. If there is not enough space in the existing
blocks, new blocks are allocated using the ensure_num_empty_slots
method to accommodate the additional tokens.
The token IDs are divided into chunks of size block_size
(except for
the first chunk, which may be smaller), and each chunk is appended to a
separate block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_ids
|
List[int]
|
The sequence of token IDs to be appended. |
required |
num_computed_slots
|
Optional[int]
|
The number of KV cache slots that are already filled (computed). When sliding window is enabled, this is used to compute how many blocks to drop at the front of the sequence. Without sliding window, None can be passed. Without chunked prefill, it should be the same as _num_full_slots. |
None
|
extra_hash
|
Optional[int]
|
The hash value of additional factors such as adapters that influence the block, apart from the token_ids. |
None
|
Source code in vllm/core/block/block_table.py
ensure_num_empty_slots
¶
Ensures that the BlockTable has at least the specified number of empty slots available.
This method checks if the BlockTable has enough empty slots (i.e., available space) to accommodate the requested number of tokens. If not, it allocates additional blocks on the GPU to ensure that the required number of empty slots is available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_empty_slots
|
int
|
The minimum number of empty slots required. |
required |
extra_hash
|
Optional[int]
|
The hash value of additional factors such as adapters that influence the block, apart from the token_ids. |
None
|
Source code in vllm/core/block/block_table.py
fork
¶
fork() -> BlockTable
Creates a new BlockTable instance with a copy of the blocks from the current instance.
This method creates a new BlockTable instance with the same block size, block allocator, and a copy of the blocks from the current instance. The new BlockTable has its own independent set of blocks, but shares the same underlying memory allocation with the original BlockTable.
Returns:
Name | Type | Description |
---|---|---|
BlockTable |
BlockTable
|
A new BlockTable instance with a copy of the blocks from the current instance. |
Source code in vllm/core/block/block_table.py
free
¶
Frees the memory occupied by the blocks in the BlockTable.
This method iterates over all the blocks in the _blocks
list and calls
the free
method of the _allocator
object to release the memory
occupied by each block. After freeing all the blocks, the _blocks
list
is set to None
.
Source code in vllm/core/block/block_table.py
get_num_blocks_touched_by_append_slots
¶
Determine how many blocks will be "touched" by appending the token ids.
This is required for the scheduler to determine whether a sequence can continue generation, or if it must be preempted.
Source code in vllm/core/block/block_table.py
get_num_required_blocks
staticmethod
¶
get_num_required_blocks(
token_ids: List[int],
block_size: int,
num_lookahead_slots: int = 0,
) -> int
Calculates the minimum number of blocks required to store a given sequence of token IDs along with any look-ahead slots that may be required (like in multi-step + chunked-prefill).
This assumes worst-case scenario, where every block requires a new allocation (e.g. ignoring prefix caching).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_ids
|
List[int]
|
The sequence of token IDs to be stored. |
required |
block_size
|
int
|
The maximum number of tokens that can be stored in a single block. |
required |
num_lookahead_slots
|
int
|
look-ahead slots that the sequence may require. |
0
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The minimum number of blocks required to store the given sequence of token IDs along with any required look-ahead slots. |
Source code in vllm/core/block/block_table.py
get_unseen_token_ids
¶
Get the number of "unseen" tokens in the sequence.
Unseen tokens are tokens in the sequence corresponding to this block table, but are not yet appended to this block table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sequence_token_ids
|
List[int]
|
The list of token ids in the sequence. |
required |
Returns:
Type | Description |
---|---|
List[int]
|
List[int]: The postfix of sequence_token_ids that has not yet been appended to the block table. |
Source code in vllm/core/block/block_table.py
update
¶
Resets the table to the newly provided blocks (with their corresponding block ids)