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.
Datasets
The authors evaluated NearFormer on three major benchmarks:
- Long-Range Ising Model (LRIM): A physics-based node regression task on 2D grids ranging from 256 to 65,536 nodes.
- Long Range Graph Benchmark (LRGB): Includes superpixel classification (PascalVOC-SP, COCO-SP), peptide prediction, and molecular link prediction (PCQM-Contact).
- 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.
- 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.
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