FAST: Factorizable Attention for Speeding up Transformers

Armin Gerami, Monte Hoover, Pranav S. Dulepet, Ramani DuraiswamiarXiv 2024 · DOI

Abstract

The quadratic computational and memory complexity of the self-attention mechanism is a significant bottleneck for scaling Transformers to long sequences. This paper introduces FAST (Factorizable Attention), a novel approach that replaces standard Softmax with Fastmax, a factorizable polynomial kernel. By rearranging the order of operations, FAST reduces complexity from $O(N^2)$ to linear $O(N)$ relative to sequence length $N$. Experimental results on the Long Range Arena (LRA) benchmark demonstrate that FAST achieves competitive accuracy with significant speedups, outperforming standard Transformers and other linear attention variants in long-context scenarios.

1. Introduction: The Quadratic Scaling Problem

The standard self-attention mechanism in Transformers requires every token to attend to every other token, resulting in a computational cost that scales quadratically ($O(N^2)$) with the sequence length $N$. This scaling makes processing long-context data—such as high-resolution images, long videos, or entire books—prohibitively expensive. While existing linear attention methods attempt to address this via kernels or sparsification, they often suffer from vanishing gradients or lose the global dependencies that define Transformer performance.

2. Methodology: Factorizable Attention (FAST)

The core innovation of FAST is the Fastmax mechanism, which approximates the standard Softmax function using a Taylor-series-inspired polynomial kernel: $f(x) = 1 + x + \frac{x^2}{2}$.

2.1 The Factorization Trick

In vanilla attention, the Query ($Q$) and Key ($K$) matrices are multiplied first to create an $N \times N$ attention matrix, which is then multiplied by the Value ($V$) matrix. Fastmax leverages the factorable nature of its polynomial kernel to rearrange these operations. By multiplying $K$ and $V$ first, the complexity is reduced to $O(N \cdot D^p)$, where $D$ is the head dimension and $p$ is the polynomial order.

Architecture of the Factorizable Attention (FAST) mechanism.

2.2 Stability and Regularization

FAST ensures training stability by mathematically bounding gradients, preventing the exploding or vanishing gradient issues common in other linear models. Additionally, because the $N \times N$ matrix is never explicitly constructed, the authors implemented Quadratic Dropout—applying dropout specifically to the quadratic terms of the factorization—to provide effective regularization.

3. Experimental Evaluation

The authors evaluated FAST across several benchmarks:

  • Long Range Arena (LRA): A rigorous suite of five tasks (ListOps, Text, Retrieval, Image, and Pathfinder) with sequences ranging from 1,000 to 4,000 tokens.
  • Tiny Shakespeare: Used for assessing text generation capabilities.
  • MNIST: For basic classification and visualization of attention maps.

4. Key Results and Insights

  • Linear Scaling: On an RTX A6000 GPU, FAST demonstrated clear linear scaling. It reaches a "break-even" point against standard Softmax at approximately 1,400 tokens for a Llama-2 sized model ($D=128$).
  • Competitive Accuracy: On the LRA benchmark, the second-order Fastmax (Fastmax2) achieved an average score of 57.90%, surpassing the Vanilla Transformer (57.37%) and comparable models like Performer (57.69%).
  • Efficiency Gains: In specific LRA tasks like "Retrieval" and "Text," Fastmax1 was 14x to 15x faster in training steps per second than the vanilla model.
  • Preservation of Inductive Biases: Visualization of attention maps confirmed that Fastmax learns structures similar to Softmax, such as diagonal patterns in text and columnar patterns in images, indicating it retains essential architectural priors.

5. Limitations and Future Work

While FAST offers significant advantages, certain challenges remain. The "masked" version required for autoregressive generation currently faces a $D \times$ slowdown on GPUs, which might be mitigated by custom gradient implementations. Furthermore, Fastmax attention tends to be slightly less localized than Softmax. Future research will focus on scaling FAST to multi-billion parameter models to evaluate its performance at the frontier of AI development.

Summary updated

BibTeX
@misc{gerami2024fast,
  author = {Armin Gerami and Monte Hoover and Pranav S. Dulepet and Ramani Duraiswami},
  title = {{FAST: Factorizable Attention for Speeding up Transformers}},
  year = {2024},
  eprint = {2402.07901},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.07901},
}
Figure