Counting FLOPs of matrix multiplies
Compute that an (m×k)(k×n) matmul costs 2mkn FLOPs. Count the bytes it must read and write, and estimate the runtime on a GPU with a given peak FLOP rate.
Builds on: Compute, memory and scale, Weights, parameters and layers
What the animation says
How long should it take?
One layer multiplies a 4096 by 4096 block of activations by a 4096 by 4096 weight matrix. Your GPU's spec sheet promises ten to the fifteen floating-point operations per second. So how long should this one multiply take? To answer, we need two counts: how much arithmetic, and how many bytes move to and from memory. Make a guess: about one microsecond, a hundred microseconds, or ten milliseconds?
One dot product per entry
It's closer to a hundred microseconds. Here's where that comes from. Shrink the problem: a two by three matrix times a three by two. Each output entry is one row dotted with one column: three multiplies, and three adds into a running sum. Six operations. So every entry costs two times k, where k is the shared inner length. The output has m times n entries, here four, so the total is two times m times k times n. Twenty-four. Your turn: tap the row and the column that build the highlighted entry.
Scale up: compute vs memory
Back to 4096. Two times 4096 cubed is about one point four times ten to the eleven operations. Divide by ten to the fifteen per second: about 137 microseconds. Now memory. Read both inputs once, write the output once, at two bytes per number: about a hundred megabytes. At three terabytes per second, that's about 34 microseconds. Arithmetic is the bottleneck. Now multiply just one vector, m equals one, by the same weights. Which side wins?
Skinny multiplies wait on memory
Memory wins, and by a lot. The arithmetic drops to two times 4096 squared: about three hundredths of a microsecond. But all sixteen million weights still have to be read: about thirty-four megabytes, eleven microseconds. That's roughly three hundred times longer than the math. The reason: arithmetic grows with three dimensions multiplied together, data only with two. Big square multiplies are limited by arithmetic; skinny ones by memory.
Your cost estimator
Here's the whole method as one small function. This line counts operations: two times m times k times n. This line counts bytes: both inputs plus the output, times bytes per number. Then divide each by its speed. The larger time is your floor, and real code reaches only part of peak. So for any multiply: two m k n operations, divided by the speed your GPU actually achieves.
One dot product per output entry
The three formulas
Square matmul vs one vector
A reusable cost estimator
def matmul_cost(m, k, n, peak=1e15, mem=3e12, b=2):
flops = 2 * m * k * n
nbytes = b * (m * k + k * n + m * n)
return flops, nbytes, flops / peak, nbytes / mem
for m in (4096, 1):
f, nb, tc, tm = matmul_cost(m, 4096, 4096)
print(f"m={m}: {f:.2e} FLOPs, {nb/1e6:.0f} MB, "
f"compute {tc*1e6:.2f} us, memory {tm*1e6:.2f} us")
Remember 2mkn
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses