File size: 6,880 Bytes
f1a4107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Fail fast when an experiment mixes incompatible eval/calibration settings."""

from __future__ import annotations

import argparse
import hashlib
import json
from collections import defaultdict
from pathlib import Path


def sha256_file(path: Path) -> str:
    digest = hashlib.sha256()
    with open(path, "rb") as file:
        for chunk in iter(lambda: file.read(1 << 20), b""):
            digest.update(chunk)
    return digest.hexdigest()[:16]


def expected_eval(config: dict, corpus: str) -> dict:
    eval_cfg = config["eval"]
    corpus_path = Path(corpus)
    return {
        "corpus": str(corpus_path),
        "corpus_sha256": sha256_file(corpus_path),
        "max_sequences": eval_cfg["max_sequences"],
        "max_tokens": eval_cfg["max_tokens"],
        "min_tokens": eval_cfg["min_tokens"],
    }


def config_hash(value: dict) -> str:
    return hashlib.sha256(json.dumps(value, sort_keys=True).encode("utf-8")).hexdigest()[:16]


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--config", type=Path, required=True)
    parser.add_argument("--root", type=Path, default=None)
    parser.add_argument("--json-out", type=Path, default=None)
    args = parser.parse_args()

    config = json.loads(args.config.read_text())
    root = args.root or args.config.parent
    eval_cfg = config["eval"]
    calib_cfg = config["calibration"]
    corpora = {Path(path).name: path for path in eval_cfg["corpora"].values()}
    expected_by_name = {name: expected_eval(config, path) for name, path in corpora.items()}

    errors: list[str] = []
    warnings: list[str] = []
    ppl_rows = []
    baseline_by_corpus: dict[str, list[float]] = defaultdict(list)

    for path in sorted(root.rglob("ppl_*.json")):
        data = json.loads(path.read_text())
        corpus_name = Path(data.get("corpus", "")).name
        row = {"path": str(path), "corpus": corpus_name}
        ppl_rows.append(row)

        if corpus_name not in corpora:
            errors.append(f"{path}: unknown corpus {corpus_name!r}")
            continue

        expected = expected_by_name[corpus_name]
        if data.get("chunks") != expected["max_sequences"]:
            errors.append(
                f"{path}: chunks={data.get('chunks')} != config max_sequences={expected['max_sequences']}"
            )
        if data.get("tokens") is not None and data.get("chunks"):
            approx = data["chunks"] * expected["max_tokens"] - data["chunks"]
            if abs(int(data["tokens"]) - approx) > expected["max_sequences"]:
                warnings.append(
                    f"{path}: tokens={data.get('tokens')} differs from approx {approx}"
                )

        if data.get("config"):
            if data["config"] != expected:
                errors.append(f"{path}: embedded eval config differs from model config")
            if data.get("config_hash") != config_hash(expected):
                errors.append(f"{path}: config_hash mismatch")
        else:
            warnings.append(f"{path}: legacy result without config/config_hash")

        if "bf16_ppl" in data:
            baseline_by_corpus[corpus_name].append(float(data["bf16_ppl"]))

    for corpus_name, values in baseline_by_corpus.items():
        if max(values) - min(values) > 0.01:
            errors.append(
                f"BF16 baseline drift for {corpus_name}: {sorted(set(round(value, 4) for value in values))}"
            )

    calibration_rows = []
    calibration_corpora: set[Path] = set(
        Path(path) for path in calib_cfg.get("corpora", {}).values()
    )
    qad_corpus = config.get("qad", {}).get("corpus")
    if qad_corpus:
        calibration_corpora.add(Path(qad_corpus))
    for path in sorted(root.rglob("calibration.json")):
        data = json.loads(path.read_text())
        row = {"path": str(path), "corpus": Path(data.get("corpus", "")).name}
        calibration_rows.append(row)
        if data.get("corpus"):
            calibration_corpora.add(Path(data["corpus"]))
        checks = {
            "samples": calib_cfg["samples"],
            "max_tokens": calib_cfg["max_tokens"],
            "output_block": calib_cfg["output_block"],
        }
        for key, expected in checks.items():
            if data.get(key) != expected:
                errors.append(f"{path}: {key}={data.get(key)!r} != config {expected!r}")
        damping = float(data.get("damping", -1))
        if abs(damping - float(calib_cfg["damping"])) > 1e-12:
            errors.append(f"{path}: damping={damping} != config {calib_cfg['damping']}")

    eval_corpora = {Path(path) for path in corpora.values()}

    def long_lines(path: Path) -> set[str]:
        return {
            line.strip()
            for line in path.read_text(encoding="utf-8").splitlines()
            if len(line.strip()) >= 20
        }

    for eval_corpus in sorted(eval_corpora):
        if not eval_corpus.exists():
            continue
        lines = [
            line.strip()
            for line in eval_corpus.read_text(encoding="utf-8").splitlines()
            if len(line.strip()) >= 20
        ]
        duplicates = len(lines) - len(set(lines))
        if duplicates:
            errors.append(f"{eval_corpus}: {duplicates} duplicate long lines; dedup required")

    overlap_checks = 0
    for calibration_corpus in sorted(calibration_corpora):
        if not calibration_corpus.exists():
            errors.append(f"missing calibration corpus: {calibration_corpus}")
            continue
        for eval_corpus in sorted(eval_corpora):
            if not eval_corpus.exists():
                errors.append(f"missing eval corpus: {eval_corpus}")
                continue
            overlap_checks += 1
            if calibration_corpus.resolve() == eval_corpus.resolve():
                errors.append(f"calibration and eval use the same file: {eval_corpus}")
                continue
            overlap = long_lines(calibration_corpus) & long_lines(eval_corpus)
            if overlap:
                sample = next(iter(overlap))[:80]
                errors.append(
                    f"corpus overlap: {calibration_corpus} vs {eval_corpus} "
                    f"({len(overlap)} lines, e.g. {sample!r})"
                )

    report = {
        "config": str(args.config),
        "config_name": config.get("name"),
        "ppl_rows": len(ppl_rows),
        "calibration_rows": len(calibration_rows),
        "corpus_overlap_checks": overlap_checks,
        "errors": errors,
        "warnings": warnings,
        "ok": not errors,
    }
    text = json.dumps(report, indent=2, ensure_ascii=False)
    print(text)
    if args.json_out:
        args.json_out.parent.mkdir(parents=True, exist_ok=True)
        args.json_out.write_text(text + "\n")
    raise SystemExit(0 if not errors else 1)


if __name__ == "__main__":
    main()