Go to Map Courses Lab Glossary How it works Troubleshooting Start →
ITEN
← Course 06 · From model to app (Lesson 06.02 · 16 minutes · No sensor · For everyone)

Quantizing.

Writing the weights with fewer bits makes a model four times smaller, and often faster. Here you'll see how it's done, how much you really lose and what to do when you lose too much, all the way down to the 4 bits of the models that write.

What you learn
  1. How you go from 32 to 8 bits, and why the error on each weight is tiny
  2. How much space and speed you gain
  3. What calibration and training on the steps are
  4. Why language models go down to 4 bits
You need to know

If you don't remember one of these, the link takes you to the chapter that explains it.

How do you want to follow it
Preparing the scene…
  1. 01 The weights

    Every weight is a decimal number.

    After training, a model is a long list of weights01.03, numbers like 0.0173 or minus 0.84. Usually each one is stored in 32 bits, which is 4 bytes, and has about seven significant digits. MobileNet v2 has 3.4 million weights, so it takes up almost 14 megabytes. Do you really need all that precision?

    In practice. Here's how you estimate the size of a model. The number of weights times the bytes in each weight. With 32 bits that's 4 bytes, and with 8 bits it's just one.

01 / 07
(Go deeper)

A few more numbers.


(01)

The formula for the steps

For each layer you choose two numbers. The scale, meaning how much one step is worth, and the zero point, meaning which step matches the real zero. With weights from minus 1 to 1 and 256 steps, the scale is 2 divided by 255, about 0.0078.

To write a weight, you divide it by the scale, round it and add the zero point. To read it back, you do the opposite. The weight you read back isn't identical to the original, but the difference is never more than half a step, which is about 0.004 in this example.

q = round(x / scale) + zero        (an integer from 0 to 255)
x ≈ scale × (q − zero)
(02)

Layer by layer or channel by channel

In 2018 Raghuraman Krishnamoorthi at Google compared the ways of quantizing networks for the phone. MobileNet v2, quantized after training with a single scale per layer, fell apart. With one scale for each channel it stayed close to the original, and with training on the steps it won back almost everything.

The reason lies in the MobileNet filters that work on one channel at a time. Some channels have tiny weights and others have large ones. With one scale for all of them, the small ones all land on the same step and get lost. Today almost every tool uses one scale per channel.

(03)

The tools

With LiteRT, you ask the converter for quantization after training, along with a function that supplies the examples for calibration. Core ML has coremltools, PyTorch has its own quantization tools, and ExecuTorch uses them to prepare the model.

Language models use formats made for the purpose. GGUF from llama.cpp offers many 4-bit variants, such as Q4_K_M, and phone apps usually start from those.

conv = tf.lite.TFLiteConverter.from_saved_model("model")
conv.optimizations = [tf.lite.Optimize.DEFAULT]
conv.representative_dataset = a_hundred_real_examples   # calibration
open("model_int8.tflite", "wb").write(conv.convert())
(Try it on your phone)

How many bits do you need?


Choose how many bits to use and watch two things. A photo written with fewer bits per color, and a row of weights sitting on the steps. Below, you see how big MobileNet v2 would be and how far off each weight is on average.

The tool needs JavaScript turned on.

The photo gives you the idea, but it exaggerates. A model copes with the steps much better than your eye does, because the small errors of many weights partly cancel each other out.

(Quiz)

4 questions to close the lesson.

When you have answered them all, the lesson counts as done and its stone on the map turns yellow. Answering is what counts, not the score.


  1. (01)

    A model has 3.4 million weights at 32 bits. How big is it at 8 bits?

  2. (02)

    With 8 bits, what's the largest error on each weight?

  3. (03)

    What is calibration for?

  4. (04)

    The quantized model loses too much. What's the most sensible next step?

To close the lesson, answer the quiz

← Measuring for real Back to the map
(Next lesson · 06.03) Pruning, distilling, choosing the architecture