↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Memory traffic, not arithmetic

Most of a transformer block's operations do very little arithmetic per byte: adding a bias, scaling, a GELU, the exponentials of a softmax. Run one at a time, each reads its whole input from memory and writes its whole output back, and the processor spends its time waiting for data. The roofline model says the same in one line: a kernel's speed is capped by , and an elementwise op has well under one FLOP per byte.

Fusion merges such ops into one kernel that loads each input once, does all the arithmetic while the values are in registers or cache, and stores only what is needed later. The intermediate tensors never reach memory.

From a graph to kernels

TorchInductor, torch.compile's default backend, receives the FX graph from Dynamo (the Graph capture tab) after AOTAutograd has decomposed it into small ATen ops: layer_norm becomes a mean and variance, a subtraction, a multiply by the reciprocal square root and the affine transform; GELU's tanh approximation becomes its multiplies, a cube and a tanh. The Before column lists the 88 ops of GPT-2's block 0 on 128 tokens, coloured by the kernel each one ends up in.

Inductor lowers each op to a loop body over its output's index space, inlining elementwise ops into their consumers as it goes, and its scheduler then fuses the remaining nodes: two nodes fuse when they iterate over compatible ranges and one consumes the other (vertical fusion) or they read the same data (horizontal fusion). Reductions fuse with the elementwise work before and after them, so a whole layer norm, or a mask-scale-softmax, is one loop nest with an inner reduction. Matrix multiplies are left to a library by default and act as boundaries: everything between two GEMMs becomes one or two generated kernels. The After column shows the result on the CPU: 6 generated C++ kernels and 6 library calls.

Counting the bytes

The bytes of each launch come from the code Inductor wrote: the kernel's signature says which pointers it reads (const float*) and writes (float*), and the wrapper's call() gives each buffer's shape. Counting each distinct buffer once, the Eager against compiled table splits the block in two. Outside the matrix multiplies, eager PyTorch runs 12 kernels that move 12.7 MB; Inductor replaces them with 6 generated kernels that move 9.1 MB. The GEMMs are the same library calls in both, 37.8 MB, most of it the weights.

Fewer bytes did not mean less time on this CPU. Timed one by one, the generated C++ kernels took longer than eager's ops for the same work (the first layer norm 0.175 ms against 0.045 ms), and the GEMMs dominate the block, so the Time per call chart shows compiled and eager within 17 % of each other at every length (16 tokens 0.84×, 64 tokens 0.96×, 128 tokens 1.02×, 256 tokens 1.17×, 512 tokens 1.13×, eager ÷ compiled). On the GPU, where every launch has a fixed cost, the Metal version is 1.07–1.38× faster across the five lengths.

Two backends

The same scheduler drives different code generators. On the CPU Inductor writes C++ with OpenMP loops over rows and at::vec::Vectorized<float> inner loops (four floats per NEON register on this machine), compiled by clang and loaded as a shared library. On the GPU it writes Metal shaders for Apple's MPS backend, with one thread per output element and threadgroup reductions. Toggle backend to compare; on both, matrix multiplies are library calls (Apple's Accelerate on the CPU, MPS on the GPU). On NVIDIA and AMD GPUs Inductor writes Triton instead, which does not run on macOS.

Where it came from

Loop fusion is an old compiler optimisation; Halide (Ragan-Kelley et al., 2013) made the trade-off between fusing (less memory traffic, sometimes recomputation) and not fusing an explicit schedule. XLA's fusion pass and TVM (Chen et al., 2018) brought it to deep learning graphs, and DNNFusion (Niu et al., 2021) widened what could be fused. FlashAttention (Dao et al., 2022) is a hand-written fusion of the attention block that no general fuser produced at the time. TorchInductor (Ansel et al., 2024) generates fused kernels for arbitrary PyTorch programs. The Tiling tab looks inside one kernel, and Autotuning at how Inductor chooses between kernels.

Papers

  1. PyTorch 2: Faster Machine Learning Through Dynamic Python Bytecode Transformation and Graph Compilation Jason Ansel et al. · ASPLOS 2024 TorchInductor: decomposition, lowering to loop bodies, the fusing scheduler and C++/Triton code generation.
  2. Halide Jonathan Ragan-Kelley et al. · PLDI 2013 Separating the algorithm from its schedule, and the locality–recomputation trade-off behind fusion decisions.
  3. TVM: An Automated End-to-End Optimizing Compiler for Deep Learning Tianqi Chen et al. · OSDI 2018 Graph-level operator fusion and per-operator code generation for many hardware back ends.
  4. DNNFusion: Accelerating Deep Neural Networks Execution with Advanced Operator Fusion Wei Niu et al. · PLDI 2021 Classifies ops by how their indices map, to decide which pairs can fuse.
  5. FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness Tri Dao et al. · NeurIPS 2022 A hand-fused attention kernel that never writes the score matrix to memory; IO-aware analysis of why it wins.
  6. Roofline Samuel Williams, Andrew Waterman, David Patterson · Communications of the ACM 2009 Why low-intensity kernels are bound by memory bandwidth, the model fusion works against.

See also