- The paper introduces CTO, a framework that decomposes rewards into syntax and semantic components to improve translation accuracy.
- It employs compiler feedback for deterministic syntactic validation and contrastive learning for robust semantic equivalence.
- Experiments demonstrate significant performance boosts over unsupervised and RL-based methods in code translation benchmarks.
Syntax-Guided and Semantic-aware Preference Optimization for Code Translation
Motivation and Problem Statement
Code translation between programming languages is a pivotal task in software engineering, enabling modernization, legacy migration, and cross-language code reuse. The advent of LLMs has dramatically advanced automatic code translation, yet these models frequently fail to guarantee both syntactic correctness and semantic equivalence. Existing reward-based alignment strategies, including reinforcement learning from human feedback (RLHF), are impeded by sparse and unreliable semantic signals, typically derived from unit tests or reference translations. These proxies narrowly capture functional correctness and are susceptible to reward hacking, resulting in the entanglement of syntax and semantics and preventing robust optimization for code translation.
CTO: Multi-objective Preference Optimization
The paper introduces CTO, a mechanism for improving code translation by decomposing the reward structure into two distinct axes: syntax and semantics. CTO utilizes compiler feedback as an infallible syntactic signal, providing deterministic binary reward. For semantics, the authors develop a cross-lingual semantic model trained via contrastive learning, enabling the direct assessment of functional equivalence between source and target code snippets.
This methodology reformulates code translation as a multi-objective optimization problem. By linearly scalarizing syntax and semantic objectives, the reward model is defined as r∗(x,y)=wrg​(x,y)+(1−w)rs​(x,y), where w balances syntactic and semantic correctness. Preference optimization is conducted within the Direct Preference Optimization (DPO) framework, efficiently integrating both objectives without explicit RL, thereby overcoming the instabilities and resource demands of RL-based approaches.
Semantic Reward Modeling
CTO’s semantic reward model leverages an encoder architecture to embed both source and candidate target code into a shared latent space. Semantic similarity, measured by cosine similarity of embeddings, provides nuanced reward scores beyond binary pass/fail signals. Negative samples are generated by an LLM that perturbs reference code, introducing subtle semantic flaws while retaining syntactic validity, thus enriching the training distribution for robust semantic discrimination.
The semantic reward is further normalized and calibrated via z-score standardization, allowing list-wise discrimination among candidate translations. Embedding visualizations demonstrate language-agnostic clustering, confirming that the model captures functional equivalence across languages rather than surface-level similarities.
Preference Dataset Construction
Preference pairs are primarily generated based on compilation feedback, exploiting the deterministic nature of syntactic correctness. Fine-grained semantic reward scores supplement this preference data, enabling simultaneous optimization across both axes. This approach maximizes model robustness, ensuring that syntactic or semantic deviations are systematically penalized without conflating the two.
Experimental Results
CTO is evaluated on TransCoder-Test and HumanEval-X benchmarks, spanning C++, Java, and Python translation tasks. The model demonstrates superior performance across all settings, outperforming unsupervised baselines (TransCoder, TransCoder-ST), supervised finetuned models (CodeT5-SFT, CodeLlama-7B-SFT, Qwen2.5-Coder-7B-SFT), and RL-based approaches (PPOCoder). For CodeT5, CTO yields up to 3.66% accuracy improvement on TransCoder-Test and 4.27% on HumanEval-X. With CodeLlama-7B, CTO delivers 5.60% and 6.70% gains, respectively.
Ablation studies verify the critical contributions of both syntactic and semantic preferences; removal of either preference results in substantial performance degradation. CTO also outperforms alternative reward-free preference optimization techniques, such as identity preference optimization (IPO) and simple preference optimization (SimPO), underscoring the importance of explicit multi-objective reward integration.
Comparisons with reference-based semantic metrics (CodeBLEU, CodeBERTScore) reveal that CTO’s semantic model consistently achieves higher translation accuracy, confirming that reward modeling anchored in the source code yields superior alignment than reference-based evaluations.
Implications and Future Directions
The CTO framework constitutes a formal advancement for code translation by reconciling syntax and semantic objectives within a preference optimization paradigm. The cross-lingual semantic reward mechanism greatly mitigates reward hacking and avoids semantic bottlenecks inherent in test-based or reference-based proxies. CTO’s generalizability across architectures (encoder-decoder, decoder-only) and languages is demonstrated empirically.
Practically, CTO enhances reliability in automated code migration, promoting robust, semantically faithful translations even in high-stakes scenarios such as legacy modernization or safety-critical systems. Theoretically, CTO validates preference optimization as a multi-objective strategy for aligning LLMs with domain-specific requirements.
Future research may extend CTO to broader programming languages, integrate repository-level code migration, explore hierarchical semantic reward structures, and investigate scalability to even larger code models. Additionally, the methodology could inform alignment protocols in other structured generation tasks beyond code translation, such as data-to-text or SQL generation.
Conclusion
CTO unifies syntax-guided and semantic-aware preference optimization for code translation, employing compiler feedback and cross-lingual semantic reward modeling to deliver robust, accurate translations. Systematic experiments confirm its superiority over both classical and RL-based baselines. CTO establishes a versatile framework for aligning model outputs with domain requirements—paving the way for more reliable automated code migration and setting the agenda for future developments in semantic alignment within code intelligence tasks.