{"slug": "bf16-in-one-picture-8-bits-of-mantissa-round-to-nearest-even-and-what-it-hides", "title": "bf16 in one picture: 8 bits of mantissa, round-to-nearest-even, and what it hides", "summary": "A developer measured bfloat16's limits on PyTorch 2.10, showing that adding 1.0 to a bf16 counter a thousand times yields exactly 256 and that summing 4,096 random values between 0 and 1 in bf16 gives 256 versus an exact total near 2,020. The experiments trace the failures to bf16's 8-bit mantissa and round-to-nearest-even behavior, and conclude that bf16 should be used for storage while reductions accumulate in fp32.", "body_md": "Add 1.0 to a bfloat16 counter a thousand times. The answer is 256.\n\nNot 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.\n\nbf16 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.\n\n```\nfp32:  S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM   (1 + 8 + 23 bits)\nbf16:  S EEEEEEEE MMMMMMM                   (1 + 8 +  7 bits)\n       \\_______________/\n       bf16 = the top 16 bits of fp32, rounded\n```\n\nbf16 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:\n\n``` php\n3.14159265  fp32 0x40490FDB  bf16 0x4049  -> 3.140625\n0.1         fp32 0x3DCCCCCD  bf16 0x3DCD  -> 0.10009765625\n1/3         fp32 0x3EAAAAAB  bf16 0x3EAB  -> 0.333984375\n1000.0      fp32 0x447A0000  bf16 0x447A  -> 1000.0\n```\n\nThe 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.\n\nBetween 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`:\n\nSo 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.\n\nThe 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.\n\n**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.\n\nfp16 spends its bits the other way: 5 exponent bits, 10 mantissa bits. From `torch.finfo`:\n\nfp16 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\".\n\nThe flip side shows up in the first experiment. fp16's largest value, 65504, is not representable in bf16 and becomes 65536.\n\nWhen 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:\n\n``` php\n256 -> 256    257 -> 256    258 -> 258    259 -> 260\n260 -> 260    261 -> 260    262 -> 262    263 -> 264\n```\n\n257 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:\n\nThe 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.\n\nWhy 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):\n\nTruncation 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.\n\nTwo 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.\n\nHere is the part that matters for LLM inference. A 4096-long dot product of N(0,1) vectors (4096 is a typical hidden size):\n\nThree observations.\n\nFirst, 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.\n\nSecond, 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\".\n\nThird, *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.\n\nThe whole post comes from one script, `blog_03_script.py`, about 80 lines and PyTorch only. The core of it:\n\n``` python\nimport torch\ntorch.manual_seed(0)\n\n# a bf16 counter\nacc = torch.tensor(0.0, dtype=torch.bfloat16)\nfor i in range(1000):\n    acc = acc + torch.tensor(1.0, dtype=torch.bfloat16)\nprint(acc.item())  # 256.0\n\n# round-to-nearest-even vs truncation\nx = torch.randn(1_000_000)\nrne = x.to(torch.bfloat16).float()\ntrunc = (x.view(torch.int32) & ~0xFFFF).view(torch.float32)\nfor name, y in [(\"rne\", rne), (\"trunc\", trunc)]:\n    rel = (y - x) / x\n    print(name, rel.abs().max().item(), rel.abs().mean().item(), rel.mean().item())\n```\n\nSeed 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.\n\nI'm building Vahana, an open-source engine for exact MoE inference: github.com/vahana-ai/Vahana", "url": "https://wpnews.pro/news/bf16-in-one-picture-8-bits-of-mantissa-round-to-nearest-even-and-what-it-hides", "canonical_source": "https://dev.to/vahana/bf16-in-one-picture-8-bits-of-mantissa-round-to-nearest-even-and-what-it-hides-2a0o", "published_at": "2026-12-04 03:42:58+00:00", "updated_at": "2026-12-04 03:45:44.988826+00:00", "lang": "en", "topics": ["machine-learning", "ai-infrastructure", "large-language-models"], "entities": ["PyTorch", "IEEE"], "also_reported_by": [], "takeaway": "A bf16 accumulator stalls at 256 when adding 1.0 repeatedly and returns 256 for a 4,096-element sum that should total about 2,020, so multiply in bf16 but accumulate reductions in fp32.", "recommendation": {"advice": "Keep bf16 for stored weights and inputs, but always accumulate reductions and counters in fp32; a bf16 accumulator stalls at 256 and undercounts a 4,096-element sum by roughly 8x.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/bf16-in-one-picture-8-bits-of-mantissa-round-to-nearest-even-and-what-it-hides", "markdown": "https://wpnews.pro/news/bf16-in-one-picture-8-bits-of-mantissa-round-to-nearest-even-and-what-it-hides.md", "text": "https://wpnews.pro/news/bf16-in-one-picture-8-bits-of-mantissa-round-to-nearest-even-and-what-it-hides.txt", "jsonld": "https://wpnews.pro/news/bf16-in-one-picture-8-bits-of-mantissa-round-to-nearest-even-and-what-it-hides.jsonld"}}