Sub topic 8
Checking whether a model fits
This chapter assumes you can estimate weight memory from parameter count and bits per weight, and that you know what the KV cache is. Here you combine those pieces into one check against the memory you have.
The budget has three parts
A model fits when the sum of three things is below the memory available:
- Weights. Parameters times bits per weight, divided by 8.
- KV cache. It grows linearly with context length.
- Runtime overhead. This covers compute buffers, the CUDA or Metal context, and scratch space. It is often 0.5 to 1.5 GiB. Treat it as a margin, not a precise figure.
The memory available is not the number on the box. The operating system, the desktop and other programs also use memory. On a GPU with dedicated VRAM, the display may take some of it. On unified memory, the system usually caps how much the GPU may use.
A worked check
The script below extends the earlier estimates. It reports the total need and the headroom left.
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):
return 2 * layers * kv_heads * head_dim * context * bytes_per_value / 2**30
def check_fit(budget_gib, w_gib, kv_gib, overhead_gib=1.0):
need = w_gib + kv_gib + overhead_gib
return need, budget_gib - need
w = weights_gib(8.0, 4.85)
for context in (8192, 32768):
kv = kv_cache_gib(32, 8, 128, context)
need, spare = check_fit(8.0, w, kv)
verdict = "fits" if spare >= 0 else "does not fit"
print(f"context {context}: need {need:.1f} GiB, spare {spare:+.1f} GiB, {verdict}")Run against an 8 GiB card, this prints:
context 8192: need 6.5 GiB, spare +1.5 GiB, fits
context 32768: need 9.5 GiB, spare -1.5 GiB, does not fitThe weights stay at 4.5 GiB in both runs. Only the KV cache changes, from 1.0 GiB to 4.0 GiB. Context length can decide whether a model fits.
What to do when it does not fit
- Shorten the context. This cuts the KV cache directly.
- Choose a smaller quantisation. Moving from Q80 to Q4K_M cuts weight memory by roughly 40 percent. Quality drops somewhat, which a later chapter covers.
- Use a smaller model. A model with fewer parameters is the most reliable fix.
- Split the model. Keep some layers on the GPU and the rest in system RAM. This works, but speed falls toward system RAM speed, as the offload chapter explains.
Common errors
- Using the file size as the full requirement. The file is only the weights.
- Forgetting that a larger context was set in the application. Many tools default to a small context, then fail after you raise it.
- Counting total RAM instead of free RAM. Browsers and other programs may already hold several GiB.
- Mixing GB and GiB. A 4.85 GB file is about 4.5 GiB. The gap grows with larger models, so keep one unit throughout.
Exercise
An 8B model at about 4.85 bits per weight (32 layers, 8 KV heads, head size 128, 16-bit KV cache) is run with a 32768-token context on an 8 GiB GPU. Allow 1 GiB of overhead. Does it fit? Name one change that would make it fit.
Show answer
Weights are about 4.5 GiB. The KV cache is 2 × 32 × 8 × 128 × 32768 × 2 bytes, which is 4.0 GiB. With 1 GiB of overhead the total is about 9.5 GiB, more than 8 GiB, so it does not fit. Reducing the context to 8192 tokens lowers the KV cache to 1.0 GiB and the total to about 6.5 GiB, which fits. Offloading some layers to system RAM or choosing a smaller quantisation would also work.