File size: 4,389 Bytes
c5f5de4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Run pytest and assemble the W1-03 acceptance report from measured evidence."""
import argparse
import hashlib
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
from xml.etree import ElementTree

from .query_tools import TOOLS
from .snapshot import DATASETS


def read_evidence(folder, name):
    path = folder / f"{name}.json"
    return json.loads(path.read_bytes()) if path.is_file() else {}


def run(args):
    with tempfile.TemporaryDirectory(prefix="faisi-w103-") as folder:
        temp = Path(folder)
        junit = temp / "pytest.xml"
        env = os.environ.copy()
        env.update(
            FAISI_W103_SNAPSHOT_ROOT=str(args.snapshot_root.resolve()),
            FAISI_W103_WINDOW_INDEX=str(args.window_index.resolve()),
            FAISI_W103_TOKENIZER=str(args.tokenizer.resolve()),
            FAISI_W103_TEST_RESULTS=str(temp),
            TOKENIZERS_PARALLELISM="false", OPENBLAS_NUM_THREADS="1",
            OMP_NUM_THREADS="1", MKL_NUM_THREADS="1", ARROW_DEFAULT_MEMORY_POOL="system",
        )
        result = subprocess.run([sys.executable, "-m", "pytest", "-q", str(args.tests),
                                 "--junitxml", str(junit)], env=env, check=False)
        tests = errors = skipped = 0
        if junit.is_file():
            xml = ElementTree.parse(junit).getroot()
            for suite in xml.iter("testsuite"):
                tests += int(suite.attrib.get("tests", 0))
                errors += int(suite.attrib.get("failures", 0)) + int(suite.attrib.get("errors", 0))
                skipped += int(suite.attrib.get("skipped", 0))
        leakage = read_evidence(temp, "leakage")
        permutation = read_evidence(temp, "permutation")
        concurrency = read_evidence(temp, "concurrency")
    passed = tests - errors - skipped
    coverage = sorted(concurrency.get("dataset_counts", {}))
    return {
        "overall_pass": bool(
            result.returncode == 0 and tests > 0 and not errors and not skipped
            and set(coverage) == set(DATASETS)
            and leakage.get("raw_entity_name_leakage_instances") == 0
            and leakage.get("cases") == 50
            and permutation.get("verified", False)
            and concurrency.get("active_processes") == 32 and concurrency.get("calls") == 10000
            and concurrency.get("error_rate") == 0
            and concurrency.get("mean_latency_ms", float("inf")) < 3
            and concurrency.get("p99_latency_ms", float("inf")) < 10
            and max(leakage.get("max_response_tokens", 0), concurrency.get("max_response_tokens", 0)) <= 400
        ),
        "datasets_covered": [dataset for dataset in DATASETS if dataset in coverage],
        "pytest_passed_count": passed, "pytest_failed_count": errors, "pytest_skipped_count": skipped,
        "raw_entity_name_leakage_instances": leakage.get("raw_entity_name_leakage_instances"),
        "permutation_invariance_verified": permutation.get("verified", False),
        "concurrency_32proc_10k_calls": concurrency,
        "leakage_audit": leakage,
        "permutation_audit": permutation,
        "max_response_tokens": max(leakage.get("max_response_tokens", 0), concurrency.get("max_response_tokens", 0)),
        "tokenizer": {"name": "Qwen2.5-14B-Instruct", "sha256": hashlib.sha256(args.tokenizer.read_bytes()).hexdigest()},
    }


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--snapshot-root", required=True, type=Path)
    parser.add_argument("--window-index", required=True, type=Path)
    parser.add_argument("--tokenizer", required=True, type=Path)
    parser.add_argument("--tests", type=Path, default=Path("tests/test_sandbox.py"))
    parser.add_argument("--report", type=Path, required=True)
    parser.add_argument("--tools-schema", type=Path)
    args = parser.parse_args()
    report = run(args)
    args.report.parent.mkdir(parents=True, exist_ok=True)
    temp = args.report.with_suffix(".tmp")
    temp.write_text(json.dumps(report, indent=2) + "\n")
    temp.replace(args.report)
    if args.tools_schema:
        args.tools_schema.parent.mkdir(parents=True, exist_ok=True)
        args.tools_schema.write_text(json.dumps(TOOLS, indent=2) + "\n")
    print(json.dumps(report, indent=2))
    return 0 if report["overall_pass"] else 1


if __name__ == "__main__":
    raise SystemExit(main())