TL;DR
This paper introduces STOF, a novel framework designed to significantly accelerate the inference of sparse Transformers on GPUs. By combining highly optimized, flexible Multi-Head Attention (MHA) kernels with an adaptive operator fusion strategy, STOF achieves substantial speedups, making large language models more efficient.
What Problem Does This Paper Address?
Large Language Models (LLMs) have revolutionized natural language processing, but their computational demands, especially during inference, are a major bottleneck. Transformers, the backbone of LLMs, rely heavily on the Multi-Head Attention (MHA) mechanism. To reduce the computational cost of MHA, sparsity is introduced through masking. However, existing methods struggle with optimizing these sparse computations efficiently on GPUs. Current approaches often use static operator fusion schemes that can't adapt to the diverse masking patterns and varying model configurations, leading to suboptimal performance. This paper aims to bridge this gap by providing a unified framework that handles both flexible sparse attention patterns and adaptive operator fusion for efficient Transformer inference.
Methods
STOF tackles the problem through two main components:
- Unified MHA Module: This module offers specialized kernels for sparse MHA. It includes:
- Row-wise and Block-wise Kernels: These kernels are designed with unique storage formats to efficiently handle different sparsity patterns.
- Analytical Modeling: An analytical model helps select the most appropriate kernel (row-wise or block-wise) based on mask sparsity and sequence length, optimizing for data locality and synchronization.
- Two-Level Sparse Storage Format: Combines Block Compressed Sparse Row (BSR) and bitmap to represent arbitrary masks efficiently, reducing memory access and computation.
- Operator Fusion Module: This module focuses on optimizing the downstream operators (those following MHA) in the Transformer architecture.
- Fusion Scheme Conversion: It converts fusion schemes into compilation templates using numerical encoding and decoding.
- Hierarchical Search Engine: A two-stage search process is employed:
- Fusion Scheme Initialization: Uses neural hashing and predefined rules to discover representative subgraphs and derive an initial fusion scheme.
- Two-Stage Tuning: The first stage expands fusion boundaries using rules like "expand," "seize," and "compete" to find optimal operator groupings. The second stage fine-tunes kernel parameters for the determined fusion scheme. This process is guided by performance feedback and a caching mechanism to avoid redundant computations.
Datasets
The paper does not mention specific datasets used for training. For evaluation, it uses standard Transformer-based models: BERT (Base and Large), GPT2, LLaMA, T5, and ViT. The experiments are conducted on these models with varying configurations of batch size, sequence length, and masking patterns (causal, sliding window, Longformer, Bigbird).
Key Insights and Ideas
- Sparsity Optimization is Crucial: Simply applying masks isn't enough; specialized kernels are needed to exploit sparsity effectively.
- One Size Does Not Fit All: Static operator fusion schemes are insufficient for the diverse and dynamic nature of sparse Transformer computations.
- Co-design is Key: Combining optimized MHA kernels with adaptive operator fusion provides a synergistic performance boost.
- Hierarchical Search for Fusion: A multi-stage search approach is effective for navigating the complex space of fusion schemes and kernel parameters.
- Memory Locality Matters: The novel two-level sparse storage format significantly improves on-chip memory locality, leading to better performance.
Key Results
- MHA Performance: STOF achieves maximum speedups of 1.6× in MHA computation compared to state-of-the-art methods like FlexAttention on RTX 4090 and A100 GPUs.
- End-to-End Inference: STOF delivers maximum speedups of 1.4× in end-to-end inference across various models and configurations.
- Adaptive Fusion Benefits: The adaptive operator fusion component contributes significantly, especially at smaller input scales.
- Tuning Cost: STOF's tuning process is efficient, with its overhead being less than 3% of the total tuning time and significantly faster than other auto-tuning methods like MCFuser and Bolt.
- Scalability: STOF shows excellent performance scaling with longer sequence lengths, outperforming baselines and handling much larger inputs before encountering Out-of-Memory (OOM) errors.
Limitations and Future Work
- Hardware Specificity: While STOF is designed for GPUs, the paper notes preliminary tests on newer architectures (Hopper) show consistent improvements, suggesting universality. Future work includes extending evaluation to more architectures and specifically FA3.
- Dynamic Mask Patterns: STOF is inherently flexible for dynamic mask patterns. Future work aims to integrate dynamic pattern determination at runtime by extending the analytical model.
- Framework Integration: The authors plan to extend STOF to support PaddlePaddle and integrate it more transparently into compiler stacks.
- Operator Fusion Boundaries: The paper acknowledges that breaking the MHA boundary can sometimes lead to performance degradation, suggesting that preserving MHA as a distinct optimized unit is beneficial.
Summary updated