Search

Search papers and lessons

Recipe for a General, Powerful, Scalable Graph Transformer

Ladislav Rampášek, Mikhail Galkin, Vijay Prakash Dwivedi et al. · 2022

A recipe for graph Transformers: encode each node's position and structure, mix in local message passing, and add linear-time global attention that scales to graphs of thousands of nodes.

On this page

  • Colour = topic mix
  • Discrete math
  • Linear algebra
  • Architectures
  • Calculus
  • Neural network basics
  • Probability
  • ReadReady to readNot yet

TL;DR

GPS is a recipe for building a graph Transformer from three parts: an encoding of each node's position and structure, a local message-passing layer, and a global attention layer. The authors show that the global attention can be made linear in the number of nodes and edges, so the model runs on graphs of several thousand nodes, where earlier graph Transformers were limited to a few hundred. Across 16 benchmarks it beats every other graph Transformer the paper compares against on 11 tasks.

Builds on: Transformer, Message Passing Neural Network, Graph Laplacian, Linear Attention, Weisfeiler-Lehman Test · Leads to: the open GraphGPS package, and the question of which encodings a graph Transformer actually needs.

Why it mattered

Before this paper, graph Transformers let every node attend to every other node, which is powerful but costs time that grows with the square of the number of nodes. The authors' main claim is practical: a hybrid layer that pairs message passing with linear attention scales to large graphs without giving up accuracy. The paper's largest test graphs are the MalNet-Tiny function-call graphs, with up to 5,000 nodes each, which the authors describe as far larger than earlier inductive graph benchmarks.

The paper also gave the field a shared vocabulary. It sorts positional and structural encodings into local, global and relative types, and it names the three design choices every hybrid model must make. The GraphGPS package released with the paper lets each choice be swapped without rewriting the model.

Concrete numbers from the paper make the point. On the OGB-LSC PCQM4Mv2 molecular property task, GPS-medium reaches a validation mean absolute error of 0.0858 with 19.4 million parameters, while GRPE (46.2 million parameters), EGT (89.3 million) and Graphormer (48.3 million) score 0.0890, 0.0869 and 0.0864. On ogbg-molpcba, the authors report 196 seconds per epoch for a model of about 6 million parameters, against 883 seconds for SAN on the same GPU type.

The problem

Message passing, the workhorse of Graph Neural Network models, has three known weaknesses that the paper relies on. First, after many layers the node features can become too similar to tell apart, which is called over-smoothing. Second, information from distant nodes must squeeze through every edge in between, which is called over-squashing. Third, a standard message-passing network cannot distinguish some pairs of non-isomorphic graphs: it is no more expressive than the Weisfeiler-Lehman Test.

Global attention fixes the first two problems, because any node can read from any other node in one step. But it creates two new ones. Full attention costs time that grows with the square of the number of nodes. And attention has no built-in sense of where a node sits in the graph, so the graph's structure has to be supplied separately. Fully-connected graph Transformers that did this used a pairwise distance table, which is quadratic in size and does not fit the linear-attention approximations that make large graphs affordable. The paper's answer is to keep the positional information on the nodes and real edges, and to run the global attention on node features only.

How it works

Here is the pipeline in the order it runs, for one graph with N nodes, E edges, node features X, edge features E and adjacency matrix A.

