# Faster Neighborhood Attention: Reducing the O(n^2) Cost of Self Attention at the Threadblock Level

Source: [Faster Neighborhood Attention: Reducing the O(n^2) Cost of Self Attention at the Threadblock Level](<https://www.sota2.com/research/paper/68fa757a1e209131cea683c0>)

Collection index: [llms.txt](<https://www.sota2.com/research/llms.txt>)

Data updated: 2026-01-04T04:49:27.177Z

Authors: Ali Hassani, Wen-Mei Hwu, Humphrey Shi

Published: 2024-03-07T17:35:58.000Z

## 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...

## Recorded benchmark results

A research result applies to its stated dataset, split, metric and evaluation configuration. Compare like-for-like settings, preserve the metric's unit and direction, and distinguish paper-reported results from independent reproduction. The best result recorded here is not a claim of exhaustive, current global coverage.

- [Neighborhood Attention Forward Pass on NATTEN Problem Set 1-dimensional (test)](<https://www.sota2.com/research/sota/neighborhood-attention-forward-pass-on-natten-problem-set-1-dimensional-test.md>): Neighborhood Attention Forward Pass; NATTEN Problem Set 1-dimensional (test); FMHA % Matched/Outperformed (NATTEN 1D): 100.

- [Neighborhood Attention Forward Pass on NATTEN Problem Set 2-dimensional (test)](<https://www.sota2.com/research/sota/neighborhood-attention-forward-pass-on-natten-problem-set-2-dimensional-test.md>): Neighborhood Attention Forward Pass; NATTEN Problem Set 2-dimensional (test); FMHA Match/Outperform Rate (Self Attn): 99.3.

- [Neighborhood Attention Forward Pass on NATTEN Problem Set 3-dimensional (test)](<https://www.sota2.com/research/sota/neighborhood-attention-forward-pass-on-natten-problem-set-3-dimensional-test.md>): Neighborhood Attention Forward Pass; NATTEN Problem Set 3-dimensional (test); FMHA Attn Match Rate: 98.6.

## Original sources and implementation

[Original paper](<https://arxiv.org/abs/2403.04690>)

[PDF](<https://arxiv.org/pdf/2403.04690.pdf>)

[Implementation](<https://www.sota2.com/github.com/SHI-Labs/NATTEN>)
