AI Inference Server Memory: Estimate Bandwidth & Capacity
In this article
This guide explains how to estimate the memory bandwidth and capacity your AI inference server needs. You'll learn to calculate the minimum DRAM size for your model weights and KV cache, and the memory bandwidth required to meet your target latency and throughput. Start with your model's parameter count and your expected request rate, then follow the steps below.
What You'll Need
- Model parameter count (e.g., 7B, 13B, 70B)
- Precision of the weights (FP32, FP16, INT8, etc.)
- Maximum sequence length (input + output tokens)
- Number of concurrent requests (batch size) you want to support
- Target latency (time to first token or per-token generation time)
- KV cache overhead per token (typically 2 * layers * hidden_size * bytes_per_element)
Step-by-Step
- Calculate model weight memory. Multiply the number of parameters by the bytes per parameter. For FP16, that's 2 bytes; for INT8, it's 1 byte. Example: a 7B model in FP16 needs 7e9 * 2 = 14 GB.
- Estimate KV cache memory per token. For a transformer model, the KV cache size per token is roughly 2 (for K and V) * number of layers * hidden size * bytes per element. For a 7B model with 32 layers and a hidden size of 4096 in FP16, that's 2 * 32 * 4096 * 2 = 524,288 bytes ≈ 0.5 MB per token.
- Multiply by max sequence length and batch size. For a max sequence of 2048 tokens and a batch of 32, the KV cache total is 0.5 MB * 2048 * 32 = 32 GB. Add this to the weight memory to get the total capacity needed.
- Add overhead for activations and framework. Typically add 20-30% on top of the weights + KV cache to account for activations, intermediate buffers, and the runtime. For the example above, total = (14 + 32) * 1.25 ≈ 57.5 GB.
- Determine memory bandwidth requirement. Memory bandwidth is the bottleneck for autoregressive generation. For each token generated, you must read all model weights. So bandwidth = (weights size in bytes) / (target time per token). For a 14 GB model and a target of 10 ms per token, you need 14 GB / 0.01 s = 1.4 TB/s.
- Adjust for batch size. In a batch, you read the weights once and generate multiple tokens, so the effective per-token bandwidth is the same, but you get more tokens per second. The required bandwidth stays the same; the throughput increases.
- Check against real hardware. Compare your required bandwidth to the memory bandwidth of your target GPU (e.g., A100 ~2 TB/s, H100 ~3.35 TB/s, A10 ~600 GB/s). If your requirement exceeds that, you need to reduce precision, use a smaller model, or increase batch size.
Common Mistakes
- Forgetting to include the KV cache in capacity estimates.
- Using the wrong bytes per parameter (e.g., assuming FP16 when the model is INT8).
- Ignoring activation memory, which can be significant for long sequences.
- Confusing memory capacity with bandwidth—they are independent constraints.
- Using the model's total parameters instead of just the weights that are read per token (which is the same, but some models have embeddings that are not read every token).
Tips
- Use a memory calculator script (like the one on this page) to iterate quickly.
- Measure actual memory usage with tools like
nvidia-smiorpsutilduring a test run. - Consider using quantization (INT8, FP8) to reduce both capacity and bandwidth needs.
- For very long sequences, use sliding window attention or sparse attention to reduce KV cache.
- If you have multiple GPUs, distribute the model and KV cache across them, but be aware of interconnect bandwidth.
Troubleshooting
- Out of memory (OOM) errors: Reduce batch size, sequence length, or use a lower precision. If using a framework like PyTorch, enable memory efficient attention.
- Throughput too low: If you are bandwidth-bound, increase batch size to get more tokens per weight read, or switch to a model with fewer parameters.
- Latency too high: If time per token is too long, you need higher bandwidth (better hardware) or a smaller model. Sometimes reducing KV cache (shorter sequences) helps.
- Estimates are way off: Double-check the hidden size and number of layers—they vary by model. Also verify the precision of your weights (some frameworks use BF16 which is also 2 bytes).
Frequently Asked Questions
How much memory does a 7B model need?
In FP16, just the weights take 14 GB. With KV cache for a 2048-token sequence and batch of 32, you might need 40-50 GB total.
What is the difference between memory capacity and bandwidth?
Capacity is how much data you can store (GB), while bandwidth is how fast you can read/write that data (GB/s). For inference, you need enough capacity to hold the model and enough bandwidth to read the weights quickly for each token.
Can I use CPU memory instead of GPU memory?
Yes, but CPU memory bandwidth is much lower (typically 50-100 GB/s vs 1-3 TB/s on GPU). This will severely limit token generation speed.
How does batch size affect memory?
Batch size increases the KV cache memory linearly (each request has its own KV cache). It does not increase the weight memory. So large batches can quickly exhaust capacity.
What is the KV cache and why does it matter?
It stores the key and value tensors from previous tokens to avoid recomputing them. It grows with sequence length and batch size, and is often the largest memory consumer in long-context inference.
Should I use INT8 quantization?
It halves the memory and bandwidth compared to FP16, but may slightly reduce accuracy. For many tasks, the trade-off is acceptable. Always evaluate on your specific workload.