LLM-Assisted Code Cleaning For Training Accurate Code Generators

Naman Jain, Tianjun Zhang, Wei-Lin Chiang, Joseph E. Gonzalez, Koushik Sen, Ion Stoica

TL;DR

Researchers from UC Berkeley have demonstrated that how code is written in a training set is just as important as what the code does. By using an LLM-assisted pipeline to refactor messy, monolithic code into clean, modular, and well-named programs, they boosted the performance of a CodeLlama-7B model by up to 30%. This approach allowed the 7B model to outperform much larger models like AlphaCode while using significantly less training data.

What Problem Does This Paper Address?

Most progress in Large Language Models (LLMs) for code generation has come from scaling: more parameters and more data. However, the "data" used is often scraped from the web, where code is frequently messy, uses cryptic variable names (like a, b, c), and crams complex logic into single, giant functions.

While we know that human programmers benefit from clean code, the research community hasn't fully explored how code style and structure affect model training. The authors argue that "functional correctness" (whether the code works) isn't enough; if the training data is disorganized, the model struggles to learn the underlying algorithmic reasoning. This paper investigates whether "cleaning" existing datasets—making them more readable and structured—can lead to more accurate and data-efficient code generators.

Methods

The authors developed an automated code-cleaning pipeline that transforms existing datasets into "parallel" versions that follow better programming practices. The pipeline consists of three main steps, powered by an LLM (like GPT-3.5-Turbo):

The LLM-assisted code cleaning pipeline showing variable renaming, modularization, and plan annotation.
  1. Variable Renaming: Cryptic variables are replaced with descriptive, contextually relevant names (e.g., changing d to graph or a to root_u).
  2. Modularization: Complex, monolithic blocks of code are decomposed into smaller, reusable helper sub-functions with clear names (e.g., find_root, build_graph).
  3. Plan Annotation: The model generates a high-level natural language "plan" or summary for each function, which is then prepended to the code as a comment.

Crucially, the authors use an Oracle Equivalence Checker. Because LLMs can sometimes introduce bugs during refactoring, every transformed program is run against the original dataset's unit tests. If the "cleaned" code fails any test, it is discarded or retried, ensuring the training data remains functionally perfect.

Datasets

The study focused on two challenging algorithmic benchmarks:

  • APPS: A collection of 10,000 problems ranging from "Introductory" to "Competition" levels.
  • Code-Contests: A dataset derived from competitive programming platforms like Codeforces.

The researchers didn't just use these as-is; they created parallel versions ($D_{rename}$, $D_{modular}$, $D_{planning}$) to isolate which specific cleaning step provided the most benefit.

Key Insights and Ideas

  • Modularization is the MVP: Of all the transformations, breaking code into sub-functions (modularization) provided the biggest performance boost. It likely helps the model learn to decompose complex problems into manageable sub-tasks.
  • The "Textbook" Efficiency: Quality acts as a massive data multiplier. The authors found that a model trained on just 15% of the cleaned dataset could outperform a model trained on 100% of the original, messy dataset.
Data efficiency comparison: The model trained on modularized data achieves higher Pass@1 with significantly fewer samples.
  • Refactoring vs. Distillation: Most "synthetic data" approaches involve asking an LLM to write new code from scratch. This paper shows that editing existing correct code is more effective because it leverages the "disparity in difficulty" between solving a problem and simply cleaning up a known solution.
  • Planning is still a Bottleneck: While adding natural language plans helped, the improvement was modest. The authors noted that the model still struggles to generate its own high-quality plans for very complex problems, even if it can follow a "gold-standard" plan provided to it.

Key Results

  • Performance Leap: Fine-tuning CodeLlama-7B on modularized data improved Pass@1 accuracy by up to 30% on the Code-Contests benchmark compared to the original data.
  • Beating the Giants: The cleaned CodeLlama-7B (7 billion parameters) outperformed the much larger AlphaCode models (up to 41 billion parameters) on several metrics.
  • In-Context Learning: Even without fine-tuning, simply providing "clean" examples in the prompt (few-shot learning) improved the model's ability to solve new problems by ~23-29%.

Limitations and Future Work

  • Proprietary Dependency: The cleaning pipeline currently relies on powerful closed-source models (like GPT-3.5 or GPT-4) to perform the refactoring. Future work could explore using open-source models for the cleaning phase itself.
  • Plan Imprecision: Automatically generated plans can sometimes be vague or slightly off-target. The authors suggest that better filtering or "process supervision" (checking the steps of the plan) could be a fruitful next step.
  • Generalization: While highly effective for algorithmic tasks, it remains to be seen how well this specific cleaning pipeline translates to other domains like web development or systems programming.

Summary updated

BibTeX
@inproceedings{jain2024llm,
  author = {Naman Jain and Tianjun Zhang and Wei-Lin Chiang and Joseph E. Gonzalez and Koushik Sen and Ion Stoica},
  title = {{LLM-Assisted Code Cleaning For Training Accurate Code Generators}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=maRYffiUpI},
}
Figure