InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees

Nghi D. Q. Bui, Yijun Yu, Lingxiao Jiang

TL;DR

InferCode introduces a self-supervised learning framework that learns general-purpose code representations by predicting subtrees within an Abstract Syntax Tree (AST). By treating subtrees as "labels" that are automatically extracted from unlabeled source code, the model creates versatile embeddings that outperform task-specific models across clustering, clone detection, and cross-language search.

What Problem Does This Paper Address?

In the world of Machine Learning for Software Engineering (ML4SE), most models suffer from a "specialization" problem. If you train a model to predict method names (like code2vec), it becomes very good at that specific task but often fails when applied to something else, like code clustering or bug detection.

The bottleneck is labeled data. Manually labeling millions of lines of code for specific tasks is expensive and slow. While NLP has solved this using self-supervised "pretext tasks" (like predicting a missing word in a sentence), code is more structured than natural language. This paper asks: Can we create a self-supervised task specifically for the hierarchical structure of code that results in a truly "general-purpose" representation?

Methods

The authors propose InferCode, which draws a clever analogy to the Doc2vec algorithm used in natural language processing. In Doc2vec, a document is represented by a vector that is trained to predict the words it contains.

In InferCode:

  1. Code as a Tree: Every code snippet is converted into an Abstract Syntax Tree (AST) using a tool called srcML.
  2. Subtrees as Words: The model identifies meaningful subtrees (like expressions, declarations, and conditions) within the AST. These subtrees act as the "vocabulary."
  3. The Pretext Task: The model is trained to take an entire AST, compress it into a single "code vector," and then use that vector to predict which subtrees are present in that specific tree.
  4. The Architecture: They use a Tree-Based Convolutional Neural Network (TBCNN) as the encoder. They improved the standard TBCNN by:
  5. Incorporating both node types (e.g., if_statement) and actual text tokens (e.g., variable names).
  6. Using an attention mechanism to aggregate node information, ensuring the model focuses on the most important parts of the code.

Because srcML supports multiple languages (Java, C, C++, C#, etc.) and maps them to a common AST vocabulary, InferCode is inherently polyglot.

Datasets

  • Pre-training: The "Java-Large" dataset, consisting of 4 million Java files collected from GitHub.
  • Evaluation:
    • OJ Dataset: 52,000 C code snippets across 104 classes for clustering and classification.
  • Sorting Algorithm (SA) Dataset: Java implementations of 10 different sorting algorithms.
  • BigCloneBench: A massive Java benchmark for code clone detection.
  • Rosetta Code: Used for cross-language code search (finding a C# version of a Java algorithm).
  • Java-Small: Used for evaluating method name prediction.

Key Insights and Ideas

  • The "Free" Label: The most powerful idea here is that the syntax of code itself provides the labels. You don't need a human to tell you what a piece of code does to train the model; you just need a parser to tell you what subtrees are there.
  • Granularity Matters: The authors found that using "medium-sized" subtrees (like statements and expressions) works best. Entire functions are too unique (sparse), while single tokens are too generic.
  • Transfer Learning for Code: The paper demonstrates that a model pre-trained on millions of lines of unlabeled Java can be "fine-tuned" on a tiny amount of labeled C code and still achieve state-of-the-art results. This suggests the model is learning fundamental "programming logic" that transcends specific languages.

Key Results

InferCode consistently outperformed specialized baselines like code2vec, code2seq, and ASTNN:

  • Unsupervised Tasks: In code clustering, it improved the Adjusted Rand Index (ARI) by 12% over the best baseline. In clone detection, it saw a 15% boost in F1 score.
  • Cross-Language Search: It outperformed traditional information retrieval methods by 13% (Mean Reciprocal Rank) when searching for similar code across different programming languages.
  • Data Efficiency: When fine-tuning for supervised tasks, InferCode reached high accuracy using only 1% to 10% of the labeled data that other models required to reach similar performance.

Limitations and Future Work

  • Encoder Choice: While TBCNN performed well, the authors acknowledge that newer architectures like Graph Neural Networks (GNNs) or Transformers might capture even more complex dependencies.
  • Subtree Selection: The current method uses a fixed set of node types to identify subtrees. A more dynamic or learned way to identify "important" subtrees could further improve the embeddings.
  • Downstream Scope: The authors suggest expanding the evaluation to more complex software engineering tasks like bug localization and vulnerability prediction.

Summary updated

BibTeX
@inproceedings{bui2021infercode,
  author = {Nghi D. Q. Bui and Yijun Yu and Lingxiao Jiang},
  title = {{InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees}},
  booktitle = {ICSE},
  year = {2021},
  url = {https://openreview.net/forum?id=WV7K7X3EeU},
}

Loading related content...

Figure