flowchart LR
  G["Graph: nodes X, edges E, adjacency A"] --> P["Positional and structural encodings"]
  P --> L["GPS layer, repeated L times"]
  L --> M["Local branch: MPNN with edge features"]
  L --> T["Global branch: linear attention over all nodes"]
  M --> S["Sum of the two branches"]
  T --> S
  S --> F["Two-layer MLP"]
  F --> H["Task head: node, edge or graph output"]
  1. Compute the encodings once per graph. For each node, the paper uses the eigenvectors of the graph Laplacian as a global positional encoding, and the diagonal of the random-walk matrix as a local structural encoding. For each pair of nodes it can add a relative encoding, such as a distance, that is attached to the edge. These are cheap to store and add to the node features.

  2. Run the local branch. A standard message-passing layer updates each node from its real neighbours and also updates the edge features. Its cost grows with the number of edges, E.

  3. Run the global branch in parallel. The same node features go through a global attention layer in which every node looks at every other node. The paper uses a linear attention mechanism, such as Performer or BigBird, so no full N by N attention matrix is ever built. Edge features are not passed into this branch.

  4. Add the two branches and mix. The outputs of steps 2 and 3 are summed, then passed through a two-layer MLP. The paper's equations leave out residual connections and batch normalization for clarity, but both are used in the implementation.

  5. Repeat and read out. Steps 2 to 4 repeat for L layers. A task head then turns the final node features into a prediction for each node, each edge, or the whole graph by pooling.

A tiny instance. Take a path of three nodes, 1 to 2 to 3, and give only node 1 the feature 1, so X equals (1, 0, 0). Let the local branch simply add up each node's neighbours' features, so node 1 receives from node 2, node 2 receives from nodes 1 and 3, and node 3 receives from node 2. The local output is (0, 1, 0), since only node 2 has a neighbour with a nonzero value.

For the global branch, use a toy attention that gives every node the same weight, so it is the average of all three features: (1 + 0 + 0) divided by 3 is 1/3 for each node. Adding the two branches gives (0, 1, 0) + (1/3, 1/3, 1/3) = (1/3, 4/3, 1/3). Node 2 has moved most, because it is the one that received a message, and every node now carries a little global information. A real layer uses learned weights and a learned attention pattern, but the flow of information is the same.

The math

Definition (one GPS layer). Let Xℓ∈RN×dℓ\mathbf{X}^{\ell} \in \mathbb{R}^{N \times d_\ell} hold the node features at layer ℓ\ell and Eℓ∈RE×dℓ\mathbf{E}^{\ell} \in \mathbb{R}^{E \times d_\ell} hold the edge features. Let A∈RN×N\mathbf{A} \in \mathbb{R}^{N \times N} be the adjacency matrix. A GPS layer computes

XMℓ+1,Eℓ+1=MPNNeℓ(Xℓ,Eℓ,A),XTℓ+1=GlobalAttnℓ(Xℓ),Xℓ+1=MLPℓ(XMℓ+1+XTℓ+1).\begin{aligned} \mathbf{X}_M^{\ell+1}, \mathbf{E}^{\ell+1} &= \mathrm{MPNN}_e^{\ell}\left(\mathbf{X}^{\ell}, \mathbf{E}^{\ell}, \mathbf{A}\right), \\ \mathbf{X}_T^{\ell+1} &= \mathrm{GlobalAttn}^{\ell}\left(\mathbf{X}^{\ell}\right), \\ \mathbf{X}^{\ell+1} &= \mathrm{MLP}^{\ell}\left(\mathbf{X}_M^{\ell+1} + \mathbf{X}_T^{\ell+1}\right). \end{aligned}

Here MPNNeℓ\mathrm{MPNN}_e^{\ell} is a message-passing layer that uses edge features, GlobalAttnℓ\mathrm{GlobalAttn}^{\ell} is a global attention layer with its own learned weights, and MLPℓ\mathrm{MLP}^{\ell} is a two-layer perceptron. The symbol dℓd_\ell is the feature width at layer ℓ\ell.

Step 1 in symbols: the encodings

The positional encoding is built from the eigen-decomposition of the graph Laplacian L\mathbf{L}. If Lui=λiui\mathbf{L}\mathbf{u}_i = \lambda_i \mathbf{u}_i, the vector ui\mathbf{u}_i is an eigenvector, and node jj receives the jj-th entries of the first few eigenvectors as its features.

