aboutsummaryrefslogtreecommitdiff
path: root/devtools/gen_test_vectors.py
blob: 44206ecb53e16d86f2c8e41e4d9a78f8977ac3e9 (plain) (blame)
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
#!/usr/bin/env python3
"""Generate tokenizer parity vectors for the hermetic tokenizer checks.

tests/check_tokenizer_parity.py and tests/check_wordpiece_parity.py read
the stored vectors generated by this script. Regenerate the vectors when
the test corpus changes or the reference tokenizer is bumped, then check
the result into git.

For each tokenizer family it writes, under tests/test-vectors/<family>/:
  - the vocab files the C driver loads (vocab.json + merges.txt for BPE;
    vocab.txt + tokenizer_config.json for WordPiece),
  - expected.json: provenance plus a list of {text, ids} cases.

- The BPE reference is the fast Qwen tokenizer (what the C BPE backend
targets).
- The WordPiece reference is the slow BertTokenizer (the exact reference
for the C WordPiece backend).
- Both encode with add_special_tokens=False, matching the C drivers,
which emit core ids and leave [CLS]/[SEP] to the frontend.

  uv run --with transformers devtools/gen_test_vectors.py
  uv run --with transformers devtools/gen_test_vectors.py --family bpe
  uv run --with transformers devtools/gen_test_vectors.py --bpe-model DIR --fuzz 128
"""

import argparse
import json
import random
import re
import shutil
import sys
from pathlib import Path

# BPE corpus: ASCII, accents, decomposed Unicode, emoji, digits, and long
# repeated strings that stress the merge loop.
BPE_TEXTS = [
    "",
    "hello world",
    "query: what is the capital of France?",
    "document: Paris is the capital of France.",
    "punctuation: hello, world!!! (test)",
    "repeated   spaces\tand\nnewlines",
    "unicode: cafe, caf\xe9, Istanbul, İstanbul",
    "unicode decomposed: café, façade",
    "emoji: embeddings \U0001f50d are useful",
    "numbers: 12345 67.89 -0.12",
    "a " * 256,
    "antidisestablishmentarianism" * 64,
    ("document: " + "dense retrieval systems need stable tokenization. " * 256),
]

BPE_FUZZ_ALPHABET = (
    "abcdefghijklmnopqrstuvwxyz"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "0123456789"
    "     \t\n\r"
    ".,!?;:()[]{}'\"-/"
    "éèêëáàâäíìîïóòôöúùûüñç"
    "¿¡«»—…"
    "中文测试Русскийعربيहिन्दी"
    "🔍👍🏽🇺🇸"
)

# WordPiece corpus: stays within the implemented coverage (ASCII, Latin-1
# accents, common punctuation, CJK char-splitting) plus a multilingual set.
WORDPIECE_TEXTS = [
    "",
    "hello world",
    "The capital of France is Paris.",
    "Tokenization, sub-words, and WordPiece!",
    "unaffable antidisestablishmentarianism",
    "café résumé naïve Zürich Málaga",
    "COVID-19 affected 2020-2021 globally.",
    "don't can't won't it's o'clock",
    "e-mail user@example.com or http://test.org/path?q=1",
    "numbers: 12345 67.89 -0.12 3.14159 (approx)",
    "repeated   spaces\tand\nnewlines\rhere",
    "Hyphenated state-of-the-art well-known co-operate",
    "MixedCase camelCase snake_case ALLCAPS",
    "中 文 字 符",
    "a " * 200,
    "supercalifragilisticexpialidocious pneumonoultramicroscopicsilicovolcanoconiosis",
    "Müller wohnt in Zürich, Straße.",
    "L'élève préfère — n'est-ce pas?",
    "¿Cómo estás? ¡Muy bien!",
    "你好,世界。こんにちは",
]

WORDPIECE_FUZZ_ALPHABET = (
    "abcdefghijklmnopqrstuvwxyz"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "0123456789"
    "     \t\n\r"
    ".,!?;:()[]{}'\"-/@#"
    "éèêëáàâäíìîïóòôöúùûüñçßæøþ"
)


def repo_root() -> Path:
    return Path(__file__).resolve().parents[1]


def transformers_version() -> str:
    import transformers
    return transformers.__version__


def snapshot_revision(local_path: str) -> str:
    """Best-effort: pull the commit hash from an HF cache path, else ''."""
    m = re.search(r"/snapshots/([0-9a-f]{8,})/", local_path + "/")
    return m.group(1) if m else ""


