What WordPiece is
A language model reads integers, not text, so a tokenizer cuts the text into pieces from a fixed vocabulary and replaces each piece by its line number. A vocabulary of whole words would leave every rare or new word unknown; a vocabulary of single characters would make sequences long and each token nearly meaningless. WordPiece sits between the two: common words are one piece, and rarer words are spelt out from smaller pieces that continue them, written with a ## prefix.
This tab records the tokenizer of BERT base uncased (google-bert/bert-base-uncased): its vocab.txt of 30,522 entries, 5,828 of them ## pieces. The page follows one input through the five steps of the plain-Python reference code, Normalize, Basic split, WordPiece, Tokens → ids and Decode, and its ids equal those of BertTokenizerFast and the original BertTokenizer on all 325 check strings. Only the tokenizer files were downloaded, no model weights.
Normalize and the basic split
The Normalize column shows what happens to each character. Control characters are dropped. Each Chinese ideograph gets a space on either side, so it becomes a word of its own; kana such as は are ordinary letters. The text is decomposed into Unicode form NFD, which writes é as e followed by a combining accent, and the combining marks are dropped. Finally everything is lower-cased: uncased means the model never sees capitals, so “naïve café” reaches WordPiece as “naive cafe”.
The Basic split column then cuts the normalized text on whitespace, and makes every punctuation mark and symbol a word of its own; all ASCII characters that are neither letters nor digits count, so “=” does too. WordPiece works inside one of these words at a time and never looks across them.
Greedy longest match first
For each word of characters, WordPiece starts at and looks for the longest piece the vocabulary has:
It keeps , moves on to and repeats until the word is covered. If no piece starts at some position, not even a single character, the whole word becomes [UNK], and words longer than 100 characters are [UNK] without trying. The WordPiece column draws the window being looked up over the word, with the pieces kept so far, and the WordPiece loop strip plays every lookup in order. For “tokenization” it shortens the window 8 times, from the whole word down to token, then finds ##ization at once: 9 lookups. “unbelievably” takes 30 lookups to become un ##bel ##ie ##va ##bly.
Shortening the window one character at a time costs up to lookups for a word of characters. Song et al. (2021) call this MaxMatch and give LinMaxMatch, which finds the same pieces in time linear in with a trie whose failure links work like those of the Aho–Corasick string-matching algorithm. Either way the rule is greedy, not optimal: it never compares one split with another. The T5 tab scores every split and keeps the most probable one, and for the same word it chooses ▁unbe lie v ably.
The vocabulary, the special tokens and the ids
An id is simply the piece's line number in vocab.txt. Five lines are special: [PAD] 0, [UNK] 100, [CLS] 101, [SEP] 102 and [MASK] 103. Every input gets [CLS] in front and [SEP] at the end. The BERT paper makes [CLS] the first token of every sequence and uses its final state to classify the whole input; [SEP] also separates the two texts of a pair, and [MASK] replaces most of the tokens the model must guess during masked-language-model pre-training.
The Tokens → ids column lists each token and its id; clicking one opens its vocabulary entry, the entries around it, and the row of the 30,522 × 768 word_embeddings matrix that the id selects: the model knows the token only through that row. On the page's examples the 🤖 emoji and the ideograph 晴 have no piece and become [UNK]; over the 325 check strings [UNK] appears 40 times, in 27 of them.
Decoding
The Decode step maps the ids back to pieces, skips the special tokens, glues each ## piece to the one before it and puts a space before every other piece, then removes the space before “.”, “?”, “!” and “,”. It gives back the normalized text, not the input: capitals stay lower-case, accents are gone, a word that became [UNK] is dropped, and the spacing is rebuilt from the pieces, so “東京は晴れ” comes back as “東 京 は れ”. Only 39 of the 325 check strings decode back exactly. A model that has to reproduce text character for character needs a lossless tokenizer instead, such as the byte-level BPE of the GPT-2 tab.
How the vocabulary is learnt
The page shows encoding with a finished vocabulary. Schuster and Nakajima built the first wordpiece inventory to segment Japanese and Korean text for voice search: start from single characters and repeatedly add the combination of two existing units that most increases the likelihood of the training data under a language model, until the inventory reaches the size wanted. This differs from BPE, which adds the most frequent pair (see the GPT-2 tab), but both grow a vocabulary bottom-up by merging; the Unigram model of the T5 tab instead starts large and prunes. The BERT paper uses WordPiece embeddings with a 30,000-token vocabulary.
Where WordPiece came from
WordPiece was developed at Google for Japanese and Korean voice search (Schuster and Nakajima, 2012). Google's neural machine translation system (Wu et al., 2016) adopted it to translate rare words, with vocabularies of 8K to 32K wordpieces and a mark at the start of each word. BERT (Devlin et al., 2019) marked continuations with ## instead and made WordPiece the tokenizer of a generation of encoder models; Fast WordPiece (2021) made its encoding linear-time. The T5 tab shows the Unigram alternative, and the ByT5 tab drops the vocabulary altogether and reads raw UTF-8 bytes.
Papers
- Japanese and Korean voice search Introduces the wordpiece model: a vocabulary grown from characters by adding the unit that most raises the training data's likelihood.
- BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding The model whose 30,522-entry WordPiece vocabulary this tab records, with [CLS], [SEP] and [MASK].
- Google's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation Brings wordpieces to neural translation to handle rare words, comparing vocabularies of 8K, 16K and 32K pieces.
- Fast WordPiece Tokenization Names the greedy longest-match-first rule MaxMatch and computes the same pieces in linear time with a trie and failure links.
- 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 WordPiece among BPE and Unigram.