用源码直接评估语义,提升代码翻译的正确性和一致性
Improving Code Translation with Syntax-Guided and Semantic-aware Preference Optimization

- 通过对比学习构建跨语言语义模型,直接判断源码与译码功能等价
- 融合编译器语法反馈与自研语义信号,在多目标优化中显著提升性能
- 适用于需要高保真代码转换的场景,如跨语言迁移与自动化重构
大语言模型在代码翻译中展现巨大潜力,但常难以兼顾语法正确性与语义一致性。现有基于偏好的学习虽具前景,却受限于稀疏测试用例或严格参考译文带来的不可靠语义奖励。本文主张,可靠的语义奖励应直接来自源码本身。为此提出CTO方法,通过对比学习训练跨语言语义模型,直接评估源码与翻译代码的功能等价性。将代码翻译建模为多目标优化问题,将此鲁棒语义信号与基于编译器的语法反馈无缝整合至直接偏好优化框架中。在C++、Java和Python代码翻译上的大量实验表明,CTO显著优于现有基线及其它偏好优化策略。
原文摘要 · Abstract (English)
LLMs have shown immense potential for code translation, yet they often struggle to ensure both syntactic correctness and semantic consistency. While preference-based learning offers a promising alignment strategy, it is hindered by unreliable semantic rewards derived from sparse test cases or restrictive reference translations. We argue that a robust semantic reward for code translation must be derived directly from the source code. In this paper, we propose CTO to improve code translation with syntax-guided and semantic-aware preference optimization. Through contrastive learning, we train a cross-lingual semantic model to directly assess functional equivalence between source and translated code. By formulating code translation as a multi-objective optimization problem, this robust semantic signal is seamlessly unified with compiler-based syntactic feedback within the direct preference optimization framework. Extensive experiments on C++, Java, and Python translations demonstrate that CTO significantly outperforms existing baselines and alternative preference optimization strategies.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。