The local structural encoding uses the random-walk matrix R=D−1A\mathbf{R} = \mathbf{D}^{-1}\mathbf{A}, where D\mathbf{D} is the diagonal matrix of node degrees. The ii-th diagonal entry of Rk\mathbf{R}^{k} is the probability that a walk of kk steps starting at node ii ends where it started. Stacking these return probabilities for several values of kk gives each node a description of the small cycles around it.

Step 2 in symbols: the local branch

The message-passing layer updates each node from its neighbours and each edge from its endpoints. Writing N(i)\mathcal{N}(i) for the set of neighbours of node ii,

xM,iℓ+1=ϕ(xiℓ,∑j∈N(i)ψ(xiℓ,xjℓ,eijℓ)),\mathbf{x}_{M,i}^{\ell+1} = \phi\left(\mathbf{x}_i^{\ell}, \sum_{j \in \mathcal{N}(i)} \psi\left(\mathbf{x}_i^{\ell}, \mathbf{x}_j^{\ell}, \mathbf{e}_{ij}^{\ell}\right)\right),

where ϕ\phi and ψ\psi are small learned functions and eijℓ\mathbf{e}_{ij}^{\ell} is the feature of the edge from ii to jj. The sum runs only over real neighbours, so the cost is proportional to E.

Step 3 in symbols: the global branch

Standard attention for N nodes with query, key and value matrices Q,K,V∈RN×d\mathbf{Q}, \mathbf{K}, \mathbf{V} \in \mathbb{R}^{N \times d} is

Attn(Q,K,V)=softmax(QK⊤d)V.\mathrm{Attn}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \mathrm{softmax}\left(\frac{\mathbf{Q}\mathbf{K}^\top}{\sqrt{d}}\right)\mathbf{V}.

The matrix QK⊤\mathbf{Q}\mathbf{K}^\top has N squared entries, so the cost is about 2N2d2N^2 d multiply-adds. Linear attention, as in Linear Attention, replaces the softmax with a feature map ϕ\phi into m random features, which lets the product be regrouped as ϕ(Q)(ϕ(K)⊤V)\phi(\mathbf{Q})\left(\phi(\mathbf{K})^\top \mathbf{V}\right). The inner product is only m by d, so the cost is about 2Nmd2Nmd, which is linear in N. Because the global branch never sees edge features, it works with node features alone.

Step 4 in symbols: combine and mix

The two branch outputs are added and passed through the MLP, Xℓ+1=MLPℓ(XMℓ+1+XTℓ+1)\mathbf{X}^{\ell+1} = \mathrm{MLP}^{\ell}(\mathbf{X}_M^{\ell+1} + \mathbf{X}_T^{\ell+1}). Adding is the simplest way to combine the two views, and the MLP then lets the layer mix channels. Together, the total cost per layer is O(E)O(E) for the local branch plus O(N)O(N) for the linear global branch. For sparse graphs, where E is proportional to N, the whole layer is linear in N.

Worked examples

Example
Example 1: a single edge (tiny)

Take two nodes joined by one edge, with features x1=2x_1 = 2 and x2=5x_2 = 5.

  1. The local branch sums each node's neighbour. Node 1's only neighbour is node 2, so it receives 5. Node 2 receives 2. The local output is (5,2)(5, 2).
  2. The global branch uses uniform attention, which is the average of all features: (2+5)/2=3.5(2 + 5)/2 = 3.5 for each node. The global output is (3.5,3.5)(3.5, 3.5).
  3. Add the two branches: (5+3.5,  2+3.5)=(8.5,5.5)(5 + 3.5, \; 2 + 3.5) = (8.5, 5.5).
  4. Take the MLP to be the identity for this toy case. The layer output is (8.5,5.5)(8.5, 5.5).

Interpretation: each node now carries both its neighbour's value and a summary of the whole graph.

Example
Example 2: the standard case, cost of attention

