Sub topic 2
Number formats: from FP32 to 4-bit
This chapter assumes you know that a model is a large set of numbers called weights, that a byte is 8 bits, and that you can run a short Python script. By the end, you should be able to say how many bytes each weight takes in each common format. You should also be able to say why lower precision is the main way to make a model fit on your machine.
Why the format of a number matters
A model with 8 billion parameters stores 8 billion numbers. The memory it needs depends on how many bits each number uses. The same model can be about 30 GiB or about 4 GiB, depending only on the number format. The earlier chapter on parameter counts gave you the first factor. This chapter covers the second.
A number format is a rule for turning a pattern of bits into a value. Each format makes a different trade between three things:
- Range is how large and how small a value can get before it overflows or rounds to zero.
- Precision is how finely the format can tell two nearby values apart.
- Size is how many bits each value occupies.
Floating-point formats
A floating-point number splits its bits into three fields: a sign bit, an exponent, and a mantissa. The exponent sets the scale, like the power of ten in scientific notation. The mantissa holds the significant digits. More exponent bits give more range. More mantissa bits give more precision.
| Format | Bits | Sign / exponent / mantissa | Largest value | Rough precision |
|---|---|---|---|---|
| FP32 | 32 | 1 / 8 / 23 | about 3.4e38 | about 7 decimal digits |
| FP16 | 16 | 1 / 5 / 10 | 65504 | about 3 decimal digits |
| BF16 | 16 | 1 / 8 / 7 | about 3.4e38 | about 2 decimal digits |
FP32 is "single precision", the traditional default for training. FP16 halves the size but has a small exponent, so large values overflow. BF16 (brain floating point) keeps FP32's 8 exponent bits and cuts the mantissa to 7 bits. It has the same range as FP32 with less precision. Most modern models are released in BF16 for this reason. Training is sensitive to overflow, and neural networks tolerate coarse precision well.
You can see these differences with only the Python standard library.
import struct
def to_fp16(x):
# Round-trip through IEEE half precision ("e" format).
return struct.unpack("<e", struct.pack("<e", x))[0]
def to_bf16(x):
# BF16 is the top 16 bits of an FP32 value.
# This truncates, which is simpler than rounding and close enough here.
bits = struct.unpack("<I", struct.pack("<f", x))[0]
return struct.unpack("<f", struct.pack("<I", bits & 0xFFFF0000))[0]
for value in (0.1, 70000.0):
print(f"value {value}")
try:
print(" FP16:", to_fp16(value))
except OverflowError:
print(" FP16: overflow (largest value is 65504)")
print(" BF16:", to_bf16(value))For 0.1, FP16 gives 0.0999755859375 and BF16 gives 0.099609375. FP16 is closer because it has more mantissa bits. For 70000, FP16 overflows, while BF16 returns 69632. That is a rounding error of under 1%, but the value survives. This is the trade-off in one example: FP16 favours precision, and BF16 favours range.
Integer formats
An integer format stores whole numbers with no exponent. INT8 uses 8 bits and so has 256 possible values, for example -128 to 127. A 4-bit integer has only 16 possible values.
Weights are not whole numbers, so an integer format needs a scale. This is a separate floating-point number, stored alongside the integers. The stored integer times the scale approximates the original weight. For example, if the scale is 0.01, the stored integer 37 means roughly 0.37. The next chapter covers how this works for whole models. For now, keep two points in mind:
- Integer formats cost the weight bits plus a small overhead for the scales.
- Fewer bits mean fewer distinct values, so each weight is rounded more coarsely.
Some 4-bit and 8-bit formats are floating-point rather than integer, such as FP8 on recent GPUs. Files for local use are mostly the integer-with-scale kind.
From format to file size
Bytes per weight is bits divided by 8. Multiply by the parameter count to get the size of the weights.
formats = {"FP32": 32, "FP16": 16, "BF16": 16, "INT8": 8, "4-bit": 4}
params = 8e9 # an 8B-parameter model
for name, bits in formats.items():
gib = params * bits / 8 / 2**30
print(f"{name:6} {bits:2} bits {bits / 8:5.2f} bytes/weight {gib:6.1f} GiB")The output is about 29.8 GiB for FP32, 14.9 GiB for FP16 and BF16, 7.5 GiB for INT8, and 3.7 GiB for a pure 4-bit format.
Halving the bits halves the memory. Going from FP16 to 4-bit takes the model from a size that needs a 16 GB GPU to one that fits comfortably on an 8 GB card. This is why the chapter is early in the plan. Almost every later decision about hardware starts from the number of bits per weight.
Which format is used where
Different stages of a model's life use different formats:
- Training usually uses BF16 or FP32 for the main weights. Precision matters because many small updates are added up.
- Distribution often uses BF16 or FP16. The original release is the reference copy.
- Local inference often uses a quantised format with 4 to 8 bits per weight. Quantisation is the process of converting the higher-precision weights into one of these smaller formats, and the next chapter covers it.
The weights are not the only numbers in memory. Activations (intermediate results during a forward pass) and the KV cache, which a later chapter covers, have their own formats. A model quantised to 4 bits can still run its activations and KV cache at 16 bits.
Common errors
- Mixing up bits and bytes. A 16-bit format uses 2 bytes per weight. Dividing by 8 once is the whole conversion. Forgetting it gives an answer 8 times too large.
- Mixing up GB and GiB. A GiB is 2^30 bytes, about 7% larger than a GB (10^9 bytes). File sizes shown by some tools use GB. Operating system memory figures often use GiB. Pick one and stay consistent.
- Assuming FP16 and BF16 are interchangeable. They are the same size but not the same format. Loading BF16 weights as if they were FP16 gives wrong values, and converting BF16 to FP16 can overflow large values. Check the format a model was released in.
- Treating a lower bit count as a free saving. Each step down in bits removes some information. The quality chapter covers how much, and it depends on the model and the method.
Exercise
A model has 13 billion parameters and is stored in BF16. (a) How many GiB do its weights occupy? (b) The same model is converted to INT8 with negligible scale overhead. How many GiB is that? (c) Why would FP16 and BF16 give the same size but not the same behaviour?
Show answer
(a) BF16 is 16 bits, or 2 bytes, per weight. 13e9 x 2 = 26e9 bytes. Dividing by 2^30 gives about 24.2 GiB. (b) INT8 is 1 byte per weight, so 13e9 bytes, about 12.1 GiB, which is half of (a). (c) Both use 16 bits, so the size is the same. BF16 has 8 exponent bits and 7 mantissa bits, while FP16 has 5 exponent bits and 10 mantissa bits. BF16 therefore covers a far larger range (about 3.4e38 against 65504) but with lower precision, so the same bit pattern means a different value in each format.