Abstract
Transformers and large language models (LLMs) have revolutionized machine learning, with attention mechanisms at the core of their success. As the landscape of attention variants expands, so too do the challenges of optimizing their performance, particularly across different hardware platforms. Current optimization strategies are often narrowly focused, requiring extensive manual intervention to accommodate changes in model configurations or hardware environments. In this paper, we introduce AttentionEngine, a comprehensive framework designed to streamline the optimization of attention mechanisms across heterogeneous hardware backends. By decomposing attention computation into modular operations with customizable components, AttentionEngine enables flexible adaptation to diverse algorithmic requirements. The framework further automates kernel optimization through a combination of programmable templates and a robust cross-platform scheduling strategy. Empirical results reveal performance gains of up to 10x on configurations beyond the reach of existing methods. AttentionEngine offers a scalable, efficient foundation for developing and deploying attention mechanisms with minimal manual tuning. Our code has been open-sourced and is available at https://github.com/microsoft/AttentionEngine.
Introduction
Attention mechanisms are the undisputed computational foundation of modern AI, yet they represent a significant performance bottleneck. In models like Llama-3B, attention can account for up to 82% of total compute time as sequence lengths grow. While specialized libraries like FlashAttention have revolutionized performance, they suffer from a "rigidity problem"—they are often handcrafted for specific hardware (primarily NVIDIA) and specific mathematical patterns (standard Softmax).
As researchers experiment with new variants—such as Sigmoid attention, Linear attention (e.g., Mamba2), or non-standard tensor dimensions (e.g., DeepSeek V2)—these handcrafted kernels often fail. Currently, developing a high-performance kernel for a new attention variant requires weeks of manual tuning by CUDA experts. AttentionEngine addresses this gap by providing an automated, flexible compiler-like framework that abstracts attention into modular components.
Methods: Unified Attention Abstraction
The core of AttentionEngine is a "Unified Attention Abstraction" that views all attention variants through two fundamental operations:
- Relevance Scoring ($Q \times K$): Determining how tokens relate to each other.
- Aggregation ($S \times V$): Consolidating information based on those scores.
Programming Interface and Templates
Users define their attention mechanism using high-level Python/C++ templates. The framework provides two primary patterns: the Parallel Pattern for standard transformer-style attention and the Recurrent Pattern for linear attention and State Space Models (SSMs). Users can plug in custom modification functions (for masking or scaling) and row-wise normalization functions (like Softmax, ReLU, or Sigmoid).
Automated Optimization Engine
AttentionEngine employs a two-layer scheduling policy to navigate complex GPU hardware trade-offs:
- Tile Config Scheduling: Automatically explores the best "tile" sizes to ensure the hardware's compute units stay fully utilized.
- Tile Resource Scheduling: Manages the memory hierarchy, deciding when to move data between global memory, shared memory, and registers to minimize latency.
The framework then lowers these high-level descriptions into optimized kernels for both NVIDIA (via CUTE/TileLang) and AMD (via ROCm) backends.
Evaluation and Results
The authors evaluated AttentionEngine using a comprehensive microbenchmark suite of modern attention operators, including standard Softmax (Llama 3.1, DeepSeek-V2), algorithmic variants (Sigmoid, ReLU, RetNet), and Linear/SSM variants (Mamba2, YOCO). Testing was performed on NVIDIA H100 and AMD Instinct MI250 GPUs.
Key Performance Insights
- Massive Speedups for Custom Ops: On configurations unsupported by existing libraries (like Sigmoid or ReLU attention), AttentionEngine achieved up to a 10.4× speedup over unoptimized baselines.
- Handling Atypical Shapes: Many libraries require head dimensions to be powers of two. AttentionEngine natively supports "weird" shapes, such as DeepSeek V2’s $d_{qk}=192$, achieving an average speedup of 1.88× (forward) and 1.52× (backward) compared to FlashAttention-v3.
- End-to-End Gains: In full model inference for DeepSeek-V2-Lite, the framework provided a 1.4× overall end-to-end speedup.
- Cross-Platform Parity: On the AMD MI250, AttentionEngine outperformed other baselines by an average of 3.3× for forward passes, bridging a major gap in the open-source hardware ecosystem.
Conclusion and Future Work
AttentionEngine demonstrates that semantic awareness—understanding the structure of attention rather than treating it as opaque matrix multiplication—allows for superior automated optimization. While currently focused on NVIDIA and AMD GPUs, future work includes extending support to other accelerators like TPUs or specialized AI NPUs. By replacing manual labor with an automated, flexible compiler-like framework, AttentionEngine provides a scalable foundation for the next generation of attention-based architectures.
Summary updated