Skip to main content

Tokenize your documents at GB/s

Project description

Gigatoken

~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.6.0.tar.gz (690.2 kB view details)

Uploaded Source

Built Distributions

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

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

Uploaded CPython 3.10+Windows ARM64

gigatoken-0.6.0-cp310-abi3-win_amd64.whl (3.9 MB view details)

Uploaded CPython 3.10+Windows x86-64

gigatoken-0.6.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.9 MB view details)

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

gigatoken-0.6.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.6.0-cp310-abi3-macosx_11_0_arm64.whl (3.2 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gigatoken-0.6.0-cp310-abi3-macosx_10_12_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: gigatoken-0.6.0.tar.gz
  • Upload date:
  • Size: 690.2 kB
  • 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.6.0.tar.gz
Algorithm Hash digest
SHA256 8b053614d69065ebfeac9ea927635d7de81e1914f60e13e97ed34a6535630c00
MD5 aab7834436c9400a39bd48c4af50f40c
BLAKE2b-256 c0e953f2f7a5dd796ef0f63985c12a9e541119ce30019ac322b997a39e854c9c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.6.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.6.0-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 4358fc3dcaefe15a08d18468c1641cc21ff158c2c6fe4ce8fe5f961e10a9000f
MD5 abe1521427e1ca9c1fd54f44ae67161f
BLAKE2b-256 9a03a10ce5627ba4ea95af586b311526b033d78a347548d9998389d21f2701cd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.6.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 3.9 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.6.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 36755f70888f50313aba4d3cab7b20792f939c2c8e116b00a096b8151261bc49
MD5 b610a997785e3b4afc9426ab44b0e000
BLAKE2b-256 927350a5485dd6253cba339cc8240cad192766c8066746638e110cc791dcfff3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.6.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 3.9 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.6.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 acd922bf19933dda4344e989501c3a82709d13382ee5979e8ff2b80c80ae7ce4
MD5 08d9686e5d5bbb1d9c5a90653b5d8664
BLAKE2b-256 3b08aebb1b6476c98e5f9dc8f3e66c0397100520e70a9eaf2a5b40907a3a3b8f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.6.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.6.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 87c535a644a2328b2418bc081eac89883abae948c8e51842dcfbd6cac187429d
MD5 a9a1e39268e2e4d2f008c75970ab4f07
BLAKE2b-256 ba1e3b5c64cd56a67ee9b8d5a5fd14239567b9650e829a9899de77ce84ee6719

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.6.0-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 3.2 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.6.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5d6e7e09b88ef403a69f89b40bd83be1ead8b65ce1d7b5a168f987409086d456
MD5 3ccbb369d9c29e28bc353439e12d6bd8
BLAKE2b-256 f890e46ac6bdf229482f08f016befc1cff49d910aa8f397ac660eed496fe95de

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.6.0-cp310-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 3.7 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.6.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f23e08326ba6c33f6aca83fae61e20965f6195931da4b37be08384c855f400e4
MD5 aa5f43e84a6594d07a5ac2ef0aaf2c37
BLAKE2b-256 971371afdfc3518e9e9ac7e9a621c257220a035a0f0819544084589f10e098ba

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