Introduction
While modern Large Language Models (LLMs) are increasingly capable of generating syntactically correct code, they remain largely "performance-blind." Standard training objectives treat a slow bubble sort and a fast quicksort as equally valid, provided both are correct. For high-performance computing (HPC) and scientific applications, this lack of efficiency is a significant bottleneck. This paper introduces a framework to align LLMs with execution efficiency, enabling them to generate code that is not only correct but also optimized for speed.
Methodology
The researchers developed a multi-stage alignment pipeline to transform DeepSeek-Coder 6.7B into a performance-aware model. The process involves three key stages:
- Supervised Fine-Tuning (SFT): Initial training on a curated set of high-performance solutions to competitive programming problems.
- Reinforcement Learning with Performance Feedback (RLPF): Inspired by RLHF, this method uses a "stopwatch" as a ground-truth signal. A reward model is trained to predict which of two code snippets is faster, and a Proximal Policy Optimization (PPO) algorithm uses this feedback to nudge the LLM toward efficiency.
- Direct Performance Alignment (DPA): An adaptation of Direct Preference Optimization (DPO) that directly optimizes the LLM by comparing the log-probabilities of fast versus slow code pairs.
A core innovation in this framework is the adaptive margin ($\mu$) used in the loss functions. Rather than using a simple binary signal (fast vs. slow), the margin scales the training signal based on the actual execution time difference. This forces the model to prioritize optimizations that yield the most substantial speedups.
Datasets and Training
The training utilized a combination of two distinct datasets:
- CodeContests-Perf ($D_c$): A dataset of 4.5 million correct submissions across C++, Java, and Python, executed on standardized AMD EPYC systems to record precise runtimes.
- Synthetic Data ($D_s$): 10,000 pairs of fast/slow code generated by Gemini-Pro-1.0. Although small, this dataset was critical for teaching the model production-grade patterns and parallel programming libraries (like OpenMP) that are underrepresented in competitive programming data.
Key Results and Insights
The DS+RLPF model emerged as the top performer across multiple benchmarks:
- Serial Code: Achieved speedups between 0.9x and 1.6x over the base model.
- Parallel Code (OpenMP): Showed dramatic improvements, with speedups ranging from 1.9x to 4.5x.
- Kernel Optimization: In PolyBench tasks, the model achieved a 22.4x speedup on matrix multiplication kernels by implementing loop unrolling and cache-friendly access patterns.
Beyond raw speed, the researchers found that aligning for performance actually improved the model's overall correctness (pass@1). By learning the patterns of "expert" (fast) programmers, the model seemingly adopted better coding habits and more robust logic.
Limitations and Future Work
The current approach is somewhat hardware-specific, as runtimes were measured on a single CPU architecture. Performance characteristics can vary significantly across different hardware (e.g., Intel vs. AMD or CPUs vs. GPUs). Additionally, while the model excelled at shared-memory parallelism (OpenMP), distributed-memory parallelism (MPI) remains a challenge. Future work will focus on incorporating hardware-specific features directly into the reward function to improve cross-architecture generalization.
Summary updated