Paper commentary
SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing
SentencePiece learns subword tokens directly from raw text without language-specific preprocessing
- Problem Neural machine translation still depends on hand-crafted, language-dependent preprocessing. Existing subword tools need text already split into words, which non-segmented languages like Japanese lack.
- Method SentencePiece trains subword models directly on raw sentences, treating spaces as ordinary symbols. Its decoder then rebuilds the text by joining pieces and replacing the space marker.
- Results SentencePiece reaches accuracy comparable to subword training on pre-tokenized text for English-Japanese translation. On English to Japanese it scored 21.62 against 20.06 for the word baseline.
Why pre-tokenization gets in the way
Neural machine translation can in principle map source text to target text end to end, yet many systems still lean on pre- and postprocessors inherited from statistical MT. Moses offers such a pre- and postprocessor, but it rests on hand-crafted, language-dependent rules whose usefulness for NMT has not been proven. Its tools target European languages, where whitespace already separates the words.
For Chinese, Korean and Japanese, a separate word segmenter has to run before training.
The cost isn't limited to one toolchain, because multilingual models force teams to manage preprocessor settings for each language. The network itself stays language-independent, so the per-language preprocessing is the part that varies.
Existing subword tools such as subword-nmt train only from pre-tokenized sentences. That assumption is hard to meet for non-segmented languages, where pre-tokenization can't always be assumed. Pre-tokenization also makes lossless tokenization difficult to achieve. SentencePiece instead trains directly from raw sentences, using either BPE or the unigram language model.
Working on raw data at scale calls for speed-up techniques, which SentencePiece applies to both training and segmentation. For a sentence of length N, naive BPE segmentation costs O(N 2) per iteration when it scans every symbol pair. SentencePiece instead manages merged symbols in a binary heap, which gives O(N log(N )) cost. The unigram language model's training and segmentation complexity is linear in the size of the input data.
Treat the text as plain characters
SentencePiece skips word splitting and treats the raw input as a sequence of Unicode characters, with the whitespace kept as a normal symbol. Before segmentation, the whitespace is escaped with the meta symbol _ (U+2581), so the subword pieces record where the spaces were. Because the whitespace survives tokenization, detokenization reduces to a join followed by a replace, with no language rules.
The Python line detok = ''.join(tokens).replace('_', ' ') restores the spaces without ambiguity.
The authors call this design lossless tokenization, because the encoder's output keeps all the information needed to rebuild the normalized text. Earlier language-dependent tokenizers, such as the example that turns Hello world. into [Hello] [world] [.], lose the fact that no space sat before the period. Subword-nmt instead marks word-internal boundaries with the @@ marker, and that representation can't always keep consecutive whitespaces apart.
SentencePiece implements Decode(Encode(Normalize(text))) = Normalize(text), so the Decoder inverts the Encoder.
The Encoder runs the Normalizer on the input and segments the text into subwords with the trained model. The Encoder also maps the pieces to ids, so NMT systems receive id sequences directly. The Decoder converts the subword sequence back into the normalized text, which makes the two sides a reversible pair. Subword models can be learned from raw sentences, so no pre-tokenization stage is needed before training.
How a model is trained and applied
Normalizer runs first, and by default it applies NFKC normalization to the input text. Custom rules can be supplied as a TSV file, and the authors apply the longest rule when two rules conflict. The rules are compiled into a finite state transducer, an Aho-Corasick automaton, which keeps matching efficient. The authors implement only a subset of NFKC, because full canonical combining class reordering is hard to model in such a transducer.
Trainer learns the subword model from the normalized corpus, and the vocabulary size is set directly with the --vocab_size flag. Subword-nmt instead specifies a number of merge operations, which doesn't carry over to unigram language models. Naive BPE segmentation scans symbol pairs in every iteration and costs O(N 2) for an input of length N.
SentencePiece keeps the merged symbols in a binary heap, which gives an O(N log(N )) algorithm. The training and segmentation costs of the unigram language model grow linearly with the size of the input data.
The model file stores the vocabulary, the segmentation parameters and the pre-compiled finite state transducer for normalization. Because the behavior depends only on that file, the authors argue it gives reproducibility and can ship as part of an NMT model. The file is stored as a Protocol buffer, which keeps serialized data backward compatible as it grows.
Encoder runs Normalizer on the input and then tokenizes it with the trained model. Encoding also maps the pieces to ids, so the input text becomes an id sequence directly. Decoder converts the id sequence back into the normalized text.
Running SentencePiece on the fly through its library API
Offline preprocessing runs as a separate step before NMT training, so raw input is converted to ids in advance. Standalone tools aren't directly integrated into user-facing NMT applications that must preprocess user input on the fly. Offline runs also make sub-sentence data augmentation and noise injection hard to employ, and these techniques aim at improving accuracy and robustness.
Several studies inject noise by randomly changing the internal representation of sentences, and that dynamic sampling is hard to emulate with offline processing alone. SentencePiece therefore provides a C++, Python and TensorFlow library API, so the same processing can run inside existing NMT frameworks.
The C++ example trains a model, loads it with sp.Load, and encodes "Hello world." into pieces or ids. The Python example produces the same pieces and ids, and the TensorFlow example runs the encoder inside a graph. The authors note that the Python and TensorFlow wrappers call the native C++ API, so there is no performance drop.
Subword regularization samples one subword sequence from the unigram language model on each call. Kudo (2018) proposed subword regularization, which randomly changes the subword segmentation during NMT training. In the example, the text "New York" comes out as several different piece sequences across five calls to SampleEncodeAsPieces. The authors defer the details of subword regularization and its sampling hyperparameters to Kudo (2018).
Did preprocessing choices change translation quality
The KFTT English-Japanese setup supplies the Wikipedia training data, with 440k training sentences, 1166 development sentences and 1160 test sentences. The authors trained GNMT with the LSTM node size changed to 512 and the layer size changed to 6. A word model with an 80k/80k vocabulary serves as the baseline for every comparison.
Adding pre-tokenization gave 29.85, a gap the authors call marginal with no significant difference. So, for this direction, subwords learned from raw text do about as well as pre-tokenized ones.
For English to Japanese, the word baseline scored 20.06 and SentencePiece 8k shared scored 21.62. With pre-tokenization, the score fell to 20.86, so pre-tokenization hurt in this direction. The authors attribute this to pre-tokenization acting as a strong constraint on the Japanese vocabulary. They argue that unsupervised segmentation from raw input finds domain-specific Japanese vocabulary more effectively.
SentencePiece/Word with 8k/80k reached 29.14 in that direction. In en→ja, the Word/SentencePiece mix scored 21.41 and SentencePiece/Word scored 19.94. The largest gains appear when SentencePiece is applied to Japanese and the target sentence is Japanese.
Speed of training and segmentation
Table 2 measures training and segmentation time on the KFTT corpus of 440k sentences, with a vocabulary of 16k. The authors ran these experiments on Linux with Xeon 3.5Ghz processors. On English, subword-nmt and SentencePiece take almost comparable time whether or not they pre-tokenize. The authors expect this because English is a segmented language, so the vocabulary search space is largely restricted.
The gap appears on raw Japanese, where SentencePiece trains in 217.3 sec without pre-tokenization. subword-nmt needs 528.0 sec for the same training setting without pre-tokenization. For segmentation of raw Japanese, SentencePiece takes 5.9 sec, while subword-nmt takes 216.2 sec. The text claims that SentencePiece is about 380 times faster than subword-nmt in this setting.
The table values don't produce that factor, so the two sources disagree, and I quote the table figures here.
SentencePiece segments at around 21k sentences/sec on English and around 74k sentences/sec on Japanese. The authors take that speed as fast enough to run on the fly, which supports skipping pre-tokenization. Table 2 compares BPE in SentencePiece against subword-nmt, because subword-nmt is based on BPE. The authors found that BPE and unigram language models perform almost comparably.
Limits of the evidence
The translation experiments cover only English-Japanese translation of Wikipedia articles from the KFTT data. They use one NMT system, GNMT, and the authors changed the LSTM node and layer sizes to 512 and 6. Table 1 reports one BLEU value per configuration, so no spread across runs appears. They didn't test any language pair beyond English and Japanese, so the language-independence claim stays untested for other languages.
SentencePiece implements only a subset of NFKC normalization. It doesn't handle the full canonical combining class reordering, which the authors say is difficult to encode in a finite state transducer. The authors warn that NFKC results can shift with the Unicode version.
The speed numbers in Table 2 come from one hardware setup: Linux with Xeon 3.5Ghz processors. All those timings use a vocabulary of 16k, so they don't show how the times change at other vocabulary sizes. They measure only the KFTT corpus of 440k sentences.
The speed comparison uses subword-nmt as the only baseline tool. The paper measures training and segmentation time, not the end-to-end training time of an NMT system.
Paper info
- Authors
- Taku Kudo, John Richardson
- Venue
- arXiv · Preprint
- First published
- 2018-8-19
- Added
- 2026-10-20
- Updated
- 2018-8-19
- Version
- v1
- arXiv
- 1808.06226
- Category
- cs.CL
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Taku Kudo, John Richardson (2018). SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing. arXiv:1808.06226v1. https://arxiv.org/abs/1808.06226v1
BibTeX
@misc{kudo2018sentencepiece,
title = {{SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing}},
author = {Taku Kudo and John Richardson},
year = {2018},
eprint = {1808.06226},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1808.06226v1}
}