Topic 4
Memory sets the batch
Bigger batches need room. Every conversation in the batch keeps its own KV cache in accelerator memory, next to one copy of the weights:
weights + KV cache per conversation × conversations ≤ memory
The KV cache formula is the one from Module 01: 2 × layers × heads × head size × tokens × bytes per number.
How many conversations fit in memory?
Arithmetic · documented model sizes
- Weights 26.0 GB
- KV cache for 32 conversations 53.7 GB
KV cache per conversation
1.7 GB
Conversations that fit
32
Left over
313 MB
Assumptions
AI Engineering, Ch. 9: 40 layers, model dimension 5,120; about 13 billion weights. KV cache = 2 × layers × heads × head size × tokens × bytes, the formula from Module 01; the cache uses the same bytes per number as the weights here. Ignored: activations, the framework's own memory, and fragmentation, so real servers fit fewer. 1 GB = 10⁹ bytes.
Predict first
Try halving the tokens per conversation in the estimator: about twice as many conversations fit. Switching to 4-byte numbers costs more than half of them, because the weights double as well as the cache. Real servers fit fewer than this estimate, because activations, the framework itself and fragmentation all take memory.
This is where the book's two meanings of memory-bound meet: bandwidth decides how fast each step runs, capacity decides how many sequences can share it.
Quick check