Advanced Pretraining Objectives

You will see why "predict the next token" is a starting point rather than the whole story, and learn the handful of data transformations and extra losses that give a base model skills next-token prediction alone does not teach: filling in code, predicting further ahead, reading 128k tokens, absorbing a new domain, and looking at pictures.

Next-token prediction is a wonderful objective. It is simple, it uses every token as a training signal, and at scale it produces models that can write essays and code. But it has blind spots. A model trained only left to right has never once been asked to write the middle of a function given both ends. It has never been asked to think two tokens ahead. It has never seen a document longer than its training window, or an image. This chapter is about the extra signals that fill those gaps.

None of these replaces next-token prediction. Each one is a cheap modification of the data or a small extra loss that rides alongside it. The theme throughout: the base objective is fixed, so the leverage is in what you feed it and what you ask it to output.

Fill-in-the-middle: teaching a left-to-right model to infill

Open your code editor. Your cursor is in the middle of a function; there is code above and code below. You want the model to write what goes at the cursor. A pure next-token model cannot do this well: it has only ever seen "text so far, then next token", and the code below the cursor is context it has no way to condition on.

You could train a bidirectional model, but then you lose the generation ability that makes decoders useful. Fill-in-the-middle (FIM), from Bavarian et al. (2022) at OpenAI, is the trick that gets both: keep the causal decoder exactly as it is, and rearrange the training documents so that "the middle" comes last.

The PSM transformation

Take a document, pick a random span to be the middle, and split it into prefix, middle, suffix. Then reorder it with three sentinel tokens:

$$\langle\text{PRE}\rangle\ \text{prefix}\ \langle\text{SUF}\rangle\ \text{suffix}\ \langle\text{MID}\rangle\ \text{middle}\ \langle\text{EOT}\rangle$$

That is it. The model trains with ordinary next-token loss on this rearranged sequence. When it reaches the MID sentinel, everything it has seen is prefix and suffix, and the tokens it must predict are the middle. So a plain causal model learns to infill, with no architectural change. "PSM" names the order: prefix, suffix, middle.

Worked example

Document: def add(a, b):\n return a + b. Choose the middle to be return a + b. Then prefix = def add(a, b):\n , suffix = empty (end of file), middle = return a + b.

PSM training sequence: <PRE> def add(a, b):\n <SUF> <MID> return a + b <EOT>.

At inference in the editor, you build the same prompt with the real prefix and suffix and let the model generate after <MID>, stopping at <EOT>. Because the model learned to emit EOT when the middle is complete, it also learns to stop at exactly the right place to join up with the suffix.

document prefix middle suffix rearrange PSM training sequence PRE prefix SUF suffix MID middle EOT predicted with both sides in view
Figure 1. The PSM rearrangement. The model is still trained left to right on the bottom row; the trick is that by the time it reaches the middle, it has already read the suffix. Only the yellow block and the final EOT are "infilling" predictions.

The SPM variant

The alternative order puts the suffix first: suffix, then prefix, then middle. Bavarian et al. use a particular arrangement of the sentinels for it:

$$\langle\text{PRE}\rangle\ \langle\text{SUF}\rangle\ \text{suffix}\ \langle\text{MID}\rangle\ \text{prefix}\ \text{middle}\ \langle\text{EOT}\rangle$$

Why bother with a second order? Because in SPM the prefix and the middle are adjacent, which matters twice. At the token level, a tokenizer boundary artifact between prefix and middle is less likely to hurt. At the serving level, when a user keeps typing in the editor, the prefix grows and the suffix stays fixed; with SPM the growing part is at the end of the prompt, so a KV cache (see the inference chapter) of the suffix can be reused. Bavarian et al. report training on a 50/50 mix of PSM and SPM.

InteractiveFIM transformeredit the text, drag the span sliders

Pick a middle span with the two sliders and watch the document get rearranged. The sentinels are colored; the model only ever predicts what comes after MID.

Why it matters, and the "FIM-for-free" result

The obvious worry: if half your documents are scrambled, does the model get worse at ordinary left-to-right text? Bavarian et al. tested exactly this and found what they call the FIM-for-free property: applying the transformation to 50% of documents leaves left-to-right perplexity and downstream performance essentially unchanged, while adding a strong infilling ability. The intuition is that a rearranged document still contains all the same text, so the model still learns the same statistics; it just also learns the sentinel grammar.

