Skip to main content



Build GitHub


Tokenizers

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

Bindings over the Rust implementation. If you are interested in the High-level design, you can go check it there.

Otherwise, let's dive in!

Main features:

  • Train new vocabularies and tokenize using 4 pre-made tokenizers (Bert WordPiece and the 3 most common BPE versions).
  • Extremely fast (both training and tokenization), thanks to the Rust implementation. Takes less than 20 seconds to tokenize a GB of text on a server's CPU.
  • Easy to use, but also extremely versatile.
  • Designed for research and production.
  • Normalization comes with alignments tracking. It's always possible to get the part of the original sentence that corresponds to a given token.
  • Does all the pre-processing: Truncate, Pad, add the special tokens your model needs.

Installation

With pip:

pip install tokenizers

From sources:

To use this method, you need to have the Rust installed:

# Install with:
curl https://sh.rustup.rs -sSf | sh -s -- -y
export PATH="$HOME/.cargo/bin:$PATH"

Once Rust is installed, you can compile doing the following

git clone https://github.com/huggingface/tokenizers
cd tokenizers/bindings/python

# Create a virtual env (you can use yours as well)
python -m venv .env
source .env/bin/activate

# Install `tokenizers` in the current virtual env
pip install -e .

Load a pretrained tokenizer from the Hub

from tokenizers import Tokenizer

tokenizer = Tokenizer.from_pretrained("bert-base-cased")

Using the provided Tokenizers

We provide some pre-build tokenizers to cover the most common cases. You can easily load one of these using some vocab.json and merges.txt files:

from tokenizers import CharBPETokenizer

# Initialize a tokenizer
vocab = "./path/to/vocab.json"
merges = "./path/to/merges.txt"
tokenizer = CharBPETokenizer(vocab, merges)

# And then encode:
encoded = tokenizer.encode("I can feel the magic, can you?")
print(encoded.ids)
print(encoded.tokens)

And you can train them just as simply:

from tokenizers import CharBPETokenizer

# Initialize a tokenizer
tokenizer = CharBPETokenizer()

# Then train it!
tokenizer.train([ "./path/to/files/1.txt", "./path/to/files/2.txt" ])

# Now, let's use it:
encoded = tokenizer.encode("I can feel the magic, can you?")

# And finally save it somewhere
tokenizer.save("./path/to/directory/my-bpe.tokenizer.json")

Provided Tokenizers

  • CharBPETokenizer: The original BPE
  • ByteLevelBPETokenizer: The byte level version of the BPE
  • SentencePieceBPETokenizer: A BPE implementation compatible with the one used by SentencePiece
  • BertWordPieceTokenizer: The famous Bert tokenizer, using WordPiece

All of these can be used and trained as explained above!

Build your own

Whenever these provided tokenizers don't give you enough freedom, you can build your own tokenizer, by putting all the different parts you need together. You can check how we implemented the provided tokenizers and adapt them easily to your own needs.

Building a byte-level BPE

Here is an example showing how to build your own byte-level BPE by putting all the different pieces together, and then saving it to a single file:

from tokenizers import Tokenizer, models, pre_tokenizers, decoders, trainers, processors

# Initialize a tokenizer
tokenizer = Tokenizer(models.BPE())

# Customize pre-tokenization and decoding
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=True)
tokenizer.decoder = decoders.ByteLevel()
tokenizer.post_processor = processors.ByteLevel(trim_offsets=True)

# And then train
trainer = trainers.BpeTrainer(
    vocab_size=20000,
    min_frequency=2,
    initial_alphabet=pre_tokenizers.ByteLevel.alphabet()
)
tokenizer.train([
    "./path/to/dataset/1.txt",
    "./path/to/dataset/2.txt",
    "./path/to/dataset/3.txt"
], trainer=trainer)

# And Save it
tokenizer.save("byte-level-bpe.tokenizer.json", pretty=True)

Now, when you want to use this tokenizer, this is as simple as:

from tokenizers import Tokenizer

tokenizer = Tokenizer.from_file("byte-level-bpe.tokenizer.json")

encoded = tokenizer.encode("I can feel the magic, can you?")

