Twins: Revisiting the Design of Spatial Attention in Vision Transformers

Xiangxiang Chu, Zhi Tian, Yuqing Wang, Bo Zhang, Haibing Ren, Xiaolin Wei, Huaxia Xia, Chunhua Shen

TL;DR

The "Twins" paper introduces two new Vision Transformer (ViT) architectures that significantly improve efficiency and performance by rethinking how models handle spatial attention. By interleaving local and global attention mechanisms and adopting conditional positional encodings, the authors created models that outperform the state-of-the-art Swin Transformer while being much easier to deploy on hardware.

The Challenge of High-Resolution Vision Transformers

While Transformers have revolutionized Natural Language Processing, applying them to computer vision faces a major hurdle: computational complexity. Standard self-attention scales quadratically ($O(N^2)$) with the number of tokens (pixels or patches). For high-resolution images used in dense prediction tasks like object detection or segmentation, this becomes prohibitively expensive.

Previous solutions attempted to mitigate this in different ways. The Swin Transformer used "shifted windows"—computing attention in small local patches and shifting those patches to allow information to flow between them. However, this approach is complex to implement and results in uneven window sizes at the edges, making it difficult to optimize for deployment tools like TensorRT or ONNX. Alternatively, the Pyramid Vision Transformer (PVT) used global sub-sampling but struggled to match Swin's performance. The authors of "Twins" set out to find a simpler, more efficient way to balance local and global information.

Proposed Architectures: Twins-PCPVT and Twins-SVT

The researchers propose two distinct architectures to address these bottlenecks:

1. Twins-PCPVT

This model serves as a "corrected" version of the Pyramid Vision Transformer. The authors discovered that PVT’s main weakness wasn't its attention mechanism, but its use of absolute positional encodings. By replacing these with Conditional Positional Encodings (CPE)—which use depth-wise convolutions to generate positions based on the local neighborhood—they made the model more flexible and translation-invariant.

2. Twins-SVT

This is the more novel architecture, introducing Spatially Separable Self-Attention (SSSA). Inspired by the "separable convolutions" used in efficient CNNs like MobileNet, SSSA breaks down global attention into two interleaved parts:

  • Locally-grouped Self-Attention (LSA): Attention is computed only within small, non-overlapping $m \times n$ windows. This captures fine-grained, short-distance details efficiently.
  • Global Sub-sampled Attention (GSA): To allow windows to communicate, the model generates a single "representative" token for each local window (using a sub-sampling function like a strided convolution). Global attention is then computed using these representatives, capturing long-range context at a fraction of the cost.
Overall architecture of Twins-SVT, illustrating the interleaved Locally-grouped Self-Attention (LSA) and Global Sub-sampled Attention (GSA).

Key Insights

  • Positional Encoding is a Silent Killer: One of the most surprising findings was that PVT's underperformance compared to Swin was largely due to its rigid positional encoding. Simply adding a more dynamic encoding (CPE) allowed a global sub-sampled model to match or beat complex window-shifting models.
  • The "Separable" Analogy: Just as depth-wise separable convolutions broke down a complex 3D convolution into two simpler steps, SSSA breaks down global attention into a "local" step and a "summarized global" step. This achieves a similar receptive field with much lower FLOPs.
  • Deployment Matters: The authors emphasize that their design uses standard matrix multiplications. Unlike Swin’s torch.roll operations, which can be memory-unfriendly and poorly supported by inference engines, Twins models are highly optimized for modern deep learning frameworks.

Key Results

The authors evaluated their models across ImageNet-1K (classification), ADE20K (segmentation), and COCO 2017 (object detection). The Twins architectures achieved impressive gains in both accuracy and efficiency:

Performance comparison of Twins architectures against state-of-the-art models on ImageNet-1K.
  • Classification: Twins-SVT-S achieved 81.7% top-1 accuracy on ImageNet, outperforming Swin-T (81.3%) while using 35% fewer FLOPs.
  • Segmentation: On the ADE20K benchmark, Twins-SVT-L reached a new state-of-the-art 50.2% mIoU, outperforming Swin-B.
  • Detection: On COCO, Twins-PCPVT-S outperformed PVT-Small by a massive 2.6% mAP in object detection.
  • Hardware Speed: Because the architecture is simpler, a Twins-SVT-S model converted to TensorRT showed a 1.7x throughput boost compared to its original PyTorch implementation, highlighting its production-ready nature.

Limitations and Future Work

While the paper demonstrates strong results across various tasks, a few areas remain for exploration:

  • Sub-sampling Functions: While regular strided convolutions worked best for generating global representatives, more sophisticated sub-sampling methods could potentially yield even better results.
  • Scaling to Giant Models: The paper focuses on "Small," "Base," and "Large" variants. It is not yet clear how these spatially separable mechanisms behave when scaled to the "Huge" or "Giant" levels (billions of parameters) often seen in foundation models.
  • Task-Specific Heads: The study focuses on the backbone of the vision system; future work could investigate how to better integrate these "twin" attention mechanisms directly into the task-specific heads for detection and segmentation.

Summary updated

BibTeX
@inproceedings{chu2021twins,
  author = {Xiangxiang Chu and Zhi Tian and Yuqing Wang and Bo Zhang and Haibing Ren and Xiaolin Wei and Huaxia Xia and Chunhua Shen},
  title = {{Twins: Revisiting the Design of Spatial Attention in Vision Transformers}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2021},
  url = {https://openreview.net/forum?id=5kTlVBkzSRx},
}
Figure