VRAM math: will this model fit?
The three terms that decide memory use (weights, KV cache, overhead), with the formulas, effective bits per weight by quant, KV size for real models and worked fit checks you can repeat for any model and GPU.
"Will this model fit on my GPU?" is the first question in local inference, and it has a clean answer you can compute in a minute. Total memory is roughly:
total = weights + KV cache + runtime overheadThree terms, each with a formula. Learn them once and you can size any model, quant and context.
#1. Weights
weights_bytes = parameters ร bits_per_weight / 8Quantised formats store scale values alongside the weights, so use the effective bits per weight, not the headline number:
| Format | Effective bits per weight (approx) | 8B model | 70B model |
|---|---|---|---|
| F16 / BF16 | 16 | 16 GB | 140 GB |
| Q8_0 | 8.5 | 8.5 GB | 75 GB |
| Q6_K | 6.6 | 6.6 GB | 58 GB |
| Q5_K_M | 5.7 | 5.7 GB | 50 GB |
| Q4_K_M | 4.85 | 4.9 GB | 43 GB |
| Q3_K_M | 3.9 | 3.9 GB | 34 GB |
| Q2_K | 2.6 to 3.3 | about 3 GB | about 25 GB |
Example: Llama 3.1 8B at Q4_K_M = 8.03e9 ร 4.85 / 8 = 4.87 GB, which is about 4.5 GiB. Llama 3.1 70B at Q4_K_M is about 40 GiB.
GB versus GiB: a gigabyte is 10โน bytes, a gibibyte is 2ยณโฐ (1.074 ร 10โน). Memory tools differ in which they show, and the gap is 7 percent. Pick one unit and stay with it. GPU spec sheets usually mean GiB-ish capacity, while model files are listed in GB.
Rule of thumb: 4-bit weights need about 0.55 to 0.6 GB per billion parameters, 8-bit about 1.05, 16-bit 2. See quantization basics for what each format costs in quality.
#2. KV cache
Every token in the context stores a key and a value vector per layer. That memory grows with context and with concurrent sequences:
kv_bytes_per_token = 2 ร layers ร kv_heads ร head_dim ร bytes_per_element
kv_total = kv_bytes_per_token ร context_tokens ร parallel_sequencesLlama 3.1 8B (32 layers, 8 KV heads, head dim 128, 16-bit): 2 ร 32 ร 8 ร 128 ร 2 = 131,072 bytes = 128 KiB per token. An 8K context costs 1 GiB, 32K costs 4 GiB, and 128K costs 16 GiB.
KV size per token for a few real models (16-bit cache):
| Model | Layers | KV heads | Head dim | KV per token |
|---|---|---|---|---|
| Qwen2.5 7B | 28 | 4 | 128 | 56 KiB |
| Llama 3.1 8B | 32 | 8 | 128 | 128 KiB |
| Phi-3 mini (3.8B) | 32 | 32 | 96 | 384 KiB |
| Qwen2.5 32B | 64 | 8 | 128 | 256 KiB |
| Llama 3.1 70B | 80 | 8 | 128 | 320 KiB |
Models with grouped-query attention (GQA) have few KV heads, which is why modern models have much smaller caches than older ones. Notice Phi-3 mini: a 3.8B model whose cache per token is three times larger than an 8B Llama's, because it has 32 KV heads versus 8. A small model is not automatically a cheap-context model.
You can read these numbers from a model's config.json (num_hidden_layers, num_key_value_heads, hidden_size / num_attention_heads). If num_key_value_heads is absent, the model uses full multi-head attention and the KV head count equals num_attention_heads.
Concurrency multiplies it: four users, each with an 8K window, on Llama 3.1 8B is 4 ร 1 GiB = 4 GiB. In llama.cpp, -c 32768 -np 4 gives four slots of 8,192 tokens each, and all of that memory is reserved up front.
#3. Overhead
Compute buffers for the current batch, the CUDA or Metal context, scratch space and allocator fragmentation: budget roughly 0.5 to 1.5 GiB, more at long context, large batch sizes (-b, -ub) or with multiple GPUs. If your GPU also drives your display, the desktop takes another 0.3 to 1 GiB or more (browsers with hardware acceleration are notorious).
#Putting it together
Llama 3.1 8B, Q4_K_M, 8K context, one user:
weights 4.5 GiB
KV 1.0 GiB (8,192 ร 128 KiB)
overhead 0.8 GiB
total 6.3 GiBIt fits an 8 GB GPU with about 1.5 GiB to spare, but at 32K context the cache alone is 4 GiB and the total is about 9.3 GiB, which does not fit.
A fit check you can reuse:
| Item | Value |
|---|---|
| Parameters and quant | 14.7B at Q4_K_M โ 8.3 GiB |
| Context and users | 16K ร 1 user |
| KV per token | 192 KiB (48 layers, 8 KV heads, 128 dim) โ 3.0 GiB |
| Overhead | 1.0 GiB |
| Total | 12.3 GiB |
| Available | 16 GiB card, display on the iGPU โ about 15.5 GiB |
| Headroom | 3.2 GiB (21%), comfortable |
Aim for at least 10 to 15 percent headroom. A model that fits to the last megabyte will fail the moment the context grows, a second tab opens or the driver allocates a buffer.
Use the VRAM calculator to do this for any model, quant and context, and the hardware checker to compare against real devices.
#Other things that take memory
- Multimodal models add a vision encoder (hundreds of MB to a few GB) and image tokens in the context.
- Embedding and reranker models used in RAG need their own memory (local RAG).
- LoRA adapters add a little each.
- Draft models for speculative decoding add the draft model's weights and cache (speculative decoding).
- Mixture-of-experts models need memory for all experts even though only a few are active per token.
- Multiple servers on one GPU each carry their own overhead.
#When it does not fit
Options, roughly from least to most painful:
- Quantize the KV cache (8-bit roughly halves it, with little loss): see KV-cache quantization.
- Shrink the context to what you actually use. Most tasks need under 8K.
- Choose a smaller quant (Q4_K_M instead of Q5 or Q6). Below about 3 bits, quality drops quickly.
- Reduce concurrency, or shorten per-user context.
- Pick a smaller or more efficient model (one with GQA and a modern architecture).
- Offload some layers to CPU RAM. It works, but every offloaded layer runs at system-memory speed, so decode slows a lot (GPU offload).
- Add memory: a bigger card, a second card, a Mac with more unified memory (buying guide).
#Common mistakes
- Sizing by file size alone, forgetting the KV cache and overhead.
- Using the headline "24 GB" as if all of it were free.
- Mixing GB and GiB.
- Ignoring parallel slots.
-np 8with a large window multiplies cache memory. - Assuming a long-context model should be run at its maximum context.
- Forgetting that MoE needs all experts resident.
#Exercise
You want to run Qwen2.5 32B at Q4_K_M (about 32.8B parameters) on a 24 GiB GPU that also drives your display (assume 1 GiB used by the desktop). Context 16K, one user. KV is 256 KiB per token.
- How big are the weights and the KV cache?
- Does it fit with 1.2 GiB of overhead?
- What would you change?
Answers:
- Weights:
32.8e9 ร 4.85 / 8 = 19.9 GB โ 18.5 GiB. KV:16,384 ร 256 KiB = 4 GiB. - Total
18.5 + 4 + 1.2 = 23.7 GiB, against about 23 GiB available. It does not fit. - Quantize the KV cache to 8-bit (saves about 2 GiB, new total 21.7 GiB, a thin margin), or cut the context to 8K (saves 2 GiB), or move the display to integrated graphics to free the desktop's memory. Doing the KV quantization and a 12K context would leave comfortable headroom.
Quick quiz
+10 XP per correct answer, +25 XP for a perfect score
1. Approximate weight size of an 8B model at 4.85 bits per weight?
2. Which term grows with context length and the number of concurrent users?
3. Why do modern models with grouped-query attention need much smaller KV caches than older ones?
4. A 24 GB GPU is also driving your monitor. Roughly how much is available to the model?
5. The model fits only if you shrink something. Which option typically has the smallest quality cost?