Abstract
Preference learning enhances Code LLMs beyond supervised fine-tuning by leveraging relative quality comparisons. Existing methods construct preference pairs from candidates based on test case success, treating the higher pass rate sample as positive and the lower as negative. However, this approach does not pinpoint specific errors in the code, which prevents the model from learning more informative error correction patterns, as aligning failing code as a whole lacks the granularity needed to capture meaningful error-resolution relationships. To address these issues, we propose IterPref, a new preference alignment framework that mimics human iterative debugging to refine Code LLMs. IterPref explicitly locates error regions and aligns the corresponding tokens via a tailored DPO algorithm. To generate informative pairs, we introduce the CodeFlow dataset, where samples are iteratively refined until passing tests, with modifications capturing error corrections. Extensive experiments show that a diverse suite of Code LLMs equipped with IterPref achieves significant performance gains in code generation and improves on challenging tasks like BigCodeBench. In-depth analysis reveals that IterPref yields fewer errors. Our code and data will be made publicaly available.
Introduction: The Need for Granularity in Code Alignment
Standard preference learning for Large Language Models (LLMs), such as Direct Preference Optimization (DPO), typically operates on a "snippet-level" basis. In code generation, a model is shown a "preferred" snippet (one that passes tests) and a "dispreferred" snippet (one that fails).
However, code is unique: a failing snippet is often 99% correct, containing only a single logical flaw or syntax error. Standard DPO penalizes the model for every token in the failing snippet, even the correct ones. This creates a noisy signal that confuses the model, often leading it to adjust irrelevant parts of the code or overfit to specific patterns rather than learning the logic of error correction. IterPref addresses this by introducing Target-DPO, which shifts the focus from global alignment to focal alignment.
Methodology: Target-DPO and Iterative Debugging
The IterPref framework introduces two main innovations: the CodeFlow dataset and the Target-DPO loss function.
Iterative Debugging & CodeFlow Dataset
Instead of just sampling "good" and "bad" code, the researchers use an LLM to iteratively refine a buggy snippet until it passes unit tests. This process mimics a human developer's workflow. The resulting CodeFlow dataset (59k pairs) captures the specific "edit traces" required to fix a bug.
Targeted DPO Loss
Using the Longest Common Subsequence (LCS) algorithm, the framework identifies the exact lines that differ between the buggy version and the fixed version. These are labeled as "critical regions." Target-DPO introduces a masking strategy: it only calculates the loss for tokens within the identified error regions. Correct tokens that appear in both versions are masked out, forcing the model to focus its learning energy exclusively on the functional faults.
Experimental Results
The model was tested across a rigorous suite of benchmarks, including HumanEval, MBPP, BigCodeBench (BCB), and LiveCodeBench (LCB).
Performance Gains
Across all tested models (DeepSeek-Coder, CodeQwen, StarCoder2, Qwen2.5-Coder), Target-DPO consistently outperformed vanilla DPO and RPO (Relative Preference Optimization). For instance, Qwen2.5-Coder-7B equipped with Target-DPO achieved a 29.7% pass@1 on BigCodeBench-Hard, matching the performance of much larger models like DeepSeek-Coder-V2-Instruct and approaching Llama-3.1-405B-Instruct (30.4%).
Error Reduction
Statistical analysis showed that Target-DPO models produced significantly fewer common errors (like AttributeError or KeyError) compared to models trained with standard DPO. This suggests that by isolating the bug, the model learns the relationship between an error and its fix, rather than just memorizing "good" code.
Conclusion and Future Work
IterPref demonstrates that for structured data like code, global preference signals are too blunt. By focusing on focal alignment, the framework achieves high efficiency, matching massive SFT baselines with a fraction of the data. Future work will explore scaling this method to millions of samples and investigating its generalizability to other programming languages and reasoning tasks.
Summary updated