Two details that matter in practice. Document-level FIM (split the document before packing into training sequences) is cleaner than context-level FIM (split after packing) because the sentinels then always describe one coherent document. And the span should be chosen at the character level, then tokenized, rather than at the token level; otherwise the model never sees a middle that starts halfway through a token, which is exactly what a cursor does.

Every serious code model since has adopted FIM: InCoder (Fried et al. 2022) with a related "causal masking" objective, StarCoder, Code Llama, and DeepSeek-Coder all train with it, typically at a 50% rate. If you are training anything that will live in an editor, this is not optional.

Common confusion

FIM is not masked language modeling. BERT-style MLM predicts masked tokens with a bidirectional encoder and cannot generate. FIM keeps the causal decoder and the ordinary next-token loss; the "bidirectionality" is faked by moving the suffix before the middle in the input. The model is still strictly left to right over the rearranged sequence.

Multi-token prediction: think a few tokens ahead

Here is a different blind spot. Next-token loss trains the model to be very good at the immediate next token and rewards it not at all for having a plan for the token after that. Yet a good continuation of "The capital of France is" requires already knowing that "Paris" will be followed by "." rather than ", which". Could we ask the model to predict several future tokens at once, and would that be a better training signal?

The idea and the extra heads

Multi-token prediction (MTP), studied at scale by Gloeckle et al. (2024), attaches $k$ output heads to the shared trunk of the transformer. Head 1 predicts token $t+1$ from position $t$, head 2 predicts $t+2$, and so on to $t+k$. The loss is the sum of the $k$ cross-entropies:

$$\mathcal{L}_{\text{MTP}} = -\sum_{t}\sum_{i=1}^{k}\log p_{\theta}^{(i)}\!\left(x_{t+i}\mid x_{\le t}\right)$$

Each head is a small transformer layer plus the shared unembedding; the trunk is the ordinary model. At inference you can throw the extra heads away and decode with head 1 exactly as usual, so MTP is purely a training-time change to what the model is asked to know.

Worked example

Sequence: "the cat sat on the mat". At position $t = 2$ (after "the cat"), with $k = 3$: head 1 must predict "sat", head 2 must predict "on", head 3 must predict "the". All three see only "the cat". Head 3's task is hard and its loss is high, and that is the point: to lower it the trunk must encode something like "a verb phrase about location is coming", which is a richer representation than "next word is probably a verb".

What it buys you

Gloeckle et al. report that at 13B parameters with $k = 4$, MTP improved code benchmarks noticeably (they report about 12% more HumanEval problems solved and 17% more MBPP problems relative to next-token baselines), while small models could be hurt, and gains on natural-language multiple-choice benchmarks were mixed. The benefit grows with model size, which suggests the extra heads act as a regularizer that only helps once the model has capacity to spare. They also found the trained models were better at induction-style and algorithmic reasoning tasks.

DeepSeek-V3 (2024) used a variant: rather than parallel independent heads, a single sequential MTP module that predicts token $t+2$ conditioned on the trunk output and the embedding of token $t+1$, keeping the causal chain intact. They report it as a training objective that improved their benchmark scores, with the module discarded or reused for speculative decoding at inference.

The speculative-decoding connection

The extra heads have a second life. Speculative decoding (inference chapter) speeds up generation by having a cheap draft model guess several tokens which the real model then verifies in one parallel pass. With MTP, the draft model is free: head 2 through head $k$ produce the guesses. Gloeckle et al. report up to about 3× faster inference with 4 heads on code, and DeepSeek-V3 reports acceptance rates of roughly 85 to 90% for its second-token prediction, translating to about 1.8× tokens per second.

InteractiveMulti-token prediction headsstep k from 1 to 4, then try the acceptance slider

Each head reads the same trunk state at position t and predicts a different future token. The speedup readout is an illustrative model of self-speculative decoding, not a measurement.

Span corruption and UL2, briefly

Before decoders won, the dominant pretraining objective for text was span corruption, from T5 (Raffel et al. 2019). Mask out random spans of the input, replace each with a sentinel, and train an encoder-decoder to emit the sentinels followed by the missing spans. In tiny form: input "The sat on mat", target " cat the". It is FIM's ancestor, with several middles at once.

