All courses

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

▶ Watch this lesson Start the whole course

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.

Same 2 bytes, very different numbers

Each weight is typically a 2-byte number, but there are two common 16-bit formats: **fp16** and **bf16**. Training in fp16 can silently turn tiny gradients into exact zeros, so those weights stop learning. bf16 almost never does this. Both use 16 bits. So what do the bits actually encode, and what does each format give up to fit in 2 bytes?

Binary scientific notation

A float is scientific notation in base 2. It has a **sign**, an **exponent** that picks the power of 2, and a **mantissa** that holds the significant digits. - Exponent bits buy **range**: how big or how tiny a number can be. - Mantissa bits buy **precision**: how finely you can step at a given size. bf16 is fp32 with the mantissa cut short: the same range, but coarse steps. fp16 gives more bits to the mantissa, so it has finer steps but a narrow range.

The bit layout and what it buys

For normal numbers: $$x=(-1)^s\,(1+\tfrac{m}{2^{n_m}})\,2^{\,e-b}$$ Here $s$ is the sign bit, $e$ the $n_e$-bit exponent field, $b=2^{n_e-1}-1$ the bias, and $m$ the $n_m$-bit mantissa field. An all-zeros exponent field is reserved for zero and subnormals, and an all-ones field for infinity and other special values. | fmt | exp bits | mantissa bits | min normal | max | step at 1 | |---|---|---|---|---|---| | fp32 | 8 | 23 | 1.2e-38 | 3.4e38 | 1.2e-7 | | fp16 | 5 | 10 | 6.1e-5 | 65504 | 9.8e-4 | | bf16 | 8 | 7 | 1.2e-38 | 3.4e38 | 7.8e-3 | 'Step at 1' is the gap from 1 to the next representable value, $2^{-n_m}$. int8 has no exponent. It stores 256 evenly spaced integers (−128…127). One stored scale, shared by a whole tensor or block of values, converts them to real numbers.

A gradient of 1e-8 in each format

**fp16:** the smallest normal value is $2^{-14}\approx6.1\text{e-}5$. Below that come subnormals, spaced $2^{-24}\approx6\text{e-}8$ apart. Since $10^{-8}/6\text{e-}8\approx0.17$, it rounds to **0**. **bf16:** $10^{-8}=1.342\times2^{-27}$, and exponent $-27$ is far above the smallest normal exponent, $-126$. With 7 mantissa bits it is stored as $\tfrac{172}{128}\times2^{-27}\approx1.001\text{e-}8$. It survives with 0.1% error. **The cost:** near 1, bf16 steps by 0.0078, so $1.001\to1.0$. fp16 gives $1.000977$.

See it in numpy

numpy has float16 but no bf16. We emulate bf16 by rounding an fp32 number to its top 16 bits. This keeps the sign, all 8 exponent bits and 7 mantissa bits.
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

Exponent bits set range; mantissa bits set precision. bf16 keeps fp32's 8 exponent bits, so tiny gradients survive, just coarsely. fp16 has only 5. Values below about 3e-8 (half its smallest subnormal, $2^{-24}$) round to 0, and values beyond about 65504 overflow to infinity. int8 has no exponent at all: it offers 256 evenly spaced levels times a shared scale.

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.