# Hugging Face Tokenizers

> The Rust tokenizer library behind the Transformers ecosystem, for training vocabularies and encoding text

- Category: Models & Inference / Tokenizers
- Language: Rust
- Licence: Apache-2.0
- Repository: https://github.com/huggingface/tokenizers
- Website: https://huggingface.co/docs/tokenizers
- Canonical page: https://ai-tldr.dev/tools/huggingface-tokenizers/

## In short

Hugging Face Tokenizers is the Rust tokenizer library behind the Transformers ecosystem, for training vocabularies and encoding text. It is the layer that turns text into the integer ids a model consumes, and the piece the wider Transformers ecosystem loads when you call a fast tokenizer. It sits in the Tokenizers category of the Models & Inference tools and is open-source, written in Rust and licensed Apache-2.0.

## Overview

Tokenizers is Hugging Face's implementation of the tokenization algorithms in common use, written in Rust with bindings for Python, Node.js and Rust itself (a community Ruby binding lives in a separate repository). It is the layer that turns text into the integer ids a model consumes, and the piece the wider Transformers ecosystem loads when you call a fast tokenizer.

It does two jobs. The first is training: point it at a corpus and it will fit a new vocabulary using Byte-Pair Encoding, WordPiece or Unigram, with configurable normalizers, pre-tokenizers and special tokens. The second is inference-time encoding, and here the library handles the whole pre-processing chain — normalization, pre-tokenization, truncation, padding and the special tokens a given model expects.

The design detail that matters in practice is alignment tracking: normalization keeps a mapping back to the original string, so for any token you can recover the exact span of input text it came from. That is what makes token-level tasks — named-entity extraction, span highlighting, offset-based post-processing — possible without re-deriving positions by hand. The project is Apache-2.0 and states a target of tokenizing a gigabyte of text in under twenty seconds on a server CPU.

## What it does

- Train new vocabularies with Byte-Pair Encoding, WordPiece or Unigram from your own corpus
- Rust core with official Python, Node.js and Rust bindings
- Alignment tracking through normalization — map any token back to its span in the original text
- Full pre-processing chain built in: normalizers, pre-tokenizers, truncation, padding and special tokens
- The fast-tokenizer implementation the Transformers ecosystem loads, so trained tokenizers travel with models
- Apache-2.0, installable as a wheel with `pip install tokenizers`

## Getting started

The Python binding is the usual entry point. These snippets come from the project README's quick example.

1. Install

   Released wheels are on PyPI; you can also build from the repository subdirectory.

```bash
pip install tokenizers

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

2. Pick a model and a pre-tokenizer

   Choose between BPE, WordPiece and Unigram, then say how text is split into words before the model runs.

```python
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.pre_tokenizers import Whitespace

tokenizer = Tokenizer(BPE())
tokenizer.pre_tokenizer = Whitespace()
```

3. Train a vocabulary

   Hand the trainer your corpus files and the special tokens the model needs.

```python
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)
```

4. Encode text

   The encoding carries tokens, ids and the offsets back into the original string.

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

## When to use it

- Train a domain-specific vocabulary before pretraining or continued-pretraining a model
- Encode text in a production serving path with truncation, padding and special tokens handled for you
- Recover the exact source span behind a token for entity extraction or span highlighting
- Use the same tokenizer definition across Python services, Node.js front ends and Rust binaries
