Skip to content

vllm.distributed.kv_transfer.kv_connector.v1.offloading.canonical_mapping

Derivation of canonical page mappings for KV offloading.

The only place in the offloading stack that reasons about parallelism (TP/DCP/PCP); everything downstream consumes byte mappings. The canonical page of a layer is the full offloaded block without parallelism: all KV heads, all block_size * dcp * pcp tokens, in the worker's page encoding. Uncertifiable layers get an opaque fallback mapping (fail closed).

Classes:

  • ByteRegion –

    A byte region within a page that repeats once per token.

Functions:

ByteRegion dataclass

A byte region within a page that repeats once per token.

_RankContext dataclass

Sharding parameters of one worker rank within the offload group.

_attention_byte_regions(kv_cache, spec, num_blocks, head_shard, num_head_shards, cp_size)

Byte regions of an attention page, given this rank's head shard. None when the physical layout is not recognized (fail closed).

_coalesce_runs(runs)

Collapse contiguous fragments within and across runs to minimize the number of copy ops (e.g. a single-rank mapping becomes one whole-page run).

_interleave_cp_tokens(regions, num_tokens, ctx)

Place each region's num_tokens rows at their canonical token positions, one run per chunk of interleaved tokens.

_layer_mapping(spec, kv_cache, num_blocks, ctx)

Certified mapping for one layer at one rank, or None (fail closed).

_local_to_canonical_token(local_idx, ctx)

Canonical position of one of this rank's local token indices.

_opaque_fallback_mapping(page_size_bytes, num_ranks, rank)

Fallback: place the worker's page whole at a worker-exclusive offset.

_packed_kv_regions(kv_cache, spec, head_shard, num_head_shards, cp_size)

K and V adjacent per (token, head), in NHD or HND stride order.

_split_kv_regions(kv_cache, spec, head_shard, num_head_shards, cp_size)

K and V in separate page halves, in NHD or HND stride order.

_verify_tiling(layer_name, per_rank)

Whichever ranks a block elects as writers must tile the canonical page exactly once, and each rank's runs must cover exactly its local page.

canonical_format_id(kv_cache_layout)

Identity of the canonical byte format, for namespacing persisted KV. Canonical pages keep the worker's KV layout family, so the id couples the format version with that family; consumers must match it exactly. The family keeps its historical NHD/HND spelling so ids stay stable for KV persisted before the layout enum existed.

derive_canonical_mappings(vllm_config, kv_cache_config, kv_caches)

Per-layer canonical page mappings for this worker.

Empty when the worker group is not exactly the TP x PCP grid; layers absent from the result have no canonical representation.