vllm.distributed.kv_transfer.kv_connector.utils ¶
KV cache helper for store.
EngineTransferInfo dataclass ¶
Common per-remote-engine transfer state, computed at handshake.
Stored per engine_id inside TransferTopology._engines.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
KVOutputAggregator ¶
Utility class to aggregate the output of all workers into a single output corresponding to Rank 0 for scheduler.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
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 | |
TransferTopology dataclass ¶
Single source of truth for local TP identity and per-engine remote info.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
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 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 590 591 592 593 594 595 596 597 598 599 600 601 | |
local_replicates_kv_cache property ¶
local_replicates_kv_cache: bool
Whether the local engine's KV cache is replicated.
block_size_ratio ¶
Calculate the block size ratio between local and remote.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
describe ¶
describe(remote_engine_id: EngineId) -> str
One-line summary of transfer config for logging.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
get_transfer_cache_regions ¶
get_transfer_cache_regions(
cache: Tensor, layer_spec: KVCacheSpec
) -> list[Tensor] | Tensor
Return the cache tensor(s) to register as NIXL memory regions, also accounting for hybrid SSM models specificities.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
handshake_target_ranks ¶
Pre-registration: compute which remote TP ranks to handshake with.
Pure math based on local/remote TP sizes — does not require the remote engine to be registered yet.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
is_kv_replicated ¶
is_kv_replicated(remote_engine_id: EngineId) -> bool
Whether the KV cache is replicated across TP workers due to the number of TP workers being greater than the number of KV heads.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
register_remote_engine ¶
register_remote_engine(
remote_engine_id: EngineId, info: EngineTransferInfo
) -> EngineTransferInfo
Register a remote engine, unifying worker dicts state.
The caller (worker) is responsible for computing the info via the transfer policy. This method only stores and deduplicates.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
target_remote_ranks ¶
Get the remote TP rank(s) that the current local TP rank will read from. When remote tp_size > local tp_size, reads from multiple remote ranks.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
tp_ratio ¶
Calculate the tensor parallel ratio between local and remote TP.
Positive when local_tp >= remote_tp (local workers read from the same remote worker in groups of size tp_ratio). Negative when remote_tp > local_tp (ratio is flipped).
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
copy_kv_blocks ¶
copy_kv_blocks(
src_kv_caches: dict[str, Tensor],
dst_kv_caches: dict[str, Tensor],
src_block_ids: list[int],
dst_block_ids: list[int],
direction: Literal["h2d", "d2h"],
) -> None
Copy kv blocks between different buffers.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
get_current_attn_backend ¶
get_current_attn_backend(
vllm_config: VllmConfig,
layer_names: list[str] | None = None,
) -> type[AttentionBackend]
Get the first attention backend for the given layers.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
get_current_attn_backends ¶
get_current_attn_backends(
vllm_config: VllmConfig,
layer_names: list[str] | None = None,
) -> list[type[AttentionBackend]]
Get all distinct attention backends for the given layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vllm_config | VllmConfig | The current vLLM configuration. | required |
layer_names | list[str] | None | Optional list of layer names to scope the lookup. When None, all attention layers are considered. | None |
Returns:
| Type | Description |
|---|---|
list[type[AttentionBackend]] | Deduplicated list of attention backend classes. |
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
kv_postprocess_blksize_and_layout_on_receive ¶
Transforms the layout of received KV cache to the local block_size and HND. (Only works for local blocksize > remote blocksize)
prefill is HND, smaller block_size decode(local) is NHD, larger block_size
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
kv_postprocess_blksize_on_receive ¶
Transforms the layout of received KV cache blocks to the local block_size. (Only works for local blocksize > remote blocksize)
example: local blocksize = 16 tokens, remote blocksize = 4 tokens local block[0] = remote block[0, 1, 2, 3] remote is |h0-b0|h1-b0|h2-b0|h3-b0|h0-b1|h1-b1|h2-b1|h3-b1|... local is |h0-b0..................|h1-b0..................|... permute is to: 1. view => view remote as n_blocks * remote_shape(H,remoteN,D) 2. permute => (H, nblocks, remoteN, D) 3. flatten => (H, localN, D)
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
kv_postprocess_layout_on_receive ¶
Transforms the layout of received KV cache blocks to the local format.
This method corrects layout mismatches from direct memory copies by permuting the tensor dimensions.
- Source Layout:
[num_blocks, n_kv_head, block_size, head_dim] - Target Layout:
[num_blocks, block_size, n_kv_head, head_dim]
Implementation: - x = blocks_to_update.reshape(src_shape) # view local kv with sender layout - permuted_blocks = x.permute(*inv_order) # transpose n_kv_heads, block_size - cache.index_copy_(0, indices, permuted_blocks) # copy permuted kv back
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
yield_req_data ¶
Yields:
| Type | Description |
|---|---|
tuple[str, tuple[list[int], ...] | None, bool] | (req_id, new_block_id_groups, preempted) |