---
title: SentencePiece Model
url: https://www.emergentmind.com/topics/sentencepiece-model-spm
type: topic
---

# SentencePiece Model

SentencePiece is a language-independent subword tokenizer and detokenizer designed for neural text processing tasks, such as Neural Machine Translation (NMT). Unlike traditional subword segmentation approaches that require pre-tokenization into word sequences, SentencePiece operates directly on raw sentences in Unicode format, enabling a purely end-to-end and language-agnostic pipeline. It offers interchangeable implementations of both Byte-Pair Encoding (BPE) and the Unigram Language Model, along with an open-source C++ and Python interface, to support flexible subword vocabulary generation and reversible text processing. The complete model encapsulates all necessary normalization and segmentation rules, offering deterministic, lossless encoding and decoding for any input language [1808.06226].

## 1. System Architecture and Workflow

SentencePiece comprises four principal components: the Normalizer, Trainer, Encoder, and Decoder. The canonical workflow is as follows:

- **Normalization**: Raw text is first passed through the Normalizer, which by default applies Unicode NFKC normalization, implemented as a subset via FST/Aho-Corasick automaton. Custom normalization can be specified as a TSV mapping of codepoint sequences.
- **Training**: The normalized text is provided directly to the Trainer, bypassing any requirement for language-specific word or whitespace segmentation. Training produces a model (vocabulary and segmentation parameters) as a Protocol Buffer file.
- **Encoding**: At inference, new sentences are normalized and whitespace is escaped (using the underscore character, U+2581), then segmented into subword units through the Encoder, returning either text pieces or integer ids.
- **Decoding**: The Decoder reconstructs normalized text by concatenating the subwords and reversing underscore escaping.

Key to the design is lossless tokenization: all whitespace and character-level information is precisely preserved via the underscore mechanism, guaranteeing that
\[
\Decode(\Encode(\Normalize(\text{text}))) = \Normalize(\text{text})
\]
This property enables robust, language-agnostic handling without dependence on external segmenters [1808.06226].

## 2. Subword Segmentation Algorithms

SentencePiece supports two principal algorithms for learning and applying subword vocabularies:

### 2.1 Byte-Pair Encoding (BPE)

- **Vocabulary Construction**: Begin with all Unicode characters as atomic symbols. At each iteration, identify the most frequent adjacent symbol pair $(a, b)$ throughout the corpus and merge all such pairs to form a new symbol, thereby reducing overall input length.
- **Efficiency**: Naive BPE is $O(N^2)$ per input of length $N$ due to repetitive pair scanning; SentencePiece utilizes a binary heap for pair-frequency tracking, reducing both training and segmentation to $O(N \log N)$ per sentence.
- **Process Summary**:
  1. Initialize the vocabulary with all characters.
  2. Count all adjacent symbol-pair frequencies.
  3. Until desired vocabulary size $|V|$ is reached:
      a. Pop the highest-frequency pair from the heap.
      b. Add the merged symbol to $V$.
      c. Update affected pair counts.
- **Example**: Text “Hello world.”, normalized and with spaces escaped, becomes “_Hello_world.” for symbol merging.

### 2.2 Unigram Language Model

- **Model**: Each subword $w_i$ is generated independently under a unigram probability distribution $p(\cdot)$. For a split $x = (w_1,\dotsc,w_n)$, the likelihood is given by
  \[
  p(w_1, \dotsc, w_n) = \prod_{i=1}^n p(w_i)
  \]
- **Training**: Employs EM-based vocabulary pruning:
  1. Start with a large candidate vocabulary (e.g., all substrings up to length $L$) and initial weights.
  2. E-step: For each sentence, compute the most likely tokenizations (Viterbi or lattice sampling) under $p(w)$.
  3. M-step: Re-estimate $p(w)$ proportional to the expected counts from all tokenization paths.
  4. Prune the lowest-probability candidates until target $|V|$ is met.
- **Complexity**: Linear in corpus size per iteration. Full EM pseudocode is referenced but not specified in the primary paper [1808.06226].

## 3. Vocabulary Determination and Empirical Trade-Offs

The vocabulary size ($|V|$) is directly user-specified via the `--vocab_size` option. There is a central trade-off in vocabulary granularity:

- **Small $|V|$**: Finer splits, more tokens per input, longer sequences, increased computational cost for NMT.
- **Large $|V|$**: Coarser merges, more rare tokens, risk of data sparsity and out-of-vocabulary (OOV) effects.

