Pretraining
Pretraining is the first stage of training a language model, where the model learns next-token prediction on trillions of tokens of text (see Chapter 7). The training starts from random parameters. The result is a base model, a model that continues a text. For example, Llama 3 8B was pretrained on over 15 trillion tokens of public text1.
Fine-tuning (see Chapter 11) and reinforcement learning (see Chapter 12) start from a base model and use far less data. For example, DeepSeek-V3 took 2,788,000 GPU hours to train, where a GPU is a processor that does many multiplications at the same time, and one GPU hour is one GPU working for one hour. Only 5,000 of them came after the pretraining and its stage of long texts2. However, the reinforcement learning of a reasoning model can be as large as its pretraining (see Chapter 13)3.
9.1The loss of a text
The pretraining uses the loss of Section 1.3 at every position of every training text. Each position predicts the token after it. One pass through a text gives the loss at all of its positions, because causal attention hides the next token from each position (see Section 8.2).
As an example, suppose that the training data is the three rides of Chapter 1, written as the texts The ride of 1 km cost $5., The ride of 2 km cost $6. and The ride of 3 km cost $9.. Each text has 10 tokens in the tokenizer of Section 5.1, so it gives 9 predictions. The following table shows them for the third text, with the probabilities of the model that has the smallest loss on the three texts:
| token | next token | probability | loss |
|---|---|---|---|
The |
␣ride |
1 | 0 |
␣ride |
␣of |
1 | 0 |
␣of |
␣ |
1 | 0 |
␣ |
3 |
1/3 | 1.10 |
3 |
␣km |
1 | 0 |
␣km |
␣cost |
1 | 0 |
␣cost |
␣$ |
1 | 0 |
␣$ |
9 |
1 | 0 |
9 |
. |
1 | 0 |
The three texts are the same up to the distance, so the position of ␣ before the distance cannot know which distance comes. The best it can do is to give each distance the probability 1/3, and the loss there is .
The price is different. The position of ␣$ reads the distance (see Section 8.1), and each distance has one price.
The loss of the pretraining is the average of the losses at all positions of all training texts. In the example, each text has the loss 1.10 at one of its 9 positions, so the smallest average loss is . No model gets below it, because nothing before the distance tells which distance comes.
At the start of the training, the parameters are small random numbers (see Section 3.4). The logits are then near 0, and the softmax gives every token about the same probability. With the 200,000 tokens of the vocabulary of Section 5.1, each token gets about 1/200,000. The loss is then about 12.2 at every position.
Each step of the pretraining takes a batch of texts (see Section 2.4), calculates the loss at all of their positions and changes the parameters with AdamW. For example, Llama 3 405B read its 15.6 trillion tokens in about 1.15 million steps, with batches that grew from 4 million to 16 million tokens and a learning rate that rose in the first 8,000 steps and then fell (see Section 2.2)4.
9.2Training data
Pretraining data is mostly text from the web, together with code, books and scientific papers. The data mix is the share of each kind of text in the training data. For example, the tokens of Llama 3 are about 50% general knowledge and 25% math and reasoning. Code has 17%, and languages other than English have 8%5.
Text from the web is cleaned before the training. A copy of the web contains the same page many times, pages with little text and spam. Filters remove duplicate documents and lines (see Section 4.5 and Section 4.6) and pages in unwanted languages. They also remove pages that rules or a model rate as low quality, where the model works like the spam filter of Section 1.36.
For example, the filters of one dataset of 2023 removed nearly 90% of the documents of Common Crawl, a public copy of the web7. The cleaned data can still contain the questions and answers of public tests (see Section 4.5).
The data decides the probabilities that the model learns. As an example, suppose that the training data has the text of the 3 km ride 10 times, where eight of the texts have the price 9 and two have the price 10.
At the position of ␣$, the model gives 9 some probability and 10 the probability . The average loss at this position is , and the following table shows it for some values of :
| 0.5 | 0.7 | 0.8 | 0.9 | 1 | |
|---|---|---|---|---|---|
| average loss | 0.69 | 0.53 | 0.50 | 0.54 | ∞ |
The loss is smallest when , the share of the texts with 9. A model that always predicts 9 has an infinite loss, because it gives the probability 0 to the price of two texts (see Section 1.3).
It turns out that the same holds in general, for a model that can give each sequence of earlier tokens its own probabilities. The smallest loss gives each token its share among the training texts in which it follows the same tokens.
Thus, a text that appears more often gets a larger probability. A page that the web contains 100 times counts 100 times. For this reason, the data is deduplicated, and the people who train a model choose its data mix. With a fixed number of tokens, a larger share of code leaves fewer tokens for other text.
Filters that remove most of the data leave fewer new tokens. The pretraining reads most of its data once or a few times (see Section 4.6). Repeated data is almost as good as new data for up to 4 epochs. More repetitions help less and less8.
The pretraining also changes its data in stages. Long texts come in a late stage, for the reason of Section 8.5. For example, in the last stage of Llama 3 405B, the learning rate fell to 0, and the data mix gave more weight to sources of very high quality9.
9.3Training cost
We can calculate the cost of the pretraining from the size of the model and the size of the data. A training step does about 6 operations per parameter and token (see Section 3.3). Thus, pretraining a model of parameters on tokens takes about operations.
For example, Llama 3 405B has 405 billion parameters and read 15.6 trillion tokens. This gives operations, the number in its paper10.
The operations run on GPUs. One H100 SXM GPU, the kind that trained Llama 3, does at most about 990 trillion operations per second on numbers of 2 bytes11, and the training of Llama 3 405B used 38% to 43% of this12.
At 40%, one GPU would need about 3,000 years. The run used up to 16,000 GPUs, which need about 70 days.
The GPUs share the work of each step. Each GPU calculates the gradient on its part of the batch, and the GPUs exchange their gradients to get the average (see Section 2.4). The model is also split over the GPUs. Its 405 billion parameters take 810 GB at 2 bytes each, and one H100 has 80 GB of memory13.
Llama 3 405B was released as Llama 3.1 405B, and its model card, the page that describes the released model, gives 30.84 million GPU hours for its training14. Suppose that one GPU hour costs 2 dollars, as the report of DeepSeek-V3 assumes. Then the training costs about 62 million dollars.
In practice, a run on thousands of GPUs often stops because one GPU fails15. For this reason, the training saves its parameters and the averages of Adam at regular intervals. After a failure, it continues from the last saved copy and repeats the steps since that copy16.
9.4Scaling laws
A scaling law is a formula that predicts the loss of a model from its number of parameters and its number of training tokens 17. Its constants are fitted to the final losses of many training runs. For example, a fit to the runs of the Chinchilla paper of 2022 gives the following formula18: The constant 1.82 is the loss that the formula gives for unlimited parameters and tokens. It plays the role of the smallest average loss of Section 9.1. The second term becomes smaller when the model has more parameters. The third term becomes smaller when the model reads more tokens.
For example, for = 70 billion and = 1.4 trillion, the two terms are 0.081 and 0.075. The formula then gives 1.976.
Consider a problem where we are given the number of operations of a training, its budget, and our task is to choose and with so that the loss is smallest.
A larger model makes the second term smaller. However, it reads fewer tokens, because . The following picture shows the loss that the formula predicts for , the budget of a model of 2021 called Gopher19:
Each mark on the horizontal axis is 10 times the previous one. The loss is smallest at about 72 billion parameters and 1.33 trillion tokens, about 18 tokens per parameter. With 280 billion parameters, the size of Gopher, the budget gives only 343 billion tokens. The loss is then 1.995.
The Chinchilla paper found that the parameters and the tokens should grow in the same proportion, with about 20 tokens per parameter20. It tested this with Chinchilla, a model of 70 billion parameters trained on 1.4 trillion tokens with the budget of Gopher, which has 280 billion parameters and read 300 billion tokens. Chinchilla answered 67.6% of the questions of the public test MMLU correctly (see Section 4.4), and Gopher 60.0%21.
In practice, models are often trained on many more tokens per parameter. For example, Llama 3 8B read over 15 trillion tokens, about 1,900 per parameter. It was still improving at the end22.
For = 8 billion and = 15 trillion, the formula gives 2.02. The same operations give the smallest loss 1.97 with about 81 billion parameters.
However, a model with 10 times more parameters costs about 10 times more for each token that it writes, when it uses all of its parameters for each token. The reason for this is that a prediction does about 2 operations per parameter (see Section 3.3). The training is paid once. The cost of the written tokens grows with every request (see Chapter 10)23.
With a scaling law, we can choose and from small runs before the large run. For example, the fit for Llama 3 suggested 402 billion parameters and 16.55 trillion tokens for operations. The model has 405 billion parameters and read 15.6 trillion tokens24.
The final loss of a large run can be predicted from small runs too25.
Note that the constants depend on the data, the tokenizer and the model, so a fit holds only for its own setting. For example, the fit for Llama 3 gives about 41 tokens per parameter at its budget, not 20.
9.5Base models
A base model continues a text, because every training text continues after each of its positions. As an example, consider the prompt The ride of 1 km cost $5. The ride of 2 km cost $. It reads like the start of a list of rides. A base model can continue it with 6. and then with a third ride, because a list goes on after each item.
A base model can also write a text of its training data word for word, more often when the text appears many times in the data (see Section 4.6).
We can give a base model a task by starting a document that does the task. For example, in the above prompt, the first ride shows the task, and the model writes the price of the second. In the same way, a prompt that shows a task with a few examples makes a base model continue with the task (see Chapter 18)26.
The knowledge cutoff of a model is the date of the latest text in its training data. For example, the pretraining data of Llama 3.1 has a cutoff of December 202327. Its training data has no texts about later events. We can give such facts to the model only in the prompt (see Chapter 19).
The probabilities follow the data, not the truth. In the example of Section 9.2, the model gives the price 10 the probability 0.2. Sampling then writes it in about 2 of 10 runs (see Section 7.4), and the model cannot tell which price is right.
A wrong statement that is frequent on the web gets a high probability in the same way. Chapter 22 shows why a model states false facts with confidence.
Instruction tuning teaches a base model to answer a request instead of continuing it (see Section 11.2). Reinforcement learning from human feedback then teaches it which answers people prefer (see Section 12.2).
The model card of Llama 3 says that it “was pretrained on over 15 trillion tokens of data from publicly available sources”: Llama 3 model card.↩︎
The report of DeepSeek-V3, published in 2024, gives 2,664,000 H800 GPU hours for the pretraining, 119,000 for the stage of long texts and 5,000 for the training after it. It assumes a rental price of 2 dollars per GPU hour, which gives 5.576 million dollars in all: DeepSeek-V3 Technical Report.↩︎
xAI wrote in 2025 that it ran the reinforcement learning of Grok 4 “at pretraining scale”: Grok 4.↩︎
Section 3.4.1 of The Llama 3 Herd of Models gives batches of 4 million tokens at the start, 8 million after 252 million tokens and 16 million after 2.87 trillion tokens. These sizes give about 1.15 million steps for 15.6 trillion tokens, and the paper sets its cosine decay over 1,200,000 steps. The peak learning rate is , with a warmup of 8,000 steps and a cosine decay to .↩︎
Section 3.1.2 of the Llama 3 paper calls the last part “multilingual tokens”: The Llama 3 Herd of Models.↩︎
Section 3.1.1 of the Llama 3 paper describes these steps: de-duplication “at the URL, document, and line level”, heuristics against low-quality documents, quality classifiers, and the removal of domains with much personal information.↩︎
The dataset is RefinedWeb. A figure caption of its paper says that its stages “remove nearly 90% of the documents originally in CommonCrawl”. Its largest stage removes about 50% of the documents because they are not in English: The RefinedWeb Dataset for Falcon LLM.↩︎
The paper finds that “training with up to 4 epochs of repeated data yields negligible changes to loss compared to having unique data”: Scaling Data-Constrained Language Models, published in 2023.↩︎
Sections 3.4.2 and 3.4.3 of the Llama 3 paper, which calls the last stage annealing. Llama 3 405B read about 800 billion tokens of long texts and raised their length in six stages, and its last stage covered the final 40 million tokens: The Llama 3 Herd of Models.↩︎
Section 2 of the Llama 3 paper gives 405 billion parameters and 15.6 trillion tokens, and Section 1 gives FLOPs, i.e., floating-point operations.↩︎
The datasheet of the H100 gives 1,979 teraFLOPS for BF16 numbers on the SXM version and adds “Shown with sparsity. Specifications 1/2 lower without sparsity.”: NVIDIA H100 datasheet.↩︎
Section 3.3 of the Llama 3 paper says that the model “is trained on up to 16K H100 GPUs”, and it gives 38% to 43% as the share of the peak for its configurations.↩︎
Section 3.3.2 of the Llama 3 paper describes how the training splits the model and the batch over the GPUs.↩︎
The card gives 1.46 million GPU hours for 8B, 7.0 million for 70B and 30.84 million for 405B, on H100 GPUs with 80 GB: Llama 3.1 model card.↩︎
For example, the run of Llama 3 405B had 466 interruptions in a period of 54 days of its pretraining, and 419 of them were unexpected. Section 3.3.4 of the Llama 3 paper attributes about 78% of the unexpected interruptions to hardware.↩︎
Section 3.3.1 of the Llama 3 paper says that checkpointing saves each GPU’s model state “for recovery and debugging”, and that a higher checkpoint frequency reduces “the amount of lost work after a recovery”. The paper on OPT, published in 2022, says that after hardware failures “training was resumed from the last saved checkpoint”: OPT: Open Pre-trained Transformer Language Models. The PyTorch tutorial on saving models says to save the state of the optimizer too, “as this contains buffers and parameters that are updated as the model trains”: Saving and loading models.↩︎
The power law of the loss in the number of parameters, the number of tokens and the number of operations was published in 2020, with “some trends spanning more than seven orders of magnitude”: Scaling Laws for Neural Language Models.↩︎
The form of the formula is from Training Compute-Optimal Large Language Models, published in 2022, which fitted 1.69, 406.4, 410.7, 0.34 and 0.28. A replication of 2024 found these values inconsistent with the paper’s two other methods and fitted the data again, which gives the constants of the formula: Chinchilla Scaling: A replication attempt. The formula rounds the fitted constants 1.8172, 482.01, 2085.43, 0.3478 and 0.3658, and the losses of this section come from the rounded formula.↩︎
The Chinchilla paper gives this budget. It also counts the operations of attention and of the embeddings, so the budget is larger than .↩︎
The abstract of the Chinchilla paper says that “for every doubling of model size the number of training tokens should also be doubled”, and its table of projections gives 20.2 billion tokens for 1 billion parameters. The replication says that its fit “implies an optimal ratio of around 20 tokens per parameter”.↩︎
MMLU is a test of multiple-choice questions on 57 subjects. The scores are from the Chinchilla paper. The 300 billion tokens of Gopher are from Section 3.2 of Scaling Language Models: Methods, Analysis & Insights from Training Gopher.↩︎
Meta wrote that the best amount of training for an 8B model “corresponds to ~200B tokens”, and that its 8B and 70B models “continued to improve log-linearly after we trained them on up to 15T tokens”: Introducing Meta Llama 3.↩︎
A study of 2023 concludes that a team “expecting reasonably large inference demand (~1B requests) should train models smaller and longer than Chinchilla-optimal”: Beyond Chinchilla-Optimal: Accounting for Inference in Language Model Scaling Laws.↩︎
Section 3.2.1 of the Llama 3 paper: “Extrapolation of the resulting scaling law to 3.8×10^25 FLOPs suggests training a 402B parameter model on 16.55T tokens.” For these sizes, 6ND gives about , 5% more than the budget of the fit.↩︎
For example, the final loss of GPT-4 was predicted from runs with at most 1/10,000 of its operations: Section 3.1 of GPT-4 Technical Report, published in 2023.↩︎
The paper on GPT-3, published in 2020, calls this few-shot learning, where “the model is given a few demonstrations of the task at inference time as conditioning, but no weight updates are allowed”: Language Models are Few-Shot Learners.↩︎
Llama 3.1 model card. The model card of Llama 3 gives March 2023 for 8B and December 2023 for 70B, and Section 3.1 of the Llama 3 paper says that the data contains “knowledge until the end of 2023”.↩︎