File size: 5,489 Bytes
ea80f98 80ab055 ea80f98 80ab055 ea80f98 a29e3d3 ea80f98 d1d3fe6 ea80f98 d1d3fe6 ea80f98 f4188ba 42b7d88 f4188ba 4918135 f4188ba ea80f98 ecd914d ea80f98 d1d3fe6 ea80f98 ecd914d ea80f98 d1d3fe6 ea80f98 d1d3fe6 ea80f98 d1d3fe6 87c35fe d1d3fe6 ea80f98 d1d3fe6 ea80f98 d1d3fe6 ea80f98 698b93b d1d3fe6 ea80f98 d1d3fe6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | ---
language:
- ar
- en
license: apache-2.0
library_name: tokenizers
tags:
- tokenizer
- arabic
- arabic-nlp
---
# SamAI-Tokenizer
[**🚀 Try the interactive tokenizer demo**](https://huggingface.co/spaces/Samai-sa/SamAI-Tokenizer-Demo)
SamAI-Tokenizer is an Arabic-focused byte-level BPE tokenizer for training new
Arabic-heavy language models. It has a vocabulary of 65,536 tokens, applies
Unicode NFC normalization, supports lossless decoding, and retains English
coverage for multilingual training.
## Key specifications
| Property | Value |
| --- | --- |
| Algorithm | Byte-level BPE |
| Vocabulary size | 65,536 |
| Normalization | Unicode NFC |
| Pre-tokenizer | ByteLevel |
| Unknown-token fallback | Byte-level coverage |
| Named special tokens | `<pad>`, `<bos>`, `<eos>`, `<unk>`, `<user>`, `<assistant>`, `<system>` |
| Reserved token IDs | 7–255 |
| Primary language | Arabic |
| Secondary language | English |
## Arabic tokenizer benchmark
SamAI-Tokenizer was evaluated on four complementary Arabic benchmarks. Lower
tokens per word indicates more efficient tokenization.

Across the four-benchmark macro average, SamAI-Tokenizer achieved **1.6207 tokens** per word, the **second-lowest** result among the 15 tokenizers successfully evaluated.
| Rank | Tokenizer | Mean tokens/word | Extra tokens versus SamAI |
| ---: | --- | ---: | ---: |
| 1 | ALLaM-7B | 1.5021 | -4.86% |
| **2** | **SamAI-Tokenizer** | **1.6207** | **baseline** |
| 3 | Falcon-H1-7B | 1.8880 | +19.30% |
| 4 | Mistral-Small-3.1 | 1.8929 | +20.71% |
| 5 | Qwen3.8-Flash-Next | 1.9039 | +21.94% |
| 6 | Qwen3.5-4B | 1.9039 | +21.94% |
| 7 | tiktoken-o200k | 1.9919 | +26.37% |
| 8 | GPT-OSS-20B | 1.9919 | +26.37% |
| 9 | Fanar-1 | 2.1570 | +36.81% |
| 10 | DeepSeek-V3 | 2.2774 | +46.25% |
| 11 | Qwen2.5-7B | 2.3740 | +50.25% |
| 12 | Qwen3-8B | 2.3740 | +50.25% |
| 13 | GLM-5.3-Flash | 2.5225 | +60.21% |
| 14 | Kimi-K2.6 | 2.8693 | +84.13% |
| 15 | tiktoken-cl100k | 4.0800 | +164.63% |
## Training data
The tokenizer vocabulary was trained on a diverse mixture of Arabic and English text, including Modern Standard Arabic, dialectal Arabic, educational content, and mixed Arabic-English text.
## Installation
```bash
pip install -U tokenizers transformers huggingface_hub
```
## Usage
### Transformers
```python
from transformers import PreTrainedTokenizerFast
tokenizer = PreTrainedTokenizerFast.from_pretrained(
"Samai-sa/SamAI-Tokenizer"
)
text = "المملكة العربية السعودية"
ids = tokenizer.encode(text, add_special_tokens=False)
print("Token count:", len(ids))
print("Token IDs:", ids)
print("Tokens:", tokenizer.convert_ids_to_tokens(ids))
print("Decoded:", tokenizer.decode(ids))
```
### Tokenizers
```python
from huggingface_hub import hf_hub_download
from tokenizers import Tokenizer
path = hf_hub_download(
repo_id="Samai-sa/SamAI-Tokenizer",
filename="tokenizer.json",
)
tokenizer = Tokenizer.from_file(path)
encoding = tokenizer.encode(
"السلام عليكم ورحمة الله وبركاته",
add_special_tokens=False,
)
print("Token count:", len(encoding.ids))
print("Token IDs:", encoding.ids)
print("Tokens:", encoding.tokens)
print("Decoded:", tokenizer.decode(encoding.ids))
```
## Interactive demo
The [SamAI Tokenizer Playground](https://huggingface.co/spaces/Samai-sa/SamAI-Tokenizer-Demo)
lets users enter Arabic or English text and inspect:
- token count and tokens per word;
- token IDs and raw token pieces;
- original text spans and offsets;
- decoded text for round-trip verification.
## Validation
The released validation run reported zero round-trip failures and zero unknown
tokens across samples from schoolbooks, AraMix Arabic, FineWeb2 Arabic,
and FineWeb English. Additional stress tests cover Modern Standard Arabic,
dialects, Arabizi, English, code, mathematics, and Unicode edge cases.
See [`reports/validation.json`](reports/validation.json) and
[`reports/stress.json`](reports/stress.json).
## Benchmark methodology
- All available examples were used, followed by exact-text deduplication within
each benchmark.
- Token counts exclude chat templates and added special tokens.
- Tokens per word is total tokens divided by whitespace-separated words.
- The primary macro ranking gives each benchmark 25% weight.
- The pooled report sums all tokens and words before calculating the ratio.
- Tokenizers producing identical results are tokenizer-family duplicates, not
independent confirmations.
To reproduce the benchmark:
```bash
pip install -r benchmark/requirements.txt
python benchmark/benchmark.py
```
## Limitations
- Tokenization efficiency does not measure language-model quality, reasoning,
factuality, safety, or downstream accuracy.
- Fully vocalized Arabic is the clearest weakness. One heavily diacritized
ArabicMMLU category accounts for most of the gap.
- Whitespace-separated words are a transparent denominator, not a linguistic
segmentation of Arabic morphology.
- Replacing the tokenizer of an already pretrained model requires resizing and
retraining its embedding and output layers. This is not a drop-in vocabulary
replacement.
## License and data terms
The tokenizer files and repository code are released under Apache-2.0. Source
and benchmark datasets remain subject to their own licenses and terms. No
source-corpus or benchmark documents are redistributed here.
|