HELoC: hierarchical contrastive learning of source code representation

Xiao Wang, Qiong Wu, Hongyu Zhang, Chen Lyu, Xue Jiang, Zhuoran Zheng, Lei Lyu, Songlin Hu

Introduction

In the field of AI for Software Engineering, the ability to represent source code as dense vector embeddings is fundamental to downstream tasks such as vulnerability detection, clone identification, and automated code summarization. Most contemporary approaches leverage Abstract Syntax Trees (ASTs)—structured representations of a program's grammatical hierarchy—to capture the logic of the code.

However, standard Graph Neural Networks (GNNs) often suffer from a "local vision" limitation. By primarily focusing on immediate parent-child relationships, these models struggle to capture the broader hierarchical topology of deep, complex trees. Non-adjacent nodes that are separated by multiple levels are rarely effectively linked in the embedding space. To address this, the authors propose HELoC (Hierarchical Contrastive Learning of Code), a model designed to understand both the local syntax and the global hierarchical structure of source code.

Methodology

HELoC introduces two primary technical innovations: a hybrid neural architecture and a specialized self-supervised training objective.

1. Residual Self-attention Graph Neural Network (RSGNN)

To overcome the limitations of standard GNNs in handling long-range dependencies, the authors developed the RSGNN. This architecture combines two complementary mechanisms:

  • Graph Convolutional Networks (GCNs): These are utilized to capture local structural information and immediate syntactic context.
  • Self-attention Mechanism: This allows any node within the AST to "attend" to any other node, regardless of their distance in the tree, effectively capturing global context.
  • Residual Connections: These are integrated to mitigate the vanishing gradient problem, enabling the training of deeper networks capable of processing large-scale ASTs.

2. Hierarchical Contrastive Learning (HCL)

The core of HELoC's training is a self-supervised strategy that requires no human labeling. The model learns code structure through two "pretext tasks":

  • Node Level Prediction (NEP): The model is tasked with predicting the specific depth (level) of a node within the AST.
  • Node Relationship Optimization (NRO): Utilizing a triplet loss strategy, the model is trained to minimize the distance between nodes at the same hierarchical level while maximizing the distance between nodes at different levels. Crucially, the loss function incorporates the level difference ($\Delta l$), forcing the Euclidean distance in the embedding space to be proportional to the hierarchical distance in the tree.

Experimental Setup

The researchers evaluated HELoC across several large-scale benchmarks:

  • Pre-training: The model was pre-trained on the Java-Large dataset, which contains approximately 4 million Java files, to learn general hierarchical features.
  • Downstream Tasks: Evaluation was conducted on code classification (Google Code Jam, Online Judge), clone detection (BigCloneBench, OJClone), and unsupervised code clustering (Sorting Algorithm dataset).

Key Results and Insights

HELoC consistently outperformed previous state-of-the-art (SOTA) models, including ASTNN and InferCode, across all evaluated metrics:

  • Classification Accuracy: On the Google Code Jam (GCJ) task, HELoC achieved 97.2% accuracy, representing a 3.8% improvement over previous benchmarks.
  • Clone Detection: The model reached an F1 score of 98.0% on BigCloneBench. Notably, it showed high proficiency in identifying "Type-4" clones—code snippets that are syntactically different but functionally identical.
  • Unsupervised Clustering: In tasks where the model must group similar code without labels, HELoC demonstrated a 12.1% to 12.6% improvement in the Adjusted Rand Index (ARI).
  • Structural Awareness: t-SNE visualizations confirmed that HELoC naturally clusters nodes according to their tree level, validating that the model successfully internalizes the hierarchical topology of the source code.

Discussion and Future Work

The primary insight of this research is that hierarchical distance matters. By explicitly mapping tree depth to vector space distance, HELoC creates more structurally aware embeddings. Furthermore, the success of the HCL objective demonstrates that self-supervision is sufficient for learning sophisticated code representations without the need for expensive manual labeling.

Despite these strengths, the authors identify several avenues for future research. While the method is language-agnostic in theory, it has primarily been validated on Java and C; future work should explore dynamically typed languages like Python. Additionally, the computational cost of combining GNNs with full self-attention on large trees suggests a need for more efficient, sparse attention mechanisms to handle even larger codebases.

Summary updated

BibTeX
@inproceedings{wang2022heloc,
  author = {Xiao Wang and Qiong Wu and Hongyu Zhang and Chen Lyu and Xue Jiang and Zhuoran Zheng and Lei Lyu and Songlin Hu},
  title = {{HELoC: hierarchical contrastive learning of source code representation}},
  booktitle = {ICPC},
  year = {2022},
  doi = {10.1145/3524610.3527896},
}
Figure