Separable Self-attention for Mobile Vision Transformers

Sachin Mehta, Mohammad Rastegari

Abstract

Mobile vision transformers (MobileViT) can achieve state-of-the-art performance across several mobile vision tasks, including classification and detection. Though these models have fewer parameters, they have high latency as compared to convolutional neural network-based models. The main efficiency bottleneck in MobileViT is the multi-headed self-attention (MHA) in transformers, which requires $O(k^2)$ time complexity with respect to the number of tokens $k$. Moreover, MHA requires costly operations like batch-wise matrix multiplication, impacting latency on resource-constrained devices. This paper introduces a separable self-attention method with linear complexity $O(k)$. By using element-wise operations, the proposed method is optimized for resource-constrained devices. The improved model, MobileViTv2, achieves a top-1 accuracy of 75.6% on ImageNet with 3M parameters, outperforming MobileViT by 1% while running 3.2x faster on mobile devices.

Introduction: The Latency Bottleneck in Mobile ViTs

Vision Transformers (ViTs) have set new benchmarks in computer vision, but their deployment on mobile devices is hindered by high latency. While parameter reduction has been successful, the computational cost of the Multi-Headed Self-Attention (MHA) mechanism remains a major hurdle. MHA suffers from quadratic complexity $O(k^2)$ relative to the number of image patches $k$ and relies on batch-wise matrix multiplications (BMM), which are inefficient on mobile CPUs and NPUs due to memory bandwidth constraints and limited parallelization.

Separable Self-Attention

To address these bottlenecks, the authors propose Separable Self-attention. This mechanism achieves linear complexity $O(k)$ by replacing the standard "all-to-all" token interaction with a latent token approach that encodes global context through hardware-friendly operations.

The process involves three main branches:

  1. Input Branch: Maps tokens to scalar context scores using a linear layer.
  2. Key Branch: Computes a global context vector as a weighted sum of projected tokens.
  3. Value Branch: Combines the global context vector with projected tokens via element-wise multiplication.
Comparison of operations and latency across different transformer models.

By swapping matrix-matrix products for element-wise operations, MobileViTv2 significantly reduces the computational and memory overhead typical of standard transformers. As shown in the operation breakdown, the proposed method avoids the costly aten::bmm operations that dominate the latency of standard and other efficient transformers.

Key Insights: Operations vs. FLOPs

A critical takeaway is that FLOP count is often a poor indicator of actual mobile latency. MobileViTv2 demonstrates that choosing "hardware-friendly" operations—like element-wise multiplication—over "costly" ones—like BMM—is vital for real-world performance. This architectural shift allows ViTs to bridge the latency gap with highly optimized CNNs while retaining the scaling benefits of transformers at higher resolutions.

Experimental Results

MobileViTv2 was evaluated on ImageNet-1k, MS-COCO, and PASCAL VOC benchmarks. The results show a significant Pareto improvement over the original MobileViT across multiple vision tasks.

Performance of MobileViTv2 vs. MobileViTv1 across classification, detection, and segmentation tasks.
  • Speed and Accuracy: On an iPhone 12, MobileViTv2 is 3.2x faster than its predecessor. The 3M parameter model achieves 75.6% Top-1 accuracy on ImageNet-1k, a 1% improvement over MobileViTv1.
  • Downstream Tasks: In object detection (MS-COCO), MobileViTv2 achieves a 5.4% better Mean Average Precision while being 3x faster. In semantic segmentation (PASCAL VOC), it is 3.1x faster with 6.7% better mIoU.
  • Scalability: The model scales more efficiently than CNNs as input resolution or token count increases, thanks to its linear-complexity attention mechanism.

Conclusion and Future Work

MobileViTv2 proves that vision transformers can be both highly accurate and mobile-friendly by optimizing for specific hardware operations rather than just theoretical complexity. Future research may focus on low-level kernel optimizations for ViT operations and exploring more complex latent structures for ultra-high-resolution images.

Summary updated

BibTeX
@article{mehta2023separable,
  author = {Sachin Mehta and Mohammad Rastegari},
  title = {{Separable Self-attention for Mobile Vision Transformers}},
  journal = {Trans. Mach. Learn. Res.},
  year = {2023},
}
Figure