A fast WordPiece tokenizer implementation in Rust with Python bindings
Project description
WordPiece Tokenizer in Rust
A fast implementation of the WordPiece tokenizer in Rust with Python bindings using PyO3. This implementation uses a trie-based approach for O(n) time complexity, making it significantly faster than traditional O(n²) implementations.
Performance
The tokenizer uses a trie data structure to efficiently find the longest matching subwords, resulting in:
- O(n) time complexity for tokenization (vs O(n²) in naive implementations)
- O(m) space complexity where m is the total size of the vocabulary
- Fast prefix matching using a character-based trie
- Efficient token ID lookup
Key Features
- Fast tokenization using Rust
- Python bindings via PyO3
- Unicode normalization (NFKC)
- Configurable unknown token and maximum input length
- Support for custom vocabularies
Installation
From Source
- Make sure you have Rust and Python installed
- Install maturin:
pip install maturin - Build and install:
cd wordpiece_rs
maturin develop
Usage
import wordpiece_rs
# Create a vocabulary (token -> id mapping)
vocab = {
"[UNK]": 0,
"[CLS]": 1,
"[SEP]": 2,
"want": 3,
"##ed": 4,
"to": 5,
"go": 6,
"home": 7,
}
# Initialize the tokenizer
tokenizer = wordpiece_rs.WordPieceTokenizer(vocab)
# Tokenize text
tokens = tokenizer.tokenize("wanted to go home")
print(tokens) # ['want', '##ed', 'to', 'go', 'home']
# Encode text to token IDs
ids = tokenizer.encode("wanted to go home")
print(ids) # [3, 4, 5, 6, 7]
# Decode token IDs back to text
text = tokenizer.decode([3, 4, 5, 6, 7])
print(text) # "wantedtogohome"
Customization
You can customize the tokenizer by providing optional parameters:
tokenizer = wordpiece_rs.WordPieceTokenizer(
vocab,
unk_token="<UNK>", # Default: "[UNK]"
max_input_chars_per_word=100 # Default: 200
)
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wordpiece_rs-0.1.0.tar.gz.
File metadata
- Download URL: wordpiece_rs-0.1.0.tar.gz
- Upload date:
- Size: 62.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c71dcd01544e1b5f5103dcffc3ed6fffd4ac77a2f77e73a376e6b396a2f19dd2
|
|
| MD5 |
2a613102128b6436857e904649008444
|
|
| BLAKE2b-256 |
5f2d744206ad0812070d6de3e2a4d36e199eaed9e30d37c964d97b0423c007d4
|
File details
Details for the file wordpiece_rs-0.1.0-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: wordpiece_rs-0.1.0-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 835.6 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90a73d2ddd0ca3f8653506d3d01de438b425f1586295e19dd64b5f670cf4ec99
|
|
| MD5 |
d3a21eab3027cab34e93def0f7145487
|
|
| BLAKE2b-256 |
bbafd1dc2d2b304bae0eaa5c659a57363e3c46066c389e0d46ce1055c3401825
|
File details
Details for the file wordpiece_rs-0.1.0-cp313-cp313-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.
File metadata
- Download URL: wordpiece_rs-0.1.0-cp313-cp313-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.13, macOS 10.12+ universal2 (ARM64, x86-64), macOS 10.12+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a438e01faa12edebe876e2d4bbbd8b53e39ef79535a23221e9626db74ad3a2c0
|
|
| MD5 |
ac573f743092d0c51b07536cbd374ce3
|
|
| BLAKE2b-256 |
5d2184071ee3762792f93d21d0ccfcc7aece4110b53b658ce5d6ab56bb35681e
|
File details
Details for the file wordpiece_rs-0.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: wordpiece_rs-0.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.7m, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
931b6e9cf50f85d2c3cee2378da16b4a3e73849e932f1e59e41e9fdc5111af2a
|
|
| MD5 |
d7c67e76f129f1b3396ee8bfcf1438c1
|
|
| BLAKE2b-256 |
ea75b7b603026390d240300570d8673f7807b9ab107f214708dfe6e355a08e01
|