Depth-Width Tradeoffs for Transformers on Graph Tasks
Transformers are incredibly powerful, but how large do they need to be to solve complex graph algorithms? This paper demonstrates that by scaling a transformer's width (embedding dimension) linearly with the size of a graph, many complex tasks can be solved using only a constant depth (number of layers). Empirically, these "shallow and wide" models match the accuracy of "deep and narrow" ones but run significantly faster due to the highly parallel nature of modern GPU hardware.
The Depth vs. Width Dilemma
When designing transformer architectures for algorithmic reasoning on graphs, practitioners face a fundamental choice: should the model be deep (many layers) or wide (large embedding dimension)?
Previous theoretical work showed that if a transformer has a sub-linear width, it needs at least logarithmic depth to solve basic problems like graph connectivity. This paper flips the question: What algorithmic capabilities emerge if we fix the depth to a constant, but allow the width to grow linearly with the graph size?
Understanding this trade-off is crucial for efficiency. Modern hardware, such as GPUs, parallelizes computations across a layer's width but must process layers sequentially. Therefore, a "shallow and wide" model can be executed much faster than a "deep and narrow" one, provided it has the same reasoning capacity.
Theoretical Expressivity and the Width Hierarchy
The authors tackle this problem using communication complexity and the Massively Parallel Computation (MPC) framework to prove upper and lower bounds on the width required to solve specific graph tasks with constant-depth transformers.
A key insight is the establishment of a width complexity hierarchy. You don't always need deep networks for complex reasoning. A constant-depth transformer can solve tasks like detecting cycles or computing the powers of an adjacency matrix if its width scales linearly ($O(n)$) with the number of nodes.
The hierarchy breaks down as follows:
- Constant width ($O(1)$): Sufficient for local tasks like computing node degrees.
- Linear width ($O(n)$): Necessary and sufficient for cycle detection (e.g., 2-cycle detection).
- Sub-quadratic width ($O(n^{2-1/k})$): Required for counting subgraphs of size $k$.
- Quadratic width ($O(n^2)$): Strictly necessary for highly complex global tasks like Eulerian cycle verification.
Empirical Validation: Hardware Loves Width
To feed graphs into the transformer, the authors use a specific "node-adjacency" tokenization scheme. Instead of feeding a list of edges (which requires $O(n^2)$ tokens for dense graphs), each token represents a single node and its connections—essentially, a row of the graph's adjacency matrix.
They trained standard PyTorch transformers with a fixed parameter budget (e.g., ~100k parameters) but varying depth-to-width ratios on both synthetic graphs and real-world molecular property prediction datasets from the Open Graph Benchmark (OGB).
The theoretical findings translate beautifully to practice. Because GPUs parallelize across width but not depth, shallow/wide models are drastically faster to train and run than deep/narrow models, without sacrificing accuracy.
For example, on a 100-node connectivity task, a 1-layer, 125-width transformer trained and inferred significantly faster than a 10-layer, 40-width transformer, while achieving the exact same loss and accuracy curves. Furthermore, using the adjacency-row tokenization improved ROC-AUC scores over standard edge-list tokenization on real-world datasets like ogbg-molhiv (61.87 vs 54.01) and ogbg-molbbbp (67.63 vs 64.73).
Limitations and Future Work
While promising, the mathematical proofs assume the MLP layers inside the transformer can compute arbitrary functions (though bounded to polynomial size). Furthermore, the $O(1)$ depth model for subgraph counting still requires overall computation time that is exponential in the subgraph size $k$. Additionally, the adjacency node embedding used is not inherently permutation invariant, a common challenge in graph transformers.
Future work will likely explore the theoretical trade-offs of other graph tokenization schemes, such as spectral embeddings (Laplacian eigenvectors), and delve deeper into the optimization and generalization capabilities of these models beyond their theoretical expressivity.
Summary updated