Quant Memo
Advanced

Graph Transformers and Structural Encodings

A transformer lets every token attend to every other token directly, with no locality assumption at all — applying that idea to graphs means letting any node attend to any other node, as long as the model is separately told, through a structural encoding, where each node actually sits in the graph.

Prerequisites: Graph Attention Networks (GAT), Learned Positional Embeddings

An ordinary message-passing GNN only lets a node see its direct neighbors at each layer, so information from a node 6 hops away needs 6 stacked layers to arrive — and stacking that many layers causes over-smoothing. A transformer has no such restriction: every token attends to every other token in a single layer, regardless of distance. Applying that idea to a graph — let every node attend to every other node directly — sounds like it should just work. It doesn't, for one specific reason: a transformer over words gets told where each word sits via a positional encoding (this is word 1, that is word 7), but a graph has no natural left-to-right order to encode. A graph transformer has to solve that problem first, with a structural encoding, before attention over the whole graph means anything.

The analogy: a conference call with no seating chart

Imagine dialing into a conference call where anyone can speak to anyone, with no restriction on who addresses whom — that's full attention, exactly like a plain transformer. Now imagine you're also told nothing about who these people are relative to each other: no org chart, no sense of who reports to whom, who's on the same team, who's a stranger. You could technically still listen to everyone, but you'd have no idea how to weigh what anyone says — a comment from your direct manager and a comment from someone in an unrelated department would look identical. A graph transformer without a structural encoding is exactly this: full attention with no sense of graph structure at all, which throws away the one thing that made it a graph problem in the first place.

Structural encodings: telling the model where things sit

A structural (or positional) encoding for a graph is any feature computed from the graph's topology and added to each node before attention, so the model has some signal about position and relationships, even though attention itself ignores distance. Two common choices:

Laplacian eigenvectors: use the smallest few eigenvectors of the graph Laplacian L=DAL = D - A as a coordinate system for each node — nodes that are structurally close end up with similar eigenvector coordinates, giving the model something like a graph-native version of sine/cosine positional encodings.

Shortest-path distance encodings: directly compute, for every pair of nodes (i,j)(i,j), their shortest-path distance d(i,j)d(i,j) in the graph, and feed that distance into the attention computation as a bias term:

attention(i,j)exp ⁣(qikj+bd(i,j))\text{attention}(i,j) \propto \exp\!\big(q_i \cdot k_j + b_{d(i,j)}\big)

In words: the usual query-key dot product qikjq_i \cdot k_j from standard attention gets an extra additive bias bd(i,j)b_{d(i,j)} that depends only on how many hops apart ii and jj are — a learned penalty (or bonus) per distance, so the model can still favor nearby nodes without being restricted to them the way a message-passing GNN is.

Worked example 1: shortest-path bias by hand

Take a 4-node path graph 1 ⁣ ⁣2 ⁣ ⁣3 ⁣ ⁣41\!-\!2\!-\!3\!-\!4. Shortest-path distances: d(1,2)=1d(1,2){=}1, d(1,3)=2d(1,3){=}2, d(1,4)=3d(1,4){=}3, d(2,3)=1d(2,3){=}1, d(2,4)=2d(2,4){=}2, d(3,4)=1d(3,4){=}1. Suppose the learned distance bias is b1=0.5b_1 = 0.5, b2=0.0b_2 = 0.0, b3=1.0b_3 = -1.0 (closer pairs get boosted, farther pairs get penalized), and raw query-key scores qikjq_i \cdot k_j happen to all equal 1.01.0. Attention logit for node 1 attending to node 2: 1.0+0.5=1.51.0 + 0.5 = 1.5. For node 1 attending to node 3: 1.0+0.0=1.01.0 + 0.0 = 1.0. For node 1 attending to node 4: 1.0+(1.0)=0.01.0 + (-1.0) = 0.0. After softmax, node 1 assigns its highest attention weight to node 2 (nearest), decreasing weight to node 3, and least to node 4 (farthest) — full attention across the whole graph, but shaped by structure rather than blind to it.

Worked example 2: comparing to a 2-layer GNN's blind spot

The same 4-node path graph, run through a 2-layer mean-aggregation GNN: node 1 can only ever see up to 2 hops away (nodes 2 and 3), and has literally zero information from node 4 in its final representation — not "low attention," but no path for information to arrive at all. The graph transformer above, in a single layer, gave node 1 a (small but nonzero) attention weight to node 4, computed directly from the shortest-path bias b3=1.0b_3 = -1.0. That is the concrete capability gap: a shallow GNN cannot represent long-range dependencies without stacking more layers (risking over-smoothing), while a graph transformer can weigh a distant node lightly in one layer rather than never seeing it at all.

Function explorer
-2260.1
x = 1.00f(x) = 2.718

The decaying curve above is the same shape a distance bias typically takes — attention weight highest for near neighbors, falling off (but never hitting exactly zero) as graph distance grows.

A graph transformer lets every node attend to every other node directly, avoiding the multi-hop bottleneck of message-passing GNNs — but attention alone has no sense of graph structure, so it must be paired with a structural encoding (Laplacian eigenvectors, shortest-path distance biases) that tells the model where each node actually sits relative to the others.

What this means in practice

Graph transformers are used where long-range dependencies matter and the graph is small-to-medium sized — a supply-chain graph where a shock 8 hops away still matters, or a molecule/company-relationship graph where full pairwise attention is computationally feasible. The cost is quadratic in the number of nodes (every node attends to every other node), which makes graph transformers impractical for graphs with millions of nodes, where a spatial message-passing GNN (accepting the multi-hop limitation) remains the practical default. Choosing the structural encoding is not a minor detail — a poorly chosen one gives the transformer no real graph awareness at all, functionally reducing it to a set-based model that happens to ignore the graph entirely.

Practice

  1. For the 4-node path graph, compute the attention logits for node 2 attending to nodes 1, 3, and 4 using the same biases as worked example 1.
  2. Explain in one sentence why a 2-layer GNN literally cannot see 3 hops away, while a graph transformer's attention weight to a distant node is small but not zero.
  3. Name one financial graph where full pairwise attention (a graph transformer) would be computationally impractical, and explain why.

The common confusion is assuming a plain transformer applied to node features, with no structural encoding at all, already counts as a "graph" model just because the input happened to come from a graph. Without a structural encoding, attention treats the nodes as an unordered set with no notion of edges, distance, or topology whatsoever — it is not a weaker graph model, it is not a graph model at all, regardless of how well it happens to fit the training data.

Related concepts

Practice in interviews

Further reading

  • Dwivedi & Bresson, A Generalization of Transformer Networks to Graphs (2021)
  • Ying et al., Do Transformers Really Perform Bad for Graph Representation? (Graphormer, 2021)
ShareTwitterLinkedIn