Transformer Quality in Linear Time

Weizhe Hua, Zihang Dai, Hanxiao Liu, Quoc V. Le

TL;DR

FLASH (Fast Linear Attention with a Single Head) breaks the quadratic complexity bottleneck of Transformers by introducing a novel Gated Attention Unit (GAU) and a mixed chunk attention mechanism. It achieves linear scaling with sequence length while matching the quality of state-of-the-art augmented Transformers, delivering training speedups of up to 12.1$\times$ on long sequences.

What Problem Does This Paper Address?

Standard Transformers suffer from $O(T^2)$ complexity, where $T$ is the sequence length. This makes processing long contexts (like entire books or long documents) prohibitively expensive. While many "efficient" Transformer variants have been proposed, the authors identify three critical gaps:

  1. Inferior Quality: Most efficient models fail to match the performance of a "Transformer++" (a vanilla Transformer augmented with simple tweaks like Rotary Positional Embeddings and Gated Linear Units).
  2. Hardware Overhead: Theoretical linear complexity often doesn't translate to real-world speed on GPUs/TPUs because these models rely on irregular memory operations (like gather/scatter).
  3. Training Inefficiency: Linear attention methods often require sequential, RNN-style state updates during auto-regressive training, which fails to leverage the massive parallelism of modern accelerators.

Methods

The authors develop FLASH in two main steps:

1. The Gated Attention Unit (GAU)

Instead of trying to fix the standard Multi-Head Self-Attention (MHSA) layer, the authors propose a new building block. The GAU merges the Feed-Forward Network (FFN) and the Attention layer into a single unified module.

  • Gating as a Stabilizer: By using a gating mechanism (similar to GLUs), the model becomes less reliant on high-precision attention.
  • Simpler Attention: This robustness allows FLASH to use a "weaker" single-head, softmax-free attention mechanism without losing quality.
Figure 2: (a) An augmented Transformer layer which consists of two blocks: Gated Linear Unit (GLU) and Multi-Head Self-Attention (MHSA), (b) Our proposed Gated Attention Unit (GAU), (c) Pseudocode for Gated Attention Unit. Skip connection and input normalization over the residual branch are omitted in (a), (b) for brevity.

2. Mixed Chunk Attention

To achieve linear complexity, FLASH employs a hybrid attention strategy:

  • Local Quadratic Attention: The sequence is divided into non-overlapping chunks (e.g., size 256). Within each chunk, standard quadratic attention is used for high precision.
  • Global Linear Attention: Long-range dependencies across chunks are captured using a linearized attention mechanism.
  • Accelerator-Friendly Training: By chunking, the sequential "cumulative sum" bottleneck of standard linear attention is reduced from $T$ steps to $T/C$ steps (where $C$ is chunk size), significantly speeding up training on parallel hardware.
Comparison of quadratic, linear, and mixed chunk attention.

Datasets

The model was evaluated on several large-scale benchmarks:

  • Wiki-40B: For auto-regressive language modeling.
  • PG-19: A dataset of books used to test long-context (up to 8K tokens) modeling.
  • C4: Used for masked language modeling (MLM).
  • TriviaQA: Used to evaluate downstream fine-tuning performance on reading comprehension.

Key Insights and Ideas

  • Layer Design Matters More Than Approximation: The core "aha moment" is that designing a layer that is naturally easy to approximate (the GAU) is more effective than trying to approximate the complex MHSA layer directly.
  • Gating Weakens the Need for Attention: The gating mechanism effectively "alleviates the burden" of self-attention, allowing a single head to perform as well as the 12+ heads found in standard models.
  • Chunking is the Practical Path to Linear Time: By combining local quadratic and global linear attention, FLASH avoids the memory re-formatting overhead that plagues sparse attention models while maintaining the speed of linear models.

Key Results

FLASH demonstrates that you don't have to trade quality for speed:

  • Massive Speedups: On the PG-19 dataset with an 8K context, FLASH achieved a 12.1$\times$ training speedup over the baseline Transformer+ while reaching the same perplexity.
  • Competitive Quality: In head-to-head comparisons on Wiki-40B, FLASH matched or outperformed the highly optimized Transformer++ across all context lengths.
  • Scaling Efficiency: As shown in Figure 1, the speedup relative to vanilla Transformers grows dramatically as the sequence length increases, reaching 25.6$\times$ at a length of 8192.
Figure 1: TPU-v4 training speedup of FLASH relative to the vanilla Transformer (TFM) and an augmented Transformer (TFM++) for auto-regressive language modeling on Wiki- 40B — All models are comparable in size at around 110M and trained for 125K steps with 218 tokens per batch.

Limitations and Future Work

  • Scaling Laws: The experiments were conducted on models up to 500M parameters. The authors suggest that investigating how these design choices scale to multi-billion parameter "Large Language Models" is a vital next step.
  • Hyperparameter Sensitivity: The choice of chunk size ($C$) is a new hyperparameter that impacts the quality-speed trade-off, requiring task-specific tuning for optimal results.
  • Downstream Breadth: While TriviaQA results were strong, further testing on a wider variety of downstream tasks (GLUE, SuperGLUE, etc.) would further validate the generalizability of the GAU architecture.

Summary updated

BibTeX
@misc{hua2022transformer,
  author = {Weizhe Hua and Zihang Dai and Hanxiao Liu and Quoc V. Le},
  title = {{Transformer Quality in Linear Time}},
  year = {2022},
  eprint = {2202.10447},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2202.10447},
}
Figure