File size: 9,359 Bytes
b3c2a26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Intrinsic tokenizer benchmarks with explicit baseline comparability."""

from __future__ import annotations

import gc
import importlib.metadata
import json
import math
import platform
import re
import statistics
import time
from collections.abc import Callable, Iterable
from dataclasses import asdict, dataclass
from datetime import UTC, datetime
from pathlib import Path
from typing import Any, Protocol

import psutil
from datasets import load_dataset
from tokenizers import Tokenizer

from .config import (
    DEFAULT_SEED,
    KANANA_MODEL_ID,
    KANANA_REVISION,
    KLUE_REVISION,
    Paths,
)

WHITESPACE_UNIT_RE = re.compile(r"\S+")

KLUE_FIELDS: dict[str, tuple[str, ...]] = {
    "ynat": ("title",),
    "sts": ("sentence1", "sentence2"),
    "nli": ("premise", "hypothesis"),
    "ner": ("sentence",),
    "re": ("sentence",),
    "dp": ("sentence",),
    "mrc": ("context", "question"),
    "wos": ("dialogue",),
}


class Counter(Protocol):
    name: str
    reversible: bool

    def count_many(self, texts: list[str]) -> list[int]: ...


@dataclass
class DomainMetrics:
    texts: int
    characters: int
    utf8_bytes: int
    whitespace_units: int
    tokens: int
    fertility: float
    characters_per_token: float
    bytes_per_token: float
    throughput_mib_s: float
    elapsed_s_median: float


@dataclass
class FastCounter:
    name: str
    tokenizer: Tokenizer
    reversible: bool = True

    def count_many(self, texts: list[str]) -> list[int]:
        return [len(encoding.ids) for encoding in self.tokenizer.encode_batch(texts)]


@dataclass
class FunctionCounter:
    name: str
    function: Callable[[str], list[str]]
    reversible: bool = False

    def count_many(self, texts: list[str]) -> list[int]:
        return [len(self.function(text)) for text in texts]


def _flatten_strings(value: Any) -> Iterable[str]:
    if isinstance(value, str):
        if value.strip():
            yield value
    elif isinstance(value, list):
        for item in value:
            yield from _flatten_strings(item)
    elif isinstance(value, dict):
        text = value.get("text")
        if isinstance(text, str):
            yield from _flatten_strings(text)


def load_klue_domains(*, limit_per_domain: int, seed: int = DEFAULT_SEED) -> dict[str, list[str]]:
    """Load deterministic held-out Korean text without persisting examples."""

    domains: dict[str, list[str]] = {}
    for index, (config_name, fields) in enumerate(KLUE_FIELDS.items()):
        dataset = load_dataset(
            "klue/klue",
            config_name,
            split="validation",
            revision=KLUE_REVISION,
        )
        dataset = dataset.shuffle(seed=seed + index)
        texts: list[str] = []
        for row in dataset:
            for field in fields:
                texts.extend(_flatten_strings(row.get(field)))
                if len(texts) >= limit_per_domain:
                    break
            if len(texts) >= limit_per_domain:
                break
        domains[config_name] = texts[:limit_per_domain]
        if not domains[config_name]:
            raise RuntimeError(f"No benchmark text extracted for KLUE/{config_name}")
    return domains


def _timed_counts(counter: Counter, texts: list[str], *, repeats: int) -> tuple[list[int], float]:
    warmup = texts[: min(32, len(texts))]
    if warmup:
        counter.count_many(warmup)
    durations: list[float] = []
    counts: list[int] | None = None
    for _ in range(repeats):
        gc.collect()
        started = time.perf_counter()
        current = counter.count_many(texts)
        durations.append(time.perf_counter() - started)
        if counts is None:
            counts = current
        elif counts != current:
            raise RuntimeError(f"Non-deterministic token counts from {counter.name}")
    if counts is None:
        return [], 0.0
    return counts, statistics.median(durations)


def _metrics(counter: Counter, texts: list[str], *, repeats: int) -> DomainMetrics:
    counts, elapsed = _timed_counts(counter, texts, repeats=repeats)
    characters = sum(map(len, texts))
    utf8_bytes = sum(len(text.encode("utf-8")) for text in texts)
    units = sum(len(WHITESPACE_UNIT_RE.findall(text)) for text in texts)
    tokens = sum(counts)
    mib = utf8_bytes / (1024 * 1024)
    return DomainMetrics(
        texts=len(texts),
        characters=characters,
        utf8_bytes=utf8_bytes,
        whitespace_units=units,
        tokens=tokens,
        fertility=tokens / units if units else math.nan,
        characters_per_token=characters / tokens if tokens else math.nan,
        bytes_per_token=utf8_bytes / tokens if tokens else math.nan,
        throughput_mib_s=mib / elapsed if elapsed else math.inf,
        elapsed_s_median=elapsed,
    )


def _optional_counters() -> tuple[list[Counter], dict[str, str]]:
    counters: list[Counter] = []
    unavailable: dict[str, str] = {}
    try:
        from konlpy.tag import Okt

        okt = Okt()
        counters.append(
            FunctionCounter("okt", lambda text: okt.morphs(text, norm=False, stem=False))
        )
    except Exception as error:  # pragma: no cover - host dependency
        unavailable["okt"] = f"{type(error).__name__}: {error}"

    try:
        try:
            import MeCab
        except ModuleNotFoundError:
            import mecab_ko as MeCab

        tagger = MeCab.Tagger("-Owakati")
        counters.append(
            FunctionCounter("mecab-ko", lambda text: tagger.parse(text).strip().split())
        )
    except Exception as error:  # pragma: no cover - host dependency
        unavailable["mecab-ko"] = f"{type(error).__name__}: {error}"
    return counters, unavailable


def _package_versions() -> dict[str, str]:
    versions: dict[str, str] = {}
    for package in ("tokenizers", "konlpy", "mecab-ko", "mecab-ko-dic"):
        try:
            versions[package] = importlib.metadata.version(package)
        except importlib.metadata.PackageNotFoundError:
            continue
    return versions


def run_benchmark(
    root: Path,
    *,
    limit_per_domain: int = 1_000,
    repeats: int = 3,
    include_morphological: bool = True,
) -> dict[str, Any]:
    """Run and persist the full held-out intrinsic benchmark."""

    paths = Paths(root)
    tokenizer_path = root / "tokenizer.json"
    if not tokenizer_path.is_file():
        raise FileNotFoundError(tokenizer_path)
    domains = load_klue_domains(limit_per_domain=limit_per_domain)
    counters: list[Counter] = [
        FastCounter("korbyte-128k", Tokenizer.from_file(str(tokenizer_path))),
        FastCounter(
            "kanana-2",
            Tokenizer.from_pretrained(KANANA_MODEL_ID, revision=KANANA_REVISION),
        ),
    ]
    unavailable: dict[str, str] = {}
    if include_morphological:
        optional, unavailable = _optional_counters()
        counters.extend(optional)

    results: dict[str, dict[str, dict[str, Any]]] = {}
    for counter in counters:
        results[counter.name] = {}
        for domain_name, texts in domains.items():
            metrics = _metrics(counter, texts, repeats=repeats)
            results[counter.name][domain_name] = asdict(metrics)
            print(
                f"{counter.name}/{domain_name}: {metrics.tokens:,} tokens, "
                f"{metrics.throughput_mib_s:.2f} MiB/s"
            )

    reductions = {
        domain: 100
        * (1 - results["korbyte-128k"][domain]["tokens"] / results["kanana-2"][domain]["tokens"])
        for domain in domains
    }
    macro_reduction = statistics.fmean(reductions.values())
    report = {
        "schema_version": 1,
        "created_at": datetime.now(UTC).isoformat(),
        "evaluation_dataset": {
            "id": "klue/klue",
            "revision": KLUE_REVISION,
            "split": "validation",
            "limit_per_domain": limit_per_domain,
            "text_examples_redistributed": False,
            "usage_note": (
                "KLUE text was excluded from tokenizer training and used only for "
                "intrinsic development evaluation."
            ),
        },
        "baseline": {
            "kanana_model_id": KANANA_MODEL_ID,
            "kanana_revision": KANANA_REVISION,
        },
        "repeats": repeats,
        "results": results,
        "korbyte_reduction_vs_kanana_percent": reductions,
        "korbyte_macro_reduction_vs_kanana_percent": macro_reduction,
        "compression_gate_percent": 5.0,
        "compression_gate_passed": macro_reduction >= 5.0,
        "unavailable_baselines": unavailable,
        "comparability_note": (
            "OKT and MeCab-ko are morphological analyzers, not reversible fixed-vocabulary "
            "LLM tokenizers. Kanana-2 is the primary like-for-like baseline."
        ),
        "environment": {
            "platform": platform.platform(),
            "processor": platform.processor(),
            "python": platform.python_version(),
            "logical_cpu_count": psutil.cpu_count(logical=True),
            "physical_memory_bytes": psutil.virtual_memory().total,
            "packages": _package_versions(),
        },
    }
    paths.benchmark_json.parent.mkdir(parents=True, exist_ok=True)
    paths.benchmark_json.write_text(
        json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
    )
    return report