marcelroed/gigatoken: Language model tokenization at GB/s · GitHub

marcelroed/gigatoken: Language model tokenization at GB/s · GitHub

~1000x faster than HuggingFace’s tokenizers, drop-in replacement.

Tokenize your text data at GB/s!

Note that both HF tokenizers and tiktoken are already running multithreaded Rust!

Gigatoken is the fastest tokenizer for language modeling.
It supports a wide range of CPU hardware, and nearly all commonly used tokenizers.
See the Benchmarks section for detailed throughput numbers across tokenizers and CPUs.

Gigatoken can be used with its own API, or in compatibility mode with HuggingFace Tokenizers or Tiktoken.

Compatibility Mode (Easiest)

import gigatoken as gt

# Minimum change from existing HuggingFace tokenizers usage (compatibility mode)
hf_tokenizer = ...
tokenizer = gt.Tokenizer(hf_tokenizer).as_hf()

# tokenizer can be used in the same contexts as hf_tokenizer
tokens = tokenizer.encode_batch(["This is a test string", "And here is another"])

# OR with tiktoken
tiktokenizer = ...
tokenizer = gt.Tokenizer(tiktokenizer).as_tiktoken()

# Now works like existing tiktoken tokenizers
tokens = tokenizer.encode_batch(["This is a test string", "And here is another"])

A substantial amount of effort has been put into making sure the outputs match exactly with what you would get with HuggingFace Tokenizers in this setting, but this is at a non-negligible cost to performance.
You can still expect way faster performance across the board, but not quite the 1000x you will get with the Gigatoken API.

import gigatoken as gt

tokenizer = gt.Tokenizer("Qwen/Qwen3-8B")  # Accepts HF model names
file_source = gt.TextFileSource(["owt_train.txt"], separator=b"<|endoftext|>")
tokens = tokenizer.encode_files(file_source)

Using the Gigatoken API lets the Rust implementation read data directly, and skips as much overhead as possible while allowing for maximum parallelism.
Keep in mind that passing Python data structures through this API still incurs the overhead of reading from Python.

Encoding throughput on owt_train.txt (11.9 GB) — AMD EPYC 9565 72-Core Processor x 2 sockets (144 cores)

TokenizergigatokenHF tokenizerstiktokenvs HFvs tiktoken
GPT-224.53 GB/s24.8 MB/s36.0 MB/s989×681×
Phi-424.00 GB/s29.9 MB/s801×
GPT-OSS23.96 GB/s49.7 MB/s42.8 MB/s482×560×
OLMo 2 / 323.06 GB/s27.7 MB/s833×
Nemotron 322.79 GB/s49.4 MB/s462×
Qwen 322.16 GB/s34.2 MB/s648×
Llama 3 / 3.1 / 3.222.15 GB/s48.5 MB/s457×
GLM 520.97 GB/s74.8 MB/s280×
Llama 3.320.82 GB/s48.3 MB/s431×
Llama 420.77 GB/s72.7 MB/s286×
GLM 420.61 GB/s72.3 MB/s285×
Phi-4-mini20.05 GB/s27.6 MB/s726×
DeepSeek V3 / R1 / V419.69 GB/s26.2 MB/s750×
Qwen 2 / 2.519.12 GB/s27.7 MB/s691×
Kimi K218.85 GB/s
Qwen 3.5 / 3.615.49 GB/s27.7 MB/s558×
Gemma 44.82 GB/s334.1 MB/s14×
ModernBERT4.18 GB/s26.9 MB/s155×
Mistral 7B v0.33.57 GB/s354.7 MB/s10×
TinyLlama / Phi-3 (Llama 2)3.48 GB/s323.6 MB/s11×
CodeLlama3.47 GB/s347.4 MB/s10.0×
Gemma 33.43 GB/s357.2 MB/s9.6×
Gemma 12.51 GB/s342.2 MB/s7.3×


Encoding throughput on owt_train.txt (11.9 GB) — Apple M4 Max (16 cores)

TokenizergigatokenHF tokenizerstiktokenvs HFvs tiktoken
GPT-28.79 GB/s6.9 MB/s62.8 MB/s1,268×140×
Nemotron 37.82 GB/s10.9 MB/s715×
Phi-47.76 GB/s7.7 MB/s1,012×
Llama 3 / 3.1 / 3.27.60 GB/s11.2 MB/s676×
OLMo 2 / 37.56 GB/s5.8 MB/s1,299×
Llama 3.37.50 GB/s15.7 MB/s479×
Phi-4-mini6.97 GB/s7.2 MB/s964×
Kimi K26.88 GB/s
Llama 46.81 GB/s11.6 MB/s590×
Qwen 2 / 2.56.37 GB/s5.8 MB/s1,105×
Qwen 36.36 GB/s6.9 MB/s918×
Qwen 3.5 / 3.66.31 GB/s6.3 MB/s994×
GPT-OSS6.20 GB/s20.2 MB/s87.2 MB/s306×71×
GLM 46.17 GB/s15.8 MB/s392×
DeepSeek V3 / R1 / V45.68 GB/s7.2 MB/s788×
GLM 55.55 GB/s12.2 MB/s456×
ModernBERT2.64 GB/s5.8 MB/s452×
Mistral 7B v0.31.99 GB/s95.1 MB/s21×
Gemma 41.82 GB/s85.2 MB/s21×
CodeLlama1.73 GB/s80.2 MB/s22×
TinyLlama / Phi-3 (Llama 2)1.69 GB/s80.1 MB/s21×
Gemma 11.42 GB/s85.7 MB/s17×
Gemma 31.38 GB/s82.2 MB/s17×


