Abstract
Code translation is a critical task for software modernization and cross-platform compatibility. However, training effective models is hindered by the scarcity of fine-grained, aligned code snippets. This paper presents an automated pipeline that leverages Large Language Models (LLMs) to decompose full programs into logically aligned snippets. By implementing a "2-Stage-PS" training strategy—transitioning from coarse program-level alignment to fine-grained snippet-level alignment—the researchers achieved significant performance gains. Evaluation on state-of-the-art models like DeepSeek-Coder and Qwen2.5-Coder demonstrates improvements of up to 3.78% in translation accuracy across Python, Java, and C++.
Introduction
Automated code translation—the process of converting source code between programming languages—is essential for migrating legacy systems. Effective training of translation models typically relies on two data granularities: Program-Alignment (PA) and Snippet-Alignment (SA). While PA data (full functional programs) provides broad context, its length often obscures specific syntactic mappings. Conversely, SA data (small, corresponding code blocks) is ideal for learning precise syntax but is prohibitively expensive to collect manually. This research addresses this gap by introducing an automated method for generating high-quality snippet data.
Methodology: The Automated Pipeline
The authors propose a three-stage pipeline using LLMs (specifically DeepSeek-V3) to transform existing PA data into aligned snippets:
- Comment Insertion: An LLM inserts natural language comments into the source program to serve as logical separators.
- Comment-Based Program Rewriting: The LLM rewrites the target program to mirror the source's logical structure, ensuring comments appear in the same sequence.
- Split and Match: Programs are automatically partitioned at comment boundaries, resulting in perfectly aligned snippet pairs.
To maximize the utility of this data, a 2-Stage-PS training strategy is employed. The model is first fine-tuned on full programs to learn high-level semantic alignment, followed by a second stage of fine-tuning on the generated snippets to sharpen its grasp of fine-grained syntactic patterns.
Experimental Setup
The study utilizes the following datasets for training and evaluation:
- XLCoST-Program: A foundational dataset of parallel programs in 7 languages.
- XLCoST-Augmented: The newly generated dataset containing 135,654 pairs of aligned snippets.
- TransCoder-test: A standard benchmark consisting of 1,788 program pairs in Python, Java, and C++.
- XLCoST-Snippet: An existing, manually-constructed snippet dataset used for comparative analysis.
Results and Discussion
The 2-Stage-PS approach was tested on several open-source models, including DeepSeek-Coder-Instruct and Qwen2.5-Coder-Instruct.
Key Findings
- Consistent Improvements: The 2-Stage-PS method consistently outperformed standard fine-tuning across all model sizes (1.3B to 7B parameters). A maximum gain of 3.78% on the pass@1 metric was observed for the DeepSeek-Coder-6.7B model.
- Curriculum Learning: The transition from coarse to fine data acts as a form of curriculum learning, allowing models to understand the "big picture" before perfecting syntactic details.
- LLM-Driven Segmentation: Using LLMs for segmentation proved superior to traditional rule-based methods (like AST parsing) because LLMs maintain logical coherence, ensuring snippets are semantically equivalent.
- Static vs. Dynamic Languages: The method was particularly effective for translations targeting static languages (Java/C++), which benefit more from the explicit syntactic training provided by snippets.
Conclusion
This work demonstrates that automated snippet-alignment is a powerful tool for enhancing code translation models. While the approach showed marginal gains for dynamic languages like Python due to flexible syntax, it significantly improved performance for static languages. Future research will explore multi-stage training strategies and methods to better accommodate the unique challenges of dynamic programming languages.
Summary updated