Skip to main content

Python bindings for the v11 knowledge-first tokenizer (pure Rust)

Project description

v11-tokenizer — Python bindings

Pure-Rust knowledge-first tokenizer, exposed as a Python module via PyO3.

Install

From source:

cd v-tokenizers/v11/python
maturin develop --release

Then from any Python script in the same venv:

import v11

tok = v11.Tokenizer.from_file("../v11/artifacts/v11.vocab.bin")
print(tok)                                         # <v11.Tokenizer vocab_size=71260>
print(tok.vocab_size)                              # 71260
print(tok.pad_id, tok.unk_id, tok.bos_id, tok.eos_id)  # 0 1 2 3

ids = tok.encode("def fibonacci(n):")
pieces = tok.encode_pieces("def fibonacci(n):")
text = tok.decode(ids)

API

method returns notes
Tokenizer.from_file(path) Tokenizer Load from a .vocab.bin file
tok.encode(text) list[int] Tokenize to ids
tok.encode_pieces(text) list[str] Tokenize to piece strings
tok.decode(ids) str Reverse to text
tok.id_to_piece(id) str | None Look up a single id
tok.piece_to_id(piece) int | None Look up a single piece
tok.vocab_size int Total pieces
tok.pad_id, unk_id, bos_id, eos_id int Special token ids
len(tok) int Same as vocab_size

Performance

Encode throughput on a single thread (release build):

~9.3M tokens/sec
~28 MB/sec

That's roughly 10-40× faster than the Python sentencepiece package on comparable inputs, because the longest-match is a single Aho-Corasick pass with no C++ FFI hops.

Build a wheel for distribution

maturin build --release
ls target/wheels/
# v11_tokenizer-0.1.0-cp312-abi3-macosx_11_0_arm64.whl

License

Apache 2.0

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

v11_tokenizer-0.1.0.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

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

v11_tokenizer-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (211.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file v11_tokenizer-0.1.0.tar.gz.

File metadata

  • Download URL: v11_tokenizer-0.1.0.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for v11_tokenizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9cbc93a8aee9bbdef08ff98c565cb47bb2d1b20263428241ff14797f815dfa12
MD5 a2991636121164b01478cf26e45d9066
BLAKE2b-256 37711e3f4b1744a9b8c5e67aeb13fc8779ce180050b6605145b54d89e575c47f

See more details on using hashes here.

File details

Details for the file v11_tokenizer-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for v11_tokenizer-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7c26cda7447a37b24101319250b3e88f776346a122e7d3727655f2112cc450bf
MD5 b0e9448bab22f4c0b05f9bed75be661e
BLAKE2b-256 4984c60ba524b125089721a637216c5dfe2a0c55b916b0281c35808500db6f76

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