Release files for tokenizers 0.21.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokenizers 0.21.0
File Size Uploaded
tokenizers-0.21.0.tar.gz 343.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for tokenizers 0.21.0
File
tokenizers-0.21.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
tokenizers-0.21.0-cp39-abi3-win32.whl CPython 3.9 abi3 Windows x86-32 Details
tokenizers-0.21.0-cp39-abi3-musllinux_1_2_x86_64.whl CPython 3.9 abi3 Linux musl 1.2+ x86-64 Details
tokenizers-0.21.0-cp39-abi3-musllinux_1_2_i686.whl CPython 3.9 abi3 Linux musl 1.2+ x86-32 Details
tokenizers-0.21.0-cp39-abi3-musllinux_1_2_armv7l.whl CPython 3.9 abi3 Linux musl 1.2+ ARMv7l Details
tokenizers-0.21.0-cp39-abi3-musllinux_1_2_aarch64.whl CPython 3.9 abi3 Linux musl 1.2+ ARM64 Details
tokenizers-0.21.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
tokenizers-0.21.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl CPython 3.9 abi3 Linux glibc 2.17+ IBM System/390x Details
tokenizers-0.21.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl CPython 3.9 abi3 Linux glibc 2.17+ PowerPC 64-le Details
tokenizers-0.21.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-32 Details
tokenizers-0.21.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl CPython 3.9 abi3 Linux glibc 2.17+ ARMv7l Details
tokenizers-0.21.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details
tokenizers-0.21.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
tokenizers-0.21.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details

Total release size: 64.9 MB

Release files / tokenizers-0.21.0.tar.gz

Download URL tokenizers-0.21.0.tar.gz
Size 343.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ee0894bf311b75b0c03079f33859ae4b2334d675d4e93f5a4132e1eae2834fe4
BLAKE2b-256 checksum
How to use checksums
2041c2be10975ca37f6ec40d7abd7e98a5213bb04f284b869c1a24e6504fd94d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-win_amd64.whl

Download URL tokenizers-0.21.0-cp39-abi3-win_amd64.whl
Size 2.4 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
87841da5a25a3a5f70c102de371db120f41873b854ba65e52bccd57df5a3780c
BLAKE2b-256 checksum
How to use checksums
4469d21eb253fa91622da25585d362a874fa4710be600f0ea9446d8d0217cec1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-win32.whl

Download URL tokenizers-0.21.0-cp39-abi3-win32.whl
Size 2.2 MB
Tags CPython 3.9 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
eb1702c2f27d25d9dd5b389cc1f2f51813e99f8ca30d9e25348db6585a97e24a
BLAKE2b-256 checksum
How to use checksums
15b0dc4572ca61555fc482ebc933f26cb407c6aceb3dc19c301c68184f8cad03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-musllinux_1_2_x86_64.whl

Download URL tokenizers-0.21.0-cp39-abi3-musllinux_1_2_x86_64.whl
Size 9.4 MB
Tags CPython 3.9 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
4145505a973116f91bc3ac45988a92e618a6f83eb458f49ea0790df94ee243ff
BLAKE2b-256 checksum
How to use checksums
18073e88e65c0ed28fa93aa0c4d264988428eef3df2764c3126dc83e243cb36f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-musllinux_1_2_i686.whl

Download URL tokenizers-0.21.0-cp39-abi3-musllinux_1_2_i686.whl
Size 9.2 MB
Tags CPython 3.9 Linux musl 1.2+ x86-32 abi3
SHA-256 checksum
How to use checksums
c87ca3dc48b9b1222d984b6b7490355a6fdb411a2d810f6f05977258400ddb74
BLAKE2b-256 checksum
How to use checksums
d8eece83d5ec8b6844ad4c3ecfe3333d58ecc1adc61f0878b323a15355bcab24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-musllinux_1_2_armv7l.whl

Download URL tokenizers-0.21.0-cp39-abi3-musllinux_1_2_armv7l.whl
Size 8.9 MB
Tags CPython 3.9 Linux musl 1.2+ ARMv7l abi3
SHA-256 checksum
How to use checksums
089d56db6782a73a27fd8abf3ba21779f5b85d4a9f35e3b493c7bbcbbf0d539b
BLAKE2b-256 checksum
How to use checksums
f7f3b776061e4f3ebf2905ba1a25d90380aafd10c02d406437a8ba22d1724d76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-musllinux_1_2_aarch64.whl

