This repository was archived by the owner on Nov 22, 2026. It is now read-only.
Repository navigation
mojo-tokenizers v0.1.0
mojo-tokenizers v0.1.0
Released: 2026-11-04 · Commit: 4f45ef8 · Previous: first release
What it does
A Hugging Face tokenizer.json runtime for Mojo: byte-level BPE (GPT-2, Llama 3, Qwen), WordPiece with BertNormalizer (BERT) and Unigram with a
Precompiled normalizer (T5, XLM-R). Tokenizer(path), encode(text), decode(ids); ids equal the Python tokenizers crate's. Mojo 1.2 on Linux;
src/json and src/uregex are vendored, so a clone builds with mojo run -I src.
Highlights
- Ids and decodes identical to the
tokenizers0.23.3 crate on 6 real tokenizer files x 2000 generated texts (ASCII, 14 scripts, emoji, whitespace runs, numbers, added tokens). - Encoding 1 MB is 2.5 to 4 times faster than the crate through Python (80 to 140 ms against 292 to 392 ms), with less memory.
- An unsupported model, normalizer or pre-tokenizer raises at load; it never mis-tokenizes.
Changes
Added
- Byte-level BPE with a flat packed merge table and a heap merge,
ignore_merges, array or string merges, per-piece cache. - WordPiece with BertNormalizer (clean, CJK spacing, accent stripping, lowercase) and the BERT pre-tokenizer and decoder cleanup.
- Unigram (Viterbi) with the sentencepiece Precompiled normalizer, Replace, WhitespaceSplit and Metaspace.
- Added tokens cut out of the text first, with
lstripandrstrip. scripts/check.sh(oracle gate),scripts/selfcheck.sh(10 mutants),scripts/bench.sh,recipe.yaml.
Verified
| Check | Result |
|---|---|
scripts/check.sh (6 tokenizers, 12000 texts vs the crate, round trip for byte-level, API test, quickstart output, README block equals the quickstart) |
PASS all |
scripts/selfcheck.sh (10 mutants of the source) |
PASS selfcheck, every mutant rejected |
scripts/bench.sh (1 MB, single thread, crate through Python) |
GPT-2 80-87 ms vs 334-362, Qwen2.5 87-97 vs 375-392, Llama 3 79-92 vs 307-315, BERT 78-82 vs 325-340, T5 107-109 vs 295-307, XLM-R 133-140 vs 292-305 |
mojo-lib-audit |
PASS on the tagged tree |
Known limits
NFCnormalizers are accepted but not applied: feed NFC text. Other normalizers,Splitbehaviors other than Isolated,add_prefix_space, Metaspaceprepend_scheme: firstraise.- Truncation, padding, post-processing and
single_wordare ignored. - The BERT tables are Unicode 16.0.0, uregex 18; a code point assigned in between can differ from the crate.
src/tokenizers/tokenizer.mojois 594 code lines (mojo-loc); the 400-line budget was not met (n1-report.md).- The recipe was generated, not built with rattler-build; the fixtures' upstream URLs in
tests/fixtures.txtwere not fetched except T5. UNVERIFIED.
Upgrade / run
git clone <repo> && cd mojo-tokenizers && mojo run -I src examples/quickstart.mojo
Commits
4f45ef8chore: sync vendored src/uregex to mojo-uregex 2ce35f946da45cdocs: Unigram in README, api, design and perf; bench numbers for 6 tokenizers543c9b2perf: ASCII fast paths in the BERT normalizer and pre-tokenizer3276264feat: Unigram (T5, XLM-R) with Precompiled, Replace, WhitespaceSplit and Metaspacedda8f7cfeat: WordPiece with BertNormalizer and BertPreTokenizer (BERT)4dfdf9ffeat: byte-level BPE from a tokenizer.json, encode and decode