Token Lab

Build a tiny tokenizer

Before a model can use token IDs, a tokenizer learns a vocabulary from a text corpus. Change each step below, then train a small BPE example to see what it learns.

The four models in Token Lab already have trained tokenizers and model embeddings. This separate classroom example first builds a simplified, character-based BPE tokenizer, then lets you train a tiny model for its local token IDs.

1 Text corpus

A corpus is the collection of examples the tokenizer learns from. Repetition helps it discover useful pieces.

62 characters of your own text · maximum 4,000
By Lewis Carroll (1865). The bundled text is added to your own text for every training step. Read the source edition.

2 Normalize

Decide whether forms such as “Cat” and “cat” should be treated alike before learning vocabulary.

After normalization
The cat sat on the mat.
The cat wore a hat.
The cat saw a rat.

3 Pre-tokenize

The pre-tokenizer chooses chunks that BPE cannot merge across. The GPT-2-style option attaches a leading space to many chunks. Spaces appear as ␠, and line breaks as ↵. This demo does not reproduce GPT-2’s byte conversion.

Chunks (21)
The␠cat␠sat␠on␠the␠mat.↵The␠cat␠wore␠a␠hat.↵The␠cat␠saw␠a␠rat.

4 Target vocabulary size

The starting vocabulary needs one ID for each character in this corpus, plus an unknown-character ID. The target allows additional pieces learned through merges.

Minimum 16Maximum 76
Starting vocabulary (16)
<unk>↵␠.acehmnorstTw

5 Train BPE

The trainer counts adjacent pairs inside each chunk, merges the most frequent pair, and repeats until it reaches the target or runs out of repeated pairs.

LOCAL BPE DEMONSTRATION

What the tokenizer learned

20 pieces · 4 learned merges

The target vocabulary size was reached.

Merge history

StepPairNew pieceCount
1a + tat7
2h + ehe4
3␠ + c␠c3
4␠c + at␠cat3

Final vocabulary

0<unk>
1↵
2␠
3.
4a
5c
6e
7h
8m
9n
10o
11r
12s
13t
14T
15w
16at
17he
18␠c
19␠cat

Try the trained tokenizer

The same normalization and splitting rules apply to new text. Unseen characters use the local <unk> ID.

Normalized input
The cat sat on a mat.
Pieces and local IDs
T14
he17
␠cat19
␠2
s12
at16
␠2
o10
n9
␠2
a4
␠2
m8
at16
.3

6 Learn embeddings

The tokenizer above learned pieces and IDs. Now train a tiny model to predict the next token from the current token. Its eight-number input vectors start random and change as the model learns. This uses one token of context; it is not a transformer.