Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 13.3 — Quantisation
LLM Engineering · Example 2 of 2

Quantise weights to int8 and back, with the error

Symmetric quantisation: scale = max absolute weight / 127. Rounding half away from zero is written by hand.

Quantisation stores each weight in fewer bits, so a model needs less memory and often runs faster, at the price of a small rounding error. The memory is just parameters times bits divided by 8.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write weights_gb(params_billion, bits) that returns the gigabytes needed for the weights, and print it for a 7 billion parameter model at 16, 8 and 4 bits.

02

Write to_steps(ws, scale) that returns each weight divided by the scale and rounded half away from zero, as a list of whole numbers. Print the result for the weights 0.26, -0.26, 0.04 and 1.27 with scale 0.1.

Explain it without notes

01

How much memory does a model with 7 billion parameters need for its weights at 16, 8 and 4 bits?

02

How does int8 quantisation work, and what is the maximum rounding error?

03

Why can a single large weight ruin a quantised layer, and what do real systems do about it?

04

How would you decide whether a 4-bit version of a model is good enough?

Quantise weights to int8 and back, with the error
Sign in to run this example in your browser.

Expected output

scale: 0.015039
0.82 -> 55 -> 0.8272 (error 0.0072)
-1.91 -> -127 -> -1.9100 (error 0.0000)
0.07 -> 5 -> 0.0752 (error 0.0052)
0.55 -> 37 -> 0.5565 (error 0.0065)
-0.33 -> -22 -> -0.3309 (error 0.0009)
1.24 -> 82 -> 1.2332 (error 0.0068)
-0.02 -> -1 -> -0.0150 (error 0.0050)
0.96 -> 64 -> 0.9625 (error 0.0025)
max error 0.0072, mean error 0.0042
size: 32 bytes as 32-bit numbers, 12 bytes as int8 plus one 32-bit scale