vllm.core.block.cpu_gpu_block_allocator
CpuGpuBlockAllocator
¶
Bases: DeviceAwareBlockAllocator
A block allocator that can allocate blocks on both CPU and GPU memory.
This class implements the DeviceAwareBlockAllocator
interface and provides
functionality for allocating and managing blocks of memory on both CPU and
GPU devices.
The CpuGpuBlockAllocator
maintains separate memory pools for CPU and GPU
blocks, and allows for allocation, deallocation, forking, and swapping of
blocks across these memory pools.
Source code in vllm/core/block/cpu_gpu_block_allocator.py
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 |
|
_allocators
instance-attribute
¶
_block_ids_to_allocator
instance-attribute
¶
_block_ids_to_allocator: Dict[int, BlockAllocator] = {}
__init__
¶
__init__(
cpu_block_allocator: BlockAllocator,
gpu_block_allocator: BlockAllocator,
)
Source code in vllm/core/block/cpu_gpu_block_allocator.py
allocate_immutable_block
¶
allocate_immutable_block(
prev_block: Optional[Block],
token_ids: List[int],
device: Device,
extra_hash: Optional[int] = None,
) -> Block
Allocates a new immutable block with the provided token IDs on the specified device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prev_block
|
Optional[Block]
|
The previous block in the sequence. Used for prefix hashing. |
required |
token_ids
|
List[int]
|
The list of token IDs to be stored in the new block. |
required |
device
|
Device
|
The device on which to allocate the new block. |
required |
extra_hash
|
Optional[int]
|
The hash value of additional factors, such as adapters, that influence the block hash in the prefix caching block. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Block |
Block
|
The newly allocated immutable block containing the provided token IDs. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
allocate_immutable_blocks
¶
allocate_immutable_blocks(
prev_block: Optional[Block],
block_token_ids: List[List[int]],
device: Device,
extra_hash: Optional[int] = None,
) -> List[Block]
Allocates a new group of immutable blocks with the provided block token IDs on the specified device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prev_block
|
Optional[Block]
|
The previous block in the sequence. Used for prefix hashing. |
required |
block_token_ids
|
List[int]
|
The list of block token IDs to be stored in the new blocks. |
required |
device
|
Device
|
The device on which to allocate the new block. |
required |
extra_hash
|
Optional[int]
|
The hash value of additional factors, such as adapters, that influence the block hash in the prefix caching block. |
None
|
Returns:
Type | Description |
---|---|
List[Block]
|
List[Block]: The newly allocated list of immutable blocks containing the provided block token IDs. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
allocate_mutable_block
¶
allocate_mutable_block(
prev_block: Optional[Block],
device: Device,
extra_hash: Optional[int] = None,
) -> Block
Allocates a new mutable block on the specified device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prev_block
|
Optional[Block]
|
The previous block to in the sequence. Used for prefix hashing. |
required |
device
|
Device
|
The device on which to allocate the new block. |
required |
extra_hash
|
Optional[int]
|
The hash value of additional factors, such as adapters, that influence the block hash in the prefix caching block. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Block |
Block
|
The newly allocated mutable block. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
clear_copy_on_writes
¶
Clears the copy-on-write (CoW) state and returns the mapping of source to destination block IDs.
Returns:
Type | Description |
---|---|
List[Tuple[int, int]]
|
List[Tuple[int, int]]: A list mapping source block IDs to destination block IDs. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
create
staticmethod
¶
create(
allocator_type: str,
num_gpu_blocks: int,
num_cpu_blocks: int,
block_size: int,
) -> DeviceAwareBlockAllocator
Creates a CpuGpuBlockAllocator instance with the specified configuration.
This static method creates and returns a CpuGpuBlockAllocator instance based on the provided parameters. It initializes the CPU and GPU block allocators with the specified number of blocks, block size, and allocator type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
allocator_type
|
str
|
The type of block allocator to use for CPU and GPU blocks. Currently supported values are "naive" and "prefix_caching". |
required |
num_gpu_blocks
|
int
|
The number of blocks to allocate for GPU memory. |
required |
num_cpu_blocks
|
int
|
The number of blocks to allocate for CPU memory. |
required |
block_size
|
int
|
The size of each block in number of tokens. |
required |
Returns:
Name | Type | Description |
---|---|---|
DeviceAwareBlockAllocator |
DeviceAwareBlockAllocator
|
A CpuGpuBlockAllocator instance with the specified configuration. |
Notes
- The block IDs are assigned contiguously, with GPU block IDs coming before CPU block IDs.
Source code in vllm/core/block/cpu_gpu_block_allocator.py
find_cached_blocks_prefix
¶
fork
¶
Creates a new sequence of blocks that shares the same underlying memory as the original sequence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
last_block
|
Block
|
The last block in the original sequence. |
required |
Returns:
Type | Description |
---|---|
List[Block]
|
List[Block]: A new list of blocks that shares the same memory as the original sequence. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
free
¶
free(block: Block) -> None
Frees the memory occupied by the given block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block
|
Block
|
The block to be freed. |
required |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
get_and_reset_swaps
¶
Returns and clears the mapping of source to destination block IDs. Will be called after every swapping operations for now, and after every schedule when BlockManagerV2 become default. Currently not useful.
Returns:
Type | Description |
---|---|
List[Tuple[int, int]]
|
List[Tuple[int, int]]: A mapping of source to destination block IDs. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
get_common_computed_block_ids
¶
Source code in vllm/core/block/cpu_gpu_block_allocator.py
get_num_free_blocks
¶
Returns the number of free blocks available on the specified device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
Device
|
The device for which to query the number of free blocks. AssertionError is raised if None is passed. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of free blocks available on the specified device. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
get_num_full_blocks_touched
¶
Returns the number of full blocks that will be touched by swapping in/out the given blocks on to the 'device'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
blocks
|
List[Block]
|
List of blocks to be swapped. |
required |
device
|
Device
|
Device to swap the 'blocks' on. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
the number of full blocks that will be touched by swapping in/out the given blocks on to the 'device'. Non full blocks are ignored when deciding the number of blocks to touch. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
get_num_total_blocks
¶
get_physical_block_id
¶
Returns the zero-offset block id on certain device given the absolute block id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
Device
|
The device for which to query relative block id. absolute_id (int): The absolute block id for the block in whole allocator. |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The zero-offset block id on certain device. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
get_prefix_cache_hit_rate
¶
Prefix cache hit rate. -1 means not supported or disabled.
mark_blocks_as_accessed
¶
Mark blocks as accessed, only use for prefix caching.
Source code in vllm/core/block/cpu_gpu_block_allocator.py
mark_blocks_as_computed
¶
Mark blocks as accessed, only use for prefix caching.
Source code in vllm/core/block/cpu_gpu_block_allocator.py
reset_prefix_cache
¶
Reset prefix cache for specified or all devices.
Source code in vllm/core/block/cpu_gpu_block_allocator.py
swap
¶
Execute the swap for the given blocks from source_device
on to dest_device, save the current swap mapping and append
them to the accumulated self._swap_mapping
for each
scheduling move.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
blocks
|
List[Block]
|
List of blocks to be swapped. |
required |
src_device
|
Device
|
Device to swap the 'blocks' from. |
required |
dst_device
|
Device
|
Device to swap the 'blocks' to. |
required |
Returns:
Type | Description |
---|---|
Dict[int, int]
|
Dict[int, int]: Swap mapping from source_device on to dest_device. |
Source code in vllm/core/block/cpu_gpu_block_allocator.py
NullBlock
¶
Bases: Block
Null blocks are used as a placeholders for KV cache blocks that have been dropped due to sliding window. This implementation just wraps an ordinary block and prevents it from being modified. It also allows for testing if a block is NullBlock via isinstance().