Tokens: how a language model reads text
Words, subword pieces, characters or bytes. See what a tokenizer decides before any model runs, why its vocabulary is fixed, and how byte and character models such as ByT5 and CANINE skip the vocabulary altogether.
What runs in CodeSplash
- LNG031ByT5 Explained here
- LNG032CANINE Explained here
- Text encoder (MiniLM + linear head) Not verified yet
You will be able to
- Explain why language models read subword tokens rather than whole words
- Say what a fixed vocabulary does to rare words, typos and other languages
- Compare byte-level (ByT5) and character-level (CANINE) inputs with subword tokens
- Recognize that token counts set cost, context length and per-token scores
Read first
Before the model, a tokenizer
ReadA language model never sees letters. A tokenizerTokenThe unit a language model reads and writes; often a word fragment, not a whole word. first cuts the text into pieces from a fixed list, the vocabulary, and turns each piece into an id. Everything after that, the embeddings, the attention and the next-token probabilities, works on those ids.
Whole words make a poor vocabulary: every new name, typo or inflection is a word the model has never seen. Single characters make sequences long. Most models pick the middle road of subword tokensSubword tokenizationCutting text into pieces between characters and words: frequent words stay whole, rare ones split into common pieces.: frequent words stay whole, rare words split into common pieces, and nothing is ever truly unknown.
Three ways to learn the pieces
Read- Byte-pair encoding starts from single bytes and keeps merging the most frequent neighboring pair until the vocabulary is full. GPT-2 and RoBERTa use byte-level BPEByte-level BPEMerges frequent pairs of UTF-8 bytes into tokens, so any text can be written without unknown tokens..
- WordPiece merges the pair that most raises the likelihood of the training text. BERT, MiniLM and E5 use it; a piece that continues a word is written
##ing. - SentencePiece Unigram starts from a large set of candidate pieces and prunes the ones that matter least. Spaces become ▁, so no language needs spaces between words. XLM-R and multilingual-E5 use the Unigram tokenizerSentencePiece Unigram tokenizerSplits text into the most probable sequence of subword pieces under a unigram model; spaces become ▁..
All three are trained once, before the model, and then frozen with it.
DExplorable · Byte-pair encoding
How does a tokenizer learn its vocabulary, and what happens to a word it has never seen?
Training text
The text the merges are learned from.
Each merge joins the most frequent adjacent pair into one new token.
Add </w> so a token knows it ends a word (“est</w>” differs from “est”).
Start from UTF-8 bytes instead of characters, so nothing is ever unknown.
Spell an unknown character as its bytes instead of <unk>.
Word to tokenize
Words not in the training text, one with a character it never saw.
“lowest” as tokens
- low
- est</w>
Merges, in the order learned (10)
- e+s ×9
- es+t ×9
- est+</w> ×9
- l+o ×7
- lo+w ×7
- e+w ×6
- ew+est</w> ×6
- n+ewest</w> ×6
- low+</w> ×5
- d+est</w> ×3
Base symbols 11 · vocabulary 21
- Vocabulary size
- 21
- Tokens in the training text
- 28
- Tokens for the word
- 2
- Unknown tokens
- 0
- Characters per token
- 2.82
- Puzzle: Merge until every word of the training text is a single token.
- Puzzle: Tokenize “naïve” with no <unk>, without byte fallback.
After 10 merges the vocabulary has 21 tokens and the training text takes 28 tokens (2.82 characters per token). “lowest” becomes 2 tokens: low · est</w>.
A typo meets a fixed vocabulary
PredictPredict first
A subword tokenizer trained on clean text meets "recieve", a common misspelling. What happens?
Reveal
It splits into several smaller pieces. The vocabulary can't grow after training, but the pieces can always spell the word out, down to single characters or bytes. The model then has to make sense of an unusual sequence, which is one reason typos hurt.
Skipping the vocabulary: bytes and characters
ReadByte-level modelsByte-level and character-level modelsModels that read UTF-8 bytes (ByT5) or Unicode characters (CANINE) instead of a learned vocabulary: no unknown tokens, longer inputs. drop the learned vocabulary. ByT5 reads UTF-8 bytes, a fixed alphabet of 256. "café" is 4 characters but 5 bytes, because é takes two. Any language and any typo can be written, at the price of longer sequences: a model that reads bytes needs more layers or more compute for the same text.
CANINE reads Unicode code points instead (the character 猫 is code point 29,483) and then downsamples neighboring characters into blocks, so the deep layers see shorter sequences.
Reference · LNG031 · Explained hereByT5Operates on UTF-8 bytes instead of a learned subword tokenizer, reducing dependence on vocabulary segmentation. Byte-level processing is useful when text includes spelling variation, uncommon scripts, or noisy surface… Reference · LNG032 · Explained hereCANINEBuilds language representations directly from character code points, with downsampling to control the cost of long character sequences.Why token counts matter to you
ReadThree consequences follow from the tokenizer:
- Cost and context. Providers bill, and models fit, tokens, not words. Scripts the tokenizer saw less of need more tokens for the same meaning.
- Truncation. An encoder drops what lies beyond its limit. MiniLM, CodeSplash's default text encoder, reads the first 256 tokens; the GPT-2 scorer reports how many rows it had to cut.
- Comparisons. Perplexity per token depends on the tokenizer. Two models with different tokenizers can only be compared per word or per character.
Where it stands in CodeSplash
Read| Entry | In CodeSplash |
|---|---|
| ByT5 byte inputs (LNG031) | Explained here |
| CANINE character inputs (LNG032) | Explained here |
| WordPiece, byte-level BPE, SentencePiece Unigram | Run inside Text encoder and the GPT-2 scorer, written out in Python and R |
Check: tokens
CheckpointCheck yourself
Why can a byte-level model such as ByT5 read any text without an unknown token?
Show the answer
Answer: a
UTF-8 writes every character of every script as one to four bytes, so 256 symbols cover all text. The cost is length: sequences grow with every multi-byte character.
Check yourself
You switch from a subword model to a byte-level model for a corpus of Greek and Japanese text. What should you expect?
Show the answer
Answer: b
Non-Latin scripts take two or more bytes per character in UTF-8, so byte sequences grow fastest exactly where subword tokenizers were weakest.
Check yourself
What does CANINE do so that its deep layers don't pay for one position per character?
Show the answer
Answer: b
CANINE embeds Unicode code points, then a strided step merges neighboring positions into blocks, so the expensive layers run on a shorter sequence. An upsampling step restores character positions for tasks that need them.
Check yourself
A task needs to mark exact character offsets of names in text, in many scripts. Why might a character-level model like CANINE suit it better than a subword model?
Show the answer
Answer: a
Subword pieces don't line up with character boundaries in every script, so offsets need extra bookkeeping. A character-level model's positions are the characters themselves.
Check yourself
Model A reports perplexity 30 per token, model B 45 per token, on the same text. They use different tokenizers. What can you say?
Show the answer
Answer: c
A tokenizer that makes more, smaller tokens gives each one an easier job. Per-word or per-character figures put both models on the same footing.
Recap
Recap- A tokenizer, trained once and frozen with the model, turns text into ids from a fixed vocabulary.
- BPE, WordPiece and SentencePiece Unigram learn subword pieces; rare words and typos split into several.
- ByT5 reads bytes and CANINE reads characters: no unknown tokens, longer sequences.
- Token counts set cost, context and truncation, and make per-token scores incomparable across tokenizers.
Concepts in this lesson