L2 Intermediate · Module 7: Profiling: measure, don't guess
7.1The top-down profiling discipline
A GPU program can be limited by the host, by copies, by launch overhead, by memory bandwidth, by math, or by a dozen smaller ceilings, and intuition picks the wrong one most of the time. This chapter sets out the order in which to look: the whole timeline first, then the kernels that dominate it, then the one ceiling inside the worst kernel. Change one thing, then measure again.
By the end you will be able to
- ✓State the rule "no change without a measurement that names the bottleneck", and say why intuition fails on GPUs
- ✓Work through the top-down levels in order (program, timeline, kernel, instruction) and name the tool for each
- ✓Use Amdahl's law to rank what to optimize before optimizing anything
- ✓Recognize bottleneck migration as progress, and treat end-to-end time as the final judge
Modules 4 to 6 kept producing numbers that surprised us. In 5.1, the three textbook fixes to the reduction tree (less divergence, no bank conflicts) made no difference at all; what doubled the speed was putting more loads in flight. The same reduction ran at 127 GB/s in one session and 162 GB/s in another, with the same code, because the laptop had lowered its memory clock. A launch that “costs nothing” turned out to take 2.7 µs (4.6), as long as many decode kernels run.
Each time, the measurement disagreed with the guess. This module turns that into a rule:
Why guessing fails on a GPU
A CPU program is usually limited by one obvious thing. A GPU program has many ceilings, and any one of them can be the limit:
| Ceiling | Where it lives | Seen in |
|---|---|---|
| Host work: Python, tokenization, scheduling | CPU | 4.8 |
| Host-device copies, implicit syncs | PCIe, the stream queue | 4.3, 4.6 |
| Launch overhead, tiny kernels | driver, front end | 4.6 |
| Too few blocks, partial waves | the grid | 6.5 |
| DRAM bandwidth | HBM / GDDR | 3.1, 4.7 |
| Shared-memory bandwidth, bank conflicts | the SM | 6.2, 6.4 |
| Latency: too few warps or too little ILP | the SM | 2.5, 6.6 |
| Registers, spills, occupancy | the SM | 6.3 |
| Math throughput (FP32, tensor cores) | the SM | 6.7 |
At any moment a kernel is held back by one or two of these, and the others don’t matter. Speeding up something that isn’t the limit changes nothing. It can even make things worse: unrolling a loop to add ILP can raise register use until occupancy drops (6.3).
The ceilings also hide each other. A kernel that looks slow may not be the problem at all. The GPU may simply be idle between kernels, waiting for Python to launch the next one.
Top-down: four levels, in order
Look from the outside in. Each level tells you where to look at the next one, and each has a tool built for it.
Level 0: the whole program. Time end to end with a wall clock: requests per second, tokens per second, step time. This is the number users feel, and the one every later change must improve. Write it down before touching anything.
Level 1: the timeline. Is the GPU busy? Record a timeline of CPU threads, API calls, copies and kernels with Nsight Systems or the PyTorch profiler (7.2, 7.4). Look for:
- Gaps between kernels, where the GPU waits for the host: Python overhead, a data loader, a
.item()that forces a sync. - Copies that don’t overlap with compute, or happen every step.
- Many tiny kernels, each shorter than its own launch cost.
If the GPU sits idle for a large share of the timeline, no kernel tuning will help. Fix the host side first: batch the work, remove syncs, overlap copies, use CUDA Graphs.
Level 2: the kernels. Once the GPU is busy, rank kernels by total GPU time: time per call × number of calls. Usually a handful account for most of it. For a transformer, that means the GEMMs, attention, and whatever unfused element-wise ops are left over.
Level 3: inside one kernel. Only now, and only for the top kernel, open Nsight Compute (7.2). Compare its throughput with the roofline (3.3): what fraction of peak memory bandwidth and of peak compute does it reach? That tells you which ceiling it’s against. Then the detailed sections (memory traffic, warp stall reasons, occupancy, and source-level counters) point to the line responsible.
Rank before you optimize
Amdahl’s law sets the payoff. If a part takes a fraction of the total time and you make it times faster, the whole program speeds up by
Make a kernel that takes 5% of the step infinitely fast and the step gets at most faster. Make the GEMMs that take 60% just 1.5× faster and the step gets faster. Level 2 exists to find the large before you spend a week on .
There’s a second ceiling for each kernel: the roofline. A memory-bound kernel that already moves its bytes at 90% of peak bandwidth can gain at most about 10% by tuning. To get more, it has to move fewer bytes, by fusing it with a neighbor or using a smaller data type. The 5.1 reduction reached 162 of about 168 GB/s. Once a kernel is that close to the limit, stop tuning it and move on.
One change, then measure
Every optimization is an experiment. Do it like one:
- Hypothesis. Name the bottleneck and the metric that should move. For example: “the inner loop is limited by shared-memory loads, so register tiling will lower the shared-memory load count per FMA.”
- Change one thing. If you change two things and the time improves, you don’t know which one helped, or whether one of them hurt.
- Measure the targeted metric and the end-to-end time. Did the metric move? Did the time? If the metric moved but the time didn’t, that metric wasn’t the bottleneck.
- Keep it or revert it. A change that doesn’t pay for its complexity is removed.
Keep the conditions fixed while you measure. The 5.1 episode shows why: the same kernel measured 127 and 162 GB/s depending on the laptop’s power state. Measure with a warm GPU, the same clocks, the same input, and enough repetitions to see the noise. 7.3 covers what the timer actually measures.
Bottleneck migration is progress
Fix one limit and another takes its place. Reduce launch overhead with CUDA Graphs, and the timeline is suddenly full of kernels, so now the GEMMs dominate. Tile the GEMM, and shared-memory bandwidth gives way to latency, then to the FP32 units. This isn’t failure: the bottleneck moving means you removed one. At the serving level the same thing happens on a larger scale: compute, KV-cache memory, the scheduler and the network take turns being the limit as load grows.
So the loop is: measure, find the limit, remove it, measure again. Stop when the remaining limit is a ceiling of the hardware (peak bandwidth or peak FLOP/s), or when the end-to-end target is met.
The method in one page
| Level | Question | Tool | Typical fixes |
|---|---|---|---|
| 0. Program | How fast is it, end to end? | wall clock, benchmark script | (set the baseline) |
| 1. Timeline | Is the GPU busy? Where are the gaps? | Nsight Systems, PyTorch profiler | batch, remove syncs, overlap copies, CUDA Graphs |
| 2. Kernels | Which kernels take the most total time? | kernel summary from either tool | fuse, pick better library kernels, change shapes |
| 3. One kernel | Which ceiling is it against, and why? | Nsight Compute | coalescing, tiling, occupancy, tensor cores |
The rest of the module covers the tools (7.2, 7.4), what their numbers mean (7.3), and then applies the method from start to finish to one case (7.5).
Sources
Check your understanding
0 / 4 answered1.A PyTorch inference loop shows the GPU at 40% utilization. What should you look at first?
2.A kernel takes 4% of step time. You make it 3× faster. Roughly how much faster is the step?
3.After you add CUDA Graphs, the GEMMs dominate the timeline. What does that mean?
4.You change the tile size and the shared-memory unroll factor at the same time, and the kernel gets 8% faster. What is the problem?