Quantization & Memory Math
AI Engineer path · lesson 99 of 124Work out whether a model actually fits. Split a float into range and precision bits, estimate inference at N × M × 1.2, and watch a 13B model need another 78 GB the moment you make every parameter trainable.
| FP32 value | FP16 | BF16 | TF32 |
|---|---|---|---|
| 0.0123456789 | 0.0123443603515625 | 0.0123291 | 0.01234436035 |
| 1234.56789 | 1235.0 (0.035% change) | 1232.0 (0.208% change) | 1234.0 |
| 123456.789 | INF | 123392.0 | 123456.0 |
Key insight
BF16 and FP16 are the same width and are not interchangeable. BF16 trades precision for range, which is what training needs — gradients span many orders of magnitude, and a gradient that overflows to infinity destroys a step in a way a slightly imprecise one does not.
Order by bytes per parameter
Order these formats from most memory per parameter to least.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
Test your knowledge
In a floating-point format, the exponent bits determine:
The short version
Measuring a plank. You can record it to the millimetre or to the nearest centimetre, and you can allow for planks up to two metres or up to a kilometre. Those are two separate decisions — precision and range — and a tape measure has a fixed number of digits to split between them.
In more depth
Quantization & Memory Math
Whether a model runs on your hardware is arithmetic. Two formulas.
Numerical representations
Every float spends 1 bit on the sign; the rest splits between range (exponent bits) and precision (significand bits).
| Format | Bits | Bytes | Range | Precision | Note |
|---|---|---|---|---|---|
| FP64 | 64 | 8 | 11 | 52 | Default in NumPy/pandas; rare in neural nets |
| FP32 | 32 | 4 | 8 | 23 | The old train-and-serve standard |
| TF32 | 19 | 4 | 8 | 10 | NVIDIA, for GPUs — actually 19 bits, not 32 |
| BF16 | 16 | 2 | 8 | 7 | Google, for TPUs |
| FP16 | 16 | 2 | 5 | 10 | Half precision |
| INT8 / INT4 | 8 / 4 | 1 / 0.5 | — | — | Fixed point |
BF16 and FP16 are the same width and not interchangeable. 123456.789 → FP16 gives INF (out-of-bound values round to infinity); BF16 gives 123392.0.
Inference memory
N × M × 1.2Activations and KV vectors grow with sequence length and batch size; ~20% is a working assumption.
Training memory
weights + activations + gradients + optimizer states. The last two are charged per trainable parameter.
| Optimizer | States per trainable parameter |
|---|---|
| Vanilla SGD | 0 |
| Momentum | 1 |
| Adam | 2 |
How low can you go
PTQ is by far the most common. Weight quantization is more common than activation quantization (more stable, less accuracy loss).
The smallest IEEE-conforming float is 4-bit. And you cannot go below 1 bit per value.
Two traps
Interview Questions
References & Further Reading
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required