Notes

WordPiece

WordPiece is the tokeniser behind BERT, and it answers the same question as byte-pair encoding — how to cover any word from a fixed vocabulary of fragments — with a different idea of what makes a fragment worth keeping.

Merge by likelihood, not frequency
Both schemes start from single characters and build a vocabulary by merging pieces. BPE merges whichever adjacent pair occurs most often. WordPiece merges the pair that most increases the likelihood of the training corpus under a unigram language model over the pieces — in effect, the pair whose joint count is high relative to how common its parts are on their own, so it prefers a genuinely informative piece over one that was always going to co-occur anyway. Encoding is then greedy longest-match from the left of each word, with continuation pieces marked: tokenization arrives as token plus ##ization, where ## means this attaches to what came before.

Where you meet it
Google introduced it for Japanese and Korean segmentation, then used it in its neural translation system; it reached everyone through BERT, whose 30,000-piece uncased vocabulary is a WordPiece vocabulary, and through DistilBERT, ELECTRA and mBERT after it. The consequences matter more than the algorithm:

  • no UNK token for ordinary text — an unfamiliar surname or drug name decomposes into pieces and still gets represented;
  • vocabulary and model are one unit, so swapping in a different tokeniser leaves the embedding rows meaningless;
  • token counts are not word counts, which is what inflates sequence length for domain jargon and for languages the vocabulary was not fitted on.

WordPiece is a subword tokenisation scheme that builds a fixed vocabulary of word fragments by repeatedly merging the pair of pieces that most increases the likelihood of the training corpus, rather than the most frequent pair as BPE does. Encoding is greedy longest-match, with continuation pieces prefixed ##. It gives BERT and its descendants full coverage of an open vocabulary with no UNK token, at the cost of tying the model to that exact vocabulary.

A spell-checker that only knows whole words is helpless the first time it meets a new medicine name, or your surname. The fix is to keep a box of word fragments instead of a list of whole words, so that anything at all can be spelled out of pieces already in the box.

WordPiece is one way of deciding which fragments go in the box. It reads a mountain of text and keeps the pieces that do the most to explain that text, rather than simply the pieces that turn up most often.

It is the method behind BERT, one of the models that made modern language software work. Thanks to it, the system never has to shrug at a word it has not seen before.