Math toolkit
FLOP counting for matrix multiplies
A FLOP is one floating-point operation. Counting the FLOPs of a matrix multiply, using 2mnk, lets you estimate the cost of a layer, a model or a training run before you run it.
Compute costs time and money, and most of it is matrix multiplies. A FLOP (floating-point operation) is one add or one multiply. Count the FLOPs of each matrix multiply and you get a quick estimate of how long a layer or a training run will take. The formula is short enough to memorize.
The big idea
Multiplying an (m, k) matrix by a (k, n) matrix produces m · n outputs. Each output is a dot product of length k, which needs k multiplies and about k adds. Counting each multiply-add as 2 operations gives:
FLOPs = 2 · m · n · kThe shared dimension k is the one that gets summed away. It appears once in the count.
Walking through it
Multiply (1024 × 4096) by (4096 × 1024). Here m = 1024, k = 4096, n = 1024:
FLOPs = 2 · 1024 · 4096 · 1024
= 2 · 2¹⁰ · 2¹² · 2¹⁰
= 2³³
≈ 8.6 × 10⁹ (about 8.6 GFLOPs)Now a transformer-sized case. A linear layer maps width 4096 to 16384 (four times wider), applied to 2048 tokens. The token count plays the role of m:
m = 2048, k = 4096, n = 16384
FLOPs = 2 · 2048 · 4096 · 16384 = 2³⁸ ≈ 2.75 × 10¹¹Turn FLOPs into time by dividing by the throughput of your hardware. On a hypothetical accelerator that sustains 100 TFLOP/s on this matmul, the layer takes 2.75 × 10¹¹ / 10¹⁴ ≈ 2.75 ms.
Dense matrix multiplies dominate transformer compute. That is why accelerators are built around them, and why training cost estimates count FLOPs. The roofline model adds memory traffic to this picture.
Common mistakes
- Mixing conventions. Some papers count a multiply-add as one operation, others as two. A factor of 2 can change your estimate. Check which one a source uses.
- Using the wrong
m,nork.kis the shared inner dimension. For a batch of tokens,mis the number of tokens. - Stopping at FLOPs. Small batches can be limited by moving memory, not by arithmetic. FLOP counts alone do not predict speed.
Check yourself
- How many FLOPs are in
(100 × 200) · (200 × 50)? Answer: 2 · 100 · 200 · 50 = 2,000,000, or 2 MFLOPs. - If you double the batch size, what happens to the FLOPs of a linear layer? Answer: They double, because
mdoubles. The weights do not change.
Key takeaways
- Count a matrix multiply as
2mnkFLOPs. - The inner dimension
kis summed away, andm · nis the number of outputs. - Divide FLOPs by hardware throughput for a first time estimate.
- FLOPs ignore memory traffic, which can dominate at small batch sizes.
Further learning
Next, see why the starting scale of weights matters for these layers: orthogonality and initialization.
Related
Linear algebra for ML
Vectors, matrices and matrix multiplication, the language every model is written in. Learn the shapes, then batching, FLOP counts and low-rank tricks.
Batching and broadcasting
Stacking examples into a matrix turns many small multiplies into one large one, which is what GPUs are built for. Broadcasting adds biases and masks, and it fails silently when shapes line up by accident.
The roofline model and arithmetic intensity
Predicting performance from FLOPs and bytes: compute-bound vs memory-bound. Start here for the map, then work the examples.