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-tokenizersVerified import paths — ran on the pinned version, not inferred.
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.
Always check the release notes of `pytorch-tokenizers` and its associated PyTorch/ExecuTorch versions. Upgrade `pytorch-tokenizers` to match your PyTorch/ExecuTorch installation.
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.
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.