Take a graph with N = 5,000 nodes (the largest MalNet-Tiny graphs have up to 5,000), a head width d = 64, and a linear attention with m = 256 random features.

  1. Full attention needs about 2N2d=2×5,0002×642N^2 d = 2 \times 5{,}000^2 \times 64 multiply-adds. Since 5,0002=25,000,0005{,}000^2 = 25{,}000{,}000, this is 3,200,000,0003{,}200{,}000{,}000, or 3.2×1093.2 \times 10^9.
  2. Linear attention needs about 2Nmd=2×5,000×256×642Nmd = 2 \times 5{,}000 \times 256 \times 64 multiply-adds. Since 5,000×256=1,280,0005{,}000 \times 256 = 1{,}280{,}000 and ×64\times 64 gives 81,920,00081{,}920{,}000, times 2 is 163,840,000163{,}840{,}000, or 1.6384×1081.6384 \times 10^8.
  3. The ratio is 3.2×109/1.6384×108≈19.53.2 \times 10^9 / 1.6384 \times 10^8 \approx 19.5.

Interpretation: at this size, the linear version does about 20 times less work per layer. The gap grows with N, because full attention scales with N squared and the linear version with N.

Example
Example 3: a realistic molecule benchmark

The paper reports 196 seconds per epoch for a 6-million-parameter GPS model on ogbg-molpcba, against 883 seconds for SAN on the same GPU type.

  1. Divide the slower time by the faster one: 883/196883 / 196.
  2. Since 196×4.5=882196 \times 4.5 = 882, the ratio is about 4.5.
  3. So the GPS epoch is roughly 4.5 times faster on this dataset.

Interpretation: the speedup is measured on a real chemistry benchmark, and it comes from avoiding the full attention matrix, not from a smaller model. The accuracy comparison is in the Results section.

Try it: a graph has N = 1,000 nodes and d = 32. Using the formula 2N2d2N^2 d, how many multiply-adds does full attention need? Answer: 2×1,0002×32=2×106×32=6.4×1072 \times 1{,}000^2 \times 32 = 2 \times 10^6 \times 32 = 6.4 \times 10^7 multiply-adds.

Results

The table below gives a few headline numbers from the paper's main tables. Lower is better for MAE and higher is better for accuracy, F1 and AP.

BenchmarkMetricGPSClosest comparison in the paper
ZINC (Benchmarking GNNs)MAE, lower is better0.070 ± 0.004SAN: 0.139; GatedGCN: 0.282
ogbg-molhivAUROC0.7880 ± 0.0101SAN: 0.7785
ogbg-molpcbaAverage precision0.2907 ± 0.0028GIN-AK+: 0.2930; CRaWl: 0.2986
ogbg-ppaAccuracy0.8015 ± 0.0033ExpC: 0.7976
ogbg-code2F10.1894 ± 0.0024K-Subtree SAT: 0.1937
PCQM4Mv2 (validation)MAE, lower is better0.0858 (19.4M parameters)Graphormer: 0.0864 (48.3M)
MalNet-TinyAccuracy92.72% ± 0.7 (Performer), 93.36% ± 0.6 (Transformer)Best GIN: about 90%
LRGB Peptides-funcAP0.6535 ± 0.0041SAN+RWSE: 0.6439

What these numbers mean. On the five Benchmarking GNN tasks GPS is state of the art on ZINC and CIFAR10, and in the top three on MNIST, PATTERN and CLUSTER. On the OGB graph tasks it is in the top three on molpcba, ppa and code2, and it beats SAN on molhiv, where the authors note some overfitting. It beats every other graph Transformer on all four OGB tasks except SAT on code2. The MalNet-Tiny result shows the scaling claim in practice: the model is trained on graphs up to 5,000 nodes, and the Transformer version does better than the Performer version at about twice the run time.

The paper's ablations (Table 2) say which parts matter. Removing the local MPNN makes ZINC MAE jump from 0.070 to 0.217, so the local branch is not optional. Replacing full Transformer attention with Performer costs accuracy on CIFAR10 (72.31 to 70.67) and on MalNet-Tiny (93.50 to 92.64). On ZINC, global attention changes nothing, because that task depends mostly on local substructures.

