CodeLutra: Boosting LLM Code Generation via Preference-Guided Refinement

Leitian Tao, Xiang Chen, Tong Yu, Tung Mai, Ryan A. Rossi, Yixuan Li, Saayan Mitra · Submission to ICLR 2025 · Withdrawn

Large Language Models (LLMs) have significantly advanced code generation, with massive closed-source models like GPT-4 serving as the current gold standard. However, these models are resource-intensive and difficult to customize for specific tasks. While fine-tuning smaller, open-source LLMs presents a viable alternative, it often hits a performance ceiling.

A major flaw in standard Supervised Fine-Tuning (SFT) is its reliance solely on "correct" examples. In programming, code is binary—it either executes correctly or it fails. By ignoring failures, SFT misses out on the valuable lessons contained in a model's mistakes. Furthermore, standard fine-tuning struggles with the strict nature of code, where a single wrong token can render an entire script useless despite the rest of the sequence having a high probability score.

To bridge this gap, researchers have introduced CodeLutra, a novel framework that enables smaller LLMs to rival—and sometimes outperform—giants like GPT-4 by learning from both their successful and failed code attempts.

The CodeLutra Framework

CodeLutra introduces an iterative, preference-guided refinement process. Instead of relying on a larger "teacher" model to provide feedback, it uses the ultimate objective judge: the code executor.

Overview of the CodeLutra framework and its performance compared to baseline and closed-source models.

The methodology follows a continuous cycle:

  1. Iterative Generation: The model generates multiple candidate solutions for a given prompt.
  2. Execution-Based Evaluation: Each candidate is run against test cases or database schemas. Successful outputs are labeled as "chosen," while failures are "rejected."
  3. Preference Dataset Construction: The framework pairs successful and unsuccessful attempts to create a comparative dataset.
  4. Dual-Loss Optimization: CodeLutra employs a combination of Direct Preference Optimization (DPO)—which teaches the model to prefer the "chosen" code over the "rejected" code—and an SFT regularization term. This dual-loss approach is crucial; while standard DPO can inadvertently lower the probability of all outputs, the SFT term ensures the model remains strongly anchored to correct solutions.

As this cycle repeats, the model becomes progressively better at avoiding its own past pitfalls.

Key Insights and Data Efficiency

The "aha moment" of CodeLutra is that a model's incorrect attempts are just as informative as its correct ones. By contrasting the two, the model learns the specific boundaries of logical and syntactical correctness. Qualitative analysis shows that CodeLutra specifically helps models overcome "hallucinated" columns in SQL or basic syntax errors that standard SFT often fails to iron out.

Furthermore, many recent "self-improving" LLM papers rely on GPT-4 to judge the quality of a smaller model's output. CodeLutra proves that for coding, the compiler or executor is a more reliable and grounded judge than any auxiliary LLM. Remarkably, the framework is highly data-efficient, achieving significant gains with only 500 to 1,000 training samples.

Key Results

The researchers evaluated CodeLutra across several challenging benchmarks, including Spider and BIRD for text-to-SQL tasks, and DS-1000 for data science problems. The quantitative jumps are impressive, especially considering the model size.

Performance comparison of CodeLutra against various open-source, fine-tuned, and closed-source LLMs across Spider, BIRD, and DS1000 benchmarks.
  • SQL Mastery: On the Spider dataset, a Llama-3-8B model trained with CodeLutra reached an execution accuracy of 76.6%, successfully surpassing GPT-4 (74.4%).
  • Data Science Boost: On the DS-1000 benchmark, the same 8B model jumped from a baseline of 28.2% to 48.6%, nearly matching GPT-4’s 49.4%.
  • Robustness: The framework demonstrated consistent improvements across different base architectures, including Gemma-7B and StarCoder-7B, proving its broad applicability.

Limitations and Future Work

While CodeLutra excels at functional correctness, there are several avenues for future growth. Currently, the framework treats all successful code as equal, without distinguishing between "messy" and "elegant" or computationally efficient code. Future iterations could incorporate metrics for code readability and complexity.

Additionally, the system treats all failures uniformly. Learning to distinguish between a minor typo and a fundamental logical flaw could lead to even more targeted refinement. Finally, integrating static analysis tools (like linters) could provide valuable feedback even when code isn't fully executable, potentially accelerating the learning process in earlier iterations.

Summary updated

BibTeX
@misc{tao2024codelutra,
  author = {Leitian Tao and Xiang Chen and Tong Yu and Tung Mai and Ryan A. Rossi and Yixuan Li and Saayan Mitra},
  title = {{CodeLutra: Boosting LLM Code Generation via Preference-Guided Refinement}},
  year = {2024},
  eprint = {2411.05199},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2411.05199},
}
Figure