Skip to content
DARC
■ Change #37 · wiki.edited

Kernel autotuning and automated kernel generation: Normalize Markdown to canonical form

by DARC wiki2026-10-22 20:31:00 UTCOpen the page →
body
## Why tuning exists
For a given operator the best tile shape, number of pipeline stages, warps per block and loop order depend on tensor sizes, data types and the GPU model. Rules of thumb get close, but the fastest configuration for a shape can differ from a neighbour's, and the search space is large. Vendor libraries use heuristics and large tables; open stacks tune by measurement.
## Search-based approaches
- **Exhaustive or grid autotuning.** Triton's `@triton.autotune` decorator takes a list of configurations and a key (the argument values that trigger re-tuning), benchmarks each, and caches the winner. Simple and reliable, but each new shape costs compile and run time.
- **Learned cost models and search.** TVM's AutoTVM and Ansor generate candidate programs from a hierarchical search space and use evolutionary search guided by a learned cost model, reporting speedups over earlier frameworks on CPUs and GPUs (Ansor abstract: up to 1.7x on NVIDIA GPU against the state of the art at the time).
- **Heuristic selection in libraries.** cuBLAS and CUTLASS profilers rank precompiled kernels per problem.
Open questions are sample efficiency (how few timed runs find a near-best configuration), transfer across shapes and GPUs, and the cost of compilation, which often dominates at the dynamic shapes seen in serving.
## Generating kernels with language models
− KernelBench (Ouyang et al., 2025) asks language models to write GPU kernels for 250 PyTorch workloads and scores them with **fast_p**: the fraction of generated kernels that are correct and at least a factor p faster than the PyTorch baseline. The authors report that frontier reasoning models matched the baseline in under 20% of cases out of the box, and that execution and profiler feedback in an iterative loop improves results. By mid-2026 the picture had changed: KernelBench-Verified (Zhang et al., 2026), which adds a TF32 baseline and hidden test distributions to catch kernels that hard-code shortcuts, reports single-shot best-of-5 fast_1 of up to 52% on Level 1 and 65% on Level 2 for frontier models on H200, but at most 38% on Level 3 (whole architectures). Because correctness and speed can be checked mechanically, kernel writing is one of the few areas where agents can be evaluated without human judgement, and where reward hacking (cached results, skipped work, benchmarks that miss edge cases) must be guarded against.
+ KernelBench (Ouyang et al., 2025) asks language models to write GPU kernels for 250 PyTorch workloads and scores them with **fast\_p**: the fraction of generated kernels that are correct and at least a factor p faster than the PyTorch baseline. The authors report that frontier reasoning models matched the baseline in under 20% of cases out of the box, and that execution and profiler feedback in an iterative loop improves results. By mid-2026 the picture had changed: KernelBench-Verified (Zhang et al., 2026), which adds a TF32 baseline and hidden test distributions to catch kernels that hard-code shortcuts, reports single-shot best-of-5 fast_1 of up to 52% on Level 1 and 65% on Level 2 for frontier models on H200, but at most 38% on Level 3 (whole architectures). Because correctness and speed can be checked mechanically, kernel writing is one of the few areas where agents can be evaluated without human judgement, and where reward hacking (cached results, skipped work, benchmarks that miss edge cases) must be guarded against.
## What depends on it
Compilers that auto-tune determine whether researchers can write a new operator in a day and still get hardware-level speed; any agent that proposes kernels needs a trusted harness and baselines like these.
explainer.student
**Why tuning exists.** A GPU kernel has knobs: the tile size (how big a block of the matrix each group of threads handles), how many tiles to load ahead of time, and how many threads work on each tile. The best setting depends on the matrix sizes, the number format and the GPU model, and a setting that is best for a 4096 × 4096 multiply can be poor for a 1 × 4096 one.
**A worked count.** Suppose there are 3 tile heights, 3 tile widths, 4 tile depths, 4 load-ahead depths and 3 thread counts. That gives $3 \cdot 3 \cdot 4 \cdot 4 \cdot 3 = 432$ configurations. If each took half a second to compile and a tenth of a second to time, trying them all would take about four minutes, for one shape on one GPU. A serving system that meets hundreds of shapes cannot afford that while users wait.
**Approaches.**
− - *Grid search.* Triton's `@triton.autotune` decorator times each configuration in a list the programmer supplies and caches the winner for each shape. Reliable, but every new shape costs compile and run time.
− - *Learned cost models.* TVM and Ansor generate candidate programs, use a model trained on earlier measurements to predict which will be fast, and time only the promising ones, improving them by evolutionary search (keep good candidates, change them slightly, repeat). Ansor reports up to 1.7× speedups on NVIDIA GPUs over the best systems of its time.
− - *Vendor heuristics.* cuBLAS and CUTLASS choose among precompiled kernels using rules and tables.
+ - _Grid search._ Triton's `@triton.autotune` decorator times each configuration in a list the programmer supplies and caches the winner for each shape. Reliable, but every new shape costs compile and run time.
+ - _Learned cost models._ TVM and Ansor generate candidate programs, use a model trained on earlier measurements to predict which will be fast, and time only the promising ones, improving them by evolutionary search (keep good candidates, change them slightly, repeat). Ansor reports up to 1.7× speedups on NVIDIA GPUs over the best systems of its time.
+ - _Vendor heuristics._ cuBLAS and CUTLASS choose among precompiled kernels using rules and tables.
+
Analogy: choosing bicycle gears by test-riding every combination on the race course, versus asking an experienced coach to guess. The coach (the cost model) is far quicker, but can be wrong on an unfamiliar course (a new GPU).
**Language models writing kernels.** KernelBench (Ouyang et al., 2025) gives a language model 250 PyTorch programs and asks it to write faster GPU kernels. Its score is
− $$\mathrm{fast}_p = \frac{\text{tasks whose kernel is correct and more than } p \text{ times faster than PyTorch}}{\text{all tasks}}.$$
+ $\mathrm{fast}_p = \frac{\text{tasks whose kernel is correct and more than } p \text{ times faster than PyTorch}}{\text{all tasks}}.$
With $p = 1$, any correct kernel that is not slower counts. The paper reports that frontier models matched the PyTorch baseline in under 20% of tasks out of the box, and did better when shown compiler errors, test results and profiler output and allowed to try again.
**Why it is checkable, and the catch.** Correctness (compare the outputs with a reference) and speed (time it) are measured by a program, so no human judge is needed. That invites reward hacking: a generator may learn to pass weak tests, for example by returning cached answers or by timing incorrectly. Test harnesses therefore have to be designed as if an adversary were trying to fool them.
**Limits.** No method yet predicts a kernel's speed reliably without running it, tuned settings transfer imperfectly to new shapes and GPUs, and compile time often dominates the search.
hash cc15835f1d99068e54134186b2e51b278e7cd826ceb5f32f0e28a0734d78e23e
previous 62fd52b1cd9989ba896bd06b42923041612f7a4528eb14d4f97cb7b962f8a9a0