advanced30 min studyLesson 493 in Engineering

Reduction order and compensation solve related but different problems

Compare sequential and balanced sums, inspect a genuine Kahan counterexample, and explain what a Neumaier correction recovers in one controlled case.

On this page

What you’ll understand

  • Relate a balanced reduction tree's depth to a standard worst-case rounding bound.
  • Trace Kahan and Neumaier corrections without claiming either is exact for every order.
  • Evaluate accuracy together with parallel work, compiler behavior, and learning-output effects.

Batch gradients and attention reductions can add many terms of different signs and magnitudes. Their exact sum ignores parentheses, but a floating-point implementation cannot. A balanced tree shortens the chain of dependent rounding steps; compensation retains a small correction that a simple accumulator would discard. Neither method is a universal promise of correctly rounded summation. We will inspect their mechanisms and one counterexample instead of choosing an algorithm solely from its reputation.

Depth changes a bound, not every realized answer

A sequential sum repeatedly adds the next term into one accumulator. A balanced pairwise tree combines partial sums, so a term passes through roughly logarithmic depth rather than a chain proportional to the number of terms. Under the standard local-error model with no exceptional arithmetic, that shorter depth improves a common worst-case forward bound. But a particular pairing can still lose a small term, and changing data order changes which magnitudes meet. A tighter worst-case bound does not prove every individual input is more accurate under every tree.

The conditioning of the exact sum remains relevant. When large positive and negative contributions nearly cancel, a small absolute error can be large relative to the final sum. Compare absolute error and a meaningful scale before dividing by a near-zero reference. A wider accumulator reduces local rounding but does not undo products already rounded too coarsely. In a dot product, input conversion, multiplication, accumulation, and output casting each have their own precision. Name which stage a proposed reduction change actually modifies.

∣s^−s∣≤γd∑i∣xi∣,γd=du1−du(du<1)|\widehat{s}-s|\le\gamma_d\sum_i|x_i|,\qquad \gamma_d=\frac{du}{1-du}\quad(du<1)
For a sum of represented inputs under a suitable standard rounding model, d can bound the addition depth along a term's path: n−1 for a sequential chain or ceil(log2 n) for a balanced tree. No overflow or invalid relative-model behavior is assumed. This is a worst-case absolute bound, not an exact error prediction.↔ Scroll wide equations horizontally.

A correction can itself be rounded away

One Kahan variant subtracts a stored correction from the next input, adds that adjusted input to the sum, then estimates the lost low-order part through the difference between the new and old sums. This often greatly improves a long accumulation. Yet the adjusted input and correction calculations are still finite-precision operations, and an unfavorable magnitude change can discard the very correction intended to help. Compiler reassociation can also invalidate the carefully ordered expressions. Preserve the algorithm's arithmetic contract rather than algebraically simplifying the correction to zero.

Neumaier's variant adds a correction using a magnitude comparison between the running sum and the next term, then incorporates the accumulated correction in the final answer. That branch can recover a lost contribution in cases where the ordinary Kahan recurrence fails, including the controlled example below. It is still not an all-input exact sum, especially when correction accumulation or range assumptions fail. For stronger guarantees, consider documented wider, expansion-based, or exact accumulators with their stated cost and domain. Validate the actual implementation instead of assuming its function name proves the desired accuracy.

yi=fl⁡(xi−c),t=fl⁡(s+yi),c=fl⁡(fl⁡(t−s)−yi),s=ty_i=\operatorname{fl}(x_i-c),\quad t=\operatorname{fl}(s+y_i),\quad c=\operatorname{fl}(\operatorname{fl}(t-s)-y_i),\quad s=t
This explicitly ordered Kahan recurrence uses one finite-precision format for the displayed operations and starts with s=c=0. The sign convention matters. It often reduces error but does not imply exactness for all signs, magnitudes, orders, or compiler transformations.↔ Scroll wide equations horizontally.

Compare useful results, not just counted floating-point work

Test realistic gradient distributions together with adversarial mixtures, permutations, and scale changes. Verify the same represented inputs and define whether products are fused with addition. Record reduction topology, accumulator precision, correction precision, compiler settings, throughput, and memory. A compensated kernel adds arithmetic and dependencies; a balanced kernel can fit parallel hardware better. The best choice depends on the accuracy required by the later computation, not merely the smallest scalar error on one convenient fixture or the highest nominal operation count.

