用代码翻译质量优化检索嵌入,提升跨语言代码转换效果
Enhancing Cross-Language Code Translation via Task-Specific Embedding Alignment in Retrieval-Augmented Generation
- 用CodeBLEU分数指导嵌入对齐,让检索更贴合翻译任务
- 在两个数据集上分别提升14%和15%的CodeBLEU得分
- 无需微调大模型,适合工程部署与快速应用
我们提出一种新方法,通过在检索增强生成(RAG)框架中引入任务特定的嵌入对齐,提升从Fortran到C++的跨语言代码翻译质量。不同于传统使用通用嵌入的检索方法,本方法直接以CodeBLEU指标为目标进行嵌入对齐,确保嵌入在语义和语法上对翻译任务有意义。研究构建了包含2.5万条Fortran代码片段的数据集(来自Stack-V2),并用LLaMA 3.1-8B模型生成对应C++翻译。通过计算生成结果与真实答案之间的成对CodeBLEU分数,获取细粒度相似性信号,在对比学习框架中优化嵌入模型,使其检索出最有助于提升生成性能的代码对。将该优化后的嵌入集成到RAG中,显著提升了检索准确率和生成质量。在HPC Fortran2C++数据集上,平均CodeBLEU从0.64升至0.73,相对提升14%;在Numerical Recipes数据集上,从0.52升至0.60,相对提升15%。关键在于无需微调语言模型,凸显方法高效实用。
原文摘要 · Abstract (English)
We introduce a novel method to enhance cross-language code translation from Fortran to C++ by integrating task-specific embedding alignment into a Retrieval-Augmented Generation (RAG) framework. Unlike conventional retrieval approaches that utilize generic embeddings agnostic to the downstream task, our strategy aligns the retrieval model directly with the objective of maximizing translation quality, as quantified by the CodeBLEU metric. This alignment ensures that the embeddings are semantically and syntactically meaningful for the specific code translation task. Our methodology involves constructing a dataset of 25,000 Fortran code snippets sourced from Stack-V2 dataset and generating their corresponding C++ translations using the LLaMA 3.1-8B language model. We compute pairwise CodeBLEU scores between the generated translations and ground truth examples to capture fine-grained similarities. These scores serve as supervision signals in a contrastive learning framework, where we optimize the embedding model to retrieve Fortran-C++ pairs that are most beneficial for improving the language model's translation performance. By integrating these CodeBLEU-optimized embeddings into the RAG framework, our approach significantly enhances both retrieval accuracy and code generation quality over methods employing generic embeddings. On the HPC Fortran2C++ dataset, our method elevates the average CodeBLEU score from 0.64 to 0.73, achieving a 14% relative improvement. On the Numerical Recipes dataset, we observe an increase from 0.52 to 0.60, marking a 15% relative improvement. Importantly, these gains are realized without any fine-tuning of the language model, underscoring the efficiency and practicality of our approach.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。