Bytes instead of a vocabulary
Subword tokenizers such as GPT-2's BPE or T5's SentencePiece learn a vocabulary from a training corpus, and text unlike that corpus (a rare word, another script, a typo) falls apart into odd pieces or an unknown token. ByT5 drops the learned vocabulary. It writes the text as UTF-8 and makes every byte one token, so any string in any language has exactly one encoding and nothing is ever unknown. The model is T5's encoder–decoder transformer with very few changes, pre-trained on mC4, the same multilingual corpus as mT5.
The page records google/byt5-small: 3 special ids, 256 byte ids and 125 extra ids, 384 in all, an embedding width , and 12 encoder plus 4 decoder layers. The strip at the top, Text → UTF-8 bytes → ids, puts each character of the input over its bytes, then each byte's id, its embedding row and GPT-2's tokens over the same bytes.
UTF-8
UTF-8 writes each Unicode code point as one to four bytes. The leading ones of the first byte, the lead byte, say how many bytes the character takes; each following continuation byte starts with 10; the remaining bits, read in order, are the code point:
- U+0000 to U+007F:
0xxxxxxx, one byte, the same as ASCII - U+0080 to U+07FF:
110xxxxx 10xxxxxx - U+0800 to U+FFFF:
1110xxxx 10xxxxxx 10xxxxxx - U+10000 to U+10FFFF:
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
The UTF-8 card spells this out for the selected character, with the fixed prefix bits in grey and the code-point bits in purple. ï (U+00EF) is C3 AF, 東 (U+6771) is E6 9D B1 and the robot emoji (U+1F916) is F0 9F A4 96, so 'naïve café 🤖' is 12 characters but 17 bytes, and the five characters of 東京は晴れ are 15. Because lead and continuation bytes look different, a reader can always find where a character starts. Some byte values never occur in valid UTF-8 at all: C0 and C1, which could only begin an over-long encoding, and F5 to FF, which would encode code points beyond U+10FFFF.
Ids and the embedding table
The ids are the bytes shifted by three, which keeps 0, 1 and 2 for padding, the end-of-sequence token and an unknown token. The tokenizer appends the end token, id 1, once after the last byte:
Since every byte has an id, the unknown id is never produced. The Id → embedding row card looks the id up in the model's single table of numbers, read by the encoder and the decoder alike; T5 adds no position vector, because its attention carries a relative position bias instead. The table holds 0.57 M parameters, against 38.6 M in GPT-2's . The ByT5 paper counts the vocabulary and softmax matrices of mT5-Base as about 66 % of its parameters; ByT5 spends them on the transformer layers instead, and makes the encoder three times as deep as the decoder, hence the 12 and 4 layers here.
Rows that never trained
The Vocabulary table lays the 384 ids out by byte value and shades each by the norm of its embedding row. A row of 1472 numbers drawn from has norm close to , so a row still near that norm looks untouched by training. The page hatches the 131 such rows: the unknown id 2, the carriage return 0D, the never-valid bytes C0, C1, F5 and F6, and all 125 extra ids, 259 to 383.
The other never-valid bytes, F7 to FF, did train. ByT5 is pre-trained with T5's span corruption: spans of the input, 20 bytes long on average in ByT5, are each replaced by a sentinel id, and the decoder writes the missing spans back, each after its sentinel. Rather than add new ids for the sentinels, ByT5 reused the final 100 byte ids, so rows of byte values that real text never contains still learned. T5's span corruption numbers the sentinels down from the last id, one per span, so an example only ever uses as many of them as it has spans; that fits the table, where the top nine, F7 to FF, trained and F5 and F6, just below them, did not. transformers' tokenizer lists 125 extra ids beyond the bytes all the same; in this checkpoint their rows look unused.
Longer sequences
GPT-2 also starts from bytes, but merges frequent runs of them into a vocabulary of 50,257 tokens (see the GPT-2 tab). The GPT-2 on the same bytes card and the strip's GPT-2 row show its tokens over ByT5's columns, and the bars at the bottom count both. With the end token, ByT5 needs 38 ids where GPT-2 needs 7 tokens for the English sentence, 13 against 4 for 'unbelievably', 18 against 6 for 'naïve café 🤖', 16 against 9 for the Japanese and 14 against 7 for the indented code. GPT-2's merges do not respect characters either: the emoji's four bytes end up in three tokens, and none of them decodes to valid text on its own.
Self-attention costs grow with the square of the sequence length, so bytes are expensive. The ByT5 paper pre-trains on 1,024 bytes per example and finds ByT5 slower than mT5 at inference, by up to about ten times on its tasks. In return it degrades much less than mT5 under noise such as random capitalisation and does better on tasks that depend on spelling or pronunciation.
Decoding
Decoding undoes the encoding: drop the end token and every id that is not a byte, subtract 3, and read the bytes as UTF-8:
The strip Decoding, one id at a time runs this over every prefix of the ids. A prefix can stop inside a character, after its lead byte but before its last continuation byte; the tokenizer decodes with errors="ignore", so the unfinished character is left out until its last byte arrives, and the page shows its bytes so far in a dashed box. The same rule silently drops any invalid bytes a model might generate. On all five inputs the full ids decode back to exactly the input text.
Where byte models came from
Character-level language models are older than subword vocabularies; Al-Rfou et al. trained a 64-layer character transformer in 2018. In 2021 CANINE read characters and shortened the sequence by downsampling before a deep transformer, and ByT5 showed that a plain T5 can read bytes directly once the encoder is made deeper. The price is the length, and that is what later byte models attack: MEGABYTE and the Byte Latent Transformer group bytes into patches and run the large model once per patch (see the BLT tab). Mielke et al. survey the path from words to subwords, characters and bytes.
Papers
- ByT5: Towards a token-free future with pre-trained byte-to-byte models Introduces ByT5: T5 on raw UTF-8 bytes with a deeper encoder, sentinels taken from the last byte ids, and its costs and robustness against mT5.
- mT5: A massively multilingual pre-trained text-to-text transformer The multilingual SentencePiece model and the mC4 corpus that ByT5 is trained on and compared with.
- Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer T5: the encoder–decoder architecture with relative position bias and the span-corruption objective with sentinels that ByT5 keeps.
- UTF-8, a transformation format of ISO 10646 The UTF-8 standard: the lead and continuation byte patterns of the UTF-8 card and the byte values that never occur.
- Language Models are Unsupervised Multitask Learners GPT-2 and its byte-level BPE, the tokenizer the page runs on the same bytes.
- CANINE: Pre-training an Efficient Tokenization-Free Encoder for Language Representation A tokenization-free encoder over characters that downsamples the sequence before a deep transformer, contemporary with ByT5.
- Character-Level Language Modeling with Deeper Self-Attention A 64-layer transformer language model over characters, an early sign that transformers can work without subwords.
- Between words and characters: A Brief History of Open-Vocabulary Modeling and Tokenization in NLP Survey of tokenization from words to subwords, characters and bytes, with byte models in context.