File size: 16,096 Bytes
1bf933f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "datasets>=4.4.2",
#   "tokenizers>=0.22.2",
# ]
# ///
"""Rebuild the tokenizer released with Bekko Static Embedding Multilingual v1.

What this script does
---------------------
This standalone entry point downloads deterministic text prefixes from the
pinned public ``hotchpotch/bekko-embedding-v1-unsupervised`` revision. It first
trains a 30,000-token English WordPiece core, preserves those tokens and IDs,
then extends the vocabulary with multilingual candidates to exactly 200,000
tokens. It validates the result and writes ``tokenizer.json`` plus a provenance
manifest. Tokenizer training uses non-bitext data only.

How to run it
-------------
The script contains PEP 723 dependency metadata, so the shortest invocation is::

    uv run train_tokenizer.py \
        --corpus-dir output/tokenizer-corpus \
        --output-dir output/tokenizer

Use persistent paths because downloading and preparing the corpus takes time.
Run ``uv run train_tokenizer.py --help`` for all options. The generated
tokenizer is standard uncased WordPiece and needs no custom inference code.

Reproducing v1
--------------
Run this script before ``train_model.py``. Then set
``model.tokenizer_model`` in the v1 YAML recipe to the tokenizer output
directory. Do not change the pinned dataset revision, corpus sizes, vocabulary
sizes, ordering, or seed when an exact v1 reproduction is required. The model
trainer keeps sequences at 512 tokens during training and changes only the
final exported tokenizer ceiling to 32,768, as specified by the YAML recipe.
"""

from __future__ import annotations

import argparse
import hashlib
import json
import math
import os
import time
from pathlib import Path
from typing import Any

from datasets import load_dataset
from tokenizers import (
    Tokenizer,
    decoders,
    normalizers,
    pre_tokenizers,
    processors,
    trainers,
)
from tokenizers.models import WordPiece

DATASET_ID = "hotchpotch/bekko-embedding-v1-unsupervised"
DATASET_REVISION = "4db86946dc042f8f97b675bbdbe281287c254138"
RECIPE_ID = "bekko-multilingual-wp200k-en30k-uncased"
ALGORITHM = "wordpiece"
VOCAB_SIZE = 200000
ENGLISH_CORE_SIZE = 30000
LOWERCASE = True
SCRIPT_BALANCED = False
NORMALIZATION = "bert_uncased"
PRE_TOKENIZER = "bert"
ROWS_PER_LANGUAGE = 32_768
ENGLISH_ROWS_PER_SOURCE = 32_768
TEXT_COLUMN = "pos"
SPECIAL_TOKENS = ["[UNK]", "[PAD]", "[CLS]", "[SEP]", "[MASK]"]
ENGLISH_FINEWEB_CONFIG = "hotchpotch-fineweb-ir-simulated-search-queries__english__qd"
LANGUAGE_CONFIGS = {
    "ar": "hotchpotch-wikipedia-multilingual-ir-pairs__arwiki-20251202-v1.1.0__qd",
    "bg": "huggingfacefw-finewiki__bg__qd",
    "bn": "huggingfacefw-finewiki__bn__qd",
    "ca": "huggingfacefw-finewiki__ca__qd",
    "cs": "huggingfacefw-finewiki__cs__qd",
    "da": "huggingfacefw-finewiki__da__qd",
    "de": "hotchpotch-wikipedia-multilingual-ir-pairs__dewiki-20251202-v1.1.0__qd",
    "el": "huggingfacefw-finewiki__el__qd",
    "en": "hotchpotch-wikipedia-multilingual-ir-pairs__enwiki-20251202-v1.1.0__qd",
    "es": "hotchpotch-wikipedia-multilingual-ir-pairs__eswiki-20251202-v1.1.0__qd",
    "et": "huggingfacefw-finewiki__et__qd",
    "fa": "huggingfacefw-finewiki__fa__qd",
    "fi": "huggingfacefw-finewiki__fi__qd",
    "fr": "hotchpotch-wikipedia-multilingual-ir-pairs__frwiki-20251202-v1.1.0__qd",
    "he": "huggingfacefw-finewiki__he__qd",
    "hi": "huggingfacefw-finewiki__hi__qd",
    "hr": "huggingfacefw-finewiki__hr__qd",
    "hu": "huggingfacefw-finewiki__hu__qd",
    "id": "huggingfacefw-finewiki__id__qd",
    "it": "hotchpotch-wikipedia-multilingual-ir-pairs__itwiki-20251202-v1.1.0__qd",
    "ja": "hotchpotch-wikipedia-multilingual-ir-pairs__jawiki-20251202-v1.1.0__qd",
    "ko": "hotchpotch-wikipedia-multilingual-ir-pairs__kowiki-20251202-v1.1.0__qd",
    "nl": "huggingfacefw-finewiki__nl__qd",
    "no": "huggingfacefw-finewiki__no__qd",
    "pl": "huggingfacefw-finewiki__pl__qd",
    "pt": "hotchpotch-wikipedia-multilingual-ir-pairs__ptwiki-20251202-v1.1.0__qd",
    "ro": "huggingfacefw-finewiki__ro__qd",
    "ru": "hotchpotch-wikipedia-multilingual-ir-pairs__ruwiki-20251202-v1.1.0__qd",
    "sk": "huggingfacefw-finewiki__sk__qd",
    "sl": "huggingfacefw-finewiki__sl__qd",
    "sr": "huggingfacefw-finewiki__sr__qd",
    "sv": "huggingfacefw-finewiki__sv__qd",
    "sw": "huggingfacefw-finewiki__sw__qd",
    "te": "huggingfacefw-finewiki__te__qd",
    "th": "huggingfacefw-finewiki__th__qd",
    "tr": "huggingfacefw-finewiki__tr__qd",
    "uk": "huggingfacefw-finewiki__uk__qd",
    "vi": "huggingfacefw-finewiki__vi__qd",
    "yo": "huggingfacefw-finewiki__yo__qd",
    "zh": "hotchpotch-wikipedia-multilingual-ir-pairs__zhwiki-20251202-v1.1.0__qd",
}
SCRIPT_GROUPS = {
    "arabic": {"ar", "fa"},
    "cjk": {"ja", "ko", "zh"},
    "cyrillic": {"bg", "ru", "sr", "uk"},
    "greek": {"el"},
    "indic": {"bn", "hi", "te"},
    "hebrew": {"he"},
    "thai": {"th"},
    "latin": {"ca", "cs", "da", "de", "es", "et", "fi", "fr", "hr", "hu",
              "id", "it", "nl", "no", "pl", "pt", "ro", "sk", "sl", "sv",
              "sw", "tr", "vi", "yo"},
}


