KV transfer: count the actual cache representation
Inventory layers, KV heads, positions, dtype, and shards before deriving serialization and bandwidth bounds.
On this page
The question for today
What you’ll understand
- Calculate a dense-attention KV payload with the correct head count.
- Distinguish logical payload, shard payload, and actual wire bytes.
- State serial and optimistic overlapped transfer-time assumptions.
A prompt’s transfer size is not its token count times a universal constant. It depends on every stored layer’s key/value representation, KV-head count, head dimension, element size, retained positions, and partition layout. Grouped-query attention uses fewer KV heads than query heads; latent or sliding-window caches need another inventory. We derive one dense-attention payload and an explicitly ideal transfer model. Versioned vLLM connector interfaces expose loading and completion boundaries, but they do not turn a byte estimate into guaranteed latency or durable remote ownership.
Count logical elements before applying a shard factor
For a uniform dense KV layout, each retained token has a key and a value for every stored layer and KV head, each with the stated head dimension and dtype. Use KV heads, not the number of query heads. A heterogeneous model needs a sum over layers and representations rather than a single uniform product. Quantized caches can add scales, metadata, padding, or alignment; a one-byte element does not prove that complete wire payload is exactly half a two-byte layout. Record the concrete format and its version.
Map each actual source shard to destination-owned positions, layers, and heads. Equal head sharding can divide the logical payload only if those heads are genuinely partitioned without replication and the transfer layout matches. A tensor-parallel degree greater than the KV-head count can involve replication, and a different destination degree can require reshaping or redistribution. Prefix-cache reuse can remove known matching positions, but only after validating identity, access scope, and position semantics. Payload, allocated cache capacity, compressed bytes, and transmitted retries are different counters.
Separate packing, movement, and availability
A transfer path can include reading and packing device memory, host staging, network serialization, destination unpacking, and completion notification. Its effective payload bandwidth can be limited by any shared hop rather than the link’s advertised rate. Define whether a time measurement ends at host receipt, device completion, per-layer readiness, or decoder usability. Enqueueing an asynchronous copy does not make its destination safe to read or its source safe to overwrite. The pinned connector’s layer waits and save completion hooks illustrate distinct lifetime boundaries.
If packing, transfer, and unpacking are fully sequential, add their durations and non-overlapped setup. If independent chunks can pipeline across stages, the ideal duration can approach the slowest stage plus unavoidable fill and drain costs. A maximum is then a lower-bound ingredient, not a promise that every operation overlaps perfectly. Layer-wise transfer can overlap some computation while still leaving the last dependency on the critical path. Measure that path and contention before subtracting an entire network duration from a latency budget.
Validate bytes and semantics with a small fixture
Capture model and adapter digests, cache dtype, per-layer shapes, source and destination shard maps, token positions, and transferred checksums or other integrity evidence. Compare reconstructed cache state and resulting logits with a matched local computation under suitable numerical tolerances. Correct byte counts can still describe the wrong rotary positions, model revision, or adapter. Include a partial final block and an unequal shard case. If a transport compresses or converts state, evaluate both representation compatibility and output-quality effects rather than count only a smaller packet.
The roofline lab offers a bandwidth-ceiling analogy rather than a transfer simulator. Reset, explicitly set intensity eight and bandwidth 80, predict its ceiling, then Play or Step. Compare a fresh Reset with intensity eight and bandwidth 40 explicitly set; ceilings are 640 and 320 GFLOP/s. The instrument holds a synthetic compute roof and models no KV layout, serialization, packing, or network path. Use measured byte inventories and completion events for real transfer timing.
Work it through
A grouped-query cache has a 96-MiB logical payload
Assume 1,024 retained positions, 24 stored layers, eight KV heads, head dimension 128, and two-byte keys and values. There are 32 query heads, which are not the KV-head count. Ignore all metadata and padding. Effective payload bandwidth is twelve GiB/s.
The logical size is 2×1024×24×8×128×2=100,663,296 bytes, or 96 MiB. Substituting 32 query heads would overcount this specified cache by a factor of four.
Four genuinely equal nonreplicated KV-head shards each contain 24 MiB. On one aggregate path, 96 MiB at twelve GiB/s has a serialization lower bound of 96/12288 seconds, or 7.8125 ms. Do not divide that aggregate size by four again unless the bandwidth model actually contains four independent paths.
With sequential setup 0.2 ms, packing one ms, and unpacking 0.5 ms, the listed serial model totals 9.5125 ms. An overlapped design needs its own measured dependencies; the bandwidth term alone is not complete decoder-ready latency.
The logical payload is 96 MiB, each stated shard 24 MiB, and the sequential illustrative transfer path 9.5125 ms.
The living notebook
Explore the model
Interactive lab · roofline
Bandwidth scaling is only a synthetic ceiling analogy
Use independent Reset → explicitly set BOTH I and B → predict → Play/Step runs for (8,80) and (8,40), giving 640 and 320 GFLOP/s under the fixed compute roof. There are no KV tensors, byte transfers, packing operations, or completion acknowledgments.
- Attainable ceiling
- 640 GFLOP/s
- Ridge intensity
- 12.5 FLOP/byte
- Ceiling regime
- Memory-bound ceiling
Synthetic roofline ceiling min(1000, B·I), in GFLOP/s, with B in decimal GB/s and I in FLOP/byte. Playback traces the roof without changing the selected kernel. This upper-bound model is not a measured throughput prediction, hardware comparison or price model. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forInventory actual payload and shared bandwidth before assigning a handoff-time budget.
Pause & reason
Check your understanding
Your turn
Try it yourself
Use 512 positions, sixteen layers, four KV heads, dimension 128, and two bytes per element for a uniform K/V cache. Calculate logical MiB, two equal nonreplicated shard sizes, and serialization time on an aggregate four-GiB/s path.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Count both keys and values and convert using powers of two before applying the actual shard assumption.
Compare with the solution
The size is 2×512×16×4×128×2=16,777,216 bytes, or sixteen MiB. Each stated shard is eight MiB. Aggregate serialization is 16/4096 seconds, or 3.90625 ms, excluding setup, packing, contention, and completion. Two shards do not halve that time on the same aggregate bottleneck. Verify model, dtype, layout, positions, metadata, and destination compatibility before treating those bytes as usable state.
Keep the idea
Take it with you
- Use actual stored KV representation and head counts.
- Distinguish logical bytes, shard bytes, wire traffic, and allocated capacity.
- Transfer completion and overlap follow real dependencies, not enqueue timing.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.