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

Measure instead of predict

A compiler usually has several correct ways to run an operation: a vendor library or its own generated kernel, and for its own kernel many tile sizes, loop orders and thread splits. A cost model can rank them, but real hardware has effects models miss (cache conflicts, prefetchers, the exact instruction mix), so the best choice for one shape on one machine is often found faster by trying them. Autotuning compiles each candidate, times it on inputs of the real shape, and keeps the fastest.

torch.compile(mode="max-autotune") turns this on in Inductor. Every time it lowers a matrix multiply it builds a list of candidate kernels, benchmarks them while compiling, and writes the winner into the generated code.

Inductor's candidates on this CPU

On the CPU, with weights frozen as constants, each Linear of GPT-2's block 0 gets two candidates: ATen's addmm, which here calls Apple's Accelerate BLAS, and Inductor's own C++ GEMM template (the Tiling tab), compiled for the exact shapes with the bias added in the same loops. The Who won grid shows the verdict for the four Linears at five sequence lengths, recorded from Inductor's feedback hook as it tuned: at 1 token ATen wins every Linear; at 8 tokens Inductor's template wins all four, by 1.9–3.7×; at 32 it wins three of four by smaller margins; at 128 and 512 tokens ATen wins everything. Attention's att @ v product also gets both candidates and ATen keeps it each time; q @ kᵀ reads a transposed view the template does not take, so it is never tuned. The number under each verdict is how much slower the runner-up was.

Pick a cell to see its candidates' measured times. They come from Inductor's own benchmark in the compiling process, on random data of the real shape, a few dozen milliseconds per candidate.

What it costs and what it buys

Benchmarking is not free: each template candidate has to be generated and compiled by clang before it can be timed, so the first compile at 128 tokens took 10.7 s against 0.6 s when Inductor compiled the same shapes again and found its verdicts in the autotune cache on disk. The Did it pay? table compares the block's time per call under eager, the default compile and max-autotune at each length. Where the template wins, it pays: at 8 tokens the max-autotune block runs in 1.03 ms against 3.59 ms for the default compile and 3.23 ms eager, because Accelerate took longer for these 8-row products than for 32-row ones (0.46 against 0.29 ms for attn.c_attn). At 128 and 512 tokens the autotuner keeps ATen everywhere, so max-autotune and the default compile run the same GEMMs and take the same time.

The knobs Inductor does not tune

Inductor times whole kernels against each other but not the template's internal choices: the register tile comes from a score that prefers sizes dividing the shapes, and the cache blocks from the L1/L2 rule on the Tiling tab. Its source has TODO notes to tune them. The wider search does what an autotuner over those knobs would: every combination of 3 register tiles, 5 values of and 4 thread grids for mlp.c_fc at 128 tokens, 61 kernels in all, each compiled and timed; the finalists are timed again with more rounds. The register tile is what separates them: every 4 × 24 configuration (fastest rounds 0.81–0.90 ms) beat every 8 × 8 one (1.12–1.31 ms), with 4 × 16 in between. Cache block and thread grid moved the time far less. Inductor's own pick was among the finalists; re-timed, it came within 2 % of the best found (0.835 against 0.815 ms), less than either's spread between rounds. ATen's GEMM, at 0.58 ms, beat all 61: the reason the autotuner keeps ATen at this length.

The spread column of The finalists shows the other half of autotuning: when candidates are within a few per cent of each other, run-to-run noise on a busy machine can reorder them, so a tuner has to time enough rounds, or keep a margin, before it trusts a winner.

Where it came from

Empirical tuning of numerical libraries started with ATLAS (Whaley and Dongarra, 1998), which searched GEMM block sizes at install time, and FFTW (Frigo and Johnson, 1998), which times FFT plans at run time. OpenTuner (Ansel et al., 2014) made search over program configurations a general tool. For deep learning, AutoTVM (Chen et al., 2018) learned a cost model to steer the search over schedules, and Ansor (Zheng et al., 2020) generated the schedules themselves. Inductor's max-autotune is a deliberately small version: a handful of templates and library calls per op, benchmarked directly, with results cached by shape.

Papers

  1. Automatically Tuned Linear Algebra Software R. Clint Whaley, Jack J. Dongarra · SC 1998 ATLAS: generate GEMM kernels with many block sizes and keep the fastest on the installing machine.
  2. FFTW: an adaptive software architecture for the FFT Matteo Frigo, Steven G. Johnson · ICASSP 1998 Timing candidate plans at run time and remembering the winner (wisdom), as Inductor's autotune cache does.
  3. Learning to Optimize Tensor Programs Tianqi Chen et al. · NeurIPS 2018 AutoTVM: a learned cost model that decides which schedules are worth timing.
  4. Ansor: Generating High-Performance Tensor Programs for Deep Learning Lianmin Zheng et al. · OSDI 2020 Searching a generated space of tile structures instead of hand-written templates.
  5. OpenTuner Jason Ansel et al. · PACT 2014 Ensembles of search techniques over a program's configuration space.
  6. PyTorch 2: Faster Machine Learning Through Dynamic Python Bytecode Transformation and Graph Compilation Jason Ansel et al. · ASPLOS 2024 TorchInductor, whose max-autotune mode benchmarks templates against library calls.

See also