tokenizers README

The project's README from github.com/huggingface/tokenizers. As one markdown file: README.md.



Build GitHub

Provides an implementation of today's most used tokenizers, with a focus on performance and versatility.

Main features:

Performances

Performances can vary depending on hardware, but running the ~/bindings/python/benches/test_tiktoken.py should give the following on a g6 aws instance: image

Bindings

We provide bindings to the following languages (more to come!):

Installation

You can install from source using:

pip install git+https://github.com/huggingface/tokenizers.git#subdirectory=bindings/python

or install the released versions with

pip install tokenizers

Quick example using Python:

Choose your model between Byte-Pair Encoding, WordPiece or Unigram and instantiate a tokenizer:

from tokenizers import Tokenizer
from tokenizers.models import BPE

tokenizer = Tokenizer(BPE())

You can customize how pre-tokenization (e.g., splitting into words) is done:

from tokenizers.pre_tokenizers import Whitespace

tokenizer.pre_tokenizer = Whitespace()

Then training your tokenizer on a set of files just takes two lines of codes:

from tokenizers.trainers import BpeTrainer

trainer = BpeTrainer(special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"])
tokenizer.train(files=["wiki.train.raw", "wiki.valid.raw", "wiki.test.raw"], trainer=trainer)

Once your tokenizer is trained, encode any text with just one line:

output = tokenizer.encode("Hello, y'all! How are you 😁 ?")
print(output.tokens)
# ["Hello", ",", "y", "'", "all", "!", "How", "are", "you", "[UNK]", "?"]

Check the documentation or the quicktour to learn more!

tokenizers (huggingface/tokenizers)

Searching Database…

这是什么?Tokenizers 是一个自然语言处理(NLP)库,实现了当前最常用的分词器(BPE、WordPiece、Unigram)。它使用 Rust 编写以保证速度,并通过绑定提供给其他编程语言使用。它由 Hugging Face 开发,为 Transformer 模型提供分词功能。
这是独立应用,还是可以在代码中导入的库?它是一个库:核心用 Rust 编写,并提供 Python 和 Node.js 绑定,以及社区维护的 Ruby 绑定,可导入到你自己的训练或推理代码中使用。
我该如何安装和使用它?可以从源码安装,也可以通过对应语言的正式发布包安装(例如 Python 使用 pip)。然后选择一种模型类型,如 BPE、WordPiece 或 Unigram,创建分词器实例,再用你的文件训练,只需几行代码即可完成。
使用 Python 绑定是否需要安装 Rust?普通用户不需要单独安装 Rust 工具链。已发布的 Python 和 Node.js 包都是预编译好的,只有从源码编译时才需要源码构建工具。
性能是否取决于我的硬件?是的,基准测试中的速度数据会因硬件不同而有所差异。README 中给出的数字来自某个特定的 AWS g6 实例,仅供参考,并不是普遍适用的保证。
这个项目主要面向谁?它既面向研究分词方法的研究人员,也面向需要为语言模型提供快速、可靠文本预处理的生产团队。
分词的速度到底有多快?README 中说明,得益于 Rust 实现,在服务器 CPU 上对 1 GB 文本进行分词只需不到 20 秒。
能否将词元追溯到原始文本?可以。标准化处理带有对齐追踪功能,因此你总能找到某个词元在原句中对应的确切部分。
它会自动处理填充和截断吗?会。它能完成所需的全部预处理:截断、填充,并添加模型所需的特殊词元。
支持哪些分词算法?创建分词器实例时,可以在字节对编码(BPE)、WordPiece 或 Unigram 模型之间选择。
Size
22.4 MB
Stars
11.2K
Age
6y
Commits · 90 Days