Mid-Training: Annealing, Domain Bridges, and Context Extension
Broad pre-training ends when its token and compute budget ends. That stopping point does not guarantee that the resulting model is the best starting point for code assistance, mathematical reasoning, a specialist domain, or long documents. Post-training can adapt behavior, but asking a relatively small post-training dataset to repair a large distribution mismatch at the same time makes the transfer harder to diagnose.
mid-training is a name for an intermediate capability-building phase. It usually retains a pretraining-like objective while changing the data mixture, learning-rate schedule, or trained sequence length before final behavior-shaping stages. The practice is older than the name. Domain-adaptive pretraining, for example, was studied explicitly by Gururangan et al. in 2020 (Gururangan et al. 2020). Liu, Neubig, and Xiong proposed a narrower working definition of midtraining in 2025 and tested late mixtures of general and specialist data (Liu et al. 2025). No universal taxonomy has settled yet, so a useful account must describe what a phase actually changes rather than rely on its label.
Name the phase by its role
A training run is an ordered sequence of phases, not an unordered set:
Here, is the complete run; is phase ; is its training objective; is its data distribution; is its learning-rate schedule; is its token budget; is its maximum trained sequence length; and is the number of phases. Writing these fields down prevents a vague phrase such as “trained for longer” from hiding the actual intervention.
The phases are best separated by purpose and supervision:
| Phase | Typical data and objective | Intended result |
|---|---|---|
| Broad pre-training | large heterogeneous corpus; self-supervised token prediction | general base capability |
| Mid-training | selected or reweighted corpus, sometimes longer sequences; usually the same self-supervised objective | a base checkpoint closer to a later target |
| Post-training | demonstrations, preferences, rewards, policies, and safety data | a behavioral interface |
The word typical matters. Mid-training often updates every parameter, but it need not. Next-token prediction is also not a unique boundary because supervised fine-tuning can express a token-level likelihood loss. A phase is classified by the combination of its place in the pipeline, its data and supervision, and the model property it is intended to change.
This also corrects a common false distinction. continued pretraining is a method: resume a pretrained model on more self-supervised data. A mid-training phase can be continued pretraining. In Liu et al.'s experiments, “continued pretraining” names the special comparison that switches to 100 percent specialist data, while “midtraining” keeps specialist and broad data mixed (Liu et al. 2025). Other papers use continued pretraining more broadly, so the mixture must be stated rather than inferred from the name.
Post-training then changes what is supervised. It may use supervised fine-tuning (SFT) on demonstrations, direct preference optimization (DPO) from chosen-versus-rejected preferences, reinforcement learning with verifiable rewards (RLVR) from automatically checked outcomes, or other policy and safety objectives. Those later phases can still teach capabilities. The narrower claim is that a well-chosen bridge can reduce the distribution shift they must absorb.
Quantify the distributional bridge
Let denote the broad pretraining distribution and a specialist distribution such as code, mathematical text, scientific papers, or long documents. At step , a mixture can be written as
Here, is the sampling distribution at step and is the probability of drawing a token or example from under the stated sampler. At , the phase continues on broad data. At , it makes a full specialist switch. Values in between replay broad data while increasing specialist exposure. They may reduce forgetting, but do not guarantee it.
The final mixture weight does not reveal the total specialist dose. If the phase contains equal-token steps, its average specialist share is
Here, is the fraction of phase tokens expected to come from ; is the number of equal-token training steps; and is the specialist share at step . A comparison of “early” and “late” introduction is confounded if their specialist tokens differ.
For illustration, suppose normalized progress is , specialist data starts at , and its share ramps linearly to at the end:
Here, is the introduction point, is the final specialist share, and is the whole-phase specialist-token fraction under a constant token rate. This linear ramp is an accounting example, not a claim that it is the best schedule. Liu et al. instead studied fixed mixture weights after several introduction points. Their timing and weight effects interacted in small code experiments, so “earlier is better” is not a universal rule (Liu et al. 2025).
The runnable calculation shows why introduction time and final share cannot be reported separately from total exposure.
def specialist_share(progress, start, final_share):
"""Linear mixture ramp over normalized training progress."""
if not 0 <= progress <= 1:
raise ValueError("progress must be between 0 and 1")
if not 0 <= start < 1 or not 0 <= final_share <= 1:
raise ValueError("invalid schedule")
if progress < start:
return 0.0
return final_share * (progress - start) / (1 - start)
start = 0.60
final_share = 0.30
total_tokens = 100_000_000_000
whole_run_share = 0.5 * final_share * (1 - start)
for progress in (0.0, 0.4, 0.6, 0.8, 1.0):
print(f"progress={progress:.1f}: specialist={specialist_share(progress, start, final_share):.1%}")
print(f"whole-run specialist share: {whole_run_share:.1%}")
print(f"specialist tokens: {total_tokens * whole_run_share / 1e9:.1f}B")
print(f"broad tokens: {total_tokens * (1 - whole_run_share) / 1e9:.1f}B")
Keep the data schedule separate from the learning rate
“Annealing” is used for two different choices: changing the data near the end of training and decaying the learning rate. They can happen together, but they are not the same intervention. A generic warmup-stable-decay (WSD) schedule is
Here, is the learning rate at step ; is the stable rate; ends warmup; starts decay; ends the branch; rises from 0 to 1; and falls from 1 to the chosen terminal ratio. Linear, cosine, and other choices for and produce different schedules.
MiniCPM used WSD so a checkpoint on the stable plateau could support continued training and multiple decay branches (Hu et al. 2024). That is an experimental convenience, not permission to splice schedules casually. A branch must record its source checkpoint, optimizer and scheduler state, new data manifest, learning-rate path, token count, and random seeds. Resetting optimizer state or raising the learning rate is another intervention and needs its own control.
Three uses of the phase
Quality annealing
Quality annealing shifts a late data curriculum toward sources selected for quality or downstream usefulness. In OLMo 2, the first stage consumes roughly 90 to 95 percent of training FLOPs. A final 5 to 10 percent switches to Dolmino Mix 1124 while the learning rate decays. The specialized mix includes filtered web data, decontaminated task data, academic and reference sources, Q&A, and synthetic mathematics (OLMo Team 2025). That result establishes one documented recipe, not a general law that cleaner data is always best late. The source weights, filters, duplicates, and benchmark-overlap checks remain part of the claim.
A “high-quality” bucket should therefore be decomposed into observable properties: source provenance, filter thresholds, document and language mix, deduplication policy, synthetic-data share, and contamination results. Otherwise the phase cannot be reproduced or explained.
Domain mid-training
Domain mid-training uses self-supervised specialist data before behavior adaptation. Qwen2.5-Coder reports 5.2 trillion tokens of file-level training, then roughly 300 billion tokens of repository-level long-context training. Its mixture combines code, text, and mathematics, and its objectives include both next-token prediction and fill-in-the-middle code completion (Hui et al. 2024). DeepSeekMath starts from the pre-decay DeepSeek-Coder-Base-v1.5 7B checkpoint and trains for 500 billion tokens. The reported mixture includes a roughly 120-billion-token filtered math corpus, AlgebraicStack, arXiv, GitHub code, and English and Chinese web text (Shao et al. 2024). These are useful lineage examples, but neither isolates the causal contribution of every mixture component.
Earlier domain-adaptive pretraining work showed that a second self-supervised phase can improve downstream tasks in biomedical, computer-science, news, and review domains (Gururangan et al. 2020). Modern mid-training adds scale, mixture replay, and a deliberate handoff to post-training. DoReMi and related proxy methods can propose domain weights, but proxy gains must still transfer to the target model and target evaluation (Xie et al. 2023).
Long-context mid-training
Long-context mid-training is this book's functional grouping for a phase that changes trained sequence length before final behavior adaptation. It was not part of Liu et al.'s midtraining experiments. A usable extension requires three coordinated changes: a positional method, data containing dependencies at the new lengths, and a training system that can afford longer attention.
For simple Position Interpolation, denotes the original trained length, the new length, and a position in the extended sequence. The position supplied to the model is (Chen et al. 2023)
Here, is the extension factor, is the token's position in the extended sequence, and is the interpolated position supplied to rotary position embedding (RoPE). This keeps positions inside the range seen during the original training. YaRN is not just the same linear map: it treats RoPE frequency bands differently and rescales attention logits, with empirical settings that should not be assumed universal (Peng et al. 2023).
Position handling alone does not teach the model to combine distant evidence. Qwen2.5-1M illustrates the full stack. Its reported pretraining curriculum progressed from 4K through 32K, 65,536, 131,072, and 262,144 tokens. The later one-million-token input claim combines that training with inference-time position and attention methods; its multi-stage SFT belongs to post-training, not to this phase (Yang et al. 2025). A configured input limit is therefore not the same as a trained or effective context length.
| Length | Meaning |
|---|---|
| Trained length | maximum sequence length present in gradient updates |
| Accepted length | maximum input the runtime permits |
| Effective length | longest input meeting a stated quality threshold on a stated task |
| Deployable length | longest input meeting memory and latency requirements in production |
The compute budget changes too. For dense causal attention over sequences of lengths , the number of query-key pairs is
Here, is the number of query heads, is the token length of sequence , and counts causal attention pairs. At a fixed token budget, replacing many short sequences with fewer long ones raises attention work roughly in proportion to typical sequence length. FlashAttention avoids materializing the full score matrix, but it does not remove this exact dense attention arithmetic. Sequence packing, context parallelism, and memory limits therefore connect this phase directly to Chapter 10.
Design it as a transfer experiment
A mid-training run needs a control matrix, not only a new training loss. At minimum, branch the same source checkpoint into:
- broad-data continuation for the same token budget;
- a full specialist switch;
- one or more mixed schedules; and
- when post-training compatibility is the goal, a direct-to-post-training branch.
Keep total tokens, batch semantics, evaluation checkpoints, and as much of the optimizer path as possible comparable. Report both the final mixture and cumulative specialist tokens. If a schedule changes several things at once, for example data, learning rate, and sequence length, add ablations or describe the result as a bundle rather than assigning credit to one knob.
Evaluation must remain fixed while the training distribution moves:
- Target transfer: held-out specialist loss and capability evaluations chosen before the run.
- General retention: broad, multilingual, and cross-domain held-outs, plus short-context checks for a length extension.
- Post-training compatibility: the same small SFT or preference recipe applied to comparable intermediate checkpoints. Liu et al.'s strongest claims concern outcomes after SFT at 70M and 160M parameters, not arbitrary frontier-scale models (Liu et al. 2025).
- Long-context use: a matrix of length, evidence depth, distractor count, and task type. Needle retrieval alone is insufficient; models can accept long inputs while failing to use middle evidence or combine multiple facts (Liu et al. 2024; Hsieh et al. 2024).
- Data integrity: re-run deduplication and decontamination on the assembled mixture, complete long documents, and generated instruction-like material.
Training loss on the new mixture cannot serve as the retention metric because the measurement distribution changed. A simple fixed-distribution measure is
Here, is loss on a frozen held-out sample from broad distribution ; is the source checkpoint; and is the checkpoint after the phase. Positive means broad held-out loss worsened. It does not by itself prove that every general capability declined, so capability evaluations remain necessary. Every reported loss also needs a named tokenization policy.
Record the handoff contract
The output is a new base checkpoint with lineage. Its handoff record should include:
- source checkpoint and architecture hash;
- optimizer, scheduler, and precision state;
- ordered data manifests, weights, filters, and sampling schedule;
- tokens seen by source, language, format, and sequence-length bucket;
- tokenizer version, packing masks, document boundaries, and position-ID policy;
- trained context length and every RoPE or attention parameter;
- contamination report and fixed evaluation results over time; and
- the post-training recipe versions used to compare transfer.
This record separates a reproducible phase from a model file whose provenance is “continued for a while.” It also lets a later team determine whether a regression entered through data, optimization, length extension, or post-training.
“Mid-training” is a useful working term, not a settled scientific boundary. Some teams call the same operation late-stage pretraining, annealing, domain adaptation, or continued pretraining. Context extension stretches the term further because it changes position and length as well as data. The durable distinction is operational: publish the source checkpoint, objective, mixture, schedule, token budget, trained length, and intended handoff. Readers can then compare phases even when their names differ.
The training and serving systems bound the curriculum. Longer sequences raise attention work and activation memory; specialist sources may be too small to feed every replica without repetition; a changed tokenizer or position scheme may break checkpoint compatibility. Chapter 10 determines what can be trained, while Chapter 31 determines which accepted context lengths are affordable to serve. A capability bridge that violates either constraint does not produce a deployable model.
Diagnose the layer before spending the run
Different symptoms point to different interventions:
| Symptom | Evidence to gather | Likely intervention |
|---|---|---|
| high held-out loss on specialist text | fixed specialist and broad losses | domain or mixture phase |
| adequate base likelihood but wrong response format | demonstrations and behavioral evaluation | post-training |
| long input accepted but evidence use collapses with depth | length-by-depth task matrix | long-data/position training, not a larger runtime limit alone |
| quality holds but long prompts exceed memory or latency | memory and prefill profiles | serving or attention-system change |
| specialist gain accompanies broad regression | fixed retention suite and cumulative mixture dose | lower specialist share, more replay, or a different checkpoint |
This diagnosis prevents mid-training from becoming a default answer to every model gap. It is justified when the base checkpoint's data or length distribution is the limiting factor and the expected gain survives a fixed retention and post-training comparison.
Part I ends at that handoff. Chapter 5 set the budget, Chapter 6 defined the sources, Chapter 7 fixed the symbol interface, Chapter 8 and Chapter 9 fixed the model body, and Chapter 10 made the run executable. Mid-training decides whether the resulting base remains broad or moves toward a declared target. Part III then changes the supervision used to shape its behavior.
Further reading
- Liu et al., “Midtraining Bridges Pretraining and Posttraining Distributions” (controlled bridge experiments for code, math, instruction, QA, and high-quality web mixtures), 2025. arXiv:2510.14865Proposes a working definition of midtraining and, in controlled 70M- and 160M-parameter experiments, finds that late general-plus-specialist mixtures can improve code and math transfer after SFT, with interacting effects from introduction time and mixture weight.
- Gururangan et al., “Don't Stop Pretraining: Adapt Language Models to Domains and Tasks” (domain-adaptive and task-adaptive pretraining before the modern mid-training term), 2020. arXiv:2004.10964Shows that additional pretraining on domain and task corpora improves downstream performance across biomedical, computer science, news, and review tasks.
- OLMo Team, “2 OLMo 2 Furious” (late-stage curriculum training with specialized Dolmino Mix 1124), 2025. arXiv:2501.00656OLMo 2 documents and releases model weights, data mixtures, training and evaluation code, recipes, logs, and intermediate checkpoints for studying and attempting to reproduce its development.
- Hu et al., “MiniCPM: Unveiling the Potential of Small Language Models with Scalable Training Strategies” (WSD schedule for continuous training and branchable decay), 2024. arXiv:2404.06395Presents small (1.2B/2.4B) models that rival 7B-13B LLMs, using model wind-tunnel scaling experiments and a Warmup-Stable-Decay learning-rate schedule that enables continuous training.
- Hui et al., “Qwen2.5-Coder Technical Report” (specialist code continuation built on Qwen2.5), 2024. arXiv:2409.12186Reports Qwen2.5-Coder, trained on 5.2T file-level tokens followed by roughly 300B repository-level long-context tokens, using code, text, and mathematics with next-token and fill-in-the-middle objectives.
- Shao et al., “DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models” (500B-token continuation with a 120B-token filtered math corpus, then SFT and GRPO), 2024. arXiv:2402.03300DeepSeekMath combines a curated 120B-token math corpus with GRPO, a PPO variant that removes the critic and normalizes rewards within sampled groups.
- Chen et al., “Extending Context Window of Large Language Models via Positional Interpolation” (RoPE context extension via interpolation rather than extrapolation), 2023. arXiv:2306.15595Extends RoPE-based LLM context windows by linearly down-scaling position indices, avoiding unstable extrapolation and preserving original-window quality after limited fine-tuning.
- Peng et al., “YaRN: Efficient Context Window Extension of Large Language Models” (efficient RoPE context extension), 2023. arXiv:2309.00071YaRN extends RoPE-based LLaMA context windows using far fewer tokens and training steps than previous approaches, demonstrating extrapolation beyond the fine-tuning length.
- Yang et al., “Qwen2.5-1M Technical Report” (progressive long-context pretraining to 262,144 tokens, combined with inference-time methods for one-million-token inputs), 2025. arXiv:2501.15383Reports Qwen2.5-1M, progressively pre-trained to 262,144-token sequences, then combined with post-training and inference-time position and attention methods to accept inputs up to one million tokens.
Comments
Log in to comment