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 | |
MambaEngineTransferInfo dataclass ¶
Bases: EngineTransferInfo
Extends EngineTransferInfo with Mamba-hybrid transfer geometry.
For hybrid SSM+Attention models, FA and Mamba layers may require different numbers of reads from different remote ranks. This dataclass captures that per-engine transfer plan.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
is_remote_replicated instance-attribute ¶
is_remote_replicated: bool
Whether the remote engine has replicated KV heads (remote_tp_size > total_num_kv_heads).
remote_all_source_ranks instance-attribute ¶
All remote ranks this local rank reads from (FA + Mamba).
remote_fa_descriptor_bytes instance-attribute ¶
remote_fa_descriptor_bytes: int
Byte size of one FA K (or V) descriptor entry.
remote_fa_source_ranks instance-attribute ¶
Remote ranks carrying unique FA heads for this local rank.
remote_num_fa_reads instance-attribute ¶
remote_num_fa_reads: int
Number of distinct remote ranks needed for FA data.
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
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 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 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
local_replicates_kv_cache property ¶
local_replicates_kv_cache: bool
Whether the local engine's KV cache is replicated.
_build_mamba_info ¶
_build_mamba_info(
remote_tp_size: int,
remote_block_size: int,
remote_block_len: int,
remote_physical_blocks_per_logical: int,
local_block_len: int,
) -> MambaEngineTransferInfo
Compute Mamba transfer plan.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
_physical_head_range staticmethod ¶
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
block_size_ratio ¶
Calculate the block size ratio between local and remote.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
compute_split_handle_data ¶
compute_split_handle_data(
remote_engine_id: EngineId,
src_blocks_data: list[tuple[int, int, int]],
num_fa_descs: int,
abs_tp: int,
) -> list[list[tuple[int, int, int]]]
Per-remote-rank (addr, len, dev) triples for Mamba-HMA split handles.
FA descriptors (indices < num_fa_descs) are sliced by remote_num_fa_reads; mamba descriptors are sliced uniformly by abs_tp.
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
fa_head_slot ¶
Index into local FA block for this remote rank's head data.
For remote ranks in fa_source_ranks, returns 0, 1, …, reads-1. For ranks NOT in fa_source_ranks (replicated duplicates), returns the slot of the matching source rank with the same head.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
fa_rank_offset ¶
Byte offset into remote FA block for this local rank.
When local TP is replicated (local_tp > K), multiple local ranks share a head. Computes offset relative to the target remote rank's first head so it works regardless of how many heads the remote has. Returns 0 when local does not index into remote.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
filter_block_ids_for_rank ¶
filter_block_ids_for_rank(
remote_engine_id: EngineId,
remote_rank: int,
local_ids: BlockIds,
remote_ids: BlockIds,
is_mamba_group: list[bool],
) -> tuple[BlockIds, BlockIds]
Zero out FA groups for remote ranks outside fa_source_ranks.
Returns (filtered_local_ids, filtered_remote_ids). When the remote rank carries FA data for this local rank, returns the inputs unchanged.
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
needs_split_handles ¶
needs_split_handles(remote_engine_id: EngineId) -> bool
Whether per-remote-rank split handles are needed.
True when FA and mamba have different read counts, requiring different splitting factors in the local handle.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
register_remote_engine ¶
register_remote_engine(
remote_engine_id: EngineId,
remote_tp_size: int,
remote_block_size: int,
remote_block_len: int,
remote_physical_blocks_per_logical: int,
*,
local_block_len: int = 0,
) -> EngineTransferInfo
Register a remote engine, unifying worker dicts state.
Only remote engines should be registered here — the local engine's identity (tp_size, block_size, etc.) is set via __init__ params.
For Mamba models, also computes the Mamba transfer plan and builds the FA source lookup caches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_block_len | int | Local representative block_len (bytes). Required for Mamba models to compute | 0 |
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
should_skip_fa ¶
Whether to skip FA groups for this remote rank (mamba-only).
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.
For Mamba models, returns the precomputed all_source_ranks (FA + Mamba union).
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) |