Skip to the page
Tecyfy

What is quantization in AI?

Storing a model's numbers with less precision, such as 4 bits instead of 16, so it needs far less memory and can run on smaller hardware, at some cost in accuracy.

intermediate · Also called Quantisation

A model is a very large set of numbers. Each one has to be stored, and the question is how precisely. Quantization means storing them less precisely, to save space.

The arithmetic

Meta's Llama 3 8B has 8 billion numbers, published at 16 bits each. 16 bits is 2 bytes, so the model needs about 16 GB just to hold its weights.

Store each number in 8 bits and it needs about 8 GB. Store each in 4 bits and it needs about 4 GB, a quarter of the original. That is the difference between needing a large graphics card and fitting on a much smaller one.

The sum is the same at any size: parameters times bytes per parameter. It is the first sum to do before you try to run an open model yourself.

What you give up

Fewer bits means each number is rounded more coarsely. Picture recording every price in a shop to the nearest pound instead of the nearest penny. Most totals come out close; a few do not. A quantized model behaves the same way: close to the original on much of its work, a little worse on some of it.

The better methods are careful about where to round. Hugging Face describes them as lowering memory while trying to keep as much accuracy as possible, and some need a calibration pass on sample data to choose the rounding well. Squeezing all the way to 1 or 2 bits is where those careful methods are needed most.

Try itSqueeze a model into less memory

Memory for the weights

16 GB24 GB80 GB

16 GBfits a 24 GB graphics card

What rounding does to 8 weights (65,536 possible values)

−101
WeightStoredOff by
0.81370.8137<0.0001
-0.2291-0.2291<0.0001
0.04120.0412<0.0001
-0.6675-0.6675<0.0001

Hollow: the weight as trained. Filled: the value stored at this many bits.

8 billion weights × 16 bits = 16 GB just to hold them. That fits a 24 GB graphics card, with no room counted for the conversation. Each weight is now one of 65,536 values, off by up to less than 0.0001 here.

Memory is weights × bits ÷ 8, for the weights alone; the KV cache needs more on top. The weights are made up and rounded the simplest way, to evenly spaced steps; real methods choose their steps more carefully.

Where you see it

If you download an open model to run at home, with tools built on llama.cpp for example, you will usually be offered the same model at several bit sizes. The smaller file loads on smaller hardware; the larger one is closer to the original.

Hugging Face lists more than 20 quantization methods in its own library, some going as low as 1 bit, each suited to different hardware.

How to choose

Start from the memory you have. Pick the largest version that fits with some room to spare, since a long conversation needs memory for the KV cacheKV CacheWork a model saves from the text it has already read, so it is not redone for every new token. Prompt caching keeps that work between requests and charges less for it. on top of the weights. Then test it on your own tasks, because the averages in a model card may not match your use.

Sources

Where each fact comes from

  1. 1Quantization overview
  2. 2Meta-Llama-3-8B model card
  3. 3How ChatGPT and our foundation models are developed

Put this demo on your own site

Copy the code into any web page: a lesson, a blog post, a course or your team's wiki. The demo runs in a frame and works just as it does here, so your readers can try it where they read. It is free and needs no account, and nothing a reader does in it is sent anywhere.

The short script at the end lets the frame grow and shrink with the demo. Where a page cannot take scripts, choose a fixed height and the frame scrolls.

<iframe src="https://tecyfy.com/embed/demo/quantization" title="Squeeze a model into less memory" width="100%" height="1050" style="border:0;display:block" loading="lazy" allow="clipboard-write"></iframe>
<p><a href="https://tecyfy.com/concepts/quantization">Squeeze a model into less memory</a> by Tecyfy</p>
<script>addEventListener("message",function(e){if(e.origin!=="https://tecyfy.com"||!e.data||e.data.type!=="tecyfy:height")return;var h=Number(e.data.height);if(!(h>0))return;document.querySelectorAll("iframe").forEach(function(f){if(f.contentWindow===e.source)f.style.height=Math.min(h,20000)+"px"})})</script>