Inference optimization improves serving efficiency without changing model architecture. It reduces latency, increases throughput, and lowers resource demands by changing operation scheduling, memory access, and GPU utilization during deployment. section 9.3
Autoregressive generation produces one token at a time, so recomputing the complete attention context at every step wastes work. Key-value (KV) caching stores each token’s key and value vectors, while the query is recomputed for the new token; later steps reuse the stored context. section 9.3
Common inference-optimization mechanisms include:
- KV caching avoids repeated attention computation, but its memory footprint grows with sequence length and batch size. section 9.3
- Paged attention manages attention memory efficiently during deployment. section 9.0 section 9.6
- FlashAttention reduces attention-layer memory and compute overhead while retaining exactness. section 9.6
- Operator fusion, KV caching, and paged attention improve deployment throughput and latency. section 9.6
Benchmarking cached generation
A Hugging Face Transformers benchmark loads meta-llama/Llama-3.2-3B-Instruct, generates up to 256 tokens with deterministic decoding, and compares runs with use_cache enabled and disabled. The comparison measures the latency effect of KV caching. section 9.3
Serving through vLLM
A vLLM API server serves google/gemma-3-4b on port 8000, after which an OpenAI-compatible client requests an explanation of paged attention. section 9.3
Figure 9.4 At each step, the query is computed for the new token, while past key and value vectors are cached and reused. This avoids recomputation of the entire attention context, dramatically reducing inference time.
The figure traces autoregressive attention step by step: each new token adds key-value pairs to the cache, while subsequent generation reuses them instead of recomputing the full context. section 9.3