UL2 (Tay et al. 2022) argued that no single corruption is best and mixed three "denoisers", each announced by a mode token at the start of the input. The R-denoiser is regular T5 corruption: short spans, about 15% of tokens. The S-denoiser is sequential: corrupt the whole tail, which is just prefix language modeling. The X-denoiser is extreme: very long spans or a high corruption rate, so the model must generate a lot from a little. Training on the mixture with mode switching gave a single model that did well on both understanding and generation tasks. The idea that a decoder can benefit from a mixture of denoising objectives lives on, but for large decoder-only models the community mostly settled on next-token plus FIM, and UL2 is now more a reference point than a recipe.

Long-context training: from 8k to 128k

The next blind spot is length. If the model trained on 8k-token windows, it has never seen position 20,000. Its rotary embeddings (see the positional-encoding chapter) have never rotated that far, and its attention has never had to pick one relevant sentence out of a hundred pages. Feed it a 100k-token document and quality collapses.

Why you cannot just train long from the start

Attention cost grows with the square of sequence length, and activation memory grows linearly with it (see the systems chapter). Training all 15 trillion tokens at 128k would cost many times more than at 8k and would need far more context-parallel communication. Worse, most documents are short, so most of that long context would be padding or unrelated concatenated documents. So the standard recipe trains short and extends late.

The staged approach

Grattafiori et al. (2024) describe Llama 3's long-context stage in Section 3.4.2. After the main pretraining at 8k, they increased the context in six stages, from 8k up to 128k, training on roughly 800 billion tokens in total for this phase. At each stage they waited for two signals before moving on: short-context benchmark performance had fully recovered, and the model solved needle-in-a-haystack retrieval perfectly at the new length. They also report choosing a large RoPE base frequency of 500,000 for the architecture, which spreads the rotary frequencies so that positions far apart remain distinguishable at long range.

Other levers used across the field: position interpolation (Chen et al. 2023), which rescales positions so that a 32k input looks like a stretched 4k one to the existing rotary frequencies; YaRN (Peng et al. 2023), a more careful frequency-dependent rescaling; and simply raising the RoPE base, which Code Llama did (to 1,000,000) before its long-context fine-tuning. All of them are cheap ways to make the model's positional machinery tolerate lengths it never saw, so that a small amount of long-context training finishes the job.

main pre-training8k context · ~15T tokens (reported) 16k 32k 128k anneal128k six increments, ~800B tokens total (reported); advance only when short-context evals recover and needle retrieval is perfect at the new length Cost control: the quadratic-attention, high-communication phase is confined to about 5% of the tokens.
Figure 2. Staged context extension as reported for Llama 3. Each box is a gate, not a schedule: the criterion for moving on is measured, not a token count.

The data you need

Long-context training needs genuinely long documents: books, long code repositories, long-form articles, multi-turn transcripts. Concatenating short documents to fill the window teaches nothing about long-range dependence, and worse, it teaches the model to attend across document boundaries that carry no information. Llama 3 reports using an attention mask that prevents tokens from attending across documents within a packed sequence, and notes it mattered mostly for the long-context stage. Upsampling long documents in the mix is standard; some recipes also synthesize long-range tasks (multi-document question answering, long summarization) for the extension phase.

Needle in a haystack

How do you know the extension worked? The simplest probe is the needle-in-a-haystack test (Kamradt 2023): hide one distinctive sentence ("the best thing to do in San Francisco is…") at a chosen depth in a long pile of unrelated text, then ask about it. Sweep context length and needle depth, and plot retrieval accuracy as a heatmap. A model with a real 128k context is green everywhere; a model with a nominal 128k context often shows a red band in the middle depths (the "lost in the middle" effect, Liu et al. 2023) and a cliff beyond its true effective length.

InteractiveNeedle-in-a-haystack heatmaptoggle before / after the long-context stage

Retrieval accuracy over context length (columns) and needle depth (rows). This is a synthetic illustration of the typical shape, not measured data from any model.

Needle retrieval is necessary but far from sufficient. A model can find one sentence and still fail to reason over a long document. Harder probes such as RULER (Hsieh et al. 2024) add multiple needles, variable tracking, and aggregation tasks, and show that many models' "effective" context is well short of their advertised one.

Continued pretraining and domain adaptation

