Sub topic 1
Why language models need subword units
Prerequisites
This chapter assumes that you know basic Python, dictionaries, tuples, loops, and frequency counting. You should also understand that a language model receives a sequence of tokens rather than an entire sentence at once.
A token is one unit of text supplied to a model. A token might be a word, a character, or part of a word. A vocabulary is the finite set of tokens that a model knows. A tokeniser converts text into vocabulary entries and usually converts those entries into integer IDs.
The choice of token unit affects almost every part of a language system. It changes sequence length, memory use, how unknown words are handled, and how well the model can represent different languages.
Three ways to split text
A word-level tokeniser might split this sentence:
The cat sleepsinto:
["The", "cat", "sleeps"]This representation is compact when the words are common. However, natural language contains far more possible word forms than a practical vocabulary can store. Names, spelling changes, technical terms, and new words create an out-of-vocabulary (OOV) problem. An OOV word is a word that does not appear in the vocabulary.
A character-level tokeniser avoids most OOV problems because it needs only letters, digits, punctuation, and other symbols. It can represent any word by splitting it into individual characters:
["s", "l", "e", "e", "p", "s"]The cost is long sequences. The model must combine many small pieces before it can recognise useful units such as sleep or -ing.
Subword tokenisation is a compromise. A subword is a piece that can be a complete word, a word fragment, or a short sequence of characters. A subword vocabulary can represent frequent words with one token while breaking rare words into reusable pieces:
["sleep", "s", "##ing"]The exact notation depends on the tokeniser. The important property is that a rare word can often be constructed from known parts.
| Tokenisation level | Main advantage | Main cost |
|---|---|---|
| Word | Short sequences and clear whole-word units | Many unknown words and a large vocabulary |
| Character | Almost no unknown words | Very long sequences and weak word-level units |
| Subword | Balances coverage and sequence length | Requires a learned vocabulary and encoding rules |
Why subwords are a useful compromise
Subwords reuse patterns across related words. For example, a vocabulary might contain pieces corresponding to play, ##ed, and ##ing. These pieces help the model connect played, playing, and play without storing every form as a separate word.
This reuse also helps with rare technical terms. A model may not have a single token for tokenisation, but it can represent the word using smaller pieces that occur elsewhere.
Subwords do not solve every problem. A poor vocabulary can split common words unnecessarily, produce very long sequences, or represent some languages much less efficiently than others. Training data matters as much as the algorithm. A tokeniser trained mostly on English may handle English words compactly but split another language into many pieces.
The basic workflow
Subword methods usually follow two stages:
- Learn a vocabulary and rules from a representative training corpus.
- Apply those rules consistently to new text.
Byte Pair Encoding (BPE) starts with small units and learns frequent adjacent merges. WordPiece builds a vocabulary using a likelihood-style objective and commonly encodes with a greedy longest-match rule. A unigram language model tokeniser starts with candidate pieces and removes pieces that contribute least to the corpus likelihood.
SentencePiece provides a framework that can train BPE or unigram models directly from raw text. This is useful when whitespace cannot reliably separate words, as in some languages. Later chapters examine these algorithms in detail.
A practical consequence
Suppose a model has a maximum input length of 1,024 tokens. A word-level representation may fit more words, while a character-level representation may fit far fewer. A subword representation usually falls between them. Therefore, token count affects context coverage, training cost, and the amount of text the model can process at once.
The best choice depends on the languages, data, hardware limits, and expected inputs. The following chapters show how the main subword approaches learn and apply their vocabularies.
Exercise
Why is subword tokenisation usually preferred to choosing only words or only characters?
Show answer
It usually provides shorter sequences than character tokenisation while representing rare or unseen words more reliably than word tokenisation.