Enhancing Cross-Language Code Translation via Task-Specific Embedding Alignment in Retrieval-Augmented Generation

Manish Bhattarai, Minh Vu, Javier E. Santos, Ismael Boureima, Daniel O'Malley · DOI

Introduction

Scientific computing continues to rely heavily on legacy Fortran codebases, yet modern high-performance computing (HPC) environments increasingly favor C++ for its performance and extensive ecosystem. Manually translating these massive codebases is both error-prone and prohibitively expensive. While Large Language Models (LLMs) have shown promise in code translation, they often struggle with the structural nuances of legacy languages where training data is sparse.

Standard Retrieval-Augmented Generation (RAG) attempts to mitigate this by providing "few-shot" examples. However, generic embedding models often suffer from an "alignment gap," retrieving snippets that are semantically similar on the surface but do not effectively guide the LLM toward a syntactically or semantically correct translation. This research introduces a task-specific embedding alignment strategy to bridge this gap.

Methodology

The core of the proposed approach is to "teach" the retriever what constitutes a helpful translation example using the CodeBLEU metric—a specialized score evaluating code based on keywords, syntax trees, and data flow.

The proposed architecture for task-specific embedding alignment and the subsequent RAG translation pipeline.

Key Components:

  1. Synthetic Data Generation: Due to the scarcity of parallel Fortran-C++ datasets, the authors utilized Llama 3.1-70B to translate 25,000 Fortran snippets from the Stack-V2 dataset into C++, creating a synthetic parallel corpus.
  2. Soft-InfoNCE Loss: Unlike standard contrastive learning that uses binary labels, the authors proposed Soft Information Noise Contrastive Estimation (S-InfoNCE). This uses continuous CodeBLEU scores as "soft" supervision signals, forcing the embedding model (StarCoder) to map Fortran snippets closer to C++ examples that yield higher-quality translations.
  3. Plug-and-Play RAG: The aligned embedding model is integrated into a RAG pipeline. When translating a Fortran query, the system retrieves the most "helpful" Fortran-C++ pairs to prime the LLM.

Experimental Results

The study evaluated the alignment strategy across two primary benchmarks: HPC Fortran2CPP (315 real-world pairs) and Numerical Recipes (298 pairs). The aligned retriever consistently improved performance across various LLMs, including Llama 3.1 (8B and 70B), Mistral, and Mixtral.

Scatter plots comparing one-shot CodeBLEU scores for unaligned vs. aligned retrieval across different models and datasets. Most points lie above the y=x line, indicating a performance gain.

Key Findings:

  • Significant Performance Gains: On the HPC Fortran2CPP benchmark, the average CodeBLEU score increased from 0.64 to 0.73 (a 14% relative improvement). For the Numerical Recipes dataset, scores rose from 0.52 to 0.60 (a 15% relative improvement).
  • Efficiency: By fine-tuning a smaller embedding model (StarCoder) rather than a massive 70B parameter LLM, the researchers achieved state-of-the-art results with significantly fewer resources.
  • Diminishing Returns of Shots: While 1-shot and 2-shot examples provided substantial gains, adding a third example yielded diminishing returns, suggesting that the quality of retrieved context is more critical than the quantity.

Conclusion

This research demonstrates that aligning embedding models with task-specific success metrics like CodeBLEU can significantly enhance the effectiveness of RAG for specialized domains like legacy code translation. Future work will explore incorporating actual execution results (unit tests) into the loss function and extending the strategy to other language pairs, such as COBOL to Java.

Summary updated

BibTeX
@inproceedings{bhattarai2024enhancing,
  author = {Manish Bhattarai and Minh Vu and Javier E. Santos and Ismael Boureima and Daniel O'Malley},
  title = {{Enhancing Cross-Language Code Translation via Task-Specific Embedding Alignment in Retrieval-Augmented Generation}},
  booktitle = {FITML @ NeurIPS},
  year = {2024},
  url = {https://openreview.net/forum?id=xLy6fxQI0b},
}
Figure