# What Is Quantization?

> What quantization is, how it shrinks a model's memory, what it costs in accuracy, explained simply for your AI interview.

**In short:** Quantization stores a model's weights in fewer bits, such as 8 or 4 instead of 16, so the model needs less memory. The price is some accuracy, so test the smaller model on your own task before you ship it.

AI and LLM interview question Q311, Inference, performance and cost. Intermediate, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-quantization

## Say it in 30 seconds

Quantization stores a model's numbers in fewer bits. A 16-bit weight becomes 8-bit or 4-bit, so the model needs less memory and can run on cheaper hardware. The price is some accuracy. So I never ship a quantized model without testing it on our own tasks first, and I check the speed on the hardware we really use.

## Say it in 2 minutes

A language model is mostly a huge list of numbers called weights. Normally each one is stored in 16 bits. Quantization stores them in fewer bits, such as 8 or 4, so the same model takes half or a quarter of the weight memory. That matters when the model has to fit on one server or a laptop, or when a team wants to serve many users from fewer machines.

The price is accuracy. Fewer bits means each number is rounded more coarsely, so answers can get a little worse. Methods differ in how much they lose, so I don't guess. I measure the loss on our own test set, with our own kind of questions.

Smaller doesn't always mean faster. The speed-up depends on the hardware and on whether its low-level code supports that number format. Quantization also applies only to models we host, because we can't change the weights behind someone else's API.

In a real case, a hospital wants a note summariser on one server in its own building. The 16-bit model doesn't fit. At 8 bits it does, and the summaries pass the team's review set. That is the decision I'd make with the team, with the numbers in front of us.

## What is quantization, in plain words?

A language model is built from billions of numbers called weights. Each weight is learned during training and usually loaded with 16 bits. Quantization stores those numbers in fewer bits, such as 8 or 4, so the model takes less memory and can run on cheaper hardware.

Think of a hospital pharmacy that keeps a dose to three decimal places in its main system, but prints it on the label rounded to the nearest half milligram. The label is shorter and easier to handle, and the rounding costs a little precision. Quantization makes the same trade with a model's weights.

On a real team this is a deployment choice. A hospital wants a note summariser to run on one server in its own building. The full model doesn't fit in that server's memory. Stored in fewer bits, it does, and the team then checks the summaries against its own review set.

- **Weight:** One learned number inside the model. A model has billions.
- **Bit:** The smallest unit of storage, a 0 or a 1. More bits give more possible values.
- **Scale:** A multiplier that turns stored whole numbers back into real weight values.
- **Calibration:** Running sample data through a model to choose the scale and the rounding.
- **Outlier:** A rare, very large number inside the model that a simple rounding scheme handles badly.

## How does quantization shrink a model?

Hugging Face's docs define it in one line. Activations, named in that line, are the numbers passed between the model's layers while it works:

> "Quantization stores a model's weights, and sometimes its activations, in fewer bits so the model needs less memory." (Hugging Face Transformers docs)
>
> "Moving from floating-point representations to low-precision fixed integer values represented in four bits or less holds the potential to reduce the memory footprint and latency by a factor of 16x" (Gholami and colleagues, 2021 survey)

The memory maths is simple. Models are usually loaded in 16-bit numbers, which take 2 bytes per weight (a byte is 8 bits). At 8 bits a weight takes 1 byte, and at 4 bits half a byte. So the weight memory halves at 8 bits and halves again at 4 bits, before any space for the cache and the working memory.

*Diagram, Two ways to store the same weights: A made-up 7-billion-parameter model, stored two ways. Keeping 16-bit numbers is simple but big. Rounding to 8-bit halves the weight memory, and the trade-off is some loss of accuracy, which must be tested.*
1. The model keeps its 16-bit numbers as they are.
2. Each weight is rounded to an 8-bit whole number, using a scale shared by its group.
3. At 16 bits, a made-up 7-billion-parameter model needs about 14 GB.
4. At 8 bits, the same model needs about 7 GB, and quality must be checked.

Here is the arithmetic in plain Python, on the same made-up model and five of its weights. A scale maps the real numbers onto 256 whole-number values, from -128 to 127. The code then reads them back and measures the error.

```python
# A made-up 7-billion-parameter model, and the size of its weights at three precisions.
PARAMS = 7_000_000_000
for name, bits in [("16-bit", 16), ("8-bit", 8), ("4-bit", 4)]:
    gb = PARAMS * bits / 8 / 1e9
    print(f"{name} weights: {gb:.1f} GB")

# Quantize five weights to 8-bit integers (-128 to 127) with a scale and a zero-point, the whole number that stands for 0.
weights = [-0.42, 1.30, 0.07, -0.88, 0.25]
lo, hi = min(weights), max(weights)
scale = (hi - lo) / 255
zero = round(-128 - lo / scale)
ints = [max(-128, min(127, round(w / scale) + zero)) for w in weights]
back = [(q - zero) * scale for q in ints]
print("8-bit values:", ints)
print(f"largest error: {max(abs(a - b) for a, b in zip(weights, back)):.4f}")
```

Output:

```text
16-bit weights: 14.0 GB
8-bit weights: 7.0 GB
4-bit weights: 3.5 GB
8-bit values: [-74, 127, -17, -128, 4]
largest error: 0.0021
```

