Seyed Masoud Hosseini · Overview · Study log · Weekly summaries · Ideas · Search · Transcript · RSS feed
Language Modeling from Scratch · Lecture 5 of 17 · 1:14:21
Lecture 5: GPUs
Study guide
What this lecture covers
The lecture demystifies GPU hardware: why matrix multiplies get mysteriously slow at certain sizes, and what tricks (precision, fusion, recomputation, memory coalescing, tiling) make GPU code fast. It builds up from the GPU's physical layout and execution model to a full explanation of why Flash Attention works.
This is the systems-foundations lecture that assignment two builds on, since students implement parts of Flash Attention 2 in Triton. By the end you should be able to read a GPU throughput-vs-matrix-size plot and explain its wavy pattern, and describe how tiling and online softmax let Flash Attention avoid materializing the full attention matrix.
Key ideas
- Compute has scaled far faster than memory: GPU matrix-multiply throughput has grown up to 100,000x while memory bandwidth has grown only about 100x, making memory movement, not flops, the usual bottleneck.
- GPUs optimize for throughput, CPUs for latency: a GPU has many small compute units (SMs, each with streaming processors) and little control logic, running the same instruction across many threads (SIMT) rather than finishing any one task fast.
- Memory hierarchy: registers and shared memory inside an SM are fastest, L2 cache is slower, and global memory (DRAM, off-chip) is slowest, roughly 10x slower per hop.
- Lower precision doubles effective memory bandwidth: moving from FP32 to FP16 halves the bytes moved per flop, though careful mixed-precision design (e.g. FP32 accumulation) is needed for stability.
- Kernel fusion: chaining operations within the compute unit instead of writing intermediate results back to global memory avoids redundant slow memory round-trips.
- Recomputation: recomputing intermediate activations during the backward pass, instead of storing and reloading them, trades cheap compute for scarce memory bandwidth.
- Memory coalescing and burst mode: DRAM returns a whole aligned chunk ("burst section") per access, so threads accessing contiguous, aligned memory get much higher effective throughput than threads accessing scattered addresses.
- Tiling: splitting matrices into blocks small enough to fit in fast shared memory, then doing many computations per load, cuts the number of slow global memory reads by roughly the tile size.
Walkthrough
Why hardware matters for scaling (0:05)
The lecture opens with the goal of making CUDA and GPUs less mysterious, previewing a matrix-multiply throughput chart with unpredictable wavy patterns that will be explained by the end. It frames hardware understanding as essential because compute scaling, not just better deep learning ideas, has driven most of the field's progress, referencing the end of single-thread (Dennard) scaling and the shift to parallel scaling.
GPU anatomy and execution model (6:08)
The lecture contrasts CPU design (large control units, few threads, latency-optimized) with GPU design (many streaming multiprocessors, or SMs, each containing streaming processors that execute threads in parallel, throughput-optimized). It introduces the three granularities of GPU execution: blocks (assigned to an SM), warps (groups of 32 threads executing together), and threads, plus the memory hierarchy from registers up through shared memory, L2 cache, and global memory. TPUs are briefly compared, noting their similar structure (tensor cores, scalar/vector units, a dedicated matrix-multiply unit) but simpler, matmul-only design.
Why matrix multiplies dominate, and the memory-compute gap (20:18)
The lecture shows that specialized tensor cores have made matrix multiplies orders of magnitude faster than other GPU operations, meaning any efficient neural architecture must be dominated by matmuls. It then establishes the core theme: GPU compute has scaled far faster than memory bandwidth, so memory movement is increasingly the bottleneck as hardware gets newer.
The roofline model and non-memory bottlenecks (25:24)
Using the earlier throughput-vs-matrix-size chart as a running puzzle, the lecture introduces the roofline model: performance is memory-bound at small problem sizes and compute-bound at large ones. It first covers a non-memory pitfall, warp divergence from conditional branches, where threads in a warp taking different code paths must execute serially instead of in parallel.
Core memory optimization tricks (29:24)
The lecture works through four techniques in sequence: lower precision (fewer bytes moved per flop), operator fusion (keeping a chain of operations in the compute unit instead of round-tripping to global memory), recomputation (recomputing activations in the backward pass instead of storing and reloading them), and memory coalescing (exploiting DRAM's burst-mode reads by having threads access contiguous, aligned memory).
Tiling and why matrix-multiply performance looks so erratic (46:32)
Tiling is introduced as the way to minimize global memory reads in matrix multiplication: load a submatrix ("tile") into shared memory once, then reuse it for many computations. The lecture then explains the earlier mystery chart: tile-alignment issues (matrix sizes not divisible by clean powers of two) hurt coalescing, and "wave quantization" (tile counts that slightly exceed a multiple of the GPU's SM count) leave some SMs idle on a final, mostly-wasted pass, producing the sharp performance cliffs observed.
Putting it together: Flash Attention (1:04:42)
The lecture closes by showing how tiling and recomputation combine to produce Flash Attention. The QKV matrix multiplies are handled with standard tiling, but softmax is a global, row-wise operation that would normally require materializing the full attention matrix. The online softmax algorithm solves this by incrementally tracking a running maximum and normalizer as tiles are processed, so the exact softmax can be computed tile-by-tile without ever storing the full quadratic-sized matrix. The backward pass similarly avoids storing activations by recomputing them tile-by-tile.
Before you watch
- Review basic attention mechanics (Q, K, V matrix multiplies and softmax), since the lecture assumes you already know how to implement standard attention.
- Recall backpropagation and the idea of storing activations during the forward pass for use in the backward pass.
- Some familiarity with mixed-precision training will help with the precision-tradeoff discussion.
Check your understanding
- Why is memory bandwidth, rather than compute, typically the bottleneck on modern GPUs?
- What is memory coalescing, and why does traversal order (row-major vs. column-major) affect it?
- How does tiling reduce the number of global memory reads needed for a matrix multiply?
- Why can't a naive softmax be computed tile-by-tile without an online algorithm?
- What causes the sharp performance drops ("wave quantization") seen when matrix dimensions slightly exceed a multiple of the tile size times the SM count?
Vocabulary
- demystify (verb)
- To make something confusing easier to understand.
The lecture aims to demystify how GPUs actually work. - throughput-optimized (adjective)
- Designed to complete as much total work as possible, even if any single task is slower.
GPUs are throughput-optimized, unlike latency-optimized CPUs. - streaming multiprocessor (noun)
- A core compute unit inside a GPU that runs many threads together.
A GPU contains many streaming multiprocessors working in parallel. - memory hierarchy (noun)
- The layered structure of memory types from fastest and smallest to slowest and largest.
Registers sit at the top of the GPU's memory hierarchy. - bandwidth (noun)
- The rate at which data can move between memory and the processor.
Memory bandwidth has grown much slower than compute power. - bottleneck (noun)
- The part of a system that limits its overall speed.
Memory movement is usually the bottleneck, not computation. - fusion (noun)
- Combining several operations into one so data doesn't need to travel back and forth.
Kernel fusion avoids unnecessary trips to slow memory. - recomputation (noun)
- Redoing a calculation instead of storing its result, to save memory.
Recomputation trades extra compute for less memory use. - coalesce (verb)
- To combine separate memory accesses into one efficient block.
Memory coalescing groups nearby accesses for faster reads. - tile (noun)
- A small block of a larger matrix processed together to fit fast memory.
Tiling loads a small tile into shared memory before computing. - roofline model (noun)
- A way to visualize whether a program's speed is limited by compute or by memory.
The roofline model shows when performance is memory-bound. - warp (noun)
- A group of threads on a GPU that execute the same instruction together.
A warp runs 32 threads together in lockstep. - divergence (noun)
- When threads that should run together instead take different paths, slowing execution.
Warp divergence happens when threads follow different branches. - materialize (verb)
- To fully compute and store a large intermediate result.
Flash Attention avoids having to materialize the full attention matrix. - anatomy (noun)
- The internal structure and parts of something.
The lecture covers the anatomy of a GPU chip. - granularity (noun)
- The level of detail or size at which something is divided.
Threads, warps, and blocks are three granularities of GPU execution. - precision (noun)
- The number of exact digits a number format can represent.
Lower precision moves fewer bytes per computed value. - online algorithm (noun)
- A method that processes data incrementally as it arrives, without needing it all at once.
Online softmax computes the result tile by tile as data streams in. - quantization (noun)
- The process of representing values with fewer bits or coarser detail.
Wave quantization causes GPUs to sit idle on a final, wasted pass. - demystifying (adjective)
- Making something confusing become clear and understandable.
The lecture takes a demystifying approach to CUDA.
From the YouTube description
For more information about Stanford's online Artificial Intelligence programs visit: https://stanford.io/ai
To learn more about enrolling in this course visit: https://online.stanford.edu/courses/cs336-language-modeling-scratch
To follow along with the course schedule and syllabus visit: https://stanford-cs336.github.io/spring2025/
Percy Liang
Associate Professor of Computer Science
Director of Center for Research on Foundation Models (CRFM)
Tatsunori Hashimoto
Assistant Professor of Computer Science
View the entire course playlist: https://www.youtube.com/playlist?list=PLoROMvodv4rOY23Y0BoGoBGgQ1zmU_MT_
← Lecture 4: Mixture of Experts · Lecture 6: Kernels, Triton →
