Skip to main content

The fastest tokenizer for modern LLMs, up to 20x faster. Drop-in for transformers.AutoTokenizer, byte-exact, never quadratic.

Project description

tuetoken

The fastest tokenizer for modern LLMs, up to 30x faster.

tuetoken is a BPE tokenizer with a fast, safe Rust core. It is a drop-in replacement for 🤗 transformers.AutoTokenizer: it loads any model's own tokenizer.json and reproduces tokenization exactly (special tokens, chat templates, padding/truncation), up to 30x faster. It also loads OpenAI/tiktoken encodings natively, and its O(n) merger stays fast even on adversarial inputs (hashes, base64, minified code) where other tokenizers degrade to O(n²).

from tuetoken import AutoTokenizer

tok = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct")
out = tok.apply_chat_template(messages, add_generation_prompt=True)   # {"input_ids", "attention_mask"}

Detection is 100% config-driven (the model's tokenizer.json, never its name), so the same code works across families: Llama, Qwen, Mistral/Mixtral, DeepSeek, Gemma, Phi, GPT-OSS, GLM, Kimi, and more.

Install

pip install tuetoken

Build from source (development, or a platform with no prebuilt wheel):

git clone https://github.com/tuetoken-org/tuetoken && cd tuetoken
pip install maturin
maturin develop --release

Performances

Performances

Drop-in AutoTokenizer

The full 🤗 API, byte-exact with transformers.AutoTokenizer:

from tuetoken import AutoTokenizer
tok = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")

tok.encode("Hello <|eot_id|> world")                 # special-token aware -> list[int]
tok.decode(ids, skip_special_tokens=True)            # -> str
tok(texts, padding=True, truncation=True,            # batch dict: input_ids + attention_mask
    max_length=512, return_tensors="np")             #   (also "pt" for torch)
tok.apply_chat_template(messages, add_generation_prompt=True)  # -> {input_ids, attention_mask}
tok.apply_chat_template(messages, add_generation_prompt=True, return_dict=False)  # -> list[int]
tok.batch_decode(...) ; tok.convert_ids_to_tokens(...) ; tok.tokenize(...)
tok.bos_token_id ; tok.eos_token ; tok.pad_token_id ; tok.vocab_size

This matches transformers.AutoTokenizer token-for-token across byte-level models (Llama, Qwen, DeepSeek, …) and SentencePiece models (Mistral, Phi-3, CodeLlama, …).

OpenAI / tiktoken encodings

tuetoken loads OpenAI's encodings natively, with no tiktoken dependency, and is faster than tiktoken itself, by up to an order of magnitude on long inputs:

from tuetoken import Tokenizer
enc = Tokenizer.from_tiktoken("cl100k_base")   # also "o200k_base", "gpt2", ...
enc.encode_ordinary("Hello world")             # list[int]

Lower-level core

Tokenizer is the raw BPE engine (no special tokens or chat templates; that is what AutoTokenizer is for). Reach for it when you only need fast token ids or counts:

from tuetoken import Tokenizer
enc = Tokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct")   # or Tokenizer("tokenizer.json")

enc.encode_ordinary("Hello world")                # list[int]
enc.encode_ordinary_batch(texts, num_threads=0)   # parallel (0 = all cores), GIL released
enc.decode(ids) ; enc.count_tokens("Hi") ; len(enc)

For ML pipelines there are zero-copy numpy paths, a padded training-batch helper, and byte-span offsets:

import numpy as np
arr   = np.frombuffer(enc.encode_to_bytes(text), dtype=np.uint32)   # skip per-token boxing
text  = enc.decode_array(arr)
batch = enc.encode_batch(texts, max_length=512, pad_id=0)           # input_ids + attention_mask
ids, offsets = enc.encode_with_offsets("Hello café")                # byte spans, ByteLevel only

Coverage

tuetoken works with essentially every modern LLM tokenizer: ByteLevel BPE (Llama, Qwen, DeepSeek, Mistral/Mixtral, GPT-OSS, GLM, Phi, OLMo, Yi, …), SentencePiece (Llama-2, Mistral, Phi-3, CodeLlama, Gemma, …), and OpenAI/tiktoken encodings. We extend coverage constantly; if a tokenizer you need isn't supported yet, please open an issue.

Anything tuetoken can't reproduce exactly fails closed (raises at load) rather than mistokenizing, so you never get silently wrong tokens.

Linear-time on any input

Classic BPE is O(n²) per chunk and collapses on long, poorly-merging content (random identifiers, hashes, base64, minified code), tiktoken included. tuetoken's merger is O(n), so adversarial inputs that hang other tokenizers for minutes stay in the millisecond range, byte-identical.

Correctness

Every claim above is validated byte-exact against the reference tokenizers (transformers, tokenizers, tiktoken) on a large, adversarial corpus. That is the only reason those libraries appear in the test dependencies; they are not runtime dependencies of tuetoken.

Project details


Download files

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

Source Distribution

tuetoken-0.1.3.tar.gz (1.1 MB view details)

Uploaded Source

Built Distributions

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

tuetoken-0.1.3-cp313-cp313-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.13Windows x86-64

tuetoken-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

tuetoken-0.1.3-cp313-cp313-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

tuetoken-0.1.3-cp312-cp312-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.12Windows x86-64

tuetoken-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

tuetoken-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

tuetoken-0.1.3-cp311-cp311-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.11Windows x86-64

tuetoken-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

tuetoken-0.1.3-cp311-cp311-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

tuetoken-0.1.3-cp310-cp310-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.10Windows x86-64

tuetoken-0.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

tuetoken-0.1.3-cp310-cp310-macosx_11_0_arm64.whl (1.0 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file tuetoken-0.1.3.tar.gz.

File metadata

  • Download URL: tuetoken-0.1.3.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for tuetoken-0.1.3.tar.gz
Algorithm Hash digest
SHA256 942e0def2eefdbae73d1d2706ad3e8dabdf966fe2f6e6172b00e454b88cf6cd7
MD5 865204f7dc0aaf5eb3f0ed8952d480c2
BLAKE2b-256 fa015c75c6cbea82a880df79334caf1265b136ae7300fb1e3c80eb66010a0714

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3.tar.gz:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: tuetoken-0.1.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for tuetoken-0.1.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 498181f02749cb9939d3e3125fb58929e61ff12a841f780b42ced7bef80f9f71
MD5 dadd4c9b467177bedeb47dac42edc85c
BLAKE2b-256 574e36a4753785a1f825df30bfe79e0eaf36302a0ed122d32ac6c17d8598ed07

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp313-cp313-win_amd64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tuetoken-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9dc6d59d14b176790316f06c07a053c4809de0375a920c3ef0babe04c226a153
MD5 4e10d6ee64f7a1c55d182c243b756330
BLAKE2b-256 a4b8ca14cb2c2c418beb06953c6f49ab45f320c79fb7d99e0908262b56f93d37

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tuetoken-0.1.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b90a30f5d2f62e6b08bf1cb2509d6cf8d2d3d10fc2b6f540fe8eb50116c5a8a7
MD5 e22da787a5fdf5392bdde6a32283ad2b
BLAKE2b-256 e0a4b72f3f282f929482fb800be74137bb0c1b7c02fe468250c091b6ace4396a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: tuetoken-0.1.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for tuetoken-0.1.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 009c125ac07062ddcd1758c2c875c7b5e2f770922d57db1c77814d146661912a
MD5 8fd8cc16a7a16e66f0996c0eb6fdc3a8
BLAKE2b-256 6348b61fb8540789580fb2718165640b008ba8ba23ebdfcd2f411167996f50ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp312-cp312-win_amd64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tuetoken-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 02f238bfe3f609fca3381e19e6e9211d57bcf8811118d1bba3543f2c37fbc594
MD5 a0670ca38360901d359aea25244d170e
BLAKE2b-256 18db54e195dce2b92f16d82fb48e6f3fe21c0cfb7e1458eb7314cb65bb29dd5c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tuetoken-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3a132de19fc86fb864283364b9ba8e1b82c0a63c1c2fffcce85c4640f2d1b3d5
MD5 9fc1501b7b1de6f87516e6773957caae
BLAKE2b-256 536c43d12730fdf8d31762193e830d3d071d72504d03e04542795fbaada4fb44

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: tuetoken-0.1.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for tuetoken-0.1.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d8a42a2b6bbe46d6181f3b7a18638fa9c8fcc82c0a4e253cecfe5fb71778db40
MD5 08983d8b3c2c7d3c747ea56e2cb6b43d
BLAKE2b-256 c54013b4662f79537386bda2004a4a002f8e519a29f2486a07096cd568cf8017

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp311-cp311-win_amd64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tuetoken-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 60cca9044f882068783531d49f43dd7cbbafb6703b38fd87f3a069513fb59025
MD5 faee4f466737b04b0086b420b3f49b0a
BLAKE2b-256 ca1c12dae50b707f0f864faff00b1215548f7100a5056e74a274b6fa6220fe8c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tuetoken-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 55ceb20f08769f7f6c6bbff568fe95ca05690e2c0fc6a5ca1d3323fd43f01362
MD5 2fe7606047d458c8284e5653fc93d43f
BLAKE2b-256 33aa3b395ef1047fe580f567328de7e3af68333b51cdeeeb4689534f4e49675b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: tuetoken-0.1.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for tuetoken-0.1.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b7d4f3dafbc755bbed9d0b56d3015706426ce6b842d9fc3b3c2e4af00a989716
MD5 aef3133ffd82e07f84025dc4393107c6
BLAKE2b-256 38cdecd4c08d9afa791bba0e526a7dd8768900c750847cb4de4f353cb1a836aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp310-cp310-win_amd64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tuetoken-0.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9f8fef07bed8398221196c4b673c1ee863872fd24079a6bfa5e9e679bc957c1b
MD5 2bb260eb13d01f132d7b0df1ea14229c
BLAKE2b-256 ec21965fdbce94f7a194a9bf9af15a93331e5d087226a4b93fc5c3be280df062

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tuetoken-0.1.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tuetoken-0.1.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 edfc6d8d6a41e45ebfa64f3035213bfb9bf7f87a4234cb54448e77e17895fce9
MD5 ff34a79c85dba11dfd07409fe3261f9b
BLAKE2b-256 37d5551cd1f1552d16f6484be1a461da03ac870b655bd92862937dbc46257ab1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tuetoken-0.1.3-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: wheels.yml on tuetoken-org/tuetoken

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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