Skip to content

vllm.distributed.kv_transfer.kv_connector.utils

KV cache helper for store.

Classes:

  • CircuitBreaker –

    Opens for cooldown_s after failure_threshold consecutive

  • EngineTransferInfo –

    Common per-remote-engine transfer state, computed at handshake.

  • KVOutputAggregator –

    Utility class to aggregate the output of all workers into a single

  • TransferTopology –

    Single source of truth for local TP identity and per-engine remote info.

Functions:

CircuitBreaker

Opens for cooldown_s after failure_threshold consecutive failures; callers skip requests while it is open. After the cooldown, allow() admits a single probe and holds others for probe_window_s: a failed probe reopens the breaker, a success closes it, and a probe that never reports back lets the next request probe once the window passes.

EngineTransferInfo dataclass

Common per-remote-engine transfer state, computed at handshake.

Stored per (engine_id, pp_rank) inside TransferTopology._engines.

Attributes:

end_layer = 0 class-attribute instance-attribute

Exclusive global index after the last layer owned by this PP rank.

remote_block_len instance-attribute

Block length (bytes)

remote_block_size instance-attribute

Tokens per block.

remote_dcp_size = 1 class-attribute instance-attribute

Remote decode context parallel size.

remote_physical_blocks_per_logical instance-attribute

Physical blocks per logical block.

remote_pp_rank = 0 class-attribute instance-attribute

Remote producer PP rank for this engine.

start_layer = 0 class-attribute instance-attribute

Global index of the first layer owned by this PP rank.

KVOutputAggregator

Utility class to aggregate the output of all workers into a single output corresponding to Rank 0 for scheduler.

Methods:

ft_defer_kv_connector_outputs(kv_connector_outputs)

Stash KV connector outputs from failed workers for replay at the next aggregate().

kv_cache_events are dropped: replaying them could break the live workers' event quorum, and a missed event only costs a cache miss.

TransferTopology dataclass

Single source of truth for local TP identity and per-engine remote info.

Methods:

  • block_size_ratio –

    Calculate the block size ratio between local and remote.

  • dcp_consumer_count –

    How many local ranks (in aggregate) read from a given remote rank.

  • dcp_source_ranks –

    Remote ranks whose DCP slice overlaps mine (MLA, remote_dcp_size > 1).

  • describe –

    One-line summary of transfer config for logging.

  • handshake_target_ranks –

    Pre-registration: compute which remote TP ranks to handshake with.

  • is_kv_replicated –

    Whether the KV cache is replicated across TP workers due to the

  • register_remote_engine –

    Register a remote engine, unifying worker dicts state.

  • target_remote_ranks –

    Get the remote TP rank(s) that the current local TP rank will

  • tp_ratio –

    Calculate the tensor parallel ratio between local and remote TP.

Attributes:

dcp_rank property

This rank's DCP coverage rank.

with dcp_size in (1, tp_size) enforced at the connector boundary, a rank's DCP identity is always exactly tp_rank % dcp_size.

local_replicates_kv_cache property

Whether the local engine's KV cache is replicated.

block_size_ratio(remote_block_size)

Calculate the block size ratio between local and remote.

dcp_consumer_count(remote_tp_size, remote_dcp_size)

How many local ranks (in aggregate) read from a given remote rank.

Used by the producer side to know how many reader notifications to wait for before freeing a request's blocks. Reuses tp_ratio whenever the remote isn't sharded — a sharded local side already has tp_size == dcp_size, so the existing TP-ratio formula is already correct there unmodified.

dcp_source_ranks(remote_tp_size, remote_dcp_size)

Remote ranks whose DCP slice overlaps mine (MLA, remote_dcp_size > 1).

Shared by handshake_target_ranks (who to query metadata from) and compute_tp_mapping (who to actually read from) — for MLA the two questions have the identical answer, since DCP sharding is the only thing keeping a remote rank from being interchangeable with any other.

describe(remote_engine_id, remote_pp_rank=0)

One-line summary of transfer config for logging.

handshake_target_ranks(remote_tp_size, remote_dcp_size=1)

Pre-registration: compute which remote TP ranks to handshake with.

Pure math based on local/remote TP (and DCP, when the remote shards its KV cache) sizes — does not require the remote engine to be registered yet.

DCP support is scoped to dcp_size in (1, tp_size) on each side and DCP sizes that divide one another: neither side ever has a partially-duplicated, partially-sharded KV cache. When the remote is not sharded (remote_dcp_size == 1) this reduces exactly to the DTP>=PTP case, since a sharded local side already has tp_size == dcp_size.

is_kv_replicated(remote_engine_id, remote_pp_rank=0)

Whether the KV cache is replicated across TP workers due to the number of TP workers being greater than the number of KV heads.

register_remote_engine(remote_engine_id, info)

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.

target_remote_ranks(remote_engine_id, remote_pp_rank=0)

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.

tp_ratio(remote_tp_size)

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).

check_compressed_kernel_split(vllm_config, kv_cache_config, kernel_block_size)

A layer-outer layout cannot split compressed pages into kernel blocks.

clip_ssm_state_blocks(blocks, num_spec_blocks)

Drop speculative scratch slots and keep only the running-state slot.

copy_kv_blocks(src_kv_caches, dst_kv_caches, src_block_ids, dst_block_ids, direction)

Copy kv blocks between different buffers.

get_current_attn_backend(vllm_config, layer_names=None)

Get the first attention backend for the given layers.

get_current_attn_backends(vllm_config, layer_names=None)

Get all distinct attention backends for the given layers.

Parameters:

  • vllm_config

    (VllmConfig) –

    The current vLLM configuration.

  • layer_names

    (list[str] | None, default: None ) –

    Optional list of layer names to scope the lookup. When None, all attention layers are considered.

Returns:

get_current_attn_backends_and_specs(vllm_config, kv_cache_config, fallback)

Distinct (backend, spec, query heads) groups of the transfer layers, with their layer names, else fallback with no layer names.

The layer names let kernel block selection match the model runner's, which picks per attention group. Attention caches in a block-outermost layout are skipped: blocks there are never split group-wide (prepare_kernel_block_sizes), and a cache that needs smaller kernel blocks views them inside each block, so its kernel pages never split transferred blocks. Compressed caches are skipped too (check_compressed_kernel_split guards them).

get_max_offload_tokens(kv_transfer_params)

Parse the request's offload cap, preserving zero and rejecting booleans.

get_transferred_mrope_position_delta(vllm_config, sampling_params)

The M-RoPE position delta the prefill instance reported for a request whose KV this instance receives, or None if there is none to apply.

kv_connector_may_load_foreign_kv(vllm_config)

Whether the configured KV connector may load bytes that vLLM's own cache writer did not produce for this KV cache layout.

Attention backends whose fast paths assume no foreign bytes (e.g. NaNs in unwritten slots) reach the KV cache keep them only when this is False.

kv_postprocess_blksize_and_layout_on_receive(cache, indices, block_size_ratio)

Transforms the layout of received KV cache to the local block_size and LBHNC. (Only works for local blocksize >= remote blocksize)

prefill is LBHNC, smaller block_size decode(local) is LBNHC, larger block_size

kv_postprocess_blksize_on_receive(cache, indices, block_size_ratio)

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)

yield_req_data(scheduler_output)

Yields: (req_id, block_id_groups, is_full_table)

is_full_table is True when block_id_groups is the request's whole block table (new and resumed requests, which are both in scheduled_new_reqs) rather than this step's additions.