[Paper Review] Neighborhood Attention Transformer
This paper introduces Neighborhood Attention (NA), a novel sliding window self-attention mechanism that localizes self-attention to each pixel's nearest neighbors, enabling linear time and space complexity while preserving translational equivariance. NA achieves 83.2% top-1 accuracy on ImageNet and outperforms Swin Transformer and ConvNeXt in image classification and downstream vision tasks, with up to 40% faster inference and 25% less memory usage via the optimized $π$ATTEN$ package.
We present Neighborhood Attention (NA), the first efficient and scalable sliding-window attention mechanism for vision. NA is a pixel-wise operation, localizing self attention (SA) to the nearest neighboring pixels, and therefore enjoys a linear time and space complexity compared to the quadratic complexity of SA. The sliding-window pattern allows NA's receptive field to grow without needing extra pixel shifts, and preserves translational equivariance, unlike Swin Transformer's Window Self Attention (WSA). We develop NATTEN (Neighborhood Attention Extension), a Python package with efficient C++ and CUDA kernels, which allows NA to run up to 40% faster than Swin's WSA while using up to 25% less memory. We further present Neighborhood Attention Transformer (NAT), a new hierarchical transformer design based on NA that boosts image classification and downstream vision performance. Experimental results on NAT are competitive; NAT-Tiny reaches 83.2% top-1 accuracy on ImageNet, 51.4% mAP on MS-COCO and 48.4% mIoU on ADE20K, which is 1.9% ImageNet accuracy, 1.0% COCO mAP, and 2.6% ADE20K mIoU improvement over a Swin model with similar size. To support more research based on sliding-window attention, we open source our project and release our checkpoints at: https://github.com/SHI-Labs/Neighborhood-Attention-Transformer .
Motivation & Objective
- To address the quadratic complexity of standard self-attention in vision transformers, which limits scalability on high-resolution inputs.
- To develop an efficient, scalable, and differentiable sliding window attention mechanism that maintains translational equivariance and local inductive biases.
- To overcome the inefficiency and implementation bottlenecks of prior local attention methods like SASA and Swin's WSA through optimized kernel design.
- To design a new hierarchical transformer architecture, Neighborhood Attention Transformer (NAT), that leverages NA for state-of-the-art performance on image classification and dense prediction tasks.
- To release the $π$ATTEN$ library and model checkpoints to enable broader research in efficient sliding window attention for vision.
Proposed method
- NA defines a pixel-wise attention mechanism where each token attends only to its nearest neighboring pixels, forming a dynamic local receptive field.
- The mechanism is formulated as a differentiable operation that computes weighted sums over a fixed-size neighborhood around each pixel, avoiding global attention computation.
- The neighborhood is defined via a spatial kernel (e.g., 3×3, 5×5), and the attention weights are computed using query-key similarity within the local region.
- The authors implement NA efficiently using a custom C++ and CUDA kernel library called $π$ATTEN$, which enables high-speed inference and reduced memory usage.
- NAT uses overlapping convolutional patch embedding and a deeper-thinner architecture with residual blocks to enhance representational capacity.
- The model employs a hierarchical design with downsampling and multi-scale feature learning, similar to Swin, but replaces WSA with NA for improved efficiency and performance.
Experimental results
Research questions
- RQ1Can a pixel-wise sliding window attention mechanism achieve linear complexity while maintaining performance comparable to global self-attention in vision transformers?
- RQ2Does NA preserve translational equivariance and local inductive biases more effectively than window-based or block-based attention mechanisms?
- RQ3Can an efficient implementation of NA outperform existing attention mechanisms like WSA and SASA in both speed and accuracy?
- RQ4To what extent does replacing WSA with NA in a hierarchical transformer architecture improve performance on image classification and dense prediction benchmarks?
- RQ5How does the receptive field growth of NA compare to that of Swin’s shifted-window mechanism without requiring additional operations?
Key findings
- NAT-Tiny achieves 83.2% top-1 accuracy on ImageNet, a 1.9% improvement over Swin-Tiny with similar model size.
- On MS-COCO object detection, NAT-Tiny achieves 51.4% mAP, a 1.0% improvement over Swin-Tiny.
- On ADE20K semantic segmentation, NAT-Tiny reaches 48.4% mIoU, a 2.6% improvement over Swin-Tiny.
- The $π$ATTEN$ library enables NA to run up to 40% faster and use up to 25% less memory than Swin’s WSA implementation.
- Ablation studies show that replacing WSA with NA in Swin-Tiny boosts accuracy by 0.5% over the baseline, while SASA yields a slight drop.
- The 7×7 kernel size in NAT-Tiny achieves the best balance of performance and efficiency, with 83.2% ImageNet accuracy and 1537 imgs/sec throughput.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.