Skip to main content

Blazingly fast tokenizer — 50x faster, 10x smaller, 100% accurate

Project description

tokie

10-136x faster tokenization, 10x smaller model files, 100% accurate

GitHub · crates.io · HuggingFace


tokie is a fast, correct tokenizer library built in Rust with Python bindings. Drop-in replacement for HuggingFace tokenizers — supports BPE (GPT-2, tiktoken, SentencePiece), WordPiece (BERT), and Unigram encoders.

Installation

pip install tokie

Quick Start

import tokie

# Load from HuggingFace Hub (tries .tkz first, falls back to tokenizer.json)
tokenizer = tokie.Tokenizer.from_pretrained("bert-base-uncased")

# Encode — callable syntax or .encode()
encoding = tokenizer("Hello, world!")
print(encoding.ids)               # [101, 7592, 1010, 2088, 999, 102]
print(encoding.tokens)            # ['[CLS]', 'hello', ',', 'world', '!', '[SEP]']
print(encoding.attention_mask)    # [1, 1, 1, 1, 1, 1]
print(encoding.special_tokens_mask)  # [1, 0, 0, 0, 0, 1]

# Decode
text = tokenizer.decode(encoding.ids)  # "hello , world !"

# Token count (fast, no Encoding overhead)
count = tokenizer.count_tokens("Hello, world!")

# Batch encode (parallel across all cores)
encodings = tokenizer.encode_batch(["Hello!", "World"], add_special_tokens=True)

Padding & Truncation

# Truncate to max length (special tokens preserved)
tokenizer.enable_truncation(max_length=32)

# Pad all sequences in a batch to the same length
tokenizer.enable_padding(length=32, pad_id=tokenizer.pad_token_id or 0)

# Batch encode — all sequences same length, ready for model input
texts = ["Hello world", "Short", "A much longer sentence for testing"]
batch = tokenizer.encode_batch(texts, add_special_tokens=True)
for enc in batch:
    print(len(enc), enc.ids[:5])  # All length 32

Pair Encoding (Cross-Encoders)

pair = tokenizer("How are you?", "I am fine.")  # or tokenizer.encode_pair(...)
print(pair.ids)                # [101, 2129, 2024, 2017, 1029, 102, 1045, 2572, 2986, 1012, 102]
print(pair.type_ids)           # [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
print(pair.special_tokens_mask)  # [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]

Byte Offsets

enc = tokenizer.encode_with_offsets("Hello world")
for token_id, (start, end) in zip(enc.ids, enc.offsets):
    print(f"  token {token_id}: bytes [{start}:{end}]")

Save & Load (.tkz format)

tokie's binary .tkz format is ~10x smaller than tokenizer.json and loads in ~5ms:

tokenizer.save("model.tkz")
tokenizer = tokie.Tokenizer.from_file("model.tkz")

Supported Models

Works with any HuggingFace tokenizer — GPT-2, BERT, Llama 3/4, Mistral, Phi, Qwen, T5, XLM-RoBERTa, and more.

Benchmarks

Model Text Size tokie HF tokenizers Speedup
BERT 900 KB 1.69 ms 229 ms 136x
GPT-2 900 KB 1.70 ms 181 ms 107x
Llama 3 900 KB 2.04 ms 190 ms 93x
Qwen 3 45 KB 0.15 ms 8.18 ms 54x
Gemma 3 45 KB 1.01 ms 9.62 ms 10x

100% token-accurate across all models.

License

MIT OR Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tokie-0.0.10a1-cp312-cp312-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.12Windows x86-64

tokie-0.0.10a1-cp312-cp312-manylinux_2_28_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ ARM64

tokie-0.0.10a1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

tokie-0.0.10a1-cp312-cp312-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

tokie-0.0.10a1-cp312-cp312-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

tokie-0.0.10a1-cp311-cp311-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.11Windows x86-64

tokie-0.0.10a1-cp311-cp311-manylinux_2_28_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ ARM64

tokie-0.0.10a1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

tokie-0.0.10a1-cp311-cp311-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

tokie-0.0.10a1-cp311-cp311-macosx_10_12_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

tokie-0.0.10a1-cp310-cp310-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.10Windows x86-64

tokie-0.0.10a1-cp310-cp310-manylinux_2_28_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ ARM64

tokie-0.0.10a1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

tokie-0.0.10a1-cp310-cp310-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

tokie-0.0.10a1-cp310-cp310-macosx_10_12_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

File details