def resolve_file(model: str, filename: str) -> Path:
    """Return a local path to one of the model's files, from a local dir or the
    HF cache (download only if absent)."""
    local = Path(model) / filename
    if local.exists():
        return local
    from huggingface_hub import hf_hub_download
    return Path(hf_hub_download(model, filename))


def fuzz_cases(alphabet: str, count: int, seed: int) -> list[str]:
    rng = random.Random(seed)
    out = []
    for _ in range(count):
        n = rng.randint(0, 96)
        out.append("".join(rng.choice(alphabet) for _ in range(n)))
    return out


def write_vectors(out_dir: Path, family: str, model: str, revision: str,
                  texts: list[str], encode) -> None:
    out_dir.mkdir(parents=True, exist_ok=True)
    cases = [{"text": t, "ids": encode(t)} for t in texts]
    payload = {
        "family": family,
        "model": model,
        "revision": revision,
        "transformers_version": transformers_version(),
        "add_special_tokens": False,
        "cases": cases,
    }
    (out_dir / "expected.json").write_text(
        json.dumps(payload, ensure_ascii=False, indent=1) + "\n", encoding="utf-8")
    print(f"ok: wrote {len(cases)} {family} cases to {out_dir}")


def gen_bpe(root: Path, model: str, fuzz: int, seed: int) -> None:
    from transformers import AutoTokenizer
    tok = AutoTokenizer.from_pretrained(model, trust_remote_code=True)
    out_dir = root / "tests/test-vectors/bpe"
    out_dir.mkdir(parents=True, exist_ok=True)
    # Copy the shipped vocab.json + merges.txt the C BPE loader reads. The fast
    # tokenizer's save_vocabulary does not re-emit them, so copy the originals
    # the reference tokenizer was built from.
    src = ""
    for fn in ("vocab.json", "merges.txt"):
        path = resolve_file(model, fn)
        src = str(path)
        shutil.copyfile(path, out_dir / fn)
    texts = list(BPE_TEXTS) + fuzz_cases(BPE_FUZZ_ALPHABET, fuzz, seed)
    revision = snapshot_revision(src)
    write_vectors(out_dir, "bpe", model, revision, texts,
                  lambda t: tok.encode(t, add_special_tokens=False))


def gen_wordpiece(root: Path, model: str, fuzz: int, seed: int) -> None:
    from transformers import BertTokenizer
    tok = BertTokenizer.from_pretrained(model)
    out_dir = root / "tests/test-vectors/wordpiece"
    out_dir.mkdir(parents=True, exist_ok=True)
    # Copy vocab.txt and write the tokenizer_config.json the C loader reads for
    # do_lower_case (save_pretrained would emit tokenizer.json, which the C
    # side ignores).
    src = resolve_file(model, "vocab.txt")
    shutil.copyfile(src, out_dir / "vocab.txt")
    lower = "true" if tok.do_lower_case else "false"
    (out_dir / "tokenizer_config.json").write_text(
        '{"do_lower_case": %s}\n' % lower, encoding="utf-8")
    texts = list(WORDPIECE_TEXTS) + fuzz_cases(WORDPIECE_FUZZ_ALPHABET, fuzz, seed)
    revision = snapshot_revision(str(src))
    write_vectors(out_dir, "wordpiece", model, revision, texts,
                  lambda t: tok.encode(t, add_special_tokens=False))


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--family", choices=("bpe", "wordpiece", "both"), default="both")
    ap.add_argument("--bpe-model", default="Qwen/Qwen3-Embedding-0.6B",
                    help="HF name or local dir for the BPE reference tokenizer")
    ap.add_argument("--wordpiece-model", default="bert-base-uncased",
                    help="HF name or local dir for the WordPiece reference tokenizer")
    ap.add_argument("--fuzz", type=int, default=64,
                    help="deterministic random cases to bake in per family")
    ap.add_argument("--seed", type=int, default=1)
    args = ap.parse_args()

    root = repo_root()
    if args.family in ("bpe", "both"):
        gen_bpe(root, args.bpe_model, args.fuzz, args.seed)
    if args.family in ("wordpiece", "both"):
        gen_wordpiece(root, args.wordpiece_model, args.fuzz, args.seed)
    return 0


if __name__ == "__main__":
    sys.exit(main())