fill-in-the-middle
In plain wordsA training trick that teaches a text-writing AI to fill a gap using what comes both before and after it — essential for coding assistants that insert code in the middle of a file.
A training transformation and prompt format that lets a left-to-right causal language modelcausal language model, n.: A language model that reads text only from left to right, predicting each word from the words before it. The models behind today's chatbots are of this kind. insert text between a given prefix and suffix. During pretrainingpretraining, n.: The first and usually the most expensive stage of building a modern AI model, in which it learns general patterns from enormous amounts of raw text, images or code before being specialized for particular uses., a fraction of documents is split into prefix, middle and suffix, rearranged so that the middle comes last, and marked with special tokensspecial token, n.: A reserved symbol in an AI model's vocabulary that is not ordinary text but a signal — 'start of message', 'end of answer', 'begin thinking' — used to structure what the model reads and writes. — for example in prefix–suffix–middle ('PSM') order — so the model learns, by ordinary next-token predictionnext-token prediction, n.: The simple task most AI chatbots were trained on: given some text, guess the next small piece of it. Repeating that guess, piece by piece, produces whole answers., to generate the middle conditioned on both sides. At inference an editor supplies the code before and after the cursor and the model completes the gap. Mohammad Bavarian and colleagues reported that training on a large share of transformed data did not harm ordinary left-to-right performance, and recommended FIM as a default. It is standard in code models used for editor autocompletion, such as StarCoder, Code Llama and DeepSeek-Coder.
The autocomplete sends 2,000 tokens of prefix and 500 of suffix in FIM format.
Origin
Mohammad Bavarian, Heewoo Jun, Nikolas Tezak, John Schulman, Christine McLeavey, Jerry Tworek and Mark Chen of OpenAI introduced the transformation in 'Efficient Training of Language Models to Fill in the Middle' (July 2022). Related infilling objectives appeared earlier in 2022 in Meta's InCoder and CM3 models.
Related
- causal language modelcausal language model, n.: A language model that reads text only from left to right, predicting each word from the words before it. The models behind today's chatbots are of this kind.
- code generationcode generation, n.: Having an AI write computer code from a description in plain language, from finishing a single line as you type to building whole programs.
- special tokenspecial token, n.: A reserved symbol in an AI model's vocabulary that is not ordinary text but a signal — 'start of message', 'end of answer', 'begin thinking' — used to structure what the model reads and writes.
- next-token predictionnext-token prediction, n.: The simple task most AI chatbots were trained on: given some text, guess the next small piece of it. Repeating that guess, piece by piece, produces whole answers.
- GitHub CopilotGitHub Copilot, prop. n.: An AI assistant from GitHub, built into programmers' code editors, that suggests code as they type, answers questions about code and can carry out coding tasks on its own.
- masked language modelmasked language model, n.: A model trained by hiding some words in a sentence and learning to guess them from the words on both sides, like a fill-in-the-blanks exercise. BERT is the famous example.
Sources
- Bavarian, M., Jun, H., Tezak, N., Schulman, J., McLeavey, C., Tworek, J. & Chen, M. (2022). Efficient Training of Language Models to Fill in the Middle. arXiv:2207.14255. arxiv.org/abs/2207.14255
Mentioned in
code generationcode generation, n.: Having an AI write computer code from a description in plain language, from finishing a single line as you type to building whole programs.