JA EN
LearnNumerical Computing
·★ MEMBER·8 min read

The Cost of Matrix Multiplication — Where Almost All of AI's Compute Goes

Why GEMM is everything: the anatomy of O(n³), memory bandwidth and arithmetic intensity, what actually makes a GPU fast, and the intuition behind tiling — ending with you able to estimate a model's training and inference FLOPs yourself.

ModalitytextTasknumerical

The metaphor: the machine and the trucks

A factory installs a ferociously fast machine tool: it can cut a thousand parts per second. The trucks delivering raw stock, however, arrive with ten parts per second. This factory's output is set by the number of trucks, not by the machine. The machine stands idle 99% of the time.

Modern computers are that factory. Arithmetic units are ferociously fast; the bandwidth that carries data out of memory has not kept pace. Counting how many operations you perform therefore does not predict runtime. You have to count how many bytes you move at the same time.

And in AI, the overwhelming majority of that time goes into a single operation: matrix multiplication.

Why matrix multiplication is everything

Take a Transformer apart and what falls out is almost entirely matrix products: the projections to Q, K and V, the attention scores, the weighted sum over values, the output projection, and the two linear layers of the FFN. Convolutions, too, are typically lowered to a matrix product via im2col or an equivalent rearrangement.

That is why the library world gave this one operation a dedicated name — GEMM, general matrix multiply — and has been optimizing it for decades. Strip the marketing off "making AI faster" and a large fraction of it is "making GEMM faster".

The anatomy of O(n3)O(n^3)

Multiply an m×km \times k matrix AA by a k×nk \times n matrix BB to get an m×nm \times n matrix CC. One output cell is defined as

Cij=p=1kAipBpjC_{ij} = \sum_{p=1}^{k} A_{ip} B_{pj}
(1)

Read in words: "row ii of AA against column jj of BB, multiplied elementwise and summed" — a single dot product. There are mnmn cells, each costing kk multiplications and roughly kk additions. Counting a multiply and an add as one operation each,

FLOPs=2mkn\text{FLOPs} = 2\,m\,k\,n

which says nothing more exotic than: cells in the output (mnmn), times multiply-adds needed to fill one cell (kk), times two operations per multiply-add.

That is 2n32n^3 for square matrices. Double nn and the work grows eightfold. That is the content of O(n3)O(n^3), and it is also what people mean by a model's "compute".

Numbers give it scale. A square product at n=4096n = 4096 costs about 1.4×10111.4 \times 10^{11} FLOPs — over a hundred billion operations for one multiplication. A Transformer stacks dozens of layers of those and repeats them per token.

It is worth feeling, with your hands, that the atom of all this is a dot product.

FIG 1A dot product collapses one row and one column into a single number. GEMM repeats it m×n times, each of length k

That covers the operation count. But runtime is not determined by 2n32n^3 alone — the trucks from the opening are about to matter.

Arithmetic intensity: how much work per byte delivered

Here is the factory, written as a formula. Define the arithmetic intensity:

I=FLOPs performedbytes moved from memoryI = \frac{\text{FLOPs performed}}{\text{bytes moved from memory}}
(2)

Read in words: the numerator is how much work you did, the denominator is how many bytes you paid to move in order to do it. Think of II as fuel economy for data. It is the ratio of work done to data delivered — how many times you managed to reuse each byte you paid to fetch. An operation with low II stalls on bandwidth no matter how fast the arithmetic units are (memory bound). With high II you get to use the units at full tilt (compute bound). Achievable performance is capped by

performancemin(peak compute,  I×memory bandwidth)\text{performance} \le \min\big(\text{peak compute},\ \ I \times \text{memory bandwidth}\big)

which says your real speed is pinned by whichever is lower: what the arithmetic units are capable of, or what the bandwidth can keep them fed with. The first term is the machine's ceiling, the second is "bytes per second times work per byte". Plotting those two lines gives you the roofline model.

Run the numbers for GEMM. For square matrices you move three of them, elements, which in fp32 is bytes. The work is , so

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

Comments

Sign in to comment