Now suppose you already have a good general base model and you want it to be excellent at code, or mathematics, or Japanese. You could train a new model from scratch on a domain-heavy mix, but that throws away trillions of tokens of learning. Continued pretraining (CPT) instead resumes next-token training from the existing checkpoint on new, domain-focused data.

Examples that worked

Code Llama (Rozière et al. 2023) is the canonical case: start from Llama 2 and continue on about 500 billion tokens of mostly code, then specialize further for Python and for instruction following. Llemma (Azerbayev et al. 2023) continued Code Llama on a mathematics-heavy corpus. Multilingual extensions routinely continue a mostly-English model on a target-language mix. In each case the adapted model far outperforms the base on the target domain at a small fraction of the from-scratch cost.

Catastrophic forgetting, and replay

Here is the catch. Train only on code for long enough and the model gets worse at English. This is catastrophic forgetting: gradient steps on the new distribution overwrite what was learned on the old one. Two things make it worse: a high learning rate (the model moves far from its starting point) and a large distribution shift (the new data is very different from the old).

The standard mitigation is replay: mix a fraction of the original pretraining distribution back into the CPT data. Ibrahim et al. (2024) studied this systematically and report that a small replay fraction, a few percent for a mild shift and more for a strong one, combined with re-warming the learning rate and then decaying it again, recovers most of the from-scratch quality on the union of both distributions. The learning-rate part matters: resuming at the tiny end-of-schedule learning rate learns the new domain too slowly, while jumping straight to the peak rate causes a loss spike and heavy forgetting, so warm up again from a low value (Gupta et al. 2023).

Worked example

You want to adapt a 7B English model to legal text with 50B new tokens. A reasonable first recipe: 90% legal, 10% replay from the original mix; warm the learning rate from near zero up to about half the original peak over the first 1–2% of steps, then cosine decay; evaluate both a legal benchmark and a general benchmark every few thousand steps. If the general score drops more than a point or two, raise the replay fraction toward 25%.

Common confusion

Continued pretraining is not fine-tuning. Fine-tuning (the SFT chapter) uses a small curated dataset and a task-shaped loss to change behaviour. CPT uses the same next-token loss as pretraining on a large raw corpus to change knowledge. The scale is hundreds of billions of tokens, not thousands of examples, and the model afterwards is still a base model.

Curriculum and annealing, revisited

Everything above changes what the model sees. There is also leverage in when. Curriculum, in the loose sense used in LLM pretraining, means ordering the data: broad web text for most of training, then a shift toward higher-quality and more targeted data at the end, while the learning rate anneals to zero.

The last few percent of training at a decaying learning rate is disproportionately influential, because the weights settle into their final basin during it. Llama 3 reports (Section 3.1.3 and 3.4.3) that annealing the learning rate on a small amount of high-quality code and mathematical data at the very end improved the 8B model markedly on GSM8K and MATH, while the 405B model saw negligible gain, suggesting that the largest models already have that knowledge and the trick mostly helps smaller ones. The warmup-stable-decay schedule from MiniCPM (Hu et al. 2024) makes this explicit: hold the learning rate constant for most of training, then decay sharply on the best data. The data engineering chapter covers the mixing and quality-filtering side.

Model merging, warm-starting, and depth up-scaling

Three related ways to avoid starting from scratch, each in a sentence or two. Model merging averages the weights of several models trained from a shared starting point; "model soups" (Wortsman et al. 2022) showed that averaging fine-tuned checkpoints can beat any single one, and Llama 3 reports averaging models from different post-training runs. Warm-starting initializes a new model from an old one, either directly (continued pretraining is warm-starting on new data) or by growing the architecture. Depth up-scaling is a specific growth recipe: SOLAR 10.7B (Kim et al. 2023) took a 32-layer Mistral 7B, duplicated it, dropped the last 8 layers of one copy and the first 8 of the other, stacked them into a 48-layer model, and continued pretraining. The duplicated model starts much closer to a good solution than random initialization does, and the authors report strong results for the compute spent. The general lesson: weights are expensive and reusable.

Multimodal pretraining

A text model has never seen an image. Making it see is, surprisingly, mostly a data-and-adapter problem rather than a new-architecture problem, and the recipe now looks similar across labs.

Vision encoders and adapters

