C.W.K.
Stream
Lesson 04 of 12 · published

Quantization: Q4, Q5, Q8 in Practice

~12 min · quantization, deployment

Level 0Token
0 XP0/94 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Quantization reduces the precision of model weights to save memory and accelerate inference. Modern post-training quantization techniques (GPTQ, AWQ, GGUF) lose surprisingly little quality.

QuantizationBits / param7B model sizeQuality impact
FP161614 GBBaseline
INT8 / Q887 GBNear-zero quality loss
Q55~4.4 GBVery minor; often imperceptible in chat
Q443.5 GBSmall loss (~1-3%); the sweet spot for most uses
Q33~2.6 GBNoticeable degradation
Q22~1.8 GBMajor degradation

Format families

  • GPTQ (Frantar et al., 2022): post-training, calibration-data based, 4-bit weights with FP16 activations. Standard for serving.
  • AWQ (Lin et al., 2023): activation-aware weight quantization. Identifies salient weights via activation magnitudes and protects them. Often slightly higher quality than GPTQ at the same bit width.
  • GGUF: file format used by llama.cpp. Supports many quantization schemes (Q4_K_M, Q5_K_S, Q8_0, etc.). Common for on-device inference.

For most production use, Q4 is the right starting point: fits a 70B model on a single high-end GPU, quality loss is typically 1-3 points on benchmarks, often invisible in chat. Drop to Q5 or INT8 when quality matters more; drop to Q3 only when memory is the absolute constraint.

Code

Loading a quantized model·python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# Llama 3.3 70B in 4-bit via bitsandbytes
model = AutoModelForCausalLM.from_pretrained(
    "meta-llama/Llama-3.3-70B-Instruct",
    quantization_config={"load_in_4bit": True,
                         "bnb_4bit_compute_dtype": torch.bfloat16},
    device_map="auto",
)
# Now ~35 GB instead of ~140 GB; runs on a single 80GB GPU.
GGUF / llama.cpp on Mac (no GPU)·python
# Install llama-cpp-python, download a GGUF file (Q4_K_M is a great default)
from llama_cpp import Llama

llm = Llama(
    model_path="llama-3.3-70b-instruct.Q4_K_M.gguf",
    n_ctx=8192, n_gpu_layers=-1,
)
print(llm("Explain attention in three sentences:")["choices"][0]["text"])

External links

Exercise

Take the same Llama 3 8B in FP16, INT8, Q5_K_M, Q4_K_M. Run a fixed eval set (10 chat prompts, 10 math problems, 10 code completions). Score each output. Plot quality vs. quantization. Where does the cliff start for your tasks?

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.