File size: 5,045 Bytes
e593510 | 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 | #!/usr/bin/env python3
"""Validate ``results/<pipeline>/*.jsonl`` before publishing or opening a PR.
python tools/validate.py --benchmark parsebench results/datalab_accurate
Checks: canonical JSON per line, ids sorted and unique, every id exists in the
pinned data revision (``data_manifest.json`` next to ``results/``), required
fields, pipeline name matches the folder, and no private hosts, paths, run ids
or credentials anywhere in the file. Exit 1 on any finding.
Python 3.9+, standard library only.
"""
import argparse
import json
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from hfresults_common import ( # noqa: E402
BENCHMARKS,
canonical_dumps,
check_record,
foreign_hosts,
gate_scan,
load_data_manifest,
read_jsonl,
record_stem,
)
def validate_pipeline(benchmark: str, results_dir: str, manifest, *, max_report: int = 20):
spec = BENCHMARKS[benchmark]
pipeline = os.path.basename(os.path.normpath(results_dir))
findings = []
counts = {}
files = [c for c in spec["categories"] if os.path.exists(os.path.join(results_dir, c + ".jsonl"))]
if not files:
return [f"no <category>.jsonl in {results_dir}"], counts
for name in sorted(os.listdir(results_dir)):
if name.endswith(".jsonl") and name[:-6] not in spec["categories"]:
findings.append(f"unknown category file {name}")
for category in files:
path = os.path.join(results_dir, category + ".jsonl")
previous = None
seen = set()
n = 0
for number, line, record in read_jsonl(path):
n += 1
where = "%s:%d" % (path, number)
if canonical_dumps(record) != line:
findings.append(f"{where}: line is not canonical JSON (re-run pack.py)")
problems = check_record(record, benchmark=benchmark, pipeline=pipeline, category=category)
findings.extend(f"{where}: {p}" for p in problems)
if problems:
continue
_group, stem = record_stem(record)
example_id = record["request"]["example_id"]
if example_id in seen:
findings.append(f"{where}: duplicate id {example_id}")
seen.add(example_id)
if previous is not None and example_id < previous:
findings.append(f"{where}: ids not sorted ({example_id} after {previous})")
previous = example_id
if manifest is not None and stem not in manifest["_stems"].get(category, set()):
findings.append(
"{}: {} is not a document of {}@{}".format(
where, example_id, manifest["repo"], manifest["revision"][:8]
)
)
for pattern, snippet in gate_scan(line):
findings.append(f"{where}: private pattern {pattern}: {snippet!r}")
hosts = foreign_hosts(record)
if hosts:
findings.append(f"{where}: unexpected hosts outside model output: {hosts}")
if len(findings) > 5000:
break
counts[category] = n
return findings, counts
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--benchmark", required=True, choices=sorted(BENCHMARKS))
parser.add_argument("results_dirs", nargs="+", help="one or more results/<pipeline> folders")
parser.add_argument("--data-manifest", default=None, help="data_manifest.json (default: ../../data_manifest.json)")
parser.add_argument("--no-ids", action="store_true", help="skip the document id check")
parser.add_argument("--json", action="store_true", help="machine-readable summary")
args = parser.parse_args()
manifest = None
if not args.no_ids:
manifest_path = args.data_manifest or os.path.join(
os.path.dirname(os.path.normpath(args.results_dirs[0])), "..", "data_manifest.json"
)
if os.path.exists(manifest_path):
manifest = load_data_manifest(manifest_path)
else:
print(f"WARNING: no data_manifest.json at {manifest_path}; skipping id check", file=sys.stderr)
exit_code = 0
summary = {}
for results_dir in args.results_dirs:
findings, counts = validate_pipeline(args.benchmark, results_dir, manifest)
summary[results_dir] = {"documents": counts, "findings": len(findings), "first_findings": findings[:20]}
if findings:
exit_code = 1
if not args.json:
status = "FAIL" if findings else "ok"
print(f"{status} {results_dir} {json.dumps(counts)}")
for finding in findings[:20]:
print(" " + finding)
if len(findings) > 20:
print(" ... %d more" % (len(findings) - 20))
if args.json:
print(json.dumps(summary, indent=2))
return exit_code
if __name__ == "__main__":
sys.exit(main())
|