FLatten Transformer: Vision Transformer using Focused Linear Attention

Dongchen Han, Xuran Pan, Yizeng Han, Shiji Song, Gao Huang

TL;DR

The FLatten Transformer introduces a novel "Focused Linear Attention" module that overcomes the performance gap between efficient linear attention and standard softmax attention. By sharpening feature focus and restoring matrix rank through two simple, lightweight components, it achieves state-of-the-art accuracy across vision tasks while being up to 2.1x faster than traditional Transformers.

What Problem Does This Paper Address?

Transformers have revolutionized computer vision, but they come with a heavy "tax": the quadratic computation complexity of self-attention. As the number of image tokens ($N$) grows, the cost of computing pairwise similarities between every token explodes at a rate of $O(N^2)$. This makes standard Transformers prohibitively expensive for high-resolution images or long-range modeling.

"Linear attention" was proposed as a fix, reducing complexity to $O(N)$ by changing the order of matrix multiplications. However, it historically suffered from a significant performance drop. The authors of this paper identify two primary reasons for this decline:

  1. Poor Focus Ability: Unlike Softmax, which creates "sharp" attention maps that zero in on important objects, linear attention produces "blurry" maps that average out all features.
  2. Low Feature Diversity: Mathematically, the attention matrix in linear attention is bottlenecked by the head dimension ($d$). Since $d$ is usually much smaller than the number of tokens $N$, the resulting features become homogenized and lose diversity.

Methods

The authors propose the Focused Linear Attention (FLatten) module to solve these issues without losing linear efficiency. As shown in Figure 1, the core idea is to decouple the Softmax operation and compute the key-value product first.

Figure 1 illustrates how linear attention changes the computation order to achieve $O(Nd^2)$ complexity.

To fix the identified flaws, they introduce two key innovations:

  • Focused Function ($f_p$): This simple mapping function adjusts the direction of query and key vectors. By applying an element-wise power ($p$), it "pulls" vectors toward their nearest axes, effectively sharpening the attention distribution. This allows the model to focus on informative regions, mimicking the behavior of Softmax attention (see Figure 4).
  • Rank Restoration (DWC): To combat feature homogenization, the authors add a lightweight Depthwise Convolution (DWC) to the attention output. From a linear algebra perspective, this adds a sparse, potentially full-rank matrix to the low-rank linear attention matrix. As demonstrated in Figure 5, this restores the attention map to full rank, ensuring that different image positions maintain unique, diverse features.
Figure 5. Attention map (196×196) from the 3rd block of DeiT- tiny. (a) Softmax attention can learn a full-rank attention map. (b) Linear attention can not learn an attention map with a rank greater than head dim 64. Many rows of the attention map are seriously homogenized, resulting in the resemblance among output features. (c) The lightweight DWC helps linear attention learn an equivalent attention map with a high rank and maintain feature diversity. Both (b) and (c) involve focused function fp.

Datasets

The FLatten Transformer was rigorously evaluated on three major benchmarks:

  • ImageNet-1K: For large-scale image classification.
  • ADE20K: For semantic segmentation, testing the model's ability to handle dense, pixel-level predictions.
  • COCO: For object detection and instance segmentation, evaluating performance in complex scenes.

The module was integrated into several popular backbones, including DeiT, PVT, PVTv2, Swin, and CSwin, to demonstrate its broad applicability.

Key Insights and Ideas

  • Direction Matters More Than Magnitude: The Focused Function $f_p$ is designed to preserve the norm of the feature vectors while only adjusting their direction. This subtle change is enough to "sharpen" the attention map significantly.
  • The Rank Bottleneck is Real: The paper provides a clear mathematical explanation for why linear attention feels "limited"—it literally cannot represent complex, diverse relationships because its matrix rank is capped by the small channel dimension.
  • Hybrid Efficiency: By using linear attention, the authors can afford a global receptive field even in the early stages of a network, where standard Softmax Transformers are forced to use small local windows to save on computation.

Key Results

The FLatten Transformer consistently outperforms both standard Softmax baselines and other state-of-the-art linear attention methods (Figure 2).

Figure 2. Comparison of different linear attention designs on DeiT- Tiny and Swin-Tiny structures.
  • Accuracy Gains: On ImageNet-1K, FLatten-DeiT-T achieved 74.1% Top-1 accuracy, a +1.9% improvement over the standard DeiT-T, despite having lower computational complexity.
  • Efficiency: The model achieves up to 2.1x faster inference on hardware like the RTX3090 and standard CPUs compared to Softmax-based counterparts.
  • Downstream Success: In semantic segmentation on ADE20K, FLatten-PVT-T improved mIoU by 0.64% over the baseline with similar FLOPs.
  • Qualitative Sharpness: Visualizations in Figure 3 show that FLatten produces attention maps nearly identical in "sharpness" to Softmax, whereas vanilla linear attention remains blurry.
Figure 3. The distribution of Softmax attention, linear attention and our focused linear attention from DeiT-tiny. Softmax attention can produce sharp distribution, while linear attention’s distribution is relatively smooth. Our module restores the sharp distribution as the original Softmax attention. Feature corresponding to the red block is used as query. See more visualizations in Appendix.

Limitations and Future Work

While the paper demonstrates impressive results, a few areas remain for exploration:

  • Hyperparameter Sensitivity: Although the authors state the focus factor $p$ is robust (testing values from 2 to 32), finding the optimal $p$ for entirely new architectures might require some tuning.
  • Hardware Optimization: While the theoretical complexity is linear, the practical speedup depends on how well depthwise convolutions and transposed matrix multiplications are optimized in specific deep learning libraries.
  • Scaling to LLMs: The current work focuses on vision. A natural next step would be applying these "focusing" and "rank restoration" techniques to large language models (LLMs) to handle massive context windows more efficiently.

Summary updated

BibTeX
@misc{han2023flatten,
  author = {Dongchen Han and Xuran Pan and Yizeng Han and Shiji Song and Gao Huang},
  title = {{FLatten Transformer: Vision Transformer using Focused Linear Attention}},
  year = {2023},
  eprint = {2308.00442},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2308.00442},
}
Figure