TL;DR
This paper introduces CoCoS, a novel reinforcement learning framework that empowers small language models (SLMs) to effectively self-correct their code generation. By addressing the limitations of existing methods, CoCoS enables SLMs to achieve significant improvements in code quality, making advanced AI coding capabilities more accessible and cost-effective.
What Problem Does This Paper Address?
Generating correct code snippets in a single attempt is a significant challenge for even the most advanced AI models. While self-correction techniques have shown promise, they often rely on large, proprietary models (like GPT or Gemini) that are expensive to use and inaccessible for many researchers. This paper investigates whether smaller, open-source language models (SLMs) can achieve meaningful self-correction without external supervision. The authors find that existing prompting-based and even some training-based self-correction methods struggle to generalize to SLMs, often failing to produce effective revisions. This gap highlights the need for a method that can intrinsically enhance the self-correction capabilities of smaller models.
Methods
The core contribution of this paper is CoCoS (Self-Correcting Code generation using Small LMs), a reinforcement learning (RL) framework designed to enable intrinsic self-correction in SLMs. CoCoS tackles the multi-turn code generation problem by:
- Accumulated Reward Function: Instead of just rewarding the final output, CoCoS uses a reward function that aggregates rewards across the entire generation trajectory, considering previous responses. This encourages the model to not only produce correct final code but also to improve its initial response quality.
- Progressive Reward: CoCoS employs a fine-grained reward mechanism that assesses code quality based on the pass ratio of unit tests. This provides a more nuanced evaluation than a simple binary pass/fail, allowing the model to learn from incremental improvements and avoid penalizing minor errors too harshly.
- Multi-turn Markov Decision Process (MDP): The self-correction process is framed as a multi-turn MDP, where the model's state includes the problem, its previous outputs, and auxiliary instructions. The policy learns to generate the next code snippet based on this state.
The framework utilizes a REINFORCE-style policy gradient method with a leave-one-out estimator for stable training.
Datasets
The authors trained and evaluated their models on several standard code generation datasets:
- KodCode: Used for pre-training and fine-tuning "boost models" to ensure parseable code for RL training.
- MBPP (Mostly Basic Python Problems): Used for training and evaluation.
- HumanEval: Used for evaluation.
- ODEX (Open-Domain EXecution): Used for evaluation.
Key Insights and Ideas
- SLMs Struggle with Prompting-Based Self-Correction: The paper's preliminary study reveals that SLMs, unlike their larger counterparts, do not effectively self-correct using only prompting. They often fail to revise their outputs meaningfully or even degrade performance.
- The Challenge of Training-Based Methods for SLMs: Existing training-based self-correction methods, which often rely on separate corrector models or assume strong intrinsic correction abilities, are not directly applicable or effective for SLMs.
- The Power of Multi-Turn Reward Shaping: CoCoS's success hinges on its sophisticated reward system. By accumulating rewards and using a progressive assessment of unit test passes, it guides SLMs to improve iteratively without collapsing during training, a common issue with simpler reward schemes.
- Robustness to Prompt Variations: CoCoS demonstrates robustness to variations in instruction prompts, suggesting its learned self-correction capabilities are not overly dependent on specific phrasing.
Key Results
CoCoS achieves significant improvements in code generation accuracy for SLMs across various datasets:
- MBPP: CoCoS models achieved improvements of up to 35.8% on MBPP compared to baselines. For example, with the Qwen2.5-1.5B model, CoCoS reached 54.2% accuracy at t2, a 9.2% gain in ∆Acc.
- HumanEval: CoCoS showed improvements of 27.7% on HumanEval. For Qwen2.5-1.5B, it achieved 32.3% accuracy at t2, a 3.0% gain in ∆Acc.
- ODEX: CoCoS also demonstrated strong performance on ODEX, with a 5.9% ∆Acc for Qwen2.5-1.5B.
- Selective Correction: CoCoS excels at selectively correcting incorrect responses without altering correct ones, leading to the highest correction success rates.
Limitations and Future Work
- Training Cost: Online reinforcement learning incurs costs at every action step. Due to infrastructural constraints, CoCoS training was limited to only two turns.
- Scalability: Experiments were primarily conducted on 1B-scale SLMs. Evaluating scalability to larger models remains an open question.
- Future Work: The authors suggest extending CoCoS to support multi-turn training beyond two turns, potentially incorporating more cost-efficient strategies like offline reinforcement learning. They also propose exploring the scalability of their approach to larger models.
Summary updated