File size: 13,894 Bytes
e7f17a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Byte-Pair Encoding (BPE) Tokenizer for English-Malay Translation

=================================================================

We support two modes:

  1. **Shared tokenizer** (preferred for 10+2 Tied Transformer):

     A single BPE tokenizer trained on the concatenated en+ms corpus.

     Both encoder and decoder share the same vocabulary.

  2. **Separate tokenizers** (legacy):

     Two independent BPE tokenizers, one per language.



Why BPE?

  β€’ Handles subword units, so rare / unseen words are decomposed into

    known subword pieces instead of mapping to [UNK].

  β€’ Malay is morphologically rich (prefixes: me-, ber-, di-; suffixes:

    -kan, -an, -i).  BPE naturally learns these affixes as subword units,

    giving much better coverage than a word-level tokenizer.

  β€’ Keeps vocabulary compact while still reaching high coverage on both

    English and Malay.



Why shared vocabulary for en-ms?

  β€’ Both languages use the Latin script with significant lexical overlap

    (loanwords: "teknologi", "matematik", "universiti"; numbers; proper nouns).

  β€’ A joint BPE captures cross-lingual subword patterns and enables

    tied embeddings in the model (Press & Wolf, 2017), saving ~26M params.



Design choices:

  β€’ NFKC normalisation + lowercase – ensures consistent encoding of

    Unicode characters and removes casing noise.

  β€’ Whitespace pre-tokeniser – splits on spaces before BPE merges; simple

    and effective for Latin-script languages.

  β€’ Special tokens:

      [PAD]  – padding for uniform sequence lengths in batches

      [UNK]  – fallback for unknown characters

      [CLS]  – beginning-of-sequence / classification token

      [SEP]  – separator (unused in basic seq2seq but reserved)

      [MASK] – reserved for masked-LM pretraining objectives

      [BOS]  – beginning of sentence (fed to decoder at step 0)

      [EOS]  – end of sentence (signals the decoder to stop)

