Simple Algorithm
Deep Learning · Emergence · 26 min

Number Formats & Mixed Precision

From FP32 to 8- and 4-bit formats: range, precision, scaling and rounding.

Every number in a training run is rounded, trillions of times a second. Whether a run converges in 8 bits or stalls in 16 depends on three properties of the number format (its range, its precision and how it rounds) and on the scale factors that slide each tensor into the window the format can represent. This lesson derives all three from the bits, proves why stochastic rounding helps, and follows the formats down to 4 bits.

What you’ll learn

  • Decode an IEEE-style binary format and compute its largest, smallest normal and smallest subnormal values and its machine epsilon from the exponent and mantissa widths.
  • Bound the relative error of round-to-nearest, and explain swamping: why updates vanish and sums stall without FP32 master weights and accumulators.
  • Prove that stochastic rounding is unbiased, bound its variance, and explain how it rescues low-precision accumulation.
  • Describe FP8 training with per-tensor and fine-grained scaling, block-scaled microscaling formats down to 4 bits, and what must stay in higher precision.

Anatomy of a floating-point number

DerivationRange and precision from the bit widths
  1. xmax⁡=(2−2−M) 2 bx_{\max}=\big(2-2^{-M}\big)\,2^{\,b}

    The largest normal has the largest finite exponent, e−b=2E−2−b=be-b=2^E-2-b=b, and an all-ones mantissa, (1.1…1)2=2−2−M(1.1\ldots1)_2=2-2^{-M}:

  2. xmin⁡normal=21−b,xmin⁡subnormal=21−b−Mx_{\min}^{\text{normal}}=2^{1-b},\qquad x_{\min}^{\text{subnormal}}=2^{1-b-M}

    The smallest normal is 21−b2^{1-b}; subnormals fill the gap below it with even spacing, down to one unit in the last place:

  3. fl⁡(x)=x(1+δ),∣δ∣≤u=2−(M+1)for x in the normal range\operatorname{fl}(x)=x(1+\delta),\qquad\abs\delta\le u=2^{-(M+1)}\qquad\text{for }x\text{ in the normal range}

    Within each binade [2k,2k+1)[2^k,2^{k+1}) the representable numbers are evenly spaced by 2k−M2^{k-M}. The gap above 11 is the machine epsilon ϵ=2−M\epsilon=2^{-M}, and round-to-nearest has relative error at most the unit roundoff u=ϵ/2u=\epsilon/2:

FormatE / MLargestSmallest normalSmallest subnormalϵ=2−M\epsilon=2^{-M}
FP328 / 233.4×10383.4\times10^{38}1.2×10−381.2\times10^{-38}1.4×10−451.4\times10^{-45}1.2×10−71.2\times10^{-7}
TF32 (inside tensor cores)8 / 103.4×10383.4\times10^{38}1.2×10−381.2\times10^{-38}n/a9.8×10−49.8\times10^{-4}
FP165 / 1065,50465{,}5046.1×10−56.1\times10^{-5}6.0×10−86.0\times10^{-8}9.8×10−49.8\times10^{-4}
BF168 / 73.4×10383.4\times10^{38}1.2×10−381.2\times10^{-38}9.2×10−419.2\times10^{-41}7.8×10−37.8\times10^{-3}
FP8 E5M25 / 257,34457{,}3446.1×10−56.1\times10^{-5}1.5×10−51.5\times10^{-5}0.250.25
FP8 E4M34 / 34484482−6≈0.0162^{-6}\approx0.0162−9≈0.0022^{-9}\approx0.0020.1250.125
FP4 E2M12 / 166110.50.50.50.5
E4M3 breaks the IEEE rule: it gives up infinities and keeps a single NaN pattern, so the all-ones exponent holds normal numbers and its maximum is 1.75×28=4481.75\times2^8=448 instead of 240 (Micikevicius et al., 2022). E2M1 has only the values ±{0,0.5,1,1.5,2,3,4,6}\pm\{0,0.5,1,1.5,2,3,4,6\}.
Predict before you look

