Transformer Training — Presentation 02

Data and Tokenisation

What a model trains on: a corpus split into training and validation text, cut into windows, batched, and (for most models) turned into tokens by byte-pair encoding. Then what changes at scale: packing, deduplication, filtering, mixtures and tokens per parameter.

Corpus Train / validation Windows Batches BPE Packing Deduplication
Corpus → Split → Windows → Batch → Tokens
00

Topics We’ll Cover

Live companion and sources

This deck follows chapter 2, data and tokenisation, where BPE learns its first merges on the whole book in your browser. Measured numbers: results.md §1; the BPE demo below runs the site’s algorithm on the book’s first paragraph.

01

The Corpus and the Held-Out Split

The Training site trains on Lewis Carroll’s Alice’s Adventures in Wonderland (1865, Project Gutenberg eBook #11, public domain), reduced to the Decoder’s 64-character alphabet: lower case, straight quotes, no italics marks.

training: first 127,837 characters
validation: last 14,204 (10%)

Why hold text out

A model can lower its training loss by memorising. Only text it has never seen shows whether it learnt something that carries over, so the end of the book is never trained on, and runs are compared on it (deck 01).

Pinned and recorded

142,041 characters in all; SHA-256 8a2b46fd9524cc57… recorded in results.md §1, so every run can prove which text it saw. Unigram entropy of the training characters: 3.0000 nats.

02

Windows, Batches and Context Length

A training example is a window of \(S + 1 = 9\) consecutive characters. \(S = 8\) is the model’s context length: how many tokens it sees at once (real models: thousands). The inputs are \(x_i = w_i\) and the targets \(y_i = w_{i+1}\), so one window gives 8 training signals.

…the rabbit-hole went straight on…
→
16 windows × 8 characters
→
128 predictions per step

Each step draws 16 windows at random places in the training text: the batch. A window starts at \(\lfloor u\,(N - S - 1)\rfloor\) for \(u\) uniform in \([0,1)\) from the seeded generator, \(N\) the number of training characters:

src/lib/training/data.ts
for (let b = 0; b < B; b++) {
  const start = Math.floor(rng() * (train.length - S - 1));
  const w = train.slice(start, start + S + 1);
  starts.push(start);
  inputs.push(w.slice(0, S));
  targets.push(w.slice(1));
}

The 2,000-step default run sees 256,000 characters: about 2.0 passes (epochs) over the training text.

03

Why Tokens Rather Than Characters

The Decoder’s model reads characters, so it must spend its context and its layers assembling “the” from three pieces every time. Most real models read tokens: frequent fragments learnt from the text itself.

Byte-pair encoding

BPE (Sennrich, Haddow & Birch 2015) starts from single symbols and repeatedly merges the most frequent adjacent pair into a new symbol, until the vocabulary has the size wanted.

Over bytes

GPT-2 runs the same algorithm over bytes rather than characters, so any text can be encoded, with a vocabulary of 50,257 (Radford et al. 2019).

The site keeps training on characters, so that the model being trained is exactly the Decoder’s; the tokeniser is there to show how real models see text. With tokens, the same 8-position context covers more text, and a per-token loss is a larger number for the same skill (so perplexities compare only under one tokeniser).

04

Byte-Pair Encoding, Step by Step

Below, the site’s BPE (a port of scripts/training/data.py: words split as a leading space plus a run of non-spaces, ties broken by the smaller pair of ids) learns 12 merges on the book’s first paragraph. Step through it.

$$\begin{gathered}(a^\ast, b^\ast) = \arg\max_{(a,b)} \operatorname{count}(a\,b) \\ V \leftarrow V \cup \{a^\ast b^\ast\}\end{gathered}$$
05

BPE on the Whole Book

On the full training text the first merges are the language’s commonest fragments: “␣t”, “he”, “␣a”, “␣s”, “in”, and by the sixth “␣the”, a space and the whole word (the site’s chapter 2 animates them).

64

merges (a vocabulary of 128)

142,041

characters

85,614

tokens after the merges

1.659

characters per token

So an 8-token context would see about two thirds more text than 8 characters. Each merge removes exactly \(\operatorname{count}(a^\ast b^\ast)\) symbols from the corpus (fewer if the pair overlaps itself, as in “a a a”); ties go to the smaller pair of ids, so the Python reference and the browser learn identical merges.

06

Packing Documents and Document Masking

Real corpora are many documents of different lengths. Rather than pad short ones, trainers pack: concatenate them into one long stream with a separator token and cut fixed-length windows, so no compute is wasted on padding.

doc A
<sep>
doc B
<sep>
doc C
→
fixed windows

A window can then straddle two documents. Some runs mask attention across the boundary, so a document never attends to the one before it. Llama 3’s report describes doing this and finds it matters most for very long sequences (Grattafiori et al. 2024, §3.2). With a single book there is no boundary here.

07

Data at Scale: Deduplication, Filtering, Mixtures

Small corpora

Small experiments often use small, simple corpora such as TinyStories, short stories written in a child’s vocabulary, on which models of a few million parameters already write fluent English (Eldan & Li 2023).

Deduplication

Duplicated text is memorised and wastes compute; removing near-duplicates helps (Lee et al. 2021).

Filtering and mixtures

Pretraining corpora are web crawls plus books, code and papers, in trillions of tokens; most of the work is quality filtering and choosing the mixture of sources. FineWeb documents one such pipeline in detail (Penedo et al. 2024).

08

Tokens per Parameter

How much data a model of a given size should see is a scaling question (deck 13). The Chinchilla analysis found about 20 tokens per parameter compute-optimal (Hoffmann et al. 2022).

The toy

127,837 training characters for 5,376 parameters: 23.8 characters per parameter, by coincidence in the same range.

But

The default run sees that text about 2.0 times; real runs mostly see each token once. Deck 13’s toy scaling sweep trains far past one pass, which is one reason its fitted law should not be over-read.

09

What to Take Away