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

When Neural Code Completion Models Size up the Situation: Attaining Cheaper and Faster Completion through Dynamic Model Inference

Zhensu Sun, Xiaoning Du, Fu Song, Shangwen Wang, Li Li · DOI

Abstract

Large Code Models (LCMs) have revolutionized code completion but at a high computational and environmental cost. Current models often over-compute on simple tokens and persist in generating incorrect code when context is insufficient. We propose the Stop&Exit Controller (SEC), a dynamic inference framework that utilizes a lightweight classifier to either "Exit" early for confident predictions or "Stop" generation when errors are likely. Our results show that SEC can achieve up to a 71% speedup in inference while maintaining a stable developer acceptance rate, offering a more efficient and user-centric approach to AI-assisted programming.

Introduction

Modern code completion tools like GitHub Copilot rely on "heavy" Large Code Models (LCMs) with billions of parameters. These models require massive GPU resources and incur significant financial costs. The authors identify two major inefficiencies in current LCM usage: over-computation, where every layer is used even for simple tokens, and persistence in error, where models continue generating "hallucinated" code despite lacking context.

Figure 1: Comparison of hard and easy completion scenarios illustrating the need for STOP and EXIT mechanisms.

As shown in Figure 1, some code segments are "easy" to complete because similar examples exist in the immediate context, while others are "hard" due to missing information. Standard models treat both cases with the same computational intensity, leading to wasted resources and unhelpful suggestions.

Methods: The Stop&Exit Controller (SEC)

The SEC framework integrates a lightweight linear classifier after each layer of an LCM. During inference, for every token generated, the classifier decides between three actions:

  • Exit: If the current layer is confident, SEC outputs the token immediately and skips all remaining deeper layers for that round.
  • Stop: If the classifier detects insufficient information, it terminates the generation process entirely to avoid unhelpful suggestions.
  • Continue: The default behavior of passing data to the next layer.
Figure 6: The SEC architecture featuring the action classifier and the human-in-the-loop inference process with state copying.

To maintain multi-token generation, SEC employs a State Copying mechanism. Since Transformers rely on attention states from previous layers, skipping layers would normally break the sequence. SEC copies the hidden state of the exited layer to deeper layers to maintain a functional memory for subsequent tokens. This also enables a "Human-in-the-Loop" workflow (Figure 6), where the model stops at a point of uncertainty, allowing the user to provide the missing key information before triggering a fresh completion.

Empirical Insights

An empirical study of model behavior revealed two critical insights:

  1. Most code is "easy": Approximately 54.4% of tokens can be predicted correctly using just the first layer of a 12-layer GPT-2 model. Using all layers for these tokens is a massive waste of energy.
  2. The "First Token" Rule: If a model mispredicts the first token of a completion, the subsequent tokens have an acceptance rate of only 4.2%. This validates the "Stop" mechanism as a way to prune low-value generations early.

Key Results

The researchers evaluated SEC using Java (COFIC dataset) and Python (CodeSearchNet) with GPT-2 and CodeGen models.

  • Efficiency: SEC achieved an average 11.2% speedup with only a 1.1% reduction in ROUGE-L.
  • Aggressive Savings: Under optimized settings, SEC reached a 71% speedup while the developer acceptance rate remained remarkably stable (dropping only from 46.7% to 44.9%).
  • High Precision: The classifier achieved high precision for both Exit (0.944) and Stop (0.812) actions at a 0.9 confidence threshold.

Limitations and Future Work

While promising, the study was conducted on models with hundreds of millions of parameters. Future research is needed to confirm these findings on 10B+ parameter models like StarCoder. Additionally, predicting when to "Stop" remains more challenging than predicting when to "Exit," suggesting a need for more sophisticated uncertainty classifiers.

Conclusion

The Stop&Exit Controller represents a significant step toward making Large Code Models more sustainable and user-friendly. By treating model uncertainty as a signal to either save compute or avoid unhelpful interruptions, SEC aligns model inference with the practical needs of software developers.

Summary updated

BibTeX
@inproceedings{sun2024when,
  author = {Zhensu Sun and Xiaoning Du and Fu Song and Shangwen Wang and Li Li},
  title = {{When Neural Code Completion Models Size up the Situation: Attaining Cheaper and Faster Completion through Dynamic Model Inference}},
  booktitle = {ICSE},
  year = {2024},
  url = {https://openreview.net/forum?id=JYaEV12HPy},
}
Figure