{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"half-precision-and-mixed-precision","title":"bf16, fp16, and why training keeps a float32 copy","url":"https://addaly.com/learn/maths-you-need/half-precision-and-mixed-precision","position":68,"of":76,"minutes":9,"answer":"fp16 keeps three digits and a range to 65,504 while bf16 keeps two digits and float32's full range, and because a small update added to a half-precision weight rounds away entirely.","keyIdea":"fp16 keeps three digits and a range to 65,504 while bf16 keeps two digits and float32's full range, and because a small update added to a half-precision weight rounds away entirely, training keeps a float32 master copy of every weight and accumulates matrix products in float32."},"body":{"format":"markdown","text":"## Two ways to spend sixteen bits\n\nHalve the width of a float and you must give up either range or precision. The two sixteen-bit formats make opposite choices.\n\n```\nformat   sign  exponent  mantissa   max value   epsilon    digits\nfp16      1       5        10        65,504     9.8e-4     ~3\nbf16      1       8         7       3.4e38      7.8e-3     ~2\nfloat32   1       8        23       3.4e38      1.2e-7     ~7\n```\n\n**fp16** keeps ten bits of mantissa, three decimal digits, and spends only five on the exponent. Its largest value is 65,504 and its smallest normal value is `6 × 10^-5`. **bf16**, \"brain float\", keeps float32's eight exponent bits and therefore its whole range, and pays with a seven-bit mantissa: two and a bit decimal digits. A bf16 number is a float32 with its bottom sixteen bits cut off, which makes conversion a truncation rather than a computation.\n\nModule 7 established why either is wanted: half the bytes per parameter, twice the throughput on hardware built for them. The question is which half of the information to keep.\n\n## Why range beats precision for training\n\nGradients span an enormous range. Early layers of a deep network routinely see gradients of `10^-8`, and activations after a bad step can reach `10^5`. In fp16 the first underflows to zero and the second overflows to infinity, and neither problem announces itself except as a model that does not learn or a loss that becomes NaN. Module 5's overflow point moves from `exp(88.7)` to `exp(11.1)`: a logit of 12 is enough to produce infinity in fp16.\n\nbf16 has none of these problems, because its range is float32's. What it lacks is precision, and it turns out that a weight update does not need much: a gradient known to two digits still points the right way. So bf16 became the default for training wherever the hardware supports it, and fp16 survives mainly for inference on older cards.\n\n> Figure: Sixteen bits, spent two ways. fp16: 5 exponent bits, 10 mantissa bits: Largest value 65,504; Smallest normal value 0.00006; About three decimal digits; A logit of 12 already overflows exp; Training survives only with dynamic loss scaling. bf16: 8 exponent bits, 7 mantissa bits: Largest value 3.4 followed by 38 zeros; Exactly float32's range; About two decimal digits; Converting from float32 is a truncation; The default for training wherever hardware allows. A weight update does not need many digits; it needs to be representable at all, which is why range beat precision. It is also why the optimiser keeps a float32 master copy: near 1 the gap between bf16 numbers is 0.0078, so an update of 0.0001 added there vanishes entirely.\n\n## Loss scaling: the fp16 workaround\n\nWhere fp16 must be used for training, the underflow is handled by multiplying the loss by a large constant, say 1,024, before the backward pass. Every gradient is then 1,024 times larger, which lifts the `10^-8` gradients into fp16's range. The optimiser divides by the same constant before applying the update. If any gradient overflows to infinity, the step is skipped and the scale is halved; if a run of steps succeeds, the scale is doubled. This is *dynamic loss scaling*, it is in every mixed-precision training loop that uses fp16, and its entire purpose is to fit a range of `10^13` into a format that holds `10^9`.\n\n## Why there is a float32 copy\n\nHere is the fact that explains the 16 bytes per parameter in module 7. Suppose a weight is 1.0 and the update is `0.0001`. In bf16, with its two digits, `1.0 + 0.0001 = 1.0`: the update is smaller than the gap between neighbouring bf16 numbers near 1, which is `0.0078`. The weight never changes. In fp16 the gap near 1 is `0.001`, and the same update is lost.\n\nSmall updates are the normal case late in training, when the learning rate has decayed. So the optimiser keeps a **master copy** of every weight in float32, applies the update there, where the gap near 1 is `10^-7`, and then rounds the result to bf16 for the next forward pass. The half-precision weights are a working copy; the float32 ones are the truth. Remove the master copy and training stalls once the updates fall below the half-precision gap, which is typically within the first few thousand steps.\n\n## Where the precision is actually spent\n\nMixed precision is precise about what runs in what:\n\n- **Matrix multiplies**: inputs in bf16, but the products *accumulated* in float32 inside the hardware, then rounded once at the end. Accumulating a 4,096-term dot product in bf16 would lose most of it, for the reasons the next lesson gives.\n- **Softmax, layer normalisation, the loss**: computed in float32, because they involve sums of many terms and subtractions of nearly equal ones.\n- **Weights and activations in memory**: bf16.\n- **Master weights, optimiser moments**: float32.\n\nThe framework's `autocast` does this routing for you. What it cannot do is make a computation that you wrote in bf16 by hand, an accumulating sum or a variance, come out right.\n\n## Inference is more forgiving\n\nNo updates, no gradients, no accumulation across steps. Weights and activations in fp16 or bf16 lose almost nothing measurable, which is why every served model is at most half precision and usually smaller, and why the quantisation lesson can go further still. The one inference computation that still wants float32 is the final softmax over a large vocabulary, where bf16's two digits turn a distribution over 100,000 tokens into something visibly lumpy.\n\n## Seeing the gap\n\n```python\nimport torch\nw = torch.tensor(1.0, dtype=torch.bfloat16)\nprint(w + 0.001)              # 1.0     the update vanished\nw32 = torch.tensor(1.0)\nprint((w32 + 0.001).to(torch.bfloat16))   # 1.0 again after rounding, but 1.001 was kept in w32\n```\n\nThe second line is the master copy in miniature: the float32 value remembers what the bf16 one cannot, and after ten such updates it has moved to 1.01, which bf16 can then represent."},"check":{"question":"A team trains in pure bf16 with no float32 master weights. Training looks normal for a few thousand steps, then the loss flattens and the weights stop changing though the gradients are non-zero. Why?","options":[{"text":"The updates fell below the gap between adjacent bf16 values.","correct":true},{"text":"The learning rate schedule has decayed to zero.","correct":false,"tempting":"A decayed schedule would be visible in the config; the described symptom appears whenever updates fall below bf16's gap, which happens well before the rate reaches zero."},{"text":"bf16 has too small a range and the gradients have underflowed to zero.","correct":false,"tempting":"bf16 shares float32's exponent range, so underflow is not its weakness; the gradients are stated to be non-zero."},{"text":"bf16 matrix multiplies accumulate error until the gradient direction is meaningless.","correct":false,"tempting":"Accumulation inside the multiply is done in float32 by the hardware; the loss of updates happens at the moment of adding a tiny step to a coarse weight."}]},"prev":{"slug":"floating-point-anatomy","title":"What a float32 actually stores, and where its seven digits run out","url":"https://addaly.com/learn/maths-you-need/floating-point-anatomy"},"next":{"slug":"cancellation-and-summation-order","title":"Catastrophic cancellation, and why a sum depends on its order","url":"https://addaly.com/learn/maths-you-need/cancellation-and-summation-order"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'bf16, fp16, and why training keeps a float32 copy', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/half-precision-and-mixed-precision (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"bf16, fp16, and why training keeps a float32 copy","courseTitle":"The Maths You Actually Need","module":{"index":8,"slug":"numbers-in-a-machine","title":"Numbers inside a machine"},"answer":null,"keyIdea":"fp16 keeps three digits and a range to 65,504 while bf16 keeps two digits and float32’s full range, and because a small update added to a half-precision weight rounds away entirely, training keeps a float32 master copy of every weight and accumulates matrix products in float32.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"caption":"A weight update does not need many digits; it needs to be representable at all, which is why range beat precision. It is also why the optimiser keeps a float32 master copy: near 1 the gap between bf16 numbers is 0.0078, so an update of 0.0001 added there vanishes entirely.","left":{"title":"fp16: 5 exponent bits, 10 mantissa bits","items":["Largest value 65,504","Smallest normal value 0.00006","About three decimal digits","A logit of 12 already overflows exp","Training survives only with dynamic loss scaling"]},"right":{"title":"bf16: 8 exponent bits, 7 mantissa bits","items":["Largest value 3.4 followed by 38 zeros","Exactly float32’s range","About two decimal digits","Converting from float32 is a truncation","The default for training wherever hardware allows"]},"title":"Sixteen bits, spent two ways","type":"compare"},"alt":"Figure: Sixteen bits, spent two ways. fp16: 5 exponent bits, 10 mantissa bits: Largest value 65,504; Smallest normal value 0.00006; About three decimal digits; A logit of 12 already overflows exp; Training survives only with dynamic loss scaling. bf16: 8 exponent bits, 7 mantissa bits: Largest value 3.4 followed by 38 zeros; Exactly float32’s range; About two decimal digits; Converting from float32 is a truncation; The default for training wherever hardware allows. A weight update does not need many digits; it needs to be representable at all, which is why range beat precision. It is also why the optimiser keeps a float32 master copy: near 1 the gap between bf16 numbers is 0.0078, so an update of 0.0001 added there vanishes entirely."}},"blocks":[{"t":"h2","id":"two-ways-to-spend-sixteen-bits","runs":[{"text":"Two ways to spend sixteen bits"}]},{"t":"p","runs":[{"text":"Halve the width of a float and you must give up either range or precision. The two sixteen-bit formats make opposite choices."}]},{"t":"code","lang":"","label":"Text","text":"format   sign  exponent  mantissa   max value   epsilon    digits\nfp16      1       5        10        65,504     9.8e-4     ~3\nbf16      1       8         7       3.4e38      7.8e-3     ~2\nfloat32   1       8        23       3.4e38      1.2e-7     ~7"},{"t":"p","runs":[{"text":"fp16","b":true},{"text":" keeps ten bits of mantissa, three decimal digits, and spends only five on the exponent. Its largest value is 65,504 and its smallest normal value is "},{"text":"6 × 10^-5","code":true},{"text":". "},{"text":"bf16","b":true},{"text":", “brain float”, keeps float32’s eight exponent bits and therefore its whole range, and pays with a seven-bit mantissa: two and a bit decimal digits. A bf16 number is a float32 with its bottom sixteen bits cut off, which makes conversion a truncation rather than a computation."}]},{"t":"p","runs":[{"text":"Module 7 established why either is wanted: half the bytes per parameter, twice the throughput on hardware built for them. The question is which half of the information to keep."}]},{"t":"h2","id":"why-range-beats-precision-for-training","runs":[{"text":"Why range beats precision for training"}]},{"t":"p","runs":[{"text":"Gradients span an enormous range. Early layers of a deep network routinely see gradients of "},{"text":"10^-8","code":true},{"text":", and activations after a bad step can reach "},{"text":"10^5","code":true},{"text":". In fp16 the first underflows to zero and the second overflows to infinity, and neither problem announces itself except as a model that does not learn or a loss that becomes NaN. Module 5’s overflow point moves from "},{"text":"exp(88.7)","code":true},{"text":" to "},{"text":"exp(11.1)","code":true},{"text":": a logit of 12 is enough to produce infinity in fp16."}]},{"t":"p","runs":[{"text":"bf16 has none of these problems, because its range is float32’s. What it lacks is precision, and it turns out that a weight update does not need much: a gradient known to two digits still points the right way. So bf16 became the default for training wherever the hardware supports it, and fp16 survives mainly for inference on older cards."}]},{"t":"h2","id":"loss-scaling-the-fp16-workaround","runs":[{"text":"Loss scaling: the fp16 workaround"}]},{"t":"p","runs":[{"text":"Where fp16 must be used for training, the underflow is handled by multiplying the loss by a large constant, say 1,024, before the backward pass. Every gradient is then 1,024 times larger, which lifts the "},{"text":"10^-8","code":true},{"text":" gradients into fp16’s range. The optimiser divides by the same constant before applying the update. If any gradient overflows to infinity, the step is skipped and the scale is halved; if a run of steps succeeds, the scale is doubled. This is "},{"text":"dynamic loss scaling","i":true},{"text":", it is in every mixed-precision training loop that uses fp16, and its entire purpose is to fit a range of "},{"text":"10^13","code":true},{"text":" into a format that holds "},{"text":"10^9","code":true},{"text":"."}]},{"t":"h2","id":"why-there-is-a-float32-copy","runs":[{"text":"Why there is a float32 copy"}]},{"t":"p","runs":[{"text":"Here is the fact that explains the 16 bytes per parameter in module 7. Suppose a weight is 1.0 and the update is "},{"text":"0.0001","code":true},{"text":". In bf16, with its two digits, "},{"text":"1.0 + 0.0001 = 1.0","code":true},{"text":": the update is smaller than the gap between neighbouring bf16 numbers near 1, which is "},{"text":"0.0078","code":true},{"text":". The weight never changes. In fp16 the gap near 1 is "},{"text":"0.001","code":true},{"text":", and the same update is lost."}]},{"t":"p","runs":[{"text":"Small updates are the normal case late in training, when the learning rate has decayed. So the optimiser keeps a "},{"text":"master copy","b":true},{"text":" of every weight in float32, applies the update there, where the gap near 1 is "},{"text":"10^-7","code":true},{"text":", and then rounds the result to bf16 for the next forward pass. The half-precision weights are a working copy; the float32 ones are the truth. Remove the master copy and training stalls once the updates fall below the half-precision gap, which is typically within the first few thousand steps."}]},{"t":"h2","id":"where-the-precision-is-actually-spent","runs":[{"text":"Where the precision is actually spent"}]},{"t":"p","runs":[{"text":"Mixed precision is precise about what runs in what:"}]},{"t":"ul","items":[[{"text":"Matrix multiplies","b":true},{"text":": inputs in bf16, but the products "},{"text":"accumulated","i":true},{"text":" in float32 inside the hardware, then rounded once at the end. Accumulating a 4,096-term dot product in bf16 would lose most of it, for the reasons the next lesson gives."}],[{"text":"Softmax, layer normalisation, the loss","b":true},{"text":": computed in float32, because they involve sums of many terms and subtractions of nearly equal ones."}],[{"text":"Weights and activations in memory","b":true},{"text":": bf16."}],[{"text":"Master weights, optimiser moments","b":true},{"text":": float32."}]]},{"t":"p","runs":[{"text":"The framework’s "},{"text":"autocast","code":true},{"text":" does this routing for you. What it cannot do is make a computation that you wrote in bf16 by hand, an accumulating sum or a variance, come out right."}]},{"t":"h2","id":"inference-is-more-forgiving","runs":[{"text":"Inference is more forgiving"}]},{"t":"p","runs":[{"text":"No updates, no gradients, no accumulation across steps. Weights and activations in fp16 or bf16 lose almost nothing measurable, which is why every served model is at most half precision and usually smaller, and why the quantisation lesson can go further still. The one inference computation that still wants float32 is the final softmax over a large vocabulary, where bf16’s two digits turn a distribution over 100,000 tokens into something visibly lumpy."}]},{"t":"h2","id":"seeing-the-gap","runs":[{"text":"Seeing the gap"}]},{"t":"code","lang":"python","label":"Python","text":"import torch\nw = torch.tensor(1.0, dtype=torch.bfloat16)\nprint(w + 0.001)              # 1.0     the update vanished\nw32 = torch.tensor(1.0)\nprint((w32 + 0.001).to(torch.bfloat16))   # 1.0 again after rounding, but 1.001 was kept in w32"},{"t":"p","runs":[{"text":"The second line is the master copy in miniature: the float32 value remembers what the bf16 one cannot, and after ten such updates it has moved to 1.01, which bf16 can then represent."}]}],"check":{"question":[{"text":"A team trains in pure bf16 with no float32 master weights. Training looks normal for a few thousand steps, then the loss flattens and the weights stop changing though the gradients are non-zero. Why?"}],"options":[{"text":[{"text":"The updates fell below the gap between adjacent bf16 values."}],"correct":true,"tempting":null},{"text":[{"text":"The learning rate schedule has decayed to zero."}],"correct":false,"tempting":[{"text":"A decayed schedule would be visible in the config; the described symptom appears whenever updates fall below bf16’s gap, which happens well before the rate reaches zero."}]},{"text":[{"text":"bf16 has too small a range and the gradients have underflowed to zero."}],"correct":false,"tempting":[{"text":"bf16 shares float32’s exponent range, so underflow is not its weakness; the gradients are stated to be non-zero."}]},{"text":[{"text":"bf16 matrix multiplies accumulate error until the gradient direction is meaningless."}],"correct":false,"tempting":[{"text":"Accumulation inside the multiply is done in float32 by the hardware; the loss of updates happens at the moment of adding a tiny step to a coarse weight."}]}],"order":[3,1,2,0],"why":null},"prev":{"slug":"floating-point-anatomy","title":"What a float32 actually stores, and where its seven digits run out","minutes":10},"next":{"slug":"cancellation-and-summation-order","title":"Catastrophic cancellation, and why a sum depends on its order","minutes":9}}}