Search

Search papers and lessons

GPU Memory Hierarchy

Level 2 · 10 min read

A GPU has very fast arithmetic units but slow access to its large memory, so a program runs at the speed of its memory traffic unless it reuses data on-chip.

On this page

TL;DR

A GPU can do an enormous number of arithmetic operations per second, but it gets its numbers from memory, and most of that memory is far away and slow compared with the arithmetic units. A program is fast only if it moves each value from slow memory a small number of times and does a lot of work on it while it sits in fast memory nearby.

Where you'll see it: in why matrix multiplication is fast on a GPU, in why attention kernels such as FlashAttention are written around tiles, and in why a model can be limited by memory bandwidth even when its arithmetic is cheap.

Intuition

A kitchen with a far-away pantry

Think of a cook who can chop vegetables faster than anyone. The bottleneck is not the knife. It is the walk to the pantry. If the cook fetches one carrot at a time, the walking dominates the day. If the cook fetches a whole tray, brings it to the counter, and does everything that needs carrots before going back, the same knife work finishes much sooner.

A GPU has the same shape. The arithmetic units, which do the multiplications and additions, are packed into many small processors that run in parallel. The data lives in a large memory chip next to the processor, usually called high-bandwidth memory (HBM). Reading from HBM takes a long time compared with computing on a value that is already in a register. Between the two sits a small, fast, on-chip memory, which is the counter in the analogy. The part of it that a programmer controls directly is called shared memory: a scratchpad that the threads of one block of work can all read and write.

The design is a hierarchy because the cost of memory rises as the size of the memory grows. Small memories close to the processor are fast and expensive per byte. Large memories far away are slow and cheap per byte. A good program is one that keeps its working set in the small, fast levels for as long as it can.

flowchart TB
  A["Registers (per thread, tiny, fastest)"] --> B["Shared memory and L1 (on-chip, small, fast)"]
  B --> C["L2 cache (on-chip, larger, slower)"]
  C --> D["HBM (off-chip, large, much slower)"]

Note: the exact sizes and speeds change from one GPU model to the next. The shape of the hierarchy does not.

Why the numbers matter

Suppose a processor can do ten trillion operations per second but its memory can deliver only half a trillion bytes per second. A program that uses one byte for every operation can run at no more than half a trillion operations per second. The processor sits idle most of the time. A program that uses the same byte a hundred times, while it is still on-chip, can use the arithmetic units fully. The difference is not in the algorithm's arithmetic count. It is in how often the algorithm goes back to the pantry.

The math

Arithmetic intensity

The key ratio is the number of arithmetic operations a program performs per byte it moves to or from slow memory:

I=floating-point operationsbytes moved from slow memory.I = \frac{\text{floating-point operations}}{\text{bytes moved from slow memory}}.

Call II the arithmetic intensity. Its unit is operations per byte. If the hardware can do PP operations per second and move BB bytes per second, then the program's speed is limited by two ceilings. The first is the compute ceiling, PP. The second is the memory ceiling, B⋅IB \cdot I, because each byte supports II operations. The attainable speed is

attainable=min⁡(P, B⋅I).\text{attainable} = \min(P,\ B \cdot I).

The crossover, where the two ceilings meet, is I∗=P/BI^\ast = P / B. Below I∗I^\ast the program is memory-bound, and above it the program is compute-bound.

0510152025303540024681012arithmetic intensity (operations per byte)speed (trillion operations per second)
  • attainable speed
  • memory limit
Hypothetical machine: peak 10 trillion operations per second and 0.5 trillion bytes per second. The ridge is at intensity 20, where the two lines meet.

The numbers in the plot are made up for illustration. Real GPUs have their own peak and bandwidth values, and the same two-line picture applies to all of them.

What counts as a byte moved

The formula depends on what you count. If a value is read from HBM once, used many times from registers or shared memory, and then written back once, you count only the one read and the one write. If the same value is read from HBM every time it is needed, each read counts. Almost all of the difference between a slow GPU kernel and a fast one is in this count.

Worked examples

Example
Adding two vectors

Let ci=ai+bic_i = a_i + b_i for n=109n = 10^9 values, where each value is a 4-byte float. Use the hypothetical machine from the plot: peak 10 trillion operations per second and bandwidth 0.5 trillion bytes per second.

  1. Each element needs one addition, so there is 1 operation per element.
  2. Each element reads aia_i and bib_i and writes cic_i. That is three values of 4 bytes, so 12 bytes per element.
  3. The intensity is I=1/12I = 1 / 12, about 0.083 operations per byte.
  4. This is far below the ridge at 20, so the program is memory-bound. The attainable speed is min⁡(10, 0.5⋅112)=0.042\min(10,\ 0.5 \cdot \tfrac{1}{12}) = 0.042 trillion operations per second.
  5. Total bytes: 12×109=1.2×101012 \times 10^9 = 1.2 \times 10^{10}. At 0.5 trillion bytes per second, that takes 1.2×1010/5×1011=0.0241.2 \times 10^{10} / 5 \times 10^{11} = 0.024 seconds.
  6. The arithmetic alone would take 109/1013=0.000110^9 / 10^{13} = 0.0001 seconds. The memory traffic is 240 times slower than the addition.

Interpretation: a vector addition is limited by memory no matter how fast the arithmetic is. Making the processor faster does not help. Only fewer bytes or more reuse would.

Example
Multiplying two 4096 by 4096 matrices

