Search

Search papers and lessons

IO Complexity

Level 4 · 10 min read

IO complexity counts how many values an algorithm moves between slow and fast memory, which often predicts real speed better than counting operations.

On this page

  • Colour = topic mix
  • Algorithms
  • Discrete math
  • Systems
  • Sign in to see your progress

TL;DR

Ordinary complexity counts operations. On a modern accelerator that count is often the wrong number, because the time goes to moving data between a small fast memory and a large slow one. IO complexity counts the moves instead: how many words cross the boundary between fast and slow memory, as a function of the input size nn and the fast-memory size MM. Two algorithms with the same operation count can differ by a large factor in IO, and that factor often decides which one is faster.

Where you'll see it: in the analysis of tiled matrix multiplication, in external sorting of files too big for RAM, and in why FlashAttention runs faster than the textbook attention formula even though both do the same arithmetic.

Intuition

Walking to the pantry, counted

GPU Memory Hierarchy introduced the cook who chops quickly but walks slowly to the pantry. IO complexity is a bill for the walks. Ignore the knife work entirely and count trips between the pantry and the counter, where the counter holds at most MM items.

Two programs that do the same arithmetic can have very different trip counts. A program that fetches the same ingredient again every time it needs it walks back and forth constantly. A program that finishes all its work on a batch while the batch is on the counter walks far less. Big-O on operations cannot see this difference, because both programs run the same number of steps. Big-O Notation still describes how each count grows; it just counts the wrong thing when memory is the bottleneck.

This is why the idea exists. Once the processor is much faster than memory, the walk is the cost. An analysis that ignores memory predicts the wrong winner, and the mistake is not small: it can be a factor equal to the size of the counter.

Why the counter size matters

The counter has size MM, so there is a hard limit on how much can be kept close at once. If the whole problem fits on the counter, you walk once in and once out. If it does not, you must decide what to keep and what to put back. Most IO bounds have the form "operations divided by some power of MM". That says a bigger counter reduces the walking, but only by a fraction. Doubling MM does not halve the walking for every problem; for matrix multiplication it cuts the walking by about a factor of 2\sqrt{2}.

The model is deliberately crude. It has two levels and no detail about registers, caches or bandwidth. Its value is that it is simple enough to prove bounds with, and the bounds it gives are the ones that show up when measured on real hardware.

The math

The model

Definition: External memory model

A computer has a fast memory that holds at most MM words and an unlimited slow memory. An algorithm may compute only on values currently in fast memory. Its cost is the number of words it transfers between the two memories, its IO count, written Q(n,M)Q(n, M) when the input has size nn.

Here a word is one number, such as a 4-byte float. The model ignores arithmetic time and counts only transfers. Some authors group words into blocks of size BB and count block transfers instead. The results below are stated for words; the block version divides the counts by BB.

Two conventions keep the bookkeeping honest. A value is read from slow memory when it is first needed, and it is written back only when it must leave fast memory. And the analysis reports growth, as in Big-O Notation: Q(n,M)=Θ(g(n,M))Q(n, M) = \Theta(g(n, M)) means the count is pinned to gg up to constant factors.

Two standard results anchor the field. Reading nn values in one pass costs Θ(n)\Theta(n) IOs, and that cannot be avoided. Sorting nn values when MM holds only a fraction of them costs

Θ ⁣(nB log⁡M/BnB)\Theta\!\left(\frac{n}{B}\,\log_{M/B}\frac{n}{B}\right)

block transfers, the bound Aggarwal and Vitter proved in 1988. The logarithm has base M/BM/B, which is why external sorts merge many runs at once. The wider the merge, the fewer passes over the data, because each pass divides the number of runs by the fan-in rather than by two.

Matrix multiplication

For two n×nn \times n matrices with n2>Mn^2 \gt M, the Hong and Kung lower bound says any algorithm must perform

