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_export_handle_types–Handle types other processes import the KV cache through, for
-
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.
-
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. Host-resident tensors live outside it.
Methods:
-
allocate–Allocator hook for allocate_kv_cache; commits the minimum.
-
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.
-
record_transient_peak–Fold the allocated transient since peak tracking last restarted
-
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:
-
min_committed_blocks(int) –The null block, plus one under block-outermost layouts, whose views
-
physical_bytes(int) –Physically mapped bytes, including granule rounding.
Source code in vllm/v1/worker/extensible_kv_cache.py
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 | |
min_committed_blocks
property
¶
The null block, plus one under block-outermost layouts, whose views
start inside block 0 and read past it (see pad_kv_cache_segments).
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 the minimum.
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 device tensor describes the one allocation; host-resident tensors are unchanged. 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. Host-resident layers are omitted. 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.
record_transient_peak()
¶
Fold the allocated transient since peak tracking last restarted into reserved_headroom_bytes; returns the allocator stats.
Source code in vllm/v1/worker/extensible_kv_cache.py
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
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 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | |
kv_cache_export_handle_types(vllm_config)
¶
Handle types other processes import the KV cache through, for
VmmDriver.create.
Source code in vllm/v1/worker/extensible_kv_cache.py
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 the larger of reserved_headroom_bytes (the profiled or warmup allocated peak) and torch_reserve_growth_bytes. 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
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 | |
measure_kv_cache_bytes(*, init_free_memory, free_memory, committed_bytes, requested_memory, transient_peak_bytes=0, torch_reserve_growth_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
larger of transient_peak_bytes and torch_reserve_growth_bytes is 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, *, registers_memory=False)
¶
Start every layer's region on a granule.
Committing the null block then backs each layer's first granule, which TMA gathers with out-of-bounds indices touch beyond the rows they address. It also lets a defragmenting commit of any block count back each segment with one physical chunk, as RDMA registration (registers_memory) requires.
Only address space grows: commits already map whole granules per segment.
Block-outermost layouts (one segment at offset 0, see allocate) are
returned as is, and so are other layouts that are not block-compact, unless
registers_memory.
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.