snaptokens
Fast BPE tokenization in Rust. The fastest in the current generic BPE matrix, in fact.
snaptokens is an optimized fork of fastokens that is significantly quicker and simplified to work in sinter. This repo will be archived once ported to sinter.
Give snaptokens a local Hugging Face tokenizer.json and get token IDs back. Opt in to a .tkz copy when faster future loads matter.
The current twelve-tokenizer, thirteen-host comparison is 2.19× faster than Gigatoken, 14.96× faster than upstream fastokens, and 49.39× faster than Hugging Face by geometric mean of paired medians. Snaptokens wins 777/780 Gigatoken cells; the full report keeps all three losses and the no-Gemma sensitivity result visible.
Optimizations
- Hand-written scanners replace general regex machinery for recognized production tokenizer patterns, with a table-driven ASCII path and exact Unicode fallback.
- Split, ByteLevel transformation, and BPE are fused so raw bytes map directly to initial token IDs and cache hits skip ByteLevel work entirely.
- BPE uses precomputed flat lookups: byte-to-token IDs, all 65,536 initial byte pairs, open-addressed merge ranks, and CSR merge adjacency.
- Pre-tokenized text stays in one contiguous buffer with byte ranges instead of allocating a string for every piece.
- Thread-local and shared caches are amortized across whole chunks, while a fixed Rayon pool keeps worker caches warm and balances uneven BPE work.
- Non-ByteLevel tokenizers split only at vocabulary-proven unbridgeable byte pairs; byte-fallback models stay on the normal exact merge path.
- Opt-in
.tkzsidecars cache validated native construction data. Across twelve models and thirteen hosts, direct.tkzloads are 1.55× faster than Snaptokens JSON and artifacts are 24.9% smaller by geometric mean.
Benchmarks
The 2026-07-31 suite uses DeepSeek R1, Gemma 3, GLM 4.7, GPT-2, GPT-OSS, Llama 3, MiniMax M2.1, Mistral Nemo, Nemotron 3, Phi-4 mini, Qwen 2.5, and Qwen 3. It runs five batch and input shapes on Apple M2 plus twelve four-core Modal placements across AWS, GCP, and OCI.
Every timed engine first passes Hugging Face token-ID parity. Gigatoken and Snaptokens cover all twelve models; upstream fastokens covers nine. Flat-ragged engines are compared only with the same flat-ragged output contract, nested engines only with nested output.
tokenizer.json and .tkz produce the same steady-state Snaptokens encoder, so the format comparison measures fresh load, first encode, disk size, and memory rather than drawing two redundant throughput bars. TKZ loads 1.55× faster than Snaptokens JSON, but is still 1.34× slower than Hugging Face JSON in this suite.
The full benchmark report contains the per-host, per-model, per-shape, load, footprint, variability, correctness, and limitation analysis. The complete accepted evidence includes every raw round, hardware capture, hash, summary, and an empty failure ledger.
Install
Install the Python package:
pip install snaptokens
Or add the Rust crate:
[dependencies]
snaptokens = "0.2"
Use
Python:
from snaptokens import Tokenizer
tokenizer = Tokenizer.from_file("/path/to/tokenizer.json")
ids = tokenizer.encode("Tokenization should not be the bottleneck.").ids
Pass tkz_cache=True to create and reuse the adjacent .tkz file. A .tkz
path loads directly without that flag.
To replace a Transformers v4 or v5 fast-tokenizer backend:
import snaptokens
snaptokens.patch_transformers()
Call snaptokens.unpatch_transformers() to restore the original backend.
Rust:
use std::{error::Error, path::Path};
use snaptokens::Tokenizer;
/// Loads one local tokenizer and encodes a prompt.
fn main() -> Result<(), Box<dyn Error>> {
let tokenizer = Tokenizer::from_file(Path::new("/path/to/tokenizer.json"))?;
let ids = tokenizer.encode("Tokenization should not be the bottleneck.")?;
println!("{ids:?}");
Ok(())
}
Download the model yourself and pass the local JSON path. snaptokens never tries to infer or download a Hugging Face model.
Optional .tkz cache
let tokenizer = Tokenizer::from_file_with_tkz_cache(
Path::new("/path/to/tokenizer.json"),
true,
)?;
The first enabled load atomically writes tokenizer.tkz; later loads validate and reuse it. Passing that .tkz path loads it directly without creating another copy. Tokenizer::from_file remains JSON-only and never writes a cache.
Scope
Snaptokens is only built for inference on BPE tokenizers, and to do just that, well.
Real-tokenizer validation covers GPT-2, GPT-OSS, DeepSeek, MiniMax, Qwen and Qwen3.5, GLM, Nemotron, Mistral, Gemma 4, Phi-4, and Kimi K2.5 families, including added-token flags and byte fallback where present.
Credits
Licensed under Apache-2.0.
Metadata
Release files for snaptokens 0.2.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| snaptokens-0.2.6.tar.gz | 161.2 kB | With provenance | Details for snaptokens-0.2.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| snaptokens-0.2.6-cp39-abi3-manylinux_2_34_x86_64.whl | CPython 3.9 | abi3 | Linux glibc 2.34+ x86-64 | Details for snaptokens-0.2.6-cp39-abi3-manylinux_2_34_x86_64.whl |
Total release size: 2.1 MB
Release files / snaptokens-0.2.6.tar.gz
| Download URL | snaptokens-0.2.6.tar.gz |
|---|---|
| Size | 161.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aa781a36d618b670e530f6c9e5f7c91400efd5129e0e5948d78f43076631dd7c
|
|
BLAKE2b-256 checksum How to use checksums |
26f319b70a155b36dbaf44ce3549075251cf00d539e07ed54d36f0cb3ce2bb29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.
Transparency logRelease files / snaptokens-0.2.6-cp39-abi3-manylinux_2_34_x86_64.whl
| Download URL | snaptokens-0.2.6-cp39-abi3-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 1.9 MB |
| Tags | CPython 3.9 Linux glibc 2.34+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
7537370eaae77f0982cb9ef4ca3bb2f8c91e095317e3fe06a6964f2311a6af79
|
|
BLAKE2b-256 checksum How to use checksums |
a45fb3d23407602240f0ca4c2116a5cf21fb575f3213f0713224611fe750763f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.
Transparency logSecurity for snaptokens 0.2.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for snaptokens-0.2.6.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.