Speed

Why Exact by Hand Stops

Observe 32-bit floating-point swamping when small gradient or loss increments are added one by one onto a large accumulator.

The problem

Paper algebra assumes real-number associativity: (a+b)+c=a+(b+c)(a + b) + c = a + (b + c). Hardware accelerators do not store infinite-precision reals; standard 32-bit floating point (float32) allocates 11 sign bit, 88 exponent bits, and 2323 fraction bits (2424 bits of significand precision).

Once a running loss sum or parameter accumulator reaches 223=8,388,6082^{23} = 8{,}388{,}608, consecutive representable float32 values are spaced 1.01.0 apart. What happens when we add eight small increments of 0.250.25 onto 8,388,6088{,}388{,}608 in float32 on [8388608, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25]? On paper, 8⋅0.25=2.08 \cdot 0.25 = 2.0 and the total is 8,388,6108{,}388{,}610, yet naive left-to-right float32 addition loses the entire update.

Work through the numbers

Trace the naive sequential loop in float32 starting at total =8,388,608.0= 8{,}388{,}608.0. Step 1 forms the exact sum 8,388,608.258{,}388{,}608.25. Because adjacent float32 numbers in [223,224)[2^{23}, 2^{24}) are 8,388,608.08{,}388{,}608.0 and 8,388,609.08{,}388{,}609.0 (gap 1.01.0), 8,388,608.258{,}388{,}608.25 sits one-quarter of a step above 8,388,608.08{,}388{,}608.0 and rounds back down to 8,388,608.08{,}388{,}608.0.

Steps 2 through 8 repeat the identical trap: each 0.250.25 arrives alone, rounds back to 8,388,608.08{,}388{,}608.0, and vanishes. After all eight updates, naive sequential float32 reports [8388608, 0], losing 100 percent of the 2.02.0 change.

Now regroup the sum in float32 without changing precision: reduce the eight small increments first. Adding eight copies of 0.250.25 produces gain =2.0= 2.0 with zero rounding error, and adding 8,388,608.0+2.08{,}388{,}608.0 + 2.0 lands on the representable integer 8,388,610.08{,}388{,}610.0, recovering [8388610, 2].

The live model

The demo runs both float32 orders against exact update 2.02.0 (blue) and prints [naive_gain, grouped_gain], coloring preserved bars green and swamped bars crimson. At default base_scale =1.0= 1.0 (base 8,388,608, gap 1.01.0), it prints [0, 2]. At 0.25 (base 2,097,152, gap 0.250.25), both survive: [2, 2]. At 2.0 (base 16,777,216, gap 2.02.0), naive vanishes while grouped survives: [0, 2]. At 4.0 (base 33,554,432, gap 4.04.0), grouped 2.02.0 ties halfway to even 33,554,432 and rounds down: [0, 0].

Three horizontal bars at base 8388608 comparing the exact update sum of 2 against float32 naive sequential gain 0 (crimson) and float32 grouped gain 2 (green).
let base_scale = scene.parameter("base_scale", 1.0, 0.25, 4.0, 0.25);
let f = input.to(DType.F32);
let base = f.slice(0, 0, 1) * torch.full(1, base_scale).to(DType.F32);
let seq = base;
for (i in range(1, f.size(0))) {
  seq = seq + f.slice(0, i, i + 1);
}
let naive_gain = seq - base;
let grp = base + f.slice(0, 1, f.size(0)).sum().reshape(1);
let grouped_gain = grp - base;
let answer = torch.cat(torch.list().add(naive_gain).add(grouped_gain), 0);
out.set("naive_and_grouped_gain", answer);
{
  "naive_and_grouped_gain": [
    0,
    2
  ]
}
Watch the parameter change

Your turn

The starter program returns [8388608, 0] on the default fixture because each 0.25 increment is absorbed before the rest arrive. Set answer to the float32 result [total, total - base] obtained when the small increments are grouped in float32 before adding base in float32; Check tests both cases where grouping recovers [8388610, 2] and larger bases where float32 still rounds differently from float64.

Starter bars showing true update sum 2 versus swamped sequential float32 gain 0 at base 8388608.
let f = input.to(DType.F32);
let base = f.slice(0, 0, 1);
let total = base;
for (i in range(1, f.size(0))) {
  total = total + f.slice(0, i, i + 1);
}
let recovered = total - base;
let answer = torch.cat(torch.list().add(total).add(recovered), 0);
out.set("total_and_gain", answer);
{
  "total_and_gain": [
    8388608,
    0
  ]
}

Compute the float32 result [total, total - base] obtained when the small increments in input[1..] are grouped in float32 before adding input[0] in float32.

Read the result carefully

Two practical rules of large-scale training come directly from this nine-number experiment. First, parallel tensor reductions sum trees of similar-sized values before combining large partial sums, which dramatically reduces swamping compared with a long sequential loop.

Second, when base reaches 225=33,554,4322^{25} = 33{,}554{,}432 (gap 4.04.0) — or equivalently when a float16 weight of magnitude 1.01.0 (gap 2−10=0.00097656252^{-10} = 0.0009765625) receives tiny learning-rate updates of 0.00010.0001 — regrouping updates within one step is no longer enough. That is why mixed-precision training stores a float32 master copy of weights even when matrix multiplications run in 16-bit formats.

Recap

  • Floating-point addition is not associative: (base+u1)+u2(\mathrm{base} + u_1) + u_2 can round away updates that base+(u1+u2)\mathrm{base} + (u_1 + u_2) preserves.
  • At 223=8,388,6082^{23} = 8{,}388{,}608, float32 spacing reaches 1.01.0, swamping individual 0.250.25 increments added sequentially (0) while preserving their grouped sum (2).
  • Tree reductions and higher-precision master accumulators prevent millions of small gradient steps from rounding to zero.