Sub topic 5
Encoding New Text with Learned BPE Merges
Assume you know basic Python, dictionaries, tuples, loops, token frequencies, and the idea of a token as a language model input unit. This chapter focuses on applying a learned Byte Pair Encoding (BPE) vocabulary to new text.
From characters to subwords
During BPE training, the tokenizer starts with small symbols, often characters plus an end-of-word marker such as </w>. It repeatedly finds the most frequent adjacent pair and records a merge for that pair.
Encoding uses the recorded merges. It does not count the new text again or learn new merges. Instead, it applies the saved merge list in its original order.
For example, suppose training learned these merges:
| Rank | Merge |
|---|---|
| 1 | `l` + `o` → `lo` |
| 2 | `lo` + `w` → `low` |
| 3 | `e` + `r` → `er` |
| 4 | `low` + `er` → `lower` |
To encode lower, begin with:
l o w e r </w>Apply the merges in rank order:
lo w e r </w>
low e r </w>
low er </w>
lower </w>The final token may be represented internally as lower</w>, or the tokenizer may return lower and keep the end marker hidden.
A minimal BPE encoder
The following code applies a fixed merge list to one word. A merge replaces every currently adjacent occurrence of its pair. The list order matters because an earlier merge can create a pair needed by a later merge.
def merge_pair(symbols, pair):
merged = []
i = 0
while i < len(symbols):
if i + 1 < len(symbols) and (symbols[i], symbols[i + 1]) == pair:
merged.append(symbols[i] + symbols[i + 1])
i += 2
else:
merged.append(symbols[i])
i += 1
return merged
def bpe_encode_word(word, merges):
symbols = list(word) + ["</w>"]
for pair in merges:
symbols = merge_pair(symbols, pair)
return symbols
learned_merges = [
("l", "o"),
("lo", "w"),
("e", "r"),
("er", "</w>"),
("low", "er"),
]
print(bpe_encode_word("lower", learned_merges))
print(bpe_encode_word("low", learned_merges))
print(bpe_encode_word("lowest", learned_merges))The result is:
['lower</w>']
['low', '</w>']
['low', 'e', 's', 't', '</w>']lowest shares the learned subword low, but the remaining symbols cannot be merged because no suitable merge was learned. This is the main benefit of subword tokenisation: an unfamiliar word can still be represented by familiar pieces.
Important implementation details
Merge rank is part of the model
A tokenizer must preserve the exact order in which merges were learned. Applying a later merge first can produce a different segmentation or prevent an earlier merge from matching.
The saved tokenizer normally contains:
- The base vocabulary and symbol-to-ID mapping.
- The ordered merge list.
- Normalisation rules.
- Pre-tokenisation rules.
- Special token definitions.
The model is therefore more than a list of token strings.
Training and inference must agree
New text must use the same normalisation and pre-tokenisation rules as the training text. For example, if training lowercased words and separated punctuation, inference must do the same. Otherwise, the same visible text can produce different symbols and different token IDs.
End markers and whitespace
Some BPE systems attach an end-of-word marker to the final symbol. Others use a whitespace marker, byte-level representation, or separate word-boundary convention. These choices affect decoding and token statistics. Never assume that a token string alone reveals the complete boundary behavior.
Exercise
Given the merge list [("a", "b"), ("ab", "c")] and the word "abc", what symbols result after BPE encoding?
Show answer
Start with ["a", "b", "c", "</w>"]. The first merge produces ["ab", "c", "</w>"], and the second produces ["abc", "</w>"].