M0 · 8 h · full lesson
Setup, profiling, roofline
You cannot speed up a kernel you have not located. This module is the measurement habit the rest of the programme depends on.
- Prerequisite
- Python, basic PyTorch tensors.
- VRAM
- None for the drill. 8 GB if you profile a tiny PyTorch model.
- Runtime
- Under an hour for the smoke path.
What you must be able to show
- 01
From FLOPs, bytes, a peak and a bandwidth, compute intensity, the ridge, the bound, and the roofline time. The unit test is the answer key for the arithmetic.
- 02
Time one region with a stated protocol (CUDA events, or an explicit UNTESTED) and write what that number excludes: warmup, copies, other steps.
- 03
Put token-by-token decode and a diffusion step on a ridge you calculated, and say which lever is wasted on each.
Insights
- A ratio without a protocol is not a measurement. Warmup, synchronisation, sample count, and the work you left out belong in the same sentence as the number.
- Left of the ridge, rewriting the math does not move the timer. Fusion is a byte delete. Tensor cores are a math upgrade. They are not interchangeable.
- Decode of one token is usually a memory system that happens to multiply. A video step is a long activation plus attention. Porting a decode trick onto a sampler and expecting the same factor is the first way this programme goes wrong.
A speedup is a ratio of two times. Both times need a definition: which GPU, which shape, warmed up or not, synchronised or not, one step or fifty. This module is that definition. The rest of KernelDojo is not allowed to skip it.
UNTESTED on device. The arithmetic below is pure Python and is tested in the programme files. CUDA event timing is a specification. This preview has no GPU.
Roofline, in one picture. Peak math throughput and peak bandwidth fix a ridge. Arithmetic intensity is FLOPs divided by bytes moved. Left of the ridge, the kernel waits on memory. Right of it, the kernel waits on math. Fusion moves bytes. Tensor cores and algorithms move FLOPs. Doing the wrong one is how weeks disappear.
def arithmetic_intensity(flops: float, nbytes: float) -> float:
if nbytes <= 0:
raise ValueError("nbytes must be positive")
return flops / nbytes
def ridge_point(peak_flops_per_s: float, bandwidth_bytes_per_s: float) -> float:
if bandwidth_bytes_per_s <= 0:
raise ValueError("bandwidth must be positive")
return peak_flops_per_s / bandwidth_bytes_per_s
def bound(flops: float, nbytes: float, peak: float, bandwidth: float) -> str:
return "compute" if arithmetic_intensity(flops, nbytes) >= ridge_point(peak, bandwidth) else "memory"
def roofline_seconds(flops: float, nbytes: float, peak: float, bandwidth: float) -> float:
return max(flops / peak, nbytes / bandwidth)Worked arithmetic, the same numbers the unit test checks. Peak 200 TFLOP/s and bandwidth 2 TB/s put the ridge at 100 FLOP/byte. Four FLOPs moving four bytes is intensity 1, which is memory-bound: the roofline time is the byte time, not the FLOP time. A thousand FLOPs on those same four bytes is intensity 250, which is compute-bound. If you cannot reproduce that with a pencil, do not open a profiler yet.
Wrong conclusion. ‘The kernel uses Tensor Cores, so it is compute-bound.’ Tensor Cores raise the ridge. They do not move a streaming copy onto the right-hand side. Check intensity first.
| Regime | What dominates the bytes | First lever |
|---|---|---|
| LLM decode, batch 1 | Weights, every token | Quantise weights, fuse the tiny epilogue, batch if latency allows |
| Diffusion step, long latent | Activations between small ops; then attention math | Fuse the epilogue; then look at attention, not at another all-reduce slogan |
| Warmup / first call | Compile, autotune, allocator | Do not quote it |
Timing protocol you will be graded on. Warm up. Record CUDA events around the region, or synchronise on both sides of a CPU timer. Use the same protocol for both sides of a ratio. Report N. Attach a profiler table only to name the op, not as the clock.
def time_ms(fn, warmup=5, iters=20) -> float:
for _ in range(warmup):
fn()
torch.cuda.synchronize()
start, end = torch.cuda.Event(True), torch.cuda.Event(True)
start.record()
for _ in range(iters):
fn()
end.record()
torch.cuda.synchronize()
return start.elapsed_time(end) / itersSmoke test. python -m unittest modules/00-roofline/test_roofline.py — this one was run in the preview. It does not touch a device.
Expected-results table. Fill the right-hand column on your machine. Leaving it blank is honest. Copying 4.52× into it is not.
| Check | Pass | Your number |
|---|---|---|
| Intensity of BF16 RMSNorm (order of magnitude) | Below a ridge of ~50–150 FLOP/byte on current high-end GPUs | ESTIMATE until you measure peak and bandwidth |
| Event timer vs unsynchronised CPU timer | CPU timer much smaller | UNTESTED here |
| Top op in one eager step of the synthetic block | Name it from a trace | UNTESTED here |