Place a matmul on the roofline before calling it fast
Compute arithmetic intensity from a shape and compare it with one measured point, without pretending the estimate is a hardware limit.
A roofline argument starts with a ratio, not a stopwatch. For a matrix
multiplication, the operation count is 2 * m * n * k and the bytes moved are
the three matrices in float32: (m*k + k*n + m*n) * 4. Their ratio is the
arithmetic intensity in FLOPs per byte. A machine has a compute ceiling and a
bandwidth ceiling; the smaller of the two at that intensity bounds the attainable
rate.
This example computes the ratio for (64, 512, 4096) and measures one NumPy
matmul of that shape. The measured 105.8 GFLOP/s is a point on this machine, not
the machine’s peak and not a prediction for a GPU. The value of the exercise is
that the arithmetic is checkable before any timing is trusted.
Run it
From the canonical repository at commit
f958967d70992d7b09d9c75000bd8c543f1644d4:
python examples/ch11/roofline.py --shapes 64,512,4096 --jsonPython 3.11 or later with NumPy. CPU-only, no model, network, GPU or credentials. It finishes in well under a second.
Implementation
estimate_matmul counts FLOPs and bytes from the shape, assuming float32 and one
read of each input plus one write of the output. benchmark_matmul warms up once
and takes the minimum of five timings. The arithmetic-intensity field is computed
from the estimate, not from the timing.
def estimate_matmul(m: int, n: int, k: int, dtype_bytes: int = 4) -> tuple[int, int]:
flops = 2 * m * n * k
# Read A (m*k), B (k*n), write C (m*n).
bytes_moved = (m * k + k * n + m * n) * dtype_bytes
return flops, bytes_moved
def benchmark_matmul(m: int, n: int, k: int, iterations: int = 5) -> float:
a = np.random.randn(m, k).astype("float32")
b = np.random.randn(k, n).astype("float32")
# Warmup.
_ = a @ b
times = []
for _ in range(iterations):
start = time.perf_counter()
_ = a @ b
times.append(time.perf_counter() - start)
return min(times)
def run(shapes: List[List[int]]) -> List[OperatorEstimate]:
results = []
for m, n, k in shapes:
flops, bytes_moved = estimate_matmul(m, n, k)
seconds = benchmark_matmul(m, n, k)
gflops = flops / (seconds * 1e9) if seconds > 0 else 0.0
ai = flops / bytes_moved if bytes_moved > 0 else 0.0
results.append(
OperatorEstimate(
name="matmul",
m=m,
n=n,
k=k,
flops=flops,
bytes_moved=bytes_moved,
arithmetic_intensity=ai,
observed_seconds=seconds,
observed_gflops=gflops,
metadata={"iterations": 5},
)
)
return resultsExpected outcome
Observed with the command above (exit 0). Selected stable fields:
"m": 64,
"n": 512,
"k": 4096,
"flops": 268435456,
"bytes_moved": 9568256,
"arithmetic_intensity": 28.054794520547944,The estimate is 2 * 64 * 512 * 4096 = 268,435,456 FLOPs and
(64*4096 + 4096*512 + 64*512) * 4 = 9,568,256 bytes, so the intensity is
28.05 FLOPs/byte. In this run the NumPy call took about 0.00254 s and
reported about 105.8 GFLOP/s. That measured rate is one point, not a roof.
Mechanism and limitations
Documented: estimate_matmul uses the shape and a dtype width; it does not
read hardware counters. benchmark_matmul uses float32 NumPy arrays, one warm-up
and the minimum of five iterations. The tests check that both estimates are
positive, that arithmetic intensity is positive, that a timing is non-negative,
and that the result serialises.
Observed: the command exited 0 with the FLOPs, bytes and intensity above.
The measured GFLOP/s is from this machine and this single run. The targeted
tests.test_ch11_roofline is part of the canonical suite; no independent test
count is claimed here.
Not shown: the machine’s actual peak FLOPs or bandwidth; a GPU roofline; cache reuse between the three matrix reads; memory traffic from the framework; or whether the operation is compute- or bandwidth-bound at this size. The byte model is deliberately simple and can undercount real traffic. A point below a roof does not prove the roof; a point above it would prove the model wrong.
Challenge this result
Run --shapes 512,512,512 and compute the intensity by hand before looking at
the output. Then run --shapes 1,1,4096 and explain why the arithmetic intensity
is tiny while the measured rate may still look respectable. Finally, state what
additional measurement would be needed to locate the compute and bandwidth
ceilings of this machine, rather than merely placing one point between them.
Source and verification
Canonical source:
examples/ch11/roofline.py
at commit f958967d70992d7b09d9c75000bd8c543f1644d4. Tests:
tests/test_ch11_roofline.py
.
Chapter: Memory Bandwidth and Compute Bottlenecks
.
The run above is a fresh local execution on Windows 11 with Python 3.13.15 and
NumPy; it is not a hardware peak measurement.
Understand this example
Copy this prompt into your AI tool. No code runs here.
Apply this example
Copy this prompt into your AI tool. No code runs here.