Abstract
Legacy codebases, particularly those written in Fortran, remain critical to High-Performance Computing (HPC) but pose significant maintenance challenges as hardware and developer expertise shift toward modern languages like C++. While Large Language Models (LLMs) offer potential for automated translation, zero-shot performance often lacks the necessary syntactic and semantic precision for complex scientific code. This paper presents a Retrieval-Augmented Generation (RAG) framework that utilizes few-shot learning to improve translation accuracy. By dynamically retrieving relevant Fortran-C++ pairs as context, the proposed method achieves substantial gains in CodeBLEU scores across various LLMs, including GPT-4o, Granite-34B, and Llama-3, without requiring expensive fine-tuning.
Introduction
In the domain of High-Performance Computing (HPC), legacy codebases—often written in Fortran—form the backbone of critical scientific simulations. As hardware architectures evolve and the developer pool shifts toward modern languages like C++, maintaining and migrating these "ancient" codebases has become a massive bottleneck.
While LLMs like GPT-4 or Llama-3 are capable of general code translation, they often struggle with the specific syntactic and semantic nuances of complex scientific code. Standard "zero-shot" translation frequently results in bugs or structural inconsistencies. Traditional fine-tuning offers a solution but is computationally expensive and often fails to generalize when the model encounters novel programming constructs. This research explores a more flexible approach: Retrieval-Augmented Generation (RAG).
Methodology
The researchers proposed a RAG-based Few-Shot Learning framework. Instead of relying solely on a model's internal knowledge, the system follows a three-step pipeline:
- Retrieval: When a user provides a Fortran snippet, the system uses an embedding model (such as Nomic-embed or CodeBERT) to search a vector database (ChromaDB) for the most similar Fortran-C++ pairs available in the training data.
- Augmentation: The system retrieves these similar examples (1, 2, or 3 "shots") and inserts them into a prompt template. This provides the LLM with a concrete reference for how similar logic was successfully translated.
- Generation: The LLM (ranging from GPT-4o to specialized models like Granite-34B) generates the translation based on the provided context.
The study evaluated several embedding models and distance metrics (Cosine vs. L2 distance) to determine which retrieval strategies provided the most effective "hints" for the models.
Datasets and Evaluation
The framework was tested using three primary datasets:
- Numerical Recipes: 298 Fortran-C++ pairs. While foundational, these often rely on specific function libraries, which can limit general applicability.
- HPC Fortran2CPP: 315 pairs sourced from high-performance benchmarks like NPB, PolyBench, and DataRaceBench, representing "real-world" complex scientific code.
- Stack-V2: A massive collection of 500,000 raw Fortran codes. Researchers used Llama-3 70B to extract 500 "clean" examples for zero-shot performance evaluation.
Performance was quantified using CodeBLEU scores, a metric that measures not just text similarity, but syntactic and semantic correctness.
Key Insights and Results
The "RAG Jump"
The most striking finding was the performance delta between zero-shot and few-shot configurations. Even a single similar example (1-shot) led to a massive spike in CodeBLEU scores. For example, IBM’s Granite-34B Code Instruct showed one of the most impressive gains on the HPC Fortran2CPP dataset, jumping from a zero-shot score of 0.237 to a 1-shot improvement of +0.363.
Model Performance
- Mixtral-8x22B achieved the highest overall 3-shot scores on the HPC dataset, reaching a delta of +0.367 over its baseline.
- Specialization Matters: Specialized code models like Granite-34B and Meta’s Llama3-70B Instruct emerged as leaders in accuracy and reliability for these specific scientific tasks.
- Reasoning Capacity: Smaller models like Phi-3 (3.8B) struggled significantly with complex translations regardless of the RAG context, suggesting a minimum "reasoning capacity" is required for effective scientific code migration.
Quality of Context
Ablation studies, where the system intentionally retrieved the least similar examples ("bad RAG"), caused performance to crater. This proves that the relevance of the retrieved context—respecting data flow and Abstract Syntax Trees (AST)—is the primary driver of success.
Conclusion and Future Work
The RAG-based few-shot learning approach provides a powerful, training-free method to modernize legacy scientific code. By providing models with relevant structural context, researchers can significantly bridge the gap between legacy Fortran and modern C++. Future work will focus on addressing data scarcity for parallel code pairs and refining the RAG framework to handle even more diverse programming styles and complex library dependencies.
Summary updated