Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 0.23.3 instead.
Reason given by maintainers: Removing support for python3.7 and 3.8 is breaking and is move to v0.21.0



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?")

Metadata

Release files for tokenizers 0.20.4

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.20.4
File Size Uploaded
tokenizers-0.20.4.tar.gz 343.0 kB Details

Built distributions (wheels)

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

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

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

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

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

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

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

Download URL tokenizers-0.20.4-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
a6d392a20ca70692aaba8a636677b57f6c67655879773ba2b6be8cb4a19ce6b8
BLAKE2b-256 checksum
How to use checksums
9e6241dc66bda0b1d45f36f324d01a3c4389caac0526d4b4ccc2d2006c7722b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
84bf8b4a7bbf1c6bb78775ae309a5c69d08dadf7b88125d6d19ccb4738a87350
BLAKE2b-256 checksum
How to use checksums
94082d541e0e437e87e5cae903c55fc832fd032812c85e095bb7833d5fe65dfd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
e59a405459ed31b73426b364752c2e7c73f4a94210a63fd7acd161a774af7bd2
BLAKE2b-256 checksum
How to use checksums
245661b8c0cd217a5be05c4206263afdfa937848796696fee7cc14fab6c94a49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
3e960ad5c467a95e5665e518151ed9024e7aa111d2c54ff1938162cc7c2b8959
BLAKE2b-256 checksum
How to use checksums
efbf135de1cf1c10f53a10c8db5f067e7cf71adebd79ed2b5cf5729eded209af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
05c2bab579c1f31292b48bb79b6334b5346c1ec87dac81089e6098b8a20b2fd4
BLAKE2b-256 checksum
How to use checksums
b91ccc1ca0c4e4c5763b2d872935de7149633765d158f0d1a46b48d73f3a9c58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
735ffc9bba65d20f8ab5f82dfbab262bb066afc7dee3684c5e5435e7a5da445d
BLAKE2b-256 checksum
How to use checksums
f6c934c6a304529a0b3375533e0ccc4ffd4bcfa401cb57cdab5997efaf9836fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
eee647ccba9cbd36b5ec4e8e73d25dbd586ec06de7a43ff83a3dad9fec466a29
BLAKE2b-256 checksum
How to use checksums
6549e4647d5e496e64f2f639aeba9df134f59b648822a90d63ceb7b4b309ba4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
aa392bae7f0a36e4c97ad43100390ad84f2a1bfff6742604774210f7d7a4fa13
BLAKE2b-256 checksum
How to use checksums
d0298dff1d57d1d1bcae5f0d701759ca3f503ca31ecd443f75571d6ef3083043
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
075635cd7e6936cc4b3a13901c1a05690d5b533ce3d0f035dee21117dd4f04ae
BLAKE2b-256 checksum
How to use checksums
b7d1e70b4497da0e97111ef75eba32526a98bf04268ce2a7388d9759ec89acdb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
7786004e180fab72e6e873e982ccd18b3cfa31521d397b6c024cc19175abf91b
BLAKE2b-256 checksum
How to use checksums
66aa70de5a7c96621c1962e8399245efac6d6ab3bbe873457b9beb26c406c4f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
f41df992797ad0ff9472e8a2c7a3ef7178667935d984639b73da7d19b33ea4e2
BLAKE2b-256 checksum
How to use checksums
bb26a4b8dfe37c92205a4ba267d35ed7a6ca0ae1cbb2a1ed8acf84813cf48704
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.7.5

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

Download URL tokenizers-0.20.4-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
25f59ebc5b79e7bbafe86bfec62696468016627157d8a9ceba5092486796a156
BLAKE2b-256 checksum
How to use checksums
b7a1c8285e835dd185307f40bc4a308224dc357ee4d25f3fd83ef013101a5060
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.20.4 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