Details for the file tokie-0.0.10a1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: tokie-0.0.10a1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tokie-0.0.10a1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 09afab65f4fd419f3ed2ab8c7d06f387299f634aab57138ea168d61f1afa2797
MD5 1094f83bf93ee559cf18d8974751c219
BLAKE2b-256 dd6aeee8ab55f323b89950b78078a8e43fd5cf31165d11bafbd35adfbd342f32

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp312-cp312-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f4fbb2b9c7f2ead01494d12b7c992fe8436865b9c14b99adc16c85f0dfb8a595
MD5 ff869f930aa5515ad5b40e7a29c305fa
BLAKE2b-256 8bc3ef148e8ba371f6dbec17864134d85c0c88453420e9481d4c190a437386ce

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 861260702afb475f9a8ba49cfb5138c77578a906cc99f4689e5af3bd1002c5f7
MD5 f18e17de694b337a860cbc725688db50
BLAKE2b-256 19a1bbd22f810e7c193bfa0e8d5530188435a43199a814c5120cf88ed73a7da6

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5b48ccfd6546e8097bcb519d4fe1ed3368ddae3b30a59551e2f49c638d014c1a
MD5 ad29fbfe373f8ae58e79b5a8e4e306ca
BLAKE2b-256 c89eda14bc6c47eb68f092e3e0bee204f571ec16417e639679c0d9fe10b5112c

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ed9223651ef595c10c13d8f1fe9dc7187e47b3fdf2cf0264cc0e503c3f304466
MD5 529052d69abcf45377da469d431045be
BLAKE2b-256 b2b18eca93dfa69302a9381a9b67ba4b56e919fe112dccc0e9ef1a4b0b68a20d

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: tokie-0.0.10a1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tokie-0.0.10a1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1a22e08a720faa58ffbf684681bfd40a7e06ee8ca37e46c8db05e3487fbe8681
MD5 938b57bc4a060cec427345f5b603531f
BLAKE2b-256 f66555592fa38a1cf7aa2b664be11e6e4ea1301d53955abba9bd43f3f551d1d4

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp311-cp311-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a52806dfadf042e3617f0d1229ea86743a36d2fb100dd90aa5892de447cafb74
MD5 cce90acc3d27f94109704166d541eed9
BLAKE2b-256 280667992b58e0cccf2471d734a479dc4eec5d86a31e4be62aab83751727379f

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 90c8ada7641390446d75357e3cbb35fcd59ccd195a3fc25203110353dfa00f83
MD5 bd0b48a2a5e07d1aaea957fda9663225
BLAKE2b-256 bcf6cb82717065f8e36df7e4dc1cc826e081e871cd252ece5163452747d75e46

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c7d45ca9d7143843329de105aa3bf0fa36a1c266c278e3b3df93ae71b65a5738
MD5 ee0d0e395f8974402ac7b5da82a044f3
BLAKE2b-256 ccb90fe9d4707c66b42f6707195d58e046fd004e66781cd9e2ab6090b7b42cb3

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 749e73c3032500b33576219c5c211e99f61e3f67dd56588fb6a110eac7089ac6
MD5 80c3e010b269bb859d42cd014c977611
BLAKE2b-256 02822c4ff6384bd19bb10fb101055c940cd62372336136d50d29dffa38862d54

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: tokie-0.0.10a1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tokie-0.0.10a1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f8c6375ba71498bd326d94975863aeaff97ba903a622b5573d8bf01aa91acf3a
MD5 36b9db154f5126561cb74148a7f069ec
BLAKE2b-256 0b7b4865571d718a14741bacec6d1709955881c2e75f341dc74d6e719fcb1334

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp310-cp310-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 1e85b4e306438dbefa7a88a7b71a9df09686c8d028f87abc8ef10496d463eb76
MD5 b34d9bec32673c8b00af9a2b2178bb52
BLAKE2b-256 095667cbd8a2f52d0715ca02f9968aa8315c2574f4769e236dae7d8168a01373

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d01fb417101a2176c07b56fe779eca9491a04adb9225f3459f9875aa6aeec81b
MD5 512882eef802e45d83e325d52deeb03b
BLAKE2b-256 4a81f82e3f17129f1e53893d94b42ab2ad7799520759322deeca90764c005d6a

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cb6e4623506e6421e81510b22284f49b6cd8dc70c2281687c5869dc89eb501ee
MD5 37041163f2da0b4b5858aa126bf538e6
BLAKE2b-256 677febb832d0e66e11a81b3e9716343bcdae82b2984c5fd93a8c689c9d3c04b5

See more details on using hashes here.

File details

Details for the file tokie-0.0.10a1-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tokie-0.0.10a1-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8b4760024c27c55f55188ed2cea1d122684e71c90c75f3b675cc62d3ed223780
MD5 ce31d1dd5d447c066c679636b551f776
BLAKE2b-256 9da3cbcae90920847d6155ced5b17ddf45d78572f78b1494de4bc449be45bac5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page