NearFormer: Linear-Size Sparse Attention via Randomized Near Neighbors

Ali Parviz, Alex Cloninger, Gal MishnearXiv 2026

TL;DR

NearFormer is a scalable graph transformer that replaces expensive quadratic self-attention with a randomized near-neighbor (NN) graph. By sampling a few "shortcuts" from a broader geometric neighborhood, it achieves linear complexity $\mathcal{O}(|E| + nk)$, enabling long-range modeling on graphs with tens of thousands of nodes where dense transformers fail.

What Problem Does This Paper Address?

Graph Neural Networks (GNNs) typically rely on local message passing, which is efficient but struggles with long-range dependencies. To move information across a large graph, you need many layers, which leads to two classic problems: over-squashing (information gets lost in bottlenecks) and over-smoothing (node features become indistinguishable).

Graph Transformers solve this by allowing every node to attend to every other node (global attention). However, this comes with a massive $\mathcal{O}(n^2)$ cost in time and memory. While manageable for small molecules, this quadratic wall makes transformers impractical for larger graphs like social networks or high-resolution superpixel grids. Existing sparse attention methods often use random "expander" graphs that don't respect the actual geometry of the data. NearFormer aims to provide a sparse, geometry-aware attention mechanism that scales linearly.

Methods

The core innovation is the Randomized Near-Neighbor (NN) graph. Instead of connecting a node to all other nodes, or just its $k$ closest neighbors, NearFormer connects each node to $k$ random neighbors sampled from a larger candidate pool of $K$ nearest neighbors ($k \ll K$).

Key components include:

  • Randomized Binning: To avoid the $\mathcal{O}(n^2)$ cost of finding exact nearest neighbors, the authors use a randomized binning scheme. Nodes are partitioned into bins based on a fixed representation $Z$ (either raw features or Laplacian eigenvectors). Neighbors are then sampled within these bins.
  • Hybrid Architecture: NearFormer uses a dual-branch layer. One branch performs standard local message passing on the original edges $E$ to preserve local structure. The other branch performs sparse multi-head attention on an augmented graph $E_H = E \cup E_{NN}$.
  • Learned Logit Bias: A variant called NearFormer (NN-bias) introduces a learned scalar that allows the model to prioritize or suppress these "shortcut" NN edges during training.
Figure 1: Randomized near-neighbor attention in one NearFormer layer. (a) ENN connects each v to k random near neighbors in representation Z; EH = E ∪ENN. (b) Local MPNN on E and sparse attention on EH are residual-normalized, summed, and passed through a residual FFN.

Datasets

The authors evaluated NearFormer on three major benchmarks:

  1. Long-Range Ising Model (LRIM): A physics-based node regression task on 2D grids ranging from 256 to 65,536 nodes.
  2. Long Range Graph Benchmark (LRGB): Includes superpixel classification (PascalVOC-SP, COCO-SP), peptide prediction, and molecular link prediction (PCQM-Contact).
  3. ECHO (Extending Communication in Heterogeneous Objects): A benchmark for long-range propagation featuring synthetic topologies (ECHO-Synth) and molecular data (ECHO-Chem).

Key Insights and Ideas

  • Randomization as a Sparsification Strategy: The "aha moment" here is that sampling $k$ nodes from a larger $K$-scale neighborhood provides better connectivity and path compression than simply taking the $k$ closest nodes. This randomization acts as a soft rank cutoff, reaching further into the graph's geometry without increasing the edge budget.
  • Geometry-Aware Rewiring: Unlike random expander overlays (like Exphormer), NearFormer’s shortcuts are grounded in the graph's inherent geometry. This allows the model to create shortcuts between nodes that are "semantically" close even if they are many hops away in the original topology.
  • Mitigating Over-squashing: The authors provide theoretical analysis showing that NN augmentation monotonically reduces shortest-path distances and effective resistance, directly addressing the topological causes of over-squashing.

Key Results

NearFormer demonstrates a superior accuracy-scalability trade-off compared to both local GNNs and dense transformers:

  • Scalability: On the LRIM benchmark, NearFormer remained practical up to the largest grid size (65,536 nodes), whereas dense GraphGPS models ran out of memory (OOM) at just 4,096 nodes.
  • Performance: In the LRGB superpixel tasks, NearFormer variants consistently ranked first or second, significantly outperforming the data-independent Exphormer.
Table 1: LRIM-hard LogMSE ↓(σ = 0.6). Easy results are in Appendix D.4.
  • Distance Summaries: On the ECHO-Synth benchmark, NearFormer (NN) achieved the best results for estimating eccentricity and mean shortest-path distances, proving its effectiveness at capturing distributed structural information.
Table 4: ECHO test MAE (lower is better). A vertical rule separates ECHO-Synth, on the left, from ECHO- Chem, on the right. Best results are colored: first, second, third.

Limitations and Future Work

  • Task Dependency: The advantage of NN edges is not universal. For certain graph-level tasks (like the Peptide datasets), the authors found that routing NN edges through message passing was more effective than using them for attention.
  • Fixed Geometry: Currently, the representation $Z$ used for binning is fixed before training. The authors suggest that learning this representation jointly with the model could further improve performance.
  • Extremal Properties: While great at distributional summaries, the model was less dominant on tasks requiring the recovery of a single graph-wide maximum (like graph diameter).

Summary updated

Abstract

Graph transformers can capture long-range interactions, but their quadratic attention cost limits their use on large graphs. We introduce \emph{NearFormer}, a graph transformer whose global attention is restricted to a randomized near-neighbor (NN) graph. NearFormer uses an efficient randomized construction to connect each node to a small number of near neighbors from a broader neighborhood defined by a label-independent feature or spectral geometry. For a fixed retained budget \(k\), the resulting attention support has linear size \(\mathcal{O}(|E|+nk)\). The NN graph is built once by within-bin search and then held fixed. We use these edges as sparse global-attention supports, with optional learned attention biases, while preserving the original graph for local message passing. Experiments on long-range graph benchmarks and synthetic structural tasks show that NearFormer provides a favorable accuracy--scalability trade-off relative to local message passing, dense and sparse attention.
BibTeX
@misc{parviz2026nearformer,
  author = {Ali Parviz and Alex Cloninger and Gal Mishne},
  title = {{NearFormer: Linear-Size Sparse Attention via Randomized Near Neighbors}},
  year = {2026},
  eprint = {2611.12186},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.12186},
}
Figure