What is FP16's machine epsilon, the gap between 1 and the next larger FP16 number? Answer in units of 10−410^{-4}.

Show the answer

9.8 × 10⁻⁴

With M=10M=10 mantissa bits, ϵ=2−10≈9.77×10−4\epsilon=2^{-10}\approx9.77\times10^{-4}, so FP16 resolves about three decimal digits and rounds with relative error at most 2−11≈4.9×10−42^{-11}\approx4.9\times10^{-4}. BF16's 2−72^{-7} is eight times coarser; FP32's 2−232^{-23} is 8,192 times finer.

Lost updates and stalled sums

Round-to-nearest is accurate relative to the result. That is exactly what goes wrong when a small number is added to a large one: if ∣y∣\abs y is less than half the spacing of representable numbers at xx, which lies between ϵ4∣x∣\tfrac{\epsilon}{4}\abs x and ϵ2∣x∣\tfrac{\epsilon}{2}\abs x depending on where xx sits in its binade, then fl⁡(x+y)=x\operatorname{fl}(x+y)=x and yy is lost entirely. This is called swamping.

Two consequences shape every training stack:

  • Accumulators stay wide. A dot product over KK terms adds KK products into one running sum, and with a short accumulator the small late terms are swamped. DeepSeek-AI (2024) measured FP8 matrix products on their GPUs accumulating with only about 14 bits and found relative errors up to nearly 2% at K=4096K=4096; copying partial sums into FP32 every 128 products restored the precision at little cost.
  • Weights stay wide. An update ηg\eta g smaller than half a unit in the weight's last place vanishes. In BF16 that is any relative change below 2−92^{-9} to 2−82^{-8} (0.2% to 0.4%, depending on where the weight sits in its binade), which covers many updates once the learning rate has decayed. Hence FP32 master weights, the classic fix (Micikevicius et al., 2018), or Kahan-style compensated summation of the updates.
Predict before you look

A BF16 accumulator starts at 0 and adds 1.01.0 again and again with round-to-nearest-even. At what value does it stop growing?

Show the answer

256

BF16 has M=7M=7, so the sum is exact up to 28=2562^{8}=256, where the spacing becomes 2 and 256+1256+1 ties back to 256. Adding a million ones in BF16 returns 256, a relative error above 99.9%. With an FP32 accumulator (M=23M=23) the stall would come only at 224≈1.7×1072^{24}\approx1.7\times10^7.

Stochastic rounding

DerivationProof, and what it buys for sums
  1. E[SR⁡(x)]=(1−p)⌊x⌋+p(⌊x⌋+Δ)=⌊x⌋+pΔ=x,Var⁡=Δ2 p(1−p)≤Δ24\E[\operatorname{SR}(x)]=(1-p)\lfloor x\rfloor+p\big(\lfloor x\rfloor+\Delta\big)=\lfloor x\rfloor+p\Delta=x,\qquad\Var=\Delta^2\,p(1-p)\le\frac{\Delta^2}4

    Direct computation:

  2. E[∑k=1nξk]=0,Var⁡[∑k=1nξk]=∑k=1nE[ξk2]≤nΔ24\E\Big[\sum_{k=1}^n\xi_k\Big]=0,\qquad\Var\Big[\sum_{k=1}^n\xi_k\Big]=\sum_{k=1}^n\E[\xi_k^2]\le\frac{n\Delta^2}{4}

    Add nn increments, rounding the running sum stochastically each time, with fresh randomness at every step. The rounding errors ξk\xi_k have conditional mean zero given the past, so they form a martingale difference sequence; their variances add, and if the spacing stays Δ\Delta:

  3. Round-to-nearest errs systematically: increments below Δ/2\Delta/2 are always dropped. Adding 10410^4 increments of Δ/10\Delta/10 gives a total of 1000Δ1000\Delta exactly, 00 with round-to-nearest, and 1000Δ1000\Delta in expectation with stochastic rounding, with standard deviation Δ104×0.1×0.9=30Δ\Delta\sqrt{10^4\times0.1\times0.9}=30\Delta: a 3% error instead of 100%.