"""

from __future__ import annotations

import os
import tempfile
from pathlib import Path
from typing import Iterator, List, Optional, Union

from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.trainers import BpeTrainer
from tokenizers.pre_tokenizers import Whitespace
from tokenizers.normalizers import Sequence, NFKC, Lowercase
from tokenizers.processors import TemplateProcessing

# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
SPECIAL_TOKENS: List[str] = ["[PAD]", "[UNK]", "[CLS]", "[SEP]", "[MASK]", "[BOS]", "[EOS]"]
PAD_TOKEN = "[PAD]"
UNK_TOKEN = "[UNK]"
CLS_TOKEN = "[CLS]"
SEP_TOKEN = "[SEP]"
MASK_TOKEN = "[MASK]"
BOS_TOKEN = "[BOS]"
EOS_TOKEN = "[EOS]"

DEFAULT_VOCAB_SIZE = 50_000
DEFAULT_MIN_FREQUENCY = 2


# ---------------------------------------------------------------------------
# Helper: write an iterator of strings to a temporary file (needed by the
# HuggingFace `tokenizers` training API which expects file paths).
# ---------------------------------------------------------------------------
def _write_texts_to_tmpfile(texts: Iterator[str]) -> str:
    """Write an iterable of strings to a temp file, one per line. Returns path."""
    tmp = tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, encoding="utf-8")
    for line in texts:
        line = line.strip()
        if line:
            tmp.write(line + "\n")
    tmp.close()
    return tmp.name


# ---------------------------------------------------------------------------
# Core: build & train a BPE tokenizer
# ---------------------------------------------------------------------------
def build_tokenizer(

    vocab_size: int = DEFAULT_VOCAB_SIZE,

    min_frequency: int = DEFAULT_MIN_FREQUENCY,

) -> tuple[Tokenizer, BpeTrainer]:
    """

    Create an *untrained* BPE tokenizer and its trainer.



    Returns

    -------

    tokenizer : Tokenizer

        Ready to call ``tokenizer.train(files, trainer)``.

    trainer : BpeTrainer

        Configured trainer instance.

    """
    tokenizer = Tokenizer(BPE(unk_token=UNK_TOKEN))

    # --- Normalisation: NFKC (canonical Unicode) + lowercase -------------
    tokenizer.normalizer = Sequence([NFKC(), Lowercase()])

    # --- Pre-tokenisation: split on whitespace ---------------------------
    tokenizer.pre_tokenizer = Whitespace()

    # --- Trainer ---------------------------------------------------------
    trainer = BpeTrainer(
        vocab_size=vocab_size,
        min_frequency=min_frequency,
        special_tokens=SPECIAL_TOKENS,
        show_progress=True,
    )

    return tokenizer, trainer


def train_tokenizer(

    texts: Union[List[str], Iterator[str]],

    vocab_size: int = DEFAULT_VOCAB_SIZE,

    min_frequency: int = DEFAULT_MIN_FREQUENCY,

    files: Optional[List[str]] = None,

) -> Tokenizer:
    """

    Train a BPE tokenizer on the given texts **or** files.



    Parameters

    ----------

    texts : list[str] or iterator of str, optional

        Raw sentences.  Ignored when *files* is provided.

    vocab_size : int

        Target vocabulary size (default 30 000).

    min_frequency : int

        Minimum frequency for a pair to be merged.

    files : list[str], optional

        Paths to plain-text files (one sentence per line).



    Returns

    -------

    Tokenizer

        Trained tokenizer ready for encoding / decoding.

    """
    tokenizer, trainer = build_tokenizer(vocab_size, min_frequency)

    if files is not None:
        tokenizer.train(files, trainer)
    else:
        # Write texts to a temporary file so we can use the fast Rust trainer
        tmp_path = _write_texts_to_tmpfile(iter(texts))
        try:
            tokenizer.train([tmp_path], trainer)
        finally:
            os.remove(tmp_path)

    # --- Post-processing: wrap every encoded sequence with [BOS] … [EOS] -
    bos_id = tokenizer.token_to_id(BOS_TOKEN)
    eos_id = tokenizer.token_to_id(EOS_TOKEN)
    tokenizer.post_processor = TemplateProcessing(
        single=f"[BOS]:0 $A:0 [EOS]:0",
        pair=f"[BOS]:0 $A:0 [EOS]:0 [BOS]:1 $B:1 [EOS]:1",
        special_tokens=[
            ("[BOS]", bos_id),
            ("[EOS]", eos_id),
        ],
    )

    return tokenizer


# ---------------------------------------------------------------------------
# Convenience wrappers for saving / loading
# ---------------------------------------------------------------------------
def save_tokenizer(tokenizer: Tokenizer, path: Union[str, Path]) -> None:
    """Save a trained tokenizer to a JSON file."""
    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    tokenizer.save(str(path))
    print(f"[βœ“] Tokenizer saved β†’ {path}")


def load_tokenizer(path: Union[str, Path]) -> Tokenizer:
    """Load a previously saved tokenizer from a JSON file."""
    tokenizer = Tokenizer.from_file(str(path))
    print(f"[βœ“] Tokenizer loaded ← {path}")
    return tokenizer


# ---------------------------------------------------------------------------
# Encoding / decoding helpers
# ---------------------------------------------------------------------------
def encode(tokenizer: Tokenizer, text: str) -> List[int]:
    """Encode a single string and return token IDs (includes [BOS]/[EOS])."""
    return tokenizer.encode(text).ids


def decode(tokenizer: Tokenizer, ids: List[int]) -> str:
    """Decode token IDs back to a string, skipping special tokens."""
    return tokenizer.decode(ids, skip_special_tokens=True)


def get_vocab_size(tokenizer: Tokenizer) -> int:
    """Return the size of the tokenizer's vocabulary."""
    return tokenizer.get_vocab_size()


def token_to_id(tokenizer: Tokenizer, token: str) -> Optional[int]:
    """Look up the integer ID for a single token string."""
    return tokenizer.token_to_id(token)


def id_to_token(tokenizer: Tokenizer, id: int) -> Optional[str]:
    """Look up the token string for a single integer ID."""
    return tokenizer.id_to_token(id)


