Skip to main content

Tokenize your documents at GB/s

Project description

Gigatoken

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

Tokenize your text data at GB/s!

GPT-2 Speedup

Keep in mind that both HF tokenizers and tiktoken are already running multithreaded Rust!

What is Gigatoken?

Gigatoken is the fastest tokenizer for language modeling. It supports a wide range of CPU hardware, and nearly all commonly used tokenizers.

Installation

pip install gigatoken

Usage

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"])

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.

Gigatoken API (Fastest)

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.

FAQ

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 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). In addition, interactions with Python are minimized, and threads are minimally interacting with each other.

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 \
    --in-memory --validate --comparison-limit 100MB \
    --separator "<|endoftext|>"
gigatoken:    1.557 s |   11920.51 MB at  7653.89 MB/s |  2701.65 Mtok at 1734.67 Mtok/s
       hf:   15.268 s |     100.00 MB at     6.55 MB/s |    22.76 Mtok at    1.49 Mtok/s
gigatoken is 1168.58x faster than hf (by MB/s)
validation OK: 20401 documents match

This example uses the train sample from this dataset. You can see help for these flags with uvx gigatoken bench --help. Keep in mind that you might need to run twice on macOS to get a good reading since the first run will always perform a security scan.

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.


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

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

gigatoken-0.7.0.tar.gz (1.3 MB view details)

Uploaded Source

Built Distributions

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

gigatoken-0.7.0-cp310-abi3-win_arm64.whl (3.4 MB view details)

Uploaded CPython 3.10+Windows ARM64

gigatoken-0.7.0-cp310-abi3-win_amd64.whl (4.2 MB view details)

Uploaded CPython 3.10+Windows x86-64

gigatoken-0.7.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

gigatoken-0.7.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.5 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

gigatoken-0.7.0-cp310-abi3-macosx_11_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gigatoken-0.7.0-cp310-abi3-macosx_10_12_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file gigatoken-0.7.0.tar.gz.

File metadata

  • Download URL: gigatoken-0.7.0.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gigatoken-0.7.0.tar.gz
Algorithm Hash digest
SHA256 fddde7016e14d69df1587218792dd8e9122cc9c68fc99e0bd66e42a02f937e69
MD5 c406ba8c2868f544122f15299b65e405
BLAKE2b-256 027380eba2318aca05339b6f82f2bd315fec217046a7fa62b7bfa9f6f449e169

See more details on using hashes here.

File details

Details for the file gigatoken-0.7.0-cp310-abi3-win_arm64.whl.

File metadata

  • Download URL: gigatoken-0.7.0-cp310-abi3-win_arm64.whl
  • Upload date:
  • Size: 3.4 MB
  • Tags: CPython 3.10+, Windows ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gigatoken-0.7.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 aec607b965451a28e0326e99c75825a0bc92152bc3c7b85ed6eff9d25347a7a7
MD5 699595a58685de04609faba2afe08545
BLAKE2b-256 d1d36e82d88ff45e80edaf6dae0ddc4c660f85dd4a76d8f1f32c3118ea7ad7e6

See more details on using hashes here.

File details

Details for the file gigatoken-0.7.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: gigatoken-0.7.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 4.2 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gigatoken-0.7.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 92a9762834f78378000f240dbd0449ef8459d14e287eedf5ea34f2f86fd652fa
MD5 46e6111048bb1cef9ca0e0f71da1be5b
BLAKE2b-256 85b113a40ebdca1d45364ffed00b80101352f28f99b8ab254303c8d61c9a0d14

See more details on using hashes here.

File details

Details for the file gigatoken-0.7.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: gigatoken-0.7.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 4.2 MB
  • Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gigatoken-0.7.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 55fd687f04b06002b44b2efa7a71220785f94e61c15b6e52e293ad790f9661e4
MD5 fd7fb6cf0636ed1a357f3ee4cb7043b7
BLAKE2b-256 5412d2d0369d4535b3bf0c6d1809ce4748e801c1be7038d4e42475bf4d92e64a

See more details on using hashes here.

File details

Details for the file gigatoken-0.7.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

  • Download URL: gigatoken-0.7.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 3.5 MB
  • Tags: CPython 3.10+, manylinux: glibc 2.17+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gigatoken-0.7.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b0e5acc148ef105749cf3e417a6b7a8bb56351f4e8bcf56a3df25c8f9f48df0a
MD5 0ccb45de07059cddafa457884d1562e7
BLAKE2b-256 2aa617d60a80ec416c71befc04437dd1885ed121b4e28d2f55a6918af37d7227

See more details on using hashes here.

File details

Details for the file gigatoken-0.7.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: gigatoken-0.7.0-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 3.3 MB
  • Tags: CPython 3.10+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gigatoken-0.7.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c989969609f73e97b42cb5c7e2f4e30388ac1db125a60224cde6855bc24a0c5f
MD5 8632c66d8f3dc48e8033bb0425711760
BLAKE2b-256 18e3174ef6ef72e63328a8ba70fcde95a1cf57b9d924d4eaea4938f216d8fba5

See more details on using hashes here.

File details

Details for the file gigatoken-0.7.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: gigatoken-0.7.0-cp310-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 4.1 MB
  • Tags: CPython 3.10+, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gigatoken-0.7.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8555831a9c672792d75a1afcd2a0caa147796836296e61f9e6171a5d18473fb5
MD5 3c9c3b7b318ee9bf0d7257a1a008518f
BLAKE2b-256 dca2df1f170b89f4202f5b004c3a763a7a059f2eaffe2934d84a1809048b5377

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