Caching Trades Recalculation for Growing Memory
Compare repeated full-prefix decoding with KV-cached decoding without confusing asymptotic work, implementation overhead, and measured latency. Calculate linear cache-memory growth and state the limits of toy evidence.
Without a cache, each generation step runs the complete growing prefix through every decoder block again. With a cache, each new token reuses earlier keys and values and runs only its own residual path through the blocks.
Calculate Cache Memory
For standard multi-head attention:
stored scalars. The factor 2 represents keys and values.
For the frozen model at , , , and :
At , this is 64 scalars or bytes in float32.
| Cached length | Scalars | Float32 payload bytes |
|---|---|---|
| 1 | 16 | 64 |
| 2 | 32 | 128 |
| 3 | 48 | 192 |
| 4 | 64 | 256 |
These numbers exclude tensor metadata, allocator overhead, model weights, temporary scores, activations for the current token, and framework state.
State What Work Remains
At decode length , each new query still compares with keys and mixes values. Dense cached attention therefore grows linearly per new token. Across a generated sequence, that attention work remains quadratic in total length.
Caching avoids recomputing previous tokens' projections, MLPs, normalizations, and residual updates at every generation step. Under a simple dense-operation count, repeatedly running the full growing sequence incurs cubic total attention work, while cached one-row decode reduces that repeated part to quadratic. Actual latency also depends on kernels, memory bandwidth, batch, hardware, and framework overhead.
Q1. Calculate cache payload
For , , , , , and float16 storage, calculate KV scalars and payload bytes.
Answer it first, then check.
Hint
Solution
Research Boundary
That work analyzes and measures specified large models on TPU v4 systems. It supports careful inference trade-off analysis; its latency and utilization numbers do not describe this tiny CPU Python implementation.