When to Stop? Towards Efficient Code Generation in LLMs with Excess Token Prevention

Lianghong Guo, Yanlin Wang, Ensheng Shi, Wanjun Zhong, Hongyu Zhang, Jiachi Chen, Ruikai Zhang, Yuchi Ma, Zibin Zheng

Introduction

Large Language Models (LLMs) have become indispensable tools for automated code generation. However, their efficiency is often hampered by a phenomenon known as "excess token generation." Even after successfully generating the requested code block or function, many models fail to produce an end-of-sentence (<EOS>) token. Instead, they continue to generate redundant content—such as unit tests, unrelated functions, or repetitive comments—until they reach a hard token limit. This behavior not only wastes significant computational resources and energy but also degrades the developer experience by requiring manual cleanup of the output.

This paper introduces CodeFast, an acceleration framework designed to detect the logical conclusion of a code generation task in real-time. By employing a lightweight gating classifier called GenGuard, CodeFast enables models to stop inference precisely when the task is complete, achieving speedups of up to 452% without sacrificing code quality.

The Challenge of Excess Token Generation

The authors identify excess generation as a universal bottleneck across contemporary Code LLMs, including both base models (like StarCoder) and instruction-tuned variants (like Phind-CodeLlama). In some benchmarks, models were found to generate unnecessary tokens in 100% of their outputs.

While instruction tuning improves a model's ability to follow formatting constraints, it does not eliminate the "continual generation" problem. Because LLMs are autoregressive, every unnecessary token generated incurs a linear cost in time and compute. The core challenge lies in identifying the exact moment the "semantic" task is finished, even when the model's internal probability for an <EOS> token remains low.

The CodeFast Framework

CodeFast addresses this efficiency gap through a three-pronged approach: automated data construction, a lightweight classifier, and a robust decision mechanism.

1. Automatic Data Construction

To train a model to recognize stopping points, the authors developed a "sampling and labeling" pipeline. They prompted LLMs to generate code and then applied two distinct analyzers to identify the ideal termination point:

  • Syntax-based Analysis: Utilizing tree-sitter to determine where a target function or class logically ends based on language grammar.
  • Semantic-based Analysis: Leveraging LLMs (e.g., ChatGPT) to identify logical stopping points in cases where syntax alone is insufficient, such as when a model begins generating repetitive or irrelevant comments after a valid code block.

2. GenGuard: A Lightweight Gating Classifier

At the heart of CodeFast is GenGuard, a tiny linear classifier with fewer than 0.02 million parameters. GenGuard is attached to the base LLM and takes the last hidden states of the model as its input. It predicts the probability that the current token should be the final one. Crucially, the base LLM remains frozen during this process; only the GenGuard module is trained, making it extremely efficient to implement and deploy.

3. Line-Voting Mechanism

To mitigate the risk of premature stopping—which would result in truncated, non-functional code—CodeFast introduces a Line-Voting Mechanism. Rather than terminating inference based on a single token's prediction, the framework collects GenGuard’s predictions for an entire line of code. A majority vote is conducted at the end of the line to decide whether to stop. This ensures that the model only terminates when there is a consistent signal that the task is complete.

Evaluation and Performance

The authors evaluated CodeFast across several prominent benchmarks, including MBPP (Python), MBJSP (JavaScript), MBGP (Go), MBCPP (C++), and HumanEval. The framework was also tested on ClassEval to assess its performance in complex, multi-method scenarios.

Key Findings:

  • Significant Acceleration: CodeFast delivered massive inference speedups. CodeLlama-7B, for instance, saw a 4.52x speedup on the MBPP dataset. Across various models and languages, speedups typically ranged from 34% to over 400%.
  • Preservation of Quality: A critical requirement for any stopping mechanism is that it must not break the code. Across all evaluations, the Pass@1 scores (measuring functional correctness) remained virtually identical to the baseline models. CodeFast successfully filters "garbage" tokens while preserving the "gold" code.
  • Robust Generalization: GenGuard demonstrated impressive cross-language and cross-dataset performance. A module trained on Python, JavaScript, and Go performed effectively on C++ and generalized well to the HumanEval dataset without additional tuning.
  • Stability: Unlike standard LLM inference, which slows down as the max_new_tokens parameter increases, CodeFast’s performance remains stable, as it is driven by the content of the generation rather than arbitrary limits.

Conclusion and Future Work

CodeFast demonstrates that the signal for when to stop is already present within the hidden states of LLMs. By using a tiny, specialized probe like GenGuard, developers can significantly reduce the latency and cost of code generation.

While the current study focuses on function-level and class-level generation, future research will explore more complex environments, such as repository-level generation where stopping points are less clearly defined. Additionally, integrating CodeFast into popular inference engines like vLLM or HuggingFace TGI represents a vital next step for broad industry adoption.

Summary updated

BibTeX
@inproceedings{guo2024when,
  author = {Lianghong Guo and Yanlin Wang and Ensheng Shi and Wanjun Zhong and Hongyu Zhang and Jiachi Chen and Ruikai Zhang and Yuchi Ma and Zibin Zheng},
  title = {{When to Stop? Towards Efficient Code Generation in LLMs with Excess Token Prevention}},
  booktitle = {ISSTA},
  year = {2024},
}
Figure