# ---------------------------------------------------------------------------
# High-level: train a SHARED tokenizer on both languages (for tied embeddings)
# ---------------------------------------------------------------------------
def train_shared_tokenizer_from_dataset(

    dataset,

    src_lang: str = "en",

    tgt_lang: str = "ms",

    vocab_size: int = DEFAULT_VOCAB_SIZE,

    save_dir: Union[str, Path] = "tokenizer",

) -> Tokenizer:
    """

    Train a single shared BPE tokenizer on the concatenated en+ms corpus.



    This is used with the 10+2 Tied Transformer architecture, where both

    encoder and decoder share the same vocabulary and embedding matrix.



    Parameters

    ----------

    dataset : datasets.Dataset

        A HuggingFace dataset split where each example has a ``'translation'``

        dict with keys for each language code.

    src_lang : str

        Source language code (default ``'en'``).

    tgt_lang : str

        Target language code (default ``'ms'``).

    vocab_size : int

        Vocabulary size for the shared tokenizer.

    save_dir : str or Path

        Directory to save the trained tokenizer JSON file.



    Returns

    -------

    Tokenizer

        A single shared tokenizer for both languages.

    """
    save_dir = Path(save_dir)

    # Concatenate all source and target sentences into one corpus
    src_texts = [example["translation"][src_lang] for example in dataset]
    tgt_texts = [example["translation"][tgt_lang] for example in dataset]
    all_texts = src_texts + tgt_texts

    print(f"Training shared BPE tokenizer on {len(all_texts):,} sentences "
          f"({len(src_texts):,} {src_lang} + {len(tgt_texts):,} {tgt_lang}) …")
    shared_tokenizer = train_tokenizer(all_texts, vocab_size=vocab_size)
    save_tokenizer(shared_tokenizer, save_dir / "tokenizer_shared.json")

    # Sanity check
    for name, sample in [(src_lang, src_texts[0]), (tgt_lang, tgt_texts[0])]:
        enc = shared_tokenizer.encode(sample)
        print(f"\n[{name}] Sample: {sample[:80]}…")
        print(f"  Tokens : {enc.tokens[:15]}…")
        print(f"  IDs    : {enc.ids[:15]}…")
        print(f"  Decoded: {shared_tokenizer.decode(enc.ids, skip_special_tokens=True)[:80]}…")

    print(f"\n[βœ“] Shared tokenizer trained and saved to {save_dir}/tokenizer_shared.json")
    return shared_tokenizer


# ---------------------------------------------------------------------------
# High-level: train source (English) & target (Malay) tokenizers from a
# HuggingFace dataset split.
# ---------------------------------------------------------------------------
def train_tokenizers_from_dataset(

    dataset,

    src_lang: str = "en",

    tgt_lang: str = "ms",

    vocab_size: int = DEFAULT_VOCAB_SIZE,

    save_dir: Union[str, Path] = "tokenizer",

) -> tuple[Tokenizer, Tokenizer]:
    """

    Train separate BPE tokenizers for source and target languages.



    Parameters

    ----------

    dataset : datasets.Dataset

        A HuggingFace dataset split (e.g. ``dataset['train']``) where each

        example has a ``'translation'`` dict with keys for each language code.

    src_lang : str

        Source language code (default ``'en'``).

    tgt_lang : str

        Target language code (default ``'ms'``).

    vocab_size : int

        Vocabulary size for each tokenizer.

    save_dir : str or Path

        Directory to save the trained tokenizer JSON files.



    Returns

    -------

    (src_tokenizer, tgt_tokenizer)

    """
    save_dir = Path(save_dir)

    # Extract raw sentences from the dataset
    src_texts = [example["translation"][src_lang] for example in dataset]
    tgt_texts = [example["translation"][tgt_lang] for example in dataset]

    print(f"Training source tokenizer ({src_lang}) on {len(src_texts):,} sentences …")
    src_tokenizer = train_tokenizer(src_texts, vocab_size=vocab_size)
    save_tokenizer(src_tokenizer, save_dir / f"tokenizer_{src_lang}.json")

    print(f"Training target tokenizer ({tgt_lang}) on {len(tgt_texts):,} sentences …")
    tgt_tokenizer = train_tokenizer(tgt_texts, vocab_size=vocab_size)
    save_tokenizer(tgt_tokenizer, save_dir / f"tokenizer_{tgt_lang}.json")

    # Quick sanity check
    for name, tok, sample in [
        (src_lang, src_tokenizer, src_texts[0]),
        (tgt_lang, tgt_tokenizer, tgt_texts[0]),
    ]:
        enc = tok.encode(sample)
        print(f"\n[{name}] Sample: {sample[:80]}…")
        print(f"  Tokens : {enc.tokens[:15]}…")
        print(f"  IDs    : {enc.ids[:15]}…")
        print(f"  Decoded: {tok.decode(enc.ids, skip_special_tokens=True)[:80]}…")

    print(f"\n[βœ“] Both tokenizers trained and saved to {save_dir}/")
    return src_tokenizer, tgt_tokenizer


# ---------------------------------------------------------------------------
# Standalone usage
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    from datasets import load_from_disk

    print("Loading TED Talks IWSLT dataset (en ↔ ms, 2016) …")
    ds = load_from_disk("dataset/en_ms_2016")

    src_tok, tgt_tok = train_tokenizers_from_dataset(
        ds,
        src_lang="en",
        tgt_lang="ms",
        vocab_size=DEFAULT_VOCAB_SIZE,
        save_dir="tokenizer",
    )

    print(f"\nEnglish vocab size : {get_vocab_size(src_tok):,}")
    print(f"Malay vocab size   : {get_vocab_size(tgt_tok):,}")
    print(f"[PAD] id (en)      : {token_to_id(src_tok, PAD_TOKEN)}")
    print(f"[EOS] id (ms)      : {token_to_id(tgt_tok, EOS_TOKEN)}")