
Large AI models need a lot of memory. A model with billions of parameters can be too big for a laptop graphics card or a phone. Quantization is one of the most common ways to make such models smaller and cheaper to run. This guide explains what quantization is, how numbers are squeezed into fewer bits, what you give up and how to read the labels you see on downloadable models. It is based on the Hugging Face Transformers documentation on quantization.
What Is Quantization?
Hugging Face describes quantization as storing a model's weights, and sometimes its activations, in fewer bits so the model needs less memory. The weights are the numbers a model learned during training. They are usually loaded in a 16-bit format that takes 2 bytes per parameter. Quantization stores them in 8 bits, 4 bits or another small size instead.
The documentation gives a clear example. A model with 8 billion parameters needs about 16 GB for its weights in 16-bit form, about 8 GB at 8 bits and about 4 to 5 GB at 4 bits. That difference can decide whether a model fits on your hardware.
How the Numbers Are Squeezed
A quantized number can only take a small set of values: 256 for an 8-bit integer and 16 for a 4-bit integer. So the method has to map a wide range of precise values onto that small set.
The most common approach, called affine quantization, takes the smallest and largest values in a group of weights and stretches the available integer range over them. Two settings define the mapping: a scale, which is the size of one step in the original units, and a zero point, which is the integer that stands for zero. To use a number, the system converts it back, and the result is close to the original but not identical. The documentation gives a small example where 0.5 becomes 11 and then converts back to about 0.494.
Why Some Accuracy Is Lost
That tiny difference is called rounding error. Hugging Face notes that every quantization method introduces it, and that methods differ in how they stop the error from hurting the model's output. Fewer bits cost some accuracy, and each method makes a different trade between memory, speed and accuracy.
Granularity is one way to reduce the damage. One scale for a whole set of weights is cheap to store, but a single large outlier can stretch the range and spread the usable values apart for everything else. Using one scale per row, or per small group of weights such as 128, gives each scale a narrower range to cover, at the cost of some extra storage.
Two Ways to Use a Quantized Model
- Quantize while loading. You start with a full-precision model and ask the library to quantize it as it loads. Most methods in this group do not need a calibration dataset, although at least one popular method does.
- Load a model that is already quantized. Someone has done the work and saved the result. The library reads the method from the model's configuration file.
Some methods can only be loaded from a pre-made checkpoint, so you must quantize with that method's own tool first. The documentation says Transformers supports more than 20 quantization methods, so you can pick the trade-off that suits your hardware.
What the Labels Mean
- 8-bit or int8. Roughly half the memory of 16-bit, usually with a small quality change.
- 4-bit or int4. About a quarter of the memory, with a bigger risk of quality loss.
- GGUF. A single-file format that holds the weights, settings and tokenizer. The documentation warns that in some setups GGUF models are converted back to full precision when loaded, so they can use as much memory as an unquantized model.
Method names such as GPTQ, AWQ or bitsandbytes refer to different ways of choosing the scales and handling outliers. You do not need to know the math to use them, but it helps to know that each makes different trade-offs.
How to Choose
- Start from the memory you have, then pick the highest precision that fits.
- Prefer 8-bit if quality matters and memory allows it.
- Try 4-bit when the model would not fit otherwise, and test it on your own tasks.
- Check each method's page for hardware support and whether you can save or fine-tune the result.
Quantization vs Distillation
These are different tools. Quantization keeps the same model but stores its numbers with fewer bits. Distillation trains a new, smaller model to copy a larger one. They can be combined, for example by quantizing a distilled model.
Final Thoughts
Quantization is a practical trade of precision for size and cost. It lets large models run on smaller machines, but it always adds some rounding error. Test the quantized version on the work you care about before you rely on it, and pick the lowest bit count that still gives results you are happy with.
This article is based on the Hugging Face Transformers documentation pages "Quantization overview" and "Quantization concepts".
0 Comments