Fill-in-the-middle (FIM)
Also known as: FIM, infilling, code infilling
A training trick that teaches a model to write the missing piece between the text before a gap and the text after it.
Draft - this entry has not been reviewed yet.
Formal
During pretraining, a document is cut into a start, a middle and an end, and the pieces are reordered with special marker tokens as start, end, then middle; ordinary next-token prediction on that order teaches the model to produce a middle that fits both sides.
In plain English
Like fitting the last piece of a jigsaw - the pieces on every side of the hole show its shape far better than looking at one side alone.
In practice
At a Danish university, a developer clicks into the middle of a half-written function in the exam booking app; the editor sends the lines above and below, and the model returns only the few lines that belong in the gap.
Why it matters
Real editing rarely happens at the end of a file, so without it a model would guess blindly about everything below the spot being edited and suggest lines that clash with the rest.
Technical deep dive
The standard reference is Bavarian et al. (OpenAI, 2022), "Efficient Training of Language Models to Fill in the Middle". A training document is split at two uniformly random character positions into prefix, middle and suffix, then serialised with sentinel tokens. In PSM order the sequence is <PRE> prefix <SUF> suffix <MID> middle <EOT>; in SPM order the suffix comes first, so prefix and middle are contiguous, which plays better with key-value caching when the user keeps typing. The transformed sequence is trained with the ordinary causal next-token loss, so no architectural change is needed; at inference the client supplies everything up to <MID> and the model generates the middle until it emits the end-of-text token.
The paper's central empirical claim is "FIM for free": applying the transformation to a large share of pretraining documents (they tested FIM rates up to 90%) did not measurably hurt left-to-right performance, while it gave strong infilling ability. Several design details mattered. Character-level span selection generalised better than splitting on line or token boundaries, because real cursors sit mid-token. Applying FIM at the context level, after documents are packed into training sequences, beat applying it per document. And adding FIM by fine-tuning an existing model was markedly less compute-efficient than including it during pretraining. The authors recommend a FIM rate between 50% and 90% with joint PSM and SPM training, and alongside InCoder's single-line and multi-line infilling benchmarks they introduced a random-span infilling benchmark built on HumanEval.
Other lines of work converged on the same idea. InCoder (Fried et al., 2022) used causal masking, moving masked spans to the end of the sequence, and code models such as StarCoder expose FIM through dedicated tokens like <fim_prefix>, <fim_suffix> and <fim_middle>. Because sentinel names and ordering differ between model families, a completion client must format prompts exactly as the model was trained, or quality collapses silently.
Known failure modes are practical rather than theoretical. The model may regenerate text that already exists in the suffix, fail to emit the end token and run on, or produce a middle that is syntactically valid but ignores constraints stated far away in the suffix. Tokenisation at the cursor is a subtle source of error: if the prefix ends in the middle of a word, the model sees an unusual token boundary, which is why some clients back up to a token boundary before sending and re-append the characters afterwards. FIM should not be confused with masked language modelling as in BERT, which predicts isolated masked tokens bidirectionally rather than generating a variable-length span autoregressively.
What to learn first
Everything this builds on, foundations first.
- Inference
- →Token
- →Next-token prediction
- →Fill-in-the-middle (FIM)
Relationships
- Part of
- Pretraining
- Requires
- Next-token predictionToken
- Unlocks
- Code completion
- Used with
- Tokenizer
Sources & further reading
Reference works
- Bavarian et al. (2022), Efficient Training of Language Models to Fill in the Middle
- Fried et al. (2022), InCoder - A Generative Model for Code Infilling and Synthesis
Where this data comes from
This entry was drafted by an AI from the sources above and has not yet been checked by a person. Treat it as a starting point, and check anything important against the sources.
See the review queueSuggest a correction on GitHubThis term as JSON
Check yourself
Loading…