Registry / ai-ml / pytorch-tokenizers

pytorch-tokenizers

JSON →
library1.3.0pypypiunverified

PyTorch-Tokenizers is a Python package providing efficient C++ implementations for common tokenizers like SentencePiece and TikToken, along with Python bindings. It is primarily designed to serve as a dependency for other PyTorch projects, such as ExecuTorch and torchchat, to facilitate building high-performance LLM runners. The library offers significant efficiency gains for AI workloads, multilingual support, and high decode accuracy. It is actively maintained, with version 1.2.0 aligning its releases with major PyTorch and ExecuTorch updates.

pip install pytorch-tokenizers
INSTALL
IMPORT
SIG · PYTORCH-TOKENIZERS
P
pytorch-tokenizers
ai-mlpythonv1.3.0
Install
6.4s avg
Import
Disk
86MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 7.2s
py 3.11
✕ build_error
✓ 6.7s
py 3.12
✕ build_error
✓ 5.8s
py 3.13
✕ build_error
✓ 5.83s
py 3.9
✕ build_error
✕ build_error
86MB installed
● package 86MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SentencePieceTokenizer
from pytorch_tokenizers import SentencePieceTokenizer
from torchtext.transforms import SentencePieceTokenizer
This import is for `torchtext`, a separate library with its own SentencePiece tokenizer implementation. `pytorch-tokenizers` provides its own distinct implementation.
SentencePieceTokenizer
from pytorch_tokenizers import SentencePieceTokenizer
from transformers import SentencePieceTokenizer
This import is for Hugging Face's `transformers` library, which is a different tokenizer ecosystem. `pytorch-tokenizers` is a distinct PyTorch-specific implementation.

This quickstart demonstrates how to initialize and use the `SentencePieceTokenizer` from `pytorch-tokenizers`. Note that `SentencePieceTokenizer` requires a pre-trained SentencePiece model file (`.model`). For a runnable example, we temporarily generate a dummy model using the `sentencepiece` library. In practical applications, you would typically load an existing model file.

import os import tempfile import sentencepiece as spm # Required for generating dummy model from pytorch_tokenizers import SentencePieceTokenizer # 1. Create a dummy SentencePiece model file for demonstration # In real-world scenarios, you would use an existing pre-trained model. model_prefix = os.path.join(tempfile.gettempdir(), 'm_test') model_file = f'{model_prefix}.model' vocab_file = f'{model_prefix}.vocab' # Ensure clean slate for temporary files if os.path.exists(model_file): os.remove(model_file) if os.path.exists(vocab_file): os.remove(vocab_file) text_data = "Hello world. This is a test sentence. SentencePiece is great!" with open(f'{model_prefix}.txt', 'w') as f: f.write(text_data) spm.SentencePieceTrainer.train( input=f'{model_prefix}.txt', model_prefix=model_prefix, vocab_size=10, model_type='bpe' ) # 2. Instantiate the SentencePieceTokenizer from the created model file tokenizer = SentencePieceTokenizer.from_file(model_file) # 3. Encode text input_text = "This is a sample text for tokenization." encoded_tokens = tokenizer.encode(input_text) print(f"Original text: {input_text}") print(f"Encoded token IDs: {encoded_tokens}") # 4. Decode tokens decoded_text = tokenizer.decode(encoded_tokens) print(f"Decoded text: {decoded_text}") # Clean up temporary files os.remove(f'{model_prefix}.txt') os.remove(model_file) os.remove(vocab_file)
Debug
Known issues
breakingPyTorch-Tokenizers maintains tight version alignment with PyTorch and ExecuTorch. Major version changes or significant updates in these upstream libraries may introduce incompatibilities or require an update to `pytorch-tokenizers` for continued functionality.
fix
Always check the release notes of `pytorch-tokenizers` and its associated PyTorch/ExecuTorch versions. Upgrade `pytorch-tokenizers` to match your PyTorch/ExecuTorch installation.
affects: <1.2.0
gotchaThe `pytorch-tokenizers` library is primarily an internal dependency for PyTorch's on-device AI efforts (like ExecuTorch). As such, it lacks extensive standalone documentation and examples compared to general-purpose tokenization libraries (e.g., Hugging Face's `tokenizers` or `torchtext`). Users expecting a feature-rich, standalone API similar to these other libraries might find the direct usage less intuitive.
fix
Consult the `pytorch-tokenizers` GitHub repository's `README.md` and related ExecuTorch documentation for usage patterns. Do not assume feature parity or API identicality with other tokenizer libraries.
affects: All versions
gotchaTokenizers provided by `pytorch-tokenizers`, such as `SentencePieceTokenizer`, require pre-trained model files (e.g., `.model` for SentencePiece) to be instantiated. These model files are not distributed with the `pytorch-tokenizers` Python package itself.
fix
Ensure you have access to the necessary model files. These are typically generated by training SentencePiece on a corpus or obtained alongside pre-trained language models from sources like Hugging Face. The quickstart demonstrates generating a dummy file for local testing.
affects: All versions
Upgrade
Version history
1.3.0latest on PyPI · released May 26, 2026
Audit
Dependencies
pythonrequiredRequired Python version.
Agent activity
23 hits · last 30 days
node
20
OpenAI (training)
1
Resources
pytorch-tokenizers — pip install pytorch-tokenizers · libregistry