FlexLinearAttention: Compiling a Unified Abstraction into Scalable Kernels for Linear Attention

Haojie Duanmu, Size Zheng, Ningxin Zheng, Jianqiao Lu, Xuegui Zheng, Xingcheng Zhang, Li-Wen Chang, Xin Liu, Dahua LinICLR 2026

Standard "softmax" attention is the engine of the Transformer architecture, but it has a fatal flaw: its computational cost grows quadratically ($O(L^2)$) with sequence length. This makes processing long documents or massive codebases prohibitively expensive.

"Linear attention" variants (like Mamba, RetNet, and GLA) solve this by achieving $O(L)$ complexity. However, they face a severe fragmentation problem. While softmax attention has a gold standard, hyper-optimized implementation in FlashAttention, every new linear attention variant requires developers to manually write complex, hardware-specific kernels (often in Triton or CUDA) to be competitive. Furthermore, scaling these models across multiple GPUs is incredibly difficult because standard communication libraries aren't designed to fuse communication directly with the specific math of linear attention.

To solve this, researchers introduce FlexLinearAttention (FlexLA), a domain-specific compiler that automatically turns high-level PyTorch code for linear attention models into hyper-optimized, distributed kernels.

Core Methodology

FlexLA bridges the gap between high-level algorithmic research and low-level hardware optimization through three key innovations:

  1. A Unified Abstraction: FlexLA identifies that almost all linear attention variants can be decomposed into three canonical phases:
  2. Intra-chunk: Computing local state summaries within small segments of the sequence.
  3. Inter-chunk: Propagating that state across the sequence (the "scan" or "prefix sum" phase).
  4. Output Merging: Combining the global propagated state with local chunk results.
  5. Native Compute-Communication Fusion: Using a backend called Triton-Distributed, FlexLA generates kernels that overlap computation and communication at the tile level. This eliminates the need for the GPU to wait for the CPU to synchronize (host synchronization), a major bottleneck in distributed training.
  6. Targeted System Optimizations: The compiler uses Ahead-of-Time (AOT) compilation and a static dispatcher to bypass the significant runtime overhead of the Triton JIT compiler, which can otherwise dominate execution time for shorter sequences.
FlexLA Compiler Architecture and Workflow

Key Insights and Performance Results

The fundamental insight behind FlexLA is that the mathematical property of associativity allows linear attention to be expressed as a parallel scan. FlexLA exploits this to automate the "chunk-parallel" form that is usually written by hand. Standard distributed approaches often use "All-Gather" primitives that move more data than necessary; FlexLA instead generates custom communication patterns tailored to the specific state-update rule of the model, significantly reducing bandwidth pressure.

FlexLA proves that programmability versus performance is a false choice. A compiler with domain-specific knowledge can generate code that is faster than what experts write manually because it can explore a wider range of tiling and fusion strategies.

Evaluated across various architectures (HGRN, RetNet, Mamba2, GLA, GDN) on NVIDIA H100 and H20 GPU clusters, FlexLA demonstrated remarkable gains:

  • Single-GPU Speed: FlexLA kernels delivered 1.01x to 4.9x the performance of the state-of-the-art Flash-Linear-Attention (FLA) library.
  • Massive Scaling: On a cluster of 128 GPUs, FlexLA scaled to a sequence length of 16 million tokens with near-linear efficiency.
  • Distributed Superiority: For the Gated Delta Network (GDN), FlexLA outperformed the strongest open-source distributed baseline by up to 7.2x.
  • Latency Reduction: For a 256K sequence length on Scalar GLA, FlexLA reduced latency from 34.6 seconds (PyTorch eager) to just 9.2 milliseconds, and further down to 2.7 ms when distributed across 4 GPUs.
Code Abstraction and Latency Results

Limitations and Future Work

While highly effective for associative models, FlexLA's abstraction cannot currently support models that introduce non-linearities between state updates (like certain Test-Time Training layers), as these break the ability to use parallel scans. Additionally, the current implementation is built on Triton and primarily targets NVIDIA GPUs; extending this to other hardware like AMD or TPUs would require adapting the backend. Finally, while the backward pass can be implemented similarly, the primary focus of the current evaluation is on the forward pass (prefill phase).

Summary updated

BibTeX
@inproceedings{duanmu2026flexlinearattention,
  author = {Haojie Duanmu and Size Zheng and Ningxin Zheng and Jianqiao Lu and Xuegui Zheng and Xingcheng Zhang and Li-Wen Chang and Xin Liu and Dahua Lin},
  title = {{FlexLinearAttention: Compiling a Unified Abstraction into Scalable Kernels for Linear Attention}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=N4jJQvQSiN},
}
Figure