SentencePiece
Most tokenisers quietly assume that someone has already split the text on spaces. That assumption fails on Japanese and Chinese, which do not use spaces at all, and it leaks into every multilingual pipeline built on top of it. SentencePiece drops the assumption: it treats input as a raw stream of characters and learns its units directly from that.
Raw text in, the same text back out
It is a trainer and a library rather than a single algorithm, and two choices define it:
- The space is encoded as an ordinary symbol — the visible marker ▁ — so a token sequence can be joined back into the exact original string. Tokenisation becomes lossless and reversible, with no per-language detokenisation rules to maintain.
- It implements both byte-pair encoding and the unigram language model method, training a vocabulary of a chosen size from plain text and writing it to one self-contained model file.
Where you meet it
T5, ALBERT, XLNet and mBART all ship a SentencePiece model; load one of them through Hugging Face Transformers and the tokeniser file you download is that model. Because vocabulary and segmentation rules travel together in a single file, text in fine-tuning is segmented exactly as it was in pre-training — the mismatch that otherwise costs accuracy silently.
Why it matters
Tokenisation sets the ceiling on everything downstream: what counts as unknown, how long a sequence becomes, and whether generated output can be reassembled into clean text. A whitespace pre-tokeniser bakes English word boundaries into a system that may later be handed Thai or German compounds. SentencePiece makes segmentation a trained, language-neutral, reversible component instead of a hand-written assumption.
SentencePiece is a language-independent subword tokeniser that trains directly on raw text with no whitespace pre-tokenisation, implementing both byte-pair encoding and unigram language model segmentation. It encodes spaces as ordinary symbols, making tokenisation fully reversible, and packs vocabulary and rules into a single model file. That enables consistent tokenisation for languages without word spacing and guarantees that pre-training and fine-tuning segment text identically.
Software does not read whole words; it reads pieces. SentencePiece is a tool that works out, from a huge pile of text, which pieces are worth keeping — common chunks like "ing" or "tion" — so that any word, even one it has never seen before, can be built from them like a shape made of Lego bricks.
Its trick is that it never assumes words are separated by spaces. That matters, because Japanese and Chinese writing has no spaces between words. It also treats the space itself as one of the pieces, so chopped-up text can be glued back together exactly as it was written — essential when the software's job is to produce text, not only to read it.