Take a pretrained vision encoder, usually a Vision Transformer trained contrastively like CLIP (Radford et al. 2021), which turns an image into a grid of patch embeddings. Add an adapter that maps those embeddings into the language model's token space, then feed them into the LLM as if they were text tokens. LLaVA (Liu et al. 2023) used a single linear projection (later an MLP) and trained it in two stages: first only the projection on image-caption pairs, then the projection plus the LLM on instruction-style visual conversations. That is enough to get a model that can describe and answer questions about images.

Flamingo (Alayrac et al. 2022) took the other route: keep the LLM frozen and insert gated cross-attention layers that let text tokens attend to the visual features. Llama 3 (Section 7) follows the cross-attention pattern, reporting that they trained image adapters on top of the finished text model so that adding vision could not degrade text quality. The choice between "inject as tokens" and "cross-attend" trades simplicity against protecting the text model.

imagepixels vision encoderViT, pretrained, patches adapterMLP / resampler texttokens language modelimage tokens interleavedwith text tokens (LLaVA)or via cross-attention (Flamingo) Training data: captions, then interleaved image–text documents, then visual instructions.
Figure 3. The standard vision-language recipe. Note what is new: only the adapter (and sometimes cross-attention layers). The vision encoder and language model are both reused, which is why the whole thing can be trained on far less data than either was.

Interleaved data

Caption pairs teach "this image is a dog". Interleaved documents, web pages with images placed among the paragraphs that discuss them, teach the harder skill of using an image in context and of handling several images in one document. Flamingo made interleaved data central; most modern recipes mix captions, interleaved documents, OCR-heavy documents, charts, and synthetic visual question answering, staged from easy to hard.

Audio in a paragraph

Speech follows the same template with a different encoder. A pretrained speech encoder (Whisper-style, Radford et al. 2022, or a conformer trained on unlabeled audio) converts a waveform into frame embeddings; an adapter compresses and projects them into the LLM's token space; the LLM then reads them as tokens. Llama 3 (Section 8) reports a speech adapter trained this way with the text model kept fixed, so that a single text model can accept typed or spoken input. Generating audio is a separate problem usually handled by a decoder on the output side.

Reasoning-oriented pretraining data

A last lever, and one where claims run ahead of evidence, so let us be careful. Models trained on more code and mathematics tend to be better at multi-step reasoning, even on tasks that are neither code nor math. The Llama 3 authors report a mix of roughly a quarter mathematical and reasoning data and 17% code, up from earlier generations, and attribute part of their reasoning gains to it. DeepSeekMath (Shao et al. 2024) showed that a large, carefully filtered corpus of web mathematics (they report about 120 billion tokens) continued into a code model produced a strong math model at 7B.

Synthetic data goes further. The phi series (Gunasekar et al. 2023, "Textbooks Are All You Need") trained small models on LLM-generated textbook-style explanations and exercises and reported strong coding results for the size. Synthetic chain-of-thought, where a stronger model writes step-by-step solutions that go into the pretraining mix, is now common, and it clearly helps on benchmarks whose format it matches.

The balanced view. In favour: reasoning data is dense in "structure per token" and models do transfer some of it. Against: synthetic data narrows the distribution, can leak benchmark-like content and inflate scores (see the evaluation chapter on contamination), and training repeatedly on model outputs can degrade diversity (the "model collapse" concern of Shumailov et al. 2024). The practical recipe most labs converge on is a natural-data majority with a minority of high-quality synthetic reasoning data, concentrated in the annealing phase where it has the most effect.

Symbols
$x_{1..T}$ = training sequence
$k$ = number of MTP heads
$\langle\text{PRE}\rangle,\langle\text{SUF}\rangle,\langle\text{MID}\rangle$ = FIM sentinels
$\rho$ = replay fraction in CPT
STEP 1
Transform the document
With probability 0.5 apply FIM: split into prefix, middle, suffix at character positions; emit PSM or SPM order with sentinels and EOT.
STEP 2
Compute the losses
Next-token cross-entropy over the (possibly rearranged) sequence. If MTP, add the losses of heads 2..$k$ on tokens $t+2..t+k$.
STEP 3
Stage the training
Main run at short context on the broad mix; extend context in stages on long documents; anneal on high-quality data. For a domain, continue with replay $\rho$ and a re-warmed learning rate.
STEP 4
Bolt on modalities
Freeze or protect the text model; train an adapter from a pretrained encoder on captions, then interleaved data.

