arXiv:2611.12186cs.LGNov 2026

Manuscript Offprint

arXiv:2611.12186

NearFormer: Linear-Size Sparse Attention via Randomized Near Neighbors

Ali Parviz, Alex Cloninger, Gal Mishne

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Graph transformers can capture long-range interactions, but their quadratic attention cost limits their use on large graphs. We introduce 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 kk, the resulting attention support has linear size O(∣E∣+nk)\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.

1 INTRODUCTION

Graph learning supports applications involving relational data, including molecular systems, social networks, and knowledge graphs. Most graph neural networks (GNNs) follow the message-passing paradigm, in which a node iteratively aggregates information from its local neighborhood (Gilmer et al., 2017; Hamilton et al., 2017). Although this locality provides a useful inductive bias, information between distant nodes must traverse many layers. Deep message passing can suffer from over-smoothing (Oono and Suzuki, 2020) and over-squashing (Alon and Yahav, 2020; Topping et al., 2022). Its distinguishing power is also gen-

erally bounded by the 1-Weisfeiler-Lehman test (Morris et al., 2019; Xu et al., 2018).

Graph transformers offer a complementary approach by allowing attention to model long-range interactions directly. Recent architectures encode graph structure through positional or structural representations and combine global attention with local message passing (Kreuzer et al., 2021; Ying et al., 2021; Mialon et al., 2021; Rampásek et al., 2022; Black et al., 2024). Dense self-attention provides this flexibility by forming all node pairs, which requires O(n2)\mathcal{O}(n^2) time and memory for a graph with nn nodes. This cost is manageable for small molecular graphs but becomes prohibitive on larger graphs. Generic sparse or linear-attention methods such as BigBird and Performer reduce this asymptotic burden (Zaheer et al., 2020; Choromanski et al., 2021), yet their sparsity patterns were developed primarily for sequences and need not reflect the geometry of an arbitrary graph.

Graph-specific sparse attention should add long-range communication channels with few edges, and those channels should reflect graph data rather than an arbitrary node ordering. Existing methods reduce the edge count with data-independent random expander overlays (Shirzad et al., 2023) or modify the input topology according to curvature, spectral, or effective-resistance criteria (Karhadkar et al., 2023; Banerjee et al., 2022; Black et al., 2023; Arnaiz-Rodríguez et al., 2022). Expander overlays are efficient but do not adapt to the geometry of a particular graph, whereas graph rewiring can require costly structural optimization and changes the graph on which local message passing is defined.

Here, we introduce NearFormer, a scalable graph transformer based on randomized near-neighbor (NN) attention. We construct a near-neighbor graph in a task-relevant, label-independent geometry, where each node retains kk randomly selected neighbors from a broader neighborhood at the scale of its KK nearest neighbors (Linderman et al., 2020). The geometry may be defined by node attributes or graph Laplacian eigenvectors. Retaining kk neighbors per node yields at most 2nk2nk directed edge instances after symmetrization, and hence linear edge complexity for fixed kk. In contrast to KK-nearest-neighbor graphs, NN separates the size of the conceptual neighborhood from the number of retained edges: it obtains a randomized sample at that geometric scale without constructing all KK nearest-neighbor connections.

NearFormer preserves the original edge set for local message passing and uses NN edges as a separate relation for long-range attention. This separation avoids conflating a learned communication pattern with the physical or semantic topology of the input graph. The NN edges can be used as a hard sparse-attention support with an optional bounded, learnable logit bias, allowing the model to suppress or favor NN connections without densifying attention.

Main contributions. We propose randomized near-neighbor attention, a label-independent construction that samples kk edges at each node from a geometric neighborhood of scale KK, yielding linear edge complexity for fixed kk. NearFormer constructs this graph using feature or spectral geometry and employs NN edges as sparse global-attention supports or learned biases while preserving the input graph for local message passing. We evaluate NearFormer on long-range graph benchmarks and synthetic structural tasks against dense and sparse attention and local message passing, characterizing its accuracy-scalability trade-off.

References (50)