Overview
LeDex is a training framework designed to teach open-source Large Language Models (LLMs) how to explain their own coding mistakes and fix them—a process known as self-debugging. By combining an automated data collection pipeline with a novel reinforcement learning (RL) strategy that rewards both code correctness and explanation quality, LeDex boosts the performance of models like CodeLlama and StarCoder by up to 15% in coding benchmarks.
What Problem Does This Paper Address?
Even the best human developers rarely write perfect code on the first try; they rely on a cycle of trial, error, and debugging. While top-tier proprietary models like GPT-4 can "self-debug" when prompted with execution errors, smaller open-source models (like StarCoder or CodeLlama) are notoriously bad at it. They often hallucinate explanations or simply repeat the same buggy code when asked for a fix.
The core challenge is a data gap. Most code datasets contain only "gold standard" solutions. They lack the "trajectories" of debugging: the incorrect code, the specific error message, a logical explanation of the bug, and the eventual fix. Without seeing these intermediate steps, smaller models never learn the reasoning required to bridge the gap between a failing unit test and a working solution.
Methods
The authors propose a multi-stage framework called LeDex to turn LLMs into competent self-debuggers.
- Automated Data Pipeline: Instead of relying on expensive human annotations, the authors use a "teacher" model (like GPT-3.5) or even the model itself to generate explanations and refinements for buggy code. Crucially, these refinements are verified via execution. Only trajectories that result in code passing all unit tests are kept, ensuring high-quality training data.
- Supervised Fine-Tuning (SFT): The models are trained on a "Chain-of-Thought" style prompt: [Task] + [Buggy Code] + [Error Message] -> [Explanation] + [Fixed Code]. This teaches the model that explaining the bug is a prerequisite for fixing it.
- Reinforcement Learning (RL) with Dual Rewards: This is the paper's most novel contribution. Standard RL for code usually only rewards passing tests. LeDex uses two separate rewards:
- Refinement Reward: A mix of unit test pass rates and code similarity (CodeBLEU) to the ground truth.
- Explanation Reward: Uses a RoBERTa-based model to measure the semantic similarity of the model's explanation to a known correct explanation. This prevents the model from "cheating" by providing a correct fix with a nonsensical explanation.
Datasets
The authors utilized several standard competitive programming and basic coding benchmarks:
- Training: MBPP (Mostly Basic Python Problems), APPS, and CodeContests.
- Evaluation: HumanEval and MBPP, along with their more rigorous counterparts, HumanEval+ and MBPP+, which feature significantly more unit tests to catch "lucky" but incorrect solutions.
Key Insights and Ideas
- Explanations are the "Aha!" Moment: The researchers found that forcing the model to explain why code is wrong before fixing it significantly increases the success rate. It acts as a logical bridge, much like Chain-of-Thought prompting in math.
- Self-Bootstrapping Works: Surprisingly, a model can improve its debugging skills by training on data it generated itself (provided the fixes are verified by a compiler/interpreter). You don't always need a "God-model" like GPT-4 to provide the labels.
- Robustness via RL: While SFT gives the model the basic "format" of debugging, RL makes the model more robust. It helps the model handle stricter test cases (like those in HumanEval+) where simple pattern matching isn't enough.
- Iterative Refinement: Unlike standard prompting, which often plateaus after one attempt, LeDex-trained models show "iterative" ability—they can keep refining their code over multiple rounds, consistently improving their chances of success.
Key Results
- Massive Accuracy Boosts: On the MBPP benchmark, LeDex SFT improved the
pass@1(the probability that the first attempt is correct) by 15.92% for StarCoder-15B. - RL Gains: Adding RL on top of SFT provided an additional boost of up to 3.54% on
pass@1. - Refinement Success: For StarCoder, the success rate of fixing a known buggy solution jumped from a measly 6.4% (using standard prompting) to 17.5% with LeDex.
- Human-Approved: In blind tests, human developers rated the explanations from LeDex-trained models as significantly more useful and accurate than those from base models, narrowing the gap with GPT-3.5.
Limitations and Future Work
- Unit Test Dependency: The framework relies heavily on the availability of unit tests. In real-world software engineering, writing the tests is often as hard as writing the code.
- Prompt Sensitivity: The study focused on specific prompt types. The authors acknowledge that a broader range of prompt designs could potentially unlock even more performance.
- Language Scope: The current work is focused primarily on Python. Extending this to lower-level languages (like C++) or web frameworks where "execution feedback" is more complex (e.g., UI rendering) remains a future challenge.
Summary updated