NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Unraveling the Potential of Large Language Models in Code Translation: How Far are We?

Qingxiao Tao, Tingrui Yu, Xiaodong Gu, Beijun Shen

TL;DR

Researchers have developed PolyHumanEval, a massive 14-language benchmark, to reveal that current LLMs struggle with code translation due to a lack of parallel training data. By using Go as an intermediary "lingua franca" and implementing a self-training pipeline with automated verification, they achieved an 11.7% boost in translation accuracy.

What Problem Does This Paper Address?

While Large Language Models (LLMs) like GPT-4 and CodeLlama are wizards at generating code from natural language, they often stumble when translating code between programming languages (PLs). The core issue is data scarcity: LLMs are trained on massive amounts of code, but they rarely see "parallel" examples (the same logic written in both Java and Python, for instance).

Furthermore, existing benchmarks for code translation are often limited to a few common languages and don't always guarantee that the "equivalent" solutions actually produce the same output. This paper seeks to quantify exactly where LLMs fail and how we can bridge the gap without needing massive, human-annotated parallel datasets.

Methods

The authors propose a multi-pronged approach to improve translation:

  1. Intermediary Translation: Instead of translating directly from Language A to Language B, the authors suggest using a "bridge." They tested two types:
  2. Style Transfer: Converting complex source code (like Pythonic list comprehensions) into a simple procedural style (basic if and for loops) before translating to the target.
  3. Intermediary Language: Using Go as a universal middleman (A $\rightarrow$ Go $\rightarrow$ B).
  4. Self-Training: To solve the data hunger problem, they let the LLM teach itself. The model generates Python functions based on API documentation, creates unit tests for them, translates them into a target language, and then verifies the translation using the generated tests. The "verified" pairs are then used to fine-tune the model.
  5. Prompt Engineering: They explored how much "hinting" helps, testing the impact of providing target function signatures, required library imports, and line-by-line prompting.

Datasets

  • PolyHumanEval (New): The authors extended the famous OpenAI HumanEval benchmark. It now covers 14 programming languages (including emerging ones like Rust, Kotlin, and Swift) across 164 coding problems. Crucially, they built a rule-based tool to ensure all 2,296 solutions are semantically equivalent and pass the same test cases.
  • The Stack: Used to gauge language popularity for selection.
  • Self-Generated Data: 20,000 Python functions and corresponding test cases were generated to create a synthetic parallel corpus for fine-tuning.

Key Insights and Ideas

  • The Asymmetry Trap: LLMs exhibit a "biased" capability. They are much better at translating to Python than from Python. For example, CodeLlama-13B scores ~86% translating various languages to Python, but only ~67% when going the other way.
  • Go is the Lingua Franca: Surprisingly, Go emerged as the most effective intermediary language. Its simple, procedural syntax makes it easier for models to comprehend and generate, serving as a clean "logical skeleton" for the translation process.
  • Tuning Can Hurt: Standard optimization techniques like instruction tuning or further pre-training on Python actually decreased multilingual translation performance. The authors hypothesize this is due to "catastrophic forgetting" or the model becoming too biased toward Python's specific idioms.
  • Signatures are Key: Simply providing the model with the function signature of the target language (e.g., the specific types and parameters required in Java) significantly reduces syntax errors and improves accuracy.

Key Results

  • Performance Boost: The combined approach (intermediary translation + self-training) yielded an average 11.7% improvement in computational accuracy (CA) for Python-to-other translations using CodeLlama-13B.
  • Scale of Study: The researchers performed over 110,000 translations to ensure their findings were statistically robust.
  • Top Model: Among the tested open-source models, the CodeLlama series consistently outperformed StarCoder and CodeGen, even when the latter had more parameters.
  • Intermediary Success: Using Go as a bridge improved accuracy by 2.58 to 4.73 points across different model sizes.

Limitations and Future Work

  • Intermediary Selection: While Go worked well as a general bridge, it might not be the optimal intermediary for every specific pair (e.g., translating between two functional languages). The authors suggest future work should involve automatically selecting the best bridge language based on embedding similarity.
  • Data Diversity: The self-training data was generated from Python documentation. While effective, the authors note that the training set needs even more diversity to reflect the "messy" real-world code developers actually write.
  • Real-World Projects: The current benchmark uses standalone functions. Future research is needed to see how these methods scale to entire software projects with complex cross-file dependencies.

Summary updated

BibTeX
@inproceedings{tao2024unraveling,
  author = {Qingxiao Tao and Tingrui Yu and Xiaodong Gu and Beijun Shen},
  title = {{Unraveling the Potential of Large Language Models in Code Translation: How Far are We?}},
  booktitle = {Apsec},
  year = {2024},
  doi = {10.1109/apsec65559.2024.00046},
}
Figure