vllm.distributed.kv_transfer.kv_checksum.checksum
¶
Connector-agnostic checksums of KV cache blocks.
A checksum covers one block position of one KV cache group, over the unsharded model (all layers of the group, all KV heads):
-
A slice is the KV of one layer and one KV head in the block: the K and V of the block's tokens for that head. Laid out token by token and split into little-endian 8-byte pieces, its checksum is::
s = sum_j x_j * (2 * j + 1) mod 2**64
where piece x_j is piece p of the token at position t in the
request, j = t * P + p with P pieces per token.
-
The block's checksum adds up the slices of every layer and head, each times its own odd factor::
c = sum_{layer, head} m * s mod 2**64, m = 2 * (layer * H + head) + 1
where H = MAX_KV_HEADS_PER_LAYER, a fixed bound on any layer's KV
heads, so that m does not depend on the model or how it is split.
Layers are numbered by the layer number in their names and heads
globally across TP; several caches of one layer (e.g. an attention's KV
and its indexer's) number their heads consecutively, in layer name order.
Odd factors make any change to a single piece change c, and distinct
factors make misplaced data (swapped tokens, blocks, heads or layers) change
it too. Numbering pieces from the request's start makes the checksums of
consecutive blocks add up to that of the tokens they hold, so a producer and
a consumer with different block sizes compare sums over the same tokens.
A slice always lies whole on one TP rank, and each worker weighs its slices
by their global layer and head, whatever its shard or memory layout. The
parts from all TP ranks and PP stages therefore sum to c, and a producer
and a consumer can compare one value per (group, block position) regardless
of their TP sizes, PP sizes or KV cache layouts.
Replicated KV (MLA, or GQA with more TP ranks than KV heads) is tracked per
replica: a partial checksum is keyed by (replicas, replica_index) of the
layers it covers, so each consumer replica can be checked on its own.
Mamba state is not per token: a group's block is the request's running
state, at position 0. A layer whose TP split is known
(MambaSpec.state_splits) has one slice per global row of its states,
the TP-split dimension, whose pieces are its 2- or 4-byte elements read
unsigned; other Mamba layers have one slice per TP shard, which only
producers and consumers of equal TP sizes can compare.
Classes:
-
KVChecksumBlocks–The blocks of one request to checksum.
-
KVChecksumGroup–One worker's partial checksums of one request's blocks in one group.
-
KVChecksumRecord–One worker's checksums for one step.
-
KVChecksumScheduled–The requests to checksum, by request id, set by the scheduler each step.
Functions:
-
block_checksum_tags–The uint64 each block's offloaded checksum is XORed with, so that it
-
kv_checksum_enabled–Whether KV checksums are enabled;
VllmConfigrejects setups that do -
kv_checksum_platform_supported–Whether this platform can run the KV checksum kernel.
-
num_valid_tokens–How many of the first
num_tokenstokens blockpositionholds.
Attributes:
-
BLOCK_CHECKSUM_BYTES–Bytes per block of the
KVChecksumWorker.init_block_checksumstensor: -
MAX_KV_HEADS_PER_LAYER–Hin the slice factor; groups with more KV heads per layer are not -
ReplicaKey–(replicas, replica_index)of the layers a partial checksum covers.
BLOCK_CHECKSUM_BYTES = 8
module-attribute
¶
Bytes per block of the KVChecksumWorker.init_block_checksums tensor:
one int64.
MAX_KV_HEADS_PER_LAYER = 1 << 16
module-attribute
¶
H in the slice factor; groups with more KV heads per layer are not
checksummed.
ReplicaKey = tuple[int, int]
module-attribute
¶
(replicas, replica_index) of the layers a partial checksum covers.
KVChecksumBlocks
dataclass
¶
The blocks of one request to checksum.
Source code in vllm/distributed/kv_transfer/kv_checksum/checksum.py
KVChecksumGroup
dataclass
¶
One worker's partial checksums of one request's blocks in one group.
Source code in vllm/distributed/kv_transfer/kv_checksum/checksum.py
KVChecksumRecord
dataclass
¶
One worker's checksums for one step.
Source code in vllm/distributed/kv_transfer/kv_checksum/checksum.py
KVChecksumScheduled
dataclass
¶
The requests to checksum, by request id, set by the scheduler each step.
Source code in vllm/distributed/kv_transfer/kv_checksum/checksum.py
block_checksum_tags(block_hashes, fingerprint)
¶
The uint64 each block's offloaded checksum is XORed with, so that it
only matches the block it was stored for: its block hash and its group's
layers (KVChecksumRecord.fingerprints). Another TP or PP shard's KV
already checksums differently, as each worker weighs its own heads and
layers.
Source code in vllm/distributed/kv_transfer/kv_checksum/checksum.py
kv_checksum_enabled(vllm_config)
¶
Whether KV checksums are enabled; VllmConfig rejects setups that do
not support them.
Source code in vllm/distributed/kv_transfer/kv_checksum/checksum.py
kv_checksum_platform_supported()
¶
Whether this platform can run the KV checksum kernel.
num_valid_tokens(position, block_size, num_tokens)
¶
How many of the first num_tokens tokens block position holds.