Table of Contents
Fetching ...

CoTran: An LLM-based Code Translator using Reinforcement Learning with Feedback from Compiler and Symbolic Execution

Prithwish Jana, Piyush Jha, Haoyang Ju, Gautham Kishore, Aryan Mahajan, Vijay Ganesh

TL;DR

CoTran presents an LLM-based method for whole-program translation between Java and Python, addressing limitations of function-level approaches by integrating fine-grained compiler feedback and symbolic-execution-based functional testing into a reinforcement learning loop. The system trains two back-to-back LLMs (S→T and T→S) with RL+SFT, using a novel kw-Tok tokenizer and LoRA for efficiency, and is evaluated on AVATAR-TC, a large Java–Python dataset with human-written test cases. The results show substantial gains in compilation and functional-equivalence accuracy over 14 competitive tools, with particularly strong improvements when CF and SF are combined, and demonstrate easier debugging via improved first-error positions. The work provides a scalable framework for robust code translation and suggests future extensions to more language pairs and legacy-modernization tasks.

Abstract

In this paper, we present an LLM-based code translation method and an associated tool called CoTran, that translates whole-programs from one high-level programming language to another. Existing LLM-based code translation methods lack training to ensure that the translated code reliably compiles or bears substantial functional equivalence to the input code. In our work, we fine-tune an LLM using reinforcement learning, incorporating compiler feedback, and symbolic execution (symexec)-based testing feedback to assess functional equivalence between the input and output programs. The idea is to guide an LLM during fine-tuning, via compiler and symexec-based testing feedback, by letting it know how far it is from producing perfect translations. We conduct extensive experiments comparing CoTran with 14 other code translation tools, including human-written transpilers, LLM-based translation tools, and ChatGPT. Using a benchmark of over \num{57000} code pairs in Java and Python, we demonstrate that CoTran outperforms the other tools on relevant metrics such as compilation accuracy (CompAcc) and functional equivalence accuracy (FEqAcc). For example, in Python-to-Java translation, CoTran achieves 48.68% FEqAcc and 76.98% CompAcc, whereas the nearest competing tool (PLBART-base) gets 38.26% and 75.77% respectively. Additionally, CoTran, built on top of CodeT5, improves FEqAcc by +14.89% and CompAcc by +8.14% for Python-to-Java (resp., +12.94% and +4.30% for Java-to-Python).

CoTran: An LLM-based Code Translator using Reinforcement Learning with Feedback from Compiler and Symbolic Execution

TL;DR

CoTran presents an LLM-based method for whole-program translation between Java and Python, addressing limitations of function-level approaches by integrating fine-grained compiler feedback and symbolic-execution-based functional testing into a reinforcement learning loop. The system trains two back-to-back LLMs (S→T and T→S) with RL+SFT, using a novel kw-Tok tokenizer and LoRA for efficiency, and is evaluated on AVATAR-TC, a large Java–Python dataset with human-written test cases. The results show substantial gains in compilation and functional-equivalence accuracy over 14 competitive tools, with particularly strong improvements when CF and SF are combined, and demonstrate easier debugging via improved first-error positions. The work provides a scalable framework for robust code translation and suggests future extensions to more language pairs and legacy-modernization tasks.

Abstract

In this paper, we present an LLM-based code translation method and an associated tool called CoTran, that translates whole-programs from one high-level programming language to another. Existing LLM-based code translation methods lack training to ensure that the translated code reliably compiles or bears substantial functional equivalence to the input code. In our work, we fine-tune an LLM using reinforcement learning, incorporating compiler feedback, and symbolic execution (symexec)-based testing feedback to assess functional equivalence between the input and output programs. The idea is to guide an LLM during fine-tuning, via compiler and symexec-based testing feedback, by letting it know how far it is from producing perfect translations. We conduct extensive experiments comparing CoTran with 14 other code translation tools, including human-written transpilers, LLM-based translation tools, and ChatGPT. Using a benchmark of over \num{57000} code pairs in Java and Python, we demonstrate that CoTran outperforms the other tools on relevant metrics such as compilation accuracy (CompAcc) and functional equivalence accuracy (FEqAcc). For example, in Python-to-Java translation, CoTran achieves 48.68% FEqAcc and 76.98% CompAcc, whereas the nearest competing tool (PLBART-base) gets 38.26% and 75.77% respectively. Additionally, CoTran, built on top of CodeT5, improves FEqAcc by +14.89% and CompAcc by +8.14% for Python-to-Java (resp., +12.94% and +4.30% for Java-to-Python).
Paper Structure (17 sections, 6 equations, 4 figures, 3 tables, 1 algorithm)

This paper contains 17 sections, 6 equations, 4 figures, 3 tables, 1 algorithm.

Figures (4)

  • Figure 1: Fine-tuning LLMs with Compiler Feedback (CF) and Symbolic Execution Feedback (SF): (a) CoTran (baseline) is an LLM fine-tuned without feedback loops. It is fine-tuned to optimize cross-entropy loss, employing the proposed keyword-based tokenizer (kw-Tok). (b) CoTran + CF is an LLM fine-tuned using an RL-based PPOTrainer where the compiler provides feedback in the form of a reward. For P2J, javac compiler is used. For J2P, we use the pylintpylint static code analyzer as Python is an interpreted language. (c) In CoTran + CF + SF, we jointly fine-tune back-to-back (b2b) LLMs (the forward LLM for J2P and the backward LLM for P2J) using RL. Through solver-based analysis of the input Java code, we generate a set of JUnit tests, which are then verified on the output Java code to compute SF. The reward for each LLM is a combination of SF and the respective CF. (d) We begin with the forward (J2P) and backward (P2J) LLMs, fine-tuned by CoTran (baseline) and with frozen parameters. We then continue by fine-tuning a parameter-efficient (PE) version of these LLMs by interleaving RL and SFT, training only a few additional parameters through Low-Rank Adaptation (LoRA) hu2021lora (refer Algorithm \ref{['algo:b2bTrain']}).
  • Figure 2: Reward Analysis: Plot of different rewards for RL
  • Figure 3: Count of compilation errors per code (EpC): #Errors per translated code in P2J translation, sorted in the ascending order
  • Figure 4: Sub-dataset wise performance w.r.t. $\frac{\text{f}}{\text{c}}$rate: Comparison of P2J translation, across the 7 sub-datasets of AVATAR-TC (Test)