Quantization explained: what Q4_K_M means and how much memory a local model really needs
Quantization stores a model's numbers with fewer bits, so the file shrinks: Llama 3.1 8B is about 16 GB at 16-bit and a 4.92 GB file as Q4_K_M. On llama.cpp's own test, Q4_K_M raised perplexity by 2.8% and Q2_K by 56.5%, so 4-bit to 6-bit is the safe zone. To know if a model fits, add the file size and the KV cache, which grows with context: 17.18 GB for Llama 3.1 8B at 128K tokens.
- 1
What is quantization?
A model is billions of numbers (weights). Training usually stores each one in 16 bits. Quantization stores them in fewer bits, such as 8, 5 or 4, so the file gets smaller and usually faster. llama.cpp describes it as reducing “the precision of model weights”, which “shrinks the model’s size and can speed up inference.”
The size rule is simple: parameters × bits per weight ÷ 8 = bytes. Real 4-bit files are a bit more than 4 bits per weight, because they also store small scale numbers and keep some tensors at higher precision. That is why Q4_K_M is 4.89 bits per weight, not 4.
The catch is some lost accuracy. llama.cpp measures it with perplexity (how surprised the model is by real text) and KL divergence (how far its answers drift from the 16-bit model). Step 3 shows the numbers.
Llama 3.1 8B (llama.cpp quantize table) F16 16.00 bits 14.96 GiB Q8_0 8.50 bits 7.95 GiB Q6_K 6.56 bits 6.14 GiB Q4_K_M 4.89 bits 4.58 GiB Q2_K 3.16 bits 2.95 GiB Check: 8.03B × 4.89 ÷ 8 ≈ 4.9 GB (1 GiB = 1.074 GB)Key ideaFile size ≈ parameters × bits per weight ÷ 8. Fewer bits, smaller file, a little less accuracy.
- 2
How to read a GGUF file name: Q4_K_M, IQ4_XS, Q8_0
Most local models come as GGUF files. The GGUF spec calls it a binary format “designed for fast loading and saving of models”, with everything needed to load the model in one file. Hugging Face adds that it “encodes both the tensors and a standardized set of metadata”, so the file carries the architecture, the tokenizer and the quant type.
The name tells you the quant. Q4 means about 4 bits per weight. K marks the newer k-quants, which pack weights in super-blocks with their own scales (Hugging Face: “Super-blocks with 8 blocks, each block has 32 weights” for Q4_K). The last letter is the mix: llama.cpp’s tool lists Q4_K as an “alias for Q4_K_M”, and the S and M versions differ in how many tensors keep extra bits, which you can see in bits per weight (Q4_K_S 4.67, Q4_K_M 4.89).
IQ types (IQ4_XS, IQ2_M) are i-quants: smaller for similar quality. bartowski, who publishes many popular GGUF files, says to look at I-quants below Q4 on Nvidia or AMD GPUs, and notes they run slower than K-quants on CPU and Apple Metal. Names ending in _0 (Q4_0, Q8_0) are the older simple type, which Hugging Face calls legacy; bartowski still ships Q8_0 as the near-lossless option.
Decode the name Llama-3.1-8B-Instruct-Q4_K_M.gguf │ │ └ M = medium mix │ └── K = k-quant (super-blocks) └──── 4 ≈ 4-bit weights IQ4_XS = i-quant, 4-bit, extra small Q8_0 = 8-bit, simple blocks of 32Key ideaIf you do not want to think about it, bartowski’s advice is to “grab one of the K-quants”. Q4_K_M is the usual default.
- 3
How much quality do you lose at each level?
llama.cpp publishes a scoreboard for Llama 3 8B, measured against the 16-bit model on the same text. Down to Q5_K_M the change is under 1%. Q4_K_M adds 2.8% to perplexity, Q3_K_M 10.5%, and Q2_K jumps 56.5%. The drop is a cliff, not a slope: it stays small until about 4 bits, then falls fast.
An importance matrix (imatrix) helps: quantizers run sample text through the model first to learn which weights matter most. On the same scoreboard, Q4_K_M with an imatrix adds 2.4% instead of 2.8%, and Q2_K's perplexity falls from 9.75 to 8.65. Many GGUF uploads say “imatrix” in the description.
Some models lose more than others. The same README compares Llama 2 7B and Llama 3 8B: at Q2_K, Llama 3’s mean perplexity ratio was 1.56 against Llama 2’s 1.11. Newer models trained on more data seem to lose more at low bits, so treat Q2 as a last resort.
Llama 3 8B · llama.cpp perplexity scoreboardQ4_K_M costs 2.8% in perplexity; below 4 bits the loss climbs fast- Q8_0+0.04%
- Q6_K+0.35%
- Q5_K_M+0.91%
- Q4_K_M+2.8%
- IQ4_XS+3.7%
- Q3_K_M+10.5%
Perplexity rise vs the 16-bit reference (F16 perplexity 6.23), lower is better. Q2_K is off this scale at +56.5%. IQ4_XS row used an importance matrix; the others did not. Measured on CUDA, RTX 4090.Source: llama.cpp: tools/perplexity/README.md (LLaMA 3 8b scoreboard) · Nov 29, 2026 View as table
Item Value Note Q8_0 +0.04% Q6_K +0.35% Q5_K_M +0.91% Q4_K_M +2.8% IQ4_XS +3.7% with imatrix Q3_K_M +10.5% Key ideaQ6_K and Q5_K_M are near-lossless. Q4_K_M is the sweet spot. Go below Q4 only when nothing else fits.
- 4
The memory people forget: the KV cache
The file size is not the whole bill. While it reads your prompt and writes its answer, the model keeps keys and values for every token in a KV cache. It grows with context length. NVIDIA gives the formula as 2 × layers × heads × head size × bytes per number, per token; newer models share KV heads (grouped-query attention), so you use num_key_value_heads from config.json.
Llama 3.1 8B has 32 layers, 8 KV heads and a head size of 128. At 16-bit that is 131,072 bytes per token: 0.54 GB at 4K tokens, 4.29 GB at 32K and 17.18 GB at its full 128K context, more than three times the Q4_K_M file itself.
Two settings control it. Ollama picks a default context from your VRAM: 4K under 24 GiB, 32K from 24 to 48 GiB, and 256K at 48 GiB or more. OLLAMA_CONTEXT_LENGTH overrides it, so set it yourself on a big machine. And the cache itself can be quantized: Ollama’s q8_0 uses “approximately 1/2 the memory of f16 with a very small loss in precision” and q4_0 about 1/4, with Flash Attention on. llama.cpp has the same through --cache-type-k and --cache-type-v.
KV cache, Llama 3.1 8B, f16 per token: 2 × 32 × 8 × 128 × 2 bytes = 131,072 bytes 4,096 tokens → 0.54 GB 32,768 tokens → 4.29 GB 131,072 tokens → 17.18 GB Q4_K_M file → 4.92 GBKey ideaMemory needed = file size + KV cache. Long context can cost more than the model.
- 5
Pick the right file for your machine
Put it together and you can choose a file before a 40 GB download. bartowski’s rule of thumb is a file “1-2GB smaller than your GPU's total VRAM”; add RAM too if you accept slower speed. On a Mac, memory is shared, so leave room for the system.
Here is a worked case with real file sizes. Llama 3.3 70B on 48 GiB of GPU memory (51.54 GB in file-size units, because systems count in GiB): at 8K context the Q4_K_M file fits; at 32K the KV cache grows to 10.74 GB and it no longer does. You can drop to a smaller file, or keep Q4_K_M and halve the cache.
Worked workflowWorked workflow: Llama 3.3 70B on 48 GiB
Input48 GiB of GPU memory (51.54 GB, the unit file sizes use). Model: Llama 3.3 70B (80 layers, 8 KV heads, head size 128). You want 32K context for long documents.
Start at Q4_K_M, 8K context
File 42.52 GB + KV 2.68 GB = 45.2 GB. 6.34 GB left: fits.
Raise context to 32K
KV cache grows to 10.74 GB. Total 53.26 GB: 1.72 GB short.
Option A: a smaller file
IQ4_XS (37.9 GB) + 10.74 GB = 48.64 GB, 2.9 GB left. Fits, at +3.7% perplexity on llama.cpp's 8B test (Q3_K_M would be +8.1%).
Option B: quantize the KV cache
Q4_K_M (42.52 GB) + q8_0 KV 5.37 GB = 47.89 GB, 3.65 GB left. Keeps Q4_K_M. Ollama turns Flash Attention on by itself where supported, which q8_0 needs.
OLLAMA_KV_CACHE_TYPE=q8_0 \ OLLAMA_CONTEXT_LENGTH=32768 ollama serve
OutputChoose option B: Q4_K_M with a q8_0 KV cache at 32K uses 47.89 GB of 51.54 GB and keeps the better file (+2.4% perplexity vs +3.7% for IQ4_XS on the 8B test, both with an importance matrix like bartowski's files). The q8_0 cache itself costs "a very small loss in precision", per Ollama.
Key ideaPick context length first, then the biggest quant that leaves 2 GB free.
Straight from the source
The original idea, then the same idea taken apart, then how you use it today.
- 1 · Understand
- They showed a model can be squeezed after training, with no retraining, “down to 3 or 4 bits per weight, with negligible accuracy degradation”, and ran a 175-billion-parameter model on a single GPU.
- 2 · Break it down
- Post-training: you take finished weights and round them smartly, using a little sample data
- It is one-shot and fast: about four GPU hours for a 175B model, by the paper’s count
- 3–4 bits was the point where quality stayed close to the original
- 3 · Use it
- This is why 4-bit downloads are the default today. GPTQ and AWQ files, and the Q4_K_M-style types inside GGUF files, are all post-training quantizations of the same original model.
- 1 · Understand
- They introduced 4-bit NormalFloat (NF4), built for the bell-curve shape of model weights, and fine-tuned “a 65B parameter model on a single 48GB GPU” while keeping 16-bit fine-tuning quality.
- 2 · Break it down
- Weights cluster near zero, so NF4 spends its 16 levels where the weights actually are
- The base model stays frozen at 4-bit; small adapter layers are trained on top
- Quantization is not only for running models: it made fine-tuning big models affordable
- 3 · Use it
- If you fine-tune an open model on one GPU, you will likely load it in 4-bit NF4 with QLoRA. For running it, you will usually convert the result to GGUF.
Words to know
- Quantization
- Storing model weights with fewer bits (8, 5, 4…) to make the file smaller and faster, with some loss of accuracy.
- GGUF
- The single-file model format used by llama.cpp, Ollama and LM Studio. It holds the weights and the metadata needed to run them.
- K-quant (Q4_K_M)
- llama.cpp quant types that store weights in super-blocks with their own scales. S, M and L are smaller or larger mixes.
- Bits per weight (bpw)
- The real average storage per weight, including scales. Q4_K_M is about 4.89 bpw on Llama 3.1 8B.
- Perplexity
- How surprised a model is by real text. Lower is better; the rise versus the 16-bit model shows quantization loss.
- KV cache
- Memory that holds keys and values for every token in the context. It grows with context length and adds to the file size.
Watch
Hand-picked from the people who explain this best. The video loads only when you press play.
Walks through what the Q numbers in a model name mean and how to pick one when you run models with Ollama. Good if you already run models locally.
A short, visual explanation of why fewer bits shrink a model and what you trade for it. Watch first if the idea is new.
Will this model fit your machine?
Pick a model, a quant type and a context length. Memory = the real file size plus the KV cache; the bar shows both against your memory.
Fits: needs 5.46 GB of 17.18 GB (16 GiB).
- Weights
- 4.92 GB
- real file size
- KV cache
- 0.54 GB
- 4,096 tokens, f16
- Your memory
- 16 GiB
- = 17.18 GB · keep 2 GB free
- Quality cost of Q4_K_M
- +2.8%
- perplexity vs F16, Llama 3 8B
This fits. 11.72 GB stays free for your apps and the runtime. Best file for 16 GiB at this context: .
File sizes are GB (10^9 bytes); the memory your system shows is GiB, about 7% more, so we convert it. On a Mac, macOS keeps part of unified memory for itself, so the GPU can use less than the total. Splitting a model between GPU and RAM works but runs slower.
| Quant | Bits / weight | Perplexity rise | Llama 3.1 8B | Qwen3 32B | Llama 3.3 70B |
|---|---|---|---|---|---|
| 16/32-bit | 16 / 32 | baseline | 32.13 (F32) | 65.53 (BF16) | 141.12 (F16) |
| Q8_0 | 8.50 | +0.04% | 8.54 | 34.82 | 74.98 |
| Q6_K | 6.56 | +0.35% | 6.60 | 26.88 | 57.89 |
| Q5_K_M | 5.70 | +0.91% | 5.73 | 23.21 | 49.95 |
| Q4_K_M | 4.89 | +2.8% | 4.92 | 19.76 | 42.52 |
| IQ4_XS | 4.46 | +3.7%* | 4.45 | 17.69 | 37.90 |
| Q3_K_M | 4.00 | +10.5% | 4.02 | 15.97 | 34.27 |
| Q2_K | 3.16 | +56.5% | 3.18 | 12.34 | 26.38 |
Sources: llama.cpp quantize (bits per weight), perplexity scoreboard (rise vs F16; *with importance matrix), model links (file sizes). Checked Nov 29, 2026.Swipe →
Try it
Pick a model file for your own machine (10 minutes)
- 1Find your memory: Windows Task Manager → Performance (GPU memory and RAM); Mac: Apple menu → About This Mac.
- 2In the calculator above, choose Llama 3.1 8B, type your memory, and keep 4K context. Note the best file it suggests.
- 3Switch context to 32K and 128K. Watch the KV cache bar grow and the suggestion change.
- 4Pick Custom, open the config.json of a model you are curious about on Hugging Face, and copy num_hidden_layers, num_key_value_heads and head_dim.
- 5Write one line you could say in an interview: “At Q4_K_M it is X GB, plus Y GB of KV cache at Z tokens, so it needs about N GB.”
It checks what you understood, then gives you a small real task to try.
Read the prompt
Be my patient tutor. I just studied this lesson: "Quantization: GGUF, Q4_K_M and memory". Summary: Quantization stores a model's numbers with fewer bits, so the file shrinks: Llama 3.1 8B is about 16 GB at 16-bit and a 4.92 GB file as Q4_K_M. On llama.cpp's own test, Q4_K_M raised perplexity by 2.8% and Q2_K by 56.5%, so 4-bit to 6-bit is the safe zone. To know if a model fits, add the file size and the KV cache, which grows with context: 17.18 GB for Llama 3.1 8B at 128K tokens. Key terms: Quantization, GGUF, K-quant (Q4_K_M), Bits per weight (bpw), Perplexity, KV cache. Do this, one step at a time, waiting for me each time: 1. Ask me to explain the main idea in my own words. Correct me kindly with one concrete example. 2. Ask 4 questions, easy to hard. After each answer, say what was right and what was missing. 3. Give me one small workflow I can try in 10 minutes that uses this idea in real work or study. Walk me through it step by step and check my result. 4. End with 3 lines: what I now know, what I should practise, what to learn next.
Check yourself
Pick an answer to see if you are right, and why.
Done? Keep going
Mark this lesson complete to keep your place. Progress is saved on this device, no login.
- AI Engineer interview guideSizing GPU memory for a model (weights plus KV cache at your context) is a common system-design question.Open the guide
- Forward Deployed Engineer interview guideOn-prem customers ask which model fits their hardware. Know how quant type and context change the answer.Open the guide
Questions people ask
Is Q4_K_M good enough?
For most local use, yes. On llama.cpp's Llama 3 8B test it raised perplexity by 2.8% over the 16-bit model, while Q5_K_M was 0.91% and Q6_K 0.35%. If you have room, Q5_K_M or Q6_K is closer to the original; below Q4 the loss rises fast (Q3_K_M 10.5%).
How much VRAM do I need for a 70B model?
The Llama 3.3 70B Q4_K_M file is 42.52 GB, Q8_0 is 74.98 GB and Q2_K is 26.38 GB. Add the KV cache: 1.34 GB at 4K tokens and 10.74 GB at 32K (f16). So plan for about 48 GB for Q4_K_M with a modest context, or split the model between GPU and RAM and accept slower output.
Is it better to run a big model at Q2 or a smaller model at Q4?
Usually the smaller model at Q4 or higher. llama.cpp's scoreboard shows Q2_K adding 56.5% perplexity on Llama 3 8B, and its README notes Llama 3 lost more than Llama 2 at Q2_K. If you must go below Q4, test both on your own task.
Sources and further reading
- llama.cpp: tools/quantize/README.md (Llama-3.1-8B table)
- llama.cpp: tools/perplexity/README.md (LLaMA 3 8b scoreboard)
- llama-quantize man page (Debian): quant types with size and perplexity
- ggml: GGUF file format spec (docs/gguf.md)
- Hugging Face Hub docs: GGUF
- bartowski/Meta-Llama-3.1-8B-Instruct-GGUF (Hugging Face)
- bartowski/Qwen_Qwen3-32B-GGUF (Hugging Face)
- bartowski/Llama-3.3-70B-Instruct-GGUF (Hugging Face)
- Llama 3.1 8B config.json (unsloth mirror of Meta weights)
- Llama 3.3 70B config.json (unsloth mirror)
- NVIDIA: Mastering LLM Techniques: Inference Optimization
- Sebastian Raschka: KV cache calculations
- Ollama FAQ: context length and K/V cache quantization
- Ollama docs: context length (defaults by VRAM)
- Ollama library: llama3.1:latest (8.03B, Q4_K_M, 4.9GB)
- llama.cpp server README: --cache-type-k / --cache-type-v
- LM Studio docs: download a model (quantization)
- OpenAI: Introducing gpt-oss (MXFP4)
- Frantar et al. (2022): GPTQ, arXiv 2210.17323
- Dettmers et al. (2023): QLoRA, arXiv 2305.14314