MLTokenizer is a comprehensive tokenization system for machine learning models, with a focus on performance, cross-model compatibility, and extensibility.
pip install mltokenizer
# Clone the repository
git clone https://github.com/mltokenizer/mltokenizer.git
cd mltokenizer
# Install with pip
pip install -e .
# Build Rust extensions (optional, requires Rust toolchain)
pip install maturin
maturin develop --release
# Build and run with Docker Compose
docker-compose up -d
# Or build and run individual services
docker build -t mltokenizer .
docker run -p 8000:8000 mltokenizer
from mltokenizer.algorithms.bpe import BPETokenizer
from mltokenizer.normalization.normalizers import SequenceNormalizer
# Create a BPE tokenizer
tokenizer = BPETokenizer(
vocab_size=10000,
normalizer=SequenceNormalizer.default()
)
# Train the tokenizer
with open("training_data.txt", "r") as f:
texts = [line.strip() for line in f]
tokenizer.train(texts=texts)
# Tokenize a text
result = tokenizer.encode("Hello, world!", return_tokens=True)
print(result.tokens) # List of tokens
print(result.input_ids) # List of token IDs
# Decode tokens back to text
decoded = tokenizer.decode(result.input_ids)
print(decoded) # "Hello, world!"
# Save the tokenizer
tokenizer.save("./my_tokenizer")
# Load the tokenizer
loaded_tokenizer = BPETokenizer.load("./my_tokenizer")
from mltokenizer.models.bert import BertTokenizer
from mltokenizer.models.gpt import GPTTokenizer
# Load BERT tokenizer
bert_tokenizer = BertTokenizer.from_huggingface("bert-base-uncased")
# Load GPT tokenizer
gpt_tokenizer = GPTTokenizer.from_huggingface("gpt2")
# Tokenize with both and compare
bert_result = bert_tokenizer.encode("Hello, world!", return_tokens=True)
gpt_result = gpt_tokenizer.encode("Hello, world!", return_tokens=True)
print(f"BERT tokens: {bert_result.tokens}")
print(f"GPT tokens: {gpt_result.tokens}")
# Train a tokenizer
tokenize train-tokenizer \
--tokenizer-type bpe \
--input-file data/train.txt \
--output-dir ./my_tokenizer \
--vocab-size 20000
# Load a pretrained tokenizer
tokenize load-pretrained --model-name bert-base-uncased
# Tokenize text
tokenize tokenize "Hello, world!" --tokenizer-id bert_base_uncased
# Analyze token distribution
tokenize analyze-distribution \
--input-file data/corpus.txt \
--tokenizer-id bert_base_uncased \
--output-file analysis.json
# Start the tokenization server
tokenize serve --port 8000
┌─────────────────────────────────────────────────────────────────────┐
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌───────────────────────┐ │
│ │ Tokenization│ │ Encoding │ │ Normalization │ │
│ │ Core │───▶│ Management │───▶│ Engine │ │
│ └─────────────┘ └─────────────┘ └───────────────────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌───────────────────────┐ │
│ │ Algorithm │ │ Vocabulary │ │ Pre/Post-Process │ │
│ │ Registry │ │ Management │ │ Pipeline │ │
│ └─────────────┘ └─────────────┘ └───────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────┐ ┌───────────────────────────┐ │
│ │ Performance Monitoring │◀───│ Orchestration Hub │ │
│ └─────────────────────────────┘ └───────────────────────────┘ │
│ │ ▲ │
│ ▼ │ │
│ ┌─────────────────────────────┐ ┌───────────────────────────┐ │
│ │ Model Adapters │ │ Tokenization Laboratory │ │
│ └─────────────────────────────┘ └───────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
The MLTokenizer system is built on several key components:
The Tokenization Laboratory provides an interactive interface for experimenting with different tokenizers, visualizing token distributions, and analyzing tokenization efficiency.
# Start the tokenization lab
cd tokenization_lab
npm run tauri dev
The API documentation is available at /docs when the server is running:
tokenize serve
# Then navigate to http://localhost:8000/docs
For detailed API documentation, see our API Reference.
BaseTokenizertrain(), encode(), decode(), save(), and load()models directoryFor performance-critical applications, the system offers several optimization options:
The system includes tools for analyzing and enhancing cross-model compatibility:
from mltokenizer.utils.compatibility import compare_tokenization, transfer_tokenization
# Compare tokenization between models
comparison = compare_tokenization(
text="This is a test",
tokenizer_a=bert_tokenizer,
tokenizer_b=gpt_tokenizer
)
# Transfer tokenization from one model to another
transferred = transfer_tokenization(
tokens=bert_result.tokens,
source_tokenizer=bert_tokenizer,
target_tokenizer=gpt_tokenizer
)
Comprehensive tests are included for all components:
# Run all tests
pytest
# Run specific test categories
pytest tests/python/unit
pytest tests/python/integration
pytest tests/python/performance
pytest tests/python/multilingual
# Run with coverage report
pytest --cov=mltokenizer
MIT
If you use MLTokenizer in your research, please cite:
@software{mltokenizer2023,
author = {MLTokenizer Team},
title = {MLTokenizer: A Comprehensive Tokenization System},
year = {2023},
url = {https://github.com/mltokenizer/mltokenizer}
}
Contributions are welcome! Please check the CONTRIBUTING.md file for guidelines.
A comprehensive ML Tokenization System migrated from the older OpenTokenizer structure. Includes a Python library, API, interactive lab (Tauri), CLI, and robust testing.
# Clone the repository
git clone https://github.com/llamasearchai/OpenTokenizer.git
cd OpenTokenizer
# Install dependencies
pip install -e .
# Build Rust components
cd mltokenizer-rs
cargo build --release
from mltokenizer import BPETokenizer
tokenizer = BPETokenizer()
tokenizer.train(["sample text"])
tokens = tokenizer.encode("sample text")
print(tokens)
See CONTRIBUTING.md for guidelines.
Python
94.5%
Rust
5.0%
MLTokenizer is a comprehensive tokenization system for machine learning models, with a focus on performance, cross-model compatibility, and extensibility.
pip install mltokenizer
# Clone the repository
git clone https://github.com/mltokenizer/mltokenizer.git
cd mltokenizer
# Install with pip
pip install -e .
# Build Rust extensions (optional, requires Rust toolchain)
pip install maturin
maturin develop --release
# Build and run with Docker Compose
docker-compose up -d
# Or build and run individual services
docker build -t mltokenizer .
docker run -p 8000:8000 mltokenizer
from mltokenizer.algorithms.bpe import BPETokenizer
from mltokenizer.normalization.normalizers import SequenceNormalizer
# Create a BPE tokenizer
tokenizer = BPETokenizer(
vocab_size=10000,
normalizer=SequenceNormalizer.default()
)
# Train the tokenizer
with open("training_data.txt", "r") as f:
texts = [line.strip() for line in f]
tokenizer.train(texts=texts)
# Tokenize a text
result = tokenizer.encode("Hello, world!", return_tokens=True)
print(result.tokens) # List of tokens
print(result.input_ids) # List of token IDs
# Decode tokens back to text
decoded = tokenizer.decode(result.input_ids)
print(decoded) # "Hello, world!"
# Save the tokenizer
tokenizer.save("./my_tokenizer")
# Load the tokenizer
loaded_tokenizer = BPETokenizer.load("./my_tokenizer")
from mltokenizer.models.bert import BertTokenizer
from mltokenizer.models.gpt import GPTTokenizer
# Load BERT tokenizer
bert_tokenizer = BertTokenizer.from_huggingface("bert-base-uncased")
# Load GPT tokenizer
gpt_tokenizer = GPTTokenizer.from_huggingface("gpt2")
# Tokenize with both and compare
bert_result = bert_tokenizer.encode("Hello, world!", return_tokens=True)
gpt_result = gpt_tokenizer.encode("Hello, world!", return_tokens=True)
print(f"BERT tokens: {bert_result.tokens}")
print(f"GPT tokens: {gpt_result.tokens}")
# Train a tokenizer
tokenize train-tokenizer \
--tokenizer-type bpe \
--input-file data/train.txt \
--output-dir ./my_tokenizer \
--vocab-size 20000
# Load a pretrained tokenizer
tokenize load-pretrained --model-name bert-base-uncased
# Tokenize text
tokenize tokenize "Hello, world!" --tokenizer-id bert_base_uncased
# Analyze token distribution
tokenize analyze-distribution \
--input-file data/corpus.txt \
--tokenizer-id bert_base_uncased \
--output-file analysis.json
# Start the tokenization server
tokenize serve --port 8000
┌─────────────────────────────────────────────────────────────────────┐
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌───────────────────────┐ │
│ │ Tokenization│ │ Encoding │ │ Normalization │ │
│ │ Core │───▶│ Management │───▶│ Engine │ │
│ └─────────────┘ └─────────────┘ └───────────────────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌───────────────────────┐ │
│ │ Algorithm │ │ Vocabulary │ │ Pre/Post-Process │ │
│ │ Registry │ │ Management │ │ Pipeline │ │
│ └─────────────┘ └─────────────┘ └───────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────┐ ┌───────────────────────────┐ │
│ │ Performance Monitoring │◀───│ Orchestration Hub │ │
│ └─────────────────────────────┘ └───────────────────────────┘ │
│ │ ▲ │
│ ▼ │ │
│ ┌─────────────────────────────┐ ┌───────────────────────────┐ │
│ │ Model Adapters │ │ Tokenization Laboratory │ │
│ └─────────────────────────────┘ └───────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
The MLTokenizer system is built on several key components:
The Tokenization Laboratory provides an interactive interface for experimenting with different tokenizers, visualizing token distributions, and analyzing tokenization efficiency.
# Start the tokenization lab
cd tokenization_lab
npm run tauri dev
The API documentation is available at /docs when the server is running:
tokenize serve
# Then navigate to http://localhost:8000/docs
For detailed API documentation, see our API Reference.
BaseTokenizertrain(), encode(), decode(), save(), and load()models directoryFor performance-critical applications, the system offers several optimization options:
The system includes tools for analyzing and enhancing cross-model compatibility:
from mltokenizer.utils.compatibility import compare_tokenization, transfer_tokenization
# Compare tokenization between models
comparison = compare_tokenization(
text="This is a test",
tokenizer_a=bert_tokenizer,
tokenizer_b=gpt_tokenizer
)
# Transfer tokenization from one model to another
transferred = transfer_tokenization(
tokens=bert_result.tokens,
source_tokenizer=bert_tokenizer,
target_tokenizer=gpt_tokenizer
)
Comprehensive tests are included for all components:
# Run all tests
pytest
# Run specific test categories
pytest tests/python/unit
pytest tests/python/integration
pytest tests/python/performance
pytest tests/python/multilingual
# Run with coverage report
pytest --cov=mltokenizer
MIT
If you use MLTokenizer in your research, please cite:
@software{mltokenizer2023,
author = {MLTokenizer Team},
title = {MLTokenizer: A Comprehensive Tokenization System},
year = {2023},
url = {https://github.com/mltokenizer/mltokenizer}
}
Contributions are welcome! Please check the CONTRIBUTING.md file for guidelines.
A comprehensive ML Tokenization System migrated from the older OpenTokenizer structure. Includes a Python library, API, interactive lab (Tauri), CLI, and robust testing.
# Clone the repository
git clone https://github.com/llamasearchai/OpenTokenizer.git
cd OpenTokenizer
# Install dependencies
pip install -e .
# Build Rust components
cd mltokenizer-rs
cargo build --release
from mltokenizer import BPETokenizer
tokenizer = BPETokenizer()
tokenizer.train(["sample text"])
tokens = tokenizer.encode("sample text")
print(tokens)
See CONTRIBUTING.md for guidelines.
Python
94.5%
Rust
5.0%