Flashlight: PyTorch Compiler Extensions to Accelerate Attention Variants

Bozhi You, Irene Wang, Zelal Su Mustafaoglu, Abhinav Jangda, Angélica Moreira, Roshan Dathathri, Divya Mahajan, Keshav PingaliarXiv 2025 · DOI

Abstract

Flashlight is a compiler-native framework for PyTorch designed to automatically generate highly optimized, FlashAttention-style kernels for diverse attention variants directly from standard Python code. By extending the PyTorch compiler with advanced fusion techniques and algebraic transformations, Flashlight eliminates the need for hand-written custom kernels or restrictive templates. It matches or exceeds the performance of existing solutions like FlexAttention while supporting complex mechanisms such as Differential Attention and AlphaFold's Evoformer gated attention.

Introduction

Attention mechanisms are central to modern Large Language Models (LLMs), yet their standard implementations often suffer from high memory consumption and latency. While FlashAttention addressed these issues through operation fusion and tiling, it remains a rigid, hand-optimized kernel limited to "vanilla" attention. As researchers develop novel variants—such as ALiBi, Differential Attention, or gated attention—they often face a significant engineering hurdle: writing custom Triton or CUDA kernels.

Existing tools like PyTorch's FlexAttention offer some relief but rely on static programming templates that cannot accommodate all architectures. Furthermore, the default PyTorch compiler (torch.compile) often fails to fuse complex operations across matrix multiplication boundaries. Flashlight addresses these gaps by making the PyTorch compiler smarter about matrix math and reductions, enabling the automatic generation of optimized kernels for any attention variant.

Methods

Flashlight enhances the PyTorch compiler, specifically the TorchInductor backend, through several key innovations that allow it to analyze and optimize idiomatic PyTorch code.

Overview of the Flashlight compilation workflow within the PyTorch stack, highlighting the TorchInductor extensions.

The framework introduces several novel compiler passes:

  • Unified Reduction IR: Flashlight treats Matrix Multiplication (GEMM) as a generalized reduction operation rather than an opaque library call. This allows the compiler to fuse operations both before and after the matrix multiplication.
  • Structural Fusion with Dimension Demotion: By "demoting" a parallel dimension into a sequential reduction loop, Flashlight can fuse producer and consumer kernels, significantly reducing memory latency by keeping intermediate tensors on-chip.
  • Semantic Fusion with Algebraic Transformation: Flashlight leverages algebraic homomorphisms (ring theory) to transform multi-pass reductions, such as stable softmax, into single-pass "online" algorithms, enabling deeper kernel fusion.
  • Tiling-Aware Dimension Elimination: By identifying dimensions small enough to fit within hardware tiles, Flashlight can eliminate specific loops, allowing the fusion of the two core matrix multiplications in attention ($QK^T$ and the subsequent $V$ multiplication) into a single kernel.

Results

Flashlight was evaluated on NVIDIA H100 and A100 GPUs across a variety of attention benchmarks, including those supported by FlexAttention (e.g., ALiBi, Softcap) and complex variants that are not (e.g., Differential Attention, Evoformer).

Key findings include:

  • Competitive Performance: On variants supported by FlexAttention, Flashlight matches or exceeds performance, achieving up to 1.48x speedups on certain configurations by avoiding template overhead.
  • Superiority on Complex Variants: For models like Differential Attention and Evoformer, Flashlight provides speedups of 5x or more compared to standard torch.compile.
  • Real-World Application: In AlphaFold2 inference, Flashlight reduced end-to-end latency by 6% to 9% on both H100 and A100 GPUs.

Conclusion

Flashlight demonstrates that a compiler-centric approach can replace restrictive templates and manual kernel engineering for attention optimization. By encoding mathematical properties and breaking the "black box" of matrix multiplication, Flashlight enables researchers to experiment with novel attention mechanisms without sacrificing performance. Future work will focus on integrating runtime sparsity inspection to further optimize computations for sparse attention patterns.

Summary updated

BibTeX
@misc{you2025flashlight,
  author = {Bozhi You and Irene Wang and Zelal Su Mustafaoglu and Abhinav Jangda and Angélica Moreira and Roshan Dathathri and Divya Mahajan and Keshav Pingali},
  title = {{Flashlight: PyTorch Compiler Extensions to Accelerate Attention Variants}},
  year = {2025},
  eprint = {2511.02043},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2511.02043},
}
Figure