High-performance Byte Pair Encoding (BPE) tokenizer written in modern C++ with Python bindings.
Project description
| Feature | Description |
|---|---|
| High Performance | Optimized core written in C++17 for maximum throughput and minimal latency. |
| Multi-threading | Built-in support for concurrent processing to handle massive text corpora rapidly. |
| Memory Efficient | Strict resource management ensures low memory overhead during training and encoding. |
| Python Integration | Native Python bindings utilizing pybind11 for seamless ML framework integration. |
| Modular Architecture | Clean separation of concerns allows for easy extension and customization of tokenization rules. |
| AI/LLM Ready | Designed specifically for the stringent requirements of modern transformer architectures. |
Why py_tokenizer?
Tokenization is the critical first step in natural language processing. While many tokenizers exist, py_tokenizer differentiates itself by focusing on raw compute efficiency without sacrificing usability. It is developed natively on mobile/embedded environments (Android via Termux), which enforces strict memory constraints and results in a highly optimized, lean codebase. Whether you are deploying on resource-constrained devices or scaling up on Linux servers, py_tokenizer delivers consistent, robust performance.
Project Architecture
The project follows a standard wrapper architecture:
- Core Library (C++): Handles all intensive computations, string manipulations, frequency counting, and merging operations.
- Binding Layer (pybind11): Exposes the C++ classes and methods to Python, handling memory safety and type conversions natively.
- Python API: Provides a Pythonic interface, allowing users to import the tokenizer just like any native Python module.
Installation & Building from Source
Currently, py_tokenizer supports Linux and Android (via Termux). Windows and macOS support are planned for future releases.
Prerequisites
- CMake (Version 3.10 or higher)
- C++ Compiler (GCC or Clang supporting C++17)
- Python 3.x
- pybind11 headers
Build Instructions
- Clone the repository:
git clone [https://github.com/ANSH-ins/py_tokenizer.git](https://github.com/ANSH-ins/py_tokenizer.git)
cd py_tokenizer
- Create a build directory and compile:
mkdir build
cd build
cmake ..
make -j$(nproc)
- Install the Python module:
python setup.py install
Usage Examples
Python Integration
import py_tokenizer
# Initialize the BPE Tokenizer
tokenizer = py_tokenizer.BPETokenizer()
# Train the tokenizer on your dataset
tokenizer.train(
file_path="dataset.txt",
vocab_size=10000,
special_tokens=["<PAD>", "<UNK>", "<BOS>", "<EOS>"]
)
# Encode text to tokens
text = "Artificial intelligence relies on efficient tokenization."
encoded_ids = tokenizer.encode(text)
print("Encoded IDs:", encoded_ids)
# Decode tokens back to text
decoded_text = tokenizer.decode(encoded_ids)
print("Decoded Text:", decoded_text)
C++ Integration
#include "py_tokenizer/bpe_tokenizer.hpp"
#include <iostream>
#include <vector>
#include <string>
int main() {
// Initialize
py_tokenizer::BPETokenizer tokenizer;
// Train
std::vector<std::string> special_tokens = {"<PAD>", "<UNK>", "<BOS>", "<EOS>"};
tokenizer.train("dataset.txt", 10000, special_tokens);
// Encode
std::string text = "Artificial intelligence relies on efficient tokenization.";
std::vector<int> encoded_ids = tokenizer.encode(text);
// Decode
std::string decoded_text = tokenizer.decode(encoded_ids);
std::cout << "Decoded: " << decoded_text << std::endl;
return 0;
}
Constructor Arguments
When initializing or training the BPETokenizer, you can configure its behavior using the following parameters:
| Argument | Type | Default | Description |
|---|---|---|---|
| vocab_size | int | Required | The target size of the vocabulary to be generated. |
| file_path | string | Required | Path to the text corpus for training the BPE model. |
| special_tokens | list / vector | [] | List of special structural tokens (e.g., ). |
| num_threads | int | 1 | Number of threads to utilize during the training phase. |
| casing | enum | PRESERVE | Rules for handling text capitalization (LOWERCASE, PRESERVE). |
View Project Folder Structure (Click to Expand)
```text py_tokenizer/ ├── CMakeLists.txt # Build configuration ├── README.md # Project documentation ├── setup.py # Python installation script ├── include/ # C++ Header files │ └── py_tokenizer/ │ ├── bpe_tokenizer.hpp │ └── utils.hpp ├── src/ # C++ Source files │ ├── bpe_tokenizer.cpp │ └── utils.cpp ├── bindings/ # pybind11 wrapper code │ └── bindings.cpp ├── tests/ # Unit tests (C++ and Python) │ ├── test_core.cpp │ └── test_tokenizer.py └── examples/ # Usage examples ├── example.cpp └── example.py ```Roadmap & Future Plans (Click to Expand)
* **Version 0.03**: Introduce streaming text tokenization for continuous data feeds. * **Platform Expansion**: Official build support and CI/CD pipelines for Windows and macOS. * **Advanced Normalization**: Implement customizable pre-tokenization steps (Unicode normalization, regex splitting). * **GPU Acceleration**: Research feasibility of offloading frequency counts to CUDA/OpenCL. * **Hugging Face Hub**: Native export formats to integrate directly with the transformers library.| Detail | Information |
|---|---|
| Developer Name | Ansh Raj |
| Location | India |
| Education | Class 9th |
| Core Competency | C++ |
| Languages | C++, Python, JavaScript, HTML, CSS, Kotlin, Go |
| Environments | Termux, Cxxdroid, Pydroid, Acode |
Contact & Links
- Email: anshraj0000000001@gmail.com
- GitHub: ANSH-ins
Contributing
Contributions to py_tokenizer are highly encouraged. Please follow standard open-source workflows: fork the repository, create a feature branch, commit your changes with clear messages, and open a Pull Request. Ensure that all tests pass and that your code adheres to modern C++ conventions.
Acknowledgements
Special thanks to the open-source community, the maintainers of pybind11, and the developers behind Android coding environments like Termux and Cxxdroid that made the development of this project possible on mobile devices.
License
This project is licensed under the MIT License. See the LICENSE file in the repository for full details. You are free to use, modify, and distribute this software in both open-source and commercial projects.
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
File details
Details for the file py_tokenizer_ansh-0.0.3.tar.gz.
File metadata
- Download URL: py_tokenizer_ansh-0.0.3.tar.gz
- Upload date:
- Size: 227.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a06f102ee8d50fb084272208993199f4d31f41029c5c874b0152dd0a2d854b97
|
|
| MD5 |
e1b77396a8ef3b812a58488562ac285e
|
|
| BLAKE2b-256 |
41c407cc7dfa607384e30abd007dd6729b44deda77e23b96f12113cc0bfa911d
|