Sub topic 5
Subword Tokenisation: BPE and WordPiece
Assume you know basic Python, regular expressions, and the purpose of tokenisation in an NLP task such as search or classification. This chapter focuses on subword tokenisation, which splits words into reusable pieces instead of treating every complete word as an independent token.
Why split words into subwords?
Word-level tokenisation gives each word a vocabulary entry. This works well for frequent words, but it creates two problems:
- A vocabulary can become very large.
- New words, misspellings, names, and technical terms may not exist in the vocabulary.
An out-of-vocabulary word is a word that the tokenizer cannot represent using its known vocabulary. Older systems replaced such words with a single unknown token, such as <UNK>. That replacement loses information.
Subword tokenisation builds words from smaller units. For example, a tokenizer might represent unhappiness as:
un + happinessOr it might use smaller pieces:
un + happi + nessThe pieces un, happi, and ness can also occur in many other words. A fixed vocabulary can therefore represent more word forms.
Byte Pair Encoding
Byte Pair Encoding, usually called BPE, learns a vocabulary by repeatedly merging frequent adjacent symbols.
A simple word might begin as character symbols with an end marker:
l o w </w>If l and o frequently occur together, BPE may merge them:
lo w </w>It continues merging frequent pairs until it reaches the desired vocabulary size. The learned merge rules might later produce:
lower</w> → lowerDuring encoding, the tokenizer starts with small symbols and applies the learned merges in their priority order. Frequent words often become one token. Rare words are represented by several subword tokens.
Many modern systems use byte-level BPE. Instead of starting with Unicode characters, they start with bytes. This lets the tokenizer represent arbitrary text, including unusual characters, although the resulting pieces may look less readable.
WordPiece
WordPiece also learns a vocabulary of subwords, but its training objective is different. It selects pieces that help the model assign a high probability to the training text, rather than simply choosing the most frequent adjacent pair.
At encoding time, WordPiece commonly uses longest-match-first search. It finds the longest vocabulary entry that matches the beginning of the remaining text. Continuation pieces often have a marker such as ##.
For example, a vocabulary might contain:
un
##play
##ableThe word unplayable becomes:
un ##play ##ableThe ## marker means that the piece continues an existing word. Tokenizers may use different marker conventions, so the exact display is implementation-specific.
def wordpiece(word, vocabulary):
pieces = []
start = 0
while start < len(word):
end = len(word)
match = None
while end > start:
text = word[start:end]
candidate = text if start == 0 else "##" + text
if candidate in vocabulary:
match = candidate
break
end -= 1
if match is None:
return ["[UNK]"]
pieces.append(match)
start = end
return pieces
vocabulary = {"un", "play", "##ing", "##able", "##play"}
for word in ["playing", "unplayable", "unknown"]:
print(word, "->", wordpiece(word, vocabulary))This simplified encoder demonstrates the lookup rule. A production tokenizer also handles special tokens, normalization, pre-tokenisation, and model-specific vocabulary files.
BPE and WordPiece compared
| Feature | BPE | WordPiece |
|---|---|---|
| Common training idea | Merge frequent symbol pairs | Choose pieces using a likelihood-oriented objective |
| Common encoding rule | Apply learned merge priorities | Longest-match-first |
| Continuation marker | Implementation-dependent | Often `##` |
| Unknown-word behaviour | Often avoids unknown words with byte or character fallback | May emit `[UNK]` if no complete segmentation exists |
Both methods reduce vocabulary pressure and help models handle unseen word forms. They do not understand meaning, however. A piece such as ##bank is reused because of its spelling, not because the tokenizer knows whether the word refers to finance or a river.
Choosing a subword tokenizer
Use the tokenizer expected by the model. A model trained with WordPiece generally cannot use a BPE tokenizer without changing the model input mapping. Keep tokenisation and detokenisation rules with the model configuration.
Subword tokenisation is usually a good default for neural language models. For a small search system, word tokens plus carefully chosen normalization may be easier to inspect. Always measure the effect on sequence length, unknown-token rate, memory use, and task accuracy.
Exercise
Why can subword tokenisation represent a new word more effectively than a word-level vocabulary?
Show answer
It can assemble the new word from smaller vocabulary pieces that were learned from other words, instead of replacing the entire word with one unknown token.