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

CodeDPO: Aligning Code Models with Self Generated and Verified Source Code

Kechi Zhang, Ge Li, Yihong Dong, Jingjing Xu, Jun Zhang, Jing Su, Yongfei Liu, Zhi Jin

Abstract

Code generation models have shown significant potential for programming tasks. However, existing training methods like supervised fine-tuning (SFT) face key limitations: they do not effectively teach models to prioritize correct over incorrect solutions in ambiguous situations, nor do they effectively optimize the runtime efficiency of the generated code. To address these challenges, we propose CodeDPO, a framework that integrates preference learning into code generation to improve two key code preference factors: code correctness and efficiency. CodeDPO employs a novel dataset construction method, utilizing a self-generation-and-validation mechanism that simultaneously generates and evaluates code and test cases. The underlying assumption is that test cases executable by multiple code snippets provide more reliable validation, and code that passes more tests is more likely to be correct. Through this self-validation process, our PageRank-inspired algorithm iteratively updates the ranking score of each code snippet, ultimately creating a code preference optimization dataset based on correctness and efficiency. Through comprehensive evaluations of five widely used benchmarks, CodeDPO demonstrates significant improvements in correctness and efficiency compared to existing methods.

Introduction

While Large Language Models (LLMs) have become remarkably proficient at writing code, they face two major hurdles. First, the SFT Ceiling: traditional Supervised Fine-Tuning trains models to mimic "correct" code but fails to distinguish between high-quality solutions and mediocre ones that merely pass tests. This often leads to performance saturation where the model's likelihood of generating incorrect code increases alongside its likelihood of generating correct code. Second, Efficiency Neglect: most training methods focus solely on correctness, ignoring the runtime performance (efficiency) of the generated code.

Existing preference-learning methods often rely on expensive "teacher" models like GPT-4 to label data, which limits scalability. CodeDPO addresses these issues by creating a self-sustaining pipeline for alignment that optimizes for both correctness and efficiency without requiring proprietary models.

Methodology

The CodeDPO framework follows a four-step pipeline designed to generate high-quality preference data through self-verification.

The CodeDPO pipeline: from data seed construction to model optimization via mutual verification and efficiency ranking.
  1. Data Seed Construction: Programming concepts are extracted from real-world open-source repositories to create diverse task prompts.
  2. Mutual Verification (The "PageRank" Trick): The model generates multiple code snippets and test cases for a prompt. Instead of simple pass/fail metrics, a PageRank-inspired algorithm is used. A test case is considered "credible" if passed by many snippets, and a snippet is "correct" if it passes many credible tests. This suppresses "weak" tests (e.g., assert True) that do not help distinguish code quality.
  3. Efficiency Optimization: Once credible tests are identified, the system measures execution time. Preference pairs are created where the "winner" is the faster version of two correct solutions.
  4. DPO Training: The model is trained using Direct Preference Optimization (specifically the RPO variant) to prefer higher-ranked, faster code.

Results and Evaluation

CodeDPO was evaluated on five major benchmarks, including HumanEval/HumanEval+, MBPP/MBPP+, and EffiBench. A 6.7B parameter model trained with CodeDPO achieved a state-of-the-art 83.5% pass rate on HumanEval, significantly outperforming baseline SFT models.

Runtime speedup across different model architectures on the HumanEval+ benchmark after CodeDPO alignment.

Beyond correctness, CodeDPO-aligned models produced code that was 1.25x to 1.45x faster on average compared to their baselines. The framework also showed robustness across architectures, with a 36.1% relative improvement on StarCoder2-7B and consistent gains in competitive programming tasks on LiveCodeBench.

Discussion and Conclusion

CodeDPO demonstrates that models can "lift themselves up by their bootstraps" through mutual verification, removing the "GPT-4 tax" for alignment. However, the framework currently relies on code being executable, which may limit its application in abstract logic or UI-heavy domains. Future work could integrate static analysis to address code readability, maintainability, and security vulnerabilities. Additionally, expanding the diversity of the initial data seeds could improve performance in specialized libraries like PyTorch or TensorFlow.

Summary updated

BibTeX
@inproceedings{zhang2025codedpo,
  author = {Kechi Zhang and Ge Li and Yihong Dong and Jingjing Xu and Jun Zhang and Jing Su and Yongfei Liu and Zhi Jin},
  title = {{CodeDPO: Aligning Code Models with Self Generated and Verified Source Code}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
  url = {https://openreview.net/forum?id=XwUteLQvNH},
}
Figure