Lensa ML
Lensa ML

Tokenization & BPE

Step 1 of 6

Why Tokenize?

Text must become numbers before a model can process it

Merge operations applied: 00
ThetransformermodelprocessestextSequence length: 36 tokensVocabulary: 26 tokens← fewer merges = more tokens, smaller vocab | more merges = fewer tokens, larger vocab →
MERGES
0
TOKENS
36
VOCAB
26

No merges yet — pure character-level tokenization. Every letter is its own token. The vocabulary is tiny (just 26 letters), but the sequence is very long.

Step 1 of 6: Why Tokenize?

Text must become numbers before a model can process it

Merge operations applied: 00
ThetransformermodelprocessestextSequence length: 36 tokensVocabulary: 26 tokens← fewer merges = more tokens, smaller vocab | more merges = fewer tokens, larger vocab →
MERGES
0
TOKENS
36
VOCAB
26

No merges yet — pure character-level tokenization. Every letter is its own token. The vocabulary is tiny (just 26 letters), but the sequence is very long.

Step 2 of 6: Character vs Word Tokens

The tradeoff between vocabulary size and sequence length

unbelievableunbelievable12unbelievable3unbelievable1rethinkingrethinking10rethinking3rethinking1xylophonistxylophonist11xylophonist4[UNK]1Charactervocab ~26Subwordvocab ~30kWordvocab ~200k+
LEVEL
Subword
VOCAB SIZE
~30k
SEQ LENGTH
balanced

Subword level: BPE discovers meaningful pieces — 'un' (negation), 'believ' (root), 'able' (suffix). Even rare words like 'xylophonist' can be split into known parts. The sweet spot!

Step 3 of 6: Byte-Pair Encoding Merges

Iteratively merge the most frequent character pairs

Merge step (0 = characters)0
Corpus: "low lower lowest new newer newest wide wider widest"lowlow</w>lowerlower</w>lowestlowest</w>newnew</w>newernewer</w>newestnewest</w>widewide</w>widerwider</w>widestwidest</w>Top pair counts (next merge picks #1):w + e4l + o3o + w3e + r3r + </w>3
MERGE #
0
VOCAB SIZE
11
TOP PAIR
4

Starting from individual characters (plus end-of-word marker </w>). BPE counts every adjacent pair across the corpus — the most frequent pair will be merged first.

Step 4 of 6: Building the Vocabulary

Watch the vocabulary grow as merges accumulate

Number of BPE merges5
Vocab ↑Tokens ↓Merges →
MERGES
5
VOCAB
16
TOTAL TOKENS
36
AVG TOK LEN
1.9

5 merges: vocab = 16, total tokens = 36. The curves show the fundamental BPE tradeoff — more merges = larger vocabulary but shorter sequences. Real models use 30k–100k merges.

Step 5 of 6: Tokenizing New Text

Apply learned BPE rules to unseen words

startlowest</w>1w+e→welowest</w>2l+o→lolowest</w>4s+t→stlowest</w>5st+</w>→st</w>lowest</w>✓ All tokens found in vocabulary
WORD
lowest
TOKENS
3
MERGES APPLIED
4
IN VOCAB
Yes

Merges are applied in the order they were learned during training — rank 1 was the most frequent pair in the corpus, rank 2 the next, and so on. At inference, BPE scans this fixed list top-to-bottom: #1 (w+e→we), #2 (l+o→lo), #4 (s+t→st), #5 (st+</w>→st</w>). Only rules whose pair exists in the current tokens actually fire. This fixed priority is what makes BPE deterministic and fast.

Step 6 of 6: Tokens to IDs

From subwords to integers to embedding vectors

Thetransformermodellearns101467224331118294434571055↓ Embedding lookupEmbedding[101]: (showing 8 of 768 dims)-0.310.49-0.450.210.12-0.390.50-0.39
TOKEN
The
ID
101
POSITION
0
EMBED DIM
d=768

Token "The" → ID 101 → embedding vector. Each token ID maps to a row in the embedding matrix (typically 768 or 1024 dimensions). This dense vector is what the transformer actually processes — not the text itself.