Learn the MachineDansk

Explained with an interactive demo

What is quantization?

Quantization stores a model’s weights with fewer bits, for example 4 instead of 16. The model takes up 3–4 times less space and can generate text faster, but every weight gets rounded. Try it on real trained weights, see what one large outlier does, and work out what a language model needs in memory and bandwidth.

The demo needs JavaScript. The text below explains what it shows.

Embed this demo on your site

Copy the code into your page. The demo loads from learnthemachine.com, so it stays up to date, and the link under it shows where it comes from.

How it works: a scale and an integer

Each weight w is replaced by an integer q and a shared scale s, so that w ≈ s · q. With b bits and symmetric quantization, q can go from −(2b−1 − 1) to 2b−1 − 1: from −127 to 127 with 8 bits, from −7 to 7 with 4 bits and only −1, 0 and 1 with 2 bits. The simplest scale is absmax: s is the largest absolute value divided by the largest integer, so the largest weight is hit exactly and every other one is rounded to the nearest level. In the demo, 8 bits give an error of 0.4% of the weights’ typical size, 4 bits 6.8%, 3 bits 18% and 2 bits 57%, where 19 of the 32 weights become 0.

Why it matters: memory and speed

Llama 2 7B has 6.74 billion parameters. At 16 bits the weights take up 13.5 GB, at 8 bits with scales about 7.2 GB and at 4 bits about 3.8 GB. When a model generates one answer at a time, every weight has to be read from memory for each new token, so memory bandwidth sets an upper limit. With the 100 GB/s of an Apple M2, the limit is about 7 tokens a second at 16 bits and about 26 at 4 bits. The real speed is lower, but fewer bytes give more tokens. When a long prompt is processed, or many answers are written at once, many tokens are computed per read of the weights, and then it is more often the compute that limits.

Quantization is used for training too: QLoRA fine-tunes with LoRA adapters on top of a base model at 4 bits, see What is LoRA?

Outliers: one large value ruins the scale

Turn on the outlier in the demo. One weight six times larger than the others drags the scale up with it. At 4 bits with one scale for all 32 weights, 12 of the 31 ordinary weights are rounded to 0 and the error rises from 6.8% to 47%. At 3 bits 30 of them become 0, and the last one is rounded up to twice its value. Large language models have exactly such values: Dettmers et al. (2022) found that from around 6.7 billion parameters, a few dimensions in the hidden states emerge with far larger values than the rest. Their method, LLM.int8(), handles them by computing those dimensions at 16 bits and the rest at 8 bits.

Groups: more scales, fewer casualties

The usual fix is to give each small group of weights its own scale. In the demo, groups of 8 confine the damage to the outlier’s own group: the error at 4 bits falls from 47% to 20%, and only 4 weights become 0. The scales cost space: one 16-bit scale per 8 weights adds 2 bits to every weight. Real formats therefore use larger groups, such as 32 weights in llama.cpp’s Q4_0, which gives 4.5 bits per weight, or 128 in many GPTQ models.

Smarter rounding

Rounding to the nearest level is the simplest approach. GPTQ (Frantar et al., 2022) quantizes one column at a time and adjusts the remaining weights so that the layer’s output changes as little as possible on a small set of example texts. With it, GPT models with 175 billion parameters were quantized to 3–4 bits with very little loss in accuracy. AWQ (Lin et al., 2023) protects the weights that are multiplied by large activations by scaling them up before quantization. Both use example data, not just the weights.

Limits

How much quality drops depends on the model, the method and the task. 8 bits is often close to lossless, 4 bits with good methods typically gives a small loss, and below 4 bits the loss grows quickly. So measure on your own tasks rather than trusting a single number. Quantizing activations and the KV cache is a separate matter. The demo shows one row of 32 weights from a small network; it shows the mechanism, not what happens in a particular language model. The speeds in the calculator are upper limits from bandwidth, not measurements.

Run a real language model yourself

In the Open language model lab you run Pythia-70M on your own computer, see what the model predicts, and check, among other things, whether the KV cache gives the same logits as running the whole text through again. The lab requires full access.

See the Open language model labOr watch a model learn with gradient descent, free

More explainers