Tokenizers, pretraining, and scaling laws

Share
Feature figure for Tokenizers, pretraining, and scaling laws

In the background posts we built models that read numbers, and in Post 28 we learned how to tell whether they were any good. This post turns to text, and to the three decisions that shape a language model before a single weight is trained: how we cut text into tokens, what objective we ask the model to solve, and how much data we feed it. We will build all three on WikiText-2 and land on a headline of 5.461 nats per token at 356,072 parameters. A nat is a unit of information, and one nat is about 1.44 bits. Nats per token therefore measures the model's compression cost. The through-line is a simple one. A language model is a compression engine, and every choice in this post is a choice about what gets compressed and how tightly.

We work with the raw WikiText-2 split from Hugging Face, about 2 million tokens of clean Wikipedia prose, small enough to train on a single GPU in a notebook. The plan is to look at the data first, then fit five tokenizers on the same sample, then compare five pretraining objectives on the same tiny batch, and finally run a small scaling sweep to see loss fall as the model grows.

First, look at the data

Before any tokenizer or model, we inspect the raw rows. The first thing we find is that a lot of WikiText-2 is not prose at all. Empty lines make up 35.27 percent of the training rows, so we drop them before language modeling. The remaining documents are skewed in length, with a mean of 95 whitespace tokens and a maximum of 699, which tells us fixed short blocks will keep batches cheap.

Empty rows dominate the raw training split

The whitespace vocabulary is large and long-tailed. Counting every space-separated word in the training split gives 76,603 distinct tokens, and the frequency curve falls off the way a Zipf distribution always does. A Zipf distribution means a few words are very common and most are rare, so the curve falls steeply and then flattens into a long tail. That long tail is exactly the problem subword tokenization exists to solve, and it shows up immediately as an out-of-vocabulary rate. On the validation split, a whitespace model misses 9.5 percent of tokens, while a byte-level view has zero unknown rate by construction.

Two more findings matter for the rest of the post. There are 2,054 exact duplicate rows in the training split, and 265 training rows that appear verbatim in validation or test. We remove both, and we also drop 64 rows where validation and test overlap. After cleaning we are left with 21,448 training rows, 2,308 validation rows, and 2,891 test rows, and we assert that no exact text is shared between splits. The test split stays untouched until the end.

Tokenization

The 9.5 percent out-of-vocabulary rate from the whitespace view is the problem tokenization has to fix. A tokenizer maps text to integer IDs, and the vocabulary size trades sequence length against embedding size. Byte Pair Encoding starts from characters and merges the most frequent adjacent pairs. WordPiece merges pieces that maximize the likelihood of the training data, and marks continuation pieces with a ## prefix. SentencePiece works on raw text with whitespace escaped, so it never needs a pre-tokenization step. Byte-level models start from the 256 byte IDs and never produce an unknown token. Character-level models use single characters and make sequences very long.

We fit all five views on the same 3,000-document sample, each with a 1,000-token vocabulary. The probe sentence tells the story in miniature. BPE splits "quick" into qu, ick, and b, row, n for "brown". WordPiece produces the same pieces but marks continuations with ##. SentencePiece keeps the leading space attached to the word, so "quick" becomes a single piece with an escaped leading space and "brown" splits into b and rown. The byte-level model renders each character as its raw byte ID, so the sentence becomes a long run of single-byte tokens. The character model produces one ID per character, the longest view of all.

The average over 100 validation documents makes the trade concrete. BPE averages 195.73 tokens per document, WordPiece 214.77, SentencePiece 206.25, byte-level 205.57, and character-level 513.31. The character model is more than twice as long as the subword models, which means twice the sequence length and twice the attention cost for the same text. WordPiece shows an unknown rate of 0.0002 on the validation sample, while the byte-level model shows 0.0. That is about two unknown tokens per ten thousand, so WordPiece still drops a rare piece of text that byte-level never drops.