TL;DR
TransCoder is a fully unsupervised neural transcompiler that translates functions between C++, Java, and Python using only monolingual source code. By leveraging unsupervised machine translation techniques—cross-lingual pretraining, denoising, and back-translation—it outperforms commercial rule-based systems and successfully maps complex library functions across languages without requiring expert-crafted rewrite rules.
What Problem Does This Paper Address?
Transcompilation, or source-to-source translation, is the process of converting code from one high-level language (like C++) to another (like Python). It is essential for modernizing legacy codebases (e.g., moving from COBOL to Java) or ensuring interoperability between different platforms.
Traditionally, this relies on handcrafted rewrite rules applied to Abstract Syntax Trees (ASTs). This approach is brittle, expensive, and requires deep expertise in both the source and target languages. While Neural Machine Translation (NMT) has revolutionized natural language processing, it typically requires massive "parallel" datasets—the same text written in two different languages. In the world of programming, such parallel datasets are scarce, making supervised neural transcompilation difficult to scale.
Methods
The authors propose TransCoder, a sequence-to-sequence (seq2seq) Transformer model trained in a purely unsupervised manner. The approach relies on three pillars of unsupervised machine translation:
- Cross-lingual Language Model (XLM) Pretraining: The model is first trained on a Masked Language Modeling (MLM) objective across all three languages. Because programming languages share many "anchor points" (keywords like
ifandfor, mathematical operators, and English strings), this pretraining maps similar logical concepts into a shared latent space regardless of the specific syntax. - Denoising Auto-Encoding (DAE): The decoder is trained to reconstruct a clean version of a function from a corrupted version (where tokens are randomly masked or shuffled). This teaches the model to always generate syntactically valid code.
- Back-translation: This is the core of the unsupervised learning process. A model translates a function from Language A to Language B, and then another model attempts to translate it back to Language A. The reconstruction error is used to update the model, allowing it to generate its own parallel training data iteratively.
Datasets
- Training: The authors used the GitHub public dataset from Google BigQuery, extracting over 2.8 million open-source repositories. They focused on C++, Java, and Python, specifically training on standalone functions.
- Evaluation: To move beyond unreliable metrics like BLEU (which only measures token overlap), the authors created a new validation and test set from GeeksforGeeks. This dataset contains 852 parallel functions across the three languages, accompanied by unit tests to verify the functional correctness of the generated translations.
Key Insights and Ideas
- The Power of Anchor Points: Unlike natural languages like English and Chinese, which share few tokens, programming languages are highly overlapping. Keywords, digits, and standard library names act as powerful anchors that naturally align the model's internal representations across languages.
- Computational Accuracy vs. BLEU: The paper highlights that BLEU scores are a poor proxy for code quality. A single character error (like a
/instead of a//in Python) can result in a high BLEU score but a completely broken program. By using unit tests, the authors introduce "computational accuracy" as the primary metric for success. - Beam Search Benefits: Using beam search (generating multiple hypotheses and selecting the best) significantly boosts performance. In many cases, the model finds a correct translation within the top candidates, even if it isn't the most probable one initially.
Key Results
TransCoder significantly outperforms commercial and rule-based baselines in terms of computational accuracy.
- Java to Python: TransCoder achieved 68.7% accuracy (with Beam 25), nearly doubling the 38.3% accuracy of the rule-based
j2pybaseline. - C++ to Java: TransCoder reached 74.8% accuracy, outperforming a commercial solution (Tangible Software Solutions) which scored 61.0%.
- Library Mapping: The model successfully learned to map complex data structures, such as converting Python's
deque()to C++'sstd::dequeand correctly using the corresponding methods likepop_back()andpush_back().
Limitations and Future Work
While impressive, TransCoder has clear limitations:
- Type Inference: Translating from a dynamically-typed language (Python) to a statically-typed one (Java/C++) remains a challenge, as the model must infer variable types that aren't explicitly present in the source.
- Small Syntax Errors: The model occasionally makes minor errors that prevent compilation, such as using the wrong operator for a specific type (e.g., using
!on an integer in Java). - Future Directions: The authors suggest that performance could be further improved by integrating compiler feedback directly into the training loop (iterative error correction) or by adding hard syntactic constraints to the decoder to ensure the output always compiles.
Summary updated