Models
The text before training
Collecting, cleaning, de-duplicating and tokenizing trillions of tokens
A training run reads text, and what it reads decides most of what the model becomes. Qwen3, the family of this site's model, was pre-trained on about 36 trillion tokens in 119 languages. Nobody types that much. It is gathered from the web and other sources, including text read out of PDF documents and text generated by earlier models. A pipeline of this kind strips web pages of markup, filters them for safety and personal data, clears out duplicates, scores what is left for quality, mixes the sources in chosen proportions, cuts the result into tokens and writes it into files that thousands of GPUs can read in parallel. This lesson follows that pipeline from a raw web page to a window of 4,096 token numbers. The Qwen3 report gives its totals, sources and stages but does not describe its web cleaning step by step, so the cleaning steps shown here are the ones the Llama 3 report describes. The lessons after it train on the result.
Chapter 1 of 9
Where the text comes from
Most of it is the public web, and most of the web is unusable
The largest single source for every recent model is the public web: pages fetched by crawlers and stored as raw HTML. The Llama 3 report says that much of its data is obtained from the web and describes the cleaning that follows. To that are added sources that are scarce on the open web or need their own handling, such as code, mathematics and text in languages other than English. The Qwen3 report gives the total for this site's model family: about 36 trillion tokens covering 119 languages and dialects, up from 29 languages in Qwen2.5.
Two of Qwen3's sources are not web pages at all. A version of Qwen2.5-VL, a model that reads images, was fine-tuned to recognise the text in a large volume of PDF-like documents, and the recognised text was then refined by Qwen2.5. Qwen2.5-Math and Qwen2.5-Coder generated synthetic text for mathematics and for code. The report puts the result of this at trillions of additional tokens. So earlier models of the same family supply part of the text the next one learns from.
Raw pages are a poor training text. A typical page is mostly navigation menus, cookie warnings, adverts and footers, with the article itself a small part. The same page exists at many addresses and in many near copies. Some pages are harmful, and some hold personal data such as names, addresses and phone numbers. Every one of these problems costs something if it reaches training. Boilerplate wastes compute, because the model spends steps predicting menus. Duplicates make the model memorise and repeat. Personal data can be reproduced later word for word.
So the pipeline before training is mostly subtraction. Each stage removes a share of what arrives, and the stages are ordered so that the cheap ones run first on the most data, and the expensive ones, such as a neural quality classifier, run last on what is left. In the scene the outside sources come in through one controlled entry, are stored, and are processed on the same kind of GPU servers that later run training.
Chapter 2 of 9
From HTML to plain text
Keep the article, drop the page around it
The first real stage pulls text out of HTML. The Llama 3 team built its own parser for this, tuned for precision in removing boilerplate and for recall of the actual content, and compared it with popular third-party parsers in human evaluations. The aim is to keep the paragraphs, headings, lists and tables of the article and to drop everything that is repeated across a site.
Some content needs special care. Mathematics on web pages is often a picture of a formula, with the formula's text in the image's alt attribute, so the Llama 3 parser keeps the alt text. Pages with code or mathematics are processed so that their structure survives, because a code block whose line breaks and indentation are lost is no longer valid code.
One finding in the report goes against intuition. The team tried keeping markdown markers, such as # for a heading and ** for bold, and found that they hurt a model trained mostly on web data, compared with plain text. So all markdown markers are removed. The report says only that it evaluated different cleaning configurations experimentally, and it does not describe how. One common way to run such an experiment is to train a small model on each version of the text and keep the version whose model scores better. The report does describe small-model experiments of that kind for choosing the data mix, which a later chapter covers.
For the runbook page in the scene, 84 KB of HTML becomes about 6 KB of plain text: a title, four paragraphs, a numbered list of commands and a short table. Everything else on the page is gone.
Chapter 3 of 9
Safety and personal data
Whole domains are dropped before a word is read
The cheapest filter works on the address of a page. The Llama 3 pipeline removes data from websites likely to contain unsafe content or high volumes of personally identifiable information, PII, from domains ranked harmful under Meta's safety standards, and from domains known to contain adult content. A domain on one of these lists costs nothing to drop, because no page from it needs to be parsed.
Lists miss things, so the next filter reads the text. One heuristic the report names is dirty word counting: a page is scored by how many words from a list of offensive terms it contains, and pages above a threshold are dropped. It catches adult sites that the domain lists do not cover.
Personal data matters for a reason that is easy to miss. A model can reproduce text it has seen often or seen in an unusual form, sometimes word for word. A phone number that appears in the training text may come back in an answer months later, to someone who never asked for it. Removing such data before training is the only reliable fix. Methods exist that try to make a trained model forget a fact, called machine unlearning, and filters can screen the model's answers, but neither is reliable, and no switch cleanly removes one fact from 32.8 billion trained weights.
The same filters are applied to the multilingual data, each with its own language's lists. In the scene this is a pass over the stored text on the processing servers. It drops a share of the corpus, and what survives moves on to de-duplication.
Chapter 4 of 9
Removing duplicates three ways
By address, by near-identical document, and by repeated line
The web repeats itself. The same article is syndicated to many sites, the same page is fetched in many crawls, and the same sentence of boilerplate appears on millions of pages. A model that sees a passage many times spends compute on it many times and becomes more likely to reproduce it exactly. The Llama 3 pipeline removes duplicates at three levels.
At the URL level it keeps the most recent version of each address across the whole dataset. That is a simple lookup.
At the document level it uses MinHash to find near duplicates across the whole dataset. Each document is cut into shingles, short overlapping runs of words. Two documents are near duplicates when their sets of shingles overlap heavily, measured as the size of the overlap divided by the size of the union, called the Jaccard similarity. Comparing every pair of documents is impossible at this scale, so MinHash computes a short signature for each document: for each of many hash functions it keeps the smallest hash of any shingle. The chance that two documents share the smallest hash under one function equals their Jaccard similarity, so the share of matching entries in two signatures estimates it. Documents whose signatures agree closely are grouped, and one is kept.
At the line level it removes lines that appear more than 6 times in each bucket of 30 million documents. That strips leftover boilerplate such as menus and cookie warnings. The report notes that it also removes some frequent high-quality text, and that its evaluations still showed strong improvements.
Chapter 5 of 9
Heuristics and quality classifiers
Cheap rules first, then small models that judge each page
After de-duplication the text is clean in form and still mixed in quality. The Llama 3 report lists heuristics for the next pass. A duplicated n-gram coverage ratio removes lines of repeated content such as log output or error messages, which can be very long and unique and so slip through line de-duplication. A token-distribution check compares the tokens of each document with the tokens of the whole corpus, measured as a Kullback-Leibler divergence, and removes documents with an excessive number of outlier tokens, such as pages of hashes or encoded data.
Then come model-based filters. A fast classifier, fasttext, is trained to recognise whether a page is the kind of text Wikipedia would cite. A heavier classifier, based on RoBERTa, is trained on judgements made by Llama 2's chat model: the team described the quality requirements, asked Llama 2 to say whether cleaned documents met them, and trained the classifier on those answers. For efficiency the scores are produced with DistilRoberta, a smaller model. Each document gets a quality score, and the configuration of thresholds is chosen by experiment.
Code and mathematics have their own pipelines. Their token distributions differ so much from prose that they need their own HTML extraction, features and heuristics, and their own classifiers trained to find pages with step-by-step reasoning in STEM subjects and code mixed with explanation. A model is already part of the pipeline that makes the next model.
Chapter 6 of 9
The tokenizer is built before the model
A vocabulary of pieces, fixed for the model's whole life
A model does not read characters. It reads numbers from a fixed vocabulary, and the tokenizer that turns text into those numbers is fixed before training begins. It is often reused from an earlier model. Qwen3 uses Qwen's tokenizer, the one built for the first Qwen models in 2023, and Llama 3 started from the 100K tokens of OpenAI's tiktoken tokenizer. Neither was built from its own pre-training text. Qwen3's tokenizer uses byte-level byte-pair encoding, BPE, with a vocabulary of 151,669 entries. Byte-level means that the starting pieces are the 256 possible bytes, so any text in any language or script can be encoded and nothing is ever unknown. Pair encoding means that the vocabulary is grown from a sample of text by repeatedly finding the most frequent pair of adjacent pieces and adding their merge as a new piece, until the vocabulary has the size wanted. Common words end up as one token, and rare ones as several.
Before any merging, the text is first split into rough words, a step called pre-tokenization, and merges never cross the edge of one of these pieces. Qwen's tokenizer.json does it with a regular expression that keeps a word together with the space in front of it, puts each digit on its own, and separates runs of punctuation and of whitespace. That is why " Pending" with its leading space is one piece, and why a number such as 4096 always becomes four tokens, one per digit.
The model's config gives a vocab_size of 151,936, a little above 151,669. The embedding table and the output head have one row per entry of that size, so the extra rows are room the model has and the tokenizer never uses.
The tokenizer decides how far a training budget goes. Llama 3 grew its vocabulary to 128K entries, 100K from OpenAI's tiktoken tokenizer and 28K more for languages other than English, and its English compression rose from 3.17 to 3.94 characters per token. At a fixed number of training tokens that is about 24 percent more English text read. The sentence "The pod is stuck in Pending." is 28 characters, and Qwen3's tokenizer turns it into 7 tokens, 4.0 characters each. Once training starts the vocabulary can't change without retraining the embedding table and the output head.
Chapter 7 of 9
The mix, and changing it on the way
How much of each kind of text, and when
Clean text from many sources still has to be combined, and the proportions matter as much as the cleaning. Web data over-represents some subjects. The Llama 3 team built a classifier that sorts web pages by the kind of knowledge they hold, and used it to downsample categories that are over-represented, such as arts and entertainment. Then it ran scaling-law experiments: several small models trained on candidate mixes, their results used to predict how a large model would do on each mix, and the best candidate trained at a larger scale to confirm it. The final mix holds roughly 50 percent general knowledge, 25 percent mathematics and reasoning, 17 percent code and 8 percent multilingual tokens.
The mix also changes during the run. Qwen3's pre-training has three stages. In the general stage, S1, all models are trained on over 30 trillion tokens with sequences of 4,096 tokens. In the reasoning stage, S2, about 5 trillion more tokens with a larger share of STEM, coding, reasoning and synthetic data are used, still at 4,096, and the learning rate decays faster. In the long-context stage, S3, all models are trained on hundreds of billions of tokens with sequences of 32,768 tokens. Of that long-context text, 75 percent is between 16,384 and 32,768 tokens long and the other 25 percent between 4,096 and 16,384.
Llama 3 ends in a similar way. On its final 40 million tokens it lowers the learning rate to zero in a straight line and raises the share of very high-quality sources, which it calls annealing, and averages the checkpoints taken during that phase to produce the final model.
Chapter 8 of 9
Into shards and windows
Tokenize once, store numbers, read them in parallel
Training does not tokenize on the fly. The whole corpus is tokenized once and stored as token numbers, so that the GPUs never wait for text processing. Each number needs a fixed width on disk. Qwen3's vocabulary has 151,936 rows, more than the 65,536 values that fit in 2 bytes, so each token needs at least 3 and in practice 4 bytes. At 4 bytes, 36 trillion tokens come to 144 terabytes of numbers, before any copies.
The numbers are split into many files, called shards, so that thousands of training processes can each read their own part at once. Inside a shard, documents follow each other, separated by an end-of-text token. For Qwen3 that is <|endoftext|>, number 151643. A window for training is cut from this stream without regard to where documents start. A window of 4,096 tokens may hold the end of one document, the separator and the start of the next. The window needs 4,097 consecutive tokens, because the targets are the inputs moved along by one, as the lesson How wrong, and which way shows.
The order of windows is shuffled, so that a batch mixes sources and no stretch of the run sees only one website. The reader also records how far it has read. When a run restarts from a checkpoint after a hardware failure, it must continue from the same point in the data, or some text would be seen twice and some never.
Chapter 9 of 9
Keeping the test questions out
A benchmark seen in training measures what was memorised
Benchmarks are how models are compared, and benchmark questions are on the web. A question and its answer that end up in the training text make the model look better on that benchmark than it is, because it can remember the answer. This is called contamination, and it can happen by accident, since a pipeline that collects trillions of tokens will collect copies of benchmark questions that people have posted, quoted or discussed.
The Llama 3 report measures it after the fact with 8-gram overlap. An 8-gram is a run of 8 consecutive tokens. A benchmark example is scored by the share of its tokens that are part of an 8-gram that also occurs somewhere in the pre-training corpus. Examples above a threshold count as contaminated, and the rest form the clean subset. Following Singh et al., 2024, the report then compares the model's score on the clean subset with its score on the entire benchmark, and calls the difference the estimated performance gain. The threshold is chosen separately for each benchmark, as the value that shows the largest significant gain. The report measures contamination this way after training. It does not say that it removed benchmark questions from the training text. It also notes the limits: for some benchmarks the 8-gram overlap is so high that no useful estimate is possible, and other detection methods would be needed. A gap between the two scores also does not prove that contamination caused it, because the contaminated questions may simply be easier ones.
The same idea applies at much smaller scale to anyone who fine-tunes a model. If the questions used to evaluate a support model were also in its training tickets, its score says nothing about new tickets. The fix uses the same tool as the de-duplication chapter of this lesson: de-duplicate the evaluation set against the training set before training, and keep the two apart for good. The lesson From base model to your model comes back to this.
What to keep
Trillions of tokens, mostly removed
Pre-training text starts as raw web pages and other sources and reaches the GPUs as shards of token numbers. On the way the HTML is reduced to plain text, unsafe and personal data are removed by domain lists and by reading the text, duplicates are removed by address, by MinHash similarity and by repeated lines, and heuristics and small classifiers trained on a larger model's judgements score what remains. The tokenizer is fixed before the model is trained, often reused from an earlier model, and its vocabulary fixes the size of the embedding table and the output head for good. Sources are mixed in proportions chosen with small models and scaling laws, and the mix and the window length change during the run. Many of these stages, such as the cleaning settings, the quality thresholds and the mix, are chosen by experiment on the output. Others, such as the domain blocklists, are set by policy.