Dive into Deep Learning · §13.1
The Roofline Model: Compute, Bandwidth, and Overhead Regimes
arithmetic intensity · roofline bound · ridge point · asynchronous timing · kernel profiling
Motivation
bfloat16 peak.Arithmetic Intensity
I \;=\; \frac{\textrm{FLOPs performed}}{\textrm{bytes moved (DRAM $\leftrightarrow$ on-chip)}}
bf16, yielding I = 0.25\text{ FLOP/B} at any size.Matrix Multiplication
I \;=\; \frac{2}{b}\left(\frac{1}{F} + \frac{1}{B} + \frac{1}{D}\right)^{-1} \;\approx\; \frac{2B}{b}
Training vs. Decoding
The Roofline Model
Hardware Thresholds
The ridge point I^* = P/\beta is the minimum arithmetic intensity required to reach peak compute throughput:
I^*_{\textrm{bf16}} \;=\; \frac{165.2\textrm{ TFLOP/s}}{1.008\textrm{ TB/s}} \;\approx\; 164\textrm{ FLOP/byte}, \qquad I^*_{\textrm{tf32}} \;=\; \frac{82.6\textrm{ TFLOP/s}}{1.008\textrm{ TB/s}} \;\approx\; 82\textrm{ FLOP/byte}
bf16): Since I = \frac{2n^3}{2(3n^2)} = \frac{n}{3}, the roofline model predicts that square products become compute-bound once n \gtrsim 3 I^* \approx 500.Accurate Benchmarking
time.perf_counter() around an unsynchronized kernel call measures only the CPU enqueue time h (\sim 10\,\mu\text{s}), not the GPU execution time t_{\textrm{dev}}.Synchronization Hazards
When Python requests a tensor value living on the GPU, the host blocks until all queued kernels finish, adding a device-to-host transfer latency \sigma and destroying host–device overlap:
t_{\textrm{once}} \;\approx\; \max(h,\, t_{\textrm{dev}}), \qquad t_{\textrm{every}} \;\lesssim\; h + t_{\textrm{dev}} + \sigma
read every step: 0.063 s read once: 0.040 s
Hidden barriers in training loops: float(loss), loss.item(), print(x), host branches if loss < 0.1:, and data-dependent shapes (x[x > 0]). Always accumulate metrics on the GPU and synchronize only at logging intervals.
Benchmarking Discipline
d2l.Benchmark runs warm-up iterations first to absorb JIT compilation, cuBLAS handle creation, and kernel loading.repeats calls.bf16 matmul): Intensity I = 4096/3 \approx 1365 \gg 164, so the roofline predicts 2 \cdot 4096^3 / P \approx 0.83\text{ ms}, matching measurement within \sim 10\%.Bandwidth vs. Compute Roof
Multiplying B activation rows by a 16384 \times 16384 bfloat16 weight matrix (512\text{ MiB}) gives intensity I \approx B, crossing the ridge I^* = 164 between B = 128 and B = 256:
NVIDIA GeForce RTX 4090: ridge point 164 FLOP/byte
B = 1: bound 0.53 ms (memory ), measured 0.57 ms, 0.9 TFLOP/s
B = 4: bound 0.53 ms (memory ), measured 0.57 ms, 3.7 TFLOP/s
B = 16: bound 0.53 ms (memory ), measured 0.57 ms, 15.0 TFLOP/s
B = 64: bound 0.54 ms (memory ), measured 0.58 ms, 58.9 TFLOP/s
B = 128: bound 0.54 ms (memory ), measured 0.64 ms, 106.9 TFLOP/s
B = 256: bound 0.83 ms (compute), measured 0.90 ms, 152.2 TFLOP/s
B = 1024: bound 3.33 ms (compute), measured 3.34 ms, 164.6 TFLOP/s
From B = 1 to B = 128, execution time stays flat near 0.6\text{ ms} (the time to stream 512\text{ MiB} at \sim 0.9\text{ TB/s}) despite 128\times more arithmetic; above B = 256, time grows linearly with B along the compute roof.
Bandwidth-Bound Regime
On an 8192 \times 8192 float32 matrix (256\text{ MiB}), unary operations (x + 1, x * 1.5, sin(x), sigmoid(x)) each read 256\text{ MiB} and write 256\text{ MiB} (512\text{ MiB} total, or 0.53\text{ ms} at 1.008\text{ TB/s}):
add: 0.59 ms/call
mul: 0.59 ms/call
sin: 0.59 ms/call
sigmoid: 0.58 ms/call
sin, sigmoid) take the exact same \sim 0.6\text{ ms} (\sim 0.9\text{ TB/s}) as simple addition.Unfused Chains
In eager execution, each operation in the 8-op GELU approximation gelu_ish launches a separate kernel that reads its inputs from DRAM and writes its output back to DRAM:
1 op: 0.59 ms/call
chain: 5.25 ms/call
Six unary ops move 6 \times 2 = 12 tensors and two binary ops move 2 \times 3 = 6 tensors (18 array transfers vs. 2 for x + 1), making the eager chain 8\text{--}9\times slower until a compiler fuses it into one kernel (§13.3).
Size Sweep
bfloat16 products from n = 256 to 8192 tests whether compute saturation begins at the paper threshold n \approx 500 (I = n/3 = 164).Overhead-Bound Regime
Comparing wall-clock time per call (d2l.Benchmark) against actual GPU kernel execution time (d2l.kernel_times) on a 256 \times 256 bfloat16 tensor exposes where the constant time goes:
256 x 256 product : 21.0 us per call, device busy 3.2 us
x + 1 on 256 x 256: 10.9 us per call, device busy 1.0 us
The GPU finishes either operation in 1\text{--}4\,\mu\text{s}; the remaining 10\text{--}100\,\mu\text{s} is host dispatch latency h (Python interpreter, framework dispatcher, and CUDA driver launch). For a 256 \times 256 product, the most expensive part is the line of Python that asks for it.
Unified Time Model
t \;\gtrsim\; \max\!\left(h,\; \frac{\textrm{FLOPs}}{P},\; \frac{\textrm{bytes}}{\beta}\right)
Diagnosis & Remedies
| Regime | Profiler & Timing Symptom | Standard Systems Remedies |
|---|---|---|
| Compute-bound | GPU busy \approx 100\%; I > I^*; time \propto \textrm{FLOPs} | Narrower precision (bf16/fp8), fewer FLOPs, more GPUs |
| Bandwidth-bound | GPU busy \approx 100\%; I < I^*; time \propto \textrm{bytes} | Operator fusion, larger batch B, narrower types, on-chip tiling |
| Overhead-bound | GPU idle between kernels; time \approx h independent of size | Compile the whole step, CUDA graph replay, larger batches |
Step Accounting
Consider one SGD step of a 3-layer MLP (4096 \to 4096 \to 4096 \to 10) on batch B = 1024 in float32 using tf32 Tensor Cores (P = 82.6\text{ TFLOP/s}, I^* = 82\text{ FLOP/byte}):
wall clock 2.67 ms per step, device busy 2.65 ms (99%), matrix products 80%
0.85 ms 32% Kernel2<cutlass_80_tensorop_s1688gemm_256x128_16x3_n
0.82 ms 31% Kernel2<cutlass_80_tensorop_s1688gemm_128x128_32x3_t
0.42 ms 16% multi_tensor_apply_kernel<TensorListMetadata<2>, Bin
0.40 ms 15% Kernel2<cutlass_80_tensorop_s1688gemm_128x256_16x3_n
0.06 ms 2% vectorized_elementwise_kernel<4, BinaryFunctor<float
0.02 ms 1% vectorized_elementwise_kernel<4, launch_clamp_scalar
The profile matches our 2.5\text{ ms} paper prediction within 10\%: the GPU is busy nearly 100\% of the step, and gemm kernels account for > 75\% of device time.
Check Your Understanding
On an RTX 4090 (bfloat16 peak P = 165\text{ TFLOP/s}, bandwidth \beta = 1.0\text{ TB/s}, ridge I^* = 164\text{ FLOP/byte}, host launch h = 10\,\mu\text{s}), predict the binding floor and minimum time for each bfloat16 operation:
x + 1 on 10^8 numbers (200\text{ MB} read + 200\text{ MB} write).Answers:
Key Takeaways
| Concept / Quantity | Formula / Definition | Value on RTX 4090 | Systems Role in Deep Learning |
|---|---|---|---|
| Arithmetic Intensity (I) | \textrm{FLOPs} / \textrm{bytes moved} | 0.25 (add) to > 10^3 (GEMM) | Separates memory-starved ops from math-saturated ops |
| Matmul Intensity (\mathbf{X}\mathbf{W}) | \approx 2B/b for B \ll D, F | \sim 1 (B=1) to 910 (B=8192) | Explains why batching speeds up linear layers |
| Roofline Bound | \textrm{Throughput} \le \min(P, I\beta) | P = 165\text{ TF/s}, \beta = 1\text{ TB/s} | Upper bound on achievable FLOP/s at intensity I |
| Ridge Point (I^*) | P / \beta | 164\text{ FLOP/B} (bf16), 82 (tf32) |
Minimum intensity required to saturate Tensor Cores |
| Three-Floor Step Bound | t \gtrsim \max(h, \textrm{FLOPs}/P, \textrm{bytes}/\beta) | h \approx 13\text{--}100\,\mu\text{s} | Diagnoses overhead-, compute-, and bandwidth-bound ops |
| Synchronization Rule | Warm up + barrier outside loop | d2l.Benchmark, kernel_times |
Prevents host–device serialization and enqueue-only timing |