Sub topic 16
Choosing hardware and a quantisation for your goal
This chapter turns the earlier estimates into a decision procedure. It assumes you can compute weight memory from parameters and bits per weight, and that you know the KV cache grows with context length. It also assumes you know that decode speed is bounded by memory bandwidth, and that you can run llama-bench. Here you combine these to choose a quantisation, and to decide whether a machine is worth buying or using.
Start from the goal, not the model
"Run the biggest model I can" is a poor goal. Write down three constraints first:
- Quality floor. The smallest model and highest compression that still does your task acceptably. Test this on your own prompts, not on leaderboards.
- Context length. The longest prompt plus answer you need. Chat needs a few thousand tokens. Summarising documents or working across a code base may need 32k or more.
- Speed target. Interactive use feels fine at roughly 10 to 15 tokens per second. Batch jobs can run slower. Long prompts also need fast prompt processing, which depends on compute, not bandwidth.
The decision procedure
Work through these steps in order. Each step can reject a candidate cheaply before you spend time downloading files.
- Pick a model family and parameter count that could meet your quality floor.
- Pick a quantisation. Start at Q4KM, the usual balance of size and quality.
- Add weights, KV cache, and runtime overhead. Compare the total with your memory budget.
- Divide memory bandwidth by file size to get a decode ceiling. Expect real speed well below it.
- Benchmark the best candidate. Change only one thing at a time.
A script that does steps 2 to 4
The script below sweeps several quantisations for one model and reports whether each fits and what its decode ceiling is. The bits-per-weight values are approximate averages for each GGUF type. Check your actual file size, since it is the number that matters.
QUANT_BITS = {
"Q8_0": 8.5,
"Q6_K": 6.6,
"Q5_K_M": 5.7,
"Q4_K_M": 4.85,
"Q3_K_M": 3.9,
}
def plan(params_b, layers, kv_heads, head_dim, context,
budget_gib, bandwidth_gb_s, overhead_gib=1.5):
kv = 2 * layers * kv_heads * head_dim * context * 2 / 2**30
for name, bits in QUANT_BITS.items():
file_bytes = params_b * 1e9 * bits / 8
total = file_bytes / 2**30 + kv + overhead_gib
ceiling = bandwidth_gb_s / (file_bytes / 1e9)
verdict = "fits" if total <= budget_gib else "too big"
print(f"{name:<7} {total:5.1f} GiB {verdict:<7} "
f"ceiling {ceiling:5.1f} tok/s")
# 8B model, 8192-token context, a GPU with 12 GiB and 360 GB/s.
plan(params_b=8.0, layers=32, kv_heads=8, head_dim=128,
context=8192, budget_gib=12, bandwidth_gb_s=360)Output:
Q8_0 10.4 GiB fits ceiling 42.4 tok/s
Q6_K 8.6 GiB fits ceiling 54.5 tok/s
Q5_K_M 7.8 GiB fits ceiling 63.2 tok/s
Q4_K_M 7.0 GiB fits ceiling 74.2 tok/s
Q3_K_M 6.1 GiB fits ceiling 92.3 tok/sEverything fits here, so the choice is about quality, not memory. The 12 GiB card can afford Q6K or even Q80 for an 8B model. Saving memory with Q3KM would buy speed you do not need and cost quality you can use. When memory is plentiful, spend it on a higher-precision quantisation or a longer context. Stretch to a larger model only if one fits.
Matching common goals to hardware
| Goal | What dominates | Sensible choice |
|---|---|---|
| Interactive chat with a 7B to 14B model | Bandwidth for decode speed | A GPU with 12 to 16 GiB, Q4_K_M to Q6_K |
| Long-document work at 32k+ context | KV cache size, prompt processing | Spend memory on context, not quant precision. Consider a quantised KV cache |
| Largest model on one machine, speed secondary | Total memory capacity | Unified memory (large Apple or similar systems) or a lot of system RAM, Q4 or Q5 |
| Mixture-of-experts model | Memory capacity for all experts, but only active experts are read per token | Large RAM or unified memory. Decode speed follows active parameters |
| Shared server, several users | Compute and total memory per request | A discrete GPU. Each user adds KV cache |
Treat the table as a starting point for the procedure above, not a replacement for it.
Choosing a quantisation level
Use the quality tradeoff from earlier chapters as a ladder:
- Q8_0 is close to the original quality. Use it when memory is free and you want a baseline.
- Q5KM and Q6_K give very small quality loss. They suit tasks with exact outputs, such as code or structured data.
- Q4KM is the default for most users. It is usually the best size per quality.
- Q3 and below loses quality faster, and small models suffer more. Use these only when the alternative is a model that does not fit at all.
A larger model at Q4 usually beats a smaller model at Q8 when both fit. Compare candidates of equal file size on your own prompts.
When the model does not fit
You have four levers, listed from least to most costly in quality:
- Shorten the context. KV cache scales linearly, so halving the context halves it.
- Quantise the KV cache, if your runtime supports it. This saves memory at some quality cost.
- Choose a lower quantisation of the same model.
- Offload only part of the model to the GPU with
-ngl. The layers left on the CPU run at system RAM bandwidth, so speed falls toward the slower tier as more layers stay behind.
Partial offload works but is rarely a first choice. If 90% of the layers are on the GPU, the remaining 10% still read at CPU memory speed on every token, and they can take a large share of each token's time.
Common mistakes
- Using the ceiling as a prediction. Real decode speed is often 50 to 80% of the ceiling. Always benchmark.
- Forgetting the KV cache. A model that fits at 4k context can fail at 32k.
- Mixing units. Bandwidth is in GB/s (decimal), while memory budgets are often in GiB. The difference is about 7%.
- Ignoring prompt processing. A machine with high bandwidth but little compute decodes well and still takes a long time to read a long prompt.
- Judging quality from perplexity alone. Test the actual task.
Verify the choice
Run the benchmark on the chosen file, then compare it with your estimate:
llama-bench -m model-Q4_K_M.gguf -p 512 -n 128 -ngl 99If measured generation speed is far below the ceiling, check that all layers are actually offloaded and that the model is not spilling into system RAM. If the speed meets your target and your test prompts give acceptable answers, the choice is done.
Exercise
A GPU has 24 GiB of memory and 1000 GB/s of bandwidth. You want a 32B model at Q4KM (4.85 bits per weight) with 64 layers, 8 KV heads, head size 128, and a 16-bit KV cache. Allow 1.5 GiB of overhead. Does it fit at 8192 tokens of context? At 32768? What is the decode ceiling?
Show answer
Weights are 32e9 × 4.85 / 8 = 19.4e9 bytes, which is 18.07 GiB. KV cache at 8192 tokens is 2 × 64 × 8 × 128 × 8192 × 2 bytes = 2 GiB, so the total is 18.07 + 2 + 1.5 = 21.6 GiB, and it fits. At 32768 tokens the KV cache is 8 GiB, so the total is 27.6 GiB, and it does not fit. The decode ceiling is 1000 / 19.4 ≈ 51.5 tokens per second, and real speed will be lower.
Exercise
A laptop with 64 GB of unified memory has about 100 GB/s of bandwidth. A desktop GPU has 16 GiB and 500 GB/s. For a 4.9 GB model file that fits on both, which decodes faster by the ceiling formula, and what is the main reason you might still choose the laptop?
Show answer
The laptop's ceiling is 100 / 4.9 ≈ 20 tokens per second. The GPU's ceiling is 500 / 4.9 ≈ 102 tokens per second, so the GPU decodes faster. The laptop's advantage is capacity. It can hold much larger models, or very long contexts, that cannot fit in 16 GiB.