GENERATION STRATEGIES
The problem: Without KV cache, each new token requires recomputing attention over all previous tokens. Complexity: O(n²) per step.
The solution: Store computed K,V pairs. Each new token only computes attention against cached keys/values. Complexity: O(n) state, O(1)-ish work per step.
Memory cost (per request): 2 × seq_len × num_layers × num_kv_heads × head_dim × bytes × batch.
Example: LLaMA-3-8B-like (32 layers, 8 KV heads, head_dim 128, BF16): 131 KB/token — 4K context ≈ 0.5 GB/request, 32K ≈ 4 GB. GQA (fewer KV heads) cuts this proportionally.
Watch out: weights-only budgeting OOMs on the first long-context batch. Budget weights + cache × concurrency + overhead.
vLLM
PagedAttention, continuous batching, highest throughput. The default for GPU serving. Use when throughput per GPU matters.
TGI (Text Generation Inference)
Hugging Face's production server. Good integration with Hugging Face ecosystem. Use when staying inside HF tooling.
Ollama
Local inference with GGUF quantized models. Simplest way to run models locally. Use for development and personal use.
llama.cpp
CPU + GPU hybrid inference. Best for running large models on limited hardware. Use when no datacenter GPU is available.