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

Hot or Cold? Adaptive Temperature Sampling for Code Generation with Large Language Models

Yuqi Zhu, Jia Li 0011, Ge Li, Yunfei Zhao, Jia Li 0012, Zhi Jin, Hong Mei

Abstract

Large Language Models (LLMs) typically employ uniform decoding strategies for both natural language and code. However, programming languages possess a rigid syntax where specific structural tokens act as logical anchors. This paper introduces AdapT (Adaptive Temperature) sampling, a decoding-time heuristic that applies high temperature to structural "challenging" tokens to encourage logical exploration and low temperature to "confident" tokens to minimize syntax errors. Evaluated on HumanEval and MBPP, AdapT significantly improves code generation accuracy and reduces common errors without requiring model retraining.

The Challenge of Code Generation

While Large Language Models have revolutionized code generation, they often rely on decoding strategies optimized for natural language (NL). Standard temperature sampling uses a single parameter $T$ to balance predictability and creativity. However, programming languages (PL) are fundamentally different: they are governed by strict, non-forgiving syntax. A single misplaced character can render an entire script non-functional. Current strategies treat every token equally, failing to distinguish between structural "anchors" that define logic and highly predictable boilerplate code.

The authors analyzed the loss distribution of code versus natural language, finding that code exhibits distinct "peaks"—specific tokens that are significantly harder for the model to predict. As shown in Figure 5, code generation involves higher variance in token difficulty compared to the relatively smoother loss curves of natural language.

Comparison of loss distributions (-logP) between natural language (NL) and code across HumanEval and MBPP benchmarks.

AdapT Sampling Methodology

Statistical analysis revealed two types of tokens:

  1. Challenging Tokens: Difficult to predict, often representing the start of a new logical block.
  2. Confident Tokens: Easily inferred from the preceding context.

The "Start of Block" heuristic identifies that challenging tokens almost always appear at the beginning of a code block (e.g., immediately following an indentation change). Figure 1 illustrates this relationship, where tokens like while and if correspond to sharp peaks in the negative log probability (-logP).

Visualization of code tokens and their corresponding -logP peaks. Highlighted tokens represent 'challenging' structural anchors.

AdapT sampling dynamically adjusts the temperature $T$ based on token position:

  • High Temperature ($a$): Applied to the initial token of a code block to allow the model to explore diverse logical paths (e.g., deciding between an if statement or a while loop).
  • Low Temperature ($b$): Applied to all other tokens to maintain focus and prevent syntax errors once the block's logic is established.

Experimental Results

The researchers evaluated AdapT on HumanEval and MBPP using models like CodeGeeX-13B and CodeGen-2B.

  • Accuracy Gains: On HumanEval, AdapT improved the pass@15 score of CodeGeeX-13B from 36.0% to 40.9%, a 13.6% relative improvement.
  • Error Reduction: Qualitative analysis showed a significant drop in NameError and SyntaxError. For CodeGen-2B, NameError instances decreased by 44.3% compared to standard sampling.
  • Zero-Shot Utility: As a decoding-time heuristic, AdapT requires no retraining or fine-tuning, making it an efficient "plug-and-play" solution for existing LLMs.

Conclusion and Future Work

AdapT sampling demonstrates that treating code tokens differently based on their structural role leads to more robust and accurate generation. By reducing "tail randomness" in predictable segments while maintaining creativity at decision points, the method achieves state-of-the-art performance across multiple benchmarks.

While the current heuristic focuses on block starts, future work may involve learning-based temperature adjustment to identify challenging tokens within complex expressions or across different programming languages with varying syntax structures.

Summary updated

BibTeX
@inproceedings{zhu2024hot,
  author = {Yuqi Zhu and Jia Li and Ge Li and Yunfei Zhao and Jia Li and Zhi Jin and Hong Mei},
  title = {{Hot or Cold? Adaptive Temperature Sampling for Code Generation with Large Language Models}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2024},
  doi = {10.1609/aaai.v38i1.27798},
}
Figure