Limits. The authors report that GPS overfits on ogbg-molhiv, and that on PascalVOC-SP the Laplacian positional encodings do not help, since removing them gives a higher F1 of 0.3846. The results use a fixed budget of about 500,000 parameters for the long-range benchmarks and per-dataset hyperparameter settings, so a different budget or setup could change the ranking. The paper hypothesises that explicit edge features are not needed inside the global attention; its experiments support this on the benchmarks it tests, not in general.

Reading guide

Read the paper in this order, and use the lessons listed for each part.

  • Abstract and Section 1 (Introduction). The problems with message passing and the cost of full attention. Needs Message Passing Neural Network and Transformer.
  • Section 2 (Related Work). Graph Transformers and linear Transformers such as Performer and BigBird. Performer is a linear attention method that approximates the softmax with random features; BigBird is a sparse attention pattern in which each position attends to a few local, random and global positions. Needs Linear Attention and Attention Mechanism.
  • Section 3.1 (Positional and structural encodings). The local, global and relative categories in Table 1. Needs Graph Laplacian, Random Walk and Positional Encoding.
  • Section 3.2 (Why PE and SE in MPNN). The counterexamples to 1-WL. Needs Weisfeiler-Lehman Test and Graph Isomorphism.
  • Section 3.3 (GPS layer). The update equations and the modularity, scalability and expressivity arguments. Needs Residual Connection, Batch Normalization and Softmax.
  • Section 4 (Experiments). The benchmark tables. Read it with the Results section above. The GIN paper, How Powerful are Graph Neural Networks?, is the reference for what message passing can and cannot tell apart.
  • Appendices A to D. Dataset details, hyperparameters and the GPS schematic. Read these only when you want to reproduce a number.

Check yourself

QuestionWhat are the three ingredients of the GPS recipe?Show answer

A positional or structural encoding of the nodes, a local message-passing layer that uses real edges, and a global attention layer that lets every node read from every other node. The two layers run in parallel and their outputs are added before an MLP.

QuestionA graph has N = 1,000 nodes and width d = 32. Using full attention cost of 2 N squared d multiply-adds, how many does one layer need?Show answer

2×1,0002×32=6.4×1072 \times 1{,}000^2 \times 32 = 6.4 \times 10^7 multiply-adds. The number grows with the square of N, so doubling N to 2,000 multiplies this cost by four.

QuestionWhy does GPS run the message-passing layer and the global attention layer in parallel instead of stacking many message-passing layers first?Show answer

Stacking many message-passing layers before a Transformer, as in the GraphTrans design the paper cites, risks losing information early through over-smoothing and over-squashing, before the global layer can act. Running a local layer and a global layer side by side at every depth lets each layer keep the local structure while still giving every node a global view.

QuestionYou have a set of molecules with about 30 atoms each. Should you use GPS with linear attention?Show answer

Probably not for the attention choice alone. Linear attention exists to make very large graphs affordable; at 30 nodes the full attention matrix is tiny, and the paper's own ablations show that full Transformer attention beats Performer on several tasks. Keep the full attention if it fits your budget, and keep the message-passing branch, since the paper shows the local branch is essential.

Sources

  • Rampášek, L., Galkin, M., Dwivedi, V. P., Luu, A. T., Wolf, G., Beaini, D. (2022). Recipe for a General, Powerful, Scalable Graph Transformer. arXiv:2205.12454. https://arxiv.org/abs/2205.12454
  • Xu, K., Hu, W., Leskovec, J., Jegelka, S. (2018). How Powerful are Graph Neural Networks? arXiv:1810.00826. https://arxiv.org/abs/1810.00826

Read the full paper

Ladislav Rampášek, Mikhail Galkin, Vijay Prakash Dwivedi et al., 2022. arXiv:2205.12454