Inference Engineering

Lesson 10 of 14

Quantization

A model is billions of numbers. Writing them with fewer bits always saves memory, but it only makes the model faster where reading those numbers was the slow part. This lesson shows how the squeezing works, why a few giant numbers make it hard, and when it buys you speed.

From one weight to the whole model

One shot, three scales, with real numbers for Llama 3.1 8B on one NVIDIA H100. “8 bits” is FP8; “4 bits” is 4-bit codes with one 16-bit scale per 128 weights. It plays by itself; press a stage to replay it, or drag to look around. The shelf is to scale along its length.

01 · One number

A ruler with fewer marks

A model is a long list of numbers called weights, learned in training. Llama 3.1 8B, the model Parrot runs, writes each of its 8 billion weights in 16 bits, two bytes, so the list fills 16.1 GB of GPU memory. Quantization writes each one with fewer bits: 8, or even 4.

Picture each weight on a ruler. Four bits count only to 16, so the ruler gets 15 marks (one is dropped so zero sits in the middle). Each small group of weights gets its own ruler, stretched to reach its largest weight. Memory keeps the spacing once per group, the scale, and for each weight only its nearest mark’s number. Every weight ends up off by at most half a mark: the rounding error.

Squeezing sixteen weights onto a ruler

Bits per weight
Before
After
  • Weight as stored in 16 bits
  • Ruler marks
  • Rounded value
  • Rounding error (the gap)
  • Giant

A toy with groups of 8. Real schemes use groups of 128, where the scales cost only 0.125 extra bits per weight.

02 · One giant

Big values crowd the rest

A ruler stretched to reach one huge value spreads its marks apart, and the group’s ordinary weights crowd onto a few. Sizes below are in σ, the typical size of a weight. Three giants of 12σ on one ruler make the other weights’ typical error 5.5 times larger. Smaller groups keep the damage local.

The numbers each layer hands to the next, the activations, have far more giants than the weights, which is why they are harder to squeeze. Methods such as GPTQ and AWQ choose how each weight is rounded to shrink the errors that matter most.

One row of 256 weights, and what three giants do to it

Plays by itself; change a setting to take over.

Stored
Grid step
Levels in use
RMS error, ordinary weights

weightoutlierrounded value, errorlevels

256 weights from a seeded normal sample. Grey lines are the marks of each group's ruler, pink ticks the rounded values; the lower panel is each weight's rounding error.

03 · Faster or only smaller

Speed comes only where reading was slow

To type each token of a reply, the GPU sends every weight over lesson 1’s belt from its memory to the chip. For one user that trip sets the pace, so 4-bit weights make Llama 3.1 8B type about 3.2 times faster. Not four: the scales, the 16-bit embedding and output layer, and the conversation’s cards still travel.

Reading the question is different. All its tokens go through together, so the arithmetic sets the pace. Four-bit weights are unpacked to 16 bits for that arithmetic, so this step gets no faster. The H100 can also calculate in an 8-bit format, FP8, at 2.0001010713563776 times the speed, which speeds up both steps.

Llama 3.1 8B on one H100

Plays by itself; move the slider to take over.

Memory

Time to type each token

Time to read a 2,048-token question

  • Embedding and output layer, always 16-bit
  • Rest of the weights
  • Room for cards
  • Safety margin (10%)
  • Over the margin

The fastest each step could possibly go for one user. Teal is memory: space, or time spent reading it. Amber is time spent on arithmetic.

04 · The big model

When it saves a GPU

So, for the CFO: Parrot’s 8B model already fits on one H100, so quantization cannot halve the GPU count; it makes each GPU do more. It removes GPUs only for bigger models. Llama 3.1 70B needs 141 GB in 16 bits, two H100s. In 8 bits it is 72.7 GB: inside 80.0 GB, but not inside the 72.0 GB left after the safety margin, so no cards fit. In 4 bits it is 39.5 GB, with room for 99,186 tokens of cards.

The price is accuracy: barely measurable at 8 bits in the published papers, small but real at 4. Measure on your own traffic first.

Llama 3.1 70B: one H100 or two?

Plays by itself; move the slider to take over.

Memory

Time to type each token

Time to read a 2,048-token question

  • Embedding and output layer, always 16-bit
  • Rest of the weights
  • Room for cards
  • Safety margin (10%)
  • Over the margin

Each box is one H100. The embedding and output layer stay in 16 bits at every precision.

05 · Playground

Choose the precisions yourself

Pick a model and how its weights and cards are stored. Compare FP8 with INT8, then try 8-bit cards on the 4-bit 70B model.

Precision planner

NVIDIA H100s, 80 GB each. Conversations of 8,192 tokens.

Model

Memory

Time to type each token

Time to read a 2,048-token question

  • Embedding and output layer, always 16-bit
  • Rest of the weights
  • Room for cards
  • Safety margin (10%)
  • Over the margin

Floors for one user: a 1,024-token conversation while typing, a 2,048-token question while reading.

06 · Glossary

The vocabulary, in one place

Weight
One of the numbers a model learned in training. Llama 3.1 8B has about 8 billion.
Bit
A single 0 or 1. With b bits you can write 2 to the power b different values: 65,536 with 16 bits, 256 with 8, 16 with 4.
Quantization
Writing each weight with fewer bits by rounding it to the nearest mark of a coarser ruler.
Group and scale
Weights are rounded in small groups (128 is common). Each group stores its spacing of marks, the scale, once in 16 bits.
Rounding error
How far a weight moves when it snaps to its nearest mark: at most half a mark.
Giant (outlier)
A value far larger than the others in its group. It stretches the group’s ruler and crowds the rest onto a few marks.
σ (standard deviation)
The typical size of a weight. The figures measure weights and errors in σ.
Activations
The numbers each layer hands to the next. They change with every input and have many more giants than the weights.
Token
The unit a model reads and writes: a piece of a word, about three-quarters of one on average.
Decode and prefill
Decode types the reply one token at a time, reading every weight per token. Prefill reads the whole question in one pass.
FP8
An 8-bit number format the H100 can also do arithmetic in, at 2.0001010713563776 times its 16-bit rate.
W4A16 and W8A8
Weights in 4 bits with 16-bit arithmetic; or weights and activations both in 8 bits, so the arithmetic runs in 8 bits too.
Cards (KV cache)
The notes the model keeps for each conversation (lesson 4). They share GPU memory with the weights.
Safety margin
The 10% of GPU memory a server leaves unused so it does not run out mid-reply.

You can now

  • Compute the bits per weight and the memory of a quantized model, scales and 16-bit embedding included, and decide whether it fits on a GPU beside its KV cache.
  • Explain why 4-bit weights speed up typing (decode), even by more than they shrink the model, but not reading the prompt (prefill), while 8-bit arithmetic (FP8 W8A8) speeds up both.
  • Decide when to use weight-only INT4, FP8 or INT8 W8A8, or an FP8 KV cache, depending on whether a deployment is short of memory, serving few users, or busy and limited by arithmetic, and say why one large value crowds the rest of its group.

Try it in the Serving Lab: Go back to the GPU that ran out of KV memory in lesson 4 and fix it by storing numbers in fewer bits.

Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.