CodeTrek: Flexible Modeling of Code using an Extensible Relational Representation

Pardis Pashakhanloo, Aaditya Naik, Yuepeng Wang, Hanjun Dai, Petros Maniatis, Mayur NaikICLR 2022

TL;DR

CodeTrek is a deep learning framework that represents codebases as extensible relational databases, allowing models to leverage both raw syntax and complex program-analysis queries. By using biased random walks over these relational graphs, it captures long-range semantic dependencies that traditional models miss, outperforming state-of-the-art neural models by up to 19% on challenging Python reasoning tasks.

What Problem Does This Paper Address?

Deep learning for code has traditionally relied on two main representations: sequences of tokens (processed by Transformers) or Abstract Syntax Trees (ASTs) (processed by Graph Neural Networks).

However, both have significant blind spots. Transformers struggle with long-range dependencies because the "relevant" context might be thousands of tokens away in a different file. GNNs are often limited by a fixed number of message-passing "hops," meaning they can only see a small local neighborhood.

The authors argue that code reasoning often requires "inter-procedural" context—understanding how a function call in one class affects an exception raised in another. To solve this, we need a representation that is both rich in semantic information (like data flow and call graphs) and flexible enough to traverse long distances without drowning in noise.

Methods

CodeTrek introduces a multi-step pipeline to turn raw source code into a learnable representation:

CodeTrek Relational Pipeline
  1. Codebases as Databases: Using the Semmle/CodeQL framework, the authors convert Python code into a relational database. This includes "base relations" (basic syntax like statements and expressions) and "derived relations" (complex semantic information like "which variable defines this value" or "which function calls another").
  2. Relational Graphs (Rel2Graph): The database is converted into a graph where each tuple (row) is a node. Edges are created based on "referential integrity constraints"—essentially following foreign keys between tables (e.g., a statement node connects to its containing scope node).
  3. Biased Random Walks (Graph2Walks): To extract context, the model performs random walks starting from an "anchor node" (like a placeholder in a bug-detection task). Crucially, these walks are biased: the developer can tell the model to favor certain node types (like statements or expressions) to ensure the walks capture meaningful program logic rather than getting lost in trivial syntax.
  4. The Architecture: Each walk is embedded using a Transformer encoder. Because a single task instance might generate many walks, the model uses a DeepSet architecture to aggregate these embeddings into a single, order-invariant representation for final prediction.

Datasets

The authors evaluated CodeTrek using the ETH Py150 Open corpus, which contains 125,000 Python modules. They tested on four diverse tasks:

  • Variable Misuse: Predicting if the wrong variable was used in a specific location.
  • Exception Prediction: Predicting which of 20 built-in Python exceptions should be caught in a try-except block.
  • Unused Definition (New): Predicting if a variable assignment is never actually used.
  • Variable Shadowing (New): Detecting when a local variable has the same name as a variable in an outer scope, "hiding" it.

The latter two tasks were introduced by the authors as "declarative" benchmarks—tasks that can be solved by logic queries but serve as a "litmus test" for whether a neural model can learn complex, non-local rules.

Key Insights and Ideas

  • Declarative Power: The "extensible" part of the title is key. If a developer realizes the model needs better data-flow information, they don't need to redesign the neural architecture; they simply write a CodeQL query to add a new relation to the database, and CodeTrek automatically incorporates it into the graph.
  • Semantic Shortcuts: By including call graphs as edges, CodeTrek allows the random walks to "jump" across function boundaries. This effectively turns a long-range dependency into a single step in the graph.
  • Distance vs. Relevance: The paper demonstrates that "distance" in the code (number of lines or AST hops) isn't the best metric for relevance. A biased walk that follows control-flow edges is far more efficient at finding the "smoking gun" for a bug than a model that simply looks at everything within a 10-line radius.

Key Results

CodeTrek consistently outperformed top-tier models like CuBERT (a BERT-based model for code) and GREAT (a graph-augmented Transformer):

  • Accuracy Boost: CodeTrek achieved 91% accuracy on Variable Misuse and 63% on Exception Prediction (outperforming GREAT by 19 points on the latter).
  • Robustness: On "out-of-distribution" tests (real-world bugs from GitHub), CodeTrek maintained a significant lead, showing that its reliance on relational structure makes it less prone to "memorizing" specific variable names compared to token-based models.
  • Ablation Success: Removing the "derived relations" (the complex analysis queries) caused a massive drop in accuracy (from 63% to 45% on Exception prediction), proving that the "relational" approach is doing the heavy lifting.

Limitations and Future Work

  • Dependency on External Tools: CodeTrek relies on the Semmle/CodeQL compiler to build its database. While powerful, this adds a heavy pre-processing step compared to simple tokenization.
  • Manual Bias Tuning: Currently, the "biases" for the random walks (e.g., "look at statements 5x more often than modules") are set manually by the task designer. The authors suggest that learning these walk specifications automatically is a promising future direction.
  • Pre-training Gap: While CodeTrek beats pre-trained models like CuBERT on most tasks, it actually performed slightly worse on EXCEPTION-FUN (a simpler, function-level version of the task). This suggests that for very local tasks, the massive pre-training of Transformers on millions of files still provides a "memorization" advantage that CodeTrek's structural approach doesn't yet match.

Summary updated

BibTeX
@inproceedings{pashakhanloo2022codetrek,
  author = {Pardis Pashakhanloo and Aaditya Naik and Yuepeng Wang and Hanjun Dai and Petros Maniatis and Mayur Naik},
  title = {{CodeTrek: Flexible Modeling of Code using an Extensible Relational Representation}},
  booktitle = {International Conference on Learning Representations},
  year = {2022},
  url = {https://openreview.net/forum?id=q35OLfanFF},
}
Figure