Quantization (INT8, INT4, FP8)

In one sentence

Store weights (and optionally activations and KV cache) in 8 or 4 bits instead of 16: 2–4× less memory and faster decode; nearly free at 8-bit, a quality cost to measure at 4-bit.

Analogy: Saving a photo as a high-quality JPEG: a quarter of the size and nobody notices, until you compress so hard that fine text in the image becomes unreadable.

Weight-only INT4 for an 8B model: quantize once offline with a small calibration set; at runtime the kernel reads 4-bit weights and dequantizes on the fly.

How it works

  • Each group of values keeps a scale factor that maps the low-bit numbers back.
  • Decode reads every weight for each token, so fewer bytes per weight means less time per token.
  • W8A8 (FP8/INT8) quantizes weights and activations, is usually near-lossless and also speeds up prefill.
  • W4A16 shrinks weights ~4× and dequantizes inside the matmul; GPTQ and AWQ are the standard serving formats.
  • 4-bit costs little on 70B models but more on 7–8B, math, code and long context: rerun your evals.

Check yourself

Answer to mark it learned; your answers aren't scored.

You serve a 70B model at batch 64+ on H100s. A teammate switched it to weight-only INT4 (AWQ) to raise throughput, but tokens/s did not improve and even dropped a little. What do you recommend?

Go deeper

GPU memory: weights + KV cache Interactive lab
Full explanation

Quantization stores a model's numbers in fewer bits: weights (and sometimes activations and the KV cache) go from 16-bit floats to 8 or 4 bits, plus a scale factor per group of values to map them back.

Why it speeds things up: low-batch decode is memory-bandwidth bound, because each new token reads every weight from HBM (see Prefill vs decode). Fewer bytes per weight means less time per token, and the freed memory goes to KV cache and bigger batches.

From safest to most aggressive:

  • FP8 / INT8, W8A8: weights and activations in 8 bits, running on low-precision tensor cores (FP8 needs Ada or Hopper and newer). Usually near-lossless, and it also speeds up compute-bound prefill. INT8 needs outlier handling (LLM.int8(), SmoothQuant).
  • INT4 weight-only, W4A16: weights ~4× smaller than FP16, dequantized to 16-bit inside the matmul. GPTQ (second-order error correction) and AWQ (scales up the ~1% of salient channels, found from activations) are the standard serving formats. bitsandbytes NF4 is the easy quantize-on-load option behind QLoRA. GGUF (llama.cpp, e.g. Q4_K_M) targets CPUs, Macs and laptops.
  • 3 bits or fewer: large quality drops except on the biggest models.

Rule of thumb: 8-bit is safe; 4-bit costs little on 70B models but visibly more on 7–8B ones and on math, code and long context. Never ship one without re-running your own evals.

Code Load a model in 4-bit, two ways
pythonLoad a model in 4-bit, two ways
import torch
from transformers import AutoModelForCausalLM, BitsAndBytesConfig

MODEL = "meta-llama/Llama-3.1-8B-Instruct"  # any causal LM on the Hub

# A) Quantize on load with bitsandbytes NF4: easy, QLoRA-style, not the fastest kernels
bnb = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",              # 4-bit 'normal float', suits ~Gaussian weights
    bnb_4bit_use_double_quant=True,         # quantize the scales too: ~0.37 bit/param saved
    bnb_4bit_compute_dtype=torch.bfloat16,  # matmuls run in bf16 after dequantizing
)
model = AutoModelForCausalLM.from_pretrained(MODEL, quantization_config=bnb, device_map="auto")
print(f"{model.get_memory_footprint() / 1e9:.1f} GB")  # ~16 GB in bf16 -> ~5-6 GB

# B) Production: serve a pre-quantized AWQ/GPTQ checkpoint with fast kernels
from vllm import LLM

llm = LLM(
    model="hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4",  # example AWQ repo
    # quantization is auto-detected from the checkpoint config; passing quantization="awq"
    # by hand forces the plain AWQ kernel instead of the faster awq_marlin one
    kv_cache_dtype="fp8",   # optional: halve the KV cache too; re-check long-context evals
)
Real example