Download URL tokenizers-0.21.0-cp39-abi3-musllinux_1_2_aarch64.whl
Size 9.0 MB
Tags CPython 3.9 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
eb7202d231b273c34ec67767378cd04c767e967fda12d4a9e36208a34e2f137e
BLAKE2b-256 checksum
How to use checksums
c86954a0aee4d576045b49a0eb8bffdc495634309c823bf886042e6f46b80058
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL tokenizers-0.21.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 3.0 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
e84ca973b3a96894d1707e189c14a774b701596d579ffc7e69debfc036a61a04
BLAKE2b-256 checksum
How to use checksums
220669d7ce374747edaf1695a4f61b83570d91cc8bbfc51ccfecf76f56ab4aac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl

Download URL tokenizers-0.21.0-cp39-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Size 3.4 MB
Tags CPython 3.9 Linux glibc 2.17+ IBM System/390x abi3
SHA-256 checksum
How to use checksums
400832c0904f77ce87c40f1a8a27493071282f785724ae62144324f171377273
BLAKE2b-256 checksum
How to use checksums
814207600892d48950c5e80505b81411044a2d969368cdc0d929b1c847bf6697
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl

Download URL tokenizers-0.21.0-cp39-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Size 3.1 MB
Tags CPython 3.9 Linux glibc 2.17+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
d8b09dbeb7a8d73ee204a70f94fc06ea0f17dcf0844f16102b9f414f0b7463ba
BLAKE2b-256 checksum
How to use checksums
4df65ed6711093dc2c04a4e03f6461798b12669bc5a17c8be7cce1240e0b5ce8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL tokenizers-0.21.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl
Size 3.1 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-32 abi3
SHA-256 checksum
How to use checksums
9aeb255802be90acfd363626753fda0064a8df06031012fe7d52fd9a905eb00e
BLAKE2b-256 checksum
How to use checksums
578b7da5e6f89736c2ade02816b4733983fca1c226b0c42980b1ae9dc8fcf5cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl

Download URL tokenizers-0.21.0-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Size 2.8 MB
Tags CPython 3.9 Linux glibc 2.17+ ARMv7l abi3
SHA-256 checksum
How to use checksums
6b43779a269f4629bebb114e19c3fca0223296ae9fea8bb9a7a6c6fb0657ff8e
BLAKE2b-256 checksum
How to use checksums
7edb3433eab42347e0dc5452d8fcc8da03f638c9accffefe5a7c78146666964a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL tokenizers-0.21.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 2.9 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
6b177fb54c4702ef611de0c069d9169f0004233890e0c4c5bd5508ae05abf193
BLAKE2b-256 checksum
How to use checksums
f71483429177c19364df27d22bc096d4c2e431e0ba43e56c525434f1f9b0fd00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL tokenizers-0.21.0-cp39-abi3-macosx_11_0_arm64.whl
Size 2.6 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f53ea537c925422a2e0e92a24cce96f6bc5046bbef24a1652a5edc8ba975f62e
BLAKE2b-256 checksum
How to use checksums
227a88e58bb297c22633ed1c9d16029316e5b5ac5ee44012164c2edede599a5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release files / tokenizers-0.21.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL tokenizers-0.21.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 2.6 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
3c4c93eae637e7d2aaae3d376f06085164e1660f89304c0ab2b1d08a406636b2
BLAKE2b-256 checksum
How to use checksums
b05c8b09607b37e996dc47e70d6a7b6f4bdd4e4d5ab22fe49d7374565c7fefaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

Release history Release notifications | RSS feed

This release

0.21.0 This release

15 release files

0.9.4

41 release files

0.9.3

17 release files

0.9.2

17 release files

0.9.1

17 release files

0.8.1

17 release files

0.8.0

17 release files

0.7.0

17 release files

0.5.2

13 release files

0.5.1

13 release files

0.5.0

13 release files

0.4.2

13 release files

0.4.1

13 release files

0.4.0

13 release files

0.2.1

13 release files

0.2.0

13 release files

0.1.1

13 release files

0.1.0

13 release files

0.0.9

13 release files

0.0.8

13 release files

0.0.7

13 release files

0.0.6

13 release files

0.0.5

13 release files

0.0.4

13 release files

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page