Skip to content

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

  1. 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.

  2. 02

    Time one region with a stated protocol (CUDA events, or an explicit UNTESTED) and write what that number excludes: warmup, copies, other steps.

  3. 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 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.

modules/00-roofline/roofline.py
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.

RegimeWhat dominates the bytesFirst lever
LLM decode, batch 1Weights, every tokenQuantise weights, fuse the tiny epilogue, batch if latency allows
Diffusion step, long latentActivations between small ops; then attention mathFuse the epilogue; then look at attention, not at another all-reduce slogan
Warmup / first callCompile, autotune, allocatorDo 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.

Timing sketch — UNTESTED, requires CUDA
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) / iters

Smoke 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.

CheckPassYour number
Intensity of BF16 RMSNorm (order of magnitude)Below a ridge of ~50–150 FLOP/byte on current high-end GPUsESTIMATE until you measure peak and bandwidth
Event timer vs unsynchronised CPU timerCPU timer much smallerUNTESTED here
Top op in one eager step of the synthetic blockName it from a traceUNTESTED here