Maps to: Stanford CS 217 Hardware Accelerators for Machine Learning (Winter 2026), Lecture 11 · Tue 02/17/2026 · GPU Architectures. Lab 4 (accelerator top level) is due today. Reading: none is listed on the schedule for this session. Background used here:
- E. Lindholm, J. Nickolls, S. Oberman, J. Montrym, NVIDIA Tesla: a unified graphics and computing architecture, IEEE Micro 2008;
- NVIDIA’s Volta, Ampere and Hopper architecture whitepapers;
- the CUDA C++ Programming Guide;
- V. Volkov, Better performance at lower occupancy, GTC 2010.
Basis: The slides are not public. This lecture follows the session title and the public NVIDIA documentation above.
The experiments use a model of an H100-like GPU written for these notes (
gpu.py):
- SIMT divergence and coalescing counters;
- a cycle-level warp scheduler for one SM;
- the CUDA occupancy calculation;
- an energy model of matrix instructions;
- a GEMM tiling cost model of the whole chip.
NVIDIA does not publish several of the numbers the model needs: memory latency, L2 bandwidth, per-SM bandwidth limits and launch overhead. These are assumed, labelled, and varied in the scripts. Times and efficiencies are model outputs, not measurements of an H100. The experiments do not reflect what was said in class. Code:
code/lecture11/(4 scripts + 1 module; under 3 s each) Reading time: 3–4 hours including running the code
0. How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- Describe the GPU’s execution hierarchy (grid → thread blocks → warps → threads, on SMs and their sub-cores) and memory hierarchy (registers, shared memory/L1, L2, HBM), with H100 numbers.
- Explain SIMT execution, and compute the cost of divergence, uncoalesced accesses and shared-memory bank conflicts.
- Explain latency hiding by multithreading, and use Little’s law to compute how much parallelism an SM needs.
- Compute occupancy and its limiter. Explain why occupancy is a means and not a goal.
- Explain how tensor cores multiplied ML throughput within the same programming model, and why matrix instructions amortize instruction and operand overheads.
- Explain why Hopper added asynchronous copies (TMA), warpgroup MMAs from shared memory and thread-block clusters, and estimate a GEMM’s efficiency from its tile, pipeline depth, shape and waves.
- Say what generality costs and buys, and what still limits GPU efficiency on ML.
0.2 Prerequisites and scope
| Assumed | Introduced here | Developed later |
|---|---|---|
| Lecture 1: instruction overhead, roofline, energy per operation | SIMT, warps, divergence, coalescing | reconfigurable dataflow as the opposite design point (L12–13) |
| Lecture 3: data parallelism, SIMD | latency hiding, occupancy | memory-centric designs for decode (L14–15) |
| Lecture 5: systolic arrays, output-stationary accumulation | tensor cores, MMA instructions, wgmma | Blackwell and NVIDIA’s view of AI hardware (L17) |
| Lecture 7: banks, Little’s law, tiling | TMA, clusters, GEMM tiling on a GPU, waves | |
| Lecture 10: kernels, launches, FlashAttention |
0.3 Map
§1 the GPU's organization; SIMT, divergence, coalescing, banks ← code 01 (Parts 1-2)
§2 latency hiding: warps, Little's law, ILP vs TLP ← code 01 (Parts 3-4)
§3 occupancy ← code 02
§4 tensor cores: throughput, formats, why matrix instructions ← code 03
§5 a GEMM on the whole GPU: TMA, clusters, pipelines, waves ← code 04
§6 GPUs as "repurposed and augmented" accelerators
§7 summary §8 pitfalls §9 exercises §10 further reading §11 next lecture
0.4 Notation
| Symbol | Meaning |
|---|---|
| SM | streaming multiprocessor (132 on an H100 SXM) |
| warp | 32 threads executing one instruction stream; warpgroup: 4 warps |
| CTA | cooperative thread array = thread block, the unit scheduled on one SM |
| occupancy | resident warps ÷ maximum warps per SM (64) |
| BM, BN, BK | a CTA’s tile of C (BM × BN) and its K-step |
| S | pipeline stages (tile copies kept in flight + 1) |
| cluster (c_m, c_n) | CTAs that share tiles through distributed shared memory |
| FMA | fused multiply-add = one MAC = 2 FLOPs |
1. The GPU’s organization
1.1 From graphics to general purpose
GPUs began as fixed-function graphics pipelines. In the early 2000s their vertex and pixel stages became programmable shaders. With NVIDIA’s G80 (2006), the separate shader types were replaced by an array of unified programmable processors, and CUDA (2007) exposed those processors to ordinary C programs. Lindholm et al. (IEEE Micro 2008) describe that design.
The architectural bet was throughput over latency:
- run tens of thousands of threads;
- spend the transistors on arithmetic lanes and a large register file, not on branch predictors and big caches;
- hide memory latency by switching between threads instead of avoiding it.
Machine learning arrived on GPUs (AlexNet, 2012) because a dense matrix multiplication is that ideal throughput workload. The GPU was repurposed. From Volta (2017) on it was also augmented: tensor cores for matrices (§4), then new formats, asynchronous copy engines and clusters (§5).
1.2 The hierarchy, with H100 numbers
| Level | Execution | Memory (H100 SXM, whitepaper / datasheet) |
|---|---|---|
| GPU | 132 SMs enabled (144 on the full GH100 die) | 80 GB HBM3 at 3.35 TB/s; 50 MB L2 |
| SM | runs several CTAs; 4 sub-cores (“processing blocks”) | 256 KB register file (64 K × 32-bit), 256 KB L1/shared memory (up to 228 KB shared) |
| sub-core | 1 warp scheduler issuing 1 instruction/clock; 32 FP32 lanes; 1 tensor core; special-function units | a 64 KB slice of the register file |
| warp | 32 threads, one instruction stream, per-lane registers |
The program’s view mirrors this:
- a grid of thread blocks (CTAs) of up to 1,024 threads;
- each CTA runs on one SM and shares that SM’s shared memory;
- the hardware splits each CTA into warps of 32.
Note how much of the chip’s on-chip memory is registers: 132 × 256 KB = 34 MB. That is comparable to the 50 MB L2, and the reason will be clear in §2.
1.3 SIMT and divergence
The 32 lanes of a warp share one instruction stream: single instruction, multiple threads. Unlike SIMD (Lecture 3), the programmer writes scalar code per thread, and the hardware handles control flow. When the lanes of a warp disagree on a branch, the warp executes both paths one after the other, masking off the lanes of the other path.
Script 01, Part 1 (if/else with 10 instructions per side, 1,024 threads):
| Condition | Divergent warps | Lane efficiency |
|---|---|---|
| all threads take the same side | 0 / 32 | 100% |
by warp index ((thread // 32) even) | 0 / 32 | 100% |
by lane (thread even, or lane < 16) | 32 / 32 | 50% |
| random data, p = 0.05 | 27 / 32 | 54% |
| the same data, sorted first | 1 / 32 | 97% |
A rare branch is not cheap. A warp diverges unless all 32 lanes agree, and with p = 0.05 the chance that none of the 32 takes it is 0.95³² = 19%. Loops behave the same way: a warp iterates until its longest thread is done. Random exponential trip counts give 25% lane efficiency, and sorting them first gives 88%.
The fixes are all about making a warp’s threads agree:
- branch on warp-uniform values;
- sort or bin the work;
- move rare cases into a separate kernel.
Since Volta each thread has its own program counter (“independent thread scheduling”). That makes fine-grained synchronization safe, but divergent paths still execute one at a time.
1.4 Coalescing
A warp’s memory instruction is one request for 32 addresses. The hardware fetches every distinct 32-byte sector those addresses touch. Script 01, Part 2:
| Access pattern (4-byte elements) | Sectors | Efficiency |
|---|---|---|
| unit stride | 4 | 100% |
| unit stride, 16 B per lane | 16 | 100% |
| misaligned start | 5 | 80% |
| stride 2 | 8 | 50% |
| stride 32 (a matrix column) | 32 | 12.5% |
| array of 3-float structs, one field | 12 | 33% |
| random gather | 32 | 12.5% |
Data layout is therefore a first-class performance decision. Prefer structure-of-arrays, consecutive threads on consecutive elements, and vector loads. Transpose through shared memory when an algorithm must walk columns.
1.5 Shared-memory banks
Shared memory is a software-managed scratchpad of 32 banks, each 4 bytes wide: Lecture 7’s banked SRAM. Word w lives in bank w mod 32. A warp access takes as many passes as the largest number of distinct words requested from one bank; identical words are broadcast.
| Access to a 32 × 32 float tile | Passes |
|---|---|
| a row | 1 |
| a column, pitch 32 | 32 |
| a column, pitch 33 (padded) | 1 |
| a column, pitch 34 | 2 |
This is Lecture 7’s gcd rule: a column spreads over 32 / gcd(pitch, 32) banks. That is why tiled transposes pad each row by one word, and why the tensor-core layouts of §5 use “swizzled” shared-memory layouts that permute addresses with XOR, the same trick as Lecture 7’s XOR bank mapping.
2. Latency hiding
2.1 The problem and the GPU’s answer
A load from HBM takes several hundred cycles. Our model assumes 600 cycles, the order microbenchmark papers report; NVIDIA does not publish it.
A CPU core fights latency with large caches, prefetchers and out-of-order execution: expensive structures per thread. A GPU instead keeps many warps resident on each SM and lets each scheduler pick, every cycle, a warp whose next instruction is ready. While some warps wait for memory, others compute.
How much parallelism is enough? Little’s law (Lecture 7) answers: data in flight = latency × bandwidth. One SM’s share of the H100’s 3.35 TB/s is 13.9 bytes per cycle at 1.83 GHz, so
Across the GPU that is 1.1 MB in flight. That is why the register file is so large: every resident warp keeps its full state on chip, so switching costs nothing.
The same law holds for arithmetic. Each of the 4 schedulers issues one instruction per cycle, and a dependent FMA can issue 4 cycles after its predecessor. Each scheduler therefore needs 4 independent instruction streams: 16 warps per SM.
2.2 The simulation
gpu.simulate_sm runs W warps on one SM, cycle by cycle:
- 4 schedulers;
- in-order warps;
- an ALU latency of 4;
- loads that complete after 600 cycles, but no faster than the SM’s memory pipe returns bytes.
Script 01, Part 3 (memory utilization / ALU utilization):
| Kernel | 8 warps | 16 | 32 | 64 | Little’s law |
|---|---|---|---|---|---|
| streaming: 1 load of 128 B, 2 ALU | 12% | 24% | 47% | 92% | 65 warps |
| compute: 32 dependent ALU | 51% | 100% | 100% | 100% | 16 warps |
| compute: 32 ALU in 4 chains | 100% | 100% | 100% | 100% | 4 warps |
| mixed: 1 load + 64 dependent ALU | 9% / 15% | 17% / 30% | 32% / 55% | 52% / 90% |
The simulation follows Little’s law closely. The streaming kernel is linear in warps until bandwidth saturates, and even the maximum of 64 warps is just short of the 65 required.
2.3 Parallelism inside a warp
Warps are not the only source of independent work. Script 01, Part 4, at 16 warps:
| Streaming kernel | Bandwidth used | Warps needed (Little) |
|---|---|---|
| 1 load of 128 B per iteration | 24% | 65 |
| 4 independent loads (unrolled) | 85% | 16 |
| 1 load of 512 B (16 B per lane) | 92% | 16 |
Unrolling and vector loads put more bytes in flight per warp. Volkov’s “better performance at lower occupancy” (GTC 2010) made this point: instruction-level parallelism (ILP) substitutes for thread-level parallelism (TLP).
Two more lessons from Part 4:
- Latency sets the requirement. At 300 cycles the streaming kernel needs 33 warps; at 1,000 cycles it needs 108, more than an SM can hold. Per-warp ILP or asynchronous copies (§5) become mandatory.
- The scheduling policy has side effects. Greedy-then-oldest (GTO) issues from the same warp until it stalls, then from the oldest ready warp. At 64 warps it finishes the median warp sooner than loose round-robin: 14.2 k vs 17.6 k cycles. But it starves the youngest warps until 27.3 k, and they then run alone with too little parallelism to hide latency. With a fixed amount of work per warp this “tail” costs more than GTO gains. Real GPUs soften it because new blocks keep arriving as old ones finish.
Hardware link. The GPU spends its area on state: registers for many warps. A CPU spends it on speculation: predictors, reorder buffers, caches. Both pay for latency tolerance. The GPU’s choice is the better one when there is massive parallelism, which ML has.
3. Occupancy
3.1 The calculation
A CTA is resident on an SM only if all its resources fit. Occupancy = resident warps ÷ 64. The number of resident blocks is the minimum of four limits (compute capability 9.0, from the CUDA C++ Programming Guide):
| Resource | H100 SM limit | Blocks it allows |
|---|---|---|
| warps | 64 | 64 // warps per block |
| blocks | 32 | 32 |
| registers | 65,536 × 32-bit, allocated per warp in units of 256 | 65,536 // (warps × ⌈32 × regs / 256⌉ × 256) |
| shared memory | 228 KiB (227 KiB per block), 1 KiB reserved per block | 228 KiB // (smem + 1 KiB) |
Script 02, Part 1 (kernel resources are illustrative):
| Kernel | Threads | Regs | Smem | Blocks | Warps | Occupancy | Limited by |
|---|---|---|---|---|---|---|---|
| element-wise | 256 | 32 | 0 | 8 | 64 | 100% | warps & registers |
| row reduction | 512 | 40 | 4 KiB | 3 | 48 | 75% | registers |
| SIMT tiled GEMM | 256 | 128 | 32 KiB | 2 | 16 | 25% | registers |
| FlashAttention-style | 128 | 200 | 96 KiB | 2 | 8 | 12% | registers & smem |
| Hopper tensor-core GEMM (128 × 256, 4 stages) | 384 | 168 | 192 KiB | 1 | 12 | 19% | registers & smem |
The tensor-core GEMM does not fit on an A100 at all (164 KiB of shared memory per SM).
3.2 The staircase
Occupancy moves in steps (Part 2, figure 02_occupancy.png):
- 32 → 40 registers per thread drops 100% to 75%;
- 64 registers gives 50%, 128 gives 25%;
- 113 KiB of shared memory fits two blocks per SM, but 114 KiB fits only one, so one more KiB halves the occupancy.
- At 512 threads per block, 160 registers per thread need 81,920 registers: the block cannot launch at all.
3.3 Occupancy is a means
Part 3 turns occupancy into performance with the SM model:
| Streaming kernel (256 threads/block) | Regs | Warps | Occupancy | Bytes in flight | Bandwidth |
|---|---|---|---|---|---|
| 1 load of 4 B per thread per iteration | 32 | 64 | 100% | 8,192 | 92% |
| unrolled × 4 | 64 | 32 | 50% | 16,384 | 95% |
| unrolled × 8 | 128 | 16 | 25% | 16,384 | 95% |
| 16-B vector loads, unrolled × 4 | 128 | 16 | 25% | 32,768 | 98% |
A compute-bound kernel with 8 independent FMA chains per warp keeps the ALUs 100% busy at 12% occupancy. The quantity that matters is independent work in flight per SM, bytes or instructions. Warps are one way to get it; registers spent on unrolling are another. The fastest ML kernels (§3.1, §5) run at 12–19% occupancy because they keep their in-flight work in asynchronous copies, not in warps.
4. Tensor cores
4.1 More math per clock, same programming model
Volta (V100, 2017) added tensor cores: units that execute a small matrix multiply-accumulate, D = A·B + C, as one warp-wide instruction. Script 03, Part 1 derives the per-SM rates from the datasheets:
| GPU | SMs | FP32 FLOP/clk/SM | Tensor FLOP/clk/SM (fp16/bf16, dense) | Ratio |
|---|---|---|---|---|
| V100 | 80 | 128 | 1,024 | 8× |
| A100 | 108 | 128 | 2,048 | 16× |
| H100 | 132 | 256 (277 from the datasheet) | 4,096 | 16× |
The datasheet peak divided by (SMs × clock) reproduces the whitepapers’ unit counts, with one inconsistency. H100’s 67 TFLOPS FP32 figure implies a 1.98 GHz clock, while its tensor figure implies 1.83 GHz. Both are printed.
Why did this multiply ML throughput without a new programming model?
- The tensor core is invoked from the same SIMT program. One warp (or, on Hopper, a warpgroup of 4 warps) issues a matrix instruction instead of thousands of FMAs.
- Almost nobody writes those instructions by hand. Libraries (cuBLAS, cuDNN), templates (CUTLASS) and compilers (Triton, TorchInductor in Lecture 10) emit them.
- Frameworks call GEMMs and convolutions, so the speedup arrived through a library update.
4.2 Formats
Script 03, Part 2 (dense peaks in TFLOPS, Lecture 1’s hw_specs.py):
| GPU | year | fp16 | bf16 | tf32 | fp8 | fp4 | Ridge of the narrowest format |
|---|---|---|---|---|---|---|---|
| V100 | 2017 | 125 | – | – | – | – | 139 FLOP/B |
| A100 | 2020 | 312 | 312 | 156 | – | – | 306 (int8) |
| H100 | 2022 | 989 | 989 | 495 | 1,979 | – | 591 |
| B200 | 2024 | 2,250 | 2,250 | 1,100 | 4,500 | 9,000 | 1,125 |
Each generation added a narrower format (Lecture 4) and roughly doubled the tensor peak. Memory bandwidth grew more slowly, so the ridge point keeps rising. More and more kernels are memory-bound: Lecture 1’s memory wall, measured across GPU generations.
4.3 Why a matrix instruction is cheap: the energy argument
Lecture 1 showed that a general-purpose instruction costs ~70 pJ of overhead around a ~1 pJ add (Horowitz, 45 nm). A matrix instruction attacks two overheads:
- control (fetch, decode, schedule), amortized over the instruction’s FMAs;
- operand delivery through the register file.
Script 03, Part 3 applies Lecture 1’s 45 nm energies. The register-file bank size (16 KiB) is an assumption. Read the shares, not the absolute picojoules:
| Instruction | FMAs per instruction | Control | Register file | Shared memory | Math | Math share |
|---|---|---|---|---|---|---|
| scalar FMA (CPU-style) | 1 | 70.0 | 28.3 | – | 4.6 | 4% |
| SIMT FFMA (32 lanes) | 32 | 2.19 | 28.3 | – | 4.6 | 13% |
| HMMA m16n8k16 (warp) | 2,048 | 0.03 | 1.55 | – | 2.0 | 56% |
| wgmma m64n256k16 (warpgroup) | 262,144 | 0.00 | 0.88 | 0.24 | 2.0 | 64% |
| accumulators kept in the unit (K = 4,096) | 262,144 | 0.00 | 0.003 | 0.24 | 2.0 | 89% |
(pJ per FMA.) SIMT amortizes the control over 32 lanes, but every FMA still reads three 4-byte operands and writes one: 16 bytes of register traffic. An m × n × k MMA reads A and B once for m·n·k FMAs, so its operand traffic falls with the tile.
Its fp32 accumulators are still read and written every instruction, which costs 8 / k bytes per FMA, 0.5 B at k = 16. That is now the largest overhead. Keeping the accumulators inside the unit for the whole reduction removes it. This is Lecture 5’s output-stationary systolic array, and Blackwell’s separate tensor memory for accumulators moves in this direction. A specialized accelerator such as a TPU keeps the operands and partial sums inside the array, so that 89% row is roughly its starting point.
4.4 Feeding the tensor cores
Script 03, Part 4. To keep one H100 SM’s 2,048 FMA/clk busy:
| Instruction | Instructions per clock per SM | Register file B/clk | Shared memory B/clk |
|---|---|---|---|
| SIMT FFMA | 64 | 32,768 | 0 |
| HMMA m16n8k16 | 1 | 1,792 | 0 |
| wgmma m64n256k16 | 0.008 | 1,024 | 80 |
| accumulators in the unit | 0.008 | 4 | 80 |
An SM can issue 4 instructions per clock, so SIMT cannot reach tensor throughput at all. Ampere-style HMMA works, but pushes ~1.8 KB per clock through the register file. That is why Hopper’s wgmma reads A and B straight from shared memory (80 B/clk of the 128 B/clk available), runs asynchronously, and lets one warpgroup keep a 64 × 256 accumulator tile. The tensor cores outgrew the register file, and the architecture moved operands closer to the math.
5. A GEMM on the whole GPU
5.1 How a modern GEMM kernel is organized
A Hopper GEMM kernel, in CUTLASS or Triton, is a small dataflow machine:
- Each CTA owns a BM × BN tile of C and loops over K in steps of BK.
- A producer warp issues TMA (Tensor Memory Accelerator) copies. These are asynchronous bulk copies of whole tiles from global to shared memory, described by a tensor descriptor and started by a single thread. Completion is signalled through a barrier in shared memory.
- Consumer warpgroups issue wgmma instructions on tiles already in shared memory. This is warp specialization.
- S pipeline stages of shared memory keep S − 1 tile copies in flight while one is consumed. The latency is covered by bytes in flight, not by warps (§2), so the kernel runs at 1 CTA per SM and ~19% occupancy.
- A thread-block cluster of CTAs on neighbouring SMs can read each other’s shared memory (distributed shared memory). TMA can multicast one tile into several CTAs’ shared memory, so CTAs that need the same A or B tile fetch it from L2 once.
- An epilogue applies bias, activation and casts to the accumulator tile (Lecture 10’s fusion) and writes C.
gpu.gemm_time models this:
- per K-step, compute time = 2·BM·BN·BK at the SM’s tensor rate;
- load time = the tile bytes at the SM’s share of L2 bandwidth (divided by the multicast factor);
- with S stages a step takes max(compute, load, shared memory, (latency + load)/(S − 1));
- the CTAs of a wave share their A and B panels in L2, so DRAM delivers each panel once per wave;
- waves = ⌈CTAs / (132 × CTAs per SM)⌉.
The L2 bandwidth is the shakiest assumption. We use A100’s published 5,120 B/clk at H100’s 1.83 GHz (9.4 TB/s).
5.2 Tiles and clusters
Per K-step, a CTA’s arithmetic intensity against L2 is BM·BN / (BM + BN) FLOPs per byte (bf16), and a cluster divides the shared operand’s bytes. Script 04, Part 1 (8192³, best stage count each):
| Tile | Cluster | FLOP per L2 byte | Efficiency | Bound |
|---|---|---|---|---|
| 64 × 64 | – | 32 | 29% | L2 bandwidth per SM |
| 128 × 128 | – | 64 | 57% | L2 bandwidth per SM |
| 128 × 128 | 2 × 1 | 85 | 75% | L2 bandwidth per SM |
| 128 × 256 | – | 85 | 75% | L2 bandwidth per SM |
| 128 × 256 | 2 × 1 | 128 | 92% | tensor cores |
The SM needs 4,096 FLOP/clk ÷ 38.8 B/clk (its L2 share) ≈ 106 FLOP per byte. Only the large tile with multicast exceeds it.
Doubling the tensor cores per SM from A100 to H100 without doubling the bytes per SM per clock (our assumption) is exactly what makes clusters and multicast necessary. Part 5 checks the sensitivity:
| L2 bandwidth | best configuration |
|---|---|
| half the assumed | 57% |
| assumed | 92% |
| double | 93% |
At double bandwidth, all three configurations reach ~93% and the choice stops mattering. The conclusion is conditional on the assumption.
5.3 Pipeline depth
Part 2 (128 × 256, cluster 2 × 1):
| Copy latency | 1 stage | 2 stages | 3 stages | 4 stages |
|---|---|---|---|---|
| 300 cycles | 50% | 93% | 93% | 93% |
| 600 cycles | 48% | 83% | 92% | 92% |
| 1,200 cycles | 45% | 66% | 92% | 92% |
Without pipelining, every step waits for its copy. Each stage of this tile costs 48 KiB of shared memory, so 4 stages (192 KiB) is the maximum. This is Little’s law again: S − 1 copies in flight must cover latency × bandwidth.
5.4 Shape: size, waves, tiles
Part 3, square GEMMs with the best configuration (figure 04_gemm_efficiency.png):
| M = N = K | 256 | 512 | 1,024 | 2,048 | 4,096 | 8,192 | 16,384 |
|---|---|---|---|---|---|---|---|
| efficiency | 1% | 5% | 25% | 69% | 86% | 92% | 96% |
Small GEMMs cannot fill 132 SMs, and they pay the pipeline prologue and the launch.
Wave quantization: with M = K = 4,096 and 128 × 256 tiles, N = 4,096 gives 512 tiles, which is 3.9 waves of 132 at 86%. N = 4,352 gives 544 tiles: 5 waves, the last one almost empty, at 74%. Efficiency is a sawtooth in N. Tile quantization is the analogous effect for M or N that is not a multiple of the tile: the edge tiles compute padding.
Library remedies include:
- many tile variants chosen per shape (our
best_gemmtries 450); - split-K;
- “stream-K” or persistent kernels, which divide the work more finely than whole tiles.
5.5 LLM shapes: prefill vs decode
Part 4, the four projections of one Llama 3 8B layer (QKV, O, gate+up, down; 436 MB of bf16 weights):
| Tokens | Time per layer | Efficiency | Bound |
|---|---|---|---|
| 1 | 146 µs | 0.3% | HBM bandwidth |
| 64 | 149 µs | 19% | HBM bandwidth |
| 256 | 182 µs | 62% | HBM bandwidth |
| 2,048 | 1.02 ms | 89% | tensor cores |
| 8,192 | 3.98 ms | 91% | tensor cores |
In decode, the 130 µs needed to stream the weights from HBM is the GEMM time; four launches add the rest. With 64-wide tiles the projections already have 64–448 CTAs, enough to pull full bandwidth under our 2× per-SM assumption, so the tuner uses no split-K.
No tile, cluster or pipeline helps decode. Only batching (Lecture 10), fewer bytes per weight (Lecture 4), or more bandwidth per FLOP (Lectures 14–15) do. The H100’s ridge is at ~295 tokens: below that batch, the projections are memory-bound.
6. GPUs as “repurposed and augmented” accelerators
What generality costs.
- An SM carries a SIMT front end per sub-core, a 256 KB register file, caches, and units ML rarely uses (FP64, texture, graphics remnants).
- Operands travel register file → tensor core, or shared memory → tensor core, instead of flowing between neighbouring PEs as in a systolic array.
- Every kernel launch, synchronization and DRAM round trip between kernels is exposed (Lecture 10).
- The 45 nm model of §4.3 puts SIMT arithmetic at 13% of its energy and an accumulator-stationary unit at 89%. The tensor cores close most of that gap, but not all of it.
What generality buys.
- Programmability and a vast software ecosystem.
- Fast adoption of new algorithms: FlashAttention (Lecture 10) was a CUDA kernel before it was anything else.
- Graceful handling of the irregular parts of models: sparsity, dynamic shapes, custom operators.
- Hardware evolution under a stable model: tensor cores, new formats, TMA and clusters arrived without breaking a single CUDA program.
What still limits GPU efficiency on ML (from this lecture’s experiments and Lectures 6 and 10):
- Memory bandwidth in decode: 0.3% of peak at batch 1 (§5.5).
- Non-matmul work on the much slower CUDA cores and SFUs (Lecture 6’s Amdahl and exp bottlenecks).
- Launch and synchronization overheads for small kernels.
- Shape effects: waves, tiles, small matrices.
- Data movement inside the chip: L2 bandwidth per SM (§5.2), the register file (§4.4).
- In practice, power: sustained clocks under a 700 W limit are often below the boost clock behind the datasheet peak.
The next lectures take the other side of each trade. Lectures 12–13 lay the whole computation out in space to remove launches and DRAM round trips. Lectures 14–15 move compute to the memory.
7. Summary
- A GPU runs warps of 32 threads on SMs: H100 has 132 SMs × 4 sub-cores, 256 KB of registers and 256 KB of L1/shared memory per SM, 50 MB of L2, and 3.35 TB/s of HBM.
- SIMT runs both sides of a divergent branch; a 5% branch still splits 27 of 32 warps. Memory is fetched in 32-byte sectors (12.5% efficiency for strided or random access), and shared memory in 32 banks (padding removes column conflicts).
- Latency hiding is Little’s law: 600 cycles × 13.9 B/cycle = 8.3 KB in flight per SM, or 65 single loads. Unrolling and vector loads (ILP) substitute for warps (TLP).
- Occupancy is the minimum over warps, registers and shared memory. It moves in steps, and it is a means: the fastest tensor-core kernels run at 12–19%.
- Tensor cores give 8–16× the FP32 math per clock per SM in the same programming model. Each generation added a narrower format, and the ridge rose from 139 to 1,125 FLOP/B.
- Matrix instructions amortize control and operand traffic. The math share of energy rises from 13% (SIMT) to 56% (HMMA), 64% (wgmma), and 89% with accumulators kept in the unit. Feeding the tensor cores forced Hopper to move operands to shared memory and copies to TMA.
- A Hopper GEMM is a pipeline: TMA producers, wgmma consumers, S stages, clusters with multicast. In our model the 128 × 256 tile with a 2-CTA cluster reaches 92% on 8192³. Efficiency depends on size (5% at 512³), waves (a sawtooth) and the assumed L2 bandwidth.
- Decode projections run at 0.3% of peak: they are a memory problem that no kernel optimization solves.
8. Common pitfalls
- “Rare branches are cheap.” A warp diverges unless all 32 lanes agree (§1.3).
- “Maximize occupancy.” Maximize work in flight; the best GEMMs run at 19% occupancy (§3.3).
- “Peak TFLOPS is what a kernel gets.” Size, waves, tiles and memory decide the fraction (§5.4); decode gets 0.3%.
- “Tensor cores made the register file irrelevant.” They made it the bottleneck; Hopper moved operands to shared memory (§4.4).
- “More SMs always help.” Not when the tiles do not fill a wave (§5.4), or when the work is memory-bound.
- “Model results are measurements.” The L2 bandwidth and latency are assumptions; doubling the L2 bandwidth erases the cluster’s advantage (§5.2).
- “Datasheet clocks are sustained clocks.” Under a power limit they often are not; even the datasheet’s own FP32 and tensor figures imply different clocks (§4.1).
9. Exercises
Exercise 1 (divergence). In a warp, lanes 0–7 take a 20-instruction path and lanes 8–31 a 5-instruction path. What does the warp cost, and what is its lane efficiency? What if the 8 lanes were spread one per warp over 8 warps?
Answer
The warp diverges, so it runs both paths: 25 cycles. Useful lane-cycles = 8 × 20 + 24 × 5 = 280; issued = 32 × 25 = 800, so efficiency is 35%.
If each of 8 warps has one long lane, all 8 warps cost 25 cycles. Total 200 warp-cycles, against 8 × 5 + 20 = 60 if the 8 long lanes were packed into one warp. Grouping similar work into the same warp is what counts.
Exercise 2 (structs vs arrays). A kernel reads all three fields of an array of 3-float structs, one field per instruction. How many sectors does a warp move in total, and how many with a structure of arrays?
Answer
Array of structs: each field load spans 32 × 12 = 384 bytes, which is 12 sectors. Three loads move 36 sectors (1,152 B) for 384 useful bytes, unless L1 catches the re-reads.
Structure of arrays: each field load is unit-stride, 4 sectors, so 12 sectors (384 B) in total: 3× less traffic.
Exercise 3 (bank conflicts). A warp reads a column of a float matrix in shared memory with row pitch 36 words. How many passes? Which pitches between 32 and 40 are conflict-free?
Answer
gcd(36, 32) = 4. The column covers 32/4 = 8 banks with 4 words each: 4 passes.
Conflict-free needs gcd(p, 32) = 1, so the odd pitches 33, 35, 37 and 39.
Exercise 4 (Little’s law). With the model’s numbers (600 cycles, 13.9 B/cycle per SM), how many warps with one 16-byte-per-lane load each saturate an SM’s bandwidth? How many bytes must be in flight across the H100?
Answer
A warp-load of 16 B per lane is 512 B. 8,321 / 512 = 16.3 warps.
Across 132 SMs: 8,321 × 132 ≈ 1.1 MB in flight. Equivalently, 3.35 TB/s × 328 ns (600 cycles at 1.83 GHz).
Exercise 5 (occupancy). A kernel uses 256 threads, 96 registers per thread and 40 KiB of shared memory per block on an H100. Compute its occupancy and limiter. What does reducing registers to 80 change?
Answer
- Warps: 8 per block, so the warp limit allows 8 blocks.
- Registers: 96 × 32 = 3,072 per warp × 8 warps = 24,576 per block, so 65,536 / 24,576 allows 2 blocks.
- Shared memory: 41 KiB per block, so 228 / 41 allows 5 blocks.
Occupancy = 2 × 8 / 64 = 25%, limited by registers.
At 80 registers: 2,560 × 8 = 20,480 per block, so 3 blocks = 24 warps = 37.5%. Shared memory is not yet binding.
Exercise 6 (peak from parts). Reconstruct the H100 SXM’s dense bf16 peak and A100’s from SM count, tensor cores per SM, FMAs per tensor core per clock and clock frequency.
Answer
- H100: 132 SMs × 4 tensor cores × 512 FMA/clk × 2 FLOP × 1.83 GHz = 989 TFLOPS.
- A100: 108 × 4 × 256 × 2 × 1.41 GHz = 312 TFLOPS.
Clock for clock, an H100 SM does twice the tensor math of an A100 SM.
Exercise 7 (operand traffic). Count the register-file bytes of an HMMA m16n8k16 (fp16 A and B, fp32 C and D) and compare per FMA with a SIMT FFMA.
Answer
A: 16 × 16 × 2 = 512 B. B: 16 × 8 × 2 = 256 B. C read and D write: 16 × 8 × 4 = 512 B each. Total 1,792 B for 2,048 FMAs = 0.875 B/FMA, against 16 B/FMA for FFMA: 18× less.
The accumulators alone are 1,024 B (0.5 B/FMA).
Exercise 8 (when do accumulators dominate?). For an m × n × k MMA with bf16 inputs and fp32 accumulators read and written per instruction, compare A+B bytes per FMA with accumulator bytes per FMA. For m = 64, n = 256, what k would make them equal?
Answer
- A + B per FMA: 2(m + n)k / (mnk) = 2(m + n)/(mn) = 640 / 16,384 = 0.039 B/FMA.
- Accumulators: 8/k B/FMA.
They are equal at k = 8 / 0.039 ≈ 205. Instructions have k = 16, so accumulator traffic is ~13× the input traffic. Hence the move to keep accumulators near the math: systolic arrays, and Blackwell’s tensor memory.
Exercise 9 (choosing a tile). The SM needs 4,096 FLOP/clk ÷ 38.8 B/clk ≈ 106 FLOP per L2 byte. Which tiles (bf16, no cluster) meet that? Why not use 256 × 256?
Answer
Without a cluster, FLOP per L2 byte = BM·BN/(BM + BN): 128 × 128 → 64, 128 × 256 → 85, 256 × 256 → 128. Only 256 × 256 meets 106 without a cluster.
But its fp32 accumulators are 256 × 256 × 4 = 256 KiB, the entire register file of the SM, leaving nothing for anything else, and its shared memory per stage is 64 KiB. Hence 128 × 256 tiles in 2-CTA clusters, which behave like a 256 × 256 tile split across two SMs.
Exercise 10 (waves). For M = K = 4,096 with 128 × 256 tiles and one CTA per SM, list the values of N between 4,096 and 5,120 (multiples of 256) and their waves. Propose two fixes for the bad ones.
Answer
Tiles = 32 × N/256:
| N | tiles | waves | slots filled |
|---|---|---|---|
| 4,096 | 512 | 4 | 97% |
| 4,352 | 544 | 5 | 82% |
| 4,608 | 576 | 5 | 87% |
| 4,864 | 608 | 5 | 92% |
| 5,120 | 640 | 5 | 97% |
Fixes:
- pick a different tile for these shapes, for example 128 × 128 with 2 CTAs per SM, so that the quanta are smaller;
- split the last wave’s work more finely: split-K for the leftover tiles, or stream-K / persistent kernels that divide all K-iterations evenly over 132 SMs.
Exercise 11 (decode bound). Using §5.5, what is the upper bound on Llama 3 8B tokens/s at batch 1 from the projections alone on an H100? At what batch do the projections stop being memory-bound?
Answer
Per layer, 436 MB / 3.35 TB/s = 130 µs. Over 32 layers that is 4.2 ms, so ~240 tokens/s (214 with the model’s launches). Attention and the LM head make it lower.
A projection’s intensity is ≈ 2·M·N·K / (2·N·K) = M FLOP/byte, so it reaches the ridge of 295 at M ≈ 295 tokens. Part 4 shows 62% at 256 tokens and 89% at 2,048.
Exercise 12 (programming: TMA in the SM model). Extend gpu.simulate_sm with a “copy engine”. One producer warp issues bulk copies of 16 KiB that complete after the memory latency, limited by bandwidth, without occupying any consumer warp. How many stages (copies in flight) saturate the SM’s bandwidth, and how many warps does the kernel then need?
Answer
Little’s law: 8,321 bytes in flight / 16,384 bytes per copy is about 0.5, so one copy in flight already covers the latency (two for margin, since each copy must also arrive at 13.9 B/cycle, which takes 1,180 cycles).
The kernel then needs only enough warps to consume the data: for a tensor-core GEMM, one or two consumer warpgroups. That is why Hopper GEMMs run at 1 CTA and 12 warps per SM.
Implementation:
- Add a
copyinstruction kind whose completion time follows the same bandwidth server. - Let one warp issue copies up to S ahead of the consumers.
- Have consumers wait on the copy’s completion instead of on their own loads.
10. Further reading
- E. Lindholm, J. Nickolls, S. Oberman, J. Montrym, “NVIDIA Tesla: a unified graphics and computing architecture”, IEEE Micro 28(2), 2008.
- J. Nickolls, I. Buck, M. Garland, K. Skadron, “Scalable parallel programming with CUDA”, ACM Queue 2008.
- NVIDIA, Volta, Ampere (A100) and Hopper (H100) architecture whitepapers; CUDA C++ Programming Guide (thread hierarchy, occupancy, compute-capability tables).
- V. Volkov, “Better performance at lower occupancy”, GTC 2010; V. Volkov, Understanding Latency Hiding on GPUs, PhD thesis, UC Berkeley, 2016.
- T. G. Rogers, M. O’Connor, T. M. Aamodt, “Cache-conscious wavefront scheduling”, MICRO 2012 (warp scheduling policies, GTO).
- M. Gebhart et al., “Energy-efficient mechanisms for managing thread context in throughput processors”, ISCA 2011 (register-file energy).
- Z. Jia, M. Maggioni, B. Staiger, D. P. Scarpazza, “Dissecting the NVIDIA Volta GPU architecture via microbenchmarking”, 2018; and later microbenchmark studies of Ampere and Hopper.
- NVIDIA CUTLASS documentation (CuTe, warp specialization, TMA, clusters); P. Tillet et al., “Triton”, MAPL 2019.
- M. Osama et al., “Stream-K: work-centric parallel decomposition for dense matrix-matrix multiplication on the GPU”, PPoPP 2023.
- J. Shah et al., “FlashAttention-3”, NeurIPS 2024 (warp specialization and asynchrony on Hopper).
11. Next lecture
Lecture 12, Reconfigurable dataflow architectures, takes the opposite approach. A GPU executes one kernel after another and hides latency with thousands of threads. Plasticine (Prabhakar et al., ISCA 2017) instead lays a program out in space:
- pattern compute units and pattern memory units connected by a static network;
- parallel patterns (map, reduce, fold, filter, groupBy) as the programming abstraction;
- intermediate results streaming between units without returning to DRAM.
We will compile patterns onto a small grid, simulate a coarse-grained pipeline, and measure what spatial execution saves over kernel-by-kernel execution.