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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- sw
|
| 5 |
+
- rw
|
| 6 |
+
- fr
|
| 7 |
+
- en
|
| 8 |
+
tags:
|
| 9 |
+
- tokenizer
|
| 10 |
+
- byte-level-bpe
|
| 11 |
+
- multilingual
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# OTK-BPE: Optimized BPE Tokenizers
|
| 15 |
+
|
| 16 |
+
This repository hosts the OTK-BPE family of production-grade Byte-Level BPE
|
| 17 |
+
(BBPE) tokenizers for regional and multilingual language models. It now
|
| 18 |
+
covers **Swahili**, **Kinyarwanda**, and a **merged French + Kinyarwanda +
|
| 19 |
+
English + Swahili** vocabulary β each available at **three vocab sizes: 50k,
|
| 20 |
+
100k, and 150k** β so you can pick the tradeoff between compactness and
|
| 21 |
+
coverage that fits your use case.
|
| 22 |
+
|
| 23 |
+
(Companion repo: [`olaverse/otk-bpe-50k`](https://huggingface.co/olaverse/otk-bpe-50k)
|
| 24 |
+
covers the Nigerian-languages family β Yoruba, Igbo, Hausa, Pidgin, and a
|
| 25 |
+
unified Naija tokenizer, all fixed at 50k.)
|
| 26 |
+
|
| 27 |
+
## Which one should I use?
|
| 28 |
+
|
| 29 |
+
| Your situation | Use |
|
| 30 |
+
|---|---|
|
| 31 |
+
| Only need Swahili | `sw-150k` (best fertility and entity handling in this family; drop to `sw-50k`/`sw-100k` only if embedding-table size is tightly constrained) |
|
| 32 |
+
| Only need Kinyarwanda | `kin-150k` |
|
| 33 |
+
| Need French, Kinyarwanda, English, and Swahili in one model | `merged-150k` |
|
| 34 |
+
| Storage/embedding-table size is a hard constraint | Step down to `-100k` or `-50k` in the same language β fertility degrades gradually, not a cliff (see Benchmarks) |
|
| 35 |
+
|
| 36 |
+
**150k is the recommended default across all three families** β in every
|
| 37 |
+
benchmark below, fertility and entity-handling both improved monotonically
|
| 38 |
+
from 50k β 100k β 150k, with no exceptions. There's no size in this range
|
| 39 |
+
where a smaller vocab wins outright.
|
| 40 |
+
|
| 41 |
+
## Key Features
|
| 42 |
+
|
| 43 |
+
- **Byte-Level BPE (BBPE)**: maps raw UTF-8 bytes to printable characters β
|
| 44 |
+
0.00% Out-Of-Vocabulary, zero `[UNK]` tokens, by construction.
|
| 45 |
+
- **Diacritic Preservation & Normalization**: NFC normalization inside the
|
| 46 |
+
pre-tokenization chain, so accents and combining diacritics don't get split
|
| 47 |
+
into decomposed code points.
|
| 48 |
+
- **Code-Mixed English Support**: the Swahili and Kinyarwanda tokenizers each
|
| 49 |
+
blend a English-Wikipedia component into training; the merged tokenizer
|
| 50 |
+
treats English as a full first-class language rather than a minority blend.
|
| 51 |
+
- **Emoji & Symbol Merging**: curated emoji vocab is injected during training
|
| 52 |
+
so common emoji merge into single tokens instead of fragmenting.
|
| 53 |
+
|
| 54 |
+
## Tokenizer Models in This Repository
|
| 55 |
+
|
| 56 |
+
All loaded from `olaverse/otk-bpe`, selected via the `subfolder` argument.
|
| 57 |
+
|
| 58 |
+
| Subfolder | Languages | Vocab Size |
|
| 59 |
+
|---|---|---|
|
| 60 |
+
| `sw-50k` / `sw-100k` / `sw-150k` | Swahili | 50,000 / 100,000 / 150,000 |
|
| 61 |
+
| `kin-50k` / `kin-100k` / `kin-150k` | Kinyarwanda | 50,000 / 100,000 / 150,000 |
|
| 62 |
+
| `merged-50k` / `merged-100k` / `merged-150k` | French + Kinyarwanda + English + Swahili | 50,000 / 100,000 / 150,000 |
|
| 63 |
+
|
| 64 |
+
## Performance Benchmarks
|
| 65 |
+
|
| 66 |
+
Fertility = average tokens per word (lower is better). Entity fragmentation
|
| 67 |
+
= share of capitalized/likely-proper-noun words split into more than one
|
| 68 |
+
token (lower is better) β this metric predicts downstream NER difficulty
|
| 69 |
+
before any model is even trained.
|
| 70 |
+
|
| 71 |
+
**Swahili benchmark** uses the MasakhaNEWS Swahili *test* split (42,494
|
| 72 |
+
entity-candidate words checked) β a real, curated held-out set, never seen
|
| 73 |
+
during training.
|
| 74 |
+
|
| 75 |
+
**Kinyarwanda, French, and English benchmarks** use a held-out, non-training
|
| 76 |
+
slice of the same streaming sources (FineWeb-2 / Wikipedia) rather than a
|
| 77 |
+
curated test set β **MasakhaNEWS does not include Kinyarwanda** (only the
|
| 78 |
+
related-but-distinct Rundi/Kirundi), so no equivalent curated benchmark
|
| 79 |
+
exists for that language yet. Treat these three languages' numbers as
|
| 80 |
+
directionally reliable, not as rigorously verified as the Swahili figures.
|
| 81 |
+
|
| 82 |
+
### Swahili
|
| 83 |
+
|
| 84 |
+
| Tokenizer | Vocab Size | Fertility | Entity Fragmentation |
|
| 85 |
+
|---|---|---|---|
|
| 86 |
+
| **otk-bpe sw-150k** | 150,000 | **1.210 (Best!)** | 0.341 |
|
| 87 |
+
| otk-bpe sw-100k | 100,000 | 1.233 | 0.435 |
|
| 88 |
+
| otk-bpe merged-150k | 150,000 | 1.264 | 0.519 |
|
| 89 |
+
| otk-bpe sw-50k | 50,000 | 1.285 | 0.626 |
|
| 90 |
+
| otk-bpe merged-100k | 100,000 | 1.302 | 0.672 |
|
| 91 |
+
| AfroXLMR | 250,002 | 1.597 | **0.441 (Best entity handling)** |
|
| 92 |
+
| otk-bpe merged-50k | 50,000 | 1.393 | 0.822 |
|
| 93 |
+
| GPT-4o (o200k_base) | 200,019 | 1.841 | 0.768 |
|
| 94 |
+
| mBERT | ~119,547 | 2.071 | 0.575 |
|
| 95 |
+
| GPT-4 (cl100k_base) | 100,277 | 2.462 | 0.820 |
|
| 96 |
+
|
| 97 |
+
### Kinyarwanda
|
| 98 |
+
|
| 99 |
+
| Tokenizer | Vocab Size | Fertility | Entity Fragmentation |
|
| 100 |
+
|---|---|---|---|
|
| 101 |
+
| **otk-bpe kin-150k** | 150,000 | **1.377 (Best!)** | **0.334 (Best!)** |
|
| 102 |
+
| otk-bpe kin-100k | 100,000 | 1.409 | 0.420 |
|
| 103 |
+
| otk-bpe merged-150k | 150,000 | 1.465 | 0.475 |
|
| 104 |
+
| otk-bpe kin-50k | 50,000 | 1.483 | 0.561 |
|
| 105 |
+
| otk-bpe merged-100k | 100,000 | 1.523 | 0.567 |
|
| 106 |
+
| otk-bpe merged-50k | 50,000 | 1.662 | 0.709 |
|
| 107 |
+
| AfroXLMR | 250,002 | 2.495 | 0.718 |
|
| 108 |
+
| mBERT | ~119,547 | 2.702 | 0.715 |
|
| 109 |
+
| GPT-4o (o200k_base) | 200,019 | 2.189 | 0.798 |
|
| 110 |
+
| GPT-4 (cl100k_base) | 100,277 | 2.798 | 0.854 |
|
| 111 |
+
|
| 112 |
+
Kinyarwanda is where general-purpose multilingual tokenizers are weakest
|
| 113 |
+
across the board β even the smallest dedicated tokenizer here (`kin-50k`)
|
| 114 |
+
beats every baseline on fertility, and `kin-150k` beats every baseline on
|
| 115 |
+
both metrics simultaneously.
|
| 116 |
+
|
| 117 |
+
### French
|
| 118 |
+
|
| 119 |
+
| Tokenizer | Vocab Size | Fertility | Entity Fragmentation |
|
| 120 |
+
|---|---|---|---|
|
| 121 |
+
| **otk-bpe merged-150k** | 150,000 | **1.378 (Best!)** | 0.428 |
|
| 122 |
+
| otk-bpe merged-100k | 100,000 | 1.409 | 0.502 |
|
| 123 |
+
| GPT-4o (o200k_base) | 200,019 | 1.478 | 0.479 |
|
| 124 |
+
| otk-bpe merged-50k | 50,000 | 1.486 | 0.620 |
|
| 125 |
+
| AfroXLMR | 250,002 | 1.597 | **0.434 (Best entity handling)** |
|
| 126 |
+
| mBERT | ~119,547 | 1.616 | 0.388 |
|
| 127 |
+
| GPT-4 (cl100k_base) | 100,277 | 1.720 | 0.578 |
|
| 128 |
+
|
| 129 |
+
### English
|
| 130 |
+
|
| 131 |
+
| Tokenizer | Vocab Size | Fertility | Entity Fragmentation |
|
| 132 |
+
|---|---|---|---|
|
| 133 |
+
| mBERT | ~119,547 | **1.429 (Best!)** | **0.285 (Best!)** |
|
| 134 |
+
| GPT-4o (o200k_base) | 200,019 | 1.436 | 0.452 |
|
| 135 |
+
| otk-bpe merged-150k | 150,000 | 1.443 | 0.558 |
|
| 136 |
+
| GPT-4 (cl100k_base) | 100,277 | 1.460 | 0.496 |
|
| 137 |
+
| otk-bpe merged-100k | 100,000 | 1.500 | 0.659 |
|
| 138 |
+
| AfroXLMR | 250,002 | 1.538 | 0.425 |
|
| 139 |
+
| otk-bpe merged-50k | 50,000 | 1.632 | 0.803 |
|
| 140 |
+
|
| 141 |
+
English is the merged tokenizer's weakest relative showing β expected, since
|
| 142 |
+
none of the 4 languages get a dedicated vocabulary in the merged design, and
|
| 143 |
+
mBERT/AfroXLMR both have far larger, English-rich vocabularies to draw on.
|
| 144 |
+
`merged-150k` still lands within ~1% of mBERT's fertility despite covering 3
|
| 145 |
+
additional languages in the same vocabulary.
|
| 146 |
+
|
| 147 |
+
### Reading the entity fragmentation numbers honestly
|
| 148 |
+
|
| 149 |
+
Across every language and every vocab size, **AfroXLMR wins or ties on entity
|
| 150 |
+
fragmentation more often than it wins on fertility** β this is a real,
|
| 151 |
+
consistent pattern, not noise. General-purpose tokenizers with much larger
|
| 152 |
+
vocabularies (AfroXLMR: 250k) hold an edge on rare and foreign proper nouns
|
| 153 |
+
specifically, since that's a long-tail, open-ended category where raw vocab
|
| 154 |
+
size matters more than language-dedication. Increasing OTK-BPE's own vocab
|
| 155 |
+
size from 50kβ150k substantially narrows this gap (and outright wins it for
|
| 156 |
+
Kinyarwanda) but does not fully close it for Swahili, French, or the merged
|
| 157 |
+
tokenizer at 150k. If NER-style entity accuracy is the critical downstream
|
| 158 |
+
task, this is worth weighing against the fertility advantage β OTK-BPE is
|
| 159 |
+
not a strict win on every axis, and this repository reports that plainly
|
| 160 |
+
rather than only showing the metrics where it wins.
|
| 161 |
+
|
| 162 |
+
## How to Use
|
| 163 |
+
|
| 164 |
+
### Installation
|
| 165 |
+
|
| 166 |
+
```bash
|
| 167 |
+
pip install tokenizers transformers huggingface_hub
|
| 168 |
+
```
|
| 169 |
+
|
| 170 |
+
### Method A: Standard Transformers Loading
|
| 171 |
+
|
| 172 |
+
```python
|
| 173 |
+
from transformers import AutoTokenizer
|
| 174 |
+
|
| 175 |
+
# Load the Swahili tokenizer at your chosen size
|
| 176 |
+
tokenizer = AutoTokenizer.from_pretrained("olaverse/otk-bpe", subfolder="sw-150k")
|
| 177 |
+
|
| 178 |
+
text = "Habari yako? Leo ni siku nzuri sana π"
|
| 179 |
+
inputs = tokenizer(text)
|
| 180 |
+
|
| 181 |
+
print("Tokens:", tokenizer.tokenize(text))
|
| 182 |
+
print("IDs:", inputs["input_ids"])
|
| 183 |
+
print("Decoded:", tokenizer.decode(inputs["input_ids"]))
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
```python
|
| 187 |
+
# Kinyarwanda
|
| 188 |
+
tokenizer = AutoTokenizer.from_pretrained("olaverse/otk-bpe", subfolder="kin-150k")
|
| 189 |
+
|
| 190 |
+
# Merged (French + Kinyarwanda + English + Swahili)
|
| 191 |
+
tokenizer = AutoTokenizer.from_pretrained("olaverse/otk-bpe", subfolder="merged-150k")
|
| 192 |
+
```
|
| 193 |
+
|
| 194 |
+
### Method B: Lightweight Raw BPE Loading
|
| 195 |
+
|
| 196 |
+
```python
|
| 197 |
+
from olaverse import Tokenizer
|
| 198 |
+
|
| 199 |
+
# Supports: "sw-50k", "sw-100k", "sw-150k", "kin-50k", "kin-100k", "kin-150k",
|
| 200 |
+
# "merged-50k", "merged-100k", "merged-150k"
|
| 201 |
+
tokenizer = Tokenizer("sw-150k")
|
| 202 |
+
|
| 203 |
+
ids = tokenizer.encode("Habari yako? Leo ni siku nzuri sana π")
|
| 204 |
+
print("IDs:", ids)
|
| 205 |
+
print("Decoded:", tokenizer.decode(ids))
|
| 206 |
+
```
|
| 207 |
+
|
| 208 |
+
## Datasets Used for Training
|
| 209 |
+
|
| 210 |
+
| Source | License | Role |
|
| 211 |
+
|---|---|---|
|
| 212 |
+
| FineWeb-2 (`swh_Latn`, `kin_Latn`, `fra_Latn`) | ODC-By | Web text |
|
| 213 |
+
| Wikipedia (sw, rw, fr, en) | CC BY-SA 4.0 | Encyclopedic corpora |
|
| 214 |
+
| MasakhaNEWS (Swahili train split) | CC BY 4.0 | News text, dense with named entities |
|
| 215 |
+
| [`olaverse/qg-passages-multi`](https://huggingface.co/datasets/olaverse/qg-passages-multi) | Apache-2.0 | Domain coverage for Swahili, French, and English |
|
| 216 |
+
| Wikipedia (English) | CC BY-SA 4.0 | Code-mixed blending (sw/kin) or full component (merged) |
|
| 217 |
+
| Curated Emoji Registry | β | Common social-media emoji |
|
| 218 |
+
|
| 219 |
+
MasakhaNEWS Swahili *test* split was excluded from all training corpora and
|
| 220 |
+
reserved exclusively for the fertility/entity benchmarks above.
|
| 221 |
+
|
| 222 |
+
## Links
|
| 223 |
+
|
| 224 |
+
- [Companion repo: Nigerian languages (otk-bpe-50k)](https://huggingface.co/olaverse/otk-bpe-50k)
|
| 225 |
+
- Olaverse Library Docs
|
| 226 |
+
|
| 227 |
+
## License
|
| 228 |
+
|
| 229 |
+
Apache-2.0. Training data licenses noted per source above; please retain
|
| 230 |
+
attribution to upstream datasets.
|