All courses

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

▶ Watch this lesson Start the whole course

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.

How long should a matrix multiply take?

A layer multiplies a $4096\times4096$ block of activations by a $4096\times4096$ weight matrix. Your GPU's spec sheet says $10^{15}$ FLOP/s. Roughly how long should this take: closer to 1 µs, 100 µs, or 10 ms? To answer, you need two counts: how much arithmetic the multiply needs, and how many bytes must move to and from memory. Both counts follow directly from the three matrix dimensions.

One dot product per output entry

In $C = AB$ with $A$ being $m\times k$ and $B$ being $k\times n$, each entry $C_{ij}$ is a dot product of a length-$k$ row with a length-$k$ column. That is $k$ multiplies and about $k$ adds, so roughly $2k$ FLOPs. $C$ has $mn$ entries, so the total is $2mkn$. Memory is different. At minimum, each input number is read once and each output is written once, so data moved grows only like the matrix sizes.

The three formulas

For $(m\times k)(k\times n)$ with $b$ bytes per number ($b=2$ here): $$\text{FLOPs} = 2mkn$$ $$\text{bytes} = b\,(mk + kn + mn)$$ Here $mk$ and $kn$ are the inputs read and $mn$ is the output written. $$t_{\text{compute}} \approx \frac{2mkn}{\text{peak FLOP/s}},\quad t_{\text{memory}} \approx \frac{\text{bytes}}{\text{memory speed}}$$ The real runtime is at least the larger of the two. Real code reaches only part of peak.

Square matmul vs one vector

$m=k=n=4096$, peak $10^{15}$ FLOP/s, memory $3\times10^{12}$ B/s. - FLOPs $=2\cdot4096^3\approx1.37\times10^{11}$, giving **137 µs**. - Bytes $=2\cdot3\cdot4096^2\approx101$ MB, giving 34 µs. Arithmetic dominates. Now take $m=1$, a single vector times the same weights: - FLOPs $\approx3.4\times10^7$, giving 0.03 µs. - Bytes $\approx33.6$ MB, giving **11 µs**. Reading the weights dominates by about 300×.

A reusable cost estimator

The same arithmetic as the example, as a function you can reuse for any shape. The printout writes µs as "us".
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

An $(m\times k)(k\times n)$ matmul costs $2mkn$ FLOPs and, with 2-byte numbers, moves about $2(mk+kn+mn)$ bytes. Divide FLOPs by peak FLOP/s and bytes by memory speed. The larger result is your time floor. Arithmetic grows like a product of three dimensions, while data grows only like products of two. So big multiplies are limited by arithmetic, and skinny ones by memory.

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.