Let N=4096N = 4096 and each entry a 4-byte float. A matrix product does 2N32N^3 operations, which is 1.374×10111.374 \times 10^{11}. Here is the same product under two data-movement strategies.

  1. Naive strategy: every operand is fetched from HBM each time it is used. Each output entry is a dot product of length NN, so it reads 2N2N values. There are N2N^2 output entries, so the total is 2N32N^3 values, or 8N3=5.5×10118N^3 = 5.5 \times 10^{11} bytes.
  2. The naive intensity is 1.374×1011/5.5×1011=0.251.374 \times 10^{11} / 5.5 \times 10^{11} = 0.25 operations per byte. That is well below the ridge, so the program is memory-bound.
  3. Time at the memory limit: 5.5×1011/5×1011≈1.15.5 \times 10^{11} / 5 \times 10^{11} \approx 1.1 seconds.
  4. Tiled strategy: load a small square block of AA and of BB into shared memory, multiply those blocks completely, and accumulate into a block of CC kept on-chip. Each block is reused by many output entries before it is evicted. In the ideal case each matrix is read once and CC is written once, which is 3N2×4=2.01×1083N^2 \times 4 = 2.01 \times 10^8 bytes.
  5. The ideal intensity is 1.374×1011/2.01×108≈6831.374 \times 10^{11} / 2.01 \times 10^8 \approx 683 operations per byte. That is far above the ridge at 20, so the program is compute-bound.
  6. Time at the compute limit: 1.374×1011/1013≈0.01371.374 \times 10^{11} / 10^{13} \approx 0.0137 seconds. The tiled version is about 80 times faster on this machine, with the same arithmetic.

Interpretation: nothing about the mathematics changed. Only the reuse pattern changed, and that moved the program from memory-bound to compute-bound. This is why Matrix Multiplication libraries on GPUs are built around tiles.

Tradeoffs and failure modes

Fast on-chip memory is small. A tile that is too large will not fit, and the kernel has to spill to slow memory, or the number of threads running at once falls and the hardware sits idle. Choosing a tile size is a balance between reuse and capacity.

SituationWhat goes wrongBetter choice
Elementwise operation, such as activation or additionIntensity is low, so the kernel is memory-boundFuse it with neighbouring operations so the data is not written and read again
Large matrix productNaive loops refetch operands from HBMTiled kernels that stage blocks in shared memory
Attention over a long sequenceThe full score matrix is written to HBM and read backA fused, tiled kernel that keeps partial results on-chip, as in [[flashattention-paper
Small inputsLaunch overhead and idle threads dominateBatch the work, or accept that the problem is too small for the GPU

A common mistake is to measure only the arithmetic count. Two kernels can perform the same number of operations and differ by an order of magnitude in time, because one moves far more bytes. A second mistake is to tune for the peak compute number. If the intensity is low, the peak number is never reached, and extra processors will not help.

History

The idea that a fast processor should keep a small, fast copy of frequently used data is older than GPUs. Maurice Wilkes described this kind of buffer, which became the cache, in 1965. Caches then became standard in general-purpose processors, and the hierarchy from registers down to main memory became a basic part of computer architecture.

The external-memory model was made formal by Aggarwal and Vitter in 1988. They counted the number of block transfers between a small memory and a large one, rather than the number of arithmetic steps. That is the same accounting used today when people say a kernel is memory-bound.

The roofline picture of a compute ceiling and a bandwidth ceiling was popularised by Williams, Waterman and Patterson in 2009, as a way to read the performance of a kernel at a glance.

GPUs were first used for general computation with programmable shared memory, which gave programmers direct control over the small on-chip level. In 2022 Dao and collaborators applied the same reasoning to attention. FlashAttention computed the exact attention result with far fewer trips to HBM, and it showed that avoiding memory traffic can be a bigger win than reducing arithmetic.

Check yourself

QuestionA kernel performs 400 operations and moves 40 bytes. What is its arithmetic intensity, and on a machine with ridge point 20, is it memory-bound or compute-bound?Show answer

The intensity is 400/40=10400 / 40 = 10 operations per byte. Since 10<2010 \lt 20, the program is below the ridge, so it is memory-bound. Its speed is limited by the memory ceiling, not by the arithmetic units.

QuestionWhy does a fused kernel that computes two operations in one pass often beat two separate kernels, even though both do the same arithmetic?Show answer

Two separate kernels write the intermediate result to HBM and the second kernel reads it back. Fusing them removes that round trip, so the total number of bytes moved drops while the arithmetic stays the same. Intensity goes up, which can move the program toward compute-bound.

QuestionIn the 4096 by 4096 example, how many bytes does the naive strategy move, and how does that compare with the ideal tiled strategy?Show answer

The naive strategy moves 8N3=5.5×10118N^3 = 5.5 \times 10^{11} bytes. The ideal tiled strategy moves 3N2×4=2.01×1083N^2 \times 4 = 2.01 \times 10^8 bytes. The naive version moves about 2700 times as much data. The arithmetic is identical.

QuestionWhy is a larger tile not always better for a GPU kernel?Show answer

A larger tile gives more reuse per byte loaded, which helps intensity. But the tile must fit in the small on-chip memory. If it does not fit, the kernel spills to HBM and loses the benefit. Also, a large tile can limit how many threads run at once, which leaves processors idle. The best size balances reuse against capacity and occupancy.

QuestionA GPU has peak 15 trillion operations per second and bandwidth 1 trillion bytes per second. What is its ridge point, and what intensity does a kernel need to be compute-bound?Show answer

The ridge point is P/B=15/1=15P / B = 15 / 1 = 15 operations per byte. A kernel needs an intensity of at least 15 operations per byte to reach the compute ceiling. Below that, its speed is capped by bandwidth times intensity.

Sources