tinycodinglm / i18n /i18n-0001.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
2.36 kB
opencode trace
System You are an expert NLP engineer. You explain tokenization clearly and give correct, minimal Python examples.

You are an expert NLP engineer. You explain tokenization clearly and give correct, minimal Python examples.

User

I trained a BPE tokenizer on English-only text and now non-English text uses far more tokens. Why, and how do I fix it?

Assistant

The tokenizer still works (byte fallback never fails), but it suffers from over-segmentation: merges learned from English never cover other scripts, so they fall back to single bytes. The standard measure is fertility = tokens per word (or per character). Higher fertility means longer sequences, higher cost, and usually worse quality.

Illustrative fertility with a typical 32k English-trained byte-level BPE:

input tokens (approx)
hello 1
xin chào (Vietnamese) 5-7, diacritics split off
你好世界 (Chinese) ~12, i.e. 3 byte-tokens per character
مرحبا (Arabic) ~10, 2 byte-tokens per character

English text stays near 1 token per word while CJK text costs ~3 tokens per character. This imbalance is called the tokenizer parity problem.

Fixes:

  1. Train on a balanced multilingual corpus (temperature sampling so small languages are upsampled).
  2. Verify script coverage: every target script should appear in whole-token merges, not just bytes.
  3. Consider a larger vocabulary (64k-256k) when supporting many scripts.
  4. Evaluate fertility per language before shipping the tokenizer:
python
def fertility(tokenizer, words):
    """Average tokens per word; compare across languages."""
    return sum(len(tokenizer.encode(w)) for w in words) / max(1, len(words))

langs = {
    "en": ["hello", "world", "tokenizer"],
    "vi": ["xin", "chào", "thế", "giới"],
    "zh": ["你好", "世界", "分词"],
    "ar": ["مرحبا", "بالعالم"],
}
for lang, words in langs.items():
    print(lang, round(fertility(tokenizer, words), 2))

Aim for fertility within ~2x across supported languages; if one script is an outlier, its training share was too small.