Faster Neighborhood Attention: Reducing the O(n^2) Cost of Self Attention at the Threadblock Level
Ali Hassani, Wen-Mei Hwu, Humphrey Shi
Abstract
Neighborhood attention reduces the cost of self attention by restricting each token's attention span to its nearest neighbors. This restriction, parameterized by a window size and dilation factor, draws a spectrum of possible attention patterns between linear projection and self attention. Neighborhood attention, and more generally sliding window attention patterns, have long been bounded by infrastructure, particularly in higher-rank spaces (2-D and 3-D), calling for the development of custom kernels, which have been limited in either functionality, or performance, if not both. In this work, we aim to massively improve upon existing infrastructure by providing two new methods for implementing neighborhood attention. We first show that neighborhood attention can be represented as a batched GEMM problem, similar to standard attention, and implement it for 1-D and 2-D neighborhood attention. These kernels on average provide 895% and 272% improvement in full precision runtime compared to existing naive CUDA kernels for 1-D and 2-D neighborhood attention respectively. We find that aside from being heavily bound by memory bandwidth, certain inherent inefficiencies exist in all unfused implementations of neighborhood attention, which in most cases undo their theoretical efficiency gain. Motivated by the progress made into fused dot-product attention kernels, we developed fused neighborhood attention; an adaptation of fused dot-product attention kernels that allow fine-grained control over attention across different spatial axes. Known for reducing the quadratic time complexity of self attention to a linear complexity, neighborhood attention can now enjoy a reduced and constant memory footprint, and record-breaking half precision runtime. We observe that our fused implementation successfully circumvents some of the unavoidable inefficiencies in unfused implementations...
Ask about this paper
Your agent reads all of it.
Lune indexed this paper to the last equation, along with the top-tier papers that cite it. Ask a question and the answer quotes them.
Cited by top-tier papers7
- Attention on the SphereBoris Bonev, Max Rietmann, Andrea Paris, Alberto Carpentieri et al.NeurIPS 2025 · 13 citations
- InvFusion: Bridging Supervised and Zero-shot Diffusion for Inverse ProblemsNoam Elata, Hyungjin Chung, Jong Chul Ye, Tomer Michaeli et al.NeurIPS 2025 · 10 citations
- NAF: Zero-Shot Feature Upsampling via Neighborhood Attention FilteringLoïck Chambon, Paul Couairon, Éloi Zablocki, Alexandre Boulch et al.CVPR 2026 · 6 citations
- LaplacianFormer: Rethinking Linear Attention with Laplacian KernelZhe Feng, Sen Lian, Changwei Wang, Muyang Zhang et al.ICLR 2026 · 3 citations
- PRSformer: Disease Prediction from Million-Scale Individual GenotypesPayam Dibaeinia, Chris German, Suyash Shringarpure, Adam Auton et al.NeurIPS 2025 · 3 citations
Builds on9
- An Image is Worth 16x16 Words: Transformers for Image Recognition at ScaleAlexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn et al.ICLR 2021 · 21,477 citations
- Scalable Diffusion Models with TransformersWilliam Peebles, Saining XieICCV 2023 · 5,568 citations
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-AwarenessTri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra et al.NeurIPS 2022 · 5,493 citations
- Big Bird: Transformers for Longer SequencesManzil Zaheer, Guru Guruganesh, Kumar Avinava Dubey, Joshua Ainslie et al.NeurIPS 2020 · 3,159 citations
- ViViT: A Video Vision TransformerAnurag Arnab, Mostafa Dehghani, Georg Heigold, Chen Sun et al.ICCV 2021 · 2,947 citations
Related papers
- Hilbert-Guided Sparse Local AttentionYunge Li, Lanyu XuICLR 2026 · 1 citation
- FlexLinearAttention: Compiling a Unified Abstraction into Scalable Kernels for Linear AttentionHaojie Duanmu, Size Zheng, Ningxin Zheng, Jianqiao Lu et al.ICLR 2026
- FSA: An Alternative Efficient Implementation of Native Sparse Attention KernelRan Yan, Youhe Jiang, Zhuoming Chen, Haohui Mai et al.ICLR 2026 · 10 citations
- Fast Attention Over Long Sequences With Dynamic Sparse Flash AttentionMatteo Pagliardini, Daniele Paliotta, Martin Jaggi, François FleuretNeurIPS 2023 · 26 citations
- FlashAttention-T: Towards Fully Tensorized Attention by Exploiting Tensor-Vector ParallelismJianxing Xu, Yuanbo Wen, Jun Bi, Ruibai Xu et al.PPoPP 2026
