PyTorch: How torch.compile achieves up to 10× speedup through kernel fusion
The PyTorch compiler torch.compile achieves up to 10× speedup on GPUs by eliminating redundant writes to global memory through kernel fusion. Instead of separate GPU kernels for each operation, the Inductor compiler automatically merges dependent operations into a single kernel that keeps intermediate values in the processor's fast registers.
This article was generated using artificial intelligence from primary sources.
PyTorch’s compiler torch.compile achieves speedups of up to 10× on GPUs without a single change to the model code. The key mechanism behind this speedup is kernel fusion — a technique for merging multiple operations into a single GPU kernel to eliminate costly writes of intermediate values to global memory.
Why does GPU memory slow down model training?
GPU computing does not charge a uniform cost for all operations. Launching each GPU kernel carries a fixed overhead, and every write of intermediate values to global memory is hundreds of times slower than operations in the processor’s own registers. Without a compiler, PyTorch executes each operation (multiply, add, sigmoid) as a separate kernel with its own writes and reads to and from global memory.
Concretely, three consecutive pointwise operations (x * w, + b, .sigmoid()) without fusion require 5 reads and 3 writes — a total of 8 full tensor I/O operations. After kernel fusion: 3 reads + 1 write = 4 operations, which is a 50% reduction in memory traffic.
How does torch.compile automatically fuse kernels?
torch.compile uses the Inductor compiler, which analyzes the computation graph and applies multiple types of fusion:
- Vertical fusion (most common): merges dependent operations so that intermediate values remain in GPU registers. Eliminates all temporary tensors that would otherwise need to go into global memory.
- Pointwise fusion: merges element-wise operations (
multiply → add → sigmoid) into a single Triton kernel. - Reduction fusion: combines reduction operations (e.g., summations) with surrounding operations — critical for batch normalization.
- GEMM + Epilogue fusion: adds bias and activation functions directly at the end of matrix multiplication.
- Horizontal fusion: executes independent operations on the same input (e.g.,
sin(x)andcos(x)) in parallel within a single kernel.
What happens to kernels in practice?
The Triton kernel generated by fusion keeps all intermediate values in GPU registers and only writes the result at the end:
Load x, Load w, Load b
→ tmp = x * w (stays in registers)
→ tmp = tmp + b (stays in registers)
→ tmp = sigmoid(tmp)(stays in registers)
Store result (the only write to global memory)
Inductor automatically names fused kernels using the convention triton_poi_fused_* (pointwise) and triton_per_fused_* (reduction), which is visible by setting the TORCH_LOGS="output_code" variable.
Results and application
The PyTorch community documents speedups of up to 10× in real models thanks to the combination of fusion, elimination of kernel launch overhead, and reduced memory traffic. All that is needed is a single line of code:
model = torch.compile(model)
The Inductor compiler then independently builds the optimal Triton or CUDA kernel, without the need for manual GPU code writing. This automation makes torch.compile one of the most important tools for production ML training and inference in the PyTorch ecosystem.
Frequently Asked Questions
- What is kernel fusion in PyTorch and why does it speed up training?
- Kernel fusion is a technique for merging multiple GPU operations into a single kernel, so that intermediate values remain in fast registers instead of being written to global memory. This reduces the number of memory operations and kernel launches, which are the most common GPU computing bottlenecks.
- Do you need to change code for torch.compile to work?
- No. It is sufficient to add the torch.compile() decorator — the Inductor compiler automatically detects and applies all types of kernel fusion without any changes to the model.
- How much exactly does kernel fusion reduce memory traffic?
- In a typical pointwise fusion example, the number of memory operations drops from 8 to 4 (50% reduction), and the number of GPU kernels from 3 to 1. Total speedup can reach 10× depending on model architecture.
📬 AI news in your inbox
A daily digest built your way — pick topics, sources and cadence. One-click unsubscribe.
Related news
CNCF: HAMi becomes Incubating project — GPU virtualization for Kubernetes AI workloads
LF AI & Data: 'context is the new bottleneck' — open infrastructure Docling and DocLang
PyTorch: Triton 3.7 brings plugin extensions and TLX — up to +15% throughput on AMD MI350