NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

PerfRL: A Small Language Model Framework for Efficient Code Optimization

Shukai Duan, Nikos Kanakaris, Xiongye Xiao, Heng Ping, Chenyu Zhou, Nesreen K. Ahmed, Guixiang Ma, Mihai Capota, Theodore L. Willke, Shahin Nazarian, Paul Bogdan

TL;DR

PerfRL is a novel framework that pairs Small Language Models (SLMs) with Reinforcement Learning (RL) to automate and optimize code performance. By integrating real-time feedback from unit tests and execution runtimes, a lightweight 60-million parameter model can outperform massive 16-billion parameter models while using a fraction of the energy and training time.

What Problem Does This Paper Address?

Optimizing code to run faster and consume less energy is a notoriously difficult task, traditionally requiring deep human expertise and constant adaptation to shifting hardware architectures. While Large Language Models (LLMs) like Codex or CodeGen have shown promise in automating code generation, they suffer from two major flaws when applied to optimization:

  1. Resource Intensity: Massive LLMs require immense computational power and energy, making them impractical for low-cost, edge computing, or cyber-physical systems with strict power budgets.
  2. Lack of Functional Verification: LLMs generate code based on token probabilities. They can easily produce code that looks superficially correct but contains fatal syntax errors or logical bugs because they cannot inherently test or run the code they write.

The authors introduce PerfRL to bridge this gap, proving that smaller, resource-efficient models can achieve state-of-the-art code optimization when guided by dynamic environmental feedback.

Methods

PerfRL combines a Small Language Model (SLM) with a lightweight reinforcement learning algorithm called RRHF (Rank Responses to Align Language Models with Human Feedback).

The framework operates in three main phases:

  1. Fine-Tuning: The base SLM (specifically CodeT5 with 60M parameters) is initially fine-tuned on a specialized code optimization dataset using standard cross-entropy loss.
  2. Sample Generation: For a given slow program, the model generates multiple candidate optimized versions using a mix of greedy sampling (beam search) and random sampling. Crucially, the training process includes the ground-truth target code to ensure the RL algorithm always has at least one correct sample to learn from.
  3. RL Supervision and Correction: Each generated candidate is executed through a Python interpreter and evaluated against unit tests. A reward model scores the code based on its validity and performance:
  4. R1: Cannot compile/syntax error.
  5. R2: Runtime error, timeout, or failed unit test.
  6. R3: Passed all unit tests but showed no performance improvement.
  7. R4: Passed all unit tests and successfully improved execution runtime.

The model's parameters are then updated by maximizing the probability of generating high-reward (R4) code snippets.

Fig. 1. Overview of the PerfRL framework. (a) Training. We first fine-tune an SLM model using the whole training dataset. Then, we pass the input codes into the fine-tuned LM to generate a predefined number of optimized samples for each input program. We assign a score value to each sample and calculate its reward using the reward model. We utilize the score and reward values to calculate the Lrank and Ltuning loss values for RL. The final loss L is calculated by combining the two previous loss values and is used to retrain the model. In that way, our framework incorporates feedback from unit tests into its training process. Thus, it is more likely to generate optimized code that is free from syntactical and logical errors, mitigating hallucinations. (b) Reward model. Depending on the status of the code (e.g. can be compiled, has a run-time error or passes all the unit tests) a different reward value is given by the reward model. (c) Inference. During inference, the final SLM is utilized to generate multiple samples of candidate source codes. These generated source codes are then evaluated using the reward model. All samples that do not receive an R4 reward are filtered out. The source codes that remain after this elimination process are considered as the optimized code.

Datasets

The framework was trained and evaluated using the PIE dataset, a specialized benchmark derived from the CodeNet collection. PIE captures real-world program trajectories where developers progressively modify their code to improve performance. The authors focused on the Python subset of the dataset, utilizing approximately 36,000 samples for training, 1,000 for testing, and 2,000 for validation. Each sample includes a slow/fast code pair and an associated unit test file.

Key Insights and Ideas

  • The Power of Small: The most profound insight is that reinforcement learning democratizes AI code optimization. Instead of scaling up model size to billions of parameters, scaling up environmental interaction (via unit tests) allows a 60M parameter model to excel.
  • Overcoming the "Cold Start" in Code RL: Language models often generate broken code early in RL training, leaving the reward model with nothing but penalties to hand out. PerfRL bypasses this by injecting the target program into the training samples, ensuring the model always observes what a "perfect" optimization looks like.
  • Proportional Learning: As RL steps progress, both the compilation pass rate and the optimization rate increase cleanly in tandem, proving that the model is genuinely learning the semantics of efficient programming rather than just memorizing patterns.

Key Results

PerfRL was benchmarked against massive models from the CodeGen family (2B and 16B parameters) and OpenAI's Codex.

  • Superior Speedups: PerfRL achieved an absolute Speedup (SP) of 4.93x and a Runtime Reduction (RTR) of 79.72%. For comparison, the 16-billion parameter CodeGen model only achieved a 1.69x speedup and 40.83% RTR.
  • Massive Energy Savings: Training PerfRL consumed 27,000 kJ of energy, which is half the energy required by CodeGen-2B and over 11 times less than CodeGen-16B (311,040 kJ).
  • Reduced Hardware Demands: PerfRL was trained in roughly 30 hours on a single NVIDIA A100 GPU, whereas the baseline CodeGen models required multiple days of training across clusters of high-end NVIDIA A6000 GPUs.

Limitations and Future Work

While PerfRL excels at making minor, highly effective modifications to optimize code, it struggles when an optimization requires a drastic semantic or structural overhaul. Because LLM tokenizers process code textually, functionally identical programs that look completely different structurally are difficult for the model to reconcile.

The authors suggest that future iterations should incorporate machine learning techniques capable of parsing structural information (such as Abstract Syntax Trees or control-flow graphs) directly into the language model, allowing it to better understand complex architectural code transformations.

Summary updated

BibTeX
@misc{duan2023perfrl,
  author = {Shukai Duan and Nikos Kanakaris and Xiongye Xiao and Heng Ping and Chenyu Zhou and Nesreen K. Ahmed and Guixiang Ma and Mihai Capota and Theodore L. Willke and Shahin Nazarian and Paul Bogdan},
  title = {{PerfRL: A Small Language Model Framework for Efficient Code Optimization}},
  year = {2023},
  eprint = {2312.05657},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2312.05657},
}
Figure