Archive of AI

Source Snapshot / 2020-05-huggingface-tokenizers-datasets-1

huggingface/tokenizers

Linked event: 2020-05 · Hugging Face Open-Sources High-Performance Tokenizers Library and Zero-Copy Datasets Framework

Publisher
GitHub
Archived
2026-11-23
Capture Method
Direct page capture
HTTP / SHA-256
200 / 280fb14e213fb5f8
Original URL
Open source ↗
Wayback Backup
Not archived
This page preserves a plain-text static snapshot against link rot. When citing figures, tables, or full legal terms, verify against the original URL and Wayback Machine archive.

[来源关键摘录] 基于 Rust 实现的高速多语言分词器开源仓库,兼顾科研灵活性与生产级吞吐效率。

[正文前段节选] Skip to content

Navigation Menu

Appearance settings

You signed in with another tab or window. Reload to refresh your session.

You signed out in another tab or window. Reload to refresh your session.

You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

Repository navigation

NotificationsYou must be signed in to change notification settings

Fork1.3k (1.3k)

Star11.3k (11.3k)You must be signed in to star a repository

💥 Fast State-of-the-Art Tokenizers optimized for Research and Production

huggingface.co/docs/tokenizers

bertgptlanguage-modelnatural-language-processingnatural-language-understandingnlptransformers

Apache-2.0 license

Contributing

Cite this repository

Custom properties

120 watching

Report repository

BranchesTags

Open more actions menu

Latest commit

2,251 Commits

Folders and files

Last commit message

Last commit date

docs/source-doc-builder

.gitattributes

CITATION.cff

CONTRIBUTING.md

REQUIRED_FOR_V1.md

View all files

Repository files navigation

The SOTA tokenization library on all languages, all models, all hardwares.

Our goal with tokenizers is to develop and maintain the industry's standard tokenization engine, making it the defacto place for everyone to contribute to the whole ecosystem.

Release candidate: v1.0.0.rc.x

As we are switching from 0.23 to v1.0.0, the current library does not ship all features. If you are worried, check the v1 features section for more details of what we are bringing back.

We will publish a blog about what breaking changes we introduced. We strived to keep them as small as possible.

Installation

pip install --pre tokenizers

For Rust, add the crate to your Cargo.toml:

[dependencies]

tokenizers = "1.0.0"

Training (train) and downloading from the Hub (http) are opt-in features. The crate's

Using the crate section lists every feature and how to

build a smaller binary.

>>> from tokenizers import Tokenizer

>>> tokenizer = Tokenizer.from_pretrained("meta-llama/Llama-3.1-8B") # or .from_file(path)

>>> compiled = tokenizer.compile()

>>> compiled.encode("Hello, y'all! How are you 😁 ?")

Encoding(ids=[128000, 9906, 11, 379, 65948, 0, 2650, 527, 499, 27623, 223, 949], type_ids=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], attention_mask=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

>>> compiled.encode(("What is this?", "A pair.")).type_ids

[0, 0, 0, 0, 0, 1, 1, 1, 1]

>>> compiled.tokenize("Hello")

['<|begin_of_text|>', 'Hello']

>>> compiled.decode([15339, 1917])

'hello world'

>>> compiled.decode_tokens(compiled.encode("Hello"))

Tokenizer holds the tokenizer.json config and can be edited (e.g. tokenizer.truncation = ...);

compile() builds the CompiledTokenizer that encodes and decodes.

Performances

For more details, checkout https://huggingface-tokenizers-v1.static.hf.space/index.html.

✅ reference implementation

✅ tokenizers-wasm on npm

C / C++ / Java / Go/ etc

community, external repo

v1.0.0 roadmap:

As we work toward v1, we are gonna bring back the entire python API that allows transformers' style of interacting with a tokenizer object:

Python Bindings

Bring training back: this should be easy, but we want the perfs gains to come as well.

Batch encoding API with zero-copy views on encoded ids

dlpack API for torch / JAX / numpy interop.

Composition: edit components of the pipeline (tokenizer.normalizer = [...]). The editable

Tokenizer spec and compile() have landed; per-component handles are next.

