What is quantization, and how does it let a model fit on a small device?
- Posted
- Tags
- ai, llm, local-llm
Quantization stores each of a model's weights in fewer bits, a bit like lossy compression. It shrinks the model and often speeds it up, at the cost of small rounding errors that matter more the fewer bits you use.
I watched a KodeKloud short on quantization. I have been exploring models that can run locally on a phone for easy jobs like face recognition, labelling images, or writing a short poem, and I kept seeing the word without really knowing what it meant.
The question I had
How can a model with billions of numbers inside it run on a laptop or a phone?
What I found
The memory problem
A model is mostly a huge list of numbers called weights. Take an 8 billion weight model. If every weight is stored in 32 bits (4 bytes), the model needs 8 billion times 4 bytes, which is 32 GB. A laptop with 16 GB of RAM cannot hold that.
Storing each weight in fewer bits fixes the size problem:
| Bits per weight | Bytes per weight | Size for 8 billion weights |
|---|---|---|
| 32 | 4 | 32 GB |
| 16 | 2 | 16 GB |
| 8 | 1 | 8 GB |
| 4 | half a byte | around 4 GB |
Storing weights in fewer bits is called quantization. Storing them in 8 bits is "8 bit quantization", and so on. In practice many models are already released in 16 bits, so 16 GB is often the real starting point, and the 32 bit row is the textbook one. The sizes are also for the weights only. A real 4 bit file ends up a bit larger than 4 GB, and the model needs extra memory while it runs.
What a weight looks like
A real weight is not a tidy number like 0.43. It is more like 0.43261877, with many digits after the decimal point. Fewer bits cannot hold all of those digits, so some precision is lost. As an illustration, a 16 bit version might keep only 0.4326. (Real 16 bit numbers are stored in binary, so this is a picture of the idea, not the exact format.)
A worked 8 bit example
Eight bits can only hold a whole number. A signed 8 bit number covers -128 to 127, which is 256 different values. So the weight has to become a whole number first.
- Look at all the weights in a layer. Say they all sit between -1 and 1.
- Match that range to the whole numbers: -1 becomes -127, 0 stays 0, and 1 becomes 127. In practice that means multiplying each weight by 127.
- My weight: 0.43261877 times 127 is about 54.94.
- Round it to 55, and store 55.
- When the model runs, it divides 55 by 127 and gets back 0.4331.
The weight started as 0.4326 and came back as 0.4331. That small difference is the quantization error.
Real methods are a bit smarter than one range for a whole layer. They usually use a separate scale for each small group of weights, so a single odd, very large weight does not stretch the range for all the others.
Why the model still works
I expected changing billions of weights to break the model. It does not, mostly because a model can tolerate small changes as long as every weight stays close to its original value. Good quantization methods also keep the more sensitive weights at more bits, so the answers stay very similar.
What it costs and what it gains
- Smaller. The model fits in less memory, so it can run on a laptop.
- Often faster. Producing each word means reading the weights from memory, and there is less to read. This helps most when memory speed is the limit, which is common on laptops and phones.
- A little worse. The rounding errors add up. How much depends on how far you go:
| Bits | What I can expect (a rule of thumb) |
|---|---|
| 16 and 8 | Answers almost the same as the original |
| 4 | Fine for everyday tasks, more mistakes on harder ones |
| Below 4 | Usually clearly worse, though cleverer methods push this further |
Where my epiphany came in
I first thought of quantization as squeezing a model smaller. Then I saw it is lossy compression, like turning a photo into a smaller JPEG. My first guess was that the hard part is keeping the long decimals. It is not. The hard part is keeping what the model does, so the answers stay good while the numbers get rougher. That is why there are different quantization methods. They choose their scales and decide which weights deserve more bits in different ways, and that choice is what separates a 4 bit model that still works from one that does not.
For my phone idea, it means a small model at 4 or 8 bits is a reasonable thing to try for easy tasks, and I should test it on the actual task before trusting it. I wrote about bits and bytes earlier, and about open weight models, which are the ones you can actually quantize and run yourself.
Takeaway
Quantization is lossy compression for a model: fewer bits per weight means a smaller and often faster model, paid for with small rounding errors. Down to about 8 bits the loss is hard to notice, around 4 bits it starts to show on harder tasks, and the method used matters as much as the number of bits.