The Performance Model

Dive into Deep Learning · §13.1

The Roofline Model: Compute, Bandwidth, and Overhead Regimes
arithmetic intensity · roofline bound · ridge point · asynchronous timing · kernel profiling

Why Do Matrix Products Run at 2% or 100% of Peak?

Motivation

  • Large square product (8192 \times 8192): Sustains nearly 100\% of the RTX 4090’s 165.2\text{ TFLOP/s} bfloat16 peak.
  • Small square product (256 \times 256): Achieves less than 2\% of peak—over 50\times lower utilization on the exact same primitive.
  • Why it matters: A faster GPU or second card helps only if we know whether compute (P), memory bandwidth (\beta), or host dispatch (h) binds the step.

Counting FLOPs, Bytes, and Arithmetic Intensity

Arithmetic Intensity

I \;=\; \frac{\textrm{FLOPs performed}}{\textrm{bytes moved (DRAM $\leftrightarrow$ on-chip)}}

  • The hardware gap: While moving 1\text{ B} from DRAM takes 1/\beta \approx 1\text{ ps}, Tensor Cores can execute P/\beta \approx 164\text{ FLOPs} in that picosecond.
  • Elementwise benchmark (y_i = x_i + 1): Performs 1\text{ FLOP} per element while reading 2\text{ B} and writing 2\text{ B} in bf16, yielding I = 0.25\text{ FLOP/B} at any size.

Arithmetic Intensity of a Linear Layer Matmul

Matrix Multiplication

I \;=\; \frac{2}{b}\left(\frac{1}{F} + \frac{1}{B} + \frac{1}{D}\right)^{-1} \;\approx\; \frac{2B}{b}

  • The smallest dimension binds: Whichever of batch tokens B, input width D, or output width F is smallest bottlenecks data reuse.
  • Batch reuse of weights: When B \ll D, F, fetching \mathbf{W} (bDF bytes) dominates DRAM traffic, and each weight is reused across all B batch rows.

One GPT Layer Spans a Factor of 1,000 in Intensity

Training vs. Decoding

  • Single-token decode (B = 1): Streams 8.4\text{ MB} of weights for a single use (I \approx 1.0\text{ FLOP/B}, deeply bandwidth-bound).
  • Batched decode (B = 64): Reuses weights across 64 streams (I \approx 60\text{ FLOP/B}), raising throughput \sim 60\times at the same step time.
  • Training (B = 8{,}192): Layer width D = 2048 becomes the smallest dimension (I \approx 910 \gg I^* = 164), saturating Tensor Cores.

The Roofline Bound and the Ridge Point

The Roofline Model

  • An operation with arithmetic intensity I runs no faster than memory delivers operands (I\beta) and no faster than the compute units execute arithmetic (P): \textrm{Throughput} \;\leq\; \min(P,\; I \beta)
  • Below the ridge (I < I^*): The kernel is bandwidth-bound; arithmetic units wait for DRAM, so fusing extra math onto loaded registers is nearly free.
  • Above the ridge (I > I^*): The kernel is compute-bound; arithmetic units are saturated, and only faster math formats or fewer FLOPs help.

Ridge Point on the RTX 4090: 164 FLOP/Byte in bf16

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}

  • Data-center accelerators have even higher ridge points: On an H100 (P = 989\text{ TFLOP/s}, \beta = 3.35\text{ TB/s}), I^* \approx 295\text{ FLOP/byte} because Tensor Core FLOP/s has scaled faster than HBM bandwidth.
  • Square matrix multiplication (B = D = F = n in 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.
  • Cache caveat: Here \beta is off-chip DRAM bandwidth; operands small enough to stay inside the 72\text{ MB} on-die L2 cache can exceed the DRAM bandwidth roof.

Asynchronous Dispatch and the GPU Timing Trap

Accurate Benchmarking

  • Asynchronous execution: Python enqueues kernel descriptors into the GPU driver queue and immediately advances to the next Python statement while the GPU works behind it.
  • Why naive timers lie: Starting and stopping 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}}.

Hidden Host-Device Barriers Serialize Execution

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 Properly with Warm-Up and Barriers

