Vision Transformers (ViTs) have revolutionized computer vision, but their primary bottleneck remains the self-attention mechanism. Because self-attention complexity grows quadratically with the number of tokens, these models are often too slow for real-time applications or resource-constrained devices. Researchers from Baidu Inc. have introduced an integrated compression pipeline that achieves a massive 121% throughput increase on DeiT-Small with virtually no loss in accuracy by exploiting the inherent heterogeneity of attention patterns across different layers.
The Problem: One-Size-Fits-All Compression
Standard ViTs treat every layer uniformly, performing "dynamic" self-attention where an attention matrix is calculated uniquely for every input image. This is computationally expensive. Previous attempts to accelerate ViTs usually fall into two categories: pruning/aggregation (dropping tokens) or sparse/static attention (fixing weights). However, these methods often ignore a crucial reality: Transformer layers are not uniform.
As shown in the figure above, early layers tend to focus on general spatial structures that are remarkably similar across different images, while later layers produce highly redundant, low-rank feature maps. Applying a uniform compression strategy across all layers often sacrifices too much accuracy for speed.
Proposed Method: Heterogeneous Acceleration
The authors propose an integrated pipeline consisting of two key components tailored to specific parts of the network: DGSSA for early layers and GLAD for later layers.
1. Dynamic-Guided Static Self-Attention (DGSSA)
In the first few layers of a ViT, attention maps for different images look nearly identical. This suggests that the "dynamic" part of self-attention—calculating weights per image—is overkill.
- Static Self-Attention (SSA): The authors replace dynamic attention with a static matrix that is learned during training and shared across all images.
- Dynamic Guidance: To prevent the performance drop typically associated with static matrices, they use a closed-form mathematical solution to initialize the static matrix so it mimics the behavior of a pre-trained dynamic model. This "inheritance" allows the static layers to retain high representational power while being significantly faster to compute.
2. Global Aggregation Pyramid (GLAD)
In later layers, attention maps become "low-rank," indicating that many tokens carry redundant information.
- Global Projection: Unlike pruning (which deletes tokens) or local pooling (which only looks at neighboring pixels), GLAD uses a global linear projection to reduce the number of tokens.
- Token Pyramid: By aggregating tokens globally, the model preserves spatial information more effectively than simple pruning, creating a "token pyramid" that gets slimmer as data moves deeper into the network.
Experimental Results
The researchers evaluated their methods on the ImageNet-1K dataset using DeiT and Swin Transformer architectures. The results are among the most impressive in the field of ViT acceleration:
- DeiT-Small: Achieved a 121.1% speedup (2080 img/s vs. 940 img/s) with 0% accuracy loss (79.9% Top-1).
- DeiT-Base: Achieved a 113% speedup with only a 0.2% drop in accuracy.
- Swin-Small: Provided a 10.5% throughput boost with no accuracy loss, demonstrating that even optimized hierarchical models benefit from this approach.
- SOTA Comparison: The proposed method significantly outperformed other state-of-the-art techniques like TOME (64.9% speedup) and DynamicViT (62.2% speedup) on the same hardware.
Key Insights
- Attention Heterogeneity is Key: Early layers are image-agnostic (spatial-heavy), while later layers are image-specific but redundant (token-heavy). Tailoring compression to these roles is more efficient than uniform strategies.
- Hardware Friendliness: Unlike sparse attention mechanisms that require specialized kernels, DGSSA uses standard matrix multiplication, making it immediately faster on any standard GPU or CPU.
- Global > Local: For token reduction, looking at the whole image via global aggregation is more robust than local operations like 2x2 pooling, as it better captures the true spatial distribution of redundancy.
Conclusion and Future Work
By acknowledging and exploiting the different roles of Transformer layers, this research provides a practical path toward deploying high-performance ViTs on edge devices. While the current pipeline relies on empirical heuristics to choose which layers to compress, future work involving AutoML or Neural Architecture Search (NAS) could automate this process, further optimizing the balance between speed and accuracy across diverse computer vision tasks.
Summary updated