vllm.distributed.kv_transfer.kv_connector.utils ¶
KV cache helper for store.
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
44 45 46 47 48 49 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 | |
__init__ ¶
__init__(expected_finished_count: int)
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
aggregate ¶
aggregate(
outputs: list[ModelRunnerOutput | None],
output_rank: int = 0,
) -> ModelRunnerOutput | None
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
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 | |
from_connector classmethod ¶
from_connector(connector: KVConnectorBase, world_size: int)
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
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 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 | |
__init__ ¶
__init__(
tp_rank: int,
remote_tp_size: dict[EngineId, int],
is_mla: bool,
total_num_kv_heads: int,
attn_backend: type[AttentionBackend],
engine_id: EngineId,
remote_block_size: dict[EngineId, int],
) -> None
__post_init__ ¶
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
block_size_ratio ¶
Calculate the block size ratio between local and remote TP.
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
block_size_ratio_from_engine_id ¶
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_target_remote_ranks_from_engine_id ¶
is_kv_replicated ¶
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
replicates_kv_cache ¶
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
tp_ratio_from_engine_id ¶
_make_src_and_dst_indices ¶
_make_src_and_dst_indices(
src_block_ids: list[int],
dst_block_ids: list[int],
src_device: device | str,
dst_device: device | str,
) -> tuple[Tensor, Tensor]
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)
Source code in vllm/distributed/kv_transfer/kv_connector/utils.py
get_kv_connector_cache_layout ¶
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], ...], bool] | (req_id, new_block_id_groups, preempted) |