Session 2, Exercise 3 · Session 3, Exercise 3

KV Cache Budget

The weights and the KV cache share one accelerator's memory. The capacity left over sets the batch size, and the batch size sets the arithmetic intensity. Change the sequence length and the precision and watch how a capacity limit ends up holding down bandwidth efficiency as well.

KV/seq = 2 · L · S · d · bkv · Bsz = ⌊(M − P·b − reserve) / KV⌋ · tok/s ≲ B / (P·b) × Bsz

How one accelerator's memory is divided

Weights Reserve (activations, fragmentation) KV cache (one cell per sequence) Unused
Chip
Weight precision
KV cache precision
Attention (number of KV heads)
Weights
KV / sequence
Capacity available for KV
Max batch size
Throughput ceiling
Compute utilization ≈ Bsz/I*

How fast the batch size shrinks as sequences grow

Max batch size for three weight precisions at the currently chosen chip, KV precision, attention and reserve ratio. The dashed line is the batch size needed to reach the roof (Bsz ≈ I*).

Check against the solution tables ()
H100 80GB, S = 4,096, KV fp16, reserve 0%. The "Solution" column holds the rounded values from the solution tables of Session 2, Exercise 3 and Session 3, Exercise 3.
ItemComputedSolution

What this calculation leaves out