File size: 8,883 Bytes
f17dddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Phase 1 — tests unitaires du tokenizer BPE de Cortex-0.

Rapides (CPU, quelques secondes) : un **tokenizer jouet** est entraîné une fois
pour toute la session sur un petit corpus français embarqué. Le tokenizer final
(32k, corpus ~200 Mo) est produit par `notebooks/01_tokenizer.ipynb` sur Colab.

Deux catégories de tests :
  * invariants qui ne dépendent PAS du corpus (tokens spéciaux, round-trip,
    accents, emojis, scripts inconnus, byte_fallback) → testés sur le tokenizer jouet ;
  * invariants du tokenizer FINAL (vocab == 32000) → ignorés si l'artefact réel
    n'est pas présent (avant le run Colab).
"""

from __future__ import annotations

import json
from pathlib import Path

import pytest

from src.tokenizer import load_tokenizer
from src.tokenizer.special_tokens import (
    ALL_SPECIAL_TOKENS,
    BASE_SPECIAL_TOKENS,
    MODALITY_SPECIAL_TOKENS,
    N_SPECIAL_TOKENS,
    REASONING_SPECIAL_TOKENS,
    SPECIAL_TOKEN_IDS,
    SPECIAL_TOKENS_MAP_FILE,
    TOKENIZER_CONFIG_FILE,
    TOKENIZER_DIR,
    VOCAB_SIZE,
)
from src.tokenizer.train_tokenizer import (
    assert_special_token_ids,
    save_artifacts,
    train_tokenizer,
)

ROOT = Path(__file__).resolve().parents[1]

# 20 phrases françaises variées (accents, ponctuation, majuscules, chiffres).
FRENCH_SENTENCES = [
    "Bonjour, comment allez-vous aujourd'hui ?",
    "Le café coûte trois euros à Paris.",
    "L'élève écrit une phrase sur le tableau noir.",
    "Où est la forêt de Brocéliande ?",
    "Le garçon mange une pomme rouge et juteuse.",
    "Les enfants jouent dans le jardin après l'école.",
    "Il fait très beau à Montréal en été.",
    "La bibliothèque ouvre à neuf heures précises.",
    "Ce problème de mathématiques est vraiment difficile.",
    "Le chat dort paisiblement sur le canapé du salon.",
    "J'ai acheté deux baguettes et un croissant.",
    "La France compte de nombreuses régions pittoresques.",
    "Elle répond toujours avec beaucoup de patience.",
    "N'oubliez pas de fermer la porte à clé !",
    "Le train pour Lyon part dans quinze minutes.",
    "Nous irons à la mer dès que possible.",
    "Cette recette nécessite un four préchauffé.",
    "La langue française est riche en nuances.",
    "Il pleut des cordes depuis ce matin.",
    "Merci beaucoup pour votre aide précieuse.",
]

TOY_CORPUS = FRENCH_SENTENCES * 60 + [
    "Le soleil brille, les oiseaux chantent, tout va bien.",
    "Une phrase française supplémentaire pour l'entraînement.",
]

ACCENTS = "é è ê à ç ù î ô ë ï ü ÿ œ æ"
EMOJIS = "🦾 🇬🇳 🚀 🙂 ❤️ 🎉"
UNKNOWN_SCRIPTS = "こんにちは世界 مرحبا بالعالم Здравствуй мир"


@pytest.fixture(scope="session")
def toy_dir(tmp_path_factory) -> Path:
    """Entraîne un tokenizer jouet (vocab ~1500) une fois par session."""
    out = tmp_path_factory.mktemp("toy_tokenizer")
    tok = train_tokenizer(TOY_CORPUS, vocab_size=1500, min_frequency=1, show_progress=False)
    save_artifacts(tok, out)
    return out


@pytest.fixture(scope="session")
def toy(toy_dir):
    return load_tokenizer(toy_dir)


# ---------------------------------------------------------------------------
# Source de vérité des tokens spéciaux
# ---------------------------------------------------------------------------


def test_special_token_lists_are_disjoint_and_unique():
    assert len(set(ALL_SPECIAL_TOKENS)) == N_SPECIAL_TOKENS
    assert set(BASE_SPECIAL_TOKENS).isdisjoint(REASONING_SPECIAL_TOKENS)
    assert set(BASE_SPECIAL_TOKENS).isdisjoint(MODALITY_SPECIAL_TOKENS)
    assert set(REASONING_SPECIAL_TOKENS).isdisjoint(MODALITY_SPECIAL_TOKENS)


def test_special_token_counts():
    assert len(BASE_SPECIAL_TOKENS) == 4
    assert len(REASONING_SPECIAL_TOKENS) == 6
    assert len(MODALITY_SPECIAL_TOKENS) == 9
    assert N_SPECIAL_TOKENS == 19


def test_special_token_ids_are_contiguous():
    assert sorted(SPECIAL_TOKEN_IDS.values()) == list(range(N_SPECIAL_TOKENS))


def test_required_special_tokens_present():
    required = [
        "<|pad|>", "<|bos|>", "<|eos|>", "<|unk|>",
        "<|think|>", "<|plan|>", "<|tool_call|>", "<|tool_result|>", "<|critic|>", "<|moe_route|>",
        "<|image_start|>", "<|image_end|>", "<|image_pad|>",
        "<|audio_start|>", "<|audio_end|>", "<|audio_pad|>",
        "<|video_start|>", "<|video_end|>", "<|video_pad|>",
    ]
    for token in required:
        assert token in SPECIAL_TOKEN_IDS, f"token spécial manquant : {token}"


# ---------------------------------------------------------------------------
# Artefacts sur disque
# ---------------------------------------------------------------------------


def test_three_artifacts_written(toy_dir):
    for name in ("tokenizer.json", SPECIAL_TOKENS_MAP_FILE, TOKENIZER_CONFIG_FILE):
        assert (toy_dir / name).is_file(), f"{name} absent"


def test_special_tokens_map_contains_all_required(toy_dir):
    data = json.loads((toy_dir / SPECIAL_TOKENS_MAP_FILE).read_text(encoding="utf-8"))
    present = set(data.get("additional_special_tokens", []))
    present |= {data.get("pad_token"), data.get("bos_token"), data.get("eos_token"), data.get("unk_token")}
    for token in ALL_SPECIAL_TOKENS:
        assert token in present, f"{token} absent de special_tokens_map.json"


def test_special_token_ids_match_expected(toy):
    assert_special_token_ids(toy.raw)
    for token, expected in SPECIAL_TOKEN_IDS.items():
        assert toy.token_to_id(token) == expected, token


def test_special_token_ids_stable_across_two_runs(tmp_path):
    a = train_tokenizer(TOY_CORPUS, vocab_size=1500, min_frequency=1, show_progress=False)
    b = train_tokenizer(TOY_CORPUS, vocab_size=1500, min_frequency=1, show_progress=False)
    assert a.get_vocab() == b.get_vocab()


# ---------------------------------------------------------------------------
# Round-trip et cas limites
# ---------------------------------------------------------------------------


@pytest.mark.parametrize("sentence", FRENCH_SENTENCES)
def test_roundtrip_french_sentences(toy, sentence):
    assert toy.decode(toy.encode(sentence)) == sentence


def test_roundtrip_accents(toy):
    assert toy.decode(toy.encode(ACCENTS)) == ACCENTS


def test_roundtrip_emojis(toy):
    assert toy.decode(toy.encode(EMOJIS)) == EMOJIS


def test_roundtrip_unknown_scripts(toy):
    assert toy.decode(toy.encode(UNKNOWN_SCRIPTS)) == UNKNOWN_SCRIPTS


def test_roundtrip_empty_and_whitespace(toy):
    assert toy.decode(toy.encode("")) == ""
    assert toy.decode(toy.encode("   ")) == "   "


def test_roundtrip_very_long(toy):
    long_text = " ".join(FRENCH_SENTENCES * 20)
    assert toy.decode(toy.encode(long_text)) == long_text


def test_special_tokens_recognized_when_in_text(toy):
    text = "Bonjour <|bos|> le monde <|eos|>"
    ids = toy.encode(text)
    assert toy.token_to_id("<|bos|>") in ids
    assert toy.token_to_id("<|eos|>") in ids


def test_encode_returns_ints(toy):
    ids = toy.encode("Un test.")
    assert ids and all(isinstance(i, int) for i in ids)
    assert all(0 <= i < toy.vocab_size for i in ids)


def test_decode_skips_special_tokens(toy):
    ids = [toy.token_to_id("<|bos|>")] + toy.encode("salut") + [toy.token_to_id("<|eos|>")]
    decoded = toy.decode(ids, skip_special_tokens=True)
    assert "<|bos|>" not in decoded and "<|eos|>" not in decoded
    assert "salut" in decoded


def test_byte_fallback_is_enabled(toy):
    # Un caractère jamais vu (CJK) doit survivre au round-trip -> repli octet.
    assert toy.decode(toy.encode("漢字第")) == "漢字第"


# ---------------------------------------------------------------------------
# Entraînement
# ---------------------------------------------------------------------------


def test_trainer_respects_vocab_size_cap():
    tok = train_tokenizer(TOY_CORPUS, vocab_size=800, min_frequency=1, show_progress=False)
    assert tok.get_vocab_size() <= 800


def test_min_frequency_reduces_vocab():
    low = train_tokenizer(TOY_CORPUS, vocab_size=2000, min_frequency=1, show_progress=False)
    high = train_tokenizer(TOY_CORPUS, vocab_size=2000, min_frequency=20, show_progress=False)
    assert high.get_vocab_size() <= low.get_vocab_size()


# ---------------------------------------------------------------------------
# Invariants du tokenizer FINAL (nécessite le run Colab)
# ---------------------------------------------------------------------------


@pytest.mark.skipif(
    not (ROOT / TOKENIZER_DIR / "tokenizer.json").is_file(),
    reason="tokenizer final absent : généré par notebooks/01_tokenizer.ipynb (Colab)",
)
def test_final_tokenizer_vocab_size_is_exactly_32000():
    final = load_tokenizer()
    assert final.vocab_size == VOCAB_SIZE
    for token, expected in SPECIAL_TOKEN_IDS.items():
        assert final.token_to_id(token) == expected