The roofline lab isolates a synthetic work-versus-memory ceiling, not reduction accuracy. Reset, set both intensity I=2 FLOP/byte and memory bandwidth B=100 GB/s, predict 200 GFLOP/s, then Play or Step. Reset and re-enter I=8 and B=100 to predict 800 GFLOP/s. The fixed compute roof is 1,000 GFLOP/s and playback only traces it. Extra compensation operations can raise counted intensity without increasing useful sums per second, so this diagram cannot choose a reduction algorithm or certify Kahan, Neumaier, or pairwise error.

An exact half-precision trace distinguishes two corrections

Three represented batch-gradient contributions are 8,192, one, and −8,192. Round every operation in the stated Kahan recurrence to binary16, ties to even. Compare a Neumaier correction under the same finite range.

  1. The exact sum is one. After the first term, Kahan has s=8,192 and c=0. Adding one rounds back to 8,192, while the correction becomes −1.

  2. For the final term, the adjusted value −8,192−(−1)=−8,191 rounds to −8,192 in binary16. The final sum is zero and the recomputed correction is zero, so this Kahan order loses the answer.

  3. Neumaier retains the lost one as a separate correction when 8,192+1 rounds back to 8,192. Adding −8,192 then cancels the main sum exactly, leaving correction one and final result one. This is a verified case, not an all-order exactness guarantee.

The result

Kahan returns zero and Neumaier returns one for this specified half-precision order. Different recurrences and pairings must be evaluated with their actual rounding semantics.

Explore the model

Interactive lab

Interactive lab · roofline

Accuracy work has a cost model, not a free speedup

Reset, set both I=2 FLOP/byte and memory B=100 decimal GB/s, predict 200 GFLOP/s, then Play or Step. Reset and re-enter I=8 and B=100 to predict 800. The fixed compute ceiling is 1,000 and playback traces it; extra counted operations are not extra useful reductions. This model does not sum inputs or simulate rounding.

Accuracy work has a cost model, not a free speedupSynthetic roofline ceiling min(1000, B·I), in GFLOP/s, with B in decimal GB/s and I in FLOP/byte. Playback traces the roof without changing the selected kernel. This upper-bound model is not a measured throughput prediction, hardware comparison or price model. Current readings: Attainable ceiling: 640 GFLOP/s; Ridge intensity: 12.5 FLOP/byte; Ceiling regime: Memory-bound ceiling. All parameters are adjustable with the labeled controls below.Hypothetical roofline upper bound: 640 GFLOP per second at operational intensity 8 FLOP per byte and bandwidth 80 gigabytes per second.0.1110641101001000Operational intensity · FLOP/byte · log scaleCeiling · GFLOP/s · log scalePeak: 1000
Attainable ceiling / kernelBandwidth roof / traceCompute ceiling
Attainable ceiling
640 GFLOP/s
Ridge intensity
12.5 FLOP/byte
Ceiling regime
Memory-bound ceiling
t = 0 s

Synthetic roofline ceiling min(1000, B·I), in GFLOP/s, with B in decimal GB/s and I in FLOP/byte. Playback traces the roof without changing the selected kernel. This upper-bound model is not a measured throughput prediction, hardware comparison or price model. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forAccuracy, useful reduction throughput, and operation-count ceilings need separate evidence.

Check your understanding

What does the controlled Kahan counterexample establish about compensated summation?

Try it yourself

Under binary16 nearest rounding, compare the parenthesizations (8,192+1)+(−8,192) and (8,192−8,192)+1 for represented inputs. What does this show and what does it not prove about pairwise reduction?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Round each addition at its own step and distinguish one favorable pairing from a universal algorithm guarantee.

Compare with the solution

The first rounds 8,192+1 to 8,192 and returns zero. The second cancels the large terms to zero before adding one, returning one. This shows that pairing matters for these inputs; it does not prove that every balanced tree or every reordered dataset is exact or always more accurate.

Take it with you

  • A balanced tree reduces addition depth under a standard worst-case model.
  • Kahan and Neumaier preserve corrections through different finite-precision recurrences.
  • Verify actual rounding, conditioning, compiler behavior, and useful throughput rather than assuming universal exactness.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.