Practice

Exercise 1 — add FIM to the data pipeline

Extend code/lumen/data.py with a fim_transform(doc, rate=0.5, mode="psm") function operating on strings: pick two random character positions, split, and return the rearranged string with sentinel strings. Add the three sentinels to the tokenizer in code/lumen/tokenizer.py as special tokens. Train the tiny GPT from code/lumen/gpt2.py with and without FIM on the toy corpus and compare left-to-right validation loss (should be nearly equal) and a hand-made infilling prompt (should only work with FIM).

Solution sketch

Draw i, j = sorted(random.sample(range(len(doc)+1), 2)). PSM: f"<PRE>{doc[:i]}<SUF>{doc[j:]}<MID>{doc[i:j]}<EOT>". Register the sentinels as atomic tokens so BPE never splits them. Apply the transform per document before packing. On the toy corpus the losses will be within noise of each other, which is the FIM-for-free property in miniature.

Exercise 2 — a two-head MTP model

Modify code/lumen/gpt2.py so the final hidden state feeds two heads: the existing one for $t+1$ and a new small block plus shared unembedding for $t+2$. Train with the summed loss. Then implement greedy self-speculative decoding: draft token $t+2$ from head 2, verify with a normal forward pass, and measure the acceptance rate on held-out text.

Solution sketch

Shift the targets by two for the second head (y2 = x[:, 2:]) and drop the last two positions. Tie the unembedding. For verification: run the trunk on the sequence plus the drafted token, and accept it if head 1's argmax at the previous position equals the draft. Expect acceptance well below the 85–90% DeepSeek reports; a tiny model on a tiny corpus is not confident two steps ahead, and that is informative.

Exercise 3 — forgetting and replay

Take a model trained on the toy corpus, then continue training it on a different toy corpus (for example, only arithmetic strings) with replay fractions of 0%, 5%, and 25%. Track validation loss on both corpora and plot them against steps.

Solution sketch

With 0% replay the original-corpus loss climbs steadily; with 5% it flattens; with 25% it barely moves while the new-corpus loss falls almost as fast. Try also resuming at the peak learning rate versus re-warming from a tenth of it: the spike in the first few hundred steps is the loss-spike-and-forgetting effect described by Gupta et al.

Check yourself
In PSM fill-in-the-middle training, which tokens does the model predict using information from the suffix?
The model is still causal. Because the suffix is placed before the MID sentinel, only the tokens after MID (the middle and EOT) are predicted with the suffix in context. The loss is computed everywhere, but only those predictions are infilling.
Why can multi-token-prediction heads be used for speculative decoding with no extra draft model?
The extra heads are a built-in draft model: their predictions are proposed, then a single verification forward pass accepts the longest correct prefix.
Llama 3 reports extending context from 8k to 128k in six stages. What criterion did they report using before advancing to the next stage?
Section 3.4.2 of the Llama 3 paper describes advancing once short-context performance had recovered and needle retrieval was perfect, over roughly 800B tokens total.
You continue pretraining a general model on 300B tokens of code and its English benchmarks drop sharply. The most direct fix is…
Replay counteracts catastrophic forgetting by keeping the old distribution in the gradient; a gentle re-warm avoids the early loss spike that drives much of the forgetting.

Key takeaways

  • Fill-in-the-middle is a data rearrangement (prefix, suffix, middle with sentinels) that teaches a causal decoder to infill at essentially no cost to left-to-right quality; every serious code model uses it.
  • Multi-token prediction adds heads for tokens t+2..t+k; it helps larger models (notably on code) and doubles as a free draft model for speculative decoding.
  • Long context is trained late and in stages, on genuinely long documents, with rotary-frequency adjustments and needle-in-a-haystack as the gate; needle retrieval is necessary, not sufficient.
  • Continued pretraining adapts a base model to a domain cheaply, but needs replay and a re-warmed learning rate to avoid catastrophic forgetting.
  • The annealing phase on high-quality data is where curriculum has the most leverage, especially for smaller models.
  • Vision and audio are bolted on with a pretrained encoder plus an adapter, trained on captions then interleaved data; reasoning-heavy and synthetic data help, in moderation, and are best concentrated late.

Further reading