Floating point: fp32, fp16, bf16, int8
Compare how exponent bits (range) and mantissa bits (precision) differ across fp32, fp16 and bf16. Explain why fp16 can underflow very small values, such as tiny gradients, to zero while bf16 rarely does.
Builds on: Compute, memory and scale, What training does
What the animation says
Same 2 bytes, different numbers
Most model weights are stored in just two bytes. But there are two common two-byte formats: fp16 and bf16. Train in fp16, and tiny gradients can silently become exact zeros. Those weights simply stop learning. bf16 almost never does this, with the very same sixteen bits. So the bits must be split differently. Which part of a float decides how tiny a number can get?
Binary scientific notation
The answer: the exponent. A float is scientific notation in base two. It has a sign bit, an exponent that picks the power of two, and a mantissa holding the significant digits. Exponent bits buy range. Mantissa bits buy precision: how finely you can step at a given size. fp32 splits its thirty-two bits into eight exponent bits and twenty-three mantissa bits. fp16 has five exponent bits and ten mantissa bits. bf16 has eight and seven. Tap the sixteen-bit format that has the same range as fp32.
What the bits buy
A float's value is one plus the mantissa fraction, times two to the power of the exponent minus a bias. In fp16, these bits give one plus a half, times two to the zero: one point five. So fp16's smallest normal number is about six times ten to the minus five. Its largest is about sixty-five thousand. Below that come subnormals, evenly spaced about six times ten to the minus eight apart. bf16 goes down to ten to the minus thirty-eight. Now ten to the minus eight goes into fp16. What gets stored?
1e-8 in both formats
Zero. Ten to the minus eight is only a sixth of fp16's smallest step, so it rounds down. bf16 still has normal exponents down there. Ten to the minus eight is one point three four times two to the minus twenty-seven. Seven mantissa bits store it as one seventy-two over one twenty-eight. That's about one point zero zero one times ten to the minus eight: a tenth of a percent off. The cost shows near one. bf16 steps by about eight thousandths, so one point zero zero one becomes exactly one. fp16, with finer steps, keeps one point zero zero zero nine eight.
See it in numpy
numpy has float sixteen but no bf16. This function fakes bf16: it rounds an fp32 number to its top sixteen bits. Those sixteen bits keep the sign, all eight exponent bits, and seven mantissa bits. This loop sends four gradients through both formats, from ten to the minus three down to ten to the minus thirty. Before we run it: tap every input that fp16 turns into zero.
Range vs precision, and int8
So: exponent bits set range, and mantissa bits set precision. bf16 keeps fp32's eight exponent bits, so tiny gradients survive, just coarsely. fp16 has only five. Values below about three times ten to the minus eight round to zero, and values above sixty-five thousand five hundred four overflow to infinity. int8 has no exponent at all: two hundred fifty-six evenly spaced integers, from minus one twenty-eight to one twenty-seven. One shared scale, stored per tensor or per block, turns them into real numbers.
Binary scientific notation
The bit layout and what it buys
A gradient of 1e-8 in each format
See it in numpy
import numpy as np
def bf16(x): # round fp32 to nearest bf16 (top 16 bits)
b = np.array([x], np.float32).view(np.uint32)
b = (b + 0x7FFF + ((b >> 16) & 1)) & 0xFFFF0000
return float(b.view(np.float32)[0])
for g in [1e-3, 1e-6, 1e-8, 1e-30]:
print(g, float(np.float16(g)), bf16(g))
# fp16: 1e-8 and 1e-30 become 0.0; bf16 keeps them
print(float(np.float16(1.001)), bf16(1.001))
# 1.0009765625 vs 1.0: bf16 steps are coarse
Range vs precision
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses