A comprehensive framework for evaluating Large Language Model (LLM) tokenizers across multiple languages and domains, with a focus on efficiency, fairness, and vocabulary utilization.
This repository provides a systematic approach to benchmarking tokenizers used in modern LLMs. It evaluates tokenizers across multiple dimensions:
- Efficiency: Compression ratio and fertility metrics
- Fairness: Gini coefficient for multilingual equity
- Vocabulary Utilization: Global and per-language vocabulary usage
- Language Coverage: Support for Nordic languages (Icelandic, Faroese, Swedish, Danish, Norwegian), Germanic languages (German, Dutch, English), and code
The framework follows evaluation methodologies from recent research, including approaches used by Apertus and other multilingual tokenizer studies.
- 📊 Six core metrics: Compression ratio, fertility, Gini coefficient, vocabulary utilization (global & per-language), and token counts
- 🌍 Multilingual support: Evaluate tokenizers across 8+ languages with special focus on under-represented Nordic languages
- 💻 Code tokenization: Includes programming language evaluation
- 📈 Automated visualization: Generate publication-ready plots for all metrics
- ⚙️ Flexible configuration: Easy YAML-based setup for tokenizers, data sources, and evaluation parameters
- 🔄 Incremental evaluation: Skip already-evaluated tokenizers or re-run specific metrics
- 📝 Detailed reporting: Auto-generated comprehensive evaluation reports
- Python 3.8+
- Hugging Face account (for accessing gated models like Llama)
- Clone the repository:
git clone <repository-url>
cd wp4-tokenizer- Install dependencies:
pip install -r requirements.txt- (Optional) Set up Hugging Face authentication for gated models:
echo "your_hf_token_here" > hf_token.txtPre-download all tokenizers specified in the configuration:
python download.pyThis caches tokenizers locally in ./hf_cache/ for faster evaluation.
The framework expects:
- Monolingual data: Parquet files in
data/monolingual/{language}/directories - Parallel data: FLORES+ dev sets in
openlanguagedata/flores_plus/
Use the provided scripts to fetch data:
python fetch_data.py # Download monolingual data from HuggingFace datasets
python fetch_flores.py # Download FLORES+ parallel dataEvaluate all tokenizers across all metrics:
python evaluate.pyOptions:
--metrics [metric1 metric2 ...]: Run specific metrics only (choices:fertility,compression,vocab_utilization,vocab_utilization_per_lang,gini)--skip-existing: Skip tokenizers already evaluated (default: True)--no-skip-existing: Re-evaluate all tokenizers
Examples:
# Evaluate only fairness metric
python evaluate.py --metrics gini
# Re-evaluate all tokenizers and metrics
python evaluate.py --no-skip-existing
# Evaluate specific metrics
python evaluate.py --metrics compression fertilityCreate plots for all metrics:
python visualize.pyPlots are saved to eval_results/plots/:
compression_ratio.png- Efficiency per language (higher is better)fertility.png- Tokens per word per language (lower is better)gini_coefficient.png- Fairness across languages (lower is better)vocab_utilization.png- Global vocabulary usage (higher is better)vocab_utilization_per_language.png- Language-specific vocabulary usagetotal_token_count.png- Absolute token counts per languagedata_size_mb.png- Input data sizes
Edit config/config.yaml to customize the evaluation:
# Cache and output directories
cache_dir: "./hf_cache"
output_dir: "./eval_results"
# Tokenizers to evaluate
tokenizers:
- "mistralai/Mistral-Nemo-Base-2407"
- "meta-llama/Meta-Llama-3.1-8B"
- "Qwen/Qwen2.5-7B"
- "google/gemma-2-9b"
- "TrustLLMeu/baseline-7-8b_2-3t-tokens_llama"
# Add custom tokenizers here
# Data paths
monolingual_data:
English: "data/monolingual/english/*.parquet"
Icelandic: "data/monolingual/icelandic/*.parquet"
# ... more languages
parallel_data:
English: "openlanguagedata/flores_plus/eng_Latn.dev"
# ... more languages
# Evaluation parameters
sample_size_mb: 10 # MB of data per language for efficiency tests
text_column_name: "text" # Column name in parquet filesFormula: Bytes / Tokens
Measures how efficiently a tokenizer compresses text. Higher values indicate better compression.
Formula: Tokens / Words
Average number of subword tokens per word. Lower values indicate more efficient word-level encoding.
Formula: Gini = (1/n) × (n + 1 - (2 × Σ[(n + 1 - i) × cᵢ] / Σcᵢ))
Measures inequality in tokenization costs across languages using parallel data. Lower values indicate fairer multilingual support (0 = perfect equality, 1 = maximum inequality).
Formula: Unique Tokens Used / Total Vocabulary Size
Percentage of the vocabulary actually used across all test data. Higher values indicate more efficient vocabulary design.
Formula: Unique Tokens Used in Language L / Total Vocabulary Size
Language-specific vocabulary usage, revealing how different languages utilize the tokenizer's vocabulary.
Formula: Σ length(tokenize(textᵢ))
Absolute number of tokens needed to encode the test corpus. Lower values indicate more efficient encoding.
For detailed metric explanations and interpretation, see EVALUATION_REPORT.md.
wp4-tokenizer/
├── config/
│ └── config.yaml # Main configuration file
├── data/
│ └── monolingual/ # Monolingual evaluation data
│ ├── english/
│ ├── icelandic/
│ └── ...
├── openlanguagedata/
│ └── flores_plus/ # FLORES+ parallel data
├── eval_results/
│ ├── results.json # Main evaluation results
│ ├── granular_results.json # Detailed token counts and data sizes
│ └── plots/ # Generated visualizations
├── hf_cache/ # Cached tokenizer files
├── download.py # Download and cache tokenizers
├── evaluate.py # Main evaluation script
├── visualize.py # Generate plots from results
├── fetch_data.py # Fetch monolingual data
├── fetch_flores.py # Fetch FLORES+ parallel data
├── requirements.txt # Python dependencies
├── EVALUATION_REPORT.md # Detailed results and analysis
└── README.md # This file
Main results file containing all metrics for each tokenizer:
{
"mistralai/Mistral-Nemo-Base-2407": {
"fertility": {"English": 1.33, "Icelandic": 2.48, ...},
"compression": {"English": 4.65, "Icelandic": 2.74, ...},
"vocab_utilization": 0.585,
"vocab_utilization_per_lang": {"English": 0.346, ...},
"gini": 0.097
},
...
}Detailed statistics including:
- Data sizes (MB) per language
- Total token counts per language per tokenizer
Publication-ready visualizations for all metrics.
- Tokenizer Development: Benchmark your custom tokenizer against established models
- Model Selection: Choose the most appropriate tokenizer for your multilingual application
- Fairness Analysis: Evaluate multilingual equity in tokenizer design
- Research: Support academic research on tokenizer efficiency and fairness
The framework currently evaluates:
- Mistral-Nemo-Base-2407: Mistral AI's 2024 release
- Meta-Llama-3.1-8B: Meta's Llama 3.1 series
- Qwen2.5-7B: Alibaba's Qwen 2.5 model
- gemma-2-9b: Google's Gemma 2 series
- baseline-7-8b_2-3t-tokens_llama: TrustLLMeu custom tokenizer with enhanced Nordic language support
10MB per language collected from:
- FineWeb-Edu
- FineWeb2
- HPLT (High-Performance Language Technologies)
- Custom Faroese collections
FLORES+ development sets for fairness evaluation (Gini coefficient).
If you use this evaluation framework in your research, please cite:
@software{tokenizer_eval_framework,
title={Tokenizer Evaluation Framework},
author={TrustLLM WP4},
year={2024},
url={https://github.com/your-org/wp4-tokenizer}
}Contributions are welcome! Areas for improvement:
- Additional metrics (e.g., character coverage, byte-pair encoding efficiency)
- More languages and language families
- Integration with more tokenizer types (SentencePiece, custom implementations)
- Performance optimizations for large-scale evaluations
[Add your license information here]
- Evaluation methodology inspired by Apertus and recent multilingual tokenizer research
- FLORES+ dataset for parallel evaluation data
- FineWeb, HPLT, and other data providers
For questions or issues, please open a GitHub issue or contact [your contact information].
Note: This framework is part of the TrustLLM project Work Package 4, focusing on improving multilingual language model support for Nordic and European languages.