Horizon-Length Prediction: Advancing Fill-in-the-Middle Capabilities for Code Generation with Lookahead Planning

Yifeng Ding, Hantian Ding, Shiqi Wang, Qing Sun, Varun Kumar, Zijian Wang · DOI

Introduction

In modern software development, AI-powered code assistants are increasingly tasked with "infilling" or Fill-in-the-Middle (FIM) operations—generating code to bridge a gap between an existing prefix and suffix. While Large Language Models (LLMs) excel at generating code from scratch, standard training objectives often fail to produce coherent infilling results.

The primary culprit is the standard Next-Token Prediction (NTP) objective, which is inherently "myopic." By focusing solely on the immediate next token, models often lose sight of the broader context provided by the suffix. This leads to common failure modes where the generated code is locally plausible but syntactically or logically redundant with the code that follows the cursor. This paper introduces Horizon-Length Prediction (HLP), a lightweight auxiliary training objective designed to instill a sense of "lookahead planning" in code models.

The HLP Framework

The core innovation of HLP is to provide the model with a sense of the "generation horizon"—specifically, how much code remains to be written before reaching the suffix.

The HLP architecture adds a small auxiliary head to the transformer to predict the remaining distance to the suffix.

Methodology

  1. The Objective: During training, the model is tasked with predicting the percentage of the missing "middle" section that remains. If a middle section consists of $M$ tokens and the model is currently at token $t$, the target value is $y_t = (M-t)/M$.
  2. Architecture: A minimal "HLP head" (a single linear layer) is added on top of the transformer's hidden states. This head uses a sigmoid function to output a value between 0 and 1.
  3. Joint Training: The model is optimized using a combined loss function: $Loss = NTP\_Loss + \lambda \cdot HLP\_Loss$.
  4. Zero-Inference Overhead: Crucially, the HLP head is discarded after training. The model retains the planning intuition developed during pre-training, but the actual inference process remains standard autoregressive decoding, ensuring no additional latency for the end user.

Experimental Evaluation

The researchers evaluated HLP across several model families, including DeepSeek-Coder and StarCoder2 (ranging from 1.3B to 7B parameters), using a diverse set of benchmarks:

  • SAFIM: Tests syntax-aware structures like algorithms and API calls.
  • CrossCodeEval & RepoEval: Evaluates repository-level context, requiring the model to understand dependencies across multiple files.
  • Defects4J: Measures the ability to generate functional patches for real-world Java bugs.
  • CRUXEval: Assesses code reasoning and logic consistency.

Analysis and Insights

Probing experiments confirmed that standard NTP models' internal states contain almost no information regarding the remaining distance to the suffix. In contrast, HLP-trained models demonstrate a near-perfect correlation between their internal representations and the generation horizon.

HLP-trained models exhibit a significantly higher attention weight ratio toward the suffix early in the generation process.

Analysis of the model's attention mechanisms revealed that HLP encourages "Lookahead Attention." As shown in the figure above, HLP-enhanced models pay significantly more attention to the suffix tokens right from the start of the generation. By "looking at the destination," these models can better plan a path that integrates seamlessly with the existing code.

Furthermore, the paper highlights that many existing benchmarks use "post-processing" (manual truncation) to hide the flaws of standard FIM models. Without this artificial aid, standard models' performance drops by up to 13.8%. HLP effectively closes this gap by teaching the model to naturally conclude its generation at the correct point.

Key Results

  • Performance Gains: HLP improved FIM performance by up to 24% on repository-level benchmarks (Exact Match) without requiring manual post-processing.
  • Practical Utility: On the Defects4J bug-fixing benchmark, HLP-enhanced models successfully fixed 18% more bugs than the baseline.
  • Efficiency: The auxiliary head adds fewer than 0.01% additional parameters during training and zero overhead during inference.
  • Reasoning: Surprisingly, HLP also improved performance on CRUXEval, suggesting that global planning helps maintain logical consistency during complex reasoning tasks.

Conclusion and Future Work

Horizon-Length Prediction offers a simple, effective, and cost-free method for improving the coherence of code infilling. While the current study focused on models up to 7B parameters and the domain of code, the underlying principle of "horizon-blindness" likely affects other structured generation tasks. Future research may explore applying HLP to larger-scale models (70B+) and other domains such as mathematical reasoning or long-form document generation.

Summary updated

BibTeX
@inproceedings{ding2024horizon,
  author = {Yifeng Ding and Hantian Ding and Shiqi Wang and Qing Sun and Varun Kumar and Zijian Wang},
  title = {{Horizon-Length Prediction: Advancing Fill-in-the-Middle Capabilities for Code Generation with Lookahead Planning}},
  booktitle = {Sys2-Reasoning @ NeurIPS},
  year = {2024},
  url = {https://openreview.net/forum?id=L0agONQi9X},
}
Figure