InCoder: A Generative Model for Code Infilling and Synthesis

Daniel Fried, Armen Aghajanyan, Jessy Lin, Sida Wang, Eric Wallace, Freda Shi, Ruiqi Zhong, Scott Yih, Luke Zettlemoyer, Mike Lewis

Overview

InCoder is a 6.7B parameter generative model designed to handle both code synthesis (writing from scratch) and code infilling (editing existing code). By utilizing a "causal masking" objective during training, it learns to use bidirectional context to perform tasks like type inference, variable renaming, and docstring generation in a zero-shot manner, without sacrificing standard left-to-right generation performance.

The Challenge of Code Editing

Most large language models (LLMs) for code, such as the original Codex or GPT-3, are strictly "causal"—they generate text from left to right. While this is highly effective for writing a new function from a prompt, it is poorly suited for the way developers actually work: repeatedly editing, refactoring, and fixing existing code.

If you want a model to rename a variable or infer a return type, it needs to know not just what came before the cursor, but also what comes after it. Traditional masked models like BERT provide this bidirectional context but struggle to generate long, coherent sequences. InCoder addresses this gap by creating a unified architecture that excels at both synthesis and arbitrary infilling.

The Causal Masking Approach

The core innovation in InCoder is the Causal Masking training objective.

  1. Permutation: During training, the model takes a document and replaces random spans of code with a sentinel token (e.g., <Mask:0>).
  2. Relocation: The original text from those spans is moved to the end of the document, preceded by the same sentinel and followed by an <EOM> (End of Mask) token.
  3. Autoregression: The model is trained to predict the entire permuted sequence.

This simple trick allows a standard decoder-only Transformer to "see" the code that follows a gap before it has to fill that gap. At inference time, a developer can place a mask token anywhere in their code, and the model will generate the missing piece conditioned on the entire surrounding file.

Key Insights: The Power of Bidirectionality

  • Bidirectionality is Essential for Editing: For tasks like type prediction, knowing how a variable is used later in the function is often more informative than the function signature itself. InCoder’s ability to "look ahead" leads to massive performance gains in these scenarios.
Infilling performance vs. right context
  • The "No-Free-Lunch" Myth: A common fear is that training a model for infilling will degrade its ability to do standard left-to-right synthesis. InCoder’s ablation studies show this isn't the case; the causal masking model performs comparably to standard language models on synthesis tasks.
  • StackOverflow as a Knowledge Bridge: Including StackOverflow data significantly improved the model's ability to understand natural language intent, which is crucial for tasks like docstring generation and interactive code refinement.

Key Results and Performance

InCoder was evaluated on a variety of benchmarks, including a modified version of HumanEval adapted for single-line and multi-line infilling, CodeXGLUE, and TypeWriter OSS.

Infilling benchmark results
  • Zero-Shot Superiority: On the HumanEval single-line infilling task, InCoder achieved a 69.0% pass rate, significantly outperforming standard left-to-right baselines (48.2%) and even the much larger code-davinci-001 (63.0%).
  • Type Inference: InCoder showed a dramatic lead in return type prediction, hitting 58.1% accuracy on CodeXGLUE, compared to just 12.0% for traditional left-to-right models.
  • Competitive with Fine-Tuning: In docstring generation, InCoder’s zero-shot performance (18.27 BLEU) approached the performance of models specifically fine-tuned on 250,000 examples (like CodeBERT at 19.06 BLEU).
  • Scaling: Perplexity and performance consistently improved with model size (from 1.3B to 6.7B parameters), suggesting that even larger versions would yield better results.

Limitations and Future Work

  • Context Window: Like most Transformers of its era, InCoder is limited by a 2048-token context window, which can be restrictive for very large codebases.
  • Training Saturation: The authors noted that the model's perplexity had not yet saturated, meaning more training time and data would likely continue to boost performance.
  • Instruction Tuning: While InCoder is powerful in a zero-shot setting, the authors suggest that future work could involve fine-tuning the model on specific human instructions to make it even more helpful as an interactive coding assistant.

Summary updated

BibTeX
@inproceedings{fried2023incoder,
  author = {Daniel Fried and Armen Aghajanyan and Jessy Lin and Sida Wang and Eric Wallace and Freda Shi and Ruiqi Zhong and Scott Yih and Luke Zettlemoyer and Mike Lewis},
  title = {{InCoder: A Generative Model for Code Infilling and Synthesis}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=hQwb-lbM6EL},
}
Figure