Quantization · Part 1 · Study guide

Number formats

Before we can shrink a model, we need to know how it stores its numbers. This guide is a set of small bites to learn that, with tiny examples you can play with. Click the bits. Break things.

Audience
Anyone new to model quantization. No math background needed beyond powers of 2.
Scope
How FP32, FP16, BF16 and INT8 store numbers, and the range and precision trade-off. Not yet: how to quantize a model.

Before you start

You should know what a power of 2 is: 23 = 2 × 2 × 2 = 8. That is all. Every other idea is explained on the page.

Bite 1

Why do we care?

A model is a huge pile of numbers called parameters. Every number takes some bytes of memory. So:

memory = number of parameters × bytes per number

Let us start small. Say each number takes 4 bytes (this is the FP32 format, you will see why later). Now count up:

Numbers× 4 bytesIn plain words
14 bytestiny
1,0004,000 bytes4 KB, a short text file
1 million4 million bytes4 MB, one phone photo
1 billion4 billion bytes4 GB, a small movie
70 billion280 billion bytes280 GB

Here is the shortcut. One billion bytes is one gigabyte (GB). So when the parameters are counted in billions, the answer is already in GB: 70 billion × 4 bytes = 280 GB. Pick a model size below and watch what each format does.

Model size:
  • FP32 · 4 bytes each70B × 4 = 280 GB
  • FP16 / BF16 · 2 bytes each70B × 2 = 140 GB
  • INT8 · 1 byte each70B × 1 = 70 GB
  • INT4 · 0.5 bytes each70B × 0.5 = 35 GB

Weights only. Real use needs extra memory for activations and the KV cache. INT4 is a preview of what comes later in this series.

Work it out by hand for 70 billion parameters: FP32 needs 280 GB, FP16 needs 140 GB, INT8 needs 70 GB, and INT4 needs 35 GB.

Fewer bytes per number means a smaller model. So the goal is clear: use fewer bytes.

Remember

Memory = number of parameters × bytes per number. Fewer bytes per number means a smaller model.

But what is inside those bytes? Let us open one up.

Bite 2

Bits are switches

A bit is a switch that is either 0 or 1. Put 8 of them in a row and you have a byte. Each switch is worth a different amount: 1, 2, 4, 8, and so on. Add up the switches that are on.

128
64
32
16
8
4
2
1

64 + 8 + 2

74

Range here: 0 to 255. Try switching every bit on.

Example: the bits 01001010 are 64 + 8 + 2 = 74. The interactive bit row above lets you check any pattern.

With 8 bits there are 28 = 256 patterns. That is why INT8 can hold only 256 different values. And they are whole numbers only.

Remember

N bits give 2N patterns. 8 bits give 256 patterns, so INT8 holds 256 whole numbers.

INT8 only holds whole numbers like 0 to 255. How do we store 0.15625, or a huge number like one billion?

Bite 3

Floats: sign, exponent, mantissa

The answer is floating point. It works like scientific notation. Take 0.15625 from the question above. We can write it as:

0.15625 = +1 × 1.25 × 2-3

Three pieces: a sign (+ or -), an exponent (the power of 2, which sets the scale), and a mantissa (the digits after the point). The leading 1 in 1.25 is always there, so we do not store it. It is called the hidden 1.

The exponent is stored with a bias so it can be negative. For FP32 the bias is 127. Click any bit below and watch the number change.

1 + 8 + 23 = 32 bits
SignExponentMantissa
Sign
0 means positive (+1)
Exponent
Stored as 124. Subtract the bias 127: 124 - 127 = -3. So the scale is 2-3 = 0.125.
Mantissa
The bits give 0.25. Add the hidden 1: 1 + 0.25 = 1.25.

+1 × 1.25 × 0.125

0.15625

Load a number:

Example: 0.15625 in FP32 has sign 0 (positive), exponent 124 (124 − 127 = −3, so the scale is 2-3), and a mantissa that gives 0.25. So 1.25 × 2-3 = 0.15625.

Now switch to FP16 and BF16 above. Same idea, but the bits are split in different ways. Look at how many bits the exponent and the mantissa get.

Remember

A float is sign × (1 + mantissa) × 2exponent. The exponent sets the scale. The mantissa sets the detail.

FP16 and BF16 both use 16 bits, but they split them in different ways. Why have two, and what does each gain or lose?

Bite 4

Range vs precision

That split is the whole trade. More exponent bits give a wider range: how big and how small a number can be. More mantissa bits give more precision: how close neighbour numbers are. With a fixed number of bits, you trade one for the other.

FormatBits (S/E/M)BiggestSmallestDigits kept
FP321 / 8 / 233.402823e+381.175494e-38about 7
FP161 / 5 / 10655046.103516e-5about 3
BF161 / 8 / 73.389531e+381.175494e-38about 2

FP16 and BF16 both use 16 bits, but they choose differently. FP16 keeps more precision. BF16 keeps the same range as FP32 and gives up precision. Type a number below and see what each format does to it. Try 70000 and 0.00001.

FP32Rounded

1000.299988

Error: 1.220703e-5 · Numbers near here are 6.103516e-5 apart

0
10001000
11110100001001100110011
FP16Rounded

1000.5

Error: 0.2 · Numbers near here are 0.5 apart

0
11000
1111010001
BF16Rounded

1000

Error: 0.3 · Numbers near here are 4 apart

0
10001000
1111010
SignExponentMantissa

Example: 70000 is too big for FP16 (its biggest value is 65,504), so FP16 turns it into infinity. FP32 and BF16 both store it, though BF16 rounds it.

Notice that 0.1 is never exact in any format. Computers count in 2s, and 0.1 does not fit neatly. More mantissa bits just get closer.

Remember

More exponent bits mean more range. More mantissa bits mean more precision. With a fixed number of bits, you trade one for the other.

Back to our memory problem: how far can we cut the bytes before the numbers break?

Bite 5

Self-check quiz

Six questions to check what stuck from the four bites above. Pick an answer and the correct one is shown with the reason. Your score is not saved.

1. A model has 7 billion parameters stored in FP16. About how much memory do the weights need?

2. Which part of a float mostly decides how big or small the number can be?

3. BF16 and FP16 both use 16 bits. What is different about BF16?

4. Which format is the least precise (it keeps the fewest digits)?

5. You store the number 70,000 in FP16. What happens?

6. How many different bit patterns can INT8 hold?

Answer key

  1. 14 GB. 7 billion × 2 bytes = 14 GB.
  2. The exponent. It sets the scale, so more exponent bits give a wider range.
  3. It has more exponent bits, so it has a wider range.
  4. BF16. It has only 7 mantissa bits.
  5. It becomes infinity. The biggest FP16 value is 65,504.
  6. 256. 28 = 256.

Next up, the answer to the last question: how to turn a big format into a small one (INT8, INT4) without losing too much.

Sources

  1. 1.
    A Visual Guide to Quantization by Maarten Grootendorst

    The main reference for this series. This page is my own notes and examples built after reading it.