This page documents the kernel selection and autotuning framework within PyTorch's TorchInductor backend. The process enables automatic selection and optimization of kernel implementations for fused operations during lowering and scheduling, choosing among various backends such as Triton, CUTLASS, ATen, and others.
Specifically, this page covers:
ChoiceCaller abstractions.At a high level, the kernel selection and autotuning flow starts from scheduled nodes (produced by the scheduler), and picks the optimal kernel implementation. Multiple candidate kernel templates are generated and benchmarked to select the best-performing variant. This process is cached persistently for future reuse by shape.
The flow is:
This process optimizes kernel choices such as GEMM implementations, pointwise operators, and fused subgraphs.
Sources:
torch/_inductor/select_algorithm.py45-70
torch/_inductor/autotune_process.py31-180
torch/_inductor/template_heuristics/triton.py104-156
torch/_inductor/runtime/triton_heuristics.py40-60
torch/_inductor/codecache.py40-56
Inductor manages multiple kernel template abstractions, each representing a backend or kernel generation strategy. The key types are:
| Template Type | Description | Primary Use | Source & Lines |
|---|---|---|---|
TritonTemplate | Jinja2 templates for Triton GPU kernels | GPU matrix multiplications, reductions | torch/_inductor/select_algorithm.py:68-74torch/_inductor/kernel/mm.py:87-110 |
ExternKernelChoice | Wrappers for ATen/cuBLAS/cuDNN external kernels | Stable fallback implementations | torch/_inductor/select_algorithm.py:72-78torch/_inductor/kernel/mm.py:140-155 |
CUTLASS2xGemmTemplate | CUTLASS 2.x library based GEMM templates for CUDA | CUDA GEMM kernels | torch/_inductor/kernel/mm.py:24 |
CKGemmTemplate | AMD Composable Kernel (CK) templates for ROCm | ROCm GEMM kernels | torch/_inductor/kernel/mm.py:26 |
CppGemmTemplate | C++ based gemm templates for CPU | CPU GEMM kernels | torch/_inductor/kernel/mm.py:15 |
SubgraphTemplate | Autotuned arbitrary FX GraphModules | Complex fused subgraphs | torch/_inductor/codegen/subgraph.py:60-70 |
These template classes provide a uniform interface for kernel rendering, launching, and benchmarking.
Kernel selection is built atop the abstract base class ChoiceCaller defined in the IR system. It defines the interface to:
Below is the ChoiceCaller subclass hierarchy used for various template types:
TritonTemplateCaller benchmarks Triton-generated kernels.ExternKernelCaller benchmarks ATen and other external kernels as a baseline.SubgraphChoiceCaller benchmarks arbitrary FX subgraphs compiled into modules.Sources:
torch/_inductor/select_algorithm.py56-80
torch/_inductor/codegen/subgraph.py60-75
torch/_inductor/ir.py40-42
Kernel templates are parameterized by configuration classes that specify properties such as block sizes, number of warps, and pipeline stages. These configurations define the search space for autotuning.
Typical config dataclasses include:
| Config Class | Description | Additional Fields | Source & Lines |
|---|---|---|---|
BaseConfig / GemmConfig | Base GEMM tiling and scheduling parameters | block_m, block_n, block_k, num_stages, num_warps | torch/_inductor/heuristics/template/triton.py:114-130 |
ROCmGemmConfig | AMD ROCm-specific GEMM config extension | matrix_instr_nonkdim, waves_per_eu, kpack | torch/_inductor/heuristics/template/triton.py:236-247 |
BlackwellGPUGemmConfig | NVIDIA Blackwell GPU GEMM with persistent TMA specifics | epilogue_subtile, warp_specialize, flatten, and meta-WS knobs | torch/_inductor/heuristics/template/triton.py:155-175 |
These configs define optimization parameters that heuristics prune or prioritize.
Heuristic classes generate candidate configurations for a given template, device type, and operator:
CUDAConfigHeuristicROCmConfigHeuristicXPUConfigHeuristicCPUConfigHeuristicHeuristics encapsulate logic on allowed parameter combinations, device properties, and degraded fallback choices.
The registry system allows registering and retrieving heuristics by (template_name, device_type, op_name) keys.
Sources:
torch/_inductor/template_heuristics/triton.py104-265
torch/_inductor/choices.py131-147
torch/_inductor/heuristics/registry.py40-140
The CachingAutotuner class manages autotuning lifecycle:
PersistentCache for previously benchmarked best configs.The key entrypoint is autotune_select_algorithm which:
AutotuneProcessPool.The autotuning is essential to achieve high performance by exploring diverse kernel configurations and backend implementations.
To isolate benchmarking overhead and ensure stable measurements, tuning is performed in separate subprocesses managed by:
TuningProcess: A single benchmark subprocess managing jobs via IPC pipes.AutotuneProcessPool: A pool of TuningProcess instances distributing benchmarking requests asynchronously.TritonBenchmarkRequest / ExternKernelBenchmarkRequest / SubgraphGPUBenchmarkRequest: Typed requests encapsulating kernel and input details to benchmark.The pool supports environment variable isolation (e.g., CUDA_VISIBLE_DEVICES) and cache synchronization.
Sources:
torch/_inductor/select_algorithm.py48-70
torch/_inductor/autotune_process.py1-180
torch/_inductor/runtime/triton_heuristics.py40-57
Inductor's MM kernel selection covers different backends and heuristics. It attempts candidate kernels from:
The main dispatching and autotune calls occur in torch/_inductor/kernel/mm.py with utilities like autotune_select_algorithm.
This allows leveraging hardware-specific backends with autotuning to fine-tune block sizes and launch parameters.
Sources:
torch/_inductor/kernel/mm.py84-165
torch/_inductor/select_algorithm.py48-70
Inductor supports autotuning for:
SubgraphChoiceCaller which compiles FX GraphModules and compares runtime performance [codegen/subgraph.py].CustomOpConfig specifying multiple decompositions with input-size based dispatch via RangeBounds, allowing multiple implementations for the same logical op [kernel/custom_op.py].This flexibility widens the autotuning capability beyond fixed kernels to arbitrary compositions and user-defined ops.
An auxiliary approach used in Inductor is padding matrix dimensions to multiples suitable for efficient kernels (e.g., aligning to 8 for float16). This enables optimized kernels from libraries or reduces the number of special cases.
Key points:
Sources:
torch/_inductor/fx_passes/pad_mm.py100-200
| Symbol | Location | Description |
|---|---|---|
autotune_select_algorithm | torch/_inductor/select_algorithm.py:49 | Main kernel selection and autotuning routine |
TritonTemplate | torch/_inductor/select_algorithm.py:68 | Triton kernel template wrapper |
ChoiceCaller | torch/_inductor/ir.py:40 | Abstract base class for kernel choice benchmarking |
CachingAutotuner | torch/_inductor/runtime/triton_heuristics.py:43 | Caches best kernel configs, manages tuning |
AutotuneProcessPool | torch/_inductor/autotune_process.py:31 | Manages parallel autotuning subprocesses |
BenchmarkTensors | torch/_inductor/select_algorithm.py:132 | Holds input/output tensors used during benchmarking |
SubgraphChoiceCaller | torch/_inductor/codegen/subgraph.py:60 | Autotunes complex subgraph kernels |
This completes the full technical overview of kernel selection and autotuning in TorchInductor.
Sources:
torch/_inductor/select_algorithm.py1-130
torch/_inductor/autotune_process.py1-180
torch/_inductor/template_heuristics/triton.py100-265
torch/_inductor/kernel/mm.py80-160
torch/_inductor/codegen/subgraph.py30-130
torch/_inductor/kernel/custom_op.py20-200
Refresh this wiki
This wiki was recently refreshed. Please wait 1 day to refresh again.