Transformer Quality in Linear Time

Weizhe Hua, Zihang Dai, Hanxiao Liu, Quoc V Le · OpenReview

Abstract

We revisit the design choices in Transformers, and propose methods to address their weaknesses in handling long sequences. First, we propose a simple layer named gated attention unit, which allows the use of a weaker single-head attention with minimal quality loss. We then propose a linear approximation method complementary to this new layer, which is accelerator-friendly and highly competitive in quality. The resulting model, named FLASH, matches the perplexity of improved Transformers over both short (512) and long (8K) context lengths, achieving training speedups of up to 4.9× on Wiki-40B and 12.1× on PG-19 for auto-regressive language modeling, and 4.8× on C4 for masked language modeling.

Introduction: The Quadratic Bottleneck

Standard Transformer architectures have revolutionized natural language processing but are fundamentally limited by a "quadratic bottleneck." The computational complexity and memory requirements of the self-attention mechanism grow with the square of the sequence length ($O(T^2)$). This scaling behavior makes processing long-form content—such as entire books, long legal documents, or extensive codebases—prohibitively expensive.

While numerous "efficient" or "linear" Transformers have been proposed to mitigate this, they frequently suffer from three critical drawbacks:

  1. Quality Degradation: They often fail to match the performance of a well-tuned "Transformer++" baseline (vanilla Transformer with Gated Linear Units and Rotary Embeddings).
  2. Hardware Inefficiency: Theoretical linear complexity often fails to translate into actual speedups on modern hardware (GPUs/TPUs) due to non-optimal memory access patterns.
  3. Training Latency: Many linear methods are optimized for inference but remain slow during training, particularly for auto-regressive tasks.

The FLASH Architecture

To overcome these challenges, the authors introduce FLASH (Fast Linear Attention with a Single Head). The core innovation lies in rethinking the fundamental building block of the Transformer.

The Gated Attention Unit (GAU)

Instead of the traditional alternating stack of Multi-Head Attention (MHA) and Feed-Forward Networks (FFN), FLASH utilizes a unified Gated Attention Unit (GAU). The GAU uses a gating mechanism (similar to a Gated Linear Unit) as its primary backbone.

A key insight of this work is that a sufficiently powerful gating mechanism can handle much of the feature transformation, allowing the attention component to be significantly simplified. In GAU, the complex multi-head attention is replaced by a single-headed, softmax-free attention mechanism. This simplification maintains model quality while drastically reducing computational overhead.

Comparison of the standard Transformer block vs. the Gated Attention Unit (GAU) with implementation details.

Mixed Chunk Attention

To achieve true linear scaling ($O(T)$) for long sequences, FLASH employs a "mixed chunk" strategy. The input sequence is divided into non-overlapping chunks.

  • Local Attention: Within each chunk, the model applies standard quadratic attention to capture fine-grained local dependencies.
  • Global Attention: Between chunks, a linearized attention mechanism is used to capture long-range context.

This hybrid approach is specifically designed to be "accelerator-friendly." By chunking the data, the model breaks the sequential dependencies inherent in many linear attention methods, allowing for massive parallelization on modern hardware.

Experimental Evaluation

The researchers evaluated FLASH against a rigorous "Transformer++" baseline across several large-scale datasets:

  • C4: For bidirectional masked language modeling.
  • Wiki-40B: For auto-regressive language modeling.
  • PG-19: A dataset of books (average length ~69k tokens) specifically for testing long-range dependencies.
  • TriviaQA: For downstream reading comprehension performance.

Key Results and Discussion

FLASH demonstrates that it is possible to achieve linear scaling without sacrificing the quality of state-of-the-art quadratic models.

Performance Parity and Speedup

FLASH is the first linear-complexity model to achieve perplexity parity with fully-augmented Transformers (TFM++) across context lengths from 512 to 8192 tokens. On the PG-19 dataset with an 8K context length, FLASH achieved a remarkable 12.1x training speedup over the baseline.

Training speedup of FLASH compared to TFM and TFM++ baselines across different sequence lengths.

Efficiency and Fine-tuning

Even the quadratic version of the model, FLASH-Quad, outperforms standard Transformers due to the inherent efficiency of the GAU architecture. In downstream tasks like TriviaQA, FLASH matched the F1 scores of the TFM++ baseline while being 2.7x to 2.8x faster to train and fine-tune.

Conclusion

FLASH represents a significant step forward in efficient Transformer design. By unifying gating and attention into a single GAU layer and utilizing a hardware-aware mixed-chunk attention strategy, it successfully breaks the quadratic bottleneck. Future work will involve exploring the scaling laws of FLASH for models with billions of parameters and expanding its application to a wider variety of multimodal tasks.

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