tokenizer.save to persist a modified or trained tokenizer on disk.

role_to_token override.

Bring back offset output: this should be useful for people training.

Windowing / stride in encode.

bitnorm: more performance from optimized normalization, as this will bring breaking changes we want to make sure it gets to v1.

Windowing (truncation with stride).

Improve training performance

Improve decode performance

Offset tracking

Padding: 2D flat buffer

Wordcache: shrn vs emulated movemask

Fix ModernBERT / OLMo load failure ("64-bit hash collision").

Unroll more regex: we need to cover the most used ones, we might have missed one or two.

C & C++ bindings for loading, encode and decode APIs

Improve / rewrite the bitcannon crate

Java, Go, Swift... bindings, supported by the C bindings

tk-devices — exploratory GPU encoding and batch decoding that keeps text and ids on the

device: upload the vocabulary once, compute output positions in parallel, gather the bytes on the

GPU. An optional component aimed at large batches, subject to prototyping and measurement.

Crate details

tk-encode — the inference engine

The models (BPE, Unigram, WordPiece, WordLevel) and the full pipeline: Normalizer,

PreTokenizer, Model, PostProcessor, Decoder.

Why separate: you don't need all features to serve a model in production settings. Splitting it from training

means a serving binary never links a trainer, a corpus reader, legacy converters, etc.

bitcannon — bitstream pre-tokenization

Unicode tag classification plus pre-tokenization as a bitstream program. Follows Interleaved Bitstream Execution for Multi-Pattern Regex Matching on GPUs

(MICRO'25, 10.1145/3725843.3756052) but we added our own algorithm to make sure we don't need a parabix engine.

Characters are first classified into "tags", which use a sparse table generated when compiling bitcannon. This means there are 0 depencencies to the heavy unicode tables.

Ships byte-exact grammars for gpt2 / ByteLevel, cl100k, o200k, tekken, deepseek and kimi-k2.

The classes or tags each have a high and low nibble (encoded on a u8), and carry refinement when needed. Using u8 tags allows us to leverage less bitsream, meaning have a simpler / faster pre tokenization.

Why separate: we just thought it might be useful to some people someday. As far as we know, this implementation is the fastest for every single pretokenizer regex on CPU.

tk-serialize — the reader

from_json_file turns a canonical tokenizer.json into a Tokenizer.

Why separate: this just makes it optional for some usecases!

tk-convert — the upgrade pass

canonicalize_file rewrites legacy a tokenizer.json written by any older version of this library into

the canonical form the reader accepts. A pure JSON→JSON rewrite; it depends on nothing but

std::path and serde_json.

Why separate: every config ever published stays readable without the runtime carrying a decade

of compatibility branches. But, the legacy code amounted to quite a lot of the final crate size. Disabling it allows on device builds to be smaller.

tk-train — the training half

The Trainer trait, every concrete *Trainer, TrainerWrapper and the Trainable extension.

The tokenizers crate does not re-export it. Its API will still change, so the crate stays 0.x

until it settles. It trains only the models whose feature is on (bpe, unigram, wordpiece,

wordlevel). Add esaxx_fast for the C++ suffix array behind Unigram training.

Why separate: training is a batch job on a workstation; inference is a hot loop in a server.

They have opposite constraints, so they get opposite dependency budgets.

bitmap_gen — dev-only table generator

cargo run -p bitmap_gen regenerates bitcannon's committed classify tables from unicode-properties, emitting one Atom tag per codepoint.

Why separate — this is what buys the zero Unicode dependency. unicode-properties is a

dependency of this crate and of nothing else: the tables it produces are checked into

bitcannon/src/classify/atom_tables.rs as ordinary source, so the Unicode data is resolved once, at

development time, by a crate that is never linked into anything that ships and is never published.

No build script either — bitcannon compiles with no code generation step, and a release binary

contains the tags without containing a Unicode crate to derive them. The release workflow re-runs

the generator and fails if the committed table differs, so "baked" cannot silently mean "stale".

tokenizers — umbrella

[...内容已截断,完整版请查看原文或 Wayback 快照]