L1 Beginner · Module 2: Why GPUs: hardware first principles

2.3Memory bandwidth and the memory hierarchy

A GPU's memory is a ladder, from tiny, fast registers down to large, slow HBM and slower host memory. Fast kernels keep data on the top rungs and reuse it. LLM decode can't, because it has to stream every weight from the bottom of the ladder on every step.

Runs on a CPU or in the browser✦ Interactive~20 min

By the end you will be able to

  • ✓Name the levels of the GPU memory hierarchy with their rough capacity, latency and bandwidth
  • ✓Distinguish bandwidth from latency, and say which one limits a streaming workload
  • ✓Explain why moving data costs more energy than computing on it
  • ✓Measure your own machine's memory bandwidth and compare it with HBM

Memory can be big, fast or cheap, and you can only pick two. Every computer resolves this the same way: a hierarchy of small fast memories close to the arithmetic, backed by larger slower ones further away.

Two numbers per level

  • Latency: how long one request takes to come back. Measured in cycles or nanoseconds.
  • Bandwidth: how many bytes per second arrive once many requests are in flight. Measured in GB/s or TB/s.

They are different things. A motorway can have a long trip time and still move many cars per hour. For streaming a large weight matrix, bandwidth decides the time, as long as enough requests are in flight to cover the latency (2.5).

The ladder

Data

Registers

inside each SM

capacity

256 KB × 132 SMs

latency

~1 cycle

read it once

doesn't fit

Shared mem / L1

inside each SM

capacity

256 KB × 132 SMs

latency

~30 cycles

read it once

doesn't fit

L2 cache

on the GPU die

capacity

50 MB, shared

latency

~250 cycles

read it once

doesn't fit

HBM3

stacked next to the die

capacity

80 GB

latency

~600 cycles

read it once

4.78 ms

Host DRAM

over PCIe Gen5

capacity

up to TBs

latency

~3000 cycles

read it once

250.00 ms

16.0 GB fits in HBM3. Streaming it from there takes 4.78 ms, during which the GPU could do 4.72 TFLOP of BF16 math.

H100 SXM at 1.98 GHz. Latencies and on-chip bandwidths are approximate microbenchmark figures.

fig. 2.3The H100 memory hierarchy. Pick a working set, or drag the slider, to see which level can hold it and how long one full read takes from there. Turn on human scale to feel the latencies.

From the top:

  • Registers are private to each thread and readable every cycle. Each SM has 256 KB of them, which sounds like a lot until you divide it among 2,048 threads.
  • Shared memory / L1 is a scratchpad (up to 228 KB per SM) that the threads of one block share. Kernels load a tile from HBM into shared memory once and then reuse it many times (4.4, 6.1).
  • L2 is a 50 MB cache shared by all SMs. It catches reuse across blocks.
  • HBM is the GPU’s main memory: 80 GB at 3.35 TB/s. Weights and the KV cache live here.
  • Host DRAM sits across the PCIe bus at about 64 GB/s, 50 times slower than HBM. Engines use it to offload KV cache that doesn’t fit, and pay for it in latency.

Moving data costs more than computing

Every byte moved also costs energy. Measurements on a 45 nm process (Horowitz, ISSCC 2014) put a 32-bit floating-point multiply at about 4 pJ and a 32-bit read from off-chip DRAM at about 640 pJ, over a hundred times more. The ratio has stayed large on newer processes. Arithmetic is cheap; getting operands to the arithmetic is the expensive part, both in time and in power.

Lab: measure your own bandwidth 🟢

The lab copies arrays of growing size and reports GB/s. Small arrays stay in your CPU’s caches; large ones come from DRAM.

labs/m02/2.3/bandwidth.py
for _ in range(reps):
np.copyto(dst, src) # read src, write dst
gbps = 2 * n_bytes / best_time / 1e9
Terminal window
python labs/m02/2.3/bandwidth.py
output (one laptop, single thread)
working set GB/s
16 KiB 42.8 ##########
128 KiB 45.0 ###########
1 MiB 25.4 ######
8 MiB 30.4 #######
32 MiB 23.9 #####
256 MiB 23.7 #####
1024 MiB 23.7 #####
DRAM bandwidth here: ~24 GB/s. An H100's HBM: ~3,350 GB/s (~142x).
Time to read 16 GB of 8B-model weights once: this machine ~0.68 s, H100 ~4.8 ms.

Your numbers will differ. The shape is what matters: a step down once the working set no longer fits in cache, and a DRAM plateau far below HBM. That plateau is why running an 8B model on a laptop CPU gives about one or two tokens per second.

Sources

repo · BGEBEFORE-GPU-ENGINEERINGBEFORE-GPU-ENGINEERINGPhase 1: memory bandwidth, the hierarchy, and why data movement dominates.
book · FREGLYAI Systems Performance EngineeringChris FreglyGPU architecture chapters: registers, shared memory, L2 and HBM on Hopper.

Check your understanding

0 / 4 answered
  1. 1.Which number decides how long it takes to stream a 16 GB weight file, once enough loads are in flight?

  2. 2.Where do an LLM's weights and KV cache live during serving?

  3. 3.Roughly how does the energy of a 32-bit DRAM read compare with a 32-bit multiply?

  4. 4.What makes a kernel efficient with respect to the memory hierarchy?