Skip to content

Making Deep Learning Go Brrrr From First Principles

optionalextendedfundamentalblogrubric 7/122022 · ~25 min

Horace He

Status

TL;DR. GPU time goes to one of three things: compute (doing FLOPs), memory bandwidth (moving tensors between global memory and the compute units) and overhead (everything else, such as Python and framework dispatch). Find which one dominates before you optimise, because each has a different fix.

An A100 offers 312 TFLOPs/s for matmuls on Tensor Cores but only 19.5 TFLOPs/s for anything else. In BERT, non-matmul operations are 0.2% of the FLOPs, yet normalisation and pointwise operations reach 250× and 700× fewer FLOPs/s than matmuls. The cause is bandwidth: an elementwise operator must read its input from global memory and write its result back, and with fp32 a simple unary operator needs about a hundred operations per element before compute becomes the limit.

So the levers differ by regime. Bandwidth-bound: fuse operators so intermediates never touch global memory. Overhead-bound: make each kernel bigger, or trace the program so the CPU stops dispatching every small operation. Compute-bound: make sure the FLOPs run as matmuls on Tensor Cores.

The post’s x * 2 microbenchmark makes the regimes visible. With few repeats the kernel saturates memory bandwidth and achieves about 0.2 TFLOPs/s. Achieved FLOPs grow linearly with repeats up to near the 9.75 TFLOPs/s peak, with the switch between about 32 and 64 repeats. On overhead, Python performs about 32 million additions per second, and PyTorch about 280 thousand operations per second on tiny tensors. In the time Python performs one FLOP, an A100 could perform 9.75 million.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Derive / compute

    The post quotes an A100 at 1.5 TB/s of global memory bandwidth and 19.5 TFLOPs/s of non-matmul compute. For an fp32 unary operator that reads and writes every element once, estimate how many operations per element are needed before compute rather than memory is the bottleneck. Then use the same reasoning to explain why a fused x.cos().cos() costs about the same as x.cos().

    What a strong answer covers
    • Each element moves 4 bytes in and 4 bytes out, so 1.5 TB/s supports about 1.5e12 / 8 ≈ 190 billion elements per second.
    • 19.5e12 operations per second divided by about 1.9e11 elements per second ≈ 100 operations per element. Below that the kernel is memory-bound, matching the post's "about a hundred operations".
    • Unfused, x.cos().cos() makes 4 global reads and writes (read x, write x1, read x1, write x2); fused, it makes 2. A second cos adds a few FLOPs, far below the ~100 needed to matter, so the fused cost is set by the 2 memory accesses alone.
    • Same logic explains why GELU costs about as much as ReLU, and why recomputation can lower both memory and runtime.
  2. Mechanism

    Describe the post's x * 2 repeat experiment and what its achieved-FLOPs and achieved-bandwidth curves show. How would you use the "double the batch size" test and the profiler to decide whether a small reranker's inference loop is overhead-bound, and what fix fits each of the three regimes?

    What a strong answer covers
    • Repeating x = x * 2 inside one fused kernel raises compute per byte without adding memory traffic. Below about 32 repeats bandwidth is saturated and compute idles; above about 64 compute is saturated near the 9.75 TFLOPs peak and achieved bandwidth falls. Achieved FLOPs start near 0.2 TFLOPs.
    • Overhead test: if doubling the batch raises runtime by much less than 2× (the post's example is about 10%), time is dominated by Python and framework dispatch, not GPU work. The profiler shows gaps on the GPU timeline while the CPU catches up.
    • Scale of overhead: Python manages about 32 million additions per second and PyTorch about 280 thousand tiny-tensor operations per second, versus 312 TFLOPs for A100 matmuls.
    • Fixes: overhead → larger batches, tracing (jit.trace, FX, jax.jit) or CUDA Graphs; bandwidth → operator fusion (NVFuser, XLA, Triton kernels); compute → move FLOPs onto Tensor Core matmuls.
  3. Critique

    The post uses one A100 and fp32 microbenchmarks to build its mental model. Critique which conclusions carry over to bf16 LLM inference on newer GPUs and which may not, using FlashAttention and speculative decoding as test cases.

    What a strong answer covers
    • Carries over: the three-regime split, the claim that non-matmul ops are a tiny share of FLOPs but a large share of time, and fusion as the bandwidth fix. FlashAttention is exactly this argument applied to attention.
    • Changes: the exact thresholds (about 100 ops per element, 32 or 64 repeats) depend on the FLOPs-to-bandwidth ratio of the chip and on the data type; halving bytes per element with bf16 moves them.
    • Single-stream LLM decoding is memory-bound on weights and KV cache, which is why speculative decoding trades extra FLOPs for fewer memory passes. At large serving batches the same step can become compute-bound.
    • Not covered: multi-GPU communication as a fourth cost, and the post gives no measurement of a full model, so it is a model for reasoning, not a benchmark.

Done with this page?

Status