def sha256_file(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def normalize_document(value: Any) -> str:
    text = " ".join(str(value).split())
    if not text:
        raise ValueError("Bekko document is empty")
    return text


def materialize_source(path: Path, config: str, rows: int) -> dict[str, Any]:
    """Download exactly one deterministic streaming prefix, with safe resume."""
    metadata_path = path.with_suffix(".json")
    if path.is_file() and metadata_path.is_file():
        metadata = json.loads(metadata_path.read_text(encoding="utf-8"))
        if (metadata.get("rows") == rows and metadata.get("config") == config
                and metadata.get("revision") == DATASET_REVISION
                and metadata.get("sha256") == sha256_file(path)):
            return metadata
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_suffix(".txt.incomplete")
    stream = load_dataset(
        DATASET_ID, config, revision=DATASET_REVISION, split="train", streaming=True
    )
    written = 0
    with temporary.open("w", encoding="utf-8") as handle:
        for row in stream.take(rows):
            handle.write(normalize_document(row[TEXT_COLUMN]) + "\n")
            written += 1
    if written != rows:
        temporary.unlink(missing_ok=True)
        raise RuntimeError(f"{config} supplied {written:,}/{rows:,} rows")
    os.replace(temporary, path)
    metadata = {
        "config": config, "dataset": DATASET_ID, "revision": DATASET_REVISION,
        "rows": written, "sha256": sha256_file(path), "text_column": TEXT_COLUMN,
    }
    metadata_path.write_text(
        json.dumps(metadata, indent=2, sort_keys=True) + "\n", encoding="utf-8"
    )
    print(f"corpus {path.stem}: {written:,} documents", flush=True)
    return metadata


def prepare_corpus(
    corpus_dir: Path, rows_per_language: int, english_rows_per_source: int
) -> tuple[list[Path], dict[str, Path]]:
    """Build 39-language extension data and a 1:1 English Wiki/FineWeb core."""
    root = corpus_dir / (
        f"multi-{rows_per_language}-english-{english_rows_per_source}-v2"
    )
    multilingual: list[Path] = []
    sources: dict[str, Any] = {}
    for language, config in sorted(LANGUAGE_CONFIGS.items()):
        if language == "en":
            continue
        path = root / "multilingual" / f"{language}.txt"
        sources[language] = materialize_source(path, config, rows_per_language)
        multilingual.append(path)
    english_paths = {
        "wikipedia": root / "english" / "wikipedia.txt",
        "fineweb": root / "english" / "fineweb.txt",
    }
    sources["en_wikipedia"] = materialize_source(
        english_paths["wikipedia"], LANGUAGE_CONFIGS["en"], english_rows_per_source
    )
    sources["en_fineweb"] = materialize_source(
        english_paths["fineweb"], ENGLISH_FINEWEB_CONFIG, english_rows_per_source
    )
    manifest = {
        "bitext_included": False,
        "english_sampling": "equal document count: Bekko Wikipedia and FineWeb",
        "english_rows_per_source": english_rows_per_source,
        "extension_language_count": 39,
        "rows_per_extension_language": rows_per_language,
        "sources": sources,
    }
    (root / "manifest.json").write_text(
        json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8"
    )
    return multilingual, english_paths


def build_wordpiece(vocab_size: int, lowercase: bool = LOWERCASE) -> tuple[Tokenizer, trainers.WordPieceTrainer]:
    if vocab_size <= len(SPECIAL_TOKENS):
        raise ValueError("vocab_size must exceed special-token count")
    tokenizer = Tokenizer(WordPiece(unk_token="[UNK]"))
    tokenizer.normalizer = normalizers.BertNormalizer(
        clean_text=True, handle_chinese_chars=True,
        strip_accents=lowercase, lowercase=lowercase,
    )
    tokenizer.pre_tokenizer = pre_tokenizers.BertPreTokenizer()
    tokenizer.decoder = decoders.WordPiece(prefix="##", cleanup=True)
    trainer = trainers.WordPieceTrainer(
        vocab_size=vocab_size, min_frequency=2, special_tokens=SPECIAL_TOKENS,
        continuing_subword_prefix="##", limit_alphabet=16_000,
    )
    return tokenizer, trainer


def extension_training_paths(paths: list[Path]) -> list[Path]:
    if not SCRIPT_BALANCED:
        return paths
    by_language = {path.stem: path for path in paths}
    largest = max(len(group) for group in SCRIPT_GROUPS.values())
    weighted: list[Path] = []
    for languages in SCRIPT_GROUPS.values():
        repeats = math.ceil(largest / len(languages))
        for language in sorted(languages):
            weighted.extend([by_language[language]] * repeats)
    if set(by_language) != set().union(*SCRIPT_GROUPS.values()):
        raise RuntimeError("SCRIPT_GROUPS must cover every extension language once")
    return weighted


def merge_vocabularies(
    english: Tokenizer, multilingual: Tokenizer, total_size: int
) -> tuple[Tokenizer, list[str]]:
    """Preserve every English token ID, then append multilingual candidates."""
    english_order = [
        token for token, _ in sorted(english.get_vocab().items(), key=lambda item: item[1])
    ]
    candidate_order = [
        token for token, _ in sorted(multilingual.get_vocab().items(), key=lambda item: item[1])
    ]
    merged = list(english_order)
    seen = set(merged)
    for token in candidate_order:
        if token not in seen:
            merged.append(token)
            seen.add(token)
            if len(merged) == total_size:
                break
    if len(merged) != total_size:
        raise RuntimeError(f"Only {len(merged):,}/{total_size:,} unique tokens available")
    vocab = {token: index for index, token in enumerate(merged)}
    tokenizer = Tokenizer(
        WordPiece(vocab=vocab, unk_token="[UNK]", continuing_subword_prefix="##")
    )
    tokenizer.normalizer = normalizers.BertNormalizer(
        clean_text=True, handle_chinese_chars=True,
        strip_accents=LOWERCASE, lowercase=LOWERCASE,
    )
    tokenizer.pre_tokenizer = pre_tokenizers.BertPreTokenizer()
    tokenizer.decoder = decoders.WordPiece(prefix="##", cleanup=True)
    tokenizer.post_processor = processors.TemplateProcessing(
        single="[CLS] $A [SEP]", pair="[CLS] $A [SEP] $B [SEP]",
        special_tokens=[
            ("[CLS]", vocab["[CLS]"]), ("[SEP]", vocab["[SEP]"])
        ],
    )
    return tokenizer, english_order


def train_tokenizer(
    *, corpus_dir: Path, output_dir: Path,
    rows_per_language: int = ROWS_PER_LANGUAGE,
    english_rows_per_source: int = ENGLISH_ROWS_PER_SOURCE,
    vocab_size: int = VOCAB_SIZE,
    english_core_size: int = ENGLISH_CORE_SIZE,
) -> Path:
    if english_core_size >= vocab_size:
        raise ValueError("english_core_size must be smaller than vocab_size")
    multilingual_paths, english_paths = prepare_corpus(
        corpus_dir, rows_per_language, english_rows_per_source
    )
    output_dir.mkdir(parents=True, exist_ok=True)
    started = time.monotonic()

    english, english_trainer = build_wordpiece(english_core_size)
    english.train(
        [str(english_paths["wikipedia"]), str(english_paths["fineweb"])],
        english_trainer,
    )
    if english.get_vocab_size() != english_core_size:
        raise RuntimeError("English core did not reach its exact requested size")

    candidate_size = vocab_size + english_core_size
    multilingual, multilingual_trainer = build_wordpiece(candidate_size)
    multilingual.train(
        [str(path) for path in extension_training_paths(multilingual_paths)],
        multilingual_trainer,
    )
    tokenizer, english_order = merge_vocabularies(english, multilingual, vocab_size)
    final_vocab = tokenizer.get_vocab()
    if any(final_vocab[token] != index for index, token in enumerate(english_order)):
        raise RuntimeError("English core token IDs changed during extension")
    tokenizer.enable_truncation(max_length=512)

    tokenizer_path = output_dir / "tokenizer.json"
    tokenizer.save(str(tokenizer_path))
    (output_dir / "tokenizer_config.json").write_text(json.dumps({
        "clean_up_tokenization_spaces": True,
        "do_lower_case": LOWERCASE,
        "model_max_length": 512,
        "tokenizer_class": "BertTokenizerFast",
        "unk_token": "[UNK]", "pad_token": "[PAD]", "cls_token": "[CLS]",
        "sep_token": "[SEP]", "mask_token": "[MASK]",
    }, indent=2, sort_keys=True) + "\n", encoding="utf-8")
    (output_dir / "special_tokens_map.json").write_text(json.dumps({
        "unk_token": "[UNK]", "pad_token": "[PAD]", "cls_token": "[CLS]",
        "sep_token": "[SEP]", "mask_token": "[MASK]",
    }, indent=2, sort_keys=True) + "\n", encoding="utf-8")
    (output_dir / "english-core-vocab.json").write_text(
        json.dumps(english_order, ensure_ascii=False) + "\n", encoding="utf-8"
    )
    manifest = {
        "algorithm": ALGORITHM, "bitext_included": False,
        "dataset": DATASET_ID, "dataset_revision": DATASET_REVISION,
        "english_core_ids_preserved": True,
        "english_core_size": english_core_size,
        "english_sampling": "Bekko Wikipedia:FineWeb = 1:1 documents",
        "extension_language_count": 39, "lowercase": LOWERCASE,
        "recipe_id": RECIPE_ID, "rows_per_language": rows_per_language,
        "english_rows_per_source": english_rows_per_source,
        "script_balanced": SCRIPT_BALANCED,
        "runtime_seconds": time.monotonic() - started,
        "tokenizer_sha256": sha256_file(tokenizer_path),
        "vocab_size_actual": tokenizer.get_vocab_size(),
        "vocab_size_requested": vocab_size,
    }
    (output_dir / "manifest.json").write_text(
        json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8"
    )
    return output_dir


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--corpus-dir", type=Path, default=Path("output/multilingual-tokenizers/corpus"))
    parser.add_argument("--output-dir", type=Path, default=Path("output/multilingual-tokenizers") / RECIPE_ID)
    parser.add_argument("--rows-per-language", type=int, default=ROWS_PER_LANGUAGE)
    parser.add_argument("--english-rows-per-source", type=int, default=ENGLISH_ROWS_PER_SOURCE)
    parser.add_argument("--vocab-size", type=int, default=VOCAB_SIZE)
    parser.add_argument("--english-core-size", type=int, default=ENGLISH_CORE_SIZE)
    args = parser.parse_args()
    print(train_tokenizer(
        corpus_dir=args.corpus_dir, output_dir=args.output_dir,
        rows_per_language=args.rows_per_language,
        english_rows_per_source=args.english_rows_per_source,
        vocab_size=args.vocab_size, english_core_size=args.english_core_size,
    ))


if __name__ == "__main__":
    main()