Transformers over Directed Acyclic Graphs

Yuankai Luo, Veronika Thost, Lei Shi

Introduction

Graph Neural Networks (GNNs) are the standard for learning from graph data, but they often struggle with "long-range" dependencies because they typically only aggregate information from immediate neighbors. Transformers solve this by allowing every node to attend to every other node, but this introduces two major drawbacks: standard attention has a quadratic $O(N^2)$ complexity, making it prohibitively slow for large graphs, and Transformers are structurally blind, treating graphs like sets of tokens unless structural information (inductive bias) is explicitly injected.

While researchers have developed "Graph Transformers" for undirected graphs (like molecules), Directed Acyclic Graphs (DAGs) present a unique challenge. DAGs—which represent structures like source code Abstract Syntax Trees (ASTs), neural network architectures, and citation networks—have a strict "partial order" or flow. Existing DAG-specific GNNs capture this flow but are computationally slow because they process nodes asynchronously. This paper introduces a specialized Transformer framework for DAGs that replaces standard global attention with a "reachability-based" mechanism, achieving state-of-the-art accuracy while being significantly faster and more memory-efficient.

Methodology: Injecting DAG Structure into Transformers

The authors propose a two-part framework that acts as a "plug-and-play" recipe, capable of being applied to almost any existing Graph Transformer:

  1. DAG Reachability-based Attention (DAGRA): Instead of a node attending to every other node in the graph, it only attends to nodes it can actually "reach" (its descendants) or nodes that can reach it (its ancestors).
  2. Why it works: This faithfully captures the natural flow of information in a DAG.
  3. Efficiency: By ignoring unreachable nodes, the computation becomes much leaner. While standard attention is $O(V^2)$, DAGRA scales with the average size of a node's reachability set, which is often much smaller in sparse graphs.
  4. DAG Positional Encoding (DAGPE): Transformers need to know where a node is located within the structure. The authors adapt the classic sine/cosine encoding functions from the original Transformer paper but apply them to a node's depth (its distance from the "source" nodes of the DAG).
Architecture of the DAG-aware Transformer, illustrating the reachability-based attention mask.

The authors demonstrate that this can be implemented efficiently using message-passing frameworks, avoiding the memory bottlenecks associated with large, dense attention masks.

Key Insights and Theoretical Grounding

  • The Expressivity Trade-off: One might assume that restricting attention to only reachable nodes would result in lost information. However, the authors point out that in most DAGs, all nodes share common "source" nodes. Information can flow from one branch to another through these common ancestors in just two layers, maintaining the expressive power of a global Transformer while operating much faster.
  • Theoretical Connection: The authors prove a connection between their reachability attention and Random Walk theory (specifically a variant of PageRank). They show that their attention mechanism naturally aligns with the limit distribution of a random walk on a DAG.

Datasets and Key Results

The approach was rigorously tested across a diverse set of DAG-based tasks, including ogbg-code2 (Python ASTs), Neural Architecture performance prediction, academic citation networks (Cora, Citeseer, Pubmed), and MalNet-Tiny (function call graphs for malware detection).

The "DAG+" versions of Transformers consistently outperformed both vanilla Transformers and specialized DAG-GNNs across these benchmarks.

Performance and training time comparisons on the ogbg-code2 dataset.
  • Superior Accuracy: Adding the DAG components to state-of-the-art models yielded consistent improvements. For example, on the ogbg-code2 benchmark, DAG+SAT achieved a significantly higher F1 score (20.2%) than the base SAT model (19.4%).
  • Massive Speedups: The efficiency gains are striking. On the ogbg-code2 dataset, the DAG+SAT model took roughly 11 minutes (681s) per epoch, compared to 40 minutes (2437s) for the standard SAT and over 100 minutes (6018s) for the previous SOTA DAG model (DAGNN).
  • Interpretability: Visualization of the attention weights in the study showed that the model successfully ignores "noise" from unrelated nodes and focuses strictly on the structural path relevant to the target node.

Limitations and Future Work

The authors note that there are relatively few high-quality, large-scale DAG datasets currently available for benchmarking, which limits broader evaluation. Furthermore, while the model captures graph structure perfectly, tasks like source code analysis still require better natural language understanding of the tokens within the nodes. Future work could involve more customized modeling for specific types of DAGs, as some structures might benefit from even more specialized inductive biases beyond simple reachability.

Summary updated

BibTeX
@inproceedings{luo2023transformers,
  author = {Yuankai Luo and Veronika Thost and Lei Shi},
  title = {{Transformers over Directed Acyclic Graphs}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=g49s1N5nmO},
}
Figure