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|>"
      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.584 s |   11920.51 MB at 20412.35 MB/s |  2701.65 Mtok at 4626.23 Mtok/s
       hf:    3.738 s |     100.00 MB at    26.75 MB/s |    22.76 Mtok at    6.09 Mtok/s
gigatoken is 763.08x faster than hf
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.

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 8 hours!

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.1.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.1-cp310-abi3-win_arm64.whl (3.4 MB view details)

Uploaded CPython 3.10+Windows ARM64

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

Uploaded CPython 3.10+Windows x86-64

gigatoken-0.7.1-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.1-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.1-cp310-abi3-macosx_11_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

gigatoken-0.7.1-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.1.tar.gz.

File metadata

  • Download URL: gigatoken-0.7.1.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.1.tar.gz
Algorithm Hash digest
SHA256 90ccfa906f5728d5fa2752cf1c502a98e6a22387741be1c811ef1b24826a23a4
MD5 e80e0a3eb5baf82750bfd5573447a675
BLAKE2b-256 f9cd84b85492ea43ebdba780a2580eb4d452142473ce1dd070164312369dd310

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.7.1-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.1-cp310-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 efb6d647e971b0268bda89f96a823cde5a56a783f7f452d1d11fdb7b22c86536
MD5 c553d4da6f491c830081baeb64e871d7
BLAKE2b-256 67ea712150aa700dfaebc1f0d5f6f3a462182b064f4eaec976eb0cfa210cd0b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.7.1-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.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 5e3e0fcc12c6c7d563a2c125d30ef20f2aa6888611ea1fb7654a2d093415693a
MD5 e45533e0732919444bdcf3852913599e
BLAKE2b-256 02790604de8a441d916f355d9de5c7aaceb390a17c8c4b38db31f69c990aed5b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.7.1-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.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3b99d7efe11af4070daa1bf94132bf750fd9cfc5c478b624332b9c38299435ca
MD5 f9bef22fc6ef4562a804eae908d8519d
BLAKE2b-256 f9ae9d28177a991b52853bf75ba0cfe8b72a837d9e47741fba6a26f47c7c6ab2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.7.1-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.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8adaf333b60df6e0a178cf293089ca0165ad41364beb1b2a8409f404475d2468
MD5 df019481ec06c254a6893a9bffea7eea
BLAKE2b-256 fb21f560534d61d72071001af9a36b29d85a7eb687037688bac5b7074356cf6b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.7.1-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.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1d7b71b1a9b0b9e455e24aae609748b4ca0ba7186c13be40537e5ff314c0d8c5
MD5 1ac1d1bf25cf066290b6b35613b851de
BLAKE2b-256 97f9679c04b1b9427dd3306d5a807bf932394c20f108a30e7079a59be1c17d5c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gigatoken-0.7.1-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.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 bbd990dabe41646cd082c6664a35b54fcfb481f3381f8114773ab947be96b57b
MD5 3bf3e0f352871bedb72b0f93b45938d6
BLAKE2b-256 db4ee810878ce7d2708c7e933a3c068ec367eae26427df1ce4b60ba6e83cb29e

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