Notice the trade. The model shrinks from 14 GB to 7 GB, but each weight comes back a little off. Real teams judge a quantized model by its answers on their own tasks. The error number above is only a rough hint.

## What are the main kinds, and what do they cost?

- **After training: Post-training quantization.** Shrink a finished model. Most methods that quantize while loading need no calibration data. GPTQ is the exception: it calibrates on a dataset while the model loads, and its paper's title calls it post-training quantization.
- **During training: Quantization-aware training.** The rounding is simulated while the model trains, so the model learns to cope with it. It costs more training work, and Jacob and colleagues co-designed their training for this.

> "We also co-design a training procedure to preserve end-to-end model accuracy post quantization." (Jacob and colleagues, 2017)
>
> "We develop a procedure for Int8 matrix multiplication for feed-forward and attention projection layers in transformers, which cut the memory needed for inference by half while retaining full precision performance." (LLM.int8() paper, 2022)

- **Accuracy:** Fewer bits cost some accuracy, and each method trades memory, speed and accuracy differently. Test on your own task. A general benchmark score is only a rough guide.
- **Speed:** Smaller is not automatically faster. The gain depends on the hardware and its support for that number format. Jacob and colleagues say integer-only maths can be implemented more efficiently, on commonly available integer-only hardware.
- **Outliers:** A few feature dimensions are very large. A feature dimension is one slot in the model's internal numbers. The 2022 LLM.int8() paper kept those outlier dimensions in 16-bit maths and ran more than 99.9% of values in 8-bit. That needed a special mixed-precision step.
- **Hosted models:** Quantization changes weights you host. With a provider's API, which is how your app calls their model, you can't quantize their model, so ask them which precision they serve.
- **Not the same as:** Distillation trains a smaller model to copy a bigger one. Pruning removes parts of a model. Quantization keeps the same model and stores its numbers in fewer bits.

You will meet named formats like INT8 and INT4, and methods like GPTQ, in later questions.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: fewer bits per number, so less memory.
- [ ] The memory maths: 2 bytes at 16-bit, 1 byte at 8-bit, half a byte at 4-bit.
- [ ] The cost: some accuracy, measured on your own task.
- [ ] Speed depends on hardware, so smaller is not always faster.
- [ ] Post-training versus training-aware, and when each one fits.
- [ ] How it differs from distillation and pruning.

**Common traps**

- Trap: "Quantization makes the model smarter and faster." Better: It makes the weights smaller. Quality can drop and speed depends on hardware, so measure both.
- Trap: "Going down to 4 bits is safe, so we can skip our own tests." Better: Lower bits lose more quality. Run your own tests before any quantized build goes live.
- Trap: "It's the same as pruning or distillation." Better: Those change the model's structure or size. Quantization keeps the model and uses fewer bits.

## Can you spot the problem?

**Spot the problem:** A colleague's release note for a quantized build has one claim that needs fixing. Which one?

A. The 8-bit build needs about half the weight memory of the 16-bit build.
B. The 4-bit build is as accurate as the original, so we can skip our own tests.
C. The 4-bit build fits our single server, and we will test its summaries on our review set.
D. Fewer bits lower memory use. Speed still needs a check on our own graphics chip (GPU).

**Answer:** B. Fewer bits cost some accuracy, so every quantized build needs its own tests. The other three say what the build saves and how the team will check it.

**Quick check:** A hospital's note summariser must fit on one server. Which change cuts the weight memory most directly?

A. A longer system prompt
B. Storing the weights in 8 bits instead of 16
C. Caching the prompt on the server
D. More training examples for the same model

**Answer:** B. Fewer bits per weight shrink the weights directly. A longer prompt, a prompt cache or more training data doesn't change how many bytes the weights take.

## Can you answer these from memory?

**Flash cards**

- **What is quantization?** Storing a model's weights, and sometimes the numbers it passes along, in fewer bits.
- **Why does it save memory?** Fewer bits per number. 16-bit weights take 2 bytes, 8-bit 1 byte, 4-bit half a byte.
- **What does it cost?** Some accuracy. Measure it on your own task before you ship.
- **Is smaller always faster?** No. Speed depends on the hardware and whether it supports that number format.
- **Post-training vs training-aware?** After training, with sample data for some methods such as GPTQ. Or rounding simulated during training.
- **Quantization vs distillation?** Quantization keeps the same model in fewer bits. Distillation trains a smaller model.

## Likely follow-up questions

- Q312 INT8 vs INT4 vs FP8? https://designervenkat.online/interview/ai/int8-vs-int4-vs-fp8
- Q313 GPTQ, AWQ and GGUF in one line each? (full answer coming)
- Q328 Does a 70B model fit on one 80GB GPU? (full answer coming)
- Q327 GPU basics: why does memory bandwidth limit decoding? (full answer coming)
- Q285 What is QLoRA? https://designervenkat.online/interview/ai/what-is-qlora
- Q055 What is the KV cache, and why does it speed up generation? https://designervenkat.online/interview/ai/what-is-the-kv-cache

## Learn it in depth

- [Self-Hosted Models](https://designervenkat.online/learn/ai-engineering/self-hosted-models)
