Skip to content

SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing

optionalextendedfundamentalpaperrubric 8/122018 · EMNLP 2018 (demo) · ~20 min

Taku Kudo, John Richardson

Status

TL;DR. Earlier subword tools such as subword-nmt needed text that was already split into words, which requires a different tokenizer per language and loses information about spacing. SentencePiece trains BPE or unigram-language-model subwords directly on raw sentences. It treats whitespace as an ordinary symbol (▁, U+2581), so tokenisation is reversible up to normalisation. The user specifies the final vocabulary size, and Unicode NFKC normalisation is built in. It became the default tokenizer library for many multilingual models.

Lossless tokenisation. Normalise the text, replace spaces with ▁, and segment the result into subwords. Decoding is just concatenation followed by ▁ → space:

Decode(Encode(Normalize(x)))=Normalize(x).\text{Decode}(\text{Encode}(\text{Normalize}(x))) = \text{Normalize}(x).

No language-specific rules are needed, and the spacing of the normalised string is recorded, unlike the ”@@” continuation marker of subword-nmt. SentencePiece preserves the normalised string, not necessarily the original bytes: by default it collapses repeated whitespace, so Hello world. becomes Hello world. before segmentation, and NFKC can change other characters. Exact-format applications must configure whitespace handling and normalisation explicitly and test round trips.

Engineering. BPE segmentation uses a binary heap to cost O(Nlog⁡N)O(N\log N) instead of the naive O(N2)O(N^2). Unigram-LM training and segmentation are linear in the input size. Vocabulary ids, special symbols (unk, BOS, EOS, pad, custom tags such as language indicators) and normalisation rules are stored in one self-contained model file.

Results. On Japanese–English translation (KFTT), an 8k shared SentencePiece (unigram) vocabulary beats an 80k word model: ja→en 29.55 vs 28.24 BLEU, en→ja 21.62 vs 20.06. Adding pre-tokenisation gives 29.85 for ja→en (no significant difference) but lowers en→ja to 20.86. The gains are largest when Japanese is involved, especially as the target.

  • BPE — the merge algorithm SentencePiece reimplements without pre-tokenisation.
  • T5 — a widely used model with a SentencePiece vocabulary.
  • Gemma 3 — a recent report whose tokenizer is built on SentencePiece.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Derive / compute

    Encode "Hello world." (two spaces) with a default SentencePiece model and the ▁ (U+2581) convention, decode with "join then replace ▁ by space", and state exactly what comes back. Which normaliser settings must change so that code with repeated spaces round-trips exactly, and why can subword-nmt's "@@" continuation marker not record spacing at all? Then explain why naive BPE segmentation of an N-symbol sentence costs O(N²) and how a heap brings it to O(N log N).

    What a strong answer covers
    • By default the normaliser has remove_extra_whitespaces = true, so "Hello world." is first normalised to "Hello world." and escaped as "▁Hello▁world." (add_dummy_prefix adds the leading ▁). Any segmentation decodes back to "Hello world.": Decode(Encode(x)) = Normalize(x), not x.
    • For exact-format text, set remove_extra_whitespaces = false (and usually add_dummy_prefix = false and an identity normalisation instead of NFKC). Then "Hello▁▁world." is segmented and decodes to the original; test round trips on real code.
    • The @@ scheme marks only word-internal boundaries and relies on whitespace between words, so it cannot say how many spaces there were; detokenisation is ambiguous.
    • Naive BPE rescans all adjacent pairs (O(N)) after each of up to N merges, so O(N²).
    • Keeping candidate pairs in a priority queue keyed by merge rank, and updating only the neighbours of each merge, gives O(log N) per merge and O(N log N) overall.
  2. Mechanism

    In Table 1, SentencePiece without pre-tokenisation beats SentencePiece with pre-tokenisation for English→Japanese (21.62 vs 20.86 BLEU), while the two are close for Japanese→English (29.55 vs 29.85). Explain why a Japanese word segmenter can hurt here.

    What a strong answer covers
    • Japanese has no spaces, so pre-tokenisation uses a separate segmenter (KyTea) whose word boundaries become hard constraints on the subword vocabulary.
    • Training on raw sentences lets the subword model choose units from data statistics, which can match the translation task better than linguistic word boundaries.
    • The effect is larger when Japanese is the target, because output must be generated in the constrained units, and errors in boundaries become errors in output.
    • For a space-delimited source language the constraint matters less, which is consistent with the small ja→en difference.
  3. Design

    You are building a tokenizer for a multilingual retrieval model covering English, Japanese, Hindi and code. Using SentencePiece's design choices, decide on raw-text training, normalisation, vocabulary size and algorithm, and explain how you would check that no language is badly over-segmented.

    What a strong answer covers
    • Train directly on raw text (no language-specific pre-tokenisers) so all languages are handled the same way; whitespace becomes ▁, but the default normaliser collapses repeated spaces, so for code disable remove_extra_whitespaces and verify exact round trips.
    • NFKC is the default normalisation; it folds full-width characters but can change code and some scripts, so decide per use case, and turn it off or customise it if exact strings matter.
    • Choose the final vocabulary size directly (SentencePiece fixes size rather than merge count) and sample training text so low-resource languages are not drowned out.
    • Check tokens per character (or per word) for each language on held-out text; a language with much higher fertility will have longer inputs and weaker representations.

Done with this page?

Status