Lune

NeurIPS2024Top-tier venue

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 Lune

Know this paper in minutes, not an afternoon.

Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.

7 top-tier papers have cited it since. Know what they found before your reviewers do.

Questions to start from

Lune can draw on

the full paper · the top-tier papers that cite it · related work

Get the answer

Free to start. No credit card required.

Lineage

  • ViViT: A Video Vision Transformer cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
  • FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness cites Big Bird: Transformers for Longer Sequences
  • FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
  • InvFusion: Bridging Supervised and Zero-shot Diffusion for Inverse Problems cites Scalable Diffusion Models with Transformers
  • Scalable Diffusion Models with Transformers cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
  • LaplacianFormer: Rethinking Linear Attention with Laplacian Kernel cites An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
  • PRSformer: Disease Prediction from Million-Scale Individual Genotypes cites Big Bird: Transformers for Longer Sequences
  • PRSformer: Disease Prediction from Million-Scale Individual Genotypes cites FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
Builds onCited byLeft to right by year; the more cited, the higher.

Cited by top-tier papers7

  1. Attention on the SphereBoris Bonev, Max Rietmann, Andrea Paris, Alberto Carpentieri et al.NeurIPS 2025 · 13 citations
  2. InvFusion: Bridging Supervised and Zero-shot Diffusion for Inverse ProblemsNoam Elata, Hyungjin Chung, Jong Chul Ye, Tomer Michaeli et al.NeurIPS 2025 · 10 citations
  3. NAF: Zero-Shot Feature Upsampling via Neighborhood Attention FilteringLoïck Chambon, Paul Couairon, Éloi Zablocki, Alexandre Boulch et al.CVPR 2026 · 6 citations
  4. LaplacianFormer: Rethinking Linear Attention with Laplacian KernelZhe Feng, Sen Lian, Changwei Wang, Muyang Zhang et al.ICLR 2026 · 3 citations
  5. PRSformer: Disease Prediction from Million-Scale Individual GenotypesPayam Dibaeinia, Chris German, Suyash Shringarpure, Adam Auton et al.NeurIPS 2025 · 3 citations

Builds on9

  1. 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
  2. Scalable Diffusion Models with TransformersWilliam Peebles, Saining XieICCV 2023 · 5,568 citations
  3. 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
  4. Big Bird: Transformers for Longer SequencesManzil Zaheer, Guru Guruganesh, Kumar Avinava Dubey, Joshua Ainslie et al.NeurIPS 2020 · 3,159 citations
  5. ViViT: A Video Vision TransformerAnurag Arnab, Mostafa Dehghani, Georg Heigold, Chen Sun et al.ICCV 2021 · 2,947 citations

Related papers

  1. Generalized Neighborhood Attention: Multi-dimensional Sparse Attention at the Speed of LightAli Hassani, Fengzhe Zhou, Aditya Kane, Jiannan Huang, Chieh-Yun Chen, Min Shi, Steven Walton, Markus Hoehnerbach, Vijay Thakkar, Mikhail Isaev, Qinsheng Zhang, Bing Xu, Haicheng Wu, Wen-mei Hwu, Ming-Yu Liu, Humphrey ShiCVPR Findings 2026
  2. Hilbert-Guided Sparse Local AttentionYunge Li, Lanyu XuICLR 2026 · 1 citation
  3. Fast Attention Over Long Sequences With Dynamic Sparse Flash AttentionMatteo Pagliardini, Daniele Paliotta, Martin Jaggi, François FleuretNeurIPS 2023 · 26 citations
  4. FlexLinearAttention: Compiling a Unified Abstraction into Scalable Kernels for Linear AttentionHaojie Duanmu, Size Zheng, Ningxin Zheng, Jianqiao Lu et al.ICLR 2026
  5. FSA: An Alternative Efficient Implementation of Native Sparse Attention KernelRan Yan, Youhe Jiang, Zhuoming Chen, Haohui Mai et al.ICLR 2026 · 10 citations
Dusk over the sea between two cliffs drawn in fine vertical lines