Encoding throughput on owt_train.txt (11.9 GB) — AMD Ryzen 7 9800X3D 8-Core Processor (16 cores)

TokenizergigatokenHF tokenizerstiktokenvs HFvs tiktoken
GPT-26.27 GB/s59.0 MB/s92.1 MB/s106×68×
Phi-46.09 GB/s55.4 MB/s110×
OLMo 2 / 36.06 GB/s55.4 MB/s109×
Phi-4-mini5.80 GB/s54.6 MB/s106×
GPT-OSS5.68 GB/s79.6 MB/s112.7 MB/s71×50×
Qwen 35.34 GB/s54.4 MB/s98×
Qwen 2 / 2.55.30 GB/s51.7 MB/s103×
Llama 3.35.26 GB/s79.9 MB/s66×
Llama 3 / 3.1 / 3.25.24 GB/s79.5 MB/s66×
Kimi K25.23 GB/s
Qwen 3.5 / 3.65.22 GB/s51.6 MB/s101×
Nemotron 35.20 GB/s79.0 MB/s66×
GLM 55.05 GB/s79.5 MB/s63×
GLM 45.04 GB/s79.5 MB/s63×
Llama 45.03 GB/s78.2 MB/s64×
DeepSeek V3 / R1 / V44.21 GB/s51.6 MB/s82×
ModernBERT2.84 GB/s52.1 MB/s54×
Mistral 7B v0.31.47 GB/s91.6 MB/s16×
Gemma 41.45 GB/s78.8 MB/s18×
CodeLlama1.38 GB/s85.2 MB/s16×
TinyLlama / Phi-3 (Llama 2)1.37 GB/s84.9 MB/s16×
Gemma 11.14 GB/s84.9 MB/s13×
Gemma 31.12 GB/s83.0 MB/s13×


Benchmark details

OWT (openwebtext) was chosen because it’s roughly representative of the text you get after extraction from CommonCrawl documents.
Gigatoken encodes the whole file un-split, and is thus doing more work than the other tokenizers to find the split boundaries and automatically parallelize.
HuggingFace tokenizers (encode_batch_fast) gets the first 100 MB and tiktoken (encode_ordinary_batch) the first 1 GB, both presplit on <|endoftext|>.
This is fair because neither of the compared tokenizers do caching, meaning the speed is roughly uniform throughout processing.
Tiktoken rows are currently only filled in for tokenizers with official support.

The slowest rows are the SentencePiece-based tokenizers, which are not well optimized in Gigatoken.

Each row is one distinct tokenizer (identical vocab/merges/pretokenizer), measured on a representative repo.
If you don’t see your tokenizer here, it’s likely based on some existing one.
For instance:

  • Llama 3 / 3.1 / 3.2 — Llama 3 / 3.1 / 3.2, DeepSeek-R1-Distill-Llama, Hermes 3, Saiga, and other Llama-3 finetunes
  • Llama 3.3 — Llama 3.3, Llama-3.1-Nemotron-Nano-VL, SmolLM3, Kanana 1.5, jina-embeddings-v5, Ultravox
  • Qwen 2 / 2.5 — Qwen 2 and 2.5 (incl. Coder and VL), Qwen3-Coder, Qwen3-VL, DeepSeek-R1 Qwen distills, MiMo V2.5, MiniCPM-o 2.6, InternVL3
  • Qwen 3 — Qwen 3 (incl. Embedding and Reranker), Qwen2.5-Omni, Qwen3-VL-Embedding, MiMo V2.5 Pro, jina-reranker-m0, pplx-embed, MOSS-TTS, Zeta
  • DeepSeek V3 / R1 / V4 — DeepSeek V3 / V3.1 / V3.2, R1, V4 Flash and Pro, DeepSeek-VL2
  • GLM 4 — GLM 4.1V, 4.5, and 4.7
  • GLM 5 — GLM 5 / 5.2 and GLM-4.7-Flash
  • Nemotron 3 — Nemotron 3 Nano, Super, and Ultra
  • Kimi K2 — Kimi K2 / K2.5 / K2.6 / K2.7, Kimi-Linear, Kimi-VL, Moonlight
  • Phi-4-mini — Phi-4-mini and Phi-4-multimodal
  • TinyLlama / Phi-3 (Llama 2) — TinyLlama, Phi-3-mini, Phi-3.5-mini and Phi-3.5-vision (the Llama 2 vocab)
  • Gemma 3 — Gemma 3 (270M–27B) and EmbeddingGemma
  • Gemma 4 — Gemma 4 (dense, MoE, and E-series) and DiffusionGemma

