Compare the accuracy of two implementations#

The problem#

You rewrote a kernel and want to know whether the new version is more or less accurate than the old one, or you are choosing between two summation methods. Comparing both against a float64 result measures their distance from another rounded number, not from the true answer.

Why the usual approach falls short#

Both implementations may be correct, so a pass or fail check says nothing about which is better. A float64 reference has its own rounding error, which blurs small differences, and an error in absolute terms is hard to compare across magnitudes.

The solution#

Measure each implementation's error exactly, against r.exact, an interval ExactRef guarantees to contain the exact answer, in units in the last place (ulps) of the output format. Here a plain sequential float32 loop and NumPy's np.sum, which adds in a tree, both add the same 100,000 values:

Code
import math
import random
from fractions import Fraction
import numpy as np
import exactref as er

# Seeded, so every run uses the same values
rng = random.Random(67)
# 100,000 random float32 values between 0 and 1
values = np.array([rng.uniform(0, 1) for _ in range(100000)], dtype=np.float32)

# Implementation 1: add one value at a time, rounding to float32 each time
sequential = np.float32(0)
for v in values:
    sequential = np.float32(sequential + v)

# Implementation 2: NumPy's sum, which adds in a tree
pairwise = np.sum(values)

# ExactRef's result for the same sum; r.rounded is the exact sum rounded once
# to float32
r = er.sum(values)
# The exact sum as a Fraction. r.exact gives an interval known to hold the
# exact value (an enclosure). This sum's has radius 0, so its midpoint is the
# exact value.
exact = r.exact.value().midpoint.to_fraction()

# How far an output is from the exact sum, in float32 ulps at the answer
def error_in_ulps(output):
    # frexp writes the answer as m * 2**exponent, with 0.5 <= m < 1
    exponent = math.frexp(r.rounded.tolist())[1]
    # float32 keeps 24 significant bits, so one ulp there is 2**(exponent - 24)
    ulp = Fraction(2) ** (exponent - 24)
    # Fraction(float(output)) is the output's exact value, so no rounding here
    return float(abs(Fraction(float(output)) - exact) / ulp)

# Each implementation's error, to 2 decimals
print("sequential:", round(error_in_ulps(sequential), 2))
print("pairwise:", round(error_in_ulps(pairwise), 2))
Output
sequential: 74.78
pairwise: 0.78

The tree sum is almost as accurate as float32 allows; the loop is about 75 ulps off. Both are correct, and each uses only part of the tolerance, the most a correct output can differ from r.rounded:

Code
# The most a correct float32 sum of these values can differ from r.rounded
tolerance = r.tolerance().tolist()
print("tolerance:", tolerance)

# How much of the tolerance each output uses: its distance from r.rounded, as
# a share of the tolerance
for name, output in (("sequential", sequential), ("pairwise", pairwise)):
    share = abs(float(output) - r.rounded.tolist()) / tolerance
    print(f"{name} uses {share:.2%} of the tolerance")

# Could a correct implementation produce each output? covers() answers per
# element, and er.asarray turns NumPy's result into an ExactRef array.
print("sequential covered:", r.covers(er.asarray(sequential)).all())
print("pairwise covered:", r.covers(er.asarray(pairwise)).all())
Output
tolerance: 150.08984375
sequential uses 0.20% of the tolerance
pairwise uses 0.00% of the tolerance
sequential covered: True
pairwise covered: True

Both use only a sliver of it. The tolerance is the worst case over every correct implementation, in every order of additions that normal accumulation allows: the orders ordinary kernels add in, index order or a few SIMD lanes side by side. A typical implementation stays far below that worst case. So compare implementations by their exact errors, as above, not by how much of the tolerance they leave unused.

To compare methods before writing any code, use ExactRef's algorithms. Each one describes a fixed way of computing an operation, such as adding in a balanced tree in float32, and its error_model() gives the worst case from the shape and format alone:

Code
# The module that holds the algorithms. A sum is a dot product with a vector
# of ones, so take B = 1 in their bounds.
products = er.linalg.products

# Products added one at a time in float32, each rounded before it is added.
# error_model(k) bounds the error of one output element built from k products.
sequential_algorithm = products.matmul_sequential("float32", fma=False)
print("sequential:", sequential_algorithm.error_model(100000))

# The same products added in a balanced tree
pairwise_algorithm = products.matmul_pairwise("float32", fma=False)
print("pairwise:", pairwise_algorithm.error_model(100000))
Output
sequential: 0.006 * sum over k of |A[i,k] * B[k,j]| + 7.05e-41
pairwise: 1.07e-06 * sum over k of |A[i,k] * B[k,j]| + 1.4e-40