vllm.distributed.kv_transfer.kv_connector.utils ¶
KV cache helper for store.
HeteroTPTransferConfig dataclass ¶
Precomputed transfer plan for one (D rank, P engine) pair.
Currently only instantiated for Mamba-HMA (hybrid SSM+Attention) models where FA and mamba require different splitting factors. Could be extended to other model types that need non-uniform hetero-TP transfer sizing.
All descriptor sizes are computed here. The guarantee is: local_entry_size == remote_entry_size (for NIXL)
Attributes that start with fa_ concern FlashAttention KV cache. Attributes that start with mamba_ concern Mamba conv/SSM state.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
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 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 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 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | |
needs_split_handles property ¶
needs_split_handles: bool
Whether per-P-rank split handles are needed.
True when FA and mamba have different read counts, requiring different splitting factors in the local handle.
_validate ¶
Cross-check internal consistency.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
compute_split_handle_data ¶
compute_split_handle_data(
src_blocks_data: list[tuple[int, int, int]],
num_fa_descs: int,
abs_tp: int,
) -> list[list[tuple[int, int, int]]]
Compute per-P-rank (addr, len, tp) triples for Mamba-HMA split handles.
FA descriptors (indices < num_fa_descs) are sliced by physical_fa_num_reads; mamba descriptors are sliced uniformly by abs_tp.
Returns one list of triples per transfer target.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
describe ¶
describe() -> str
One-line summary for logging.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
fa_head_slot ¶
Index into D's FA block for this P rank's head data.
For P ranks in fa_read_targets, returns 0, 1, ..., reads-1. For P ranks NOT in fa_read_targets (replicated duplicates), returns the slot of the matching FA target with the same head.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
fa_rank_offset ¶
Byte offset into P's FA block for this D rank.
When D is replicated (D_TP > K), multiple D ranks share a head. Computes offset relative to the target P rank's first head so it works regardless of how many heads P has. When neither side replicates, falls back to tp_rank % tp_ratio. Returns 0 when D does not index into P's block.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
filter_block_ids_for_rank ¶
filter_block_ids_for_rank(
remote_rank: int,
local_ids: BlockIds,
remote_ids: BlockIds,
is_mamba_group: list[bool],
) -> tuple[BlockIds, BlockIds]
Zero out FA groups for P ranks outside fa_read_targets.
Returns (filtered_local_ids, filtered_remote_ids). When the remote rank carries FA data for this D rank, returns the inputs unchanged.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
should_skip_fa ¶
Whether to skip FA groups for this P rank (mamba-only transfer).
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 | |
TpKVTopology dataclass ¶
Helper class for tensor parallel and KV topology information for mapping between local and remote TP workers.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
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 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 | |
block_size_ratio ¶
Calculate the block size ratio between local and remote TP.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
get_target_remote_ranks ¶
Get the remote TP rank (on P) that the current local TP rank (on D) will read from. When remote tp_size > local tp_size, we read from multiple remote ranks.
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
is_kv_replicated ¶
is_kv_replicated(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. When they are equal, each TP rank still owns one distinct KV head, so this is not considered replication.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
tp_ratio ¶
Calculate the tensor parallel ratio between local and remote TP. We can think of it as the number of local TP workers-per-remote TP workers. Local workers will read from the same remote TP worker in groups of size tp_ratio.If remote tp_size > local tp_size, the ratio is flipped (remote_size/local_size) and the returned value is negative.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
_physical_head_range ¶
Physical KV head range stored in a rank's KV cache tensor.
When tp_size <= num_heads: sharded, K/TP contiguous heads per rank. When tp_size > num_heads: 1 physical head per rank. Heads are distributed contiguously (matching vLLM's GQA weight partitioning): consecutive ranks share a head before moving to the next one.
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) |