Disentangled Code Representation Learning for Multiple Programming Languages

Jingfeng Zhang, Haiwen Hong, Yin Zhang, Yao Wan, Ye Liu, Yulei Sui

Introduction

In modern software engineering, the ability for machine learning models to understand and process source code across multiple programming languages is paramount. Tasks such as cross-language code search and clone detection require models to grasp the underlying logic of a program regardless of the specific syntax used. However, traditional code embeddings often "entangle" semantics (the functional logic) with syntax (the language-specific grammar). This entanglement limits a model's ability to generalize, as the syntactic "noise" of one language can obscure the logical similarities it shares with another.

To address this, the authors propose CODEDISEN, a novel representation learning framework designed to explicitly separate the "what" of a code snippet from the "how."

The CODEDISEN Framework

CODEDISEN treats programming languages as different "styles" of expressing the same logical "content." By disentangling these components, the model can produce independent vectors for semantics and syntax, enabling more robust performance on specialized downstream tasks.

Overview of the CODEDISEN framework, illustrating the separation of code into syntax and semantic representations for matching and generation tasks.

Methodology: Multi-lingual Disentanglement

The core of CODEDISEN is a Variational Autoencoder (VAE) architecture that utilizes language-specific syntax encoders alongside a shared semantic encoder.

Architecture and the AST Masking Trick

The model employs two types of encoders:

  1. Semantic Encoder ($Enc_0$): A BiLSTM shared across all supported programming languages, designed to capture universal logic.
  2. Syntax Encoders ($Enc_i$): Individual encoders for each language (e.g., Java, Python, C#, C++).

A critical innovation is the use of masked Abstract Syntax Trees (ASTs). To prevent the syntax encoder from learning semantic information, the authors feed it AST paths where leaf nodes (such as variable names and identifiers) have been removed. This forces the encoder to focus exclusively on structural compilation nodes and grammatical rules.

The CODEDISEN architecture featuring shared semantic and language-specific syntax encoders, a shared decoder, and KL regularization to align latent spaces.

Training Objectives

The framework is optimized using three specialized loss functions:

  • Cross-Language Reconstruction Loss: Encourages the model to reconstruct code in one language using the syntax vector of that language combined with a semantic vector derived from a different language.
  • Posterior Distribution Loss: Ensures that semantically equivalent programs map to the same region in the semantic latent space.
  • Attentive Code Position Loss: Uses a "Token2AST" mechanism to align specific code tokens with their structural positions in the AST.

Experimental Evaluation

The authors evaluated CODEDISEN using the CLCDSA dataset, which contains 26,000 code snippets from competitive programming platforms. This dataset is uniquely suited for the task as it provides multiple solutions in different languages for the same problem.

Key Results

CODEDISEN demonstrated significant improvements over baselines like GraphCodeBERT and Tree-LSTM:

  • Code Clone Detection: Achieved an F1 score of 0.90 on Java/Python tasks, vastly outperforming GraphCodeBERT's 0.50.
  • Code-to-Code Search: Reached an MRR of 0.436, compared to BiLSTM's 0.275.
  • Reconstruction Quality: In code translation, the model achieved a BLEU-1 score of 44.77, showing high fidelity in generating syntactically correct code from disentangled semantic vectors.

Conclusion and Future Directions

CODEDISEN successfully demonstrates that disentangling semantics from syntax leads to superior cross-language code representations. While the model currently faces challenges with complex mathematical expressions and very long code snippets, future work aims to incorporate tree-structured decoders to further improve syntactic validity and execution rates.

Summary updated

BibTeX
@inproceedings{zhang2021disentangled,
  author = {Jingfeng Zhang and Haiwen Hong and Yin Zhang and Yao Wan and Ye Liu and Yulei Sui},
  title = {{Disentangled Code Representation Learning for Multiple Programming Languages}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2021},
  doi = {10.18653/v1/2021.findings-acl.391},
}
Figure