Language-Agnostic Representation Learning of Source Code from Structure and Context

Daniel Zügner, Tobias Kirschstein, Michele Catasta, Jure Leskovec, Stephan Günnemann

Introduction

In the domain of Machine Learning for Code (ML4Code), researchers have traditionally fallen into one of two camps. The "Context" camp treats source code like natural language, utilizing sequence models such as LSTMs or Transformers to process tokens. Conversely, the "Structure" camp treats code as a complex graph, employing Graph Neural Networks (GNNs) to traverse Abstract Syntax Trees (ASTs).

Both paradigms have inherent limitations. Sequence models often fail to capture the deep logical connections hidden within a program's hierarchy. GNNs, being "local" by nature, struggle to connect pieces of code that are logically related but separated by many "hops" in the syntax tree. Furthermore, previous attempts to combine these approaches have heavily relied on language-specific features—like data-flow graphs—which require complex, specialized static analysis tools for every new programming language introduced.

Context vs. Structure Distances

To address these challenges, researchers introduce the Code Transformer, a novel model that bridges the gap between treating source code as simple text and treating it as a complex graph. By jointly learning from both raw code (context) and ASTs (structure) using a language-agnostic approach, it achieves state-of-the-art results in code summarization across five languages and stands as the first successful multilingual model for this task.

The Code Transformer Approach

The Code Transformer builds upon the XLNet architecture. Its core innovation lies in the use of Relative Positional Encodings that incorporate multiple types of distances between any two tokens, effectively merging structural and contextual signals:

  1. Context Distance: The number of tokens separating two elements in the raw text sequence.
  2. AST Shortest Path: The number of steps required to traverse from one node to another within the syntax tree.
  3. Ancestor/Sibling Distances: Metrics that preserve the "direction" of the tree (e.g., distinguishing whether a node is a parent or a sibling), a crucial detail often lost in simple shortest-path calculations.
  4. Personalized PageRank (PPR): A graph-theory metric utilized to capture the local importance and connectivity of nodes, helping the model focus on semantically relevant neighbors.
Code Transformer Architecture

Crucially, these features are entirely language-agnostic. As long as a standard AST parser is available—which is true for almost all modern programming languages—the model can be trained without the need for custom static analysis pipelines.

Key Insights and Results

The model was rigorously evaluated on two major benchmarks for code summarization (the task of predicting a function's name based on its body): CodeSearchNet (covering Python, JavaScript, Ruby, and Go) and the Java-small dataset (containing roughly 700,000 function definitions).

The Synergy of Structure and Context

The results demonstrate that "Structure" and "Context" are highly complementary. A model that understands a variable is 26 tokens away in the text and only 5 hops away in the AST is significantly better equipped to grasp the code's underlying intent. Ablation studies revealed that Personalized PageRank (PPR) is the "secret sauce" among individual structural distance metrics, providing a global-local balance that simple hop-counts miss.

Multilingual Transfer Learning

A major breakthrough of the Code Transformer is its multilingual synergy. Training a single model on multiple languages simultaneously improved performance across all of them. These gains were most pronounced for "low-resource" languages like Ruby; the multilingual version achieved an F1 score of 31.24, compared to 27.50 for the version trained exclusively on Ruby. The model effectively leveraged its structural knowledge of Python and Java to better understand Ruby.

Interestingly, training a multilingual model using only raw text (without ASTs) failed to produce these cross-language gains. This strongly suggests that the underlying structural logic of programming is the "universal language" enabling effective knowledge transfer. Qualitative analysis further supported this, showing the model could map semantically similar functions from different languages (e.g., a Python parseBool and a JavaScript getBooleanValue) to the same area in its internal embedding space, even without parallel translation data.

Limitations and Future Work

While the Code Transformer achieved state-of-the-art performance, outperforming existing models like code2seq and GREAT across all five tested languages, the authors noted an anomaly with the Go language. Adding a "pointer network"—designed to allow the model to copy variable names directly from the input—actually degraded performance on the Go test set. This was traced to a data shift: the test set for Go featured significantly fewer overlapping tokens between the function body and the name than the training set.

Future work aims to extend this language-agnostic structural approach to more complex tasks, such as automated bug detection or code generation, where grasping the logical structure of a program is even more critical than in summarization.

Summary updated

BibTeX
@inproceedings{zugner2021language,
  author = {Daniel Zügner and Tobias Kirschstein and Michele Catasta and Jure Leskovec and Stephan Günnemann},
  title = {{Language-Agnostic Representation Learning of Source Code from Structure and Context}},
  booktitle = {International Conference on Learning Representations},
  year = {2021},
}
Figure