A team serves Llama-3.1-70B. In BF16 the weights alone are ~140 GB: two 80 GB H100s, with little room left for KV cache. Switching to FP8 (~70 GB of weights) on the same two GPUs frees ~70 GB for KV, so batches grow several times and cost per token drops; their golden set moves by less than half a point. An AWQ INT4 build (~40 GB) fits on a single H100 and decodes faster at low concurrency, but loses 3 points on their text-to-SQL eval, so they use it only for a cheaper internal tier.

Trade-offs +4 · −3

+ You gain

  • ~2× (8-bit) to ~4× (4-bit) less weight memory: fewer GPUs, bigger batches, longer contexts.
  • Faster memory-bound decode; FP8 W8A8 also speeds compute-bound prefill on Hopper/Ada.
  • Mainstream: pre-quantized checkpoints and kernels in vLLM, SGLang, TensorRT-LLM and llama.cpp.
  • Makes local and edge deployment possible: an 8B model in 4-bit GGUF runs on a laptop.

− You pay

  • Quality loss is uneven: small models, math, code, long context and non-English degrade first, and averages hide it.
  • Weight-only INT4 gains vanish, and can turn into a slowdown, at large batch sizes.
  • Hardware and kernel lock-in: FP8 needs Ada/Hopper or newer, and format support varies per engine.

Use it when

Self-hosting when memory or cost is the constraint: FP8 as the default on Hopper/Ada, INT4 AWQ/GPTQ to fit a bigger model on fewer GPUs or cut low-concurrency latency, GGUF for laptops and edge.

Avoid it when

When you can't measure the quality hit on your own evals, on small models where every bit counts, or for high-throughput batch serving where weight-only INT4 buys little: use FP8 there.

Numbers to know 4
  • Bytes per parameter: BF16 2, FP8/INT8 1, INT4 0.5. A 70B model is ~140 / ~70 / ~35 GB, plus ~5–10% for scales.
  • Rule of thumb: batch-1 decode ceiling ≈ HBM bandwidth ÷ weight bytes. 8B on an H100 (3.35 TB/s): ~200 tok/s in BF16, roughly 3× that in INT4.
  • GPTQ quantized 175B-parameter models to 3–4 bits in ~4 GPU hours.
  • QLoRA's double quantization saves ~0.37 bits per parameter (~3 GB on a 65B model).
Common pitfalls 4
  • Judging only by perplexity or MMLU: damage shows first in long context, reasoning, tool-call formats and non-English. Run your task evals.
  • Serving production traffic with bitsandbytes NF4: it was built to save memory in QLoRA training, and AWQ/GPTQ kernels decode much faster.
  • Calibrating GPTQ/AWQ on English Wikipedia and then serving Spanish code.
  • Expecting weight-only INT4 to raise throughput at batch 64+: you are compute-bound there, so FP8 W8A8 is the lever.
Interview questions 2

Answer out loud first, then reveal.

We need to serve a 70B model more cheaply. How do you approach quantization?Show answer

I start from the bottleneck. If we are memory-bound, low concurrency or out of VRAM, weight-only INT4 with AWQ or GPTQ cuts weights ~4× and speeds decode. If we run large batches, FP8 W8A8 on H100s is my default: near-lossless, and it speeds the compute-bound side too. Then I gate on evals, not perplexity: our golden set with slices for long context, code and non-English, where 4-bit breaks first. And I compare against the real alternative, a smaller model in BF16 at equal cost.

Why does 4-bit quantization speed up decode but not necessarily prefill?Show answer

Decode produces one token per sequence per step, so each step reads every weight from HBM to do very little math: it is bandwidth-bound, and reading ~4× fewer bytes speeds it up almost proportionally. Prefill processes thousands of tokens per weight read, so it is compute-bound. Weight-only INT4 still dequantizes to 16-bit and runs 16-bit matmuls, so prefill gains nothing and pays some overhead. To speed prefill you need low-precision math itself: FP8 or INT8 W8A8 tensor cores.