Benchmarking Discipline

  • d2l.Benchmark runs warm-up iterations first to absorb JIT compilation, cuBLAS handle creation, and kernel loading.
  • It then places explicit device barriers before starting and before stopping the clock across repeats calls.
  • Verification (4096 \times 4096 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\%.
for _ in range(warmup):       # load kernels, compile
    f()
wait_for_device()
t0 = time.perf_counter()
for _ in range(repeats):
    f()
wait_for_device()
seconds_per_call = (time.perf_counter() - t0) / repeats
4096 x 4096 product: 0.90 ms/call

Crossing the Ridge: One Matrix Reaches Both Roofs

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.

Elementwise Kernels Run at the Bandwidth 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
  • Arithmetic is free under the bandwidth roof: Because I \le 1 \ll 82\text{ FLOP/byte}, transcendental operations (sin, sigmoid) take the exact same \sim 0.6\text{ ms} (\sim 0.9\text{ TB/s}) as simple addition.

Eager Elementwise Chains Multiply Memory Traffic

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:

0.5 * x * (1 + tanh(0.8 * (x + 0.04 * x**3)))   # 6 unary + 2 binary = 8 ops
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).

Small Matrix Products Fall Far Below the Roofline

Size Sweep

  • Sweeping square bfloat16 products from n = 256 to 8192 tests whether compute saturation begins at the paper threshold n \approx 500 (I = n/3 = 164).
  • At n = 4096 and 8192, measured throughput approaches the 165\text{ TFLOP/s} roof.
  • For n \le 1024, however, throughput falls far below the bandwidth line and climbs with a log–log slope of 3—the signature of a constant time per call while FLOPs scale as 2n^3.

Host Dispatch Latency Dominates Small Operations

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.

The Three-Floor Time Bound and Crossover Knee

Unified Time Model

t \;\gtrsim\; \max\!\left(h,\; \frac{\textrm{FLOPs}}{P},\; \frac{\textrm{bytes}}{\beta}\right)

  • The dominant floor names the regime: Overhead-bound (h), compute-bound (\textrm{FLOPs}/P), or bandwidth-bound (\textrm{bytes}/\beta).
  • Overhead knee (n^* \approx (hP/2)^{1/3}): Square matmul compute overtakes host launch at n^* \approx 1{,}000 (h \approx 13\,\mu\text{s} per op).
  • Faster GPUs need wider matrices: A 14\times faster GPU raises n^* by 14^{1/3} \approx 2.4\times.

Diagnosing and Fixing the Three Performance Regimes

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

Predicting and Profiling a Complete MLP Training Step

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}):

  • Paper prediction (2.5\text{ ms}): Five large 1024 \times 4096 \times 4096 matmuls (2 forward, 2 weight-grad, 1 input-grad) at I \approx 340 > 82 take 5 \times \frac{2 \cdot 1024 \cdot 4096^2}{82.6 \times 10^{12}} = 5 \times 0.42 = 2.1\text{ ms}, plus 0.40\text{ ms} to stream 12\text{ B} \times 33.6\text{M} = 0.40\text{ GB} in the SGD update.
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.

Concept Check: Roofline Bounds and Overhead Knees

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:

  1. Elementwise addition x + 1 on 10^8 numbers (200\text{ MB} read + 200\text{ MB} write).
  2. Applying a 2048 \times 2048 linear layer (8.4\text{ MB} of weights) to a single token (B = 1).
  3. Multiplying two 4096 \times 4096 matrices (1.37 \times 10^{11}\text{ FLOPs}, 100\text{ MB} total traffic).

Answers:

  1. Bandwidth-bound (I = 0.25 < 164): Moves 4 \times 10^8\text{ B} at 1.0\text{ TB/s} \to t \ge 0.40\text{ ms} (\gg h = 10\,\mu\text{s}).
  2. Overhead-bound (h > \textrm{bytes}/\beta > \textrm{FLOPs}/P): Streaming 8.4\text{ MB} takes 8.4\,\mu\text{s} and compute takes 0.05\,\mu\text{s}, so host dispatch h binds \to t \ge 10\,\mu\text{s}.
  3. Compute-bound (I = 1365 > 164): 1.37 \times 10^{11}\text{ FLOPs} at 165\text{ TFLOP/s} \to t \ge 0.83\text{ ms} (vs. 0.10\text{ ms} memory floor).

Summary: The Roofline and Three-Regime Model

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