Sub topic 8
Checking whether a model fits
This chapter combines the weight and KV cache estimates from earlier chapters into one pass/fail check. It assumes you can compute weight memory from parameters and bits per weight, and that you know the KV cache grows linearly with context length. It also assumes you know the difference between VRAM, system RAM and unified memory. The goal is to decide, before you download a file, whether a model and context length will fit on your machine.
The budget equation
A model fits when the memory you can use is at least the sum of four parts:
- Weights. Parameters × bits per weight ÷ 8. This is close to the size of the GGUF file.
- KV cache. 2 × layers × KV heads × head size × context × bytes per value.
- Runtime overhead. Compute buffers, the GPU driver context, and scratch space for prompt processing. Plan for roughly 0.5 to 1.5 GiB. Larger batch sizes and longer contexts push it up.
- Everything else on the same memory pool. The operating system, your desktop, a browser, and other programs.
The fourth item is where most estimates go wrong. A "24 GiB" GPU that also drives your monitors will not offer all 24 GiB to the model.
A fit-check function
This code reuses the two formulas from the earlier chapters and adds an overhead term and a verdict.
def weights_gib(params_billion, bits_per_weight):
return params_billion * 1e9 * bits_per_weight / 8 / 2**30
def kv_cache_gib(layers, kv_heads, head_dim, context, bytes_per_value=2):
# 2 = one K tensor and one V tensor per layer
return 2 * layers * kv_heads * head_dim * context * bytes_per_value / 2**30
def check_fit(label, budget_gib, params_b, bits, layers, kv_heads,
head_dim, context, overhead_gib=1.0):
need = (weights_gib(params_b, bits)
+ kv_cache_gib(layers, kv_heads, head_dim, context)
+ overhead_gib)
headroom = budget_gib - need
verdict = "fits" if headroom >= 0 else "does not fit"
print(f"{label}: need {need:.1f} GiB, headroom {headroom:+.1f} GiB, {verdict}")
# 8B model: 32 layers, 8 KV heads, head size 128, Q4_K_M (about 4.85 bits)
check_fit("8B, 8k ctx, 24 GiB GPU", 24, 8.0, 4.85, 32, 8, 128, 8192)
# 70B model: 80 layers, 8 KV heads, head size 128, Q4_K_M
check_fit("70B, 8k ctx, 24 GiB GPU", 24, 70.0, 4.85, 80, 8, 128, 8192)
check_fit("70B, 8k ctx, 48 GiB GPU", 48, 70.0, 4.85, 80, 8, 128, 8192)Expected output:
8B, 8k ctx, 24 GiB GPU: need 6.5 GiB, headroom +17.5 GiB, fits
70B, 8k ctx, 24 GiB GPU: need 43.0 GiB, headroom -19.0 GiB, does not fit
70B, 8k ctx, 48 GiB GPU: need 43.0 GiB, headroom +5.0 GiB, fitsThe 70B case on a 48 GiB card shows the pattern to expect. The weights take 39.5 GiB, the KV cache 2.5 GiB, and overhead about 1 GiB. The remaining 5 GiB is your safety margin, and the OS or other programs can use it up.
Choosing the budget number
The budget_gib value depends on which memory tier holds the model. The table lists what to put in for each.
| Setup | What to use as the budget | Common mistakes |
|---|---|---|
| Discrete GPU, model fully on GPU | VRAM minus what the desktop and other apps use (often 0.5 to 1.5 GiB) | Using the box-label VRAM figure |
| CPU only | System RAM minus the OS and other programs | Forgetting that a browser can hold several GiB |
| Apple silicon or other unified memory | Only the share the GPU may use, often well below total RAM | Assuming all unified memory is available to the GPU |
| Integrated GPU with shared system RAM | System RAM, but check the driver's cap on GPU-visible memory | Assuming the limit is the same as the RAM size |
On unified memory systems, the operating system sets a ceiling on how much the GPU can wire down. On many Macs the default is a fraction of total RAM, roughly two thirds to three quarters, and larger machines allow a higher fraction. Some systems let you raise the limit with a setting. Look up the exact figure for your OS version before you commit to a model that is close to the edge.
What to do when the numbers do not fit
Work through the levers in order of how little they hurt quality.
- Shorten the context. KV cache memory is linear in context. Dropping from 32,768 to 8,192 tokens cuts the KV cache of the 8B model above from 4.0 GiB to 1.0 GiB. If you rarely use long prompts, this costs you nothing.
- Quantise the KV cache. Runtimes such as llama.cpp can store K and V in 8-bit instead of 16-bit, which halves the KV cache. Lower bit widths save more but can hurt output quality, so test on your own prompts.
- Use a smaller quantisation. Moving from Q6K to Q4K_M saves roughly a quarter of the weight memory. Below about 4 bits, quality loss becomes easier to notice (see the quality chapter).
- Use a smaller model. A well-trained 8B model at Q6_K often beats a 70B model squeezed into 2-bit weights.
- Offload only part of the model. If the weights do not fit in VRAM but fit in VRAM plus system RAM, you can place some layers on the GPU and the rest on the CPU. It runs, but slower than a full fit. A later chapter covers it.
Edge cases that break estimates
- Context is allocated up front. Many runtimes reserve the KV cache for the full context length you configure, not for the tokens you have used so far. Setting a 128k context "just in case" can cause an out-of-memory error on the first request.
- Parallel requests multiply the cache. A server that handles four conversations at once needs a KV cache for each. Multiply the KV figure by the number of simultaneous sequences.
- Files on disk are not the same as memory in use. Runtimes often memory-map the file, so the file can appear to load instantly. If the pages do not fit in RAM, the OS swaps them in from disk and speed collapses. A model that "runs" but produces a token every few seconds is often doing this.
- Vision and embedding components. Multimodal models carry an extra projector file and need additional memory for image tokens. Add them to the weights figure.
- Mixture-of-experts models need memory for all experts even though each token uses only some. Size the fit by total parameters, not active ones.
- Fragmentation and peaks. Prompt processing can use more scratch memory than generation does. A model that fits during chat may fail when you paste a long document.
Verifying your estimate
Estimates only get you to the right neighbourhood. Load the model with the context length you intend to use and compare the runtime's own memory report against your numbers. If the real figure is more than about 10 percent above your estimate, check for a larger batch size, parallel sequences, or a model with a different head layout than you assumed. Read the layer count, KV head count and head size from the model's metadata rather than guessing them from the parameter count.
Exercise
A model has 13B parameters, 40 layers, 8 KV heads and head size 128. You plan to run it at about 4.85 bits per weight with a 16-bit KV cache and a 16,384-token context, with 1 GiB of runtime overhead. Will it fit in a 12 GiB GPU that is also driving your desktop and using about 1 GiB? What single change is most likely to make it fit?
Show answer
Weights are 13e9 × 4.85 / 8 / 2^30 ≈ 7.3 GiB. KV cache is 2 × 40 × 8 × 128 × 16384 × 2 / 2^30 = 2.5 GiB. Adding 1 GiB of overhead gives about 10.8 GiB. The usable budget is 12 − 1 = 11 GiB, so it fits by only about 0.2 GiB, which is under 2% headroom and below the 10% margin. Halving the context to 8,192 tokens reduces the KV cache to 1.25 GiB and leaves about 1.5 GiB of headroom (about 14%). Quantising the KV cache to 8-bit would give a similar saving.