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

Aligning CodeLLMs with Direct Preference Optimization

Yibo Miao, Bofei Gao, Shanghaoran Quan, Junyang Lin, Daoguang Zan, Jiaheng Liu, Jian Yang, Tianyu Liu, Zhijie DengarXiv 2024 · DOI

Introduction

Large Language Models for code (CodeLLMs) have demonstrated remarkable capabilities in code generation through extensive pre-training. However, the "alignment" phase—the process of ensuring these models follow instructions and reason logically—remains a significant challenge. Traditional approaches often rely on Proximal Policy Optimization (PPO), which can be cumbersome and limited by the quality of reward signals. This paper explores the application of Direct Preference Optimization (DPO) to CodeLLMs, demonstrating that it can significantly enhance coding performance by learning more nuanced distinctions between code quality levels.

The Sparse Reward Problem in Code Alignment

Aligning models to human intent typically involves Reinforcement Learning from Human Feedback (RLHF). In the coding domain, PPO has been the standard, utilizing a reward model to provide feedback. However, PPO is often constrained by coarse reward functions. For instance, a program that fails to compile due to a single missing character might receive the same negative reward as a completely nonsensical snippet. This "sparse reward" problem makes it difficult for models to navigate the complex logical landscape of high-level programming. Furthermore, manual rule-crafting is prone to "reward hacking," where models exploit loopholes in the rules rather than improving their underlying logic.

Methodology: Direct Preference Optimization (DPO)

To overcome these limitations, the researchers propose using Direct Preference Optimization (DPO). Unlike PPO, DPO does not require a separate reward model. Instead, it optimizes the model's policy directly using preference pairs consisting of a "chosen" response and a "rejected" response. By comparing these pairs, the model implicitly learns a fine-grained reward structure. This approach allows the model to naturally rank "nearly correct" code higher than "completely broken" code, provided the training data includes a diverse range of failure modes.

The On-Policy Data Pipeline

A critical component of this research is the pipeline developed for generating on-policy preference data. The researchers collected approximately 3,000 coding queries and associated unit tests from platforms like LeetCode and Codeforces. The target CodeLLM then generates multiple responses for each query. These responses are executed against the unit tests; those that pass all tests are labeled "chosen," while those that fail are labeled "rejected." This on-policy approach ensures the model learns from its own specific logical errors rather than generic mistakes from other models.

The on-policy data generation pipeline and the rule-based reward function used for comparison.

Key Insights: Learning from Failure and Implicit Granularity

The study highlights several key insights into the alignment process:

  • The Power of Rejection: Comparing DPO with Rejection Sampling Fine-Tuning (RFT) revealed that learning from "rejected" samples is as crucial as learning from "chosen" ones. DPO uses failures to understand what patterns to avoid.
  • On-Policy Necessity: The researchers found that using off-policy data (generated by a different model) actually degraded performance. Effective alignment requires the model to address its own specific blind spots.
  • Implicit Granularity: Even with binary feedback (Pass/Fail) from the executor, the DPO objective enables the model to develop a nuanced internal ranking of code quality across thousands of examples.
DPO demonstrates fine-grained reward differences between code responses that rule-based systems treat as identical.

Experimental Results and Robustness

The researchers applied DPO to several state-of-the-art open-source models, including CodeQwen1.5 7B, DeepSeek-Coder 6.7B, and CodeLlama-Instruct 7B.

  • CodeQwen1.5 7B: Saw the most dramatic gains, with HumanEval scores jumping from 0.829 to 0.878 and MBPP scores rising from 0.783 to 0.804.
  • DeepSeek-Coder 6.7B: Improved from 0.768 to 0.787 on HumanEval.

Notably, these models maintained their performance on the more rigorous "Plus" versions of MBPP and HumanEval, which feature significantly more test cases. This suggests that the DPO-aligned models are producing more reliable and robust code rather than simply memorizing solutions.

Conclusion

Direct Preference Optimization offers a powerful and efficient alternative to PPO for aligning CodeLLMs. By leveraging on-policy data and learning from both success and failure, DPO enables models to develop a more sophisticated understanding of code quality. While data scale remains a limitation, future work in synthetic data generation holds promise for further pushing the performance boundaries of aligned coding models.

Summary updated

BibTeX
@misc{miao2024aligning,
  author = {Yibo Miao and Bofei Gao and Shanghaoran Quan and Junyang Lin and Daoguang Zan and Jiaheng Liu and Jian Yang and Tianyu Liu and Zhijie Deng},
  title = {{Aligning CodeLLMs with Direct Preference Optimization}},
  year = {2024},
  eprint = {2410.18585},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.18585},
}
Figure