# SentencePiece

**SentencePiece** 是一个开源、与语言无关的子词分词器与去分词器，用于神经文本处理，由谷歌的工藤拓（Taku Kudo）与约翰·理查森（John Richardson）于 2018 年发布，同时发表了 EMNLP 2018 演示论文。它直接从原始句子训练子词模型，无需预先分词，并以优化的 C++ 实现了字节对编码（BPE）与一元语言模型切分算法，提供 Python、C++ 及其他语言的绑定。<sup>[1](https://arxiv.org/abs/1808.06226)</sup><sup> • </sup><sup>[2](https://github.com/google/sentencepiece)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 2018 年由工藤拓与约翰·理查森发布；EMNLP 2018 演示论文；Apache 2.0 许可证<sup>[1](https://arxiv.org/abs/1808.06226)</sup><sup> • </sup><sup>[2](https://github.com/google/sentencepiece)</sup> |
| 核心设计 | 将输入视为原始 Unicode 流；无需摩西、MeCab 或 KyTea 等语言特定的预分词器<sup>[2](https://github.com/google/sentencepiece)</sup> |
| 空白处理 | 空格用元符号 ▁（U+2581）转义并纳入词表，使去分词成为无损的字符串拼接<sup>[2](https://github.com/google/sentencepiece)</sup> |
| 算法 | BPE、一元语言模型，以及字符模型与词模型；通过 `SentencePieceTrainer` 训练<sup>[3](https://github.com/google/sentencepiece/blob/master/python/tokenizer_comparison_cheat_sheet.md)</sup> |
| 性能 | 切分速度约每秒 50,000 句，内存占用约 6 MB<sup>[2](https://github.com/google/sentencepiece)</sup> |
| 未知字符处理 | `<unk>` 词元或 `byte_fallback`，后者将未知字符分解为 UTF-8 字节词元，如 `<0xE3>`<sup>[3](https://github.com/google/sentencepiece/blob/master/python/tokenizer_comparison_cheat_sheet.md)</sup><sup> • </sup><sup>[4](https://sentencepiece.org/concept/unigram-vs-bpe-params/)</sup> |
| 知名用户 | T5（一元模型）、Llama（BPE）、Gemma 3（BPE，256k 词表）<sup>[5](https://www.kunwar.page/The_Holy_Grail.pdf)</sup><sup> • </sup><sup>[6](https://github.com/google/sentencepiece/blob/master/doc/performance_benchmark.md)</sup> |

## 为何将文本视为原始流

多数子词切分工具假定输入已被切分为词，通常依据空白或语言特定的切分器。这一假定对中文、日文、泰文等没有明确词边界的语言并不成立，且使流水线依赖可能引入或破坏信息的预处理。SentencePiece 则不同，它将输入视为原始 Unicode 字符序列，直接从原始句子训练词表，原论文称这实现了纯端到端、与语言无关的系统。<sup>[1](https://arxiv.org/abs/1808.06226)</sup>

**无损往返。** 传统分词器常丢弃空白信息，导致去分词有歧义且依赖语言。SentencePiece 用元符号 ▁（U+2581）转义空格并将其纳入词元序列，因此解码只是简单的拼接替换操作，可精确重建原文。<sup>[2](https://github.com/google/sentencepiece)</sup> 默认还会在每个句子前添加一个哑空白前缀，使句首词与句中词以相同方式分词。<sup>[7](https://google.github.io/sentencepiece/doc/normalization/)</sup>

## 规范化与训练选项

规范化总是在分词之前运行，词表从规范化文本学习并应用于规范化文本。默认规则为 `nmt_nfkc`，即 Unicode NFKC 规范化的自定义变体，附加了空格相关规范化，如合并冗余空格、转换全角空格。预定义替代方案包括普通 `nfkc`、大小写折叠变体 `nmt_nfkc_cf` 与 `nfkc_cf`，以及不进行任何规范化的 `identity`。支持自定义字符串到字符串规则，编译后的规则直接嵌入生成的 `.model` 文件，使训练与推理在任何环境下应用相同的规范化。<sup>[7](https://google.github.io/sentencepiece/doc/normalization/)</sup>

自包含的模型文件是刻意的设计点：它同时携带规范化规则、词表映射与切分模型，因此同一文件在 C++、Python、Go 或其他绑定中产生相同的分词结果。<sup>[2](https://github.com/google/sentencepiece)</sup>

训练过程暴露了对词片形成方式的约束。`split_by_whitespace`（默认 true）防止单个子词片跨越空白边界，`allow_whitespace_only_pieces` 控制完全由空白构成的词片是否有效，`pretokenization_delimiter` 可标记任何词表词片都不得跨越的硬边界，在一元模型与 BPE 模型中均适用。<sup>[8](https://github.com/google/sentencepiece/blob/master/doc/piece_constraints.md)</sup>

## 未知字符与字节回退

超出配置字符覆盖范围的字符通常映射为 `<unk>` 词元。当 `byte_fallback=true` 时，SentencePiece 改为将此类字符分解为 UTF-8 字节词元，如 `<0xE3>`，文档称这完全避免了 `<unk>` 词元，并强烈推荐现代语言模型采用。<sup>[4](https://sentencepiece.org/concept/unigram-vs-bpe-params/)</sup>

## 子词正则化

为提升训练鲁棒性，SentencePiece 支持动态子词采样：同一输入文本在每个训练步骤可被切分为不同形式。子词正则化适用于一元模型，BPE-Dropout 适用于 BPE，该库将其呈现为一种数据增强，使模型对拼写变化与噪声更具韧性。<sup>[2](https://github.com/google/sentencepiece)</sup>

## 相对于其他分词器的性能

项目仓库维护的基准在来自 FLORES-200 的平衡原始多语言数据集（英语、简体中文、日语、泰语；11.29 MB，60,720 行，中日韩及泰文文本中未人为添加空格分隔符）上比较 SentencePiece 与 [Hugging Face](https://www.edgepedia.org/zh/hugging-face) Fast 分词器及 Tiktoken。在 T5 一元模型（32k 词表）上，SentencePiece 单线程编码速度为 27.41 MB/s，Hugging Face 为 3.78 MB/s；24 线程时达到 127.60 MB/s 对 31.49 MB/s。在 Gemma 3 BPE 模型（256k 词表）上，单线程为 7.44 MB/s 对 3.66 MB/s，24 线程为 52.43 MB/s 对 20.48 MB/s。<sup>[6](https://github.com/google/sentencepiece/blob/master/doc/performance_benchmark.md)</sup>

**扩展瓶颈。** 在所有测试库中，吞吐量均不随线程数线性扩展。核心算法（SentencePiece 用 C++，Hugging Face 用 Rust）在释放全局解释器锁（GIL）的情况下并行运行，但生成的本地数组随后必须在主线程上顺序转换为 Python 对象，因为创建 Python 对象需要获取 GIL。在高线程数下，这一转换步骤成为主导。Hugging Face 更早饱和，因为它为每句构建更重的 Python `Encoding` 包装对象，而 SentencePiece 直接转换为嵌套整数列表。<sup>[6](https://github.com/google/sentencepiece/blob/master/doc/performance_benchmark.md)</sup> 项目的能力矩阵指出，SentencePiece 在几乎所有情况下都释放 GIL，包括单条与批量编码解码，并支持 Python 自由线程，而基于 Rust 的库仅在核心执行期间释放 GIL。<sup>[3](https://github.com/google/sentencepiece/blob/master/python/tokenizer_comparison_cheat_sheet.md)</sup>

能力比较还划定了三个库的分工：tiktoken 是仅推理的 BPE 编码器，不支持训练；Hugging Face `tokenizers` 覆盖 BPE、WordPiece、一元模型与 WordLevel，支持训练；SentencePiece 从原始文本训练 BPE、一元、字符与词模型。<sup>[3](https://github.com/google/sentencepiece/blob/master/python/tokenizer_comparison_cheat_sheet.md)</sup><sup> • </sup><sup>[9](https://www.dreaming.press/posts/tiktoken-vs-sentencepiece-vs-huggingface-tokenizers.html)</sup>

## SentencePiece Lite

面向移动应用、边缘设备与端侧推理，仓库包含 SentencePiece Lite，一个用现代 C++20 编写、零第三方依赖的运行时，移除了对 Protobuf 与 Abseil 的运行时依赖。它通过内存映射直接操作 [FlatBuffers](https://www.edgepedia.org/zh/flatbuffers) 二进制，无论词表大小，冷启动仅需 1.2 KB 堆内存开销，核心引擎编译为约 45 KB 的剥离静态库。项目声明，在一元与 BPE 模型上，其规范化结果均与标准 SentencePiece 模型逐字节一致，词元 ID 也相同。<sup>[2](https://github.com/google/sentencepiece)</sup>

## 采用与近期发展

SentencePiece 是多个广泛使用的模型系列背后的分词器：T5 使用一元算法，Llama 底层使用 BPE。<sup>[5](https://www.kunwar.page/The_Holy_Grail.pdf)</sup> 项目自身比较中基准测试的 Gemma 3 分词器使用 256,000 词元的词表。<sup>[6](https://github.com/google/sentencepiece/blob/master/doc/performance_benchmark.md)</sup> 版本 0.2.2 将 Python 包装迁移至 pybind11，增加原生 NumPy 支持，增强偏移映射，并在后续版本中引入实验性自由线程支持。<sup>[10](https://github.com/google/sentencepiece/releases)</sup> 学术工作此后形式化了 SentencePiece 风格 BPE 分词的语义，并考察了其与 Hugging Face 实现的关系，取决于分词规则的构建方式。<sup>[11](https://arxiv.org/abs/2309.08715)</sup>

## References

1. [Taku Kudo and John Richardson (2018). SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing. arXiv:1808.06226.](https://arxiv.org/abs/1808.06226)
2. [google/sentencepiece repository README, GitHub.](https://github.com/google/sentencepiece)
3. [Tokenizer Comparison Cheat Sheet, google/sentencepiece, GitHub.](https://github.com/google/sentencepiece/blob/master/python/tokenizer_comparison_cheat_sheet.md)
4. [SentencePiece Unigram vs BPE: model_type, Character Coverage and Split Rules, SentencePiece Guide.](https://sentencepiece.org/concept/unigram-vs-bpe-params/)
5. [The Holy Grail of Tokenizer Training: BPE and SentencePiece from Scratch.](https://www.kunwar.page/The_Holy_Grail.pdf)
6. [Tokenizer Performance Analysis (Balanced Raw Multilingual), google/sentencepiece, GitHub.](https://github.com/google/sentencepiece/blob/master/doc/performance_benchmark.md)
7. [Text Normalization in SentencePiece, SentencePiece Documentation.](https://google.github.io/sentencepiece/doc/normalization/)
8. [Vocabulary Piece Constraints, google/sentencepiece, GitHub.](https://github.com/google/sentencepiece/blob/master/doc/piece_constraints.md)
9. [tiktoken vs SentencePiece vs Hugging Face Tokenizers, dreaming.press.](https://www.dreaming.press/posts/tiktoken-vs-sentencepiece-vs-huggingface-tokenizers.html)
10. [Releases, google/sentencepiece, GitHub.](https://github.com/google/sentencepiece/releases)
11. [Martin Berglund and Brink van der Merwe (2023). Formalizing BPE Tokenization. arXiv:2309.08715.](https://arxiv.org/abs/2309.08715)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Language and vision AI › Natural language processing › NLP tasks and methods › Tokenization and segmentation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/sentencepiece

English original: https://www.edgepedia.org/sentencepiece
