VecFormer: Towards Efficient and Generalizable Graph Transformer with Graph Token Attention

Jingbo Zhou, Jun Xia, Siyuan Li, Yunfan Liu, Wenjun Wang, Yufei Huang, Changxi Chi, Mutian Hong, Zhuoli Ouyang, Shu Wang, Zhongqi Wang, Xingyu Wu, Chang Yu, Stan Z. Li

TL;DR

VecFormer introduces a vector-quantized approach to Graph Transformers, shifting global self-attention from individual nodes to a compressed set of "Graph Tokens." This dual-stage framework achieves linear computational complexity $O(N)$ while significantly boosting out-of-distribution (OOD) generalization performance across large-scale and heterophilic graph datasets.

What Problem Does This Paper Address?

Graph Transformers (GTs) have emerged as a powerful tool for graph representation learning because their global self-attention mechanisms can capture long-range dependencies that traditional Graph Neural Networks (GNNs) miss. However, standard GTs hit two major walls:

  1. Scalability Bottleneck: Computing attention between every pair of nodes results in a quadratic computational complexity of $O(N^2)$, making them unusable for large-scale graphs with millions of nodes.
  2. Poor Out-of-Distribution (OOD) Generalization: Standard GTs perform attention at the node level. When the test graph's distribution shifts from the training graph (a common real-world scenario), node-level attention weights tend to collapse into a uniform distribution, rendering the attention mechanism useless.

The authors of this paper ask: Can we design a Graph Transformer that is both highly efficient (linear time) and robust to distribution shifts?

Methods

VecFormer tackles these challenges using a novel two-stage training paradigm based on vector quantization:

  1. Stage 1: Graph Codebook Training To capture the dual nature of graph data (features and topology), VecFormer trains two separate codebooks: a Feature Codebook and a Structure Codebook. Instead of hard assignment, it uses a differentiable Soft Vector Quantization (SoftVQ) mechanism to map GNN-encoded node representations to a weighted combination of codebook vectors. These feature and structure tokens are then combined via a Token Fusion module to create semantic Graph Tokens that compactly represent the graph's data distribution. The codebooks are optimized using self-supervised reconstruction losses for features, structure, and joint graph representations.

  2. Stage 2: Graph Transformer Finetuning During the finetuning phase, the model bypasses node-to-node attention entirely. Instead, it performs cross-attention between the nodes' quantized representations (acting as queries) and a fixed-size, context-aware Graph Token List derived from the codebooks (acting as keys and values). Because the number of Graph Tokens ($N_f \times N_s$) is small and constant, the attention complexity drops from $O(N^2)$ to a linear $O(N)$.

Illustration of the VecFormer’s architecture and the training process.

Datasets

The authors evaluated VecFormer across a comprehensive suite of benchmarks:

  • Homophilic Graphs: CoraFull, Pubmed, Computer, Photo, CS, and Physics.
  • Heterophilic Graphs: Chameleon and Squirrel (benchmarked with redundant nodes removed).
  • OOD Scenarios: Cora and Citeseer (with synthetically injected spurious features), and Twitch (subgraph-level partitioned datasets exhibiting structural shifts).
  • Large-Scale Graphs: Ogbn-proteins, Amazon2m (2.4M nodes), and Pokec (1.6M nodes).
  • Biological Application: The Virtual Cell Challenge (VCC) dataset, evaluating single-cell RNA-seq data across 221,273 cells and 18,080 genes for gene perturbation prediction.

Key Insights and Ideas

  • Token-Level Attention Cures OOD Collapse: In OOD settings, standard node-level attention vectors often become orthogonal or highly dissimilar, causing the softmax function to output a flat, uniform distribution. By routing attention through quantized Graph Tokens that represent the underlying data distribution, the mathematical lower bound of the attention dot-product remains above zero. This allows the model to retain diverse, meaningful attention patterns even under distribution shifts.
Standard deviation of attention weights and performance evaluation of Node Level Attention and Graph Token Attention in OOD setting.
  • Orthogonal Efficiency: While other linear-complexity Graph Transformers rely on complex kernel functions to approximate full attention, VecFormer achieves linearity simply by reducing the number of tokens that require attention. This makes the architecture simple, hardware-friendly, and fully compatible with standard attention mechanisms.
  • Joint Feature-Structure Modeling: Because node features and neighborhood topologies are inherently correlated, optimizing separate feature and structure codebooks before fusing them ensures that the resulting tokens preserve both semantic and structural context.

Key Results

  • State-of-the-Art OOD Performance: On OOD datasets, VecFormer significantly outperformed specialized graph OOD frameworks (like CaNet) and standard GTs. It achieved an absolute accuracy improvement of 8.67% on Cora OOD and 16.91% on Citeseer OOD compared to the best-performing baseline Graph Transformers.
  • High Accuracy on Standard Tasks: VecFormer achieved top-tier performance on homophilic and heterophilic graphs, scoring 92.51% on the Computer dataset and 97.17% on Physics.
  • Scalability to Millions of Nodes: On large datasets like Amazon2m, VecFormer delivered an accuracy of 89.72% while maintaining a lower training time per epoch and a highly competitive GPU memory footprint compared to other linear models like NodeFormer and SGFormer.
  • Real-World Biological Impact: When applied to gene perturbation prediction on the VCC dataset, VecFormer improved the Differential Expression Score (DES) over standard GCN and GAT backbones by up to +3125.8% on dense graph configurations, demonstrating strong robustness to graph construction choices.

Limitations and Future Work

While VecFormer demonstrates excellent generalization and efficiency, the authors point out a few directions for future exploration:

  1. Graph Imbalance: The current model does not explicitly account for highly skewed class distributions, which is a common issue in real-world node classification.
  2. Graph Foundation Models: The authors propose that vector quantization techniques like the ones used in VecFormer could serve as a foundational building block for designing large-scale, generative graph foundation models.

Summary updated

BibTeX
@inproceedings{zhou2026vecformer,
  author = {Jingbo Zhou and Jun Xia and Siyuan Li and Yunfan Liu and Wenjun Wang and Yufei Huang and Changxi Chi and Mutian Hong and Zhuoli Ouyang and Shu Wang and Zhongqi Wang and Xingyu Wu and Chang Yu and Stan Z. Li},
  title = {{VecFormer: Towards Efficient and Generalizable Graph Transformer with Graph Token Attention}},
  booktitle = {The Web Conference},
  year = {2026},
  doi = {10.1145/3774904.3792453},
}
Figure