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

Code Comparison Tuning for Code Large Language Models

Yufan Jiang, Qiaozhi He, Xiaomin Zhuang, Zhihua WuarXiv 2024

Abstract

Code Large Language Models (Code LLMs) often struggle with identifying and fixing subtle logical errors. We introduce Code Comparison Tuning (CCT), a novel fine-tuning approach that integrates contrastive learning at both the token and sequence levels. By explicitly training models to distinguish between correct code and nearly identical buggy versions, CCT significantly enhances their debugging capabilities. Evaluated on the HumanEvalFix benchmark, CCT-tuned models show improvements of up to 4 points in pass@1 scores, with a 13B parameter model matching the performance of GPT-4.

Introduction

The rapid advancement of Code LLMs has revolutionized automated code generation. However, a persistent challenge remains: bug fixing. While these models can generate complex snippets from scratch, they often fail to detect or repair "subtle" errors—minor logic flips, variable misuses, or off-by-one errors. Standard instruction tuning, which maps natural language prompts to correct code, lacks the contrastive signal needed for models to understand why a piece of code is incorrect. When faced with these nuances, models often repeat the same mistake or introduce new ones.

The Code Comparison Tuning (CCT) Framework

To address this, we propose Code Comparison Tuning (CCT). The core idea is to teach the model to compare correct code with erroneous versions. This comparison happens at two levels:

  1. Token-level Comparison: To sensitize the model to minute changes (e.g., swapping > for <), we employ a preference loss. By identifying the first point of divergence between a correct snippet and its corrupted counterpart, we apply a triplet-style loss. This penalizes the model if it doesn't "prefer" the correct token's representation over the incorrect one in that specific context.
  2. Sequence-level Comparison: We augment the training data with "bug-fix" pairs. Correct code is paired with a version containing injected bugs, wrapped in instructions like "Fix the bugs in the code." This teaches the model the global relationship between a buggy input and a corrected output.
The CCT framework integrates standard language modeling loss with token-level and sequence-level comparison losses.

Bug Injection Strategy

A critical component of CCT is the generation of high-quality "negative" examples. We use a set of manual rules to inject bugs into functional code. These include:

  • Variable Misuse: Randomly replacing a variable with another one present in the scope.
  • Operator Misuse: Swapping logical or arithmetic operators (e.g., + to -, == to !=).
  • Function/Method Deletion: Removing essential calls like len() or range().
Bugs are manually injected into correct code to create training pairs for comparison.

The final training objective is a weighted sum of the standard Language Modeling loss ($L_{lm}$), the token-level comparison loss ($L_{token}$), and the sequence-level comparison loss ($L_{seq}$).

Experimental Setup

We fine-tuned models using the Python subset of the Evol-Instruct-Code-80k dataset. Evaluation was conducted on HumanEvalFix, a benchmark consisting of 164 problems where models must fix manually inserted errors in Python code. We also utilized HumanEvalFixDocs, which provides docstrings as additional context for the fix.

Results and Discussion

CCT consistently outperformed standard instruction tuning across various model sizes and architectures.

  • Performance Gains: CCT-CodeLlama-13B achieved a pass@1 score of 47.7%, a significant jump from the 43.5% achieved by standard instruction tuning. Remarkably, this 13B model matched the performance of GPT-4 (47.0%) on the HumanEvalFix benchmark.
  • Model Scaling: CCT-StarCoder (15.5B) saw its pass@1 score rise from 33.7% to 38.3%.
  • Data Efficiency: Analysis showed that CCT is highly data-efficient; a model tuned on just 20% of the data with CCT could outperform one tuned on 100% of the data using standard methods.

The results highlight that contrastive signals are vital for debugging. While sequence-level data augmentation helps, the addition of token-level preference loss provides a critical boost in sensitivity to subtle logic. It’s not just about the data; it’s about how the model is forced to look at the data.

Conclusion

Code Comparison Tuning offers a simple yet powerful way to enhance the debugging skills of Code LLMs. By forcing models to look at "near-miss" examples, they develop a sharper eye for logical consistency. Future research will explore more sophisticated bug generation techniques (e.g., using LLMs to create "clever" bugs) and extending the method to a wider array of programming languages and complex software environments.

Summary updated

BibTeX
@misc{jiang2024code,
  author = {Yufan Jiang and Qiaozhi He and Xiaomin Zhuang and Zhihua Wu},
  title = {{Code Comparison Tuning for Code Large Language Models}},
  year = {2024},
  eprint = {2403.19121},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2403.19121},
}
Figure