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

Self-Infilling Code Generation

Lin Zheng, Jianbo Yuan, Zhi Zhang, Hongxia Yang, Lingpeng Kong

Introduction

Most contemporary large language models (LLMs) generate code in a strictly left-to-right (L2R) fashion. While effective, this approach is brittle: if a model encounters a high-uncertainty section, it might produce an error that propagates through the rest of the file—a phenomenon known as exposure bias.

This paper introduces self-infilling, a novel decoding framework that allows code language models to generate programs non-monotonically. By interrupting standard L2R generation to "jump ahead" and draft a suffix first, models can anchor their output and then fill in the difficult middle sections. This significantly improves code quality and reduces repetitive errors.

The Missed Opportunity of FIM

Many modern code models, such as Code Llama and StarCoder, are trained with "Fill-In-the-Middle" (FIM) objectives to complete partial code. However, this capability is rarely used during the initial synthesis of a full program. The authors identify that these models can actually perform "self-infilling"—autonomously generating both the surrounding context and the missing content, rather than just filling a gap provided by a human prompt.

The Self-Infilling Framework

The authors propose a general framework that evolves standard auto-regressive decoding into a non-monotonic process through two key mechanisms:

  1. Self-Infilling Interruption: During standard L2R generation, if the model's confidence (token probability) drops below a certain threshold $\tau$, it triggers an interruption. The model stops, generates a suffix (often guided by a "suffix prompt" like a return statement), and then pivots back to infill the bypassed middle section.
  2. Looping Mechanism: To further refine the output, the framework introduces a cycle that alternates between self-infilling and L2R decoding. This allows each part of the code (prefix, middle, suffix) to be updated and synchronized based on the increasingly informative bidirectional context.
Schematic of decoding approaches: standard L2R, standard infilling, self-infilling interruption, and the iterative looping mechanism.

Key Insights

  • Suffixes as Structural Anchors: Generating the end of a function first (an "easy-first" strategy) provides a definitive goalpost. This scaffolds the generation of the "hard" middle part, ensuring it remains logically consistent with the intended return value.
  • Mitigating Degeneration: Vanilla L2R decoding often falls into "degeneration traps," producing empty programs or infinite loops of repetitive comments. Self-infilling effectively breaks these cycles by forcing the model to commit to a functional ending, drastically reducing the frequency of degenerate solutions.
  • Latent FIM Capabilities: The paper reveals that models trained on FIM objectives inherently "know" how to generate suffixes from prefixes alone, even though they weren't explicitly trained to do so without a provided suffix.

Performance and Results

The framework was evaluated across a diverse set of benchmarks, including HumanEval, MBPP, DS-1000, MultiPL-E, and GSM8K. The self-infilling framework consistently outperformed vanilla L2R baselines across models and tasks.

  • Accuracy Boost: On HumanEval, Code Llama 13B saw its pass@1 rate jump from 35.4% to 40.8% when using two looping iterations ($N=2$).
  • Scaling with Iteration: Performance generally improves as the number of looping iterations increases, demonstrating the value of the cyclic refinement process.
  • Multilingual Success: The benefits were not limited to Python; significant gains were observed in Java, C++, and PHP, proving the framework's generalizability.
Pass@1 rates on HumanEval and MBPP demonstrate that performance generally improves with more looping iterations (N) and higher interruption thresholds (τ).

Limitations and Future Work

While highly effective, the framework has a few limitations. The primary drawback is computational overhead; repeatedly re-processing and re-decoding snippets increases latency and compute requirements. Optimizing KV-caching is a promising future direction to mitigate this.

Additionally, the current formulation is constrained to infilling a single "middle" span, whereas real-world coding often involves jumping between many different locations. Finally, the authors note an instruction tuning gap: gains were less pronounced for "Instruct" versions of models, likely because FIM objectives are often omitted during instruction fine-tuning. Future instruction-tuned models should retain FIM training to fully leverage self-infilling.

Summary updated

BibTeX
@inproceedings{zheng2024self,
  author = {Lin Zheng and Jianbo Yuan and Zhi Zhang and Hongxia Yang and Lingpeng Kong},
  title = {{Self-Infilling Code Generation}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=bV9yT24t9B},
}
Figure