Add 1.0 to a bfloat16 counter a thousand times. The answer is 256.
Not 1000, not "about 1000". Exactly 256. I ran it on my laptop (PyTorch 2.10, CPU) and the counter stops at 256 and never moves again. Then I summed 4,096 random numbers between 0 and 1. The true total is about 2,020. A bf16 accumulator says 256.
bf16 is the default weight format for most open LLMs. Here is the whole format in one picture and a handful of measured experiments, so you know what it keeps, what it throws away, and where it bites.
The picture
fp32: S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM (1 + 8 + 23 bits) bf16: S EEEEEEEE MMMMMMM (1 + 8 + 7 bits) \_______________/ bf16 = the top 16 bits of fp32, rounded bf16 keeps the sign and all 8 exponent bits of fp32 and only the top 7 stored mantissa bits. With the implicit leading 1, that is 8 significant bits. Converting fp32 to bf16 means dropping the low 16 bits, with rounding. You can see it in the hex:
3.14159265 fp32 0x40490FDB bf16 0x4049 -> 3.140625 0.1 fp32 0x3DCCCCCD bf16 0x3DCD -> 0.10009765625 1/3 fp32 0x3EAAAAAB bf16 0x3EAB -> 0.333984375 1000.0 fp32 0x447A0000 bf16 0x447A -> 1000.0 The bf16 pattern is the first four hex digits of the fp32 pattern, sometimes plus one. That "plus one" is the rounding: 0x3DCCCCCD becomes 0x3DCD, not 0x3DCC, because the dropped half (0xCCCD) is more than halfway.
What 8 bits of precision means
Between 1 and 2 there are exactly 128 bf16 values: 1.0, 1.0078125, ..., 1.9921875. I counted them by enumerating the bit patterns 0x3F80 to 0x3FFF. The gap between neighbours (one "ulp") doubles at each power of two. Measured with torch.nextafter:
- near 1: 0.0078125
- near 2: 0.015625
- near 256: 2
- near 1024: 8
- near 65536: 512
So above 256, bf16 can't represent odd integers. Above 1024 it can only represent multiples of 8. That is where the counter result comes from. At 256 the next value up is 258. 256 + 1 = 257 lands exactly halfway between 256 and 258, and the tie goes to 256 (more on why below). The counter is stuck.
The uniform-sum result is the same effect, only worse. Every input is less than 1, which is less than half an ulp at 256, so every addition rounds back to 256. The fp32 accumulator, given the same bf16 inputs, gets 2020.6592 against an exact (fp64) sum of 2020.6596.
Lesson one: bf16 is a storage format, not an accumulation format. Every serious kernel multiplies bf16 inputs but accumulates in fp32. If you write your own reduction and leave the accumulator in the input dtype, you get 256.
What 8 exponent bits buys
fp16 spends its bits the other way: 5 exponent bits, 10 mantissa bits. From torch.finfo:
- fp16: max 65504, eps 0.0009765625
- bf16: max 3.39e38, eps 0.0078125
fp16 is 8 times more precise, but 70,000 in fp16 is inf. In bf16 it is 70144: wrong in the third digit, but finite. For training, where activations and gradients can swing over many orders of magnitude, overflow is the failure that kills a run, so the range wins. That is the whole reason bf16 exists: same range as fp32, half the bytes, and conversion is just "take the top half".
The flip side shows up in the first experiment. fp16's largest value, 65504, is not representable in bf16 and becomes 65536.
Round-to-nearest-even
When the dropped bits are exactly half an ulp, IEEE rounding picks the neighbour whose last bit is 0 (the "even" one). Converting the integers 250 to 265 to bf16:
256 -> 256 257 -> 256 258 -> 258 259 -> 260 260 -> 260 261 -> 260 262 -> 262 263 -> 264 257 and 261 are ties and go down. 259 and 263 are ties and go up. Each one goes to whichever neighbour is "even" in the last mantissa bit. Near 1.0, same story:
- 1 + 2^-8 = 1.00390625 (an exact tie) -> 1.0
- 1 + 3·2^-8 = 1.01171875 (an exact tie) -> 1.015625
- 1.00390625 plus a tiny bit more (1.0039072...) -> 1.0078125
The last line matters. A tie is only a tie if every dropped bit after the first is zero. One bit set anywhere in the low 15 bits and the value rounds up.
Why bother with "even" instead of always rounding ties up? Because ties then go up half the time and down half the time, so there is no systematic drift. I measured it on one million N(0,1) values, comparing proper rounding with plain truncation (zeroing the low 16 bits, which some fast conversion code does):
- round-to-nearest-even: max relative error 0.00389, mean 0.00141, mean signed error 3.5e-7
- truncation: max relative error 0.00775, mean 0.00281, mean signed error -0.00281
Truncation doubles the error. Worse, it is biased: every value moves toward zero, so the signed mean equals the absolute mean. Rounding's signed mean is essentially zero. The max relative error for rounding stays under 2^-8 = 0.0039, which is exactly the bound you expect from 8 significant bits.
Two more numbers from the same run. 99.9986% of the values changed when converted (almost no random fp32 number fits in 8 bits). And 49.98% of them came out differently under truncation and rounding, which is the coin flip you would expect: whenever the dropped half is at least halfway, rounding goes up and truncation doesn't.
What it hides
Here is the part that matters for LLM inference. A 4096-long dot product of N(0,1) vectors (4096 is a typical hidden size):
- fp32 inputs, computed in fp64: 65.0941
- the same vectors rounded to bf16, computed in fp64: 65.1501
- that result rounded to bf16 for output: 65.0
Three observations.
First, quantizing the inputs moved the answer by 0.056. That is the price of storing weights in bf16, and it is fixed: every engine that loads the same bf16 checkpoint pays exactly the same price.
Second, the output rounding is coarse. Near 64, the bf16 ulp is 0.5, so everything from 64.75 to 65.25 comes out as 65.0. This is the "hiding" in the title. Two engines that sum the same products in different orders get fp32 results that differ in the last few bits, and most of the time both round to the same bf16 output. A spot check says "identical".
Third, most of the time is not always. When the fp32 result sits near the midpoint between two bf16 values (here, near 64.75 or 65.25), a difference in the 20th bit flips the rounding and the outputs differ by a whole bf16 ulp. In a transformer that flip feeds the next layer, and a few dozen layers later it can be a different token. bf16 doesn't remove nondeterminism. It makes it rare, which makes it harder to catch.
Takeaways
- bf16 = fp32's sign and exponent plus the top 7 mantissa bits. Conversion is "keep the top 16 bits, rounded to nearest even".
- 8 significant bits: 128 values per power-of-two interval, relative error at most 2^-8.
- Never accumulate in bf16. A bf16 counter stops at 256.
- Same range as fp32, so overflow is rare. fp16 is more precise but overflows at 65504.
- Round-to-nearest-even is unbiased; truncation doubles the error and pushes everything toward zero.
- Output rounding hides small order-of-summation differences most of the time, and turns them into whole-ulp jumps the rest of the time.
Reproduce it
The whole post comes from one script, blog_03_script.py, about 80 lines and PyTorch only. The core of it:
import torch torch.manual_seed(0) # a bf16 counter acc = torch.tensor(0.0, dtype=torch.bfloat16) for i in range(1000): acc = acc + torch.tensor(1.0, dtype=torch.bfloat16) print(acc.item()) # 256.0 # round-to-nearest-even vs truncation x = torch.randn(1_000_000) rne = x.to(torch.bfloat16).float() trunc = (x.view(torch.int32) & ~0xFFFF).view(torch.float32) for name, y in [("rne", rne), ("trunc", trunc)]: rel = (y - x) / x print(name, rel.abs().max().item(), rel.abs().mean().item(), rel.mean().item()) Seed 0, PyTorch 2.10 on a Windows laptop CPU. The bit-level results (hex patterns, ulps, ties, the 256 counter) are the same on any IEEE-conforming machine. The random-sample statistics depend on the seed, but not by much.
I'm building Vahana, an open-source engine for exact MoE inference: github.com/vahana-ai/Vahana
