Enhancing Graph Transformers with Hierarchical Distance Structural Encoding

Yuankai Luo, Hongkang Li, Lei Shi, Xiao-Ming Wu

TL;DR

Graph Transformers often struggle to capture the multi-level, community-based structures inherent in real-world networks and scale poorly to large datasets. This paper introduces Hierarchical Distance Structural Encoding (HDSE), a novel method that injects multi-level graph hierarchy distances directly into the attention mechanism, mathematically boosting model expressivity and enabling linear-time scaling to massive billion-node graphs.

What Problem Does This Paper Address?

While Transformers have taken the NLP and Vision worlds by storm, applying them to graph data is tricky. Unlike Message-Passing Graph Neural Networks (MPNNs), which inherently rely on the graph's edges, Transformers treat inputs as a fully connected set of nodes. To understand the graph's topology, they rely on "positional encodings" (like Shortest Path Distance).

However, standard positional encodings fail to capture hierarchical or community structures—such as functional groups in a molecule or tight-knit communities in a social network. Furthermore, the standard global attention mechanism has a quadratic $O(N^2)$ complexity, making it notoriously prone to out-of-memory (OOM) errors on large-scale graphs with millions of nodes. The authors tackle both the lack of hierarchical inductive bias and the quadratic scaling bottleneck.

Methods

The authors propose Hierarchical Distance Structural Encoding (HDSE), which works in two main phases depending on the scale of the graph:

Architecture of HDSE
  1. Graph Hierarchy Distance (GHD): Instead of just measuring the shortest path between node A and node B, the authors use graph coarsening algorithms (like METIS, Louvain, or Newman) to group nodes into "super-nodes," creating a multi-level hierarchy. GHD measures the shortest path between nodes at these higher, coarsened levels.
  2. Standard HDSE (For smaller graphs): The distances across all hierarchy levels (from the base graph up to level $K$) are concatenated and passed through a Multi-Layer Perceptron (MLP). This outputs a learned structural bias matrix that is added directly to the standard Transformer attention scores ($A + H$).
  3. High-level HDSE (For large-scale graphs): To bypass the $O(N^2)$ bottleneck, the authors adapt linear transformers (inspired by Linformer). Instead of computing attention between all pairs of nodes, the model computes attention between the input nodes and the higher-level clusters (super-nodes). This reduces the computational complexity to $O(N)$ while still allowing nodes to gather global, hierarchical context.

Datasets

The authors evaluated their method extensively across 18 benchmark datasets:

  • Graph-level Tasks (7 datasets): ZINC, MNIST, CIFAR10, PATTERN, CLUSTER (from Benchmarking GNNs), and Peptides-func, Peptides-struct (from the Long-Range Graph Benchmark).
  • Large-scale Node Classification (11 datasets): Ranging from smaller citation networks (Cora, CiteSeer, PubMed, Actor, Squirrel, Chameleon) to massive Open Graph Benchmark (OGB) datasets (ogbn-proteins, ogbn-arxiv, arxiv-year, ogbn-products, and the massive ogbn-papers100M with ~111 million nodes).

Key Insights and Ideas

  • Provably More Expressive: The authors mathematically prove (using the Generalized Distance Weisfeiler-Leman test) that HDSE is strictly more expressive than standard Shortest Path Distance (SPD). HDSE can successfully distinguish non-isomorphic graphs (like the Dodecahedron vs. Desargues graphs) that SPD-based models treat as identical.
Graph Coarsening and Expressivity
  • Coarsening Algorithms are Domain-Specific: The success of the hierarchy depends on how you build it. The Newman algorithm works best for extracting chemical motifs in small molecular graphs, while the METIS algorithm is crucial for large-scale graphs due to its highly efficient $O(|E|)$ linear time complexity.
  • Interpretable Attention: Visualizing the attention weights reveals that HDSE successfully shifts the Transformer's focus from scattered individual nodes to cohesive community structures and chemical motifs.

Key Results

  • Plug-and-Play Performance Boost: Adding HDSE to existing baseline models (like vanilla Graph Transformer, SAT, GraphGPS, and GRIT) consistently improved performance. Notably, GraphGPS + HDSE achieved a massive 6.21% improvement in Average Precision on the challenging Peptides-func dataset.
  • Massive Scalability: By integrating High-level HDSE into the GOAT architecture, the model successfully trained on the ogbn-papers100M dataset (111M nodes, 1.6B edges) without running out of memory.
  • Speed Meets Accuracy: On large graphs, the HDSE-equipped model matched the training speed of the fastest linear graph transformers (like SGFormer) while consistently outperforming them in classification accuracy.

Limitations and Future Work

The authors note that increasing the maximum hierarchy level beyond 1 ($K > 1$) only yielded marginal performance improvements. They hypothesize that this is a limitation of the off-the-shelf graph coarsening algorithms used, which may not perfectly capture deeper hierarchical community structures.

Future Work: The primary promising direction is the development of novel, highly effective coarsening algorithms specifically tailored for graphs with complex hierarchical community structures. This would theoretically unlock the full potential of higher $K$ levels in the HDSE framework.

Summary updated

BibTeX
@inproceedings{luo2024enhancing,
  author = {Yuankai Luo and Hongkang Li and Lei Shi and Xiao-Ming Wu},
  title = {{Enhancing Graph Transformers with Hierarchical Distance Structural Encoding}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=U4KldRgoph},
}
Figure