Kernel fusion combines multiple operations into one executable kernel, reducing launch and memory overhead. It is especially useful for pointwise operations such as cosine and ReLU, where separate passes can be limited by memory bandwidth. section 17.1.5
Compilation can expose several performance improvements:
- torch.compile can fuse model forwards into compiled regions and generate optimized GPU kernels. section 17.4
- Fusing operations reduces separate kernel invocations, intermediate memory use, and potentially memory-bandwidth pressure. section 17.1.5
- The related optimization of operator reordering can combine an addition with matrix multiplication to improve cache efficiency. section 17.1.5
Compiling a model
Wrap an existing model with torch.compile before inference: model = AutoModelForCausalLM.from_pretrained(checkpoint); model = model.to(device); model = torch.compile(model). The resulting speedup depends on hardware and model architecture, and some cases may become slower. section 17.1.5
Figure 17.5 Trace of the model execution after compilation
A post-compilation execution trace makes fusion visible: forwards appear under a compiled region, alongside fused GPU operations such as optimized linear-layer kernels. section 17.4

