Summary
The opening lecture of the systems portion of the course. It builds a hardware mental model of the GPU, contrasts it with the TPU, then works through the specific tricks that determine whether code saturates the hardware. The organizing question is a plot of matmul throughput against matrix dimension, which has jagged and non-monotonic structure; the lecture is built so that every feature of that plot is explained by the end. It closes by reconstructing FlashAttention from the pieces introduced along the way.
Key Takeaways
- Dennard scaling ended in the 2000s, so performance now comes from horizontal parallelism rather than clock speed. A CPU optimizes latency with large control units and few ALUs; a GPU optimizes throughput with hundreds of lightweight cores, and individual tasks may finish slowly while aggregate throughput is high.
- The memory hierarchy is the thing to respect: registers and L1/shared memory sit physically on the SM and return in roughly 20-30 cycles, while global memory (HBM) is about an order of magnitude slower. SRAM is not used for everything because it is far more expensive per byte and must be continuously powered.
- The execution model is thread (SIMT, all threads in a warp execute the same instruction), block (guaranteed to run on one SM, which is what makes shared memory usable), and warp (32 threads, the scheduling unit). Because threads are swapped in and out at no cost, stalls on memory can be hidden by other warps.
- GPUs and TPUs are convergent evolution: every GPU concept maps onto a TPU concept, and a GPU tensor core and a TPU MXU are both systolic arrays. The difference is granularity — many small matmul units on a GPU versus a few very large ones on a TPU, which is why TPUs refuse inputs below a minimum tile dimension. Confusingly, “tensor core” means the matmul unit on a GPU and the whole processor on a TPU.
- Since the V100 added tensor cores, matmul has been the one privileged operation, running more than 10x faster than any other floating-point operation — which is why any architecture that scales with compute has a matmul at its core.
- Compute throughput is growing faster than memory bandwidth, which in turn grows faster than interconnect bandwidth. That widening gap is why nearly every optimization discussed is a memory optimization, and why the roofline model’s memory-bound diagonal is where badly written code lands.
- Six tricks: avoid control divergence (both branches of an if execute, with the inactive threads masked off, so ReLU is written as a multiply by a mask); lower precision; operator fusion; recomputation; memory coalescing; and tiling.
- Low precision has become exotic. FP8 has no single format (E4M3 versus E5M2), and MXFP8 attaches a separate E8M0 scale factor to every 32 elements — which breaks transposition, so implementations keep two quantized copies of every matrix. Real speedups are roughly 20-30%, not 2x, because quantization itself costs something, and the last layer is notoriously hard to quantize.
- DRAM reads arrive in bursts of roughly 128 bytes, so threads reading down the minor axis of a row-major matrix waste most of each burst; this is why alignment and padding matter, and why Karpathy’s nanoGPT sped up 25% by padding the vocab from 50257 to 50304. Tiling is the highest-impact idea: loading a T×T tile into shared memory cuts global reads by a factor of T. Tile sizing interacts with SM count — at 1792 a matmul produces 98 tiles which fit in the A100’s 108 SMs, while 1793 produces 120 tiles and forces a second, mostly idle wave (wave quantization).
- FlashAttention is exactly these pieces combined: tiled matmuls, an online softmax that carries a running maximum and rescales when a larger value appears, fusion of the whole sequence into one kernel, and recomputation of the N² activations on the backward pass instead of storing them.
Extracted Notes
- None yet.