What a Unigram tokenizer is
A text can usually be cut into vocabulary pieces in many ways. BPE (the GPT-2 tab) replays its learnt merges and WordPiece (the BERT tab) greedily takes the longest piece; neither compares one split with another. A Unigram tokenizer gives every piece in its vocabulary a probability and encodes a text as the split whose pieces are most probable together.
This tab records T5's tokenizer (google-t5/t5-small), a SentencePiece Unigram model: spiece.model holds 32,000 entries, 31,997 of them ordinary pieces with a log-probability each, plus , and . The page follows one input through Normalize, the Unigram lattice, the Viterbi search, Tokens → ids and Decode, run by the plain-Python reference code; its ids equal sentencepiece's on all 325 check strings.
Normalize: NFKC and ▁
The Normalize column shows the input and the string the model actually splits. Unicode normalization form NFKC folds compatibility characters into their plain forms (fi becomes fi, full-width letters become ASCII), control characters are dropped, runs of spaces collapse to one and the ends are trimmed. Then every space becomes the visible mark ▁ (U+2581), and one ▁ is added in front.
There is no pre-tokenizer: the spaces stay inside the pieces, so a piece like ▁turns can only start a word. Unlike BERT's uncased tokenizer, case and accents are kept, so ▁café is a piece of its own. On “ x = 12345” the leading spaces disappear and the model splits ▁x▁=▁12345.
The unigram model
Kudo's Unigram language model assumes the pieces of a split occur independently, so a split of the normalized text has probability . The encoding is the most probable split among all splits that spell the text, which is the one whose log-probabilities sum highest:
Each piece adds a negative log-probability, so fewer, longer pieces usually win. Under Normalize, the list headed The 4 best splits shows sentencepiece's n-best splits of the first example with their totals. For “Tokenization turns text into numbers.” the best is ▁To ken ization ▁turns ▁text ▁into ▁numbers . at −70.73; splitting ▁numbers into ▁number s gives −72.92, and ▁turns into ▁turn s gives −73.07.
The lattice and Viterbi
Listing every split would take exponential time; the Unigram lattice panel instead draws every vocabulary piece that matches somewhere in the text as a bar over the characters it covers, with its log-probability: 78 nodes for the first example. No piece is longer than 16 characters, which bounds the search. A character that no piece covers gets an node scoring the lowest log-probability in the vocabulary minus 10, that is , so a path through the lattice always exists.
The Viterbi algorithm then fills one number per end position , from left to right: , the highest total over all splits of the first characters. Each node from to offers plus its own log-probability, and the best offer is kept with a pointer back to its node:
Following the pointers back from the last position gives the path, drawn in the lattice's top lane. The Viterbi at e panel lists the offers ending at the chosen position and marks the one kept, and the Viterbi forward pass strip steps through every . For “unbelievably” the path is ▁unbe lie v ably at −40.72, ahead of ▁un be lie v ably at −44.13; WordPiece's greedy rule gives un ##bel ##ie ##va ##bly for the same word.
Two paths can score exactly the same. Three check strings do so (“!!!!!!!!”, “----------” and “?????”); the reference keeps the node with the earlier start, as sentencepiece does, and T5TokenizerFast keeps the other path on “----------”.
Tokens, ids and decoding
An id is the piece's position in spiece.model, with 0, 1 and 2. The Tokens → ids column lists the path's pieces and appends , which ends every input; T5 has no start token. Neighbouring nodes fuse into one token, so “東京は晴れ” becomes ▁ and a single scoring . T5's paper trained its SentencePiece model on 10 parts English C4 data to 1 part each of German, French and Romanian, and none of the five characters of “東京は晴れ” has a piece. Even the ï of “naïve” has none and becomes ; over the 325 check strings there are 224 tokens.
Clicking a token opens its vocabulary entry and its row of t5-small's shared embedding matrix, 32,128 rows of width 512. That is more than the 32,000 entries of spiece.model: the T5 tokenizer in transformers adds 100 sentinel tokens, to , as ids 32000–32099, and the tokenizer never produces the last 28 rows. T5's span-corruption objective replaces each dropped span of the input with one sentinel.
Decode joins the pieces, turns ▁ back into spaces and drops the first one, then glues a few marks such as “.” back to the word before. decodes to nothing (sentencepiece's own decoder writes ⁇), and collapsed spaces do not come back, so 165 of the 325 check strings decode back exactly.
Learning the vocabulary
Kudo trains the vocabulary the other way round from BPE and WordPiece, which grow it by merging. It starts from a large seed vocabulary, estimates the probabilities with the EM algorithm, scores each piece by how much the likelihood of the training data would drop without it, and keeps only the best (80 % in the paper's example), always keeping the single characters. It repeats this until the vocabulary has the size wanted. Because every split has a probability, training can also sample splits instead of always using the best one: Kudo's subword regularization samples from the l-best splits, like the list on the page, with a parameter that sharpens the choice towards the Viterbi split.
Where it came from
The Viterbi algorithm comes from decoding convolutional codes (Viterbi, 1967). Kudo introduced the Unigram tokenizer in 2018, and SentencePiece (Kudo and Richardson, 2018) packaged it, with BPE, as a language-independent tool that trains on raw sentences and treats the space as an ordinary symbol. T5 (Raffel et al., 2020) used a 32,000-piece SentencePiece vocabulary shared by its input and output; its paper calls the pieces wordpieces, though the file is a Unigram model. Bostrom and Durrett (2020) pre-trained the same model with each and found that Unigram matches or beats BPE on the tasks fine-tuned afterwards. The ByT5 tab shows the T5 family without a vocabulary, reading raw UTF-8 bytes.
Papers
- Subword Regularization: Improving Neural Network Translation Models with Multiple Subword Candidates Introduces the Unigram language-model tokenizer: Viterbi encoding, EM training with pruning, and sampling from the l-best splits.
- SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing The library and the spiece.model format this tab reads: raw-text training, NFKC normalization and ▁ for the space.
- Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer T5, whose 32,000-piece SentencePiece vocabulary this tab records, with the sentinel tokens of span corruption.
- Error bounds for convolutional codes and an asymptotically optimum decoding algorithm The dynamic-programming search the page's Viterbi forward pass runs over the lattice.
- Byte Pair Encoding is Suboptimal for Language Model Pretraining Pre-trains the same masked language model with BPE and with Unigram tokenization and finds Unigram as good or better after fine-tuning.
- Between words and characters: A Brief History of Open-Vocabulary Modeling and Tokenization in NLP A survey of word, character, subword and byte tokenization, placing Unigram among BPE and WordPiece.