Data mixtures, curricula, and synthetic data
Suppose you have done everything in the last two sections. You have 15 trillion clean, deduplicated tokens of English web text, a trillion tokens of code, a hundred billion of math, a few tens of billions of books and papers, five billion of Wikipedia, and text in a hundred other languages ranging from a trillion tokens to a few million. You plan to train on 15 trillion tokens. What fraction of each batch should come from each source?
This one decision shapes the model as much as almost any architectural choice. Too little code and the model cannot program, too much and its prose gets worse. Sample languages in proportion to their size and the model barely learns Swahili; sample them equally and it sees the same few million Swahili tokens a hundred times. This section is about how labs set those proportions, change them during training, decide how often data may repeat, and when it is safe to train on text that a model wrote.
Domain weights, tokens, and epochs
Call the sources domains , with tokens available in domain . A mixture is a vector of weights on the probability simplex, and , and each training token is drawn from domain with probability . With a total budget of tokens, domain contributes tokens, so it is seen for
In plain words, a domain's epoch count is how much of the budget it gets divided by how much of it exists. Upweighting a small domain does not create data, it repeats it.
GPT-3 is the classic worked example (Brown et al. 2020). It trained on 300 billion tokens with the mixture below. Common Crawl was 60 percent of the batches but only 0.44 epochs, so most of it was never seen, while Wikipedia was 3 percent of the batches and was seen 3.4 times.
| dataset | tokens available | weight | epochs in 300B tokens |
|---|---|---|---|
| Common Crawl (filtered) | 410B | 60% | 0.44 |
| WebText2 | 19B | 22% | 2.9 |
| Books1 | 12B | 8% | 1.9 |
| Books2 | 55B | 8% | 0.43 |
| Wikipedia | 3B | 3% | 3.4 |
The weights say what the GPT-3 team believed. Small, high-quality sources were worth repeating, and the huge web crawl was worth sampling sparsely. Modern mixtures express the same belief in a different vocabulary. LLaMA 3's final pretraining mix was roughly 50 percent general knowledge, 25 percent mathematics and reasoning, 17 percent code, and 8 percent multilingual text (Dubey et al. 2024), with the categories assigned by classifiers rather than by source.
Recomputing from the table gives 3.5 epochs for WebText2, 2.0 for Books1, and 3.0 for Wikipedia rather than the paper's 2.9, 1.9, and 3.4, because the published weights and token counts are rounded (the weights even add to 1.01). The picture is the same. Compared with sampling in proportion to size, the mixture moves mass from the web toward the curated sources, by a factor of about 6 for WebText2, 5 for Wikipedia, and 3 for Books1.
Temperature sampling across languages
The same question has a clean, much-studied answer for languages. Let be language 's natural share of the data. Temperature sampling draws from
sometimes written with a temperature , as . In plain words, raise every language's share to a power below one and renormalize, which shrinks the gaps between big and small languages.
The two ends are easy to read. With we sample in proportion to size, so every language gets the same number of epochs, , and small languages get almost no tokens. With every language gets the same share , so small languages get many epochs. In between, taking logs shows what the power does,
so a language 1,000 times smaller than English in the data is times smaller in the batches, about 8 times for . The ratio of epochs between them is , so the small language is repeated times more often per token. That is the trade-off in one formula. Lowering buys a small language more exposure at the price of repetition.
Multilingual models settled on low values. mBERT used , XLM used 0.5, and XLM-R and mT5 both used 0.3 (Conneau et al. 2020; Xue et al. 2021). XLM-R also named the trade-off it fights against, the curse of multilinguality. For a fixed model size, adding more languages improves low-resource languages at first, through transfer from related ones, and then starts to hurt every language, because capacity is shared.
With , Yoruba's 100 million tokens would be repeated 146 times in a trillion-token run, far past the point where repetition helps (we will see where that point is at the end of this section). That is what UniMax (Chung et al. 2023) fixes. It allocates the budget as uniformly as possible across languages, but caps every language at a maximum number of epochs and passes the excess on to the larger ones. Processing languages from smallest to largest, each gets the smaller of its uniform share of the remaining budget and its cap.
Temperature sampling of a language mixture
Write temperature_mixture(sizes, alpha, budget) returning (p, epochs), where sizes is an array of positive token counts, p[i] is proportional to q[i] ** alpha with q the natural shares, and epochs[i] = p[i] * budget / sizes[i]. It must be numerically safe for shares as small as 1e-12 and for alpha = 0. Also write alpha_for_share(sizes, i, target) that returns the alpha in for which language i gets sampling probability target, assuming language i is not the largest, so its share decreases as alpha grows (use bisection to a tolerance of 1e-10). Raise ValueError if the target is not reachable in .
UniMax, uniform allocation with an epoch cap
Implement unimax(sizes, budget, max_epochs) returning an array of tokens allocated to each language, in the original order. Visit languages from smallest to largest (ties in input order). Each visited language gets min(remaining_budget / languages_left, max_epochs * size), and the allocation is subtracted from the remaining budget. If the total budget exceeds max_epochs * sum(sizes), every language ends at its cap and the leftover budget is unused.
Learning the mixture with DoReMi
Hand-set weights encode intuition. Can we learn them instead? Training the full model once per candidate mixture is out of the question, so every practical method uses small proxy models. DoReMi (Xie et al. 2023) is the best-known, and its idea is worth knowing in detail because it shows how a minimax objective becomes a few lines of code.
DoReMi first trains a small reference model on some default mixture. It then trains a second small proxy model, while adjusting the domain weights to emphasize the domains where the proxy is furthest behind the reference. Formally, it solves the group distributionally robust problem
where is the loss on domain and is the simplex. In plain words, the proxy model tries to do well on every domain, and an adversary keeps pointing it at the domain where it has the most excess loss compared with the reference. Excess loss is the point. A domain that is intrinsically hard (random-looking text) has high loss for both models and no excess, so it does not soak up weight, while a domain that is learnable but not yet learned does.
The adversary's update is an exponentiated gradient step. At each step, compute the per-domain excess loss on the current batch, clipped at zero, , and set
with step size and smoothing in the paper. The weights DoReMi outputs are the average of over the whole proxy run, not the final value.
∂ Why the update is multiplicative, exponentiated gradient as mirror ascent on the simplex
The adversary wants to increase the linear objective while staying on the simplex and not jumping too far from its current weights. Measure "too far" with the KL divergence, the natural distance between distributions, and solve
Add a Lagrange multiplier for and set the derivative with respect to to zero,
The factor is the same for every , so it is just the normalizer, and . The weights stay positive automatically (the KL term forbids zeros), which is why no projection step is needed. This is mirror ascent with the negative-entropy mirror map, and averaging its iterates is what the theory of these minimax games guarantees to converge, which is why DoReMi reports the average.
The results were striking. Weights found with a 280M-parameter proxy were used to train an 8B model, 30 times larger, on The Pile's 22 domains. The 8B model improved its average one-shot accuracy on generative tasks by 6.5 points and reached the baseline's final accuracy in 75,000 steps instead of 200,000, 2.6 times faster. The learned weights also say something. The web crawl, Pile-CC, went from 11 percent of the default mixture to 61 percent, a vote for broad web text over the hand-curated domains.
One DoReMi weight update
Write doremi_update(alpha, proxy_loss, ref_loss, eta=1.0, c=1e-3) that returns the new domain weights, following the two equations above. All inputs are arrays of length m except the scalars eta and c. Then write doremi_average(alpha0, proxy_losses, ref_losses, eta=1.0, c=1e-3) that applies the update for each row of the (T, m) loss arrays, starting from alpha0, and returns the average of the T updated weight vectors.
DoReMi is one of several approaches, and none has become standard. Others fit a regression from mixture weights to validation loss across many tiny runs and pick the predicted best mixture, or fit data mixing laws that predict loss as a function of the weights. LLaMA 3's team ran scaling-law experiments on small models trained on candidate mixtures and extrapolated (Dubey et al. 2024). The honest summary is that mixtures at frontier labs are set by a combination of these proxy experiments and many careful ablations, and that the proxy's verdict does not always survive at full scale.
Curricula and annealing
Should the order of the data matter? Classic curriculum learning, easy examples first, has a weak record in language-model pretraining. The model sees each token about once, and the shuffled mixture works about as well as hand-designed orderings in most published comparisons. One kind of ordering has become universal, though, and it is about the end of training.
As the learning rate decays at the end of a run, the loss drops sharply, and the data the model sees during that drop has an outsized effect on the final model (§8.7 explains why, through the noise in the parameters). So labs anneal. During the last part of training, they switch to a mixture richer in their best data. LLaMA 3 annealed its learning rate to zero over the final 40 million tokens with high-quality sources upsampled, then averaged checkpoints from that phase (Dubey et al. 2024). OLMo 2 made it an explicit second stage, mid-training, on a separate mix called Dolmino of high-quality web text, math, and curated sources, for 50 billion tokens on the 7B model. They ran it three times with different data orders and averaged the three resulting models, which they call souping (Team OLMo 2024).
Annealing also became a tool for measuring data. To ask whether a new dataset helps, LLaMA 3's team took an 8B model halfway through training and annealed it to zero over 40 billion tokens with 30 percent of the mixture from the new dataset, then compared benchmarks. This is far cheaper than training a model from scratch per dataset. In one such test, annealing on high-quality math and code improved the 8B model's GSM8K score by 24.0 percent and MATH by 6.4 percent, but that test mixture included those benchmarks' own training sets, which the final run left out. On the 405B model, the gains from annealing were negligible. Bigger models seem to need less of this last-minute help.
Code and math
Code and math take a share of the mixture far larger than their share of the web, for two reasons. They are skills people pay for, and practitioners believe they improve reasoning in general. The second belief has some evidence behind it. Aryabumi et al. (2024) ablated code in pretraining and found that including it improved not only coding but also natural-language reasoning and world-knowledge tasks, an empirical finding at the scales they tested, up to 2.8B parameters.
Both need their own pipelines, because the filters of §8.2 are tuned for prose and destroy code and math (C4's curly-brace rule, Gopher's alphabetic-word rule). Code comes from repositories, deduplicated at the file level, filtered by license, by file statistics (line length, fraction of alphanumeric characters, autogenerated headers), and often by a quality classifier. Math is harder to find. The best results come from training a classifier to recognize math pages and iterating, recalling more math pages from the crawl with each round. DeepSeekMath built a 120-billion-token math corpus from Common Crawl this way, and math-specific text extraction must preserve LaTeX and MathML that general extractors throw away.
Synthetic data, and the risk of model collapse
The web is finite and its best parts are small. A strong model can write new training data, textbook-style explanations, exercises with solutions, rephrasings of web pages in a cleaner style. This works surprisingly well. The phi-1 model (Gunasekar et al. 2023) had 1.3B parameters and was trained on about 7 billion tokens, about 6 billion of code from The Stack and StackOverflow, filtered for educational value with a classifier trained on GPT-4's judgments, plus under a billion tokens of synthetic textbooks written by GPT-3.5. After about eight passes over that data, and a short fine-tuning on about 180 million tokens of synthetic exercises, it reached 50.6 percent on the HumanEval coding benchmark, ahead of many models more than ten times its size.
What is the risk? If every generation of models trains on text written by the previous generation, errors compound. Shumailov et al. (2024) named the result model collapse. The tails of the original distribution disappear first, then the distribution narrows toward a few modes. The simplest possible model shows why, and it is worth deriving because the mechanism is general.
Let generation 0 be a Gaussian, . Generation draws samples from generation and fits a Gaussian by maximum likelihood, so its variance is the sample variance . For Gaussian samples, follows a chi-square distribution with degrees of freedom, which has mean , so
In plain words, each generation's fit is slightly too narrow on average, because a finite sample rarely includes the extremes, and the shrinkage compounds. It is worse than the mean suggests. The logarithm of the variance is a sum of independent terms , each with mean about (since , where is the digamma function). So a typical run's variance decays like , twice as fast as the mean, which is held up by rare lucky runs. Meanwhile the mean does a random walk. Both effects show up in the cell.
The standard deviation collapses toward zero within a few hundred generations, at about the rate the drift predicts. A categorical distribution over a vocabulary collapses in an even more blunt way, which the widget below simulates. Once a rare token draws zero samples in some generation, its fitted probability is zero and it can never come back.
The good news is that collapse is a property of replacing data, not of using synthetic data. Gerstgrasser et al. (2024) showed that if each generation trains on all the real and synthetic data accumulated so far, the error stays bounded, and in a linear model they proved a bound independent of the number of generations. Real pipelines are closer to accumulation. They keep the human data, add a limited amount of synthetic data, filter it, and draw it from strong models rather than from the model being trained.
Model collapse of a categorical distribution
Write collapse(p0, n, generations, accumulate, seed=0) returning a list of length generations with the support size (number of categories with nonzero fitted probability) of the fitted distribution after each generation. Use rng = np.random.default_rng(seed). Generation 1 draws n samples from p0 with rng.choice(len(p0), size=n, p=p0) and fits the maximum-likelihood categorical (counts divided by total). Each later generation draws n samples from the previous fitted distribution. With accumulate=False, the fit uses only the new samples. With accumulate=True, it uses the counts of all samples drawn so far, in every generation.
Two cautions about synthetic data apply even without collapse. It inherits the generator's mistakes and blind spots, so it needs the same filtering and verification as web data (for code and math, executing tests or checking answers is the strongest filter). And it can carry benchmark contamination in disguise, if the generator memorized benchmark questions and paraphrases them into the training set, so the decontamination of §8.3 should run on synthetic data too. Where synthetic data shines most is not pretraining but post-training, where a small amount of targeted data shapes behavior (§14.2), and in self-improvement loops that are a field of their own (training on a model's own filtered outputs).
Epochs and repetition
If good data is scarce, how many times can you repeat it? The best evidence comes from Muennighoff et al. (2023), who trained hundreds of models on a fixed amount of unique data repeated for different numbers of epochs. Up to about 4 epochs, repeated data was almost as good as fresh data. Beyond that, each extra pass was worth less, and around 40 epochs more repetition added almost nothing. They summarized this as an effective data formula, in which unique tokens repeated extra times are worth fresh tokens, with a fitted constant , so infinite repetition is worth at most about 16 unique epochs. §9.4 derives what that formula means for compute-optimal training when data runs out, and implements it (Exercise 9.4c).
For a mixture, the rule of thumb is what matters. Compute every domain's epochs with from the start of this section, and keep the small, precious domains to a few epochs. That is why the web is trained on in under one epoch at most frontier labs, while small curated sources (Wikipedia, books, the best math) are repeated a few times, as GPT-3's mixture already did. Be careful with the distribution of repetition, too, not only its average. Hernandez et al. (2022) showed that repeating a small subset many times is far more harmful than repeating everything a few times (§8.3).
In PyTorch
In a real data loader, the mixture is a sampler over several datasets. Here is a minimal one with PyTorch's WeightedRandomSampler, plus DoReMi's weight update written with tensors, run on a toy problem with three domains of different difficulty.
Domain 2 has only 10 examples, so its 20 percent share means it is repeated about 100 times in 5,000 draws, the trap of the first section in miniature. In the DoReMi toy, domain 1 starts behind the reference and catches up, and its weight rises and stays high even after its excess loss has faded. That is the multiplicative update at work. Ignoring the smoothing, , so the weights remember the cumulative excess of each domain, and a domain loses weight only when others build up excess of their own. Domain 2 is hard for both models and never gains weight, which is the point of using excess loss.
Read more
- Xie et al. (2023). DoReMi, Optimizing Data Mixtures Speeds Up Language Model Pretraining. The minimax objective, the update rule, and the transfer from a 280M proxy to an 8B model.
- Muennighoff et al. (2023). Scaling Data-Constrained Language Models. How much repeated data is worth, with the effective-data formula.
- Chung et al. (2023). UniMax, Fairer and More Effective Language Sampling for Large-Scale Multilingual Pretraining. The epoch-capped alternative to temperature sampling, with a clear comparison table.
- Conneau et al. (2020). Unsupervised Cross-lingual Representation Learning at Scale. XLM-R, the sampling choice, and the curse of multilinguality.
- Shumailov et al. (2024). AI models collapse when trained on recursively generated data, with Gerstgrasser et al. (2024), Is Model Collapse Inevitable?. The problem and the condition under which it goes away.
- Gunasekar et al. (2023). Textbooks Are All You Need. What a small model can learn from filtered and synthetic textbook-quality data.
- Dubey et al. (2024). The Llama 3 Herd of Models. Section 3.1 describes the data mix, the annealing experiments, and the classifiers behind them.