Sub topic 16
Choosing hardware and a quantisation for your goal
This chapter assumes you know how to estimate weight memory from parameters and bits per weight, how the KV cache grows with context, and that token generation speed is capped by memory bandwidth divided by model size. Here you combine those pieces into a decision.
Start from the goal, not the hardware
Pick the constraint that matters most for your use:
- Quality: coding help or long-document analysis needs a larger model or a higher-bit quantisation.
- Speed: interactive chat feels good at roughly 10 or more tokens per second.
- Context length: summarising long documents needs a large KV cache.
- Cost: a used GPU, a Mac with unified memory, and a CPU-only box differ a lot in price per GiB of fast memory.
Pick the largest quantisation that fits
Higher-bit files lose less quality, so choose the largest quantisation that fits your budget with headroom. Reserve about 1 GiB for runtime overhead. The bits-per-weight figures below are approximate.
BITS = {"Q8_0": 8.5, "Q6_K": 6.56, "Q5_K_M": 5.7, "Q4_K_M": 4.85, "Q3_K_M": 3.9}
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 best_quant(budget_gib, params_billion, context, overhead_gib=1.0):
kv = kv_cache_gib(32, 8, 128, context)
for name, bits in sorted(BITS.items(), key=lambda item: -item[1]):
total = weights_gib(params_billion, bits) + kv + overhead_gib
if total <= budget_gib:
return name, round(total, 1)
return None
print(best_quant(budget_gib=8, params_billion=8.0, context=8192))This prints ('Q5_K_M', 7.3). Q6_K would need about 8.1 GiB, which is over the 8 GiB budget.
Check speed before you buy
Divide memory bandwidth by the file size to get a ceiling. A 5.7 GB file on 100 GB/s memory gives at most about 17 tokens per second. Real speed will be lower, so run llama-bench on your own hardware or on similar hardware before you commit.
| Situation | Reasonable choice |
|---|---|
| Fits fully in VRAM | Full offload, highest quant that leaves headroom |
| Slightly too big for VRAM | Drop one quant level, or shorten context |
| Much too big for VRAM | Partial offload, and expect speed closer to system RAM |
| Large model, tight budget | Mixture-of-experts model, since only some experts run per token |
| Long context needed | Budget for the KV cache first, then choose the quant |
Common errors
- Counting only the weights and forgetting the KV cache and overhead.
- Choosing by parameter count alone. A 4-bit 13B model usually beats an 8-bit 7B model, but a 2-bit or 3-bit file can degrade noticeably.
- Assuming a mixture-of-experts model needs less memory. It is faster per token, but all experts still have to be stored.
- Treating the bandwidth ceiling as the expected speed.
- Expecting partial offload to scale smoothly. Layers left on the CPU run at system RAM speed and slow every token.
Exercise
You have a GPU with 12 GiB of VRAM and want to run the 8B model from this chapter (32 layers, 8 KV heads, head size 128) with a 32768-token context. Does Q6_K fit with 1 GiB of overhead? What would you change if it does not?
Show answer
The KV cache is 2 × 32 × 8 × 128 × 32768 × 2 bytes = 4 GiB. Q6K weights are about 6.11 GiB. The total with overhead is about 11.1 GiB, so it fits, but only just. If it did not fit, you would drop to Q5KM or Q4K_M, shorten the context, or offload some layers.