Abstract
Migrating large-scale codebases across programming languages remains a significant challenge in software engineering due to the scarcity of parallel corpora. Traditional unsupervised methods often rely on noisy back-translation or overly verbose low-level intermediate representations (IR). This paper discusses Code Distillation (CoDist), a novel approach that utilizes a simplified, language-agnostic intermediate representation as a universal pivot. By treating translation as a multilingual decompiler task, CoDist achieves state-of-the-art performance across multiple benchmarks, demonstrating that intentional "lossy" distillation can effectively preserve semantic logic while removing language-specific syntax.
Introduction
The migration of legacy systems—such as transitioning banking infrastructure from COBOL to Java or mobile applications from Java to Swift—is a high-stakes, resource-intensive endeavor. In the realm of machine learning, program translation typically requires parallel datasets (the same logic written in two languages), which are rarely available at scale.
Existing unsupervised approaches attempt to circumvent this by using back-translation or compiler-based IRs like LLVM. However, back-translation often introduces cumulative errors, and LLVM IR frequently includes language-specific "noise" (e.g., memory management or garbage collection logic) that obscures the core program logic. CoDist addresses these issues by introducing a "Goldilocks" representation: an intermediate form that is simple enough to be language-agnostic yet rich enough to maintain functional integrity.
The Code Distillation Framework
The core of the CoDist method is the Distilled Code Compiler, which transforms source code into a unified sequence of tokens. This pipeline consists of five critical stages:
- Parsing: Source code is converted into an Abstract Syntax Tree (AST) using the TreeSitter library.
- Syntax Tree Pruning: The system removes non-essential nodes, such as language-specific punctuation and redundant syntax, while retaining core logic components like function calls and reserved keywords.
- Morpheme Unification: Disparate keywords that perform identical functions across languages (e.g.,
coutin C++ andSystem.out.printin Java) are mapped to a single unified form (e.g.,print). - Fuzzing Variations: To account for varying naming conventions (snake_case vs. camelCase), object names are treated as a "bag-of-words," ensuring the model remains resilient to stylistic differences.
- Reassembling: The pruned and unified tree is flattened into a token sequence optimized for Transformer-based processing.
Training Strategy: The Multilingual Decompiler
CoDist treats the translation task as a multilingual decompiler task. Rather than learning $N^2$ direct translation paths for $N$ languages, the model learns to "decompile" the universal distilled code into any target language. The model is pre-trained on three primary objectives:
- Masked Language Modeling (MLM): Predicting omitted segments of distilled code to learn structural patterns.
- Denoising Auto-Encoding (DAE): Reconstructing original source code from corrupted versions to improve robustness.
- Multilingual Program Generation (MPG): The primary task of generating specific target language code from the distilled pivot.
Experimental Results
CoDist was evaluated against standard benchmarks including CodeXGLUE, TransCoder GeeksforGeeks, and XLCoST, using massive monolingual datasets from GitHub and CodeNet for pre-training.
Performance Benchmarks
- GeeksforGeeks: CoDist achieved a 12.7% absolute increase in translation accuracy over TransCoder-ST.
- CodeXGLUE: The model established new SOTA marks for Java $\leftrightarrow$ C# translation, reaching BLEU scores of 85.80 (Java $\to$ C#) and 83.19 (C# $\to$ Java).
- Comparison with Large Language Models: Despite having a significantly smaller parameter footprint, CoDist outperformed GPT-3.5 (Turbo) on specific pairs, such as Java $\to$ C++ and Python $\to$ C++.
Key Insights
Semantic Pivoting via Lossy Compression
A primary finding of this research is that "lossy" compression is a feature. By intentionally discarding language-specific syntax during distillation, the model is forced to focus on the underlying logic. This allows different languages to align more naturally within the same latent space.
The Importance of Identifiers
Ablation studies revealed that while the model is robust to structural noise (like shuffling or keyword deletion), its performance drops significantly if identifier names (variable and function names) are removed. This suggests that for AI models, identifier names carry the essential semantic "soul" of the program.
Conclusion and Future Work
Code Distillation provides a scalable, efficient alternative to traditional program translation methods. By using a universal pivot, it reduces the complexity of supporting multiple languages and improves translation accuracy. However, challenges remain regarding "hallucinations" in logic and the handling of complex external library dependencies. Future iterations may incorporate Retrieval-Enhanced mechanisms, allowing the model to query external documentation to resolve ambiguous method invocations and library-specific logic.
Summary updated