Sub topic 7
Picking a model that fits your hardware: parameters and quantization
This chapter assumes you can run ollama pull and ollama run, and that you know how to read model names and tags. It also assumes you know roughly what a large language model is. The goal is to estimate, before you download anything, whether a model will fit in your memory and run at a usable speed.
What memory a model needs
A model is a large set of numbers called weights. Running it means holding those weights in fast memory. That memory is GPU memory (VRAM) if you have a supported GPU, or system RAM if you don't. On Apple Silicon, the CPU and GPU share one pool of unified memory.
Three things consume memory:
- The weights. This is the largest part and depends on parameter count and quantization.
- The KV cache. The model stores intermediate attention data for every token in the context window. It grows with
num_ctx, so a longer context needs more memory. - Runtime overhead. Buffers and the server itself add several hundred megabytes.
Parameter count
The number in a model name, such as 8b in llama3.1:8b, is the parameter count in billions. More parameters usually means better reasoning, better instruction following, and more memory use. Size classes, with typical use:
| Size | Typical use | Rough memory at 4-bit |
|---|---|---|
| 1B to 3B | Simple tasks, classification, fast local helpers | 1 to 2.5 GB |
| 7B to 9B | General chat, summarizing, light coding | 4.5 to 6 GB |
| 13B to 14B | Stronger writing and coding | 8 to 10 GB |
| 30B to 34B | High quality on a workstation | 18 to 22 GB |
| 70B | Near the top of open models | 40 to 45 GB |
These figures are approximate. Real files vary with architecture and quantization scheme.
Quantization
Weights are trained as 16-bit floating-point numbers. Quantization stores each weight with fewer bits, such as 8 or 4. This shrinks the file and the memory use, and it speeds up generation on most hardware, because generation is usually limited by how fast weights can be read from memory. The cost is a small loss of precision in the model's output.
Ollama models are stored in GGUF format. Quantization levels appear in tags with names like q4_K_M:
- The
qnumber is the approximate bits per weight. Kmarks the "K-quant" family, which gives more bits to the layers that are most sensitive.S,M, andLmean small, medium, and large variants within a level.
| Tag suffix | Bits per weight (approx.) | Quality | Notes |
|---|---|---|---|
| `fp16` | 16 | Reference | Largest. Rarely worth it for local use. |
| `q8_0` | 8.5 | Nearly identical to fp16 | Good when memory is plentiful. |
| `q5_K_M` | 5.7 | Very close | A good step up from q4. |
| `q4_K_M` | 4.8 | Good | The usual default in the library. |
| `q3_K_M` | 3.9 | Noticeable loss | Use only when memory forces it. |
| `q2_K` | 3.3 | Clear degradation | Last resort. |
If you pull a model without a quantization in the tag, you generally get a 4-bit variant. Run ollama show <model> to confirm the parameters and quantization it reports.
ollama show llama3.2Estimating size with a formula
You can estimate weight memory in gigabytes like this:
weights_GB ≈ parameters_in_billions × bits_per_weight / 8An 8B model at about 4.8 bits per weight needs roughly 8 × 4.8 / 8 = 4.8 GB. Add 1 to 2 GB for the KV cache at a modest context length, plus overhead.
This script applies the formula and compares the result with the memory you have:
def estimate_gb(params_billion: float, bits_per_weight: float, overhead_gb: float = 1.5) -> float:
"""Rough memory estimate: weights plus a flat allowance for KV cache and runtime."""
weights_gb = params_billion * bits_per_weight / 8
return weights_gb + overhead_gb
candidates = [
("llama3.2 3B q4_K_M", 3.2, 4.8),
("8B q4_K_M", 8.0, 4.8),
("8B q8_0", 8.0, 8.5),
("14B q4_K_M", 14.0, 4.8),
("70B q4_K_M", 70.0, 4.8),
]
available_gb = 16.0
for name, params, bits in candidates:
need = estimate_gb(params, bits)
verdict = "fits" if need <= available_gb * 0.8 else "too large"
print(f"{name:22s} needs about {need:5.1f} GB -> {verdict}")The script keeps a 20% margin (available_gb * 0.8) because the operating system and other programs also need memory. The flat 1.5 GB overhead is a simplification. A very long context can need more.
How the memory pool decides where the model runs
Ollama decides at load time how many model layers to place on the GPU.
Because a fenced diagram must use the required form, the decision flow is shown here.
You can see the result with ollama ps. The PROCESSOR column shows 100% GPU, 100% CPU, or a split such as 35%/65% CPU/GPU.
ollama psA split works but is slower than full GPU, because every token must pass through the CPU layers. If you see a split, a smaller model or a lower quantization often gives a bigger speedup than anything else you can tune.
Hardware rules of thumb
- Apple Silicon. Unified memory is shared, but macOS limits how much the GPU may use, often around two thirds to three quarters of total memory. A 16 GB Mac comfortably runs 7B to 9B models at 4-bit and struggles with 14B.
- NVIDIA GPUs. VRAM is the limit. An 8 GB card suits 7B to 8B at 4-bit with a moderate context. A 24 GB card suits 30B-class models at 4-bit.
- AMD GPUs. Supported on recent cards through ROCm. Check the Ollama documentation for your specific card.
- CPU only. Any model that fits in RAM will run, but expect a few tokens per second for 7B models. Prefer 1B to 3B models for interactive use.
Generation speed depends mainly on memory bandwidth, not on raw compute. A smaller or more heavily quantized model reads fewer bytes per token and so generates faster.
Context length changes the answer
The KV cache scales with num_ctx. A model that fits at 4096 tokens may not fit at 32768. If you raise the context, recheck memory with ollama ps after the model loads. The next chapter covers context settings in detail.
Decision criteria
Pick the model in this order:
- Find the largest memory pool you can use: VRAM, or about 70% of unified or system memory.
- Choose the largest parameter count that fits at
q4_K_Mwith your target context, keeping 20% free. - If you have spare memory, first raise the quantization to
q5_K_Morq8_0, or choose the next size up. A bigger model at 4-bit generally beats a smaller model at 8-bit. - If it is too slow or splits across CPU and GPU, step down one size or one quantization level.
Common errors
- "model requires more system memory than is available." The weights alone exceed your memory. Choose a smaller model or a lower quantization.
- Loads but is very slow. Check
ollama psfor a CPU/GPU split. Reduce size ornum_ctx. - Works at first, then fails on long prompts. The KV cache grew past available memory. Lower
num_ctxor use a smaller model. - Assuming the file size equals the memory need. Memory use is the file size plus KV cache and overhead, so it is always higher.
Exercise
You have a laptop with 16 GB of unified memory and want to run a 14B model at q4KM (about 4.8 bits per weight) with a 4096-token context. Estimate the memory needed using the chapter's formula and a 1.5 GB overhead, and say whether it fits with a 20% margin.
Show answer
Weights are 14 × 4.8 / 8 = 8.4 GB. Adding 1.5 GB gives about 9.9 GB. With a 20% margin, the usable budget is 16 × 0.8 = 12.8 GB, so it appears to fit. In practice macOS limits GPU memory to roughly two thirds to three quarters of total, around 10.7 to 12 GB, so it is tight and may partly run on the CPU. Check with ollama ps, and fall back to an 8B model or q3 if it splits.
Exercise
Why does lowering quantization from q80 to q4K_M usually make generation faster, not just smaller?
Show answer
Generation is mostly limited by memory bandwidth, because each new token requires reading all the weights. With about half as many bits per weight, roughly half as many bytes are read per token, so tokens come out faster. It may also let the whole model fit on the GPU instead of splitting across CPU and GPU.