Sub topic 6
Byte-Level BPE and Why It Never Needs an Unknown Token
Prerequisites and the central idea
This chapter assumes that you know what a token is, can read basic Python, and understand that ordinary BPE learns frequent adjacent symbol pairs. Byte-level BPE applies the same merge idea to bytes rather than to pre-split words or Unicode characters.
Text is stored as bytes using an encoding such as UTF-8. UTF-8 represents every Unicode character with one or more bytes. Byte-level BPE starts with these bytes, learns frequent byte-pair merges, and uses the resulting vocabulary to encode new text.
The starting alphabet contains all 256 possible byte values. Every input byte therefore has a representation, including bytes from rare scripts, emojis, unusual punctuation, and arbitrary binary-looking sequences.
Why ordinary subword BPE can produce unknown tokens
A conventional BPE tokenizer often begins with characters or pre-tokenized word symbols. Its vocabulary might contain pieces such as un, ##usual, or ing. If a new string cannot be covered by the available pieces, the tokenizer emits an unknown token such as [UNK].
This loses information. The model can see that an unknown token occurred, but it cannot recover the original spelling from that token alone.
Byte-level BPE avoids this problem because every Unicode string can first be converted to UTF-8 bytes, and every byte belongs to the initial alphabet.
| Tokenization method | Initial units | Can represent every UTF-8 input? | Typical unknown behavior |
|---|---|---|---|
| Character BPE | Characters or pre-tokenized symbols | Not always | Emits an unknown token |
| WordPiece | Vocabulary pieces | Not always | Often emits `[UNK]` for a whole word |
| Byte-level BPE | UTF-8 bytes | Yes | No unknown token is required |
Encoding and decoding
The encoder first converts text to UTF-8 bytes. It then applies learned merges in their recorded order. A merged token represents a sequence of bytes, not necessarily a complete character or word.
For example, the text café becomes the byte sequence:
63 61 66 c3 a9The final two bytes encode é. A tokenizer may keep them separate or merge them into one token, depending on training frequencies. Either choice is valid because decoding concatenates the represented bytes and then interprets the result as UTF-8.
text = "café 🚀"
raw = text.encode("utf-8")
print(list(raw))
print(raw.decode("utf-8"))
# A byte-level tokenizer can start with one token per byte.
tokens = list(raw)
reconstructed = bytes(tokens).decode("utf-8")
print(reconstructed)The byte alphabet does not mean that the model always receives one token per byte. Frequent sequences, such as common words or word fragments, are merged into larger tokens. Rare sequences fall back to smaller byte tokens.
Trade-offs and practical details
The main advantage is coverage. The tokenizer does not need a special unknown token, and encoding followed by decoding can be lossless when the same vocabulary and rules are used.
The main cost is sequence length. A rare character may require several byte tokens. A token budget that handles English efficiently may handle some languages less efficiently. A large vocabulary can reduce this cost by storing frequent multilingual byte sequences as single tokens, but it also increases the model's embedding and output layers.
Byte-level systems also need a visible representation for bytes that are awkward to display, including spaces and control values. Some implementations map bytes to printable characters internally. Those display symbols are tokenizer conventions; they do not change the underlying byte sequence.
When to choose byte-level BPE
Use byte-level BPE when lossless coverage of varied user text matters, especially for multilingual text, source code, URLs, and noisy input. Check its average sequence length on your own data. A tokenizer that never emits [UNK] can still be inefficient if it represents common text with too many tokens.
Exercise
Why can byte-level BPE encode an emoji that never appeared in its training corpus?
Show answer
The emoji is converted to one or more UTF-8 bytes, and every possible byte is already in the tokenizer's base alphabet. The tokenizer may use several byte tokens instead of a learned merged token, but it does not need an unknown token.