Q(n,M)=Ω ⁣(n3M)Q(n, M) = \Omega\!\left(\frac{n^3}{\sqrt{M}}\right)

IOs, and tiling achieves this up to constant factors. The intuition behind the bound is a counting argument. A word held in fast memory can take part in only about M\sqrt{M} multiply-adds before it has to be replaced, so the n3n^3 products force on the order of n3/Mn^3/\sqrt{M} transfers.

Tiling picks blocks of side bb with 3b2≤M3b^2 \le M, so that three blocks fit in fast memory at once. Load a block of the first input matrix and a block of the second, multiply them into a block of the output matrix that stays in fast memory, and repeat over the blocks. Choosing b≈M/3b \approx \sqrt{M/3} gives Q=Θ(n3/b)=Θ(n3/M)Q = \Theta(n^3/b) = \Theta(n^3/\sqrt{M}).

Attention

Standard attention for sequence length NN and head size dd takes query, key and value matrices Q,K,V\mathbf{Q}, \mathbf{K}, \mathbf{V}, each of size N×dN \times d, and forms the score matrix S=QK⊤S = \mathbf{Q}\mathbf{K}^\top, which has N×NN \times N entries. It then applies a softmax to each row and multiplies by VV. If each step writes its result to slow memory and the next step reads it back, the IO count is dominated by the score matrix:

Qstd=Θ(Nd+N2).Q_{\text{std}} = \Theta(Nd + N^2).

Dao et al. (2022) showed a tiled algorithm that computes the softmax one block at a time, keeping running row statistics, and never writes the full N×NN \times N matrix. Its IO count is

Qflash=Θ ⁣(N2d2M).Q_{\text{flash}} = \Theta\!\left(\frac{N^2 d^2}{M}\right).

Which count is smaller depends on MM relative to d2d^2. When fast memory is large compared with d2d^2, the tiled version does fewer transfers. The number of multiply-adds is the same in both. Only the traffic changes.

2040608010000.20.40.60.81fast memory size M (relative to a baseline of 1)IO count relative to naive
  • tiled IO / naive IO
For matrix multiplication the tiled IO count falls as 1/sqrt(M). A hundred times more fast memory cuts the transfers by a factor of ten.

Worked examples

Example
Tiled versus naive matrix multiplication

Take n=1000n = 1000 and a fast memory of M=10,000M = 10{,}000 words, so M=100\sqrt{M} = 100.

  1. A naive triple loop with no reuse costs on the order of one transfer per multiply-add, so about n3=109n^3 = 10^9 IOs.
  2. The tiled count is n3/M=109/100=107n^3 / \sqrt{M} = 10^9 / 100 = 10^7 IOs.
  3. The ratio is M=100\sqrt{M} = 100. The tiled version does one hundredth of the transfers.
  4. In a model where each transfer costs 1 nanosecond, the naive version takes 10910^9 ns, about 1 second, and the tiled version takes 10710^7 ns, about 0.01 seconds.

Interpretation: both versions perform the same 10910^9 multiply-adds. Only the reuse pattern changed, and in this model that is worth a factor of 100.

Example
FlashAttention on a sequence of 4096 tokens

Let N=4096N = 4096, d=64d = 64 and M=100,000M = 100{,}000 words. Ignore constant factors, as the model does.

  1. Standard attention: N2=16,777,216N^2 = 16{,}777{,}216 and Nd=262,144Nd = 262{,}144. The sum is Qstd≈1.704×107Q_{\text{std}} \approx 1.704 \times 10^7.
  2. FlashAttention numerator: N2d2=16,777,216×4096≈6.872×1010N^2 d^2 = 16{,}777{,}216 \times 4096 \approx 6.872 \times 10^{10}.
  3. Divide by MM: 6.872×1010/105≈6.87×1056.872 \times 10^{10} / 10^5 \approx 6.87 \times 10^5.
  4. Ratio: 1.704×107/6.87×105≈24.81.704 \times 10^7 / 6.87 \times 10^5 \approx 24.8.

Interpretation: under this model the tiled algorithm moves about 25 times fewer values. Because constants are dropped, read 25 as an order of magnitude, not as a predicted wall-clock speedup. The paper's measured gains come from the constants and from fusing the kernel, which this model does not capture.

Tradeoffs and failure modes

IO analysis is the right tool when data movement dominates. It is the wrong tool when the working set fits in cache or when arithmetic dominates.

SituationWhat goes wrongBetter choice
Kernel is memory-bound, analysed only by operation countPredicted speed is never reachedCount IOs next to operations
Large intermediate is materialised (for example an N×NN \times N score matrix)Writes and reads of it dominateFuse steps and tile, or recompute
Tile size chosen without reference to MMTiles spill out of fast memory and the kernel slows silentlyDerive the tile size from MM explicitly
Problem fits in fast memoryThe model overstates the costMeasure; the two-level model no longer applies

Real hardware has several levels, such as registers, shared memory, L2 cache and high-bandwidth memory, each with its own size and bandwidth. The two-level model is therefore a first approximation. Cache-oblivious algorithms take a different route: they are designed to be efficient for every level at once, without being told MM, and they are a standard way to get IO bounds that hold across machines.

History

Hong and Kung introduced the red-blue pebble game in 1981, a formal model of two-level memory, and used it to prove the lower bound for matrix multiplication. Their question was how much a machine's memory limits its speed on a given problem. Aggarwal and Vitter turned this into the external memory model in 1988 and analysed sorting and related problems in it, which made the model practical for databases and file systems.

The idea stayed mostly in theory tools until memory speed fell far behind arithmetic speed through the 1990s and 2000s. Wulf and McKee named this problem the memory wall in 1995. The GPU version became widely used after the FlashAttention paper in 2022, which showed that a careful IO analysis could explain a large speedup in an operation that every large language model relies on. The idea caught on because it turned a vague intuition about memory into a bound that could be proved and then checked.

Check yourself

QuestionRecall: what does MM measure in the external memory model?Show answer

The number of words the fast memory can hold at once. The algorithm may compute only on values currently in fast memory, and the IO count is the number of words moved across the boundary between the two memories.

QuestionCompute: with n=2000n = 2000 and M=40,000M = 40{,}000, how many IOs does the tiled bound n3/Mn^3/\sqrt{M} give, ignoring constants?Show answer

40,000=200\sqrt{40{,}000} = 200 and n3=8×109n^3 = 8 \times 10^9. So 8×109/200=4×1078 \times 10^9 / 200 = 4 \times 10^7 IOs.

QuestionWhy: a sort that merges 2 runs at a time and one that merges 64 runs at a time both do about nlog⁡nn \log n comparisons. Why does the 64-way merge usually do fewer IOs?Show answer

The IO bound has logarithm base M/BM/B, the fan-in of the merge. A wider merge means fewer passes over the data. With 64 inputs the logarithm is smaller than with 2 inputs, so fewer passes read and write the whole file. The comparison count does not change, which is why counting only operations misses the difference.

QuestionApplied: a kernel performs 101210^{12} operations and moves 2×10102 \times 10^{10} bytes. The hardware peaks at 101410^{14} operations per second and 101210^{12} bytes per second. Is the kernel memory-bound, and what is the fastest it can run?Show answer

The intensity is I=1012/(2×1010)=50I = 10^{12} / (2 \times 10^{10}) = 50 operations per byte. The ridge point is P/W=1014/1012=100P/W = 10^{14} / 10^{12} = 100. Since 50<10050 \lt 100, the kernel is memory-bound. Its attainable speed is min⁡(P,W⋅I)=min⁡(1014,5×1013)=5×1013\min(P, W \cdot I) = \min(10^{14}, 5 \times 10^{13}) = 5 \times 10^{13} operations per second, half the peak. To go faster, it must move fewer bytes, not compute faster.

Sources