Empirical evaluation on the KFTT English–Japanese corpus (GNMT architecture) demonstrates optimal BLEU performance near $|V|\approx8{,}000$ for shared vocabularies [1808.06226]. For instance, with shared 8k SPM vocab, BLEU scores were 29.55 for ja→en and 21.62 for en→ja, outperforming the word-based 80k vocabulary baseline.

## 4. Tokenization, Detokenization, and Round-Trip Integrity

- **Whitespace Handling**: All spaces are consistently escaped as the meta-symbol “_” before any segmentation. For example, “Hello world.” is normalized as “_Hello_world.”.
- **Encoding**:
  1. Normalize input text.
  2. Escape spaces with underscores.
  3. Segment using the chosen subword algorithm (BPE or unigram LM).
  4. Optionally, map subwords to integer ids for inference or training.
- **Detokenization/Decoding**: Subword tokens are concatenated, and underscores are mapped back to spaces, perfectly reconstructing the normalized text.
  
A canonical round-trip command sequence is:
```
spm_train --model_prefix=spm --vocab_size=1000
spm_encode --model=spm.model --output_format=piece   # Outputs: _He ll o ▁world .
spm_encode --model=spm.model --output_format=id      # Outputs: 151 88 21 887 6
spm_decode --model=spm.model --input_format=piece    # Outputs: Hello world.
```
This lossless mechanism is applicable to any language, including those without explicit whitespace segmentation [1808.06226].

## 5. Implementation Characteristics and Interface

SentencePiece provides a self-contained, portable model file storing vocabulary, segmentation rules, and normalization FST as a Protocol Buffer artifact. Notable aspects include:

- **Normalizer**: Ships with default NFKC normalization, with support for user-supplied TSV normalization rules.
- **APIs**: Offers command-line utilities (`spm_train`, `spm_encode`, `spm_decode`), C++ and Python bindings, and integration with TensorFlow, all sharing a native backend.
- **Common Flags**:
  - `--input=<file>`
  - `--model_prefix=<prefix>`
  - `--vocab_size=<int>`
  - `--normalization_rule_name=[nfkc|identity|…]`
  - `--normalization_rule_tsv=<custom.tsv>`
  - `--model_type=[bpe|unigram|char|word]`
- **On-the-Fly Utility**: Enables dynamic subword regularization via sampling (citing [Kudo, 2018]), supporting on-the-fly data augmentation scenarios [1808.06226].

## 6. Experimental Evaluation and Comparative Analysis

SentencePiece was evaluated on English–Japanese translation using KFTT, focusing on BLEU scores and segmentation speed. The key findings:

- **BLEU Score**:
  - ja→en:
    - Word 80k: 28.24
    - SPM 8k shared: 29.55
    - SPM with pre-tokenization: 29.85
  - en→ja:
    - Word 80k: 20.06
    - SPM 8k shared: 21.62
    - SPM with pre-tokenization: 20.86

- **Segmentation Speed** (440,000 sentences; Xeon 3.5 GHz):
  - On raw Japanese sentences:
    - subword-nmt (BPE): 528 s
    - SentencePiece: 217 s
    - SPM achieves ≈380× faster performance on Japanese text
  - On English sentences, performance of SPM and subword-nmt is comparable

SentencePiece offers advantages including the absence of language-specific pre-tokenizers, guaranteed reversibility, and reproducibility, alongside performance gains in direct segmentation of non-whitespace-delimited scripts [1808.06226].

## 7. Distinctive Properties and Research Context

- **Language Independence**: No requirement for pre-tokenization, making it suitable for scripts lacking explicit word boundaries (e.g., Japanese, Chinese).
- **Lossless, Reversible Segmentation**: Ensured by explicit whitespace management and self-contained normalization; relevant for reproducible NMT experiments.
- **Reproducibility**: Protocol Buffer model format encapsulates all segmentation and normalization logic.
- **Subword Regularization**: APIs facilitate sampling of alternate segmentations per [Kudo, 2018], supporting robustness in model training.
- **Research Foundations**: Builds upon BPE [Sennrich et al., ACL 2016], extended with the Unigram LM [Kudo, ACL 2018] for probabilistic segmentation and subword regularization [1808.06226].

SentencePiece is thus positioned as a general, language-agnostic toolkit that subsumes and extends prior approaches to subword segmentation in neural text processing, with demonstrated empirical advantages and comprehensive tooling for research and deployment [1808.06226].

Source: https://www.emergentmind.com/topics/sentencepiece-model-spm