USCD: Improving Code Generation of LLMs by Uncertainty-Aware Selective Contrastive Decoding

Shuai Wang, Liang Ding, Li Shen, Yong Luo, Zheng He, Wei Yu, Dacheng TaoarXiv 2024 · arXiv · OpenReview

Abstract

Large language models (LLMs) have shown remarkable capabilities in code generation. However, the effects of hallucinations (e.g., output noise) make it particularly challenging for LLMs to generate high-quality code in one pass. In this work, we propose a simple and effective uncertainty-aware selective contrastive decoding ($\mathbb{USCD}$) mechanism to improve the quality of one-pass code generation in LLMs and reduce the impact of output noise. To be specific, we first elaborately designed a negative prompt (namely lame prompt) to output noise by removing input-output examples from the standard few-shot prompt. Our preliminary study shows that the Jensen-Shannon divergence (JS divergence) between token distribution uncertainty and the output noise is relatively low (approximately $0.25$), indicating their high relevance. Then, we selectively eliminate output noise induced by lame prompts based on the uncertainty of the prediction distribution from the standard prompt. Notably, our proposed plug-and-play mechanism is an inference-only method, enjoying appealing flexibility. Extensive experiments on widely used benchmarks, e.g., HumanEval, MBPP, and MultiPL-E, upon several LLMs (i.e., Inocder-6b, CodeLlama-7b, WizardCoder-15b, StarCoder, and Llama2-7b), demonstrate that our proposed USCD significantly improves one-pass code generation, with an average pass@1 scores increase of 16.59%.

Introduction

While Large Language Models (LLMs) are impressive, they often stumble when writing code in a single attempt ("one-pass generation"). Unlike natural language, programming languages have unforgiving syntax and logic rules; a single misplaced character can render a script useless. These errors are often the result of "output noise" or hallucinations. Existing solutions usually involve expensive fine-tuning or multi-step "self-repair" cycles that require external feedback (like a Python interpreter). This research introduces a way to improve code quality at the moment of generation—during inference—without needing to retrain the model or rely on external tools.

Methods: Uncertainty-Aware Selective Contrastive Decoding

The proposed USCD mechanism operates in two stages to refine the model's output during inference:

  1. The "Lame Prompt": To understand what "noise" looks like for a specific model, a negative prompt is created. By stripping away input-output examples from a standard few-shot prompt and leaving only high-level instructions, the "lame prompt" coaxes the model into showing its noisy distribution.
  2. Uncertainty-Aware Selection: Contrastive decoding typically subtracts a "bad" distribution from a "good" one. USCD optimizes this by using the standard deviation of predicted token probabilities (logits) as a proxy for uncertainty. If the model is confident (high standard deviation), it proceeds normally. If it is "uncertain" (low standard deviation), USCD triggers the contrastive mechanism, subtracting the "lame" distribution from the "standard" one to steer the model away from likely errors.
Overview of the USCD mechanism, illustrating the use of standard and lame prompts to refine token distribution through uncertainty-aware selection.

This approach is "plug-and-play," meaning it can be applied to any open-source LLM without changing its weights.

Experimental Evaluation

The researchers evaluated USCD across three major code generation benchmarks: HumanEval (164 hand-written Python problems), MBPP (500 Python tasks), and MultiPL-E (a multi-lingual version covering 8 languages). The evaluation spanned a diverse fleet of models, including Llama2-7b, CodeLlama-7b, StarCoder, WizardCoder-15b, and Incoder-6b.

Key Results

The results were consistently positive across all tested models and benchmarks. On average, USCD improved the pass@1 score—the probability that the first generated code snippet is correct—by 16.59%.

Pass@1 scores on HumanEval for various models, comparing standard prompting against the USCD method.

Specific highlights include:

  • Significant Model Gains: The Incoder-6b model saw a 30.66% relative improvement on HumanEval, while Llama2-7b improved by 35.80%.
  • Multi-lingual Success: The method proved effective beyond Python, showing significant gains in languages like C++ and Java.
  • Competitive Performance: USCD outperformed several "internal self-repair" methods and remained highly competitive with "external self-repair" methods that utilize ChatGPT as a critic.

Discussion and Conclusion

A key insight of this work is the high correlation between a model's uncertainty and the noise produced by a lame prompt. By selectively intervening only when uncertainty is high, USCD preserves the model's strengths while mitigating its weaknesses.

However, the method has limitations. Because it requires calculating logits for both standard and lame prompts, it can slow down generation speed. Additionally, it requires access to raw logits, making it inapplicable to closed-source, API-only models like GPT-4. Future work will focus on accelerating inference speed while maintaining these accuracy gains.

Summary updated

Abstract

Large language models (LLMs) have shown remarkable capabilities in code generation. However, the effects of hallucinations (e.g., output noise) make it particularly challenging for LLMs to generate high-quality code in one pass. In this work, we propose a simple and effective \textbf{u}ncertainty-aware \textbf{s}elective \textbf{c}ontrastive \textbf{d}ecoding ($\mathbb{USCD}$) mechanism to improve the quality of one-pass code generation in LLMs and reduce the impact of output noise. To be specific, we first elaborately designed a negative prompt (namely lame prompt) to output noise by removing input-output examples from the standard few-shot prompt. Our preliminary study shows that the Jensen-Shannon divergence (JS divergence) between token distribution uncertainty and the output noise is relatively low (approximately $0.25$), indicating their high relevance. Then, we selectively eliminate output noise induced by lame prompts based on the uncertainty of the prediction distribution from the standard prompt. Notably, our proposed plug-and-play mechanism is an inference-only method, enjoying appealing flexibility. Extensive experiments on widely used benchmarks, e.g., HumanEval, MBPP, and MultiPL-E, upon several LLMs (i.e., Inocder-6b, CodeLlama-7b, WizardCoder-15b, StarCoder, and Llama2-7b), demonstrate that our proposed USCD significantly improves one-pass code generation, with an average \textit{pass@$1$} scores increase of 16.59\%. We will release code and data on GitHub.
BibTeX
@misc{wang2024uscd,
  author = {Shuai Wang and Liang Ding and Li Shen and Yong Luo and Zheng He and Wei Yu and Dacheng Tao},
  title = {{USCD: Improving Code Generation of LLMs by Uncertainty-Aware Selective Contrastive Decoding}},
  year = {2024},
  eprint = {2409.05923},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2409.05923},
}
Figure