Q: Did you just way over-optimize for a specific CPU and tokenizer? How is it so fast?

No, I way over-optimized for every combination of these!
The results are very consistent across CPUs (modern x86 and ARM), and across specific tokenizers.

The major improvements are in optimizing heavily an implementation that usually is outsourced to a Regex engine (pretokenization) using SIMD, minimizing branching and other tricks, as well as heavily optimizing caching of pretoken mappings (if a word has been seen before, look it up its encoded tokens efficiently).
Caching is a very hard problem in this domain since the cache grows very quickly, and pretoken distributions are very long-tailed.

Some gains are also achieved from minimizing interactions with Python, and avoiding communication between threads.

Q: How can I quickly check if my tokenizer is supported?

You can try it out without installing anything! The following command will validate and time tokenization for a given HuggingFace model repo:

# Download your data
wget https://huggingface.co/datasets/stanford-cs336/owt-sample/resolve/main/owt_train.txt.gz  # Just an example!
gunzip owt_train.txt.gz
uvx --with tokenizers gigatoken bench 'openai-community/gpt2' owt_train.txt \
    --validate --doc-separator "<|endoftext|>"
      cpu: Apple M4 Max, 16 cores
gigatoken:    1.432 s |   11920.51 MB at  8327.05 MB/s |  2701.65 Mtok at 1887.23 Mtok/s
       hf:   16.250 s |     100.00 MB at     6.15 MB/s |    22.76 Mtok at    1.40 Mtok/s
gigatoken is 1353.13x faster than hf
validation OK: 20401 documents match
      cpu: AMD EPYC 9565 72-Core Processor, 144 cores, 2 sockets
gigatoken:    0.486 s |   11920.51 MB at 24532.45 MB/s |  2701.65 Mtok at 5564.94 Mtok/s
       hf:    4.033 s |     100.00 MB at    24.80 MB/s |    22.76 Mtok at    5.63 Mtok/s
gigatoken is 989.21x faster than hf
validation OK: 20401 documents match

At the rates we see on the EPYC CPU, you could tokenize the entirety of Common Crawl (often considered to be the entire internet, 130 trillion tokens) in just under 6.5 hours!

This example uses the train sample from this dataset, and the CLI by default subsets to the first 100MB of the file for validation and comparison with HF.
You can see help for these flags with uvx gigatoken bench --help.
You might need to run your commands twice on macOS to get a good reading, since the first run will always perform a security scan, which will slow down the Rust code.

Q: I’ve found a mismatch/slow use-case, is this expected?

Most likely not! Despite reasonably wide testing I don’t have every use-case on hand, so please report anything you find in a GitHub Issue so I can address it as soon as possible.

If you use Gigatoken in your research, please cite it as:

@software{roed2026gigatoken,
  author = {Marcel R{\o}d},
  title = {{G}igatoken: SIMD and Cache Hierarchies for 1000x Faster Byte-Pair Encoding Tokenization on Modern CPUs},
  url = {https://github.com/marcelroed/gigatoken},
  year = {2026},
}
  • Python iteration is handled in Rust, but uses ABI3, which is slower than using internal version-specific CPython APIs. In the future I intend to specialize for each Python version to cut this overhead. Early experiments show a 2x speed improvement for overhead-bound cases.
  • File sinks are not yet implemented in the Gigatoken API.
  • WordPiece is not yet supported.
  • SentencePiece-based tokenization is not nearly as optimized as the more common BPE tokenizers. This is low priority for now since mostly Google models/BERT style models use SentencePiece.
  • Windows has not been tested much, so for now prefer using WSL.

AI Use Disclosure

A majority of this code base was crafted by hand without any use of AI (which can be seen from the project’s Git history).
In the final stages of the project, AI was used to assist:

  • Implementing the user-facing API
  • Widening of compatibility, for instance generalizing and porting the pretokenizer implementations to support more tokenizers, less interesting features like padding/truncation/unicode normalization
  • Porting SIMD strategies between AVX512/AVX2/NEON
  • Final profiling stages and the last ~4x worth of performance from eliminating branching and improving the pretoken cache hierarchy
  • Refactoring and code reuse

Source link

Share:

Atbildēt

3 latest news
News Archives
On Key

Related Posts

Claude Is Not a Compiler

Claude Is Not a Compiler

In early 2025, I wrote Is Claude a Compiler? At the time, my answer was: I don’t know. I’m now pretty sure the answer is

Solverwp- WordPress Theme and Plugin