Subscribe
AI
6 min read

What Is GEMM? The Matrix Multiply That Eats Almost All AI Compute

Every AI bill, GPU spec sheet and serving trick traces back to one 40-year-old kernel. What GEMM is, and why matrix multiplication, not FLOPs, decides what inference costs.

What Is GEMM? The Matrix Multiply That Eats Almost All AI Compute

OpenAI spent last week claiming a result about the slowest possible way to multiply matrices: a batch of papers in its math repository asserts that the matrix multiplication exponent, the number theorists call omega, is exactly 9/4. The news desk covered the claim and its verification status separately. This explainer is about the other half of the story: why one 40-year-old kernel called GEMM sits under every AI bill, every GPU spec sheet, and most of the product decisions you read about on this site.

One line of algebra, one ancient name

GEMM stands for general matrix multiply. It comes from BLAS, the Basic Linear Algebra Subprograms, a Fortran routine specification first published in 1979 by Charles Lawson, Richard Hanson, David Kincaid and Fred Krogh (ACM TOMS 5(3), 1979, documentation at netlib). The original 1979 set only covered vector operations. The matrix-matrix level, Level 3, arrived in 1990 in a paper by Jack Dongarra, Jenny Du Croz, Iain Duff and Sven Hammarling (ACM TOMS 16(1), 1990). It defines the operation as:

C = alpha * A * B + beta * C

Two matrices multiplied, a scalar scaling the product, a second matrix scaled and added in. The prefixes encode precision: dgemm is double, sgemm single, hgemm half, and gemv is the matrix-times-vector case. A single well-tuned implementation of this routine moves more floating-point operations per second than the rest of scientific computing combined. It is the workload supercomputers, cloud pricing pages and every AI accelerator are measured on.

A transformer is a GEMM sandwich

Read the layer anatomy of any modern model. Each attention block runs a projection of the hidden state into query, key and value matrices, all of which are GEMMs, followed by two more multiplications per head (scores, then the weighted sum of values), then an output projection GEMM. Each feed-forward block adds two or three more GEMMs, depending on whether the gate is a plain ReLU or a SwiGLU pair. The parts that are not GEMMs, embedding lookups, layer normalization, softmax, activation functions, sampling, are memory traffic with almost no arithmetic attached.

The standard FLOP accounting follows from this. A forward pass costs roughly 2 FLOPs per parameter per token: one multiply and one add for every weight, every token. Training adds the backward passes, giving the familiar estimate of about 6 * N * D FLOPs for a model with N parameters on D tokens, the convention used in Epoch AI's compute methodology and in Chinchilla. For a 70B model that means about 140 GFLOPs to generate each token, and a Chinchilla-style 1.8-trillion-token pretraining run lands near 7.6e23 FLOPs, all of it delivered as GEMMs stacked end to end.

Two grids of tiles joined through a gear motif into a result grid

The roofline: when multiplying stops being the problem

In 2009, Samuel Williams, Andrew Waterman and David Patterson published the Roofline model (Berkeley tech report). Its rule is a minimum of two ceilings: a program runs either as fast as the chip computes or as fast as memory feeds it, whichever is slower. The dial that decides which ceiling binds is arithmetic intensity, FLOPs performed per byte of memory traffic.

Do the arithmetic on the numbers from NVIDIA's H100 datasheet: 989 BF16 tensor-core TFLOPS dense and 3.35 TB/s of HBM3 bandwidth. The crossover sits at roughly 295 FLOPs per byte. Now look at what the two phases of inference actually move.

Generating one token for one request at a time is a matrix-vector product: every weight in the model gets read once and used for exactly two FLOPs, an arithmetic intensity near 1 FLOP per byte. The H100 spends about 0.14 ms worth of compute per token for a 70B model and about 42 ms moving the 140 GB of weights through memory, roughly 24 tokens per second. The processor sits idle more than 99 percent of the time. This is not a bug in the serving stack; it is geometry on the roofline chart.

Batch 300 requests through the same weights and each loaded weight now does 600 FLOPs, crossing the ridge. The same GPU is suddenly compute-bound, and its per-token cost collapses. Between those two points sits essentially the entire AI serving industry: continuous batching, chunked prefill, prefill/decode disaggregation, and prompt caching are all mechanisms for keeping GEMMs closer to the compute roof, a trade-off Google's inference paper on PaLM (Pope et al., 2022) made explicit when it introduced the model FLOPs utilization metric and reported 76 percent MFU on batched prefill against 29 ms low-batch latency during generation.

A storage block feeding a thin stream of tiles through a pipe into a large machine tower

The ratio explains the product roadmap

Once you see the compute-to-bandwidth ratio as the master variable, a lot of headlines reframe themselves. Quantization to int8 or int4 weights cuts the bytes each token must move, raising the tokens-per-second ceiling directly, which is one of the levers the PaLM serving paper uses. Grouped-query attention shrinks the KV cache that decode-time traffic must re-read, and that paper shows the resulting memory savings enabling 32x longer contexts. FlashAttention is an argument that even the attention block is really an IO problem, fusing operations to avoid materializing large intermediate matrices in HBM. Mixture-of-experts changes which weights load per token at all. Memory bandwidth, not FLOPs, is the number to watch on every accelerator announcement, and it is why the price of HBM and the price of AI inference move together.

The hardware was rebuilt around this one kernel

NVIDIA's tensor cores exist because general CUDA cores were not efficient enough at tiled GEMM. Mixed Precision Training (Micikevicius et al., 2018) made low-precision matmuls respectable in training, and the H100's Transformer Engine extends the idea to FP8, with the datasheet quoting up to 30x inference speedups over the A100 generation for large language models. Under the API, cuBLAS and the open-source CUTLASS template library exist almost entirely to schedule this operation well, and kernel authoring, the reason GPU programmers argue about tiling strategies, is mostly GEMM tuning.

Why the theory still matters

The classical algorithm costs n^3 for two n-by-n matrices. In 1969 Volker Strassen showed seven multiplications suffice where the block algorithm needs eight, giving n^2.807 (Numerische Mathematik 13, 1969), and complexity theorists spent the next fifty years grinding the exponent, known as omega, down toward 2, with best-known upper bounds stuck around 2.37 for years. OpenAI's October batch claimed the upper bound of 9/4 and independent groups quickly re-proved it; the November claim of a matching lower bound, if it survives verification, closes the chase. Practically, none of it changes your token bill this decade. Production stacks run the classical algorithm because blocked GEMM has better numerical stability and far less data movement per operation, and asymptotically fast schemes hide enormous constants (background on Strassen's practical limits). Theory tells you the floor. Engineering tells you the floor is 42 ms of memory traffic away.

What to take from this

When a provider quotes tokens per second, an H100 rental price, or a new 4-bit checkpoint, translate it back: how many FLOPs per byte is the workload actually getting, and who is paying for the bytes. GEMM is the factory floor. Everything else in AI economics is logistics.

Sources

AILLMeducational