Tokenization turns text into the integer ids a model reads: the text is cleaned, cut into pre-tokens, joined by learned merge ranks into tokens, looked up in a vocabulary and framed with special tokens. It is light work, but it decides how many tokens a text costs and, at scale, it can starve the model of data or disagree with itself between training and inference.
How a sentence is cut into tokens
Raw text is cleaned, cut into pre-tokens by a fixed pattern, and each pre-token is joined into tokens by repeatedly merging the adjacent pair with the best learned rank.
- The normalizer desk cleans the raw text, with lowercasing or Unicode normalization, before anything is cut.
- The pre-token splitter cuts the text into pre-tokens with a fixed pattern; no merge crosses a cut.
- The pair-rank scale picks the adjacent pair with the best learned rank inside one pre-token.
- The merge bench joins that pair into one piece, and the scale looks again until no ranked pair remains.
- 64 bytes per operation
- How many bytes a hand-written splitter that uses SIMD instructions decides in one register operation, in the maintainers' own description Hugging Face
- 1.0 to 1.35 times
- How many more tokens the same input can map to after a lab changed its tokenizer, by content type, reported by the lab Anthropic
- 1.4x English, 1.33x Spanish, 1.28x Python
- How much more a developer measured the same text to cost after the tokenizer change, by language, on his own counter Simon Willison
- A tokenizer converts text into the list of integers a model reads, in four stages: normalization, pre-tokenization, the model stage and post-processing. Hugging Face
- Normalization applies operations such as lowercasing or Unicode normalization to the raw text. Hugging Face
- Byte-level BPE models use a regular expression to split the text into pre-tokens; merges happen inside a pre-token and never across the boundary between two of them. Hugging Face
When it breaksThe new vocabulary that made the same text cost more. A lab released a model with an updated tokenizer to improve performance. The same input text mapped to more tokens, roughly 1.0 to 1.35 times depending on the content, and a developer who counted his own documents found about 1.4 times for English and nearly none for Chinese. Since price is per token, the same page cost more. Anthropic
How tokens become ids a model reads
Merged tokens are looked up in the vocabulary, wrapped in the special tokens the model expects, and handed to the GPUs in batches. This is the part that can starve the model, or disagree with itself between training and serving.
- The vocabulary drawers map each merged token to its integer id.
- The special-token stamper adds the markers the model expects around the ids, so every row has the same frame.
- The id-row hopper hands framed rows of ids to the GPUs, which must never wait on it.
- 3 to 30 times faster
- How much faster a rewritten tokenizer encodes than the previous version with one thread on one laptop chip, across ten model families, reported by its maintainers; the ids are unchanged Hugging Face
- 76% of linear
- How well the rewritten tokenizer scales across eight workers Hugging Face
- 72,000 time steps
- How many time steps an eight-word spoken sentence of about three seconds takes as raw 24 kHz audio, which is why audio is tokenized before a model reads it Neil Zeghidour
- millisecond scale or less
- How long tokenizing takes per request in a serving engine, in the speaker's experience Charles Frye
- A serving engine wraps the model with a little pre- and post-processing, tokenizers before the scheduler and a detokenizer after it. Charles Frye
- A serving engine can run several tokenizer processes but one detokenizer, because input tokens arrive at a much higher rate than output tokens are produced. Charles Frye
- Tokenizing happens once per request, off the core model loop; the speaker says latency is mostly prefill and decode. Charles Frye
When it breaksThe tokenizer that starved the model. The maintainers of a widely used tokenizer library say tokenizing has not historically been the bottleneck. But as models get faster, training on massive datasets, serving many concurrent requests or processing long inputs repeatedly can press on the tokenizer until the model waits for its data, so GPUs idle while the CPU cuts text. Hugging Face
Open questionIs the tokenizer a bottleneck worth tuning?
- As models get faster and workloads scale, training on huge datasets, many concurrent requests or long inputs can press on the tokenizer until it starves the model of data. Hugging Face
- Existing tokenizers generally do not show up as a bottleneck for models of about twenty billion parameters or more; it can show up with a smaller model and a very large input context. Charles Frye