Unsupervised Translation of Programming Languages

Baptiste Rozière, Marie-Anne Lachaux, Lowik Chanussot, Guillaume Lample

TL;DR

Researchers at Facebook AI Research (FAIR) have developed TransCoder, the first fully unsupervised neural transcompiler that translates functions between C++, Java, and Python. By leveraging massive amounts of monolingual source code and a new evaluation metric based on unit tests, TransCoder significantly outperforms traditional rule-based commercial tools without ever seeing a single parallel example during training.

What Problem Does This Paper Address?

Migrating a codebase from an obsolete language (like COBOL or Python 2) to a modern one is a nightmare. It usually requires experts in both languages and costs millions of dollars—the authors cite a case where the Commonwealth Bank of Australia spent $750 million over five years to move from COBOL to Java.

Existing "transcompilers" (source-to-source translators) are typically rule-based. They rely on handcrafted rewrite rules applied to an Abstract Syntax Tree (AST). These are brittle, hard to maintain, and often produce "ugly" code that doesn't follow the target language's conventions. While Neural Machine Translation (NMT) has revolutionized human language translation, it usually requires parallel data (the same text in two languages). In programming, parallel datasets are extremely scarce. TransCoder solves this by learning to translate using only "monolingual" data—independent repositories of C++, Java, and Python code found on GitHub.

Methods

TransCoder uses a sequence-to-sequence Transformer architecture with a single shared encoder and decoder for all languages. The "magic" lies in three unsupervised training principles:

TransCoder Training Pipeline
  1. Cross-lingual Language Model (XLM) Pretraining: The model is trained on a "Masked Language Modeling" objective. It looks at code with certain tokens hidden and tries to predict them. Because programming languages share "anchor points" (keywords like if, while, for, and mathematical operators), the model naturally maps similar concepts from different languages into the same latent space.
  2. Denoising Auto-encoding (DAE): The model is fed "noisy" or corrupted versions of code (with tokens deleted or shuffled) and trained to reconstruct the original, clean version. This teaches the decoder the specific syntax and "grammar" of the target language.
  3. Back-translation: This is the core of the unsupervised approach. The model translates a piece of Python code into C++ (initially poorly). Then, it tries to translate that C++ back into the original Python. The error between the "reconstructed" Python and the "original" Python is used to update the model. This creates a self-improving loop that generates its own synthetic parallel data.

Datasets

  • Training: The authors used the GitHub public dataset from Google BigQuery, filtering for projects with permissive licenses. They extracted functions from over 2.8 million repositories in C++, Java, and Python.
  • Evaluation (The GeeksforGeeks Benchmark): Since standard metrics like BLEU are poor for code (a single character change can break a program), the authors created a new high-quality test set. They scraped 852 parallel functions from GeeksforGeeks and, crucially, included unit tests to verify if the translated code actually runs and produces the correct output.

Key Insights and Ideas

  • The "Anchor Point" Hypothesis: Programming languages are easier to align than human languages because they share a massive amount of identical tokens (keywords, digits, and standard English variable names). This allows the model to find a "Rosetta Stone" within the syntax itself.
  • BLEU is Not Enough: The paper argues that the standard NLP metric, BLEU (which measures word overlap), is misleading for code. A translation can have a high BLEU score but fail to compile. They propose Computational Accuracy—whether the code passes unit tests—as the only gold standard for transcompilation.
  • Comments Matter: Interestingly, keeping comments in the source code during training improved performance. Comments act as additional anchor points that help the model understand the intent of the code across language barriers.

Key Results

TransCoder outperformed commercial and rule-based baselines by a wide margin:

Computational Accuracy Results
  • Java to Python: TransCoder achieved up to 68.7% computational accuracy using beam search, significantly outperforming the rule-based tool j2py (38.3%).
  • C++ to Java: TransCoder reached 74.8% accuracy with beam search, beating the commercial tool from Tangible Software Solutions (61.0%).
  • Library Alignment: The model successfully learned to map standard library functions. For example, it correctly translated Python's deque() to C++'s std::deque and swapped out the appropriate methods (like popleft() to pop_front()) without being explicitly told to do so.

Limitations and Future Work

  • Variable Types: The model sometimes struggles with type inference when moving from a dynamically-typed language (Python) to a statically-typed one (Java/C++). If the variable type isn't obvious from the context, the model may guess incorrectly.
  • Complex Data Structures: While it handles standard libraries well, it can fail on very niche or custom data structures that weren't well-represented in the training data.
  • Compilation Constraints: The authors suggest that future versions could integrate a compiler directly into the training loop (Iterative Error Correction), allowing the model to "learn from its mistakes" when the code fails to compile.

Summary updated

BibTeX
@inproceedings{roziere2020unsupervised,
  author = {Baptiste Rozière and Marie-Anne Lachaux and Lowik Chanussot and Guillaume Lample},
  title = {{Unsupervised Translation of Programming Languages}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2020},
  url = {https://openreview.net/forum?id=3ehKHqDKVrX},
}
Figure