Main Point: Serving memory grows with the active context and the number of simultaneous requests, even when model weights stay fixed. Include the KV cache and other working state when testing how many users a system can support.
Key Points
- The KV cache stores numerical key and value representations from earlier context positions for reuse during generation.
- Cache size depends on architecture, cached length, number format and simultaneous sequences.
- Under the worked assumptions, one 8,192-token sequence uses 1 GiB of KV state; 32 independent equal-length sequences use 32 GiB, before weights and other workspace.
1. Separate Model Weights From Serving State
- The retailer tests one refund question successfully. Later, longer policy documents and many simultaneous customers fill memory, although the trained model has the same parameters.
- Learned weights are parameters established through training. The KV cache is state created while processing the current context.
- During attention, keys help determine which earlier contextual information is relevant; values carry information that contributes to later representations. These are numerical arrays derived from the context.
- Prefill establishes context state. Decode reuses selected earlier representations as new tokens are generated. Caching avoids repeating some earlier-position calculations while consuming memory to retain the arrays.
- A weight-fit test covers only part of the deployed working set. Input context, generated positions, concurrent sequences and temporary workspace also need room.
2. Identify What Changes Cache Size
- Architecture determines the layer count, key-value head count and grouping, and the dimension of each representation.
- Cached token count and the number of independent active sequences determine how much context state is retained. Longer output can extend cached sequences during generation.
- Bytes per value depend on the numerical format. Shared prefixes, cache layout and offloading can change how much memory is needed and where it is held.
- Real request lengths vary. A maximum-context promise therefore has capacity and cost consequences: longer allowed inputs can reduce the number of simultaneous requests the hardware can serve.
3. Build A Clearly Scoped Estimate
Simplified cache bytes = 2 × layers × cached tokens × KV heads × head dimension × bytes per value × equal-length sequences
| Factor | Meaning |
|---|---|
| 2 | Both keys and values |
| Layers | Model stages whose state is retained |
| Cached tokens | Retained context positions |
| KV heads | Heads organise attention-related processing; this count identifies how many key/value groups the architecture stores |
| Head dimension | Values in each stored head representation |
| Bytes per value | Storage used by the selected format |
| Sequences | Independent equal-length active sequences in this example |
- Counts of layers, positions, heads and values become bytes when multiplied by bytes per value.
- This estimate covers the specified key/value arrays. Model weights, temporary workspace, allocator overhead, serving buffers and offload traffic sit outside its boundary.
- Different head arrangements, sharing policies or formats require a fresh estimate. A reduced byte count also needs quality and performance evaluation.
4. Work Through One Sequence And Then 32
Hypothetical configuration: 32 layers; 8 KV heads; head dimension 128; 8,192 cached tokens; 2 bytes/value; one sequence.
| Step | Calculation | Result |
|---|---|---|
| Values per token across layers | 2 × 32 × 8 × 128 | 65,536 values |
| Bytes per token | 65,536 × 2 bytes | 131,072 bytes |
| One sequence | 131,072 × 8,192 | 1,073,741,824 bytes = 1 GiB |
| 32 independent equal-length sequences | 32 × 1 GiB | 32 GiB |
- GiB is a binary unit: 1 GiB = 1,073,741,824 bytes. A decimal GB is 1,000,000,000 bytes. Keep that distinction in capacity calculations.
- The weight size stays unchanged as sequence count grows. The 32 GiB result covers cache state under the stated assumptions; deployment needs additional memory for weights and other work.
- Safely shared prefixes can reduce duplication. Offloading moves state elsewhere but introduces a transfer path with its own latency and bandwidth. Actual allocation needs measurement under the real request mix.
5. Test Capacity And Service Quality Together
- If memory failures appear during busy periods, inspect active sequence count, input and output lengths, cache allocation and temporary workspace. Test a realistic mix of long and short requests.
- Cache eviction may cause recomputation or fetching later. Persistent or shared state must respect customer permissions and account isolation.
- Quantizing cache state can reduce storage while changing quality or implementation performance. Layout and head grouping can also affect movement and use.
- Evaluate changes using answer quality, first-token time, streaming pace, memory and cost. Choose a context policy, serving pool and capacity target that supports the intended task.
- Batching and queues connect these per-request memory needs to total throughput and waiting time, which the next lesson examines.