Linear algebra for LLMsPart 9: Numbers on real hardware
Number formats
6 min
A float is sign × mantissa ×
A floating-point number spends its bits on three things: a sign, an exponent (how big: the range) and a mantissa (how many significant digits: the precision). Every format below is a different split.
Write
The formats you'll meet
| Format | s/e/m bits | Bytes | Max |
|---|---|---|---|
| FP32 | 1/8/23 | 4 | |
| BF16 | 1/8/7 | 2 | |
| FP16 | 1/5/10 | 2 | 65,504 |
| FP8 E4M3 | 1/4/3 | 1 | 448 |
| FP8 E5M2 | 1/5/2 | 1 | 57,344 |
| INT8 | — | 1 | 127 |
| INT4 | — | 0.5 | 7 |
Write
BF16 vs FP16: range vs precision
BF16 keeps FP32’s range but only 8 significant bits (counting the implicit leading 1): between 256 and 512 its numbers are 2 apart, so rounds back to 256. FP16 is about 8× finer but tops out at 65,504, so FP16 training needs loss scaling to keep gradients in range. BF16 doesn’t, which is why it became the default for training.
Write
Accumulate in higher precision
Adding thousands of small products in BF16 loses digits: once the running sum is large, small terms round away. Matrix units multiply BF16 inputs but accumulate in FP32, and optimizers keep an FP32 master copy of the weights for the same reason (Part 8’s 16 bytes per parameter).
Weight memory = parameters × bytes
Llama 3 8B in each format, weights only. Halving the bytes halves the memory and, as the next deck shows, roughly doubles how fast you can generate.
Write
✏️ Your turn
Size a 70B model
How much memory do the weights need in BF16? In INT4?
Work it out on paper, then go to the next card to check.
Answer
In BF16 it needs at least two 80 GB GPUs before any KV cache; in INT4 it fits on one.
In PyTorch
torch.finfo(torch.bfloat16) # eps=0.0078125, max≈3.39e38
torch.finfo(torch.float16) # eps≈0.000977, max=65504
x.to(torch.bfloat16)📌 Recap
Number formats in one card
- Exponent bits set the range; mantissa bits set the precision.
- BF16: FP32’s range in 2 bytes; FP16: finer but max 65,504; FP8: 1 byte.
- Accumulate in FP32.
- Weight memory = parameters × bytes.
Write