Gupta et al. (2015) trained networks with 16-bit fixed-point numbers. With round-to-nearest, cutting the fractional bits below 14 hurt learning, because most parameter updates rounded to zero; with stochastic rounding a network on MNIST still learned with as few as 8 fractional bits. Stochastic rounding needs a random number per rounding and adds variance, so it is used where bias matters most: for gradients in recent 4-bit training recipes, and for low-precision weights when no FP32 master copy is kept.

Check your understanding

A weight is stored in a format whose spacing near it is Δ=2−10\Delta=2^{-10}, and every step adds an update of 2−132^{-13}. What happens after 800 steps?

  1. With either rounding mode the weight grows by 800×2−13800\times2^{-13} exactly
  2. With round-to-nearest it never changes; with stochastic rounding it grows by about 800×2−13=0.098800\times2^{-13}=0.098 on average, give or take about 0.0090.009
  3. With stochastic rounding it never changes either, since each update is below half the spacing
  4. Stochastic rounding makes it drift down on average
Show the answer

B. With round-to-nearest it never changes; with stochastic rounding it grows by about 800×2−13=0.098800\times2^{-13}=0.098 on average, give or take about 0.0090.009

2−13=Δ/8<Δ/22^{-13}=\Delta/8<\Delta/2, so round-to-nearest drops every update. Stochastic rounding rounds up with probability p=1/8p=1/8, so the expected gain per step is exactly 2−132^{-13}; the standard deviation after 800 steps is Δ800×18×78≈9.4Δ≈0.009\Delta\sqrt{800\times\tfrac18\times\tfrac78}\approx9.4\Delta\approx0.009.

Eight bits and below

FP8's range is too narrow for one fixed window, so each tensor is multiplied by a scale before the cast and divided by it afterwards: loss scaling generalized to every tensor.

  • Per-tensor scaling, two encodings. Micikevicius et al. (2022) recommend E4M3 for weights and activations and E5M2 for gradients, which need range more than precision; with per-tensor scales, FP8 training matched 16-bit results across CNNs, RNNs and transformers up to 175 billion parameters with unchanged hyperparameters. Delayed scaling picks each tensor's scale from the largest magnitudes seen in recent steps, avoiding an extra pass over the data; current scaling measures it on the fly.
  • Fine-grained scaling. DeepSeek-V3 used E4M3 for all tensors, made possible by scales per 1×1281\times128 tile of activations and per 128×128128\times128 block of weights, plus FP32 accumulation every 128 products; in validation runs of about a trillion tokens its loss stayed within 0.25% (relative) of a BF16 baseline (DeepSeek-AI, 2024).
  • Microscaling (MX). An open standard gives each block of 32 elements a shared 8-bit power-of-two scale (E8M0) and narrow elements: MXFP8, MXFP6, MXFP4 (E2M1) or MXINT8, at a cost of 8/32=0.258/32=0.25 extra bits per element. Its authors trained GPT-style models up to 1.5 billion parameters with 6-bit weights, activations and gradients close to FP32 (Rouhani et al., 2023).
  • NVFP4. Blocks of 16 E2M1 values share an FP8 E4M3 scale, finer than a power of two, plus one FP32 scale per tensor (4+8/16=4.54+8/16=4.5 bits per element). Training a 12-billion-parameter model on 10 trillion tokens tracked an FP8 baseline closely, with a recipe of random Hadamard transforms to spread outliers, 16×1616\times16 block scales for weights, stochastic rounding for gradients, and about 15% of the linear layers, mostly near the output, kept in higher precision (NVIDIA, 2025).

Key takeaways

  • An IEEE-style format with EE exponent and MM mantissa bits has bias 2E−1−12^{E-1}-1, maximum (2−2−M)2 b(2-2^{-M})2^{\,b}, smallest normal 21−b2^{1-b}, subnormals to 21−b−M2^{1-b-M}, and machine epsilon 2−M2^{-M}; round-to-nearest errs by at most 2−(M+1)2^{-(M+1)} relative.
  • Adding a number smaller than half a unit in the last place loses it: a sum of ones stalls at 2M+12^{M+1} (2,048 in FP16, 256 in BF16), so accumulators and master weights stay in FP32.
  • Stochastic rounding is unbiased with variance at most Δ2/4\Delta^2/4; summed errors form a martingale growing like n\sqrt n instead of a systematic drift.
  • FP8 training scales each tensor (E4M3 for weights and activations, E5M2 for gradients) or each tile; microscaling shares a scale per block of 32 (MX) or 16 (NVFP4) to reach 4 bits, with stochastic rounding, outlier spreading and some layers kept wide.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

An IEEE-style format has 5 exponent bits and 2 mantissa bits (FP8 E5M2). What is its largest finite value?

  1. 448448
  2. 65,50465{,}504
  3. 57,34457{,}344
  4. 32,76832{,}768
Show the answer

C. 57,34457{,}344

The bias is 24−1=152^4-1=15; the largest normal exponent is 30−15=1530-15=15 and the largest mantissa (1.11)2=1.75(1.11)_2=1.75, so 1.75×215=57,3441.75\times2^{15}=57{,}344. Its exponent range matches FP16's; it simply has 8 fewer mantissa bits.

Question 2

In BF16 with round-to-nearest-even, what is 256+1256+1?

  1. 257257
  2. 256256
  3. 258258
  4. Overflow
Show the answer

B. 256256

Above 256, BF16's 8 bits of precision give a spacing of 2. 257257 lies exactly halfway between 256 and 258, and the tie goes to the even mantissa, 256. This is why accumulators are kept in FP32.

Question 3

What makes stochastic rounding unbiased?

  1. It rounds to the nearer neighbor, breaking ties at random
  2. It adds zero-mean noise to the value and then truncates
  3. It always rounds up and down alternately
  4. It rounds up with probability equal to the fractional distance to the lower neighbor, so the expected result equals the input
Show the answer

D. It rounds up with probability equal to the fractional distance to the lower neighbor, so the expected result equals the input

With p=(x−⌊x⌋)/Δp=(x-\lfloor x\rfloor)/\Delta, E[SR⁡(x)]=⌊x⌋+pΔ=x\E[\operatorname{SR}(x)]=\lfloor x\rfloor+p\Delta=x. Random tie-breaking only matters at exact midpoints and leaves round-to-nearest's bias for everything else.

Question 4

Why do microscaling formats such as MXFP4 and NVFP4 give a scale to every block of 16 or 32 values rather than one per tensor?

  1. A 4-bit element spans only a few binades, so one scale cannot cover a tensor's range; local scales adapt to each block, including blocks with outliers
  2. Because hardware cannot multiply by one scale per tensor
  3. To make the format exactly 4 bits per element
  4. Because block scales make rounding unbiased
Show the answer

A. A 4-bit element spans only a few binades, so one scale cannot cover a tensor's range; local scales adapt to each block, including blocks with outliers

E2M1 covers 0.50.5 to 66: a dozen-fold range. Per-block scales place that small window where each block's values are, at a cost of 8/32=0.258/32=0.25 (MX) or 8/16=0.58/16=0.5 (NVFP4) extra bits per element.

Further reading

  • P. Micikevicius et al., "FP8 formats for deep learning," arXiv:2209.05433 (2022).
  • S. Gupta, A. Agrawal, K. Gopalakrishnan & P. Narayanan, "Deep learning with limited numerical precision," ICML (2015).
  • B. D. Rouhani et al., "Microscaling data formats for deep learning," arXiv:2310.10537 (2023).
  • NVIDIA, "Pretraining large language models with NVFP4," arXiv:2509.25149 (2025).
  • DeepSeek-AI, "DeepSeek-V3 technical report," arXiv:2412.19437 (2024).
  • N. J. Higham, Accuracy and Stability of Numerical Algorithms (2nd ed., SIAM, 2002).