---
title: Local Attention in Neural Models
url: https://www.emergentmind.com/topics/local-attention
type: topic
---

# Local Attention in Neural Models

Local attention is a family of attention mechanisms in which each query interacts with a restricted neighborhood rather than the full input domain. In the literature, that neighborhood is defined in multiple ways: a bounded token window in autoregressive language modeling, a dependency-tree radius in syntax-aware encoders, a spatial neighborhood or patch in vision, a set of adjacent frequency bins in speech enhancement, or a learned cluster in feature space. Across these formulations, the common purpose is to impose a locality prior, reduce irrelevant long-range mixing, and often lower the computational burden of dense global attention, while preserving task-relevant context through stacking, overlap, or hybridization with global branches [2501.01039], [2304.04237], [2302.05693], [2012.15150].

## 1. Definitions and scope

In sequence models, local attention usually means that token \(i\) attends only to a bounded set of predecessors or neighbors. In sliding-window attention, the attention range is a fixed window of size \(w\); in syntax-aware local attention, the scope is local in dependency-tree distance rather than linear position; and in formal analyses of transformer recognizers, a \(k\)-local mask allows attention only to the previous \(k\) positions [2501.01039], [2012.15150], [2605.00768].

In vision, the term is broader. It can denote attention inside local image windows, overlapping neighborhoods around each pixel, patch-wise attention over aerial-image regions, geometry-preserving sparse 2D neighborhoods, feature-space clusters of similar patches, or directional positional sequences obtained by strip pooling in CNN attention modules [2112.11435], [1911.08877], [1911.12287], [2201.13027], [2403.01123]. In speech enhancement, local attention has been used to restrict spectral interactions to adjacent frequency bands or to emphasize short-range, multi-receptive-field speech structure instead of full non-local aggregation [2302.05693], [2212.03408].

The term also has an interpretive meaning. In analyses of BERT, “local” refers to what an attention head does with respect to its immediate hidden-token inputs at layer \(l-1\), whereas “global” refers to which original input tokens influence the head output after information has been mixed across layers. This distinction separates head-internal routing from input-level attribution [2004.05916].

| Setting | Neighborhood defining locality | Representative works |
|---|---|---|
| Language and sequence modeling | Bounded predecessor window, band mask, dependency-tree distance | [2501.01039], [2605.00768], [2012.15150] |
| Vision | Local windows, overlapping neighborhoods, patches, 2D sparse geometry, feature-space clusters, coordinate strips | [2304.04237], [2112.11435], [1911.12287], [2201.13027], [2403.01123] |
| Speech | Adjacent frequency bands, local receptive-field channel attention | [2302.05693], [2212.03408] |

A recurring misconception is that local attention is synonymous with a fixed linear window. The cited literature does not support that reduction. Locality may be linear, syntactic, geometric, patch-based, coordinate-aware, frequency-local, or feature-space-based, depending on the structure regarded as most informative for the task.

## 2. Core formulations and design patterns

The most common implementation is masked softmax. In full-band speech enhancement, local spectral attention introduces a binary mask
$$
M_{F,i,j} =
\begin{cases}
0, & |i-j| \le N_l \\
-\infty, & |i-j| > N_l
\end{cases}
$$
so that each frequency bin attends only to nearby bands [2302.05693]. In formal treatments of causal transformers, \(k\)-local attention is defined by
$$
M^{\le k}_{n,m}=
\begin{cases}
1 & \text{if } \max(1,n-k)\le m<n,\\
0 & \text{otherwise,}
\end{cases}
$$
which makes locality a property of the mask alone [2605.00768]. Encoder-side local transformers have also been implemented with fixed band masks such as band-1 and band-2, as well as directional masks like prev-1, prev-2, next-1, next-2, and identity [2008.05828].

A second pattern is overlapping local aggregation. QnA replaces per-window queries with learned queries shared across overlapping windows, so attention remains local and shift-invariant while avoiding the memory cost of explicit unfolding. Its single-query form can be written as
$$
z_{i,j}=\text{Attention}\!\left(\tilde{q},K_{\mathcal{N}_{i,j}}\right)\cdot V_{\mathcal{N}_{i,j}},
$$
with \(\mathcal{N}_{i,j}\) the \(k\times k\) neighborhood [2112.11435]. Slide Attention likewise keeps the query-centric local neighborhood, but reinterprets Im2Col row-wise and replaces the shifts with depthwise convolutions, then adds a deformed shifting branch merged by re-parameterization at inference time [2304.04237].

A third pattern is locality defined by structure other than raw position. BOAT supplements image-space local attention with feature-space local attention by balanced hierarchical clustering, so attention is computed inside equal-sized clusters of similar tokens rather than only inside spatial windows [2201.13027]. Syntax-aware local attention uses dependency-tree distance thresholds \(m \in \{3,4\}\), implemented through an attention mask, so a token attends to syntactically nearby words even if they are far apart linearly [2012.15150]. In aerial segmentation, LANet computes patch descriptors over \(h_p\times w_p\) regions and derives patch-specific channel attention maps, making the patch rather than the full image the basic local unit [1911.08877].

These constructions show that local attention is less a single algorithm than a design principle: attention is restricted to a task-specific neighborhood chosen to preserve a salient inductive bias while suppressing interactions deemed weak, noisy, or redundant.

## 3. Local attention in language and sequence modeling

In autoregressive language models, sliding-window attention is the canonical local mechanism. Standard self-attention has quadratic time complexity and a cache that grows linearly with sequence length, whereas sliding-window attention restricts attention to a fixed recent context of size \(w\), reducing per-head cost to \(O(dnw)\) and cache to \(O(dw)\) [2501.01039]. MSWA refines this by assigning different window sizes across heads and layers. Within a layer, head groups receive \(\frac{w_i}{4}\), \(\frac{w_i}{2}\), \(w_i\), and \(2w_i\); across depth, layer groups receive \(\frac{hw}{4}\), \(\frac{hw}{2}\), \(hw\), and \(2hw\). On 12-layer language models trained from scratch, SWA with \(w=128\) gives Wikitext-103 perplexity \(30.70\) and enwik8 \(1.22\) bpc, whereas full MSWA reaches \(29.56\) and \(1.11\) at lower relative cost; in Llama-7B fine-tuning, average accuracy rises from \(47.12\) to \(49.02\) in 3-shot and from \(42.66\) to \(49.89\) in 5-shot [2501.01039].

RAttention addresses a central limitation of sliding-window attention: complete disregard for out-of-window tokens. It adds a Residual Linear Attention branch with recurrence
$$
\mathbf{S}_t = \mathbf{S}_{t-1} + \phi(\mathbf{k}_t)^\top \mathbf{v}_t, \qquad
\mathbf{y}^{\text{rla}}_t = \phi(\mathbf{q}_t)\mathbf{S}_{t-w-1},
$$
so the local branch models tokens in \([t-w,t]\) exactly while the recurrent state summarizes tokens in \([1,t-w-1]\) [2506.15545]. At 12B scale and 8K pretraining length, RAttention-512 reports Average (0/1-shot) \(62.72\) versus \(62.67\) for Full 8k, MMLU \(52.94\) versus \(52.40\), and GSM8K \(37.39\) versus \(36.69\). On RULER after 4K pretraining, RAttn-512 obtains \(80.79\) at 4K, \(66.26\) at 8K, \(50.80\) at 16K, and \(29.59\) at 32K, far above both Full-4k and SWA-2k [2506.15545].

Local attention in encoders need not be purely linear-window-based. Syntax-aware local attention for BERT constrains attention by dependency-tree distance and leaves \([CLS]\) and \([SEP]\) unmasked. Fine-tuned on downstream tasks, it reports on Chinese benchmarks \(95.7\) accuracy on ChnSentiCorp, \(94.9\) F1 on MSRA NER, and \(78.7\) F1 on CGED, outperforming both a BERT reimplementation and a window-based local baseline [2012.15150]. A separate study on the importance of local information finds, via gradient sensitivity, that \(\gamma_{local} > \gamma_{syntactic} > \gamma_{unrelated}\) across QQP, SST-2, MRPC, and QNLI, and shows that even fully local-attention encoder configurations achieve comparable performance to unconstrained transformers on both GLUE and machine translation [2008.05828].

Theoretical work sharpens the picture. In fixed-precision transformers, global attention corresponds to a fragment with an unbounded past operator, \(k\)-local attention corresponds to a fragment with a bounded-past operator, and hybrid global-local transformers correspond to a richer fragment containing both. The paper proves that global and local attention are expressively complementary, neither subsumes the other, and hybrid global-local attention is strictly more expressive than either alone [2605.00768].

## 4. Local attention in vision and multimodal perception

Vision work has treated locality as a way to restore inductive bias that is weakened by dense global self-attention. Slide-Transformer defines local attention as restricting each query to its neighboring pixels, then makes the mechanism practical by replacing Im2Col-style neighborhood extraction with standard convolution primitives. Integrated into hierarchical backbones, Slide Attention improves ImageNet-1K accuracy across PVT, PVTv2, Swin, CSWin, and NAT variants; for example, PVT-T improves from \(75.1\) to \(78.0\), Swin-T from \(81.3\) to \(82.3\), and NAT-S from \(83.7\) to \(84.3\) [2304.04237]. QnA pursues a related goal with overlapping local aggregation and learned shared queries, reporting up-to x10 less memory and up-to x5 faster execution than existing methods while remaining competitive in accuracy [2112.11435].

Other vision formulations redefine the neighborhood itself. BOAT combines image-space local attention with feature-space local attention, grouping tokens by balanced hierarchical clustering so that semantically similar but spatially distant patches can interact. Relative to its base backbones, BOAT-Swin-T improves ImageNet-1K top-1 from \(81.3\) to \(82.3\), and BOAT-CSWin-T from \(82.7\) to \(83.7\); on ADE20K, BOAT-CSWin-T rises from \(49.3\) to \(50.5\) mIoU [2201.13027]. In generative modeling, YLG-SAGAN replaces dense SAGAN attention with a 2D local sparse attention layer that preserves geometry through Enumerate, Shift, Apply. On ImageNet-128, FID improves from \(18.65\) to \(15.94\) and Inception score from \(52.52\) to \(57.22\), with peak performance reached after about \(865\)k steps instead of more than \(1.3\)M [1911.12287].

CNN-based local attention does not necessarily use token-to-token self-attention. ELA derives horizontal and vertical 1D positional descriptors by strip pooling, processes them with 1D convolution and Group Normalization, and applies the resulting gates without channel reduction, explicitly contrasting this design with Coordinate Attention [2403.01123]. In aerial semantic segmentation, LANet treats the patch as the meaningful local unit: a Patch Attention Module computes patch-wise attention, and an Attention Embedding Module transfers local focus from high-level to low-level features. On Potsdam, LANet reaches mean F1 \(91.95\) and OA \(90.84\), improving over a baseline FCN at \(88.66\) mean F1 and \(89.42\) OA; on Vaihingen it reaches mean F1 \(88.09\) and OA \(89.83\) [1911.08877].

Multimodal and embodied systems adopt similar locality constraints. In vision-and-language navigation, Local Slot Attention applies a fixed sparse mask on a \(3\times 12\) panoramic grid so that each candidate view attends only to a local \(3\times 3\) neighborhood. The \(3\times 3\) mask gives the best unseen validation performance, and the full model improves R2R validation seen SR from \(72\) to \(76\), SPL from \(68\) to \(72\), and test unseen SPL from \(57\) to \(59\) over Recurrent VLN-Bert [2206.08645]. In object detection, Local-Global Attention uses small kernels \(k \in \{3,5,7\}\) for the local branch and a larger kernel for the global branch, then fuses them with learnable weights; on TinyPerson with a MobileNetV3 backbone, mAP@50 rises from \(9.88\) to \(10.8\) and mAP@50-95 from \(3.56\) to \(3.85\) [2411.09604].

## 5. Local attention in speech and acoustic enhancement

In full-band speech enhancement, the main locality axis is frequency. Local spectral attention replaces global frequency-wise self-attention with attention over adjacent frequency bins, motivated by the observation that low-frequency harmonic structure and high-frequency components differ substantially in full-band \(48\) kHz enhancement [2302.05693]. In MTFAA, replacing global frequency attention with local spectral attention changes PESQ from \(3.13\) to \(3.16\), CBAK from \(3.54\) to \(3.61\), STOI from \(94.6\%\) to \(94.7\%\), and SiSDR from \(17.7\) dB to \(18.8\) dB; in DPARN, PESQ moves from \(2.92\) to \(2.96\) and SiSDR from \(18.3\) dB to \(18.7\) dB [2302.05693]. The same paper reports that the improved causal MTFAA with LSA achieves the best objective score on the full-band VoiceBank+DEMAND set, with PESQ \(3.16\), CSIG \(4.35\), CBAK \(3.61\), COVL \(3.78\), and STOI \(94.7\%\) [2302.05693].

Selector-Enhancer adopts a different notion of local attention. Its local branch is a channel-wise attention mechanism with multiple receptive fields, built from parallel two-layer and four-layer convolutional stacks. A feature-filter trained with REINFORCE chooses, per time-frequency region, whether features should be routed to local attention or non-local attention [2212.03408]. The study reports that removing local attention hurts performance, and that adding it yields a gain of \(+0.16\) PESQ and \(+3.63\%\) STOI in ablation. The larger claim is not that local attention should always replace non-local attention, but that different regions of the same utterance may require different processing regimes, especially under transient or mixed noise conditions [2212.03408].

These speech examples illustrate a general principle that also appears elsewhere: local attention is frequently motivated by the unreliability of indiscriminate global mixing when the signal is heterogeneous across time, frequency, or space. In that setting, restricting the interaction span is treated as a denoising prior rather than only a complexity reduction.

## 6. Efficiency, expressivity, and persistent debates

A major reason for adopting local attention is computational scaling. Standard attention in language modeling costs \(O(dn^2)\), while sliding-window attention reduces this to \(O(dnw)\) with cache \(O(dw)\) [2501.01039]. In practice, this is not the full story, because smaller windows improve efficiency only by discarding context. RAttention makes that limitation explicit: the core weakness of standard local attention is complete disregard for out-of-window tokens, so it augments the local window with a recurrent linear memory instead of enlarging the window itself [2506.15545].

Another persistent result is that local attention is often strongest when combined with a global or non-local pathway. In relation classification, pure local attention with \(\gamma=0.0\) yields \(84.58\) F1, pure global attention with \(\gamma=1.0\) yields \(84.36\), and the best score occurs around \(\gamma=0.5\) at \(85.04\) F1 [2407.01424]. In image retrieval, GLAM reports that local attention alone is weak or even harmful in most cases, whereas baseline + global + local gives the best results [2107.08000]. In object detection, the Local-Global Attention paper reports that both individual LA and GA improve performance, but their combination gives the best results on TinyPerson [2411.09604]. These findings suggest that locality and globality are usually complementary rather than interchangeable.

Theoretical and interpretive debates reinforce that conclusion. The expressivity results for fixed-precision transformers show that local and global attention are incomparable and that combining them yields the richest fragment among the studied variants [2605.00768]. The BERT analysis likewise shows that attention maps can provide insight into local head behavior, but they are not reliable global explanations of which original input tokens matter after multilayer mixing; by the last layers, the correlation between attention distributions and input contribution drops sharply [2004.05916]. A plausible implication is that local attention should be understood as a structured routing mechanism whose value depends on the surrounding architecture, depth, and aggregation pathway, not as a universal substitute for full attention.

Several limitations recur across the literature. Fixed local windows can miss long-range dependencies, and the optimal span is task- and layer-dependent [2501.01039], [2302.05693]. Syntax-aware local attention slightly hurts QNLI and RTE, with the paper attributing this to especially important cross-sentence interactions in textual entailment [2012.15150]. In retrieval, local attention by itself is not consistently beneficial [2107.08000]. These results do not weaken the case for local attention; rather, they delimit it. Locality is most effective when the neighborhood definition matches the structure of the data and when the architecture retains some mechanism for broader aggregation.

Source: https://www.emergentmind.com/topics/local-attention