Skip to main content
QUICK REVIEW

[论文解读] Exploring and Unleashing the Power of Large Language Models in Automated Code Translation

Zhen Yang, Fang Liu|arXiv (Cornell University)|Apr 23, 2024
Natural Language Processing TechniquesComputer Science被引用 3
一句话总结

本文提出 UniTrans,一种统一框架,通过利用测试用例生成、基于执行的增强和迭代修复,提升大型语言模型(LLMs)在自动代码翻译中的性能。在 Python、Java 和 C++ 的翻译任务中,UniTrans 显著提升了多种 LLM(包括 GPT-3.5 和 LLaMA 变体)在计算准确率和精确匹配准确率方面的表现,且无需微调。

ABSTRACT

Code translation tools (transpilers) are developed for automatic source-to-source translation. Although learning-based transpilers have shown impressive enhancement against rule-based counterparts, owing to their task-specific pre-training on extensive monolingual corpora. Their current performance still remains unsatisfactory for practical deployment, and the associated training resources are also prohibitively expensive. LLMs pre-trained on huge amounts of human-written code/text have shown remarkable performance in many code intelligence tasks due to their powerful generality, even without task-specific training. Thus, LLMs can potentially circumvent the above limitations, but they have not been exhaustively explored yet. This paper investigates diverse LLMs and learning-based transpilers for automated code translation tasks, finding that: although certain LLMs have outperformed current transpilers, they still have some accuracy issues, where most of the failures are induced by a lack of comprehension of source programs, missing clear instructions on I/O types in translation, and ignoring discrepancies between source and target programs. Enlightened by the above findings, we further propose UniTrans, a Unified code Translation framework, applicable to various LLMs, for unleashing their power in this field. Specifically, UniTrans first crafts a series of test cases for target programs with the assistance of source programs. Next, it harnesses the above auto-generated test cases to augment the code translation and then evaluate their correctness via execution. Afterward, UniTrans further (iteratively) repairs incorrectly translated programs prompted by test case execution results. Extensive experiments are conducted on six settings of translation datasets between Python, Java, and C++. Three recent LLMs of diverse sizes are tested with UniTrans, and all achieve substantial improvements.

研究动机与目标

  • 探究使用大型语言模型(LLMs)作为基于学习的转换器的替代方案,实现自动代码翻译的可行性。
  • 识别并分析基于 LLM 的代码翻译失败的根本原因,特别是理解能力、输入输出规范以及结构差异处理方面的缺陷。
  • 设计一种统一的、基于提示的框架——UniTrans,以在不进行微调的情况下提升 LLM 在代码翻译中的性能。
  • 在多种 LLM 和翻译场景中评估该框架,证明其泛化能力和实际应用价值。

提出的方法

  • UniTrans 利用源代码生成目标程序的测试用例,以编码输入输出需求和程序行为。
  • 通过将测试用例信息注入提示,增强代码翻译,提升 LLM 对所需 I/O 类型和程序语义的理解。
  • 通过自动执行生成的测试用例来评估翻译后的代码,验证其正确性。
  • 通过分析测试用例执行失败情况,对错误翻译进行修复,并可选地启用迭代修复循环以实现进一步优化。
  • 该框架设计为模型无关,支持多种 LLM,包括 GPT-3.5、LLaMA-7B/13B/33B 和 CodeGen-6B。
  • 采用三阶段流程:测试用例生成、翻译增强和翻译修复,全部由提示工程和执行反馈驱动。
Figure 1. Frequencies of Failed Cases in Each Translation Dataset
Figure 1. Frequencies of Failed Cases in Each Translation Dataset

实验结果

研究问题

  • RQ1当前最先进 LLM 在自动代码翻译任务中的主要失败模式是什么?
  • RQ2LLM 是否能在无需特定任务微调的情况下,超越现有的基于学习的转换器在代码翻译中的表现?
  • RQ3如何有效利用测试用例和执行反馈来提升基于 LLM 的代码翻译性能?
  • RQ4统一的、基于提示的框架在多语言对中提升多种 LLM 的程度如何?
  • RQ5基于 LLM 构建通用、无需微调的代码翻译系统的关键设计原则是什么?

主要发现

  • GPT-3.5 等 LLM 在代码翻译准确率上优于当前基于学习的转换器,但仍存在显著的失败率。
  • LLM 失败的主要原因包括:缺乏对源程序的理解(38.51%)、缺少 I/O 类型指令(14.94%)以及忽略结构差异(41.38%)。
  • UniTrans 在所有测试的 LLM 和翻译设置中均提升了计算准确率和精确匹配准确率,尤其在 GPT-3.5 和 LLaMA 模型上表现显著。
  • 该框架在无需模型微调的情况下实现了稳定的性能提升,证明了其实际可用性和可扩展性。
  • UniTrans 中的迭代修复机制进一步提升了翻译的正确性,尤其在复杂翻译场景中效果显著。
  • 该框架在多种 LLM 家族和尺寸中均表现有效,包括开源模型(LLaMA)和闭源模型(GPT-3.5),证明了其通用性。
Figure 2. UniTrans
Figure 2. UniTrans

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。