Bidirectional Context for Autoregressive Text Infilling

This direction is merged into another direction: unidirectional context limitation (unidirectional context constraint). Its last version stays readable and citable here.

Version 1 of 118 papers18 conceptsfrozen 2026-10-04

Standard large language models (LLMs) are "suffix-blind." Because they are trained to predict the next token in a strict left-to-right sequence, they excel at continuing a prompt but struggle to fill in a gap within an existing sentence. If you provide a prefix and a suffix, a standard model cannot "see" the suffix while generating the middle, leading to incoherence where the generated text fails to connect naturally with what follows.

Researchers have developed a suite of techniques to bridge this gap, transforming unidirectional decoders into flexible "infilling" engines that can account for context on both sides of a blank.

The Unidirectional Constraint

The core issue lies in the causal attention mask used by models like GPT. As noted by Mark Chen and colleagues in Efficient Training of Language Models to Fill in the Middle, this architecture prevents tokens from attending to any information that appears later in the sequence. While this is perfect for open-ended generation, it is suboptimal for tasks like code completion or document editing, where the "future" text provides critical constraints.

To solve this, the research direction has shifted toward architectures and training objectives that allow a model to treat the suffix as part of its "known" context, even while generating the middle.

Unified Pre-training and Masking

One of the earliest breakthroughs came from Li Dong and the team behind UNILM. Instead of choosing between a bidirectional encoder (like BERT) or a unidirectional decoder (like GPT), they used a shared Transformer architecture with varying self-attention masks. By training on a mix of objectives—bidirectional, unidirectional, and sequence-to-sequence—a single model learns to encode context holistically while retaining the ability to generate text one token at a time.

This idea was extended by Bo Peng in the Probabilistically Masked Language Model (PMLM). Rather than always predicting the very next word, PMLM uses a random, uniform masking ratio. This forces the model to learn how to generate text in an arbitrary order. By learning to predict any missing word given all other words in a sentence, the model naturally gains the ability to perform "cloze-style" generation, filling in blanks regardless of where they appear.

The Fill-in-the-Middle (FIM) Transformation

While changing architecture is one path, Chris Donahue and Percy Liang demonstrated in Enabling Language Models to Fill in the Blanks that you can "teach" standard autoregressive models to infill simply by rearranging the data.

The intuition is simple: if you want a model to predict a "Middle" segment given a "Prefix" and a "Suffix," you can format your training data as <PRE> Prefix <SUF> Suffix <MID> Middle. To the model, this looks like standard left-to-right generation. However, because the Suffix now appears before the Middle in the input stream, the causal attention mechanism allows the model to attend to the suffix while generating the middle.

This "FIM" approach has become the industry standard for code-generation models, as it requires no architectural changes to the Transformer block itself.

Any-Order Generation and Diffusion

More recent work has pushed the boundaries of how we sample these missing tokens. Tianxiao Shen introduced FiLM (Fill-in Language Models), which uses a training strategy inspired by diffusion models. By sampling mask probabilities from a Beta distribution, FiLM learns to fill in gaps of varying sizes and complexities.

Similarly, Wenjie Ou and colleagues developed PMCTG, which uses perturbed masking to identify which words in a sentence conflict with their surrounding context. This allows for "surgical" editing—the model can identify a word that doesn't fit and replace it with one that satisfies both the preceding and succeeding constraints.

Beyond Generation: Retrieval and Understanding

The benefits of bidirectional context extend beyond just writing text. In the field of information retrieval, Zhichao Xu and the creators of LACONIC argue that the unidirectional nature of standard LLMs is a liability for document ranking. Retrieval requires a holistic understanding of a document's meaning. By adapting causal backbones (like Llama-3) to handle bidirectional context through specialized training curricula, they achieved state-of-the-art sparse retrieval performance, proving that "looking ahead" is as vital for understanding as it is for generation.

Papers in this version

History & citationVersion 1 · 2026-10-04

Merged into another direction · Permanent link

View frozen version 1

Cite this direction

Latest version (a living page):

@misc{lacuna_bidirectional_context_for_autoregressive_9d6df56f,
  title = {Bidirectional Context for Autoregressive Text Infilling},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/bidirectional-context-for-autoregressive-text-infilling/rdr_9d6df56ff758423a887df7f8aad0768b},
  note = {Latest version of a living research direction; cite a version for a fixed text. Accessed 2026-11-26},
}

This exact version (version 1, frozen text):

@misc{lacuna_bidirectional_context_for_autoregressive_9d6df56f_v1,
  title = {Bidirectional Context for Autoregressive Text Infilling},
  author = {{Lacuna}},
  howpublished = {Lacuna research direction},
  year = {2026},
  url = {https://lacuna.tiptreesystems.com/direction/bidirectional-context-for-autoregressive-text-infilling/rdr_9d6df56ff758423a887df7f8aad0768b/v/1},
  note = {Version 1, frozen 2026-10-04. Accessed 2026-11-26},
}