vllm.v1.worker.extensible_kv_cache
¶
KV cache backing allocation that grows after warmup.
Classes:
-
ExtensibleKVCache–Growable backing allocation for the KV cache.
Functions:
-
ensure_kv_cache_blocks–Commit enough of an extensible KV cache to address num_blocks.
-
extend_kv_cache–Commit the final size once post-warmup memory is known.
-
kv_cache_lent_for_weight_update–Make room for a layerwise weight update of model within the context.
-
measure_kv_cache_blocks–Size an extensible KV cache from the memory free after warmup.
-
measure_kv_cache_bytes–Bytes the KV cache may occupy given the memory measured after warmup.
-
num_committable_kv_blocks–Blocks warmup may address: all of them, or, for an extensible cache,
-
pad_kv_cache_segments–Start every layer's region on a granule, so that a defragmenting commit
-
registers_kv_cache_memory–Whether a KV connector registers the committed KV cache for RDMA, which
ExtensibleKVCache
¶
Growable backing allocation for the KV cache.
Reserves address space for kv_cache_config.num_blocks and commits a
prefix of blocks on demand. Blocks keep fixed offsets, so views over the
reservation and captured CUDA graphs stay valid as more are committed. The
allocation splits into segments holding num_blocks blocks each: one per
layer for block-compact layer-outermost layouts (sized by that layer's
block stride, possibly padded apart by pad_kv_cache_segments), one
overall for block-outermost.
Methods:
-
allocate–Allocator hook for allocate_kv_cache; commits only the null block.
-
blocks_within–Most blocks whose physical footprint fits in num_bytes.
-
commit–Back the first num_blocks blocks; new blocks are zeroed.
-
committable_blocks–Blocks that can be committed now without eating into the headroom.
-
committed_kv_cache_tensors–Tensor placements matching committed_views for num_blocks.
-
committed_views–Per-layer views whose storage spans exactly the committed blocks.
-
physical_bytes_for–Bytes a commit of num_blocks maps, each segment rounded to granules.
-
recommit–Map fresh zeroed pages for the blocks committed before release.
-
release_physical–Drop physical pages for sleep; addresses and views stay valid.
-
set_torch_memory_limit–Cap what torch's caching allocator reserves on this device, or lift
Attributes:
-
physical_bytes(int) –Physically mapped bytes, including granule rounding.
Source code in vllm/v1/worker/extensible_kv_cache.py
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 | |
physical_bytes
property
¶
Physically mapped bytes, including granule rounding.
_locate(start)
¶
Segment index and offset within it of allocation byte start.
_segments(tensors, layout)
¶
Segment offsets and block strides covering the allocation.
Block-compact layer-outermost layouts give each layer a num_blocks-block region, which may be padded apart. Otherwise the segments are implied by the one block stride.
Source code in vllm/v1/worker/extensible_kv_cache.py
allocate(size)
¶
Allocator hook for allocate_kv_cache; commits only the null block.
Source code in vllm/v1/worker/extensible_kv_cache.py
blocks_within(num_bytes)
¶
Most blocks whose physical footprint fits in num_bytes.
Source code in vllm/v1/worker/extensible_kv_cache.py
commit(num_blocks, defragment=False, shrink=False)
¶
Back the first num_blocks blocks; new blocks are zeroed.
Grow-only unless shrink is set, which remaps a smaller prefix and discards contents. defragment remaps each segment as one driver allocation (discarding contents): UCX cannot RDMA a range spanning several allocations.
Source code in vllm/v1/worker/extensible_kv_cache.py
committable_blocks()
¶
Blocks that can be committed now without eating into the headroom.
Bounds what warmup may commit: free memory plus the committed prefix, less the sizing margin and the larger of reserved_headroom_bytes and a share of the headroom, never below what is already committed nor above the capacity.
Source code in vllm/v1/worker/extensible_kv_cache.py
committed_kv_cache_tensors(kv_cache_config, layout, num_blocks)
¶
Tensor placements matching committed_views for num_blocks.
size is the committed total, as every tensor describes the one allocation. Under block-outermost layouts the committed blocks are one prefix of it, so the capacity placements still hold. Under layer-outermost layouts each layer's view has its own storage over its committed prefix: describe it as a one-layer tensor placed at the start of that storage.
Source code in vllm/v1/worker/extensible_kv_cache.py
committed_views(kv_cache_config, layout, kernel_block_sizes=None)
¶
Per-layer views whose storage spans exactly the committed blocks.
kv_cache_config.num_blocks must equal the committed count. Connectors
derive extents from untyped_storage().nbytes(), so they must not see the
reservation. None for layouts whose layers span several segments.
Source code in vllm/v1/worker/extensible_kv_cache.py
physical_bytes_for(num_blocks)
¶
Bytes a commit of num_blocks maps, each segment rounded to granules.
Source code in vllm/v1/worker/extensible_kv_cache.py
recommit()
¶
Map fresh zeroed pages for the blocks committed before release.
release_physical()
¶
set_torch_memory_limit(num_bytes)
¶
Cap what torch's caching allocator reserves on this device, or lift the cap (None). At the cap it releases cached segments before growing, rather than taking memory other allocations need.
Source code in vllm/v1/worker/extensible_kv_cache.py
_release_for_weight_update(kv_cache, model, *, reclaimable, fail_if_short)
¶
Whether kv_cache was released to fit the update; see the caller.
Source code in vllm/v1/worker/extensible_kv_cache.py
ensure_kv_cache_blocks(runner, num_blocks)
¶
Commit enough of an extensible KV cache to address num_blocks.
Source code in vllm/v1/worker/extensible_kv_cache.py
extend_kv_cache(runner, kv_cache_config, num_blocks)
¶
Commit the final size once post-warmup memory is known.
Afterwards the runner matches one that allocated num_blocks up front: views are rebuilt over the committed bytes (same addresses) and the KV connector is created against final memory. kv_cache_config is the engine's config the cache was allocated from.
Source code in vllm/v1/worker/extensible_kv_cache.py
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 | |
kv_cache_lent_for_weight_update(kv_cache, model, *, reclaimable, fail_if_short)
¶
Make room for a layerwise weight update of model within the context.
Only an extensible KV cache (kv_cache, None for a standard one) leaves the free memory short, since it keeps free just the transients warmup measured. When short, release the cache's physical memory for the context if it holds nothing to keep (reclaimable); else fail before any weight changes, when not even the largest layer fits and fail_if_short, or warn. Torch's memory cap is lifted for the context.
Source code in vllm/v1/worker/extensible_kv_cache.py
measure_kv_cache_blocks(kv_cache, vllm_config, device, *, init_free_memory, requested_memory)
¶
Size an extensible KV cache from the memory free after warmup.
Keeps free reserved_headroom_bytes, the larger of the profiled peak and warmup's allocated (not reserved) transient peak. Torch's cache is emptied first: its blocks may not fit the large workspaces the first real steps allocate. Then caps torch's caching allocator to leave KV_CACHE_NON_TORCH_RESERVE_BYTES free for allocations outside it.
Source code in vllm/v1/worker/extensible_kv_cache.py
measure_kv_cache_bytes(*, init_free_memory, free_memory, committed_bytes, requested_memory, transient_peak_bytes=0, margin_floor_bytes=KV_CACHE_MARGIN_FLOOR_BYTES, margin_fraction=KV_CACHE_MARGIN_FRACTION)
¶
Bytes the KV cache may occupy given the memory measured after warmup.
Everything resident except the committed KV prefix is needed by the engine;
the cache gets the rest of the budget, capped by the device headroom (free
memory plus the committed prefix) less a margin of
max(margin_floor_bytes, margin_fraction * transient_peak_bytes). The
measured transient_peak_bytes are then kept free in full. The margin
guards physical memory, so it never reduces an explicit budget that already
leaves that much free.
Source code in vllm/v1/worker/extensible_kv_cache.py
num_committable_kv_blocks(runner)
¶
Blocks warmup may address: all of them, or, for an extensible cache, the count agreed across ranks so that every rank warms up the same shapes.
Source code in vllm/v1/worker/extensible_kv_cache.py
pad_kv_cache_segments(kv_cache_config, layout, granule)
¶
Start every layer's region on a granule, so that a defragmenting commit of any block count can back each segment with one physical chunk.
Only address space grows: commits already map whole granules per segment. Block-outermost layouts have a single segment at offset 0 and are returned as is.
Source code in vllm/v1/worker/extensible_kv_cache.py
registers_kv_cache_memory(vllm_config)
¶
Whether a KV connector registers the committed KV